]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-daemon/sd-daemon.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / libsystemd / sd-daemon / sd-daemon.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <limits.h>
8 #include <mqueue.h>
9 #include <netinet/in.h>
10 #include <stdarg.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/socket.h>
16 #include <sys/stat.h>
17 #include <sys/un.h>
18 #include <unistd.h>
19
20 #include "sd-daemon.h"
21
22 #include "alloc-util.h"
23 #include "fd-util.h"
24 #include "fs-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "socket-util.h"
29 #include "strv.h"
30 #include "util.h"
31
32 #define SNDBUF_SIZE (8*1024*1024)
33
34 static void unsetenv_all(bool unset_environment) {
35
36 if (!unset_environment)
37 return;
38
39 unsetenv("LISTEN_PID");
40 unsetenv("LISTEN_FDS");
41 unsetenv("LISTEN_FDNAMES");
42 }
43
44 _public_ int sd_listen_fds(int unset_environment) {
45 const char *e;
46 int n, r, fd;
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_FDS");
66 if (!e) {
67 r = 0;
68 goto finish;
69 }
70
71 r = safe_atoi(e, &n);
72 if (r < 0)
73 goto finish;
74
75 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
76 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
77 r = -EINVAL;
78 goto finish;
79 }
80
81 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
82 r = fd_cloexec(fd, true);
83 if (r < 0)
84 goto finish;
85 }
86
87 r = n;
88
89 finish:
90 unsetenv_all(unset_environment);
91 return r;
92 }
93
94 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
95 _cleanup_strv_free_ char **l = NULL;
96 bool have_names;
97 int n_names = 0, n_fds;
98 const char *e;
99 int r;
100
101 if (!names)
102 return sd_listen_fds(unset_environment);
103
104 e = getenv("LISTEN_FDNAMES");
105 if (e) {
106 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
107 if (n_names < 0) {
108 unsetenv_all(unset_environment);
109 return n_names;
110 }
111
112 have_names = true;
113 } else
114 have_names = false;
115
116 n_fds = sd_listen_fds(unset_environment);
117 if (n_fds <= 0)
118 return n_fds;
119
120 if (have_names) {
121 if (n_names != n_fds)
122 return -EINVAL;
123 } else {
124 r = strv_extend_n(&l, "unknown", n_fds);
125 if (r < 0)
126 return r;
127 }
128
129 *names = TAKE_PTR(l);
130
131 return n_fds;
132 }
133
134 _public_ int sd_is_fifo(int fd, const char *path) {
135 struct stat st_fd;
136
137 assert_return(fd >= 0, -EBADF);
138
139 if (fstat(fd, &st_fd) < 0)
140 return -errno;
141
142 if (!S_ISFIFO(st_fd.st_mode))
143 return 0;
144
145 if (path) {
146 struct stat st_path;
147
148 if (stat(path, &st_path) < 0) {
149
150 if (IN_SET(errno, ENOENT, ENOTDIR))
151 return 0;
152
153 return -errno;
154 }
155
156 return
157 st_path.st_dev == st_fd.st_dev &&
158 st_path.st_ino == st_fd.st_ino;
159 }
160
161 return 1;
162 }
163
164 _public_ int sd_is_special(int fd, const char *path) {
165 struct stat st_fd;
166
167 assert_return(fd >= 0, -EBADF);
168
169 if (fstat(fd, &st_fd) < 0)
170 return -errno;
171
172 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
173 return 0;
174
175 if (path) {
176 struct stat st_path;
177
178 if (stat(path, &st_path) < 0) {
179
180 if (IN_SET(errno, ENOENT, ENOTDIR))
181 return 0;
182
183 return -errno;
184 }
185
186 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
187 return
188 st_path.st_dev == st_fd.st_dev &&
189 st_path.st_ino == st_fd.st_ino;
190 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
191 return st_path.st_rdev == st_fd.st_rdev;
192 else
193 return 0;
194 }
195
196 return 1;
197 }
198
199 static int sd_is_socket_internal(int fd, int type, int listening) {
200 struct stat st_fd;
201
202 assert_return(fd >= 0, -EBADF);
203 assert_return(type >= 0, -EINVAL);
204
205 if (fstat(fd, &st_fd) < 0)
206 return -errno;
207
208 if (!S_ISSOCK(st_fd.st_mode))
209 return 0;
210
211 if (type != 0) {
212 int other_type = 0;
213 socklen_t l = sizeof(other_type);
214
215 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
216 return -errno;
217
218 if (l != sizeof(other_type))
219 return -EINVAL;
220
221 if (other_type != type)
222 return 0;
223 }
224
225 if (listening >= 0) {
226 int accepting = 0;
227 socklen_t l = sizeof(accepting);
228
229 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
230 return -errno;
231
232 if (l != sizeof(accepting))
233 return -EINVAL;
234
235 if (!accepting != !listening)
236 return 0;
237 }
238
239 return 1;
240 }
241
242 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
243 int r;
244
245 assert_return(fd >= 0, -EBADF);
246 assert_return(family >= 0, -EINVAL);
247
248 r = sd_is_socket_internal(fd, type, listening);
249 if (r <= 0)
250 return r;
251
252 if (family > 0) {
253 union sockaddr_union sockaddr = {};
254 socklen_t l = sizeof(sockaddr);
255
256 if (getsockname(fd, &sockaddr.sa, &l) < 0)
257 return -errno;
258
259 if (l < sizeof(sa_family_t))
260 return -EINVAL;
261
262 return sockaddr.sa.sa_family == family;
263 }
264
265 return 1;
266 }
267
268 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
269 union sockaddr_union sockaddr = {};
270 socklen_t l = sizeof(sockaddr);
271 int r;
272
273 assert_return(fd >= 0, -EBADF);
274 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
275
276 r = sd_is_socket_internal(fd, type, listening);
277 if (r <= 0)
278 return r;
279
280 if (getsockname(fd, &sockaddr.sa, &l) < 0)
281 return -errno;
282
283 if (l < sizeof(sa_family_t))
284 return -EINVAL;
285
286 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6))
287 return 0;
288
289 if (family != 0)
290 if (sockaddr.sa.sa_family != family)
291 return 0;
292
293 if (port > 0) {
294 unsigned sa_port;
295
296 r = sockaddr_port(&sockaddr.sa, &sa_port);
297 if (r < 0)
298 return r;
299
300 return port == sa_port;
301 }
302
303 return 1;
304 }
305
306 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
307 union sockaddr_union sockaddr = {};
308 socklen_t l = sizeof(sockaddr);
309 int r;
310
311 assert_return(fd >= 0, -EBADF);
312 assert_return(addr, -EINVAL);
313 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
314 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
315
316 r = sd_is_socket_internal(fd, type, listening);
317 if (r <= 0)
318 return r;
319
320 if (getsockname(fd, &sockaddr.sa, &l) < 0)
321 return -errno;
322
323 if (l < sizeof(sa_family_t))
324 return -EINVAL;
325
326 if (sockaddr.sa.sa_family != addr->sa_family)
327 return 0;
328
329 if (sockaddr.sa.sa_family == AF_INET) {
330 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
331
332 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
333 return -EINVAL;
334
335 if (in->sin_port != 0 &&
336 sockaddr.in.sin_port != in->sin_port)
337 return false;
338
339 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
340
341 } else {
342 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
343
344 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
345 return -EINVAL;
346
347 if (in->sin6_port != 0 &&
348 sockaddr.in6.sin6_port != in->sin6_port)
349 return false;
350
351 if (in->sin6_flowinfo != 0 &&
352 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
353 return false;
354
355 if (in->sin6_scope_id != 0 &&
356 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
357 return false;
358
359 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
360 sizeof(in->sin6_addr.s6_addr)) == 0;
361 }
362 }
363
364 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
365 union sockaddr_union sockaddr = {};
366 socklen_t l = sizeof(sockaddr);
367 int r;
368
369 assert_return(fd >= 0, -EBADF);
370
371 r = sd_is_socket_internal(fd, type, listening);
372 if (r <= 0)
373 return r;
374
375 if (getsockname(fd, &sockaddr.sa, &l) < 0)
376 return -errno;
377
378 if (l < sizeof(sa_family_t))
379 return -EINVAL;
380
381 if (sockaddr.sa.sa_family != AF_UNIX)
382 return 0;
383
384 if (path) {
385 if (length == 0)
386 length = strlen(path);
387
388 if (length == 0)
389 /* Unnamed socket */
390 return l == offsetof(struct sockaddr_un, sun_path);
391
392 if (path[0])
393 /* Normal path socket */
394 return
395 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
396 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
397 else
398 /* Abstract namespace socket */
399 return
400 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
401 memcmp(path, sockaddr.un.sun_path, length) == 0;
402 }
403
404 return 1;
405 }
406
407 _public_ int sd_is_mq(int fd, const char *path) {
408 struct mq_attr attr;
409
410 /* Check that the fd is valid */
411 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
412
413 if (mq_getattr(fd, &attr) < 0) {
414 if (errno == EBADF)
415 /* A non-mq fd (or an invalid one, but we ruled that out above) */
416 return 0;
417 return -errno;
418 }
419
420 if (path) {
421 char fpath[PATH_MAX];
422 struct stat a, b;
423
424 assert_return(path_is_absolute(path), -EINVAL);
425
426 if (fstat(fd, &a) < 0)
427 return -errno;
428
429 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
430 fpath[sizeof(fpath)-1] = 0;
431
432 if (stat(fpath, &b) < 0)
433 return -errno;
434
435 if (a.st_dev != b.st_dev ||
436 a.st_ino != b.st_ino)
437 return 0;
438 }
439
440 return 1;
441 }
442
443 _public_ int sd_pid_notify_with_fds(
444 pid_t pid,
445 int unset_environment,
446 const char *state,
447 const int *fds,
448 unsigned n_fds) {
449
450 union sockaddr_union sockaddr = {
451 .sa.sa_family = AF_UNIX,
452 };
453 struct iovec iovec = {
454 .iov_base = (char*) state,
455 };
456 struct msghdr msghdr = {
457 .msg_iov = &iovec,
458 .msg_iovlen = 1,
459 .msg_name = &sockaddr,
460 };
461 _cleanup_close_ int fd = -1;
462 struct cmsghdr *cmsg = NULL;
463 const char *e;
464 bool send_ucred;
465 int r;
466
467 if (!state) {
468 r = -EINVAL;
469 goto finish;
470 }
471
472 if (n_fds > 0 && !fds) {
473 r = -EINVAL;
474 goto finish;
475 }
476
477 e = getenv("NOTIFY_SOCKET");
478 if (!e)
479 return 0;
480
481 /* Must be an abstract socket, or an absolute path */
482 if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
483 r = -EINVAL;
484 goto finish;
485 }
486
487 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
488 r = -EINVAL;
489 goto finish;
490 }
491
492 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
493 if (fd < 0) {
494 r = -errno;
495 goto finish;
496 }
497
498 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
499
500 iovec.iov_len = strlen(state);
501
502 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
503 if (sockaddr.un.sun_path[0] == '@')
504 sockaddr.un.sun_path[0] = 0;
505
506 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
507
508 send_ucred =
509 (pid != 0 && pid != getpid_cached()) ||
510 getuid() != geteuid() ||
511 getgid() != getegid();
512
513 if (n_fds > 0 || send_ucred) {
514 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
515 msghdr.msg_controllen =
516 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
517 (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0);
518
519 msghdr.msg_control = alloca0(msghdr.msg_controllen);
520
521 cmsg = CMSG_FIRSTHDR(&msghdr);
522 if (n_fds > 0) {
523 cmsg->cmsg_level = SOL_SOCKET;
524 cmsg->cmsg_type = SCM_RIGHTS;
525 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
526
527 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
528
529 if (send_ucred)
530 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
531 }
532
533 if (send_ucred) {
534 struct ucred *ucred;
535
536 cmsg->cmsg_level = SOL_SOCKET;
537 cmsg->cmsg_type = SCM_CREDENTIALS;
538 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
539
540 ucred = (struct ucred*) CMSG_DATA(cmsg);
541 ucred->pid = pid != 0 ? pid : getpid_cached();
542 ucred->uid = getuid();
543 ucred->gid = getgid();
544 }
545 }
546
547 /* First try with fake ucred data, as requested */
548 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
549 r = 1;
550 goto finish;
551 }
552
553 /* If that failed, try with our own ucred instead */
554 if (send_ucred) {
555 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
556 if (msghdr.msg_controllen == 0)
557 msghdr.msg_control = NULL;
558
559 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
560 r = 1;
561 goto finish;
562 }
563 }
564
565 r = -errno;
566
567 finish:
568 if (unset_environment)
569 unsetenv("NOTIFY_SOCKET");
570
571 return r;
572 }
573
574 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
575 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
576 }
577
578 _public_ int sd_notify(int unset_environment, const char *state) {
579 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
580 }
581
582 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
583 _cleanup_free_ char *p = NULL;
584 int r;
585
586 if (format) {
587 va_list ap;
588
589 va_start(ap, format);
590 r = vasprintf(&p, format, ap);
591 va_end(ap);
592
593 if (r < 0 || !p)
594 return -ENOMEM;
595 }
596
597 return sd_pid_notify(pid, unset_environment, p);
598 }
599
600 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
601 _cleanup_free_ char *p = NULL;
602 int r;
603
604 if (format) {
605 va_list ap;
606
607 va_start(ap, format);
608 r = vasprintf(&p, format, ap);
609 va_end(ap);
610
611 if (r < 0 || !p)
612 return -ENOMEM;
613 }
614
615 return sd_pid_notify(0, unset_environment, p);
616 }
617
618 _public_ int sd_booted(void) {
619 /* We test whether the runtime unit file directory has been
620 * created. This takes place in mount-setup.c, so is
621 * guaranteed to happen very early during boot. */
622
623 return laccess("/run/systemd/system/", F_OK) >= 0;
624 }
625
626 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
627 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
628 uint64_t u;
629 int r = 0;
630
631 s = getenv("WATCHDOG_USEC");
632 if (!s)
633 goto finish;
634
635 r = safe_atou64(s, &u);
636 if (r < 0)
637 goto finish;
638 if (u <= 0 || u >= USEC_INFINITY) {
639 r = -EINVAL;
640 goto finish;
641 }
642
643 p = getenv("WATCHDOG_PID");
644 if (p) {
645 pid_t pid;
646
647 r = parse_pid(p, &pid);
648 if (r < 0)
649 goto finish;
650
651 /* Is this for us? */
652 if (getpid_cached() != pid) {
653 r = 0;
654 goto finish;
655 }
656 }
657
658 if (usec)
659 *usec = u;
660
661 r = 1;
662
663 finish:
664 if (unset_environment && s)
665 unsetenv("WATCHDOG_USEC");
666 if (unset_environment && p)
667 unsetenv("WATCHDOG_PID");
668
669 return r;
670 }