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

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

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

87.5
/src/basic/pidref.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "errno-util.h"
4
#include "fd-util.h"
5
#include "log.h"
6
#include "missing_syscall.h"
7
#include "missing_wait.h"
8
#include "parse-util.h"
9
#include "pidfd-util.h"
10
#include "pidref.h"
11
#include "process-util.h"
12
#include "signal-util.h"
13

14
int pidref_acquire_pidfd_id(PidRef *pidref) {
10,726✔
15
        int r;
10,726✔
16

17
        assert(pidref);
10,726✔
18

19
        if (!pidref_is_set(pidref))
10,726✔
20
                return -ESRCH;
21

22
        if (pidref_is_remote(pidref))
10,726✔
23
                return -EREMOTE;
24

25
        if (pidref->fd < 0)
10,726✔
26
                return -ENOMEDIUM;
27

28
        if (pidref->fd_id > 0)
10,718✔
29
                return 0;
30

31
        r = pidfd_get_inode_id(pidref->fd, &pidref->fd_id);
5,290✔
32
        if (r < 0) {
5,290✔
33
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
34
                        log_debug_errno(r, "Failed to get inode number of pidfd for pid " PID_FMT ": %m",
×
35
                                        pidref->pid);
36
                return r;
×
37
        }
38

39
        return 0;
40
}
41

42
bool pidref_equal(PidRef *a, PidRef *b) {
20,376✔
43

44
        /* If this is the very same structure, it definitely refers to the same process */
45
        if (a == b)
20,376✔
46
                return true;
47

48
        if (!pidref_is_set(a))
20,373✔
49
                return !pidref_is_set(b);
6,054✔
50

51
        if (!pidref_is_set(b))
17,346✔
52
                return false;
53

54
        if (a->pid != b->pid)
17,340✔
55
                return false;
56

57
        if (pidref_is_remote(a)) {
3,767✔
58
                /* If one is remote and the other isn't, they are not the same */
59
                if (!pidref_is_remote(b))
×
60
                        return false;
61

62
                /* If both are remote, compare fd IDs if we have both, otherwise don't bother, and cut things short */
63
                if (a->fd_id == 0 || b->fd_id == 0)
×
64
                        return true;
65
        } else {
66
                /* If the other side is remote, then this is not the same */
67
                if (pidref_is_remote(b))
3,767✔
68
                        return false;
69

70
                /* PID1 cannot exit, hence it cannot change pidfs ids, hence no point in comparing them, we
71
                 * can shortcut things */
72
                if (a->pid == 1)
3,766✔
73
                        return true;
74

75
                /* Try to compare pidfds using their inode numbers. This way we can ensure that we
76
                 * don't spuriously consider two PidRefs equal if the pid has been reused once. Note
77
                 * that we ignore all errors here, not only EOPNOTSUPP, as fstat() might fail due to
78
                 * many reasons. */
79
                if (pidref_acquire_pidfd_id(a) < 0 || pidref_acquire_pidfd_id(b) < 0)
3,648✔
80
                        return true;
2✔
81
        }
82

83
        return a->fd_id == b->fd_id;
3,646✔
84
}
85

86
int pidref_set_pid(PidRef *pidref, pid_t pid) {
61,070✔
87
        uint64_t pidfdid = 0;
61,070✔
88
        int fd;
61,070✔
89

90
        assert(pidref);
61,070✔
91

92
        if (pid < 0)
61,070✔
93
                return -ESRCH;
61,070✔
94
        if (pid == 0) {
61,070✔
95
                pid = getpid_cached();
37,795✔
96
                (void) pidfd_get_inode_id_self_cached(&pidfdid);
37,795✔
97
        }
98

99
        fd = pidfd_open(pid, 0);
61,070✔
100
        if (fd < 0) {
61,070✔
101
                /* Graceful fallback in case the kernel is out of fds */
102
                if (!ERRNO_IS_RESOURCE(errno))
×
103
                        return log_debug_errno(errno, "Failed to open pidfd for pid " PID_FMT ": %m", pid);
×
104

105
                fd = -EBADF;
106
        }
107

108
        *pidref = (PidRef) {
61,070✔
109
                .fd = fd,
110
                .pid = pid,
111
                .fd_id = pidfdid,
112
        };
113

114
        return 0;
61,070✔
115
}
116

117
int pidref_set_pidstr(PidRef *pidref, const char *pid) {
4✔
118
        pid_t nr;
4✔
119
        int r;
4✔
120

121
        assert(pidref);
4✔
122

123
        r = parse_pid(pid, &nr);
4✔
124
        if (r < 0)
4✔
125
                return r;
4✔
126

127
        return pidref_set_pid(pidref, nr);
4✔
128
}
129

