• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/basic/lock-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <signal.h>
6
#include <stdio.h>
7
#include <string.h>
8
#include <sys/file.h>
9
#include <sys/stat.h>
10
#include <sys/wait.h>
11

12
#include "alloc-util.h"
13
#include "errno-util.h"
14
#include "fd-util.h"
15
#include "fs-util.h"
16
#include "lock-util.h"
17
#include "log.h"
18
#include "macro.h"
19
#include "missing_fcntl.h"
20
#include "path-util.h"
21
#include "process-util.h"
22

23
int make_lock_file_at(int dir_fd, const char *p, int operation, LockFile *ret) {
243,415✔
24
        _cleanup_close_ int fd = -EBADF, dfd = -EBADF;
243,415✔
25
        _cleanup_free_ char *t = NULL;
243,415✔
26

27
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
243,415✔
28
        assert(p);
243,415✔
29
        assert(IN_SET(operation & ~LOCK_NB, LOCK_EX, LOCK_SH));
243,415✔
30
        assert(ret);
243,415✔
31

32
        if (isempty(p))
486,830✔
33
                return -EINVAL;
34

35
        /* We use UNPOSIX locks as they have nice semantics, and are mostly compatible with NFS. */
36

37
        dfd = fd_reopen(dir_fd, O_CLOEXEC|O_PATH|O_DIRECTORY);
243,415✔
38
        if (dfd < 0)
243,415✔
39
                return dfd;
40

41
        t = strdup(p);
243,415✔
42
        if (!t)
243,415✔
43
                return -ENOMEM;
44

45
        fd = xopenat_lock_full(dfd,
243,415✔
46
                               p,
47
                               O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY,
48
                               /* xopen_flags = */ 0,
49
                               0600,
50
                               LOCK_UNPOSIX,
51
                               operation);
52
        if (fd < 0)
243,415✔
53
                return fd == -EAGAIN ? -EBUSY : fd;
3✔
54

55
        *ret = (LockFile) {
243,412✔
56
                .dir_fd = TAKE_FD(dfd),
243,412✔
57
                .path = TAKE_PTR(t),
243,412✔
58
                .fd = TAKE_FD(fd),
243,412✔
59
                .operation = operation,
60
        };
61

62
        return 0;
243,412✔
63
}
64

65
int make_lock_file_for(const char *p, int operation, LockFile *ret) {
616✔
66
        _cleanup_free_ char *fn = NULL, *dn = NULL, *t = NULL;
616✔
67
        int r;
616✔
68

69
        assert(p);
616✔
70
        assert(ret);
616✔
71

72
        r = path_extract_filename(p, &fn);
616✔
73
        if (r < 0)
616✔
74
                return r;
75

76
        r = path_extract_directory(p, &dn);
616✔
77
        if (r < 0)
616✔
78
                return r;
79

80
        t = strjoin(dn, "/.#", fn, ".lck");
616✔
81
        if (!t)
616✔
82
                return -ENOMEM;
83

84
        return make_lock_file(t, operation, ret);
616✔
85
}
86

87
void release_lock_file(LockFile *f) {
486,203✔
88
        if (!f)
486,203✔
89
                return;
90

91
        if (f->path) {
486,203✔
92

93
                /* If we are the exclusive owner we can safely delete
94
                 * the lock file itself. If we are not the exclusive
95
                 * owner, we can try becoming it. */
96

97
                if (f->fd >= 0 &&
243,001✔
98
                    (f->operation & ~LOCK_NB) == LOCK_SH &&
243,449✔
99
                    unposix_lock(f->fd, LOCK_EX|LOCK_NB) >= 0)
448✔
100
                        f->operation = LOCK_EX|LOCK_NB;
447✔
101

102
                if ((f->operation & ~LOCK_NB) == LOCK_EX)
243,001✔
103
                        (void) unlinkat(f->dir_fd, f->path, 0);
243,000✔
104

105
                f->path = mfree(f->path);
243,001✔
106
        }
107

108
        f->dir_fd = safe_close(f->dir_fd);
486,203✔
109
        f->fd = safe_close(f->fd);
486,203✔
110
        f->operation = 0;
486,203✔
111
}
112

