]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-daemon/sd-daemon.c
core: use an AF_UNIX/SOCK_DGRAM socket for cgroup agent notification
[thirdparty/systemd.git] / src / libsystemd / sd-daemon / sd-daemon.c
CommitLineData
abbbea81 1/***
0ebee881
KS
2 This file is part of systemd.
3
abbbea81
LP
4 Copyright 2010 Lennart Poettering
5
0ebee881
KS
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
abbbea81 15
0ebee881
KS
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
8c47c732 19
abbbea81 20#include <errno.h>
916abb21 21#include <limits.h>
0ebee881 22#include <mqueue.h>
8dd4c05b
LP
23#include <netinet/in.h>
24#include <stdarg.h>
25#include <stddef.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/socket.h>
30#include <sys/stat.h>
31#include <sys/un.h>
32#include <unistd.h>
abbbea81 33
07630cea
LP
34#include "sd-daemon.h"
35
b5efdb8a 36#include "alloc-util.h"
3ffd4af2 37#include "fd-util.h"
f4f15635 38#include "fs-util.h"
6bedfcbb 39#include "parse-util.h"
be8f4e9e 40#include "path-util.h"
3cb46740 41#include "socket-util.h"
8dd4c05b
LP
42#include "strv.h"
43#include "util.h"
44
a47806fa
LP
45#define SNDBUF_SIZE (8*1024*1024)
46
8dd4c05b
LP
47static void unsetenv_all(bool unset_environment) {
48
49 if (!unset_environment)
50 return;
51
52 unsetenv("LISTEN_PID");
53 unsetenv("LISTEN_FDS");
54 unsetenv("LISTEN_FDNAMES");
55}
56
0ebee881 57_public_ int sd_listen_fds(int unset_environment) {
abbbea81 58 const char *e;
046c93f8 59 int n, r, fd;
be8f4e9e 60 pid_t pid;
abbbea81 61
50425d16
MS
62 e = getenv("LISTEN_PID");
63 if (!e) {
abbbea81
LP
64 r = 0;
65 goto finish;
66 }
67
be8f4e9e
LP
68 r = parse_pid(e, &pid);
69 if (r < 0)
abbbea81 70 goto finish;
abbbea81
LP
71
72 /* Is this for us? */
be8f4e9e 73 if (getpid() != pid) {
abbbea81
LP
74 r = 0;
75 goto finish;
76 }
77
50425d16
MS
78 e = getenv("LISTEN_FDS");
79 if (!e) {
abbbea81
LP
80 r = 0;
81 goto finish;
82 }
83
046c93f8 84 r = safe_atoi(e, &n);
be8f4e9e 85 if (r < 0)
abbbea81 86 goto finish;
8640e111 87
046c93f8
VC
88 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
89 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
90 r = -EINVAL;
91 goto finish;
92 }
93
94 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
be8f4e9e
LP
95 r = fd_cloexec(fd, true);
96 if (r < 0)
8640e111 97 goto finish;
8640e111
LP
98 }
99
046c93f8 100 r = n;
abbbea81
LP
101
102finish:
8dd4c05b
LP
103 unsetenv_all(unset_environment);
104 return r;
105}
106
107_public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
108 _cleanup_strv_free_ char **l = NULL;
109 bool have_names;
110 int n_names = 0, n_fds;
111 const char *e;
112 int r;
113
114 if (!names)
115 return sd_listen_fds(unset_environment);
116
117 e = getenv("LISTEN_FDNAMES");
118 if (e) {
119 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
120 if (n_names < 0) {
121 unsetenv_all(unset_environment);
122 return n_names;
123 }
124
125 have_names = true;
126 } else
127 have_names = false;
128
129 n_fds = sd_listen_fds(unset_environment);
130 if (n_fds <= 0)
131 return n_fds;
132
133 if (have_names) {
134 if (n_names != n_fds)
135 return -EINVAL;
136 } else {
137 r = strv_extend_n(&l, "unknown", n_fds);
138 if (r < 0)
139 return r;
abbbea81
LP
140 }
141
8dd4c05b
LP
142 *names = l;
143 l = NULL;
144
145 return n_fds;
abbbea81 146}
7c394faa 147
0ebee881 148_public_ int sd_is_fifo(int fd, const char *path) {
7c394faa
LP
149 struct stat st_fd;
150
e6803801 151 assert_return(fd >= 0, -EBADF);
7c394faa 152
7c394faa
LP
153 if (fstat(fd, &st_fd) < 0)
154 return -errno;
155
156 if (!S_ISFIFO(st_fd.st_mode))
157 return 0;
158
159 if (path) {
160 struct stat st_path;
161
fd8bccfb 162 if (stat(path, &st_path) < 0) {
7c394faa
LP
163
164 if (errno == ENOENT || errno == ENOTDIR)
165 return 0;
166
167 return -errno;
168 }
169
170 return
171 st_path.st_dev == st_fd.st_dev &&
172 st_path.st_ino == st_fd.st_ino;
173 }
174
175 return 1;
176}
177
0ebee881 178_public_ int sd_is_special(int fd, const char *path) {
4160ec67
WD
179 struct stat st_fd;
180
e6803801 181 assert_return(fd >= 0, -EBADF);
4160ec67
WD
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 (errno == ENOENT || errno == 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
202 st_path.st_dev == st_fd.st_dev &&
203 st_path.st_ino == st_fd.st_ino;
204 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
205 return st_path.st_rdev == st_fd.st_rdev;
206 else
207 return 0;
208 }
209
210 return 1;
211}
212
e6a3081a 213static int sd_is_socket_internal(int fd, int type, int listening) {
7c394faa
LP
214 struct stat st_fd;
215
e6803801 216 assert_return(fd >= 0, -EBADF);
be8f4e9e 217 assert_return(type >= 0, -EINVAL);
7c394faa
LP
218
219 if (fstat(fd, &st_fd) < 0)
220 return -errno;
221
222 if (!S_ISSOCK(st_fd.st_mode))
223 return 0;
224
225 if (type != 0) {
226 int other_type = 0;
227 socklen_t l = sizeof(other_type);
228
229 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
230 return -errno;
231
232 if (l != sizeof(other_type))
233 return -EINVAL;
234
235 if (other_type != type)
236 return 0;
237 }
238
239 if (listening >= 0) {
240 int accepting = 0;
241 socklen_t l = sizeof(accepting);
242
243 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
244 return -errno;
245
246 if (l != sizeof(accepting))
247 return -EINVAL;
248
dde770cf 249 if (!accepting != !listening)
7c394faa
LP
250 return 0;
251 }
252
253 return 1;
254}
255
0ebee881 256_public_ int sd_is_socket(int fd, int family, int type, int listening) {
88ce42f6
LP
257 int r;
258
e6803801 259 assert_return(fd >= 0, -EBADF);
be8f4e9e 260 assert_return(family >= 0, -EINVAL);
88ce42f6 261
50425d16
MS
262 r = sd_is_socket_internal(fd, type, listening);
263 if (r <= 0)
88ce42f6
LP
264 return r;
265
266 if (family > 0) {
1c633045
ZJS
267 union sockaddr_union sockaddr = {};
268 socklen_t l = sizeof(sockaddr);
88ce42f6
LP
269
270 if (getsockname(fd, &sockaddr.sa, &l) < 0)
271 return -errno;
272
b7f42664 273 if (l < sizeof(sa_family_t))
88ce42f6
LP
274 return -EINVAL;
275
276 return sockaddr.sa.sa_family == family;
277 }
278
279 return 1;
280}
281
0ebee881 282_public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
1c633045
ZJS
283 union sockaddr_union sockaddr = {};
284 socklen_t l = sizeof(sockaddr);
7c394faa
LP
285 int r;
286
e6803801 287 assert_return(fd >= 0, -EBADF);
be8f4e9e 288 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
88ce42f6 289
50425d16
MS
290 r = sd_is_socket_internal(fd, type, listening);
291 if (r <= 0)
7c394faa
LP
292 return r;
293
7c394faa
LP
294 if (getsockname(fd, &sockaddr.sa, &l) < 0)
295 return -errno;
296
b7f42664 297 if (l < sizeof(sa_family_t))
7c394faa
LP
298 return -EINVAL;
299
300 if (sockaddr.sa.sa_family != AF_INET &&
301 sockaddr.sa.sa_family != AF_INET6)
302 return 0;
303
be8f4e9e 304 if (family != 0)
88ce42f6
LP
305 if (sockaddr.sa.sa_family != family)
306 return 0;
307
7c394faa
LP
308 if (port > 0) {
309 if (sockaddr.sa.sa_family == AF_INET) {
310 if (l < sizeof(struct sockaddr_in))
311 return -EINVAL;
312
3cb46740 313 return htons(port) == sockaddr.in.sin_port;
7c394faa
LP
314 } else {
315 if (l < sizeof(struct sockaddr_in6))
316 return -EINVAL;
317
318 return htons(port) == sockaddr.in6.sin6_port;
319 }
320 }
321
322 return 1;
323}
324
0ebee881 325_public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
1c633045
ZJS
326 union sockaddr_union sockaddr = {};
327 socklen_t l = sizeof(sockaddr);
7c394faa
LP
328 int r;
329
e6803801 330 assert_return(fd >= 0, -EBADF);
be8f4e9e 331
50425d16
MS
332 r = sd_is_socket_internal(fd, type, listening);
333 if (r <= 0)
7c394faa
LP
334 return r;
335
7c394faa
LP
336 if (getsockname(fd, &sockaddr.sa, &l) < 0)
337 return -errno;
338
b7f42664 339 if (l < sizeof(sa_family_t))
7c394faa
LP
340 return -EINVAL;
341
342 if (sockaddr.sa.sa_family != AF_UNIX)
343 return 0;
344
345 if (path) {
d1d7caee 346 if (length == 0)
7c394faa
LP
347 length = strlen(path);
348
d1d7caee 349 if (length == 0)
7c394faa 350 /* Unnamed socket */
0e098b15 351 return l == offsetof(struct sockaddr_un, sun_path);
7c394faa 352
7c394faa
LP
353 if (path[0])
354 /* Normal path socket */
cd250a39 355 return
0e098b15 356 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
cd250a39 357 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
7c394faa
LP
358 else
359 /* Abstract namespace socket */
cd250a39 360 return
0e098b15 361 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
cd250a39 362 memcmp(path, sockaddr.un.sun_path, length) == 0;
7c394faa
LP
363 }
364
365 return 1;
366}
8c47c732 367
0ebee881 368_public_ int sd_is_mq(int fd, const char *path) {
916abb21
LP
369 struct mq_attr attr;
370
0260d1d5
ZJS
371 /* Check that the fd is valid */
372 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
916abb21 373
0260d1d5
ZJS
374 if (mq_getattr(fd, &attr) < 0) {
375 if (errno == EBADF)
376 /* A non-mq fd (or an invalid one, but we ruled that out above) */
377 return 0;
916abb21 378 return -errno;
0260d1d5 379 }
916abb21
LP
380
381 if (path) {
382 char fpath[PATH_MAX];
383 struct stat a, b;
384
be8f4e9e 385 assert_return(path_is_absolute(path), -EINVAL);
916abb21
LP
386
387 if (fstat(fd, &a) < 0)
388 return -errno;
389
390 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
391 fpath[sizeof(fpath)-1] = 0;
392
393 if (stat(fpath, &b) < 0)
394 return -errno;
395
396 if (a.st_dev != b.st_dev ||
397 a.st_ino != b.st_ino)
398 return 0;
399 }
400
401 return 1;
916abb21
LP
402}
403
a354329f
LP
404_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
405 union sockaddr_union sockaddr = {
406 .sa.sa_family = AF_UNIX,
407 };
408 struct iovec iovec = {
409 .iov_base = (char*) state,
410 };
411 struct msghdr msghdr = {
412 .msg_iov = &iovec,
413 .msg_iovlen = 1,
414 .msg_name = &sockaddr,
415 };
a354329f
LP
416 _cleanup_close_ int fd = -1;
417 struct cmsghdr *cmsg = NULL;
418 const char *e;
64144440 419 bool have_pid;
be8f4e9e 420 int r;
8c47c732
LP
421
422 if (!state) {
423 r = -EINVAL;
424 goto finish;
425 }
426
a354329f
LP
427 if (n_fds > 0 && !fds) {
428 r = -EINVAL;
429 goto finish;
430 }
431
50425d16
MS
432 e = getenv("NOTIFY_SOCKET");
433 if (!e)
08bfb810 434 return 0;
8c47c732
LP
435
436 /* Must be an abstract socket, or an absolute path */
437 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
438 r = -EINVAL;
439 goto finish;
440 }
441
638b56cd
LP
442 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
443 r = -EINVAL;
444 goto finish;
445 }
446
50425d16
MS
447 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
448 if (fd < 0) {
8c47c732
LP
449 r = -errno;
450 goto finish;
451 }
452
a47806fa
LP
453 fd_inc_sndbuf(fd, SNDBUF_SIZE);
454
a354329f 455 iovec.iov_len = strlen(state);
8c47c732 456
a354329f 457 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
8c47c732
LP
458 if (sockaddr.un.sun_path[0] == '@')
459 sockaddr.un.sun_path[0] = 0;
460
0e098b15 461 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
a013bd94
LP
462 if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
463 msghdr.msg_namelen = sizeof(struct sockaddr_un);
464
64144440 465 have_pid = pid != 0 && pid != getpid();
d4a144fa 466
64144440 467 if (n_fds > 0 || have_pid) {
96d49011 468 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
c463a6f1
LP
469 msghdr.msg_controllen =
470 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
471 (have_pid ? CMSG_SPACE(sizeof(struct ucred)) : 0);
472
40f44238 473 msghdr.msg_control = alloca0(msghdr.msg_controllen);
a354329f
LP
474
475 cmsg = CMSG_FIRSTHDR(&msghdr);
64144440
ZJS
476 if (n_fds > 0) {
477 cmsg->cmsg_level = SOL_SOCKET;
478 cmsg->cmsg_type = SCM_RIGHTS;
479 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
a354329f 480
64144440 481 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
be8f4e9e 482
64144440
ZJS
483 if (have_pid)
484 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
485 }
a354329f 486
64144440
ZJS
487 if (have_pid) {
488 struct ucred *ucred;
be8f4e9e 489
64144440
ZJS
490 cmsg->cmsg_level = SOL_SOCKET;
491 cmsg->cmsg_type = SCM_CREDENTIALS;
492 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
be8f4e9e 493
64144440
ZJS
494 ucred = (struct ucred*) CMSG_DATA(cmsg);
495 ucred->pid = pid;
496 ucred->uid = getuid();
497 ucred->gid = getgid();
498 }
be8f4e9e
LP
499 }
500
501 /* First try with fake ucred data, as requested */
502 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
503 r = 1;
8c47c732
LP
504 goto finish;
505 }
506
a354329f 507 /* If that failed, try with our own ucred instead */
64144440
ZJS
508 if (have_pid) {
509 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
510 if (msghdr.msg_controllen == 0)
a354329f 511 msghdr.msg_control = NULL;
be8f4e9e
LP
512
513 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
514 r = 1;
515 goto finish;
516 }
517 }
518
519 r = -errno;
8c47c732
LP
520
521finish:
522 if (unset_environment)
523 unsetenv("NOTIFY_SOCKET");
524
8c47c732 525 return r;
8c47c732
LP
526}
527
a354329f
LP
528_public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
529 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
530}
531
be8f4e9e 532_public_ int sd_notify(int unset_environment, const char *state) {
a354329f 533 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
be8f4e9e
LP
534}
535
536_public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
537 _cleanup_free_ char *p = NULL;
538 int r;
539
540 if (format) {
541 va_list ap;
542
543 va_start(ap, format);
544 r = vasprintf(&p, format, ap);
545 va_end(ap);
546
547 if (r < 0 || !p)
548 return -ENOMEM;
549 }
550
551 return sd_pid_notify(pid, unset_environment, p);
552}
553
0ebee881 554_public_ int sd_notifyf(int unset_environment, const char *format, ...) {
be8f4e9e 555 _cleanup_free_ char *p = NULL;
8c47c732
LP
556 int r;
557
be8f4e9e
LP
558 if (format) {
559 va_list ap;
8c47c732 560
be8f4e9e
LP
561 va_start(ap, format);
562 r = vasprintf(&p, format, ap);
563 va_end(ap);
8c47c732 564
be8f4e9e
LP
565 if (r < 0 || !p)
566 return -ENOMEM;
567 }
8c47c732 568
be8f4e9e 569 return sd_pid_notify(0, unset_environment, p);
8c47c732 570}
40473a70 571
0ebee881 572_public_ int sd_booted(void) {
66e41181
LP
573 /* We test whether the runtime unit file directory has been
574 * created. This takes place in mount-setup.c, so is
575 * guaranteed to happen very early during boot. */
40473a70 576
31021ba0 577 return laccess("/run/systemd/system/", F_OK) >= 0;
40473a70 578}
09812eb7 579
0ebee881 580_public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
a9becdd6 581 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
be8f4e9e 582 uint64_t u;
a9becdd6 583 int r = 0;
09812eb7 584
a9becdd6
ZJS
585 s = getenv("WATCHDOG_USEC");
586 if (!s)
09812eb7 587 goto finish;
09812eb7 588
a9becdd6 589 r = safe_atou64(s, &u);
be8f4e9e 590 if (r < 0)
09812eb7 591 goto finish;
caffe412 592 if (u <= 0 || u >= USEC_INFINITY) {
09812eb7
LP
593 r = -EINVAL;
594 goto finish;
595 }
596
a9becdd6
ZJS
597 p = getenv("WATCHDOG_PID");
598 if (p) {
599 pid_t pid;
600
601 r = parse_pid(p, &pid);
602 if (r < 0)
603 goto finish;
604
605 /* Is this for us? */
606 if (getpid() != pid) {
607 r = 0;
608 goto finish;
609 }
09812eb7
LP
610 }
611
612 if (usec)
be8f4e9e 613 *usec = u;
09812eb7
LP
614
615 r = 1;
616
617finish:
a9becdd6 618 if (unset_environment && s)
09812eb7 619 unsetenv("WATCHDOG_USEC");
a9becdd6
ZJS
620 if (unset_environment && p)
621 unsetenv("WATCHDOG_PID");
09812eb7
LP
622
623 return r;
09812eb7 624}