130
int pidref_set_pidfd(PidRef *pidref, int fd) {
607✔
131
        int r;
607✔
132

133
        assert(pidref);
607✔
134

135
        if (fd < 0)
607✔
136
                return -EBADF;
137

138
        int fd_copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
607✔
139
        if (fd_copy < 0) {
607✔
140
                pid_t pid;
×
141

142
                if (!ERRNO_IS_RESOURCE(errno))
×
143
                        return -errno;
×
144

145
                /* Graceful fallback if we are out of fds */
146
                r = pidfd_get_pid(fd, &pid);
×
147
                if (r < 0)
×
148
                        return r;
149

150
                *pidref = PIDREF_MAKE_FROM_PID(pid);
×
151
                return 0;
×
152
        }
153

154
        return pidref_set_pidfd_consume(pidref, fd_copy);
607✔
155
}
156

157
int pidref_set_pidfd_take(PidRef *pidref, int fd) {
2,898✔
158
        pid_t pid;
2,898✔
159
        int r;
2,898✔
160

161
        assert(pidref);
2,898✔
162

163
        if (fd < 0)
2,898✔
164
                return -EBADF;
2,898✔
165

166
        r = pidfd_get_pid(fd, &pid);
2,898✔
167
        if (r < 0)
2,898✔
168
                return r;
169

170
        *pidref = (PidRef) {
2,898✔
171
                .fd = fd,
172
                .pid = pid,
173
        };
174

175
        return 0;
2,898✔
176
}
177

178
int pidref_set_pidfd_consume(PidRef *pidref, int fd) {
2,869✔
179
        int r;
2,869✔
180

181
        r = pidref_set_pidfd_take(pidref, fd);
2,869✔
182
        if (r < 0)
2,869✔
183
                safe_close(fd);
×
184

185
        return r;
2,869✔
186
}
187

188
int pidref_set_parent(PidRef *ret) {
1,130✔
189
        _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
1,130✔
190
        pid_t ppid;
1,130✔
191
        int r;
1,130✔
192

193
        assert(ret);
1,130✔
194

195
        /* Acquires a pidref to our parent process. Deals with the fact that parent processes might exit, and
196
         * we get reparented to other processes, with our old parent's PID already being recycled. */
197

198
        ppid = getppid();
1,130✔
199
        for (;;) {
1,130✔
200
                r = pidref_set_pid(&parent, ppid);
1,130✔
201
                if (r < 0)
1,130✔
202
                        return r;
203

204
                if (parent.fd < 0) /* If pidfds are not available, then we are done */
1,130✔
205
                        break;
206

207
                pid_t now_ppid = getppid();
1,130✔
208
                if (now_ppid == ppid) /* If our ppid is still the same, then we are done */
1,130✔
209
                        break;
210

211
                /* Otherwise let's try again with the new ppid */
212
                ppid = now_ppid;
×
213
                pidref_done(&parent);
×
214
        }
215

216
        *ret = TAKE_PIDREF(parent);
1,130✔
217
        return 0;
1,130✔
218
}
219

220
void pidref_done(PidRef *pidref) {
216,517✔
221
        assert(pidref);
216,517✔
222

223
        *pidref = (PidRef) {
433,034✔
224
                .fd = safe_close(pidref->fd),
216,517✔
225
        };
226
}
216,517✔
227

228
PidRef* pidref_free(PidRef *pidref) {
2,921✔
229
        /* Regularly, this is an embedded structure. But sometimes we want it on the heap too */
230
        if (!pidref)
2,921✔
231
                return NULL;
232

233
        pidref_done(pidref);
2,921✔
234
        return mfree(pidref);
2,921✔
235
}
236

237
int pidref_copy(const PidRef *pidref, PidRef *ret) {
2,922✔
238
        _cleanup_(pidref_done) PidRef copy = PIDREF_NULL;
2,922✔
239

240
        /* If NULL is passed we'll generate a PidRef that refers to no process. This makes it easy to
241
         * copy pidref fields that might or might not reference a process yet. */
242

243
        assert(ret);
2,922✔
244

245
        if (pidref) {
2,922✔
246
                if (pidref_is_remote(pidref)) /* Propagate remote flag */
2,920✔
247
                        copy.fd = -EREMOTE;
×
248
                else if (pidref->fd >= 0) {
2,920✔
249
                        copy.fd = fcntl(pidref->fd, F_DUPFD_CLOEXEC, 3);
2,914✔
250
                        if (copy.fd < 0) {
2,914✔
251
                                if (!ERRNO_IS_RESOURCE(errno))
×
252
                                        return -errno;
×
253

254
                                copy.fd = -EBADF;
×
255
                        }
256
                }
257

258
                copy.pid = pidref->pid;
2,920✔
259
                copy.fd_id = pidref->fd_id;
2,920✔
260
        }
261

262
        *ret = TAKE_PIDREF(copy);
2,922✔
263
        return 0;
2,922✔
264
}
265

