1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
11 #include <netinet/in.h>
17 #include <sys/socket.h>
22 #include "sd-daemon.h"
24 #include "alloc-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 //#include "process-util.h"
30 #include "socket-util.h"
34 /// Additional includes needed by elogind
35 #include "process-util.h"
37 #define SNDBUF_SIZE (8*1024*1024)
39 static void unsetenv_all(bool unset_environment) {
41 if (!unset_environment)
44 unsetenv("LISTEN_PID");
45 unsetenv("LISTEN_FDS");
46 unsetenv("LISTEN_FDNAMES");
49 _public_ int sd_listen_fds(int unset_environment) {
54 e = getenv("LISTEN_PID");
60 r = parse_pid(e, &pid);
65 if (getpid_cached() != pid) {
70 e = getenv("LISTEN_FDS");
80 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
81 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
86 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
87 r = fd_cloexec(fd, true);
95 unsetenv_all(unset_environment);
99 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
100 _cleanup_strv_free_ char **l = NULL;
102 int n_names = 0, n_fds;
107 return sd_listen_fds(unset_environment);
109 e = getenv("LISTEN_FDNAMES");
111 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
113 unsetenv_all(unset_environment);
121 n_fds = sd_listen_fds(unset_environment);
126 if (n_names != n_fds)
129 r = strv_extend_n(&l, "unknown", n_fds);
134 *names = TAKE_PTR(l);
139 _public_ int sd_is_fifo(int fd, const char *path) {
142 assert_return(fd >= 0, -EBADF);
144 if (fstat(fd, &st_fd) < 0)
147 if (!S_ISFIFO(st_fd.st_mode))
153 if (stat(path, &st_path) < 0) {
155 if (IN_SET(errno, ENOENT, ENOTDIR))
162 st_path.st_dev == st_fd.st_dev &&
163 st_path.st_ino == st_fd.st_ino;
169 _public_ int sd_is_special(int fd, const char *path) {
172 assert_return(fd >= 0, -EBADF);
174 if (fstat(fd, &st_fd) < 0)
177 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
183 if (stat(path, &st_path) < 0) {
185 if (IN_SET(errno, ENOENT, ENOTDIR))
191 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
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;
204 static int sd_is_socket_internal(int fd, int type, int listening) {
207 assert_return(fd >= 0, -EBADF);
208 assert_return(type >= 0, -EINVAL);
210 if (fstat(fd, &st_fd) < 0)
213 if (!S_ISSOCK(st_fd.st_mode))
218 socklen_t l = sizeof(other_type);
220 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
223 if (l != sizeof(other_type))
226 if (other_type != type)
230 if (listening >= 0) {
232 socklen_t l = sizeof(accepting);
234 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
237 if (l != sizeof(accepting))
240 if (!accepting != !listening)
247 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
250 assert_return(fd >= 0, -EBADF);
251 assert_return(family >= 0, -EINVAL);
253 r = sd_is_socket_internal(fd, type, listening);
258 union sockaddr_union sockaddr = {};
259 socklen_t l = sizeof(sockaddr);
261 if (getsockname(fd, &sockaddr.sa, &l) < 0)
264 if (l < sizeof(sa_family_t))
267 return sockaddr.sa.sa_family == family;
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);
278 assert_return(fd >= 0, -EBADF);
279 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
281 r = sd_is_socket_internal(fd, type, listening);
285 if (getsockname(fd, &sockaddr.sa, &l) < 0)
288 if (l < sizeof(sa_family_t))
291 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6))
295 if (sockaddr.sa.sa_family != family)
301 r = sockaddr_port(&sockaddr.sa, &sa_port);
305 return port == sa_port;
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);
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);
321 r = sd_is_socket_internal(fd, type, listening);
325 if (getsockname(fd, &sockaddr.sa, &l) < 0)
328 if (l < sizeof(sa_family_t))
331 if (sockaddr.sa.sa_family != addr->sa_family)
334 if (sockaddr.sa.sa_family == AF_INET) {
335 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
337 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
340 if (in->sin_port != 0 &&
341 sockaddr.in.sin_port != in->sin_port)
344 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
347 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
349 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
352 if (in->sin6_port != 0 &&
353 sockaddr.in6.sin6_port != in->sin6_port)
356 if (in->sin6_flowinfo != 0 &&
357 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
360 if (in->sin6_scope_id != 0 &&
361 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
364 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
365 sizeof(in->sin6_addr.s6_addr)) == 0;
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);
374 assert_return(fd >= 0, -EBADF);
376 r = sd_is_socket_internal(fd, type, listening);
380 if (getsockname(fd, &sockaddr.sa, &l) < 0)
383 if (l < sizeof(sa_family_t))
386 if (sockaddr.sa.sa_family != AF_UNIX)
391 length = strlen(path);
395 return l == offsetof(struct sockaddr_un, sun_path);
398 /* Normal path socket */
400 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
401 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
403 /* Abstract namespace socket */
405 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
406 memcmp(path, sockaddr.un.sun_path, length) == 0;
412 #if 0 /// UNNEEDED by elogind
413 _public_ int sd_is_mq(int fd, const char *path) {
416 /* Check that the fd is valid */
417 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
419 if (mq_getattr(fd, &attr) < 0) {
421 /* A non-mq fd (or an invalid one, but we ruled that out above) */
427 char fpath[PATH_MAX];
430 assert_return(path_is_absolute(path), -EINVAL);
432 if (fstat(fd, &a) < 0)
435 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
436 fpath[sizeof(fpath)-1] = 0;
438 if (stat(fpath, &b) < 0)
441 if (a.st_dev != b.st_dev ||
442 a.st_ino != b.st_ino)
450 _public_ int sd_pid_notify_with_fds(
452 int unset_environment,
457 union sockaddr_union sockaddr = {
458 .sa.sa_family = AF_UNIX,
460 struct iovec iovec = {
461 .iov_base = (char*) state,
463 struct msghdr msghdr = {
466 .msg_name = &sockaddr,
468 _cleanup_close_ int fd = -1;
469 struct cmsghdr *cmsg = NULL;
479 if (n_fds > 0 && !fds) {
484 e = getenv("NOTIFY_SOCKET");
488 /* Must be an abstract socket, or an absolute path */
489 if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
494 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
499 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
505 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
507 iovec.iov_len = strlen(state);
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;
513 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
516 (pid != 0 && pid != getpid_cached()) ||
517 getuid() != geteuid() ||
518 getgid() != getegid();
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);
526 msghdr.msg_control = alloca0(msghdr.msg_controllen);
528 cmsg = CMSG_FIRSTHDR(&msghdr);
530 cmsg->cmsg_level = SOL_SOCKET;
531 cmsg->cmsg_type = SCM_RIGHTS;
532 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
534 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
537 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
543 cmsg->cmsg_level = SOL_SOCKET;
544 cmsg->cmsg_type = SCM_CREDENTIALS;
545 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
547 ucred = (struct ucred*) CMSG_DATA(cmsg);
548 ucred->pid = pid != 0 ? pid : getpid_cached();
549 ucred->uid = getuid();
550 ucred->gid = getgid();
554 /* First try with fake ucred data, as requested */
555 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
560 /* If that failed, try with our own ucred instead */
562 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
563 if (msghdr.msg_controllen == 0)
564 msghdr.msg_control = NULL;
566 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
575 if (unset_environment)
576 unsetenv("NOTIFY_SOCKET");
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);
585 _public_ int sd_notify(int unset_environment, const char *state) {
586 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
589 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
590 _cleanup_free_ char *p = NULL;
596 va_start(ap, format);
597 r = vasprintf(&p, format, ap);
604 return sd_pid_notify(pid, unset_environment, p);
607 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
608 _cleanup_free_ char *p = NULL;
614 va_start(ap, format);
615 r = vasprintf(&p, format, ap);
622 return sd_pid_notify(0, unset_environment, p);
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. */
630 #if 0 /// elogind is always used without systemd running the show. (Well, it should...)
631 return laccess("/run/systemd/system/", F_OK) >= 0;
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 */
642 s = getenv("WATCHDOG_USEC");
646 r = safe_atou64(s, &u);
649 if (u <= 0 || u >= USEC_INFINITY) {
654 p = getenv("WATCHDOG_PID");
658 r = parse_pid(p, &pid);
662 /* Is this for us? */
663 if (getpid_cached() != pid) {
675 if (unset_environment && s)
676 unsetenv("WATCHDOG_USEC");
677 if (unset_environment && p)
678 unsetenv("WATCHDOG_PID");