113
static int fcntl_lock(int fd, int operation, bool ofd) {
263,574✔
114
        int cmd, type, r;
263,574✔
115

116
        assert(fd >= 0);
263,574✔
117

118
        if (ofd)
263,574✔
119
                cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW;
263,173✔
120
        else
121
                cmd = (operation & LOCK_NB) ? F_SETLK : F_SETLKW;
401✔
122

123
        switch (operation & ~LOCK_NB) {
263,574✔
124
                case LOCK_EX:
125
                        type = F_WRLCK;
126
                        break;
127
                case LOCK_SH:
484✔
128
                        type = F_RDLCK;
484✔
129
                        break;
484✔
130
                case LOCK_UN:
200✔
131
                        type = F_UNLCK;
200✔
132
                        break;
200✔
133
                default:
×
UNCOV
134
                        assert_not_reached();
×
135
        }
136

137
        r = RET_NERRNO(fcntl(fd, cmd, &(struct flock) {
263,574✔
138
                .l_type = type,
139
                .l_whence = SEEK_SET,
140
                .l_start = 0,
141
                .l_len = 0,
142
        }));
143

144
        /* If we are doing non-blocking operations, treat EACCES/EAGAIN the same as per man page. But if
145
         * not, propagate EACCES back, as it will likely be due to an LSM denying the operation (for example
146
         * LXC with AppArmor when running on kernel < 6.2), and in some cases we want to gracefully
147
         * fallback (e.g.: PrivateNetwork=yes). As per documentation, it's only the non-blocking operation
148
         * F_SETLK that might return EACCES on some platforms (although the Linux implementation doesn't
149
         * seem to), as F_SETLKW and F_OFD_SETLKW block so this is not an issue, and F_OFD_SETLK is documented
150
         * to only return EAGAIN if the lock is already held. */
151
        if ((operation & LOCK_NB) && r == -EACCES)
263,574✔
UNCOV
152
                r = -EAGAIN;
×
153

154
        return r;
263,574✔
155
}
156

157
int posix_lock(int fd, int operation) {
257✔
158
        return fcntl_lock(fd, operation, /*ofd=*/ false);
257✔
159
}
160

161
int unposix_lock(int fd, int operation) {
263,173✔
162
        return fcntl_lock(fd, operation, /*ofd=*/ true);
263,173✔
163
}
164

165
void posix_unlockpp(int **fd) {
144✔
166
        assert(fd);
144✔
167

168
        if (!*fd || **fd < 0)
144✔
169
                return;
170

171
        (void) fcntl_lock(**fd, LOCK_UN, /*ofd=*/ false);
144✔
172
        *fd = NULL;
144✔
173
}
174

175
void unposix_unlockpp(int **fd) {
×
UNCOV
176
        assert(fd);
×
177

UNCOV
178
        if (!*fd || **fd < 0)
×
179
                return;
180

181
        (void) fcntl_lock(**fd, LOCK_UN, /*ofd=*/ true);
×
UNCOV
182
        *fd = NULL;
×
183
}
184

185
int lock_generic(int fd, LockType type, int operation) {
300,031✔
186
        assert(fd >= 0);
300,031✔
187

188
        switch (type) {
300,031✔
189
        case LOCK_NONE:
190
                return 0;
191
        case LOCK_BSD:
3,142✔
192
                return RET_NERRNO(flock(fd, operation));
3,142✔
193
        case LOCK_POSIX:
1✔
194
                return posix_lock(fd, operation);
1✔
195
        case LOCK_UNPOSIX:
262,444✔
196
                return unposix_lock(fd, operation);
262,444✔
197
        default:
×
UNCOV
198
                assert_not_reached();
×
199
        }
200
}
201