266
int pidref_dup(const PidRef *pidref, PidRef **ret) {
2,918✔
267
        _cleanup_(pidref_freep) PidRef *dup_pidref = NULL;
2,918✔
268
        int r;
2,918✔
269

270
        /* Allocates a new PidRef on the heap, making it a copy of the specified pidref. This does not try to
271
         * acquire a pidfd if we don't have one yet! */
272

273
        assert(ret);
2,918✔
274

275
        dup_pidref = newdup(PidRef, &PIDREF_NULL, 1);
2,918✔
276
        if (!dup_pidref)
2,918✔
277
                return -ENOMEM;
278

279
        r = pidref_copy(pidref, dup_pidref);
2,918✔
280
        if (r < 0)
2,918✔
281
                return r;
282

283
        *ret = TAKE_PTR(dup_pidref);
2,918✔
284
        return 0;
2,918✔
285
}
286

287
int pidref_new_from_pid(pid_t pid, PidRef **ret) {
4✔
288
        _cleanup_(pidref_freep) PidRef *n = NULL;
4✔
289
        int r;
4✔
290

291
        assert(ret);
4✔
292

293
        if (pid < 0)
4✔
294
                return -ESRCH;
295

296
        n = new(PidRef, 1);
3✔
297
        if (!n)
3✔
298
                return -ENOMEM;
299

300
        *n = PIDREF_NULL;
3✔
301

302
        r = pidref_set_pid(n, pid);
3✔
303
        if (r < 0)
3✔
304
                return r;
305

306
        *ret = TAKE_PTR(n);
3✔
307
        return 0;
3✔
308
}
309

310
int pidref_kill(const PidRef *pidref, int sig) {
15,576✔
311

312
        if (!pidref)
15,576✔
313
                return -ESRCH;
314

315
        if (pidref_is_remote(pidref))
15,576✔
316
                return -EREMOTE;
317

318
        if (pidref->fd >= 0)
15,574✔
319
                return RET_NERRNO(pidfd_send_signal(pidref->fd, sig, NULL, 0));
7,970✔
320

321
        if (pidref->pid > 0)
7,604✔
322
                return RET_NERRNO(kill(pidref->pid, sig));
14,108✔
323

324
        return -ESRCH;
325
}
326

327
int pidref_kill_and_sigcont(const PidRef *pidref, int sig) {
593✔
328
        int r;
593✔
329

330
        r = pidref_kill(pidref, sig);
593✔
331
        if (r < 0)
593✔
332
                return r;
333

334
        if (!IN_SET(sig, SIGCONT, SIGKILL))
592✔
335
                (void) pidref_kill(pidref, SIGCONT);
592✔
336

337
        return 0;
338
}
339

340
int pidref_sigqueue(const PidRef *pidref, int sig, int value) {
9,778✔
341

342
        if (!pidref)
9,778✔
343
                return -ESRCH;
344

345
        if (pidref_is_remote(pidref))
9,778✔
346
                return -EREMOTE;
347

348
        if (pidref->fd >= 0) {
9,778✔
349
                siginfo_t si;
9,778✔
350

351
                /* We can't use structured initialization here, since the structure contains various unions
352
                 * and these fields lie in overlapping (carefully aligned) unions that LLVM is allergic to
353
                 * allow assignments to */
354
                zero(si);
9,778✔
355
                si.si_signo = sig;
9,778✔
356
                si.si_code = SI_QUEUE;
9,778✔
357
                si.si_pid = getpid_cached();
9,778✔
358
                si.si_uid = getuid();
9,778✔
359
                si.si_value.sival_int = value;
9,778✔
360

361
                return RET_NERRNO(pidfd_send_signal(pidref->fd, sig, &si, 0));
9,778✔
362
        }
363

364
        if (pidref->pid > 0)
×
365
                return RET_NERRNO(sigqueue(pidref->pid, sig, (const union sigval) { .sival_int = value }));
×
366

367
        return -ESRCH;
368
}
369

