Debian Codegraph @

src/libsystemd/sd-daemon/sd-daemon.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/magic.h>
4 #include <mqueue.h>
5 #include <netinet/in.h>
6 #include <poll.h>
7 #include <stdio.h>
8 #include <sys/stat.h>
9 #include <sys/un.h>
10 #include <unistd.h>
11
12 #include "sd-daemon.h"
13
14 #include "alloc-util.h"
15 #include "errno-util.h"
16 #include "escape.h"
17 #include "extract-word.h"
18 #include "fd-util.h"
19 #include "fs-util.h"
20 #include "io-util.h"
21 #include "iovec-util.h"
22 #include "log.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "pidfd-util.h"
26 #include "process-util.h"
27 #include "socket-util.h"
28 #include "stat-util.h"
29 #include "strv.h"
30 #include "time-util.h"
31
32 #define SNDBUF_SIZE (8*1024*1024)
33
34 static void unsetenv_listen(bool unset_environment) {
35 if (!unset_environment)
36 return;
37
38 assert_se(unsetenv("LISTEN_PID") == 0);
39 assert_se(unsetenv("LISTEN_PIDFDID") == 0);
40 assert_se(unsetenv("LISTEN_FDS") == 0);
41 assert_se(unsetenv("LISTEN_FDNAMES") == 0);
42 }
43
44 _public_ int sd_listen_fds(int unset_environment) {
45 const char *e;
46 int n, r;
47 pid_t pid;
48
49 e = getenv("LISTEN_PID");
50 if (!e) {
51 r = 0;
52 goto finish;
53 }
54
55 r = parse_pid(e, &pid);
56 if (r < 0)
57 goto finish;
58
59 /* Is this for us? */
60 if (getpid_cached() != pid) {
61 r = 0;
62 goto finish;
63 }
64
65 e = getenv("LISTEN_PIDFDID");
66 if (e) {
67 uint64_t own_pidfdid, pidfdid;
68
69 r = safe_atou64(e, &pidfdid);
70 if (r < 0)
71 goto finish;
72
73 if (pidfd_get_inode_id_self_cached(&own_pidfdid) >= 0) {
74 /* Is this *really* for us? */
75 if (pidfdid != own_pidfdid) {
76 r = 0;
77 goto finish;
78 }
79 }
80 }
81
82 e = getenv("LISTEN_FDS");
83 if (!e) {
84 r = 0;
85 goto finish;
86 }
87
88 r = safe_atoi(e, &n);
89 if (r < 0)
90 goto finish;
91
92 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
93 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
94 r = -EINVAL;
95 goto finish;
96 }
97
98 for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
99 r = fd_cloexec(fd, true);
100 if (r < 0)
101 goto finish;
102 }
103
104 r = n;
105
106 finish:
107 unsetenv_listen(unset_environment);
108 return r;
109 }
110
111 _public_ int sd_listen_fds_with_names(int unset_environment, char ***ret_names) {
112 _cleanup_strv_free_ char **l = NULL;
113 bool have_names;
114 int n_names = 0, n_fds;
115 const char *e;
116 int r;
117
118 if (!ret_names)
119 return sd_listen_fds(unset_environment);
120
121 e = getenv("LISTEN_FDNAMES");
122 if (e) {
123 n_names = strv_split_full(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
124 if (n_names < 0) {
125 unsetenv_listen(unset_environment);
126 return n_names;
127 }
128
129 have_names = true;
130 } else
131 have_names = false;
132
133 n_fds = sd_listen_fds(unset_environment);
134 if (n_fds <= 0)
135 return n_fds;
136
137 if (have_names) {
138 if (n_names != n_fds)
139 return -EINVAL;
140 } else {
141 r = strv_extend_n(&l, "unknown", n_fds);
142 if (r < 0)
143 return r;
144 }
145
146 *ret_names = TAKE_PTR(l);
147 return n_fds;
148 }
149
150 _public_ int sd_is_fifo(int fd, const char *path) {
151 struct stat st_fd;
152
153 assert_return(fd >= 0, -EBADF);
154
155 if (fstat(fd, &st_fd) < 0)
156 return -errno;
157
158 if (!S_ISFIFO(st_fd.st_mode))
159 return 0;
160
161 if (path) {
162 struct stat st_path;
163
164 if (stat(path, &st_path) < 0) {
165
166 if (IN_SET(errno, ENOENT, ENOTDIR))
167 return 0;
168
169 return -errno;
170 }
171
172 return stat_inode_same(&st_path, &st_fd);
173 }
174
175 return 1;
176 }
177
178 _public_ int sd_is_special(int fd, const char *path) {
179 struct stat st_fd;
180
181 assert_return(fd >= 0, -EBADF);
182
183 if (fstat(fd, &st_fd) < 0)
184 return -errno;
185
186 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
187 return 0;
188
189 if (path) {
190 struct stat st_path;
191
192 if (stat(path, &st_path) < 0) {
193
194 if (IN_SET(errno, ENOENT, ENOTDIR))
195 return 0;
196
197 return -errno;
198 }
199
200 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
201 return stat_inode_same(&st_path, &st_fd);
202 if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
203 return st_path.st_rdev == st_fd.st_rdev;
204
205 return 0;
206 }
207
208 return 1;
209 }
210
211 static int is_socket_internal(int fd, int type, int listening) {
212 struct stat st_fd;
213
214 assert_return(fd >= 0, -EBADF);
215 assert_return(type >= 0, -EINVAL);
216
217 if (fstat(fd, &st_fd) < 0)
218 return -errno;
219
220 if (!S_ISSOCK(st_fd.st_mode))
221 return 0;
222
223 if (type != 0) {
224 int other_type = 0;
225 socklen_t l = sizeof(other_type);
226
227 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
228 return -errno;
229
230 if (l != sizeof(other_type))
231 return -EINVAL;
232
233 if (other_type != type)
234 return 0;
235 }
236
237 if (listening >= 0) {
238 int accepting = 0;
239 socklen_t l = sizeof(accepting);
240
241 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
242 return -errno;
243
244 if (l != sizeof(accepting))
245 return -EINVAL;
246
247 if (!accepting != !listening)
248 return 0;
249 }
250
251 return 1;
252 }
253
254 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
255 int r;
256
257 assert_return(fd >= 0, -EBADF);
258 assert_return(family >= 0, -EINVAL);
259
260 r = is_socket_internal(fd, type, listening);
261 if (r <= 0)
262 return r;
263
264 if (family > 0) {
265 union sockaddr_union sockaddr = {};
266 socklen_t l = sizeof(sockaddr);
267
268 if (getsockname(fd, &sockaddr.sa, &l) < 0)
269 return -errno;
270
271 if (l < sizeof(sa_family_t))
272 return -EINVAL;
273
274 return sockaddr.sa.sa_family == family;
275 }
276
277 return 1;
278 }
279
280 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
281 union sockaddr_union sockaddr = {};
282 socklen_t l = sizeof(sockaddr);
283 int r;
284
285 assert_return(fd >= 0, -EBADF);
286 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
287
288 r = is_socket_internal(fd, type, listening);
289 if (r <= 0)
290 return r;
291
292 if (getsockname(fd, &sockaddr.sa, &l) < 0)
293 return -errno;
294
295 if (l < sizeof(sa_family_t))
296 return -EINVAL;
297
298 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6))
299 return 0;
300
301 if (family != 0)
302 if (sockaddr.sa.sa_family != family)
303 return 0;
304
305 if (port > 0) {
306 unsigned sa_port;
307
308 r = sockaddr_port(&sockaddr.sa, &sa_port);
309 if (r < 0)
310 return r;
311
312 return port == sa_port;
313 }
314
315 return 1;
316 }
317
318 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
319 union sockaddr_union sockaddr = {};
320 socklen_t l = sizeof(sockaddr);
321 int r;
322
323 assert_return(fd >= 0, -EBADF);
324 assert_return(addr, -EINVAL);
325 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
326 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
327
328 r = is_socket_internal(fd, type, listening);
329 if (r <= 0)
330 return r;
331
332 if (getsockname(fd, &sockaddr.sa, &l) < 0)
333 return -errno;
334
335 if (l < sizeof(sa_family_t))
336 return -EINVAL;
337
338 if (sockaddr.sa.sa_family != addr->sa_family)
339 return 0;
340
341 if (sockaddr.sa.sa_family == AF_INET) {
342 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
343
344 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
345 return -EINVAL;
346
347 if (in->sin_port != 0 &&
348 sockaddr.in.sin_port != in->sin_port)
349 return false;
350
351 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
352
353 } else {
354 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
355
356 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
357 return -EINVAL;
358
359 if (in->sin6_port != 0 &&
360 sockaddr.in6.sin6_port != in->sin6_port)
361 return false;
362
363 if (in->sin6_flowinfo != 0 &&
364 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
365 return false;
366
367 if (in->sin6_scope_id != 0 &&
368 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
369 return false;
370
371 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
372 sizeof(in->sin6_addr.s6_addr)) == 0;
373 }
374 }
375
376 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
377 union sockaddr_union sockaddr = {};
378 socklen_t l = sizeof(sockaddr);
379 int r;
380
381 assert_return(fd >= 0, -EBADF);
382
383 r = is_socket_internal(fd, type, listening);
384 if (r <= 0)
385 return r;
386
387 if (getsockname(fd, &sockaddr.sa, &l) < 0)
388 return -errno;
389
390 if (l < sizeof(sa_family_t))
391 return -EINVAL;
392
393 if (sockaddr.sa.sa_family != AF_UNIX)
394 return 0;
395
396 if (path) {
397 if (length == 0)
398 length = strlen(path);
399
400 if (length == 0)
401 /* Unnamed socket */
402 return l == offsetof(struct sockaddr_un, sun_path);
403
404 if (path[0])
405 /* Normal path socket */
406 return
407 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
408 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
409 else
410 /* Abstract namespace socket */
411 return
412 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
413 memcmp(path, sockaddr.un.sun_path, length) == 0;
414 }
415
416 return 1;
417 }
418
419 _public_ int sd_is_mq(int fd, const char *path) {
420 struct mq_attr attr;
421 int r;
422
423 /* Check that the fd is valid */
424 r = fd_validate(fd);
425 if (r < 0)
426 return r;
427
428 if (mq_getattr(fd, &attr) < 0) {
429 if (errno == EBADF)
430 /* A non-mq fd (or an invalid one, but we ruled that out above) */
431 return 0;
432 return -errno;
433 }
434
435 if (path) {
436 _cleanup_free_ char *fpath = NULL;
437 struct stat a, b;
438
439 assert_return(path_is_absolute(path), -EINVAL);
440
441 if (fstat(fd, &a) < 0)
442 return -errno;
443
444 fpath = path_join("/dev/mqueue", path);
445 if (!fpath)
446 return -ENOMEM;
447
448 if (stat(fpath, &b) < 0)
449 return -errno;
450
451 if (!stat_inode_same(&a, &b))
452 return 0;
453 }
454
455 return 1;
456 }
457
458 static int vsock_bind_privileged_port(int fd) {
459 union sockaddr_union sa = {
460 .vm.svm_family = AF_VSOCK,
461 .vm.svm_cid = VMADDR_CID_ANY,
462 .vm.svm_port = 1023,
463 };
464 int r;
465
466 assert(fd >= 0);
467
468 do
469 r = RET_NERRNO(bind(fd, &sa.sa, sizeof(sa.vm)));
470 while (r == -EADDRINUSE && --sa.vm.svm_port > 0);
471
472 return r;
473 }
474
475 static int pid_notify_with_fds_internal(
476 pid_t pid,
477 const char *state,
478 const int *fds,
479 size_t n_fds) {
480
481 SocketAddress address;
482 struct iovec iovec;
483 struct msghdr msghdr = {
484 .msg_iov = &iovec,
485 .msg_iovlen = 1,
486 .msg_name = &address.sockaddr,
487 };
488 _cleanup_close_ int fd = -EBADF;
489 int type, r;
490
491 assert_return(state, -EINVAL);
492 assert_return(fds || n_fds == 0, -EINVAL);
493
494 /* Let's make sure the multiplications below don't overflow, and also return a recognizable error in
495 * case the caller tries to send more fds than the kernel limit. The kernel would return EINVAL which
496 * is not too useful I'd say. */
497 if (n_fds > SCM_MAX_FD)
498 return -E2BIG;
499
500 const char *e = getenv("NOTIFY_SOCKET");
501 if (!e)
502 return 0;
503
504 /* Allow AF_UNIX and AF_VSOCK, reject the rest. */
505 r = socket_address_parse_unix(&address, e);
506 if (r == -EPROTO)
507 r = socket_address_parse_vsock(&address, e);
508 if (r < 0)
509 return log_debug_errno(r, "Address NOTIFY_SOCKET='%s' is neither UNIX nor VSOCK, refusing: %m", e);
510 msghdr.msg_namelen = address.size;
511
512 /* If we didn't get an address (which is a normal pattern when specifying VSOCK tuples) error out,
513 * we always require a specific CID. */
514 if (address.sockaddr.vm.svm_family == AF_VSOCK && address.sockaddr.vm.svm_cid == VMADDR_CID_ANY)
515 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
516 "VSOCK address in NOTIFY_SOCKET='%s' doesn't have CID, refusing.", e);
517
518 type = address.type == 0 ? SOCK_DGRAM : address.type;
519
520 /* At the time of writing QEMU does not yet support AF_VSOCK + SOCK_DGRAM and returns
521 * ENODEV. Fallback to SOCK_SEQPACKET in that case. */
522 fd = socket(address.sockaddr.sa.sa_family, type|SOCK_CLOEXEC, 0);
523 if (fd < 0) {
524 if (!(ERRNO_IS_NOT_SUPPORTED(errno) || errno == ENODEV) || address.sockaddr.sa.sa_family != AF_VSOCK || address.type > 0)
525 return log_debug_errno(errno, "Failed to open %s notify socket to '%s': %m", socket_address_type_to_string(type), e);
526
527 type = SOCK_SEQPACKET;
528 fd = socket(address.sockaddr.sa.sa_family, type|SOCK_CLOEXEC, 0);
529 if (fd < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) {
530 type = SOCK_STREAM;
531 fd = socket(address.sockaddr.sa.sa_family, type|SOCK_CLOEXEC, 0);
532 }
533 if (fd < 0)
534 return log_debug_errno(errno, "Failed to open %s socket to '%s': %m", socket_address_type_to_string(type), e);
535 }
536
537 if (address.sockaddr.sa.sa_family == AF_VSOCK) {
538 r = vsock_bind_privileged_port(fd);
539 if (r < 0 && !ERRNO_IS_PRIVILEGE(r))
540 return log_debug_errno(r, "Failed to bind socket to privileged port: %m");
541 }
542
543 if (IN_SET(type, SOCK_STREAM, SOCK_SEQPACKET)) {
544 if (connect(fd, &address.sockaddr.sa, address.size) < 0)
545 return log_debug_errno(errno, "Failed to connect socket to '%s': %m", e);
546
547 msghdr.msg_name = NULL;
548 msghdr.msg_namelen = 0;
549 }
550
551 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
552
553 iovec = IOVEC_MAKE_STRING(state);
554
555 bool send_ucred =
556 (pid != 0 && pid != getpid_cached()) ||
557 getuid() != geteuid() ||
558 getgid() != getegid();
559
560 if (n_fds > 0 || send_ucred) {
561 struct cmsghdr *cmsg;
562
563 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
564 msghdr.msg_controllen =
565 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
566 (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0);
567
568 msghdr.msg_control = alloca0(msghdr.msg_controllen);
569
570 cmsg = CMSG_FIRSTHDR(&msghdr);
571 if (n_fds > 0) {
572 cmsg->cmsg_level = SOL_SOCKET;
573 cmsg->cmsg_type = SCM_RIGHTS;
574 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
575
576 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
577
578 if (send_ucred)
579 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
580 }
581
582 if (send_ucred) {
583 struct ucred *ucred;
584
585 cmsg->cmsg_level = SOL_SOCKET;
586 cmsg->cmsg_type = SCM_CREDENTIALS;
587 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
588
589 ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
590 ucred->pid = pid != 0 ? pid : getpid_cached();
591 ucred->uid = getuid();
592 ucred->gid = getgid();
593 }
594 }
595
596 ssize_t n;
597
598 do {
599 /* First try with fake ucred data, as requested */
600 n = sendmsg(fd, &msghdr, MSG_NOSIGNAL);
601 if (n < 0) {
602 if (!send_ucred)
603 return log_debug_errno(errno, "Failed to send notify message to '%s': %m", e);
604
605 /* If that failed, try with our own ucred instead */
606 /* Silence static analyzers */
607 assert(msghdr.msg_controllen >= CMSG_SPACE(sizeof(struct ucred)));
608 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
609 if (msghdr.msg_controllen == 0)
610 msghdr.msg_control = NULL;
611
612 n = 0;
613 send_ucred = false;
614 } else {
615 /* Unless we're using SOCK_STREAM, we expect to write all the contents immediately. */
616 if (type != SOCK_STREAM && (size_t) n < iovec_total_size(msghdr.msg_iov, msghdr.msg_iovlen))
617 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
618 "Incomplete notify message sent to '%s': %m", e);
619
620 /* Make sure we only send fds and ucred once, even if we're using SOCK_STREAM. */
621 msghdr.msg_control = NULL;
622 msghdr.msg_controllen = 0;
623 }
624 } while (!iovec_inc_many(msghdr.msg_iov, msghdr.msg_iovlen, n));
625
626 if (address.sockaddr.sa.sa_family == AF_VSOCK && IN_SET(type, SOCK_STREAM, SOCK_SEQPACKET)) {
627 /* For AF_VSOCK, we need to close the socket to signal the end of the message. */
628 if (shutdown(fd, SHUT_WR) < 0)
629 return log_debug_errno(errno, "Failed to shutdown notify socket: %m");
630
631 char c;
632 n = recv(fd, &c, sizeof(c), MSG_NOSIGNAL);
633 if (n < 0)
634 return log_debug_errno(errno, "Failed to wait for EOF on notify socket: %m");
635 if (n > 0)
636 return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Unexpectedly received data on notify socket.");
637 }
638
639 if (DEBUG_LOGGING) {
640 _cleanup_free_ char *escaped = xescape_full(state, "\"", /* console_width = */ SIZE_MAX, XESCAPE_8_BIT);
641 log_debug("Notify message sent to '%s': \"%s\"", e, escaped ?: state);
642 }
643
644 return 1;
645 }
646
647 static void unsetenv_notify(bool unset_environment) {
648 if (!unset_environment)
649 return;
650
651 assert_se(unsetenv("NOTIFY_SOCKET") == 0);
652 }
653
654 _public_ int sd_pid_notify_with_fds(
655 pid_t pid,
656 int unset_environment,
657 const char *state,
658 const int *fds,
659 unsigned n_fds) {
660
661 int r;
662
663 r = pid_notify_with_fds_internal(pid, state, fds, n_fds);
664 unsetenv_notify(unset_environment);
665 return r;
666 }
667
668 _public_ int sd_pid_notify_barrier(pid_t pid, int unset_environment, uint64_t timeout) {
669 _cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR;
670 int r;
671
672 r = RET_NERRNO(pipe2(pipe_fd, O_CLOEXEC));
673 if (r < 0)
674 goto finish;
675
676 r = pid_notify_with_fds_internal(pid, "BARRIER=1", &pipe_fd[1], 1);
677 if (r <= 0)
678 goto finish;
679
680 pipe_fd[1] = safe_close(pipe_fd[1]);
681
682 r = fd_wait_for_event(pipe_fd[0], 0 /* POLLHUP is implicit */, timeout);
683 if (r < 0)
684 goto finish;
685 if (r == 0) {
686 r = -ETIMEDOUT;
687 goto finish;
688 }
689
690 r = 1;
691 finish:
692 unsetenv_notify(unset_environment);
693 return r;
694 }
695
696 _public_ int sd_notify_barrier(int unset_environment, uint64_t timeout) {
697 return sd_pid_notify_barrier(0, unset_environment, timeout);
698 }
699
700 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
701 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
702 }
703
704 _public_ int sd_notify(int unset_environment, const char *state) {
705 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
706 }
707
708 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
709 _cleanup_free_ char *p = NULL;
710 int r;
711
712 if (format) {
713 va_list ap;
714
715 va_start(ap, format);
716 r = vasprintf(&p, format, ap);
717 va_end(ap);
718
719 if (r < 0) {
720 r = -ENOMEM;
721 goto finish;
722 }
723 }
724
725 r = pid_notify_with_fds_internal(pid, p, /* fds= */ NULL, /* n_fds= */ 0);
726
727 finish:
728 unsetenv_notify(unset_environment);
729 return r;
730 }
731
732 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
733 _cleanup_free_ char *p = NULL;
734 int r;
735
736 if (format) {
737 va_list ap;
738
739 va_start(ap, format);
740 r = vasprintf(&p, format, ap);
741 va_end(ap);
742
743 if (r < 0) {
744 r = -ENOMEM;
745 goto finish;
746 }
747 }
748
749 r = pid_notify_with_fds_internal(/* pid= */ 0, p, /* fds= */ NULL, /* n_fds= */ 0);
750
751 finish:
752 unsetenv_notify(unset_environment);
753 return r;
754 }
755
756 _public_ int sd_pid_notifyf_with_fds(
757 pid_t pid,
758 int unset_environment,
759 const int *fds, size_t n_fds,
760 const char *format, ...) {
761
762 _cleanup_free_ char *p = NULL;
763 int r;
764
765 if (format) {
766 va_list ap;
767
768 va_start(ap, format);
769 r = vasprintf(&p, format, ap);
770 va_end(ap);
771
772 if (r < 0) {
773 r = -ENOMEM;
774 goto finish;
775 }
776 }
777
778 r = pid_notify_with_fds_internal(pid, p, fds, n_fds);
779
780 finish:
781 unsetenv_notify(unset_environment);
782 return r;
783 }
784
785 _public_ int sd_pidfd_get_inode_id(int pidfd, uint64_t *ret) {
786 int r;
787
788 assert_return(pidfd >= 0, -EBADF);
789
790 /* Are pidfds backed by pidfs where the unique inode id is relevant? Note that the pidfd
791 * passed to us is extrinsic and hence cannot be trusted to initialize our "have_pidfs" cache,
792 * instead pidfd_check_pidfs() will allocate one internally. */
793 r = pidfd_check_pidfs(/* pid_fd= */ -EBADF);
794 if (r <= 0)
795 return -EOPNOTSUPP;
796
797 r = fd_is_fs_type(pidfd, PID_FS_MAGIC);
798 if (r < 0)
799 return r;
800 if (r == 0)
801 return -EBADF; /* pidfs is definitely around, so it's the fd that's of invalid type */
802
803 return pidfd_get_inode_id_impl(pidfd, ret);
804 }
805
806 _public_ int sd_booted(void) {
807 int r;
808
809 /* We test whether the runtime unit file directory has been created. This takes place in mount-setup.c,
810 * so is guaranteed to happen very early during boot. */
811
812 r = access_nofollow("/run/systemd/system/", F_OK);
813 if (r >= 0)
814 return true;
815 if (r == -ENOENT)
816 return false;
817
818 return r;
819 }
820
821 static void unsetenv_watchdog(bool unset_environment) {
822 if (!unset_environment)
823 return;
824
825 assert_se(unsetenv("WATCHDOG_USEC") == 0);
826 assert_se(unsetenv("WATCHDOG_PID") == 0);
827 }
828
829 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
830 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
831 uint64_t u;
832 int r;
833
834 s = getenv("WATCHDOG_USEC");
835 if (!s) {
836 r = 0;
837 goto finish;
838 }
839
840 r = safe_atou64(s, &u);
841 if (r < 0)
842 goto finish;
843 if (!timestamp_is_set(u)) {
844 r = -EINVAL;
845 goto finish;
846 }
847
848 p = getenv("WATCHDOG_PID");
849 if (p) {
850 pid_t pid;
851
852 r = parse_pid(p, &pid);
853 if (r < 0)
854 goto finish;
855
856 /* Is this for us? */
857 if (getpid_cached() != pid) {
858 r = 0;
859 goto finish;
860 }
861 }
862
863 if (usec)
864 *usec = u;
865
866 r = 1;
867
868 finish:
869 unsetenv_watchdog(unset_environment);
870 return r;
871 }