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