202
int lock_generic_with_timeout(int fd, LockType type, int operation, usec_t timeout) {
535✔
203
        _cleanup_(sigkill_waitp) pid_t pid = 0;
535✔
204
        int r;
535✔
205

206
        assert(fd >= 0);
535✔
207

208
        /* A version of lock_generic(), but with a timeout. We do this in a child process, since the kernel
209
         * APIs natively don't support a timeout. We set a SIGALRM timer that will kill the child after the
210
         * timeout is hit. Returns -ETIMEDOUT if the timeout is hit, and 0 on success.
211
         *
212
         * This only works for BSD and UNPOSIX locks, as only those are fd-bound, and hence can be acquired
213
         * from any process that has access to the fd. POSIX locks OTOH are process-bound, and hence if we'd
214
         * acquire them in a child process they'd remain unlocked in the parent. */
215

216
        if (type == LOCK_NONE)
535✔
217
                return 0;
218
        if (!IN_SET(type, LOCK_BSD, LOCK_UNPOSIX)) /* Not for POSIX locks, see above. */
535✔
219
                return -EOPNOTSUPP;
220

221
        /* First, try without forking anything off */
222
        r = lock_generic(fd, type, operation | (timeout == USEC_INFINITY ? 0 : LOCK_NB));
1,070✔
223
        if (r != -EAGAIN || timeout == 0 || FLAGS_SET(operation, LOCK_NB))
535✔
224
                return r;
225

226
        /* If that didn't work, try with a child */
227

228
        r = safe_fork("(sd-flock)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pid);
525✔
229
        if (r < 0)
1,048✔
UNCOV
230
                return log_error_errno(r, "Failed to flock block device in child process: %m");
×
231
        if (r == 0) {
1,048✔
232
                struct sigevent sev = {
523✔
233
                        .sigev_notify = SIGEV_SIGNAL,
234
                        .sigev_signo = SIGALRM,
235
                };
236
                timer_t id;
523✔
237

238
                if (timer_create(CLOCK_MONOTONIC, &sev, &id) < 0) {
523✔
239
                        log_error_errno(errno, "Failed to allocate CLOCK_MONOTONIC timer: %m");
×
UNCOV
240
                        _exit(EXIT_FAILURE);
×
241
                }
242

243
                struct itimerspec its = {};
523✔
244
                timespec_store(&its.it_value, timeout);
523✔
245

246
                if (timer_settime(id, /* flags= */ 0, &its, NULL) < 0) {
523✔
247
                        log_error_errno(errno, "Failed to start CLOCK_MONOTONIC timer: %m");
×
UNCOV
248
                        _exit(EXIT_FAILURE);
×
249
                }
250

251
                if (lock_generic(fd, type, operation) < 0) {
523✔
252
                        log_error_errno(errno, "Unable to get an exclusive lock on the device: %m");
×
UNCOV
253
                        _exit(EXIT_FAILURE);
×
254
                }
255

256
                _exit(EXIT_SUCCESS);
523✔
257
        }
258

259
        siginfo_t status;
525✔
260
        r = wait_for_terminate(pid, &status);
525✔
261
        if (r < 0)
525✔
262
                return r;
263

264
        TAKE_PID(pid);
525✔
265

266
        switch (status.si_code) {
525✔
267

268
        case CLD_EXITED:
523✔
269
                if (status.si_status != EXIT_SUCCESS)
523✔
UNCOV
270
                        return -EPROTO;
×
271

272
                return 0;
273

274
        case CLD_KILLED:
2✔
275
                if (status.si_status == SIGALRM)
2✔
276
                        return -ETIMEDOUT;
2✔
277

278
                _fallthrough_;
279

280
        case CLD_DUMPED:
281
                return -EPROTO;
282

283
        default:
×
UNCOV
284
                assert_not_reached();
×
285
        }
286
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc