1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <netinet/in.h>
14 #include <sys/socket.h>
19 #include "sd-daemon.h"
21 #include "alloc-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 //#include "process-util.h"
27 #include "socket-util.h"
31 /// Additional includes needed by elogind
32 #include "process-util.h"
34 #define SNDBUF_SIZE (8*1024*1024)
36 static void unsetenv_all(bool unset_environment) {
38 if (!unset_environment)
41 unsetenv("LISTEN_PID");
42 unsetenv("LISTEN_FDS");
43 unsetenv("LISTEN_FDNAMES");
46 _public_ int sd_listen_fds(int unset_environment) {
51 e = getenv("LISTEN_PID");
57 r = parse_pid(e, &pid);
62 if (getpid_cached() != pid) {
67 e = getenv("LISTEN_FDS");
77 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
78 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
83 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
84 r = fd_cloexec(fd, true);
92 unsetenv_all(unset_environment);
96 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
97 _cleanup_strv_free_ char **l = NULL;
99 int n_names = 0, n_fds;
104 return sd_listen_fds(unset_environment);
106 e = getenv("LISTEN_FDNAMES");
108 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
110 unsetenv_all(unset_environment);
118 n_fds = sd_listen_fds(unset_environment);
123 if (n_names != n_fds)
126 r = strv_extend_n(&l, "unknown", n_fds);
131 *names = TAKE_PTR(l);
136 _public_ int sd_is_fifo(int fd, const char *path) {
139 assert_return(fd >= 0, -EBADF);
141 if (fstat(fd, &st_fd) < 0)
144 if (!S_ISFIFO(st_fd.st_mode))
150 if (stat(path, &st_path) < 0) {
152 if (IN_SET(errno, ENOENT, ENOTDIR))
159 st_path.st_dev == st_fd.st_dev &&
160 st_path.st_ino == st_fd.st_ino;
166 _public_ int sd_is_special(int fd, const char *path) {
169 assert_return(fd >= 0, -EBADF);
171 if (fstat(fd, &st_fd) < 0)
174 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
180 if (stat(path, &st_path) < 0) {
182 if (IN_SET(errno, ENOENT, ENOTDIR))
188 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
190 st_path.st_dev == st_fd.st_dev &&
191 st_path.st_ino == st_fd.st_ino;
192 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
193 return st_path.st_rdev == st_fd.st_rdev;
201 static int sd_is_socket_internal(int fd, int type, int listening) {
204 assert_return(fd >= 0, -EBADF);
205 assert_return(type >= 0, -EINVAL);
207 if (fstat(fd, &st_fd) < 0)
210 if (!S_ISSOCK(st_fd.st_mode))
215 socklen_t l = sizeof(other_type);
217 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
220 if (l != sizeof(other_type))
223 if (other_type != type)
227 if (listening >= 0) {
229 socklen_t l = sizeof(accepting);
231 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
234 if (l != sizeof(accepting))
237 if (!accepting != !listening)
244 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
247 assert_return(fd >= 0, -EBADF);
248 assert_return(family >= 0, -EINVAL);
250 r = sd_is_socket_internal(fd, type, listening);
255 union sockaddr_union sockaddr = {};
256 socklen_t l = sizeof(sockaddr);
258 if (getsockname(fd, &sockaddr.sa, &l) < 0)
261 if (l < sizeof(sa_family_t))
264 return sockaddr.sa.sa_family == family;
270 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
271 union sockaddr_union sockaddr = {};
272 socklen_t l = sizeof(sockaddr);
275 assert_return(fd >= 0, -EBADF);
276 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
278 r = sd_is_socket_internal(fd, type, listening);
282 if (getsockname(fd, &sockaddr.sa, &l) < 0)
285 if (l < sizeof(sa_family_t))
288 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, AF_INET6))
292 if (sockaddr.sa.sa_family != family)
298 r = sockaddr_port(&sockaddr.sa, &sa_port);
302 return port == sa_port;
308 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
309 union sockaddr_union sockaddr = {};
310 socklen_t l = sizeof(sockaddr);
313 assert_return(fd >= 0, -EBADF);
314 assert_return(addr, -EINVAL);
315 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
316 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
318 r = sd_is_socket_internal(fd, type, listening);
322 if (getsockname(fd, &sockaddr.sa, &l) < 0)
325 if (l < sizeof(sa_family_t))
328 if (sockaddr.sa.sa_family != addr->sa_family)
331 if (sockaddr.sa.sa_family == AF_INET) {
332 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
334 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
337 if (in->sin_port != 0 &&
338 sockaddr.in.sin_port != in->sin_port)
341 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
344 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
346 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
349 if (in->sin6_port != 0 &&
350 sockaddr.in6.sin6_port != in->sin6_port)
353 if (in->sin6_flowinfo != 0 &&
354 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
357 if (in->sin6_scope_id != 0 &&
358 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
361 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
362 sizeof(in->sin6_addr.s6_addr)) == 0;
366 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
367 union sockaddr_union sockaddr = {};
368 socklen_t l = sizeof(sockaddr);
371 assert_return(fd >= 0, -EBADF);
373 r = sd_is_socket_internal(fd, type, listening);
377 if (getsockname(fd, &sockaddr.sa, &l) < 0)
380 if (l < sizeof(sa_family_t))
383 if (sockaddr.sa.sa_family != AF_UNIX)
388 length = strlen(path);
392 return l == offsetof(struct sockaddr_un, sun_path);
395 /* Normal path socket */
397 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
398 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
400 /* Abstract namespace socket */
402 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
403 memcmp(path, sockaddr.un.sun_path, length) == 0;
409 #if 0 /// UNNEEDED by elogind
410 _public_ int sd_is_mq(int fd, const char *path) {
413 /* Check that the fd is valid */
414 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
416 if (mq_getattr(fd, &attr) < 0) {
418 /* A non-mq fd (or an invalid one, but we ruled that out above) */
424 char fpath[PATH_MAX];
427 assert_return(path_is_absolute(path), -EINVAL);
429 if (fstat(fd, &a) < 0)
432 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
433 fpath[sizeof(fpath)-1] = 0;
435 if (stat(fpath, &b) < 0)
438 if (a.st_dev != b.st_dev ||
439 a.st_ino != b.st_ino)
447 _public_ int sd_pid_notify_with_fds(
449 int unset_environment,
454 union sockaddr_union sockaddr = {
455 .sa.sa_family = AF_UNIX,
457 struct iovec iovec = {
458 .iov_base = (char*) state,
460 struct msghdr msghdr = {
463 .msg_name = &sockaddr,
465 _cleanup_close_ int fd = -1;
466 struct cmsghdr *cmsg = NULL;
476 if (n_fds > 0 && !fds) {
481 e = getenv("NOTIFY_SOCKET");
485 /* Must be an abstract socket, or an absolute path */
486 if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
491 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
496 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
502 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
504 iovec.iov_len = strlen(state);
506 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
507 if (sockaddr.un.sun_path[0] == '@')
508 sockaddr.un.sun_path[0] = 0;
510 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
513 (pid != 0 && pid != getpid_cached()) ||
514 getuid() != geteuid() ||
515 getgid() != getegid();
517 if (n_fds > 0 || send_ucred) {
518 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
519 msghdr.msg_controllen =
520 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
521 (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0);
523 msghdr.msg_control = alloca0(msghdr.msg_controllen);
525 cmsg = CMSG_FIRSTHDR(&msghdr);
527 cmsg->cmsg_level = SOL_SOCKET;
528 cmsg->cmsg_type = SCM_RIGHTS;
529 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
531 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
534 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
540 cmsg->cmsg_level = SOL_SOCKET;
541 cmsg->cmsg_type = SCM_CREDENTIALS;
542 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
544 ucred = (struct ucred*) CMSG_DATA(cmsg);
545 ucred->pid = pid != 0 ? pid : getpid_cached();
546 ucred->uid = getuid();
547 ucred->gid = getgid();
551 /* First try with fake ucred data, as requested */
552 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
557 /* If that failed, try with our own ucred instead */
559 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
560 if (msghdr.msg_controllen == 0)
561 msghdr.msg_control = NULL;
563 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
572 if (unset_environment)
573 unsetenv("NOTIFY_SOCKET");
578 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
579 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
582 _public_ int sd_notify(int unset_environment, const char *state) {
583 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
586 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
587 _cleanup_free_ char *p = NULL;
593 va_start(ap, format);
594 r = vasprintf(&p, format, ap);
601 return sd_pid_notify(pid, unset_environment, p);
604 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
605 _cleanup_free_ char *p = NULL;
611 va_start(ap, format);
612 r = vasprintf(&p, format, ap);
619 return sd_pid_notify(0, unset_environment, p);
622 _public_ int sd_booted(void) {
623 /* We test whether the runtime unit file directory has been
624 * created. This takes place in mount-setup.c, so is
625 * guaranteed to happen very early during boot. */
627 #if 0 /// elogind is always used without systemd running the show. (Well, it should...)
628 return laccess("/run/systemd/system/", F_OK) >= 0;
634 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
635 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
639 s = getenv("WATCHDOG_USEC");
643 r = safe_atou64(s, &u);
646 if (u <= 0 || u >= USEC_INFINITY) {
651 p = getenv("WATCHDOG_PID");
655 r = parse_pid(p, &pid);
659 /* Is this for us? */
660 if (getpid_cached() != pid) {
672 if (unset_environment && s)
673 unsetenv("WATCHDOG_USEC");
674 if (unset_environment && p)
675 unsetenv("WATCHDOG_PID");