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

systemd / systemd / 20401947236

20 Dec 2025 09:56PM UTC coverage: 72.701% (+0.1%) from 72.578%
20401947236

push

github

DaanDeMeyer
core/socket: modernize listen/accept_in_cgroup

4 of 9 new or added lines in 1 file covered. (44.44%)

7723 existing lines in 114 files now uncovered.

309972 of 426363 relevant lines covered (72.7%)

1133403.64 hits per line

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

84.5
/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 "pidref.h"
19
#include "process-util.h"
20
#include "string-util.h"
21
#include "time-util.h"
22

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

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

32
        if (isempty(p))
442,754✔
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);
221,377✔
38
        if (dfd < 0)
221,377✔
39
                return dfd;
40

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

45
        fd = xopenat_lock_full(dfd,
221,377✔
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)
221,377✔
53
                return fd == -EAGAIN ? -EBUSY : fd;
3✔
54

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

62
        return 0;
221,374✔
63
}
64

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

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

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

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

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

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

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

91
        if (f->path) {
441,915✔
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 &&
220,907✔
98
                    (f->operation & ~LOCK_NB) == LOCK_SH &&
221,459✔
99
                    unposix_lock(f->fd, LOCK_EX|LOCK_NB) >= 0)
552✔
100
                        f->operation = LOCK_EX|LOCK_NB;
551✔
101

102
                if ((f->operation & ~LOCK_NB) == LOCK_EX)
220,907✔
103
                        (void) unlinkat(f->dir_fd, f->path, 0);
220,906✔
104

105
                f->path = mfree(f->path);
220,907✔
106
        }
107

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

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

116
        assert(fd >= 0);
241,279✔
117

118
        if (ofd)
241,279✔
119
                cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW;
240,898✔
120
        else
121
                cmd = (operation & LOCK_NB) ? F_SETLK : F_SETLKW;
381✔
122

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

137
        r = RET_NERRNO(fcntl(fd, cmd, &(struct flock) {
241,279✔
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)
241,279✔
UNCOV
152
                r = -EAGAIN;
×
153

154
        return r;
241,279✔
155
}
156

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

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

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

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

171
        (void) fcntl_lock(**fd, LOCK_UN, /* ofd= */ false);
147✔
172
        *fd = NULL;
147✔
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) {
277,761✔
186
        assert(fd >= 0);
277,761✔
187

188
        switch (type) {
277,761✔
189
        case LOCK_NONE:
190
                return 0;
191
        case LOCK_BSD:
2,557✔
192
                return RET_NERRNO(flock(fd, operation));
2,557✔
193
        case LOCK_POSIX:
1✔
194
                return posix_lock(fd, operation);
1✔
195
        case LOCK_UNPOSIX:
240,048✔
196
                return unposix_lock(fd, operation);
240,048✔
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) {
395✔
203
        int r;
395✔
204

205
        assert(fd >= 0);
395✔
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)
395✔
216
                return 0;
395✔
217
        if (!IN_SET(type, LOCK_BSD, LOCK_UNPOSIX)) /* Not for POSIX locks, see above. */
395✔
218
                return -EOPNOTSUPP;
219

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

225
        /* If that didn't work, try with a child */
226
        _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
372✔
227
        r = pidref_safe_fork("(sd-flock)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pidref);
372✔
228
        if (r < 0)
742✔
229
                return log_error_errno(r, "Failed to flock block device in child process: %m");
×
230
        if (r == 0) {
742✔
231
                struct sigevent sev = {
370✔
232
                        .sigev_notify = SIGEV_SIGNAL,
233
                        .sigev_signo = SIGALRM,
234
                };
235
                timer_t id;
370✔
236

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

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

245
                if (timer_settime(id, /* flags= */ 0, &its, NULL) < 0) {
370✔
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) {
370✔
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);
370✔
256
        }
257

258
        siginfo_t status;
372✔
259
        r = pidref_wait_for_terminate(&pidref, &status);
372✔
260
        if (r < 0)
372✔
261
                return r;
262

263
        pidref_done(&pidref);
372✔
264

265
        switch (status.si_code) {
372✔
266

267
        case CLD_EXITED:
370✔
268
                if (status.si_status != EXIT_SUCCESS)
370✔
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