370
int pidref_verify(const PidRef *pidref) {
65,194✔
371
        int r;
65,194✔
372

373
        /* This is a helper that is supposed to be called after reading information from procfs via a
374
         * PidRef. It ensures that the PID we track still matches the PIDFD we pin. If this value differs
375
         * after a procfs read, we might have read the data from a recycled PID. */
376

377
        if (!pidref_is_set(pidref))
65,194✔
378
                return -ESRCH;
379

380
        if (pidref_is_remote(pidref))
65,192✔
381
                return -EREMOTE;
382

383
        if (pidref->pid == 1)
65,191✔
384
                return 1; /* PID 1 can never go away, hence never be recycled to a different process → return 1 */
385

386
        if (pidref->fd < 0)
50,813✔
387
                return 0; /* If we don't have a pidfd we cannot validate it, hence we assume it's all OK → return 0 */
388

389
        r = pidfd_verify_pid(pidref->fd, pidref->pid);
33,017✔
390
        if (r < 0)
33,017✔
391
                return r;
2,076✔
392

393
        return 1; /* We have a pidfd and it still points to the PID we have, hence all is *really* OK → return 1 */
394
}
395

396
bool pidref_is_self(PidRef *pidref) {
38,084✔
397
        if (!pidref_is_set(pidref))
38,084✔
398
                return false;
38,084✔
399

400
        if (pidref_is_remote(pidref))
38,082✔
401
                return false;
402

403
        if (pidref->pid != getpid_cached())
38,082✔
404
                return false;
405

406
        /* PID1 cannot exit, hence no point in comparing pidfd IDs, they can never change */
407
        if (pidref->pid == 1)
657✔
408
                return true;
409

410
        /* Also compare pidfd ID if we can get it */
411
        if (pidref_acquire_pidfd_id(pidref) < 0)
657✔
412
                return true;
413

414
        uint64_t self_id;
651✔
415
        if (pidfd_get_inode_id_self_cached(&self_id) < 0)
651✔
416
                return true;
417

418
        return pidref->fd_id == self_id;
651✔
419
}
420

421
int pidref_wait(PidRef *pidref, siginfo_t *ret, int options) {
13,849✔
422
        int r;
13,849✔
423

424
        if (!pidref_is_set(pidref))
13,849✔
425
                return -ESRCH;
13,849✔
426

427
        if (pidref_is_remote(pidref))
13,849✔
428
                return -EREMOTE;
429

430
        if (pidref->pid == 1 || pidref_is_self(pidref))
13,848✔
431
                return -ECHILD;
×
432

433
        siginfo_t si = {};
13,848✔
434
        if (pidref->fd >= 0)
13,848✔
435
                r = RET_NERRNO(waitid(P_PIDFD, pidref->fd, &si, options));
4,998✔
436
        else
437
                r = RET_NERRNO(waitid(P_PID, pidref->pid, &si, options));
8,850✔
438
        if (r < 0)
×
439
                return r;
440

441
        if (ret)
13,848✔
442
                *ret = si;
9,858✔
443

444
        return 0;
445
}
446

447
int pidref_wait_for_terminate(PidRef *pidref, siginfo_t *ret) {
13,849✔
448
        int r;
13,849✔
449

450
        for (;;) {
13,849✔
451
                r = pidref_wait(pidref, ret, WEXITED);
13,849✔
452
                if (r != -EINTR)
13,849✔
453
                        return r;
13,849✔
454
        }
455
}
456

457
bool pidref_is_automatic(const PidRef *pidref) {
182,996✔
458
        return pidref && pid_is_automatic(pidref->pid);
182,996✔
459
}
460

461
void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
84,702✔
462
        siphash24_compress_typesafe(pidref->pid, state);
84,702✔
463
}
84,702✔
464

465
int pidref_compare_func(const PidRef *a, const PidRef *b) {
91,472✔
466
        int r;
91,472✔
467

468
        assert(a);
91,472✔
469
        assert(b);
91,472✔
470

471
        r = CMP(pidref_is_set(a), pidref_is_set(b));
274,416✔
472
        if (r != 0)
91,472✔
473
                return r;
×
474

475
        r = CMP(pidref_is_automatic(a), pidref_is_automatic(b));
91,472✔
476
        if (r != 0)
91,472✔
477
                return r;
×
478

479
        r = CMP(pidref_is_remote(a), pidref_is_remote(b));
182,944✔
480
        if (r != 0)
91,472✔
481
                return r;
×
482

483
        r = CMP(a->pid, b->pid);
91,472✔
484
        if (r != 0)
70,616✔
485
                return r;
23,890✔
486

487
        if (a->fd_id != 0 && b->fd_id != 0)
67,582✔
488
                return CMP(a->fd_id, b->fd_id);
506✔
489

490
        return 0;
491
}
492

493
DEFINE_HASH_OPS(pidref_hash_ops, PidRef, pidref_hash_func, pidref_compare_func);
494

495
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(pidref_hash_ops_free,
×
496
                                    PidRef, pidref_hash_func, pidref_compare_func,
497
                                    pidref_free);
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