2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include <netinet/in.h>
29 #include <sys/socket.h>
34 #include "sd-daemon.h"
36 #include "alloc-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "socket-util.h"
45 #define SNDBUF_SIZE (8*1024*1024)
47 #if 0 /// UNNEEDED by elogind
48 static void unsetenv_all(bool unset_environment) {
50 if (!unset_environment)
53 unsetenv("LISTEN_PID");
54 unsetenv("LISTEN_FDS");
55 unsetenv("LISTEN_FDNAMES");
58 _public_ int sd_listen_fds(int unset_environment) {
63 e = getenv("LISTEN_PID");
69 r = parse_pid(e, &pid);
74 if (getpid() != pid) {
79 e = getenv("LISTEN_FDS");
89 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
90 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
95 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
96 r = fd_cloexec(fd, true);
104 unsetenv_all(unset_environment);
108 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
109 _cleanup_strv_free_ char **l = NULL;
111 int n_names = 0, n_fds;
116 return sd_listen_fds(unset_environment);
118 e = getenv("LISTEN_FDNAMES");
120 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
122 unsetenv_all(unset_environment);
130 n_fds = sd_listen_fds(unset_environment);
135 if (n_names != n_fds)
138 r = strv_extend_n(&l, "unknown", n_fds);
150 _public_ int sd_is_fifo(int fd, const char *path) {
153 assert_return(fd >= 0, -EBADF);
155 if (fstat(fd, &st_fd) < 0)
158 if (!S_ISFIFO(st_fd.st_mode))
164 if (stat(path, &st_path) < 0) {
166 if (errno == ENOENT || errno == ENOTDIR)
173 st_path.st_dev == st_fd.st_dev &&
174 st_path.st_ino == st_fd.st_ino;
180 _public_ int sd_is_special(int fd, const char *path) {
183 assert_return(fd >= 0, -EBADF);
185 if (fstat(fd, &st_fd) < 0)
188 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
194 if (stat(path, &st_path) < 0) {
196 if (errno == ENOENT || errno == ENOTDIR)
202 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
204 st_path.st_dev == st_fd.st_dev &&
205 st_path.st_ino == st_fd.st_ino;
206 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
207 return st_path.st_rdev == st_fd.st_rdev;
215 static int sd_is_socket_internal(int fd, int type, int listening) {
218 assert_return(fd >= 0, -EBADF);
219 assert_return(type >= 0, -EINVAL);
221 if (fstat(fd, &st_fd) < 0)
224 if (!S_ISSOCK(st_fd.st_mode))
229 socklen_t l = sizeof(other_type);
231 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
234 if (l != sizeof(other_type))
237 if (other_type != type)
241 if (listening >= 0) {
243 socklen_t l = sizeof(accepting);
245 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
248 if (l != sizeof(accepting))
251 if (!accepting != !listening)
258 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
261 assert_return(fd >= 0, -EBADF);
262 assert_return(family >= 0, -EINVAL);
264 r = sd_is_socket_internal(fd, type, listening);
269 union sockaddr_union sockaddr = {};
270 socklen_t l = sizeof(sockaddr);
272 if (getsockname(fd, &sockaddr.sa, &l) < 0)
275 if (l < sizeof(sa_family_t))
278 return sockaddr.sa.sa_family == family;
284 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
285 union sockaddr_union sockaddr = {};
286 socklen_t l = sizeof(sockaddr);
289 assert_return(fd >= 0, -EBADF);
290 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
292 r = sd_is_socket_internal(fd, type, listening);
296 if (getsockname(fd, &sockaddr.sa, &l) < 0)
299 if (l < sizeof(sa_family_t))
302 if (sockaddr.sa.sa_family != AF_INET &&
303 sockaddr.sa.sa_family != AF_INET6)
307 if (sockaddr.sa.sa_family != family)
311 if (sockaddr.sa.sa_family == AF_INET) {
312 if (l < sizeof(struct sockaddr_in))
315 return htobe16(port) == sockaddr.in.sin_port;
317 if (l < sizeof(struct sockaddr_in6))
320 return htobe16(port) == sockaddr.in6.sin6_port;
327 #if 0 /// UNNEEDED by elogind
328 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
329 union sockaddr_union sockaddr = {};
330 socklen_t l = sizeof(sockaddr);
333 assert_return(fd >= 0, -EBADF);
334 assert_return(addr, -EINVAL);
335 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
336 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
338 r = sd_is_socket_internal(fd, type, listening);
342 if (getsockname(fd, &sockaddr.sa, &l) < 0)
345 if (l < sizeof(sa_family_t))
348 if (sockaddr.sa.sa_family != addr->sa_family)
351 if (sockaddr.sa.sa_family == AF_INET) {
352 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
354 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
357 if (in->sin_port != 0 &&
358 sockaddr.in.sin_port != in->sin_port)
361 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
364 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
366 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
369 if (in->sin6_port != 0 &&
370 sockaddr.in6.sin6_port != in->sin6_port)
373 if (in->sin6_flowinfo != 0 &&
374 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
377 if (in->sin6_scope_id != 0 &&
378 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
381 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
382 sizeof(in->sin6_addr.s6_addr)) == 0;
387 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
388 union sockaddr_union sockaddr = {};
389 socklen_t l = sizeof(sockaddr);
392 assert_return(fd >= 0, -EBADF);
394 r = sd_is_socket_internal(fd, type, listening);
398 if (getsockname(fd, &sockaddr.sa, &l) < 0)
401 if (l < sizeof(sa_family_t))
404 if (sockaddr.sa.sa_family != AF_UNIX)
409 length = strlen(path);
413 return l == offsetof(struct sockaddr_un, sun_path);
416 /* Normal path socket */
418 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
419 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
421 /* Abstract namespace socket */
423 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
424 memcmp(path, sockaddr.un.sun_path, length) == 0;
430 #if 0 /// UNNEEDED by elogind
431 _public_ int sd_is_mq(int fd, const char *path) {
434 /* Check that the fd is valid */
435 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
437 if (mq_getattr(fd, &attr) < 0) {
439 /* A non-mq fd (or an invalid one, but we ruled that out above) */
445 char fpath[PATH_MAX];
448 assert_return(path_is_absolute(path), -EINVAL);
450 if (fstat(fd, &a) < 0)
453 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
454 fpath[sizeof(fpath)-1] = 0;
456 if (stat(fpath, &b) < 0)
459 if (a.st_dev != b.st_dev ||
460 a.st_ino != b.st_ino)
468 _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
469 union sockaddr_union sockaddr = {
470 .sa.sa_family = AF_UNIX,
472 struct iovec iovec = {
473 .iov_base = (char*) state,
475 struct msghdr msghdr = {
478 .msg_name = &sockaddr,
480 _cleanup_close_ int fd = -1;
481 struct cmsghdr *cmsg = NULL;
491 if (n_fds > 0 && !fds) {
496 e = getenv("NOTIFY_SOCKET");
500 /* Must be an abstract socket, or an absolute path */
501 if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
506 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
511 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
517 fd_inc_sndbuf(fd, SNDBUF_SIZE);
519 iovec.iov_len = strlen(state);
521 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
522 if (sockaddr.un.sun_path[0] == '@')
523 sockaddr.un.sun_path[0] = 0;
525 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
527 have_pid = pid != 0 && pid != getpid();
529 if (n_fds > 0 || have_pid) {
530 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
531 msghdr.msg_controllen =
532 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
533 (have_pid ? CMSG_SPACE(sizeof(struct ucred)) : 0);
535 msghdr.msg_control = alloca0(msghdr.msg_controllen);
537 cmsg = CMSG_FIRSTHDR(&msghdr);
539 cmsg->cmsg_level = SOL_SOCKET;
540 cmsg->cmsg_type = SCM_RIGHTS;
541 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
543 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
546 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
552 cmsg->cmsg_level = SOL_SOCKET;
553 cmsg->cmsg_type = SCM_CREDENTIALS;
554 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
556 ucred = (struct ucred*) CMSG_DATA(cmsg);
558 ucred->uid = getuid();
559 ucred->gid = getgid();
563 /* First try with fake ucred data, as requested */
564 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
569 /* If that failed, try with our own ucred instead */
571 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
572 if (msghdr.msg_controllen == 0)
573 msghdr.msg_control = NULL;
575 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
584 if (unset_environment)
585 unsetenv("NOTIFY_SOCKET");
590 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
591 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
594 _public_ int sd_notify(int unset_environment, const char *state) {
595 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
598 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
599 _cleanup_free_ char *p = NULL;
605 va_start(ap, format);
606 r = vasprintf(&p, format, ap);
613 return sd_pid_notify(pid, unset_environment, p);
616 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
617 _cleanup_free_ char *p = NULL;
623 va_start(ap, format);
624 r = vasprintf(&p, format, ap);
631 return sd_pid_notify(0, unset_environment, p);
634 _public_ int sd_booted(void) {
635 #if 0 /// With elogind, the system is (should never be) booted by systemd
636 /* We test whether the runtime unit file directory has been
637 * created. This takes place in mount-setup.c, so is
638 * guaranteed to happen very early during boot. */
640 return laccess("/run/systemd/system/", F_OK) >= 0;
646 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
647 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
651 s = getenv("WATCHDOG_USEC");
655 r = safe_atou64(s, &u);
658 if (u <= 0 || u >= USEC_INFINITY) {
663 p = getenv("WATCHDOG_PID");
667 r = parse_pid(p, &pid);
671 /* Is this for us? */
672 if (getpid() != pid) {
684 if (unset_environment && s)
685 unsetenv("WATCHDOG_USEC");
686 if (unset_environment && p)
687 unsetenv("WATCHDOG_PID");