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

systemd / systemd / 15986406979

30 Jun 2025 05:03PM UTC coverage: 72.045% (-0.09%) from 72.13%
15986406979

push

github

bluca
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options

ephemeral/ephemeral-import are described as possible '--mutable' options but
not present in the list. Note, "systemd-sysext --help" lists them correctly.

300514 of 417119 relevant lines covered (72.05%)

708586.28 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 <fcntl.h>
4
#include <signal.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <sys/file.h>
9
#include <unistd.h>
10

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

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

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

31
        if (isempty(p))
491,734✔
32
                return -EINVAL;
33

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

36
        dfd = fd_reopen(dir_fd, O_CLOEXEC|O_PATH|O_DIRECTORY);
245,867✔
37
        if (dfd < 0)
245,867✔
38
                return dfd;
39

40
        t = strdup(p);
245,867✔
41
        if (!t)
245,867✔
42
                return -ENOMEM;
43

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

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

61
        return 0;
245,864✔
62
}
63

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

68
        assert(p);
657✔
69
        assert(ret);
657✔
70

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

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

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

83
        return make_lock_file(t, operation, ret);
657✔
84
}
85

86
void release_lock_file(LockFile *f) {
491,027✔
87
        if (!f)
491,027✔
88
                return;
89

90
        if (f->path) {
491,027✔
91

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

96
                if (f->fd >= 0 &&
245,421✔
97
                    (f->operation & ~LOCK_NB) == LOCK_SH &&
245,887✔
98
                    unposix_lock(f->fd, LOCK_EX|LOCK_NB) >= 0)
466✔
99
                        f->operation = LOCK_EX|LOCK_NB;
465✔
100

101
                if ((f->operation & ~LOCK_NB) == LOCK_EX)
245,421✔
102
                        (void) unlinkat(f->dir_fd, f->path, 0);
245,420✔
103

104
                f->path = mfree(f->path);
245,421✔
105
        }
106

107
        f->dir_fd = safe_close(f->dir_fd);
491,027✔
108
        f->fd = safe_close(f->fd);
491,027✔
109
        f->operation = 0;
491,027✔
110
}
111

112
static int fcntl_lock(int fd, int operation, bool ofd) {
262,783✔
113
        int cmd, type, r;
262,783✔
114

115
        assert(fd >= 0);
262,783✔
116

117
        if (ofd)
262,783✔
118
                cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW;
262,366✔
119
        else
120
                cmd = (operation & LOCK_NB) ? F_SETLK : F_SETLKW;
417✔
121

122
        switch (operation & ~LOCK_NB) {
262,783✔
123
                case LOCK_EX:
124
                        type = F_WRLCK;
125
                        break;
126
                case LOCK_SH:
502✔
127
                        type = F_RDLCK;
502✔
128
                        break;
502✔
129
                case LOCK_UN:
208✔
130
                        type = F_UNLCK;
208✔
131
                        break;
208✔
132
                default:
×
133
                        assert_not_reached();
×
134
        }
135

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

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

153
        return r;
262,783✔
154
}
155

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

160
int unposix_lock(int fd, int operation) {
262,366✔
161
        return fcntl_lock(fd, operation, /*ofd=*/ true);
262,366✔
162
}
163

164
void posix_unlockpp(int **fd) {
152✔
165
        assert(fd);
152✔
166

167
        if (!*fd || **fd < 0)
152✔
168
                return;
169

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

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

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

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

184
int lock_generic(int fd, LockType type, int operation) {
298,927✔
185
        assert(fd >= 0);
298,927✔
186

187
        switch (type) {
298,927✔
188
        case LOCK_NONE:
189
                return 0;
190
        case LOCK_BSD:
2,994✔
191
                return RET_NERRNO(flock(fd, operation));
2,994✔
192
        case LOCK_POSIX:
1✔
193
                return posix_lock(fd, operation);
1✔
194
        case LOCK_UNPOSIX:
261,590✔
195
                return unposix_lock(fd, operation);
261,590✔
196
        default:
×
197
                assert_not_reached();
×
198
        }
199
}
200

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

205
        assert(fd >= 0);
457✔
206

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

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

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

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

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

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

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

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

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

255
                _exit(EXIT_SUCCESS);
447✔
256
        }
257

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

263
        TAKE_PID(pid);
449✔
264

265
        switch (status.si_code) {
449✔
266

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

271
                return 0;
272

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

277
                _fallthrough_;
278

279
        case CLD_DUMPED:
280
                return -EPROTO;
281

282
        default:
×
283
                assert_not_reached();
×
284
        }
285
}
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