1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
9 #include <netinet/in.h>
15 #include <sys/socket.h>
20 #include "sd-daemon.h"
22 #include "alloc-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 //#include "process-util.h"
28 #include "socket-util.h"
32 /// Additional includes needed by elogind
33 #include "process-util.h"
35 #define SNDBUF_SIZE (8*1024*1024)
37 static void unsetenv_all(bool unset_environment) {
39 if (!unset_environment)
42 unsetenv("LISTEN_PID");
43 unsetenv("LISTEN_FDS");
44 unsetenv("LISTEN_FDNAMES");
47 _public_ int sd_listen_fds(int unset_environment) {
52 e = getenv("LISTEN_PID");
58 r = parse_pid(e, &pid);
63 if (getpid_cached() != pid) {
68 e = getenv("LISTEN_FDS");
78 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
79 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
84 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
85 r = fd_cloexec(fd, true);
93 unsetenv_all(unset_environment);
97 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
98 _cleanup_strv_free_ char **l = NULL;
100 int n_names = 0, n_fds;
105 return sd_listen_fds(unset_environment);
107 e = getenv("LISTEN_FDNAMES");
109 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
111 unsetenv_all(unset_environment);
119 n_fds = sd_listen_fds(unset_environment);
124 if (n_names != n_fds)
127 r = strv_extend_n(&l, "unknown", n_fds);
132 *names = TAKE_PTR(l);
137 _public_ int sd_is_fifo(int fd, const char *path) {
140 assert_return(fd >= 0, -EBADF);
142 if (fstat(fd, &st_fd) < 0)
145 if (!S_ISFIFO(st_fd.st_mode))
151 if (stat(path, &st_path) < 0) {
153 if (IN_SET(errno, ENOENT, ENOTDIR))
160 st_path.st_dev == st_fd.st_dev &&
161 st_path.st_ino == st_fd.st_ino;
167 _public_ int sd_is_special(int fd, const char *path) {
170 assert_return(fd >= 0, -EBADF);
172 if (fstat(fd, &st_fd) < 0)
175 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
181 if (stat(path, &st_path) < 0) {
183 if (IN_SET(errno, ENOENT, ENOTDIR))
189 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
191 st_path.st_dev == st_fd.st_dev &&
192 st_path.st_ino == st_fd.st_ino;
193 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
194 return st_path.st_rdev == st_fd.st_rdev;
202 static int sd_is_socket_internal(int fd, int type, int listening) {
205 assert_return(fd >= 0, -EBADF);
206 assert_return(type >= 0, -EINVAL);
208 if (fstat(fd, &st_fd) < 0)
211 if (!S_ISSOCK(st_fd.st_mode))
216 socklen_t l = sizeof(other_type);
218 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
221 if (l != sizeof(other_type))
224 if (other_type != type)
228 if (listening >= 0) {
230 socklen_t l = sizeof(accepting);
232 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
235 if (l != sizeof(accepting))
238 if (!accepting != !listening)
245 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
248 assert_return(fd >= 0, -EBADF);
249 assert_return(family >= 0, -EINVAL);
251 r = sd_is_socket_internal(fd, type, listening);
256 union sockaddr_union sockaddr = {};
257 socklen_t l = sizeof(sockaddr);
259 if (getsockname(fd, &sockaddr.sa, &l) < 0)
262 if (l < sizeof(sa_family_t))
265 return sockaddr.sa.sa_family == family;
271 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
272 union sockaddr_union sockaddr = {};
273 socklen_t l = sizeof(sockaddr);
276 assert_return(fd >= 0, -EBADF);
277 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
279 r = sd_is_socket_internal(fd, type, listening);
283 if (getsockname(fd, &sockaddr.sa, &l) < 0)
286 if (l < sizeof(sa_family_t))
289 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6))
293 if (sockaddr.sa.sa_family != family)
299 r = sockaddr_port(&sockaddr.sa, &sa_port);
303 return port == sa_port;
309 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
310 union sockaddr_union sockaddr = {};
311 socklen_t l = sizeof(sockaddr);
314 assert_return(fd >= 0, -EBADF);
315 assert_return(addr, -EINVAL);
316 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
317 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
319 r = sd_is_socket_internal(fd, type, listening);
323 if (getsockname(fd, &sockaddr.sa, &l) < 0)
326 if (l < sizeof(sa_family_t))
329 if (sockaddr.sa.sa_family != addr->sa_family)
332 if (sockaddr.sa.sa_family == AF_INET) {
333 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
335 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
338 if (in->sin_port != 0 &&
339 sockaddr.in.sin_port != in->sin_port)
342 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
345 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
347 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
350 if (in->sin6_port != 0 &&
351 sockaddr.in6.sin6_port != in->sin6_port)
354 if (in->sin6_flowinfo != 0 &&
355 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
358 if (in->sin6_scope_id != 0 &&
359 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
362 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
363 sizeof(in->sin6_addr.s6_addr)) == 0;
367 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
368 union sockaddr_union sockaddr = {};
369 socklen_t l = sizeof(sockaddr);
372 assert_return(fd >= 0, -EBADF);
374 r = sd_is_socket_internal(fd, type, listening);
378 if (getsockname(fd, &sockaddr.sa, &l) < 0)
381 if (l < sizeof(sa_family_t))
384 if (sockaddr.sa.sa_family != AF_UNIX)
389 length = strlen(path);
393 return l == offsetof(struct sockaddr_un, sun_path);
396 /* Normal path socket */
398 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
399 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
401 /* Abstract namespace socket */
403 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
404 memcmp(path, sockaddr.un.sun_path, length) == 0;
410 #if 0 /// UNNEEDED by elogind
411 _public_ int sd_is_mq(int fd, const char *path) {
414 /* Check that the fd is valid */
415 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
417 if (mq_getattr(fd, &attr) < 0) {
419 /* A non-mq fd (or an invalid one, but we ruled that out above) */
425 char fpath[PATH_MAX];
428 assert_return(path_is_absolute(path), -EINVAL);
430 if (fstat(fd, &a) < 0)
433 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
434 fpath[sizeof(fpath)-1] = 0;
436 if (stat(fpath, &b) < 0)
439 if (a.st_dev != b.st_dev ||
440 a.st_ino != b.st_ino)
448 _public_ int sd_pid_notify_with_fds(
450 int unset_environment,
455 union sockaddr_union sockaddr = {
456 .sa.sa_family = AF_UNIX,
458 struct iovec iovec = {
459 .iov_base = (char*) state,
461 struct msghdr msghdr = {
464 .msg_name = &sockaddr,
466 _cleanup_close_ int fd = -1;
467 struct cmsghdr *cmsg = NULL;
477 if (n_fds > 0 && !fds) {
482 e = getenv("NOTIFY_SOCKET");
486 /* Must be an abstract socket, or an absolute path */
487 if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
492 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
497 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
503 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
505 iovec.iov_len = strlen(state);
507 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
508 if (sockaddr.un.sun_path[0] == '@')
509 sockaddr.un.sun_path[0] = 0;
511 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
514 (pid != 0 && pid != getpid_cached()) ||
515 getuid() != geteuid() ||
516 getgid() != getegid();
518 if (n_fds > 0 || send_ucred) {
519 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
520 msghdr.msg_controllen =
521 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
522 (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0);
524 msghdr.msg_control = alloca0(msghdr.msg_controllen);
526 cmsg = CMSG_FIRSTHDR(&msghdr);
528 cmsg->cmsg_level = SOL_SOCKET;
529 cmsg->cmsg_type = SCM_RIGHTS;
530 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
532 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
535 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
541 cmsg->cmsg_level = SOL_SOCKET;
542 cmsg->cmsg_type = SCM_CREDENTIALS;
543 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
545 ucred = (struct ucred*) CMSG_DATA(cmsg);
546 ucred->pid = pid != 0 ? pid : getpid_cached();
547 ucred->uid = getuid();
548 ucred->gid = getgid();
552 /* First try with fake ucred data, as requested */
553 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
558 /* If that failed, try with our own ucred instead */
560 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
561 if (msghdr.msg_controllen == 0)
562 msghdr.msg_control = NULL;
564 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
573 if (unset_environment)
574 unsetenv("NOTIFY_SOCKET");
579 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
580 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
583 _public_ int sd_notify(int unset_environment, const char *state) {
584 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
587 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
588 _cleanup_free_ char *p = NULL;
594 va_start(ap, format);
595 r = vasprintf(&p, format, ap);
602 return sd_pid_notify(pid, unset_environment, p);
605 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
606 _cleanup_free_ char *p = NULL;
612 va_start(ap, format);
613 r = vasprintf(&p, format, ap);
620 return sd_pid_notify(0, unset_environment, p);
623 _public_ int sd_booted(void) {
624 /* We test whether the runtime unit file directory has been
625 * created. This takes place in mount-setup.c, so is
626 * guaranteed to happen very early during boot. */
628 #if 0 /// elogind is always used without systemd running the show. (Well, it should...)
629 return laccess("/run/systemd/system/", F_OK) >= 0;
635 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
636 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
640 s = getenv("WATCHDOG_USEC");
644 r = safe_atou64(s, &u);
647 if (u <= 0 || u >= USEC_INFINITY) {
652 p = getenv("WATCHDOG_PID");
656 r = parse_pid(p, &pid);
660 /* Is this for us? */
661 if (getpid_cached() != pid) {
673 if (unset_environment && s)
674 unsetenv("WATCHDOG_USEC");
675 if (unset_environment && p)
676 unsetenv("WATCHDOG_PID");