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 /// Additional includes needed by elogind
46 #include "process-util.h"
48 #define SNDBUF_SIZE (8*1024*1024)
50 static void unsetenv_all(bool unset_environment) {
52 if (!unset_environment)
55 unsetenv("LISTEN_PID");
56 unsetenv("LISTEN_FDS");
57 unsetenv("LISTEN_FDNAMES");
60 _public_ int sd_listen_fds(int unset_environment) {
65 e = getenv("LISTEN_PID");
71 r = parse_pid(e, &pid);
76 if (getpid_cached() != pid) {
81 e = getenv("LISTEN_FDS");
91 assert_cc(SD_LISTEN_FDS_START < INT_MAX);
92 if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
97 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
98 r = fd_cloexec(fd, true);
106 unsetenv_all(unset_environment);
110 _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
111 _cleanup_strv_free_ char **l = NULL;
113 int n_names = 0, n_fds;
118 return sd_listen_fds(unset_environment);
120 e = getenv("LISTEN_FDNAMES");
122 n_names = strv_split_extract(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
124 unsetenv_all(unset_environment);
132 n_fds = sd_listen_fds(unset_environment);
137 if (n_names != n_fds)
140 r = strv_extend_n(&l, "unknown", n_fds);
151 _public_ int sd_is_fifo(int fd, const char *path) {
154 assert_return(fd >= 0, -EBADF);
156 if (fstat(fd, &st_fd) < 0)
159 if (!S_ISFIFO(st_fd.st_mode))
165 if (stat(path, &st_path) < 0) {
167 if (IN_SET(errno, ENOENT, ENOTDIR))
174 st_path.st_dev == st_fd.st_dev &&
175 st_path.st_ino == st_fd.st_ino;
181 _public_ int sd_is_special(int fd, const char *path) {
184 assert_return(fd >= 0, -EBADF);
186 if (fstat(fd, &st_fd) < 0)
189 if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
195 if (stat(path, &st_path) < 0) {
197 if (IN_SET(errno, ENOENT, ENOTDIR))
203 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
205 st_path.st_dev == st_fd.st_dev &&
206 st_path.st_ino == st_fd.st_ino;
207 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
208 return st_path.st_rdev == st_fd.st_rdev;
216 static int sd_is_socket_internal(int fd, int type, int listening) {
219 assert_return(fd >= 0, -EBADF);
220 assert_return(type >= 0, -EINVAL);
222 if (fstat(fd, &st_fd) < 0)
225 if (!S_ISSOCK(st_fd.st_mode))
230 socklen_t l = sizeof(other_type);
232 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
235 if (l != sizeof(other_type))
238 if (other_type != type)
242 if (listening >= 0) {
244 socklen_t l = sizeof(accepting);
246 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
249 if (l != sizeof(accepting))
252 if (!accepting != !listening)
259 _public_ int sd_is_socket(int fd, int family, int type, int listening) {
262 assert_return(fd >= 0, -EBADF);
263 assert_return(family >= 0, -EINVAL);
265 r = sd_is_socket_internal(fd, type, listening);
270 union sockaddr_union sockaddr = {};
271 socklen_t l = sizeof(sockaddr);
273 if (getsockname(fd, &sockaddr.sa, &l) < 0)
276 if (l < sizeof(sa_family_t))
279 return sockaddr.sa.sa_family == family;
285 _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
286 union sockaddr_union sockaddr = {};
287 socklen_t l = sizeof(sockaddr);
290 assert_return(fd >= 0, -EBADF);
291 assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
293 r = sd_is_socket_internal(fd, type, listening);
297 if (getsockname(fd, &sockaddr.sa, &l) < 0)
300 if (l < sizeof(sa_family_t))
303 if (!IN_SET(sockaddr.sa.sa_family, AF_INET, 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 _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening) {
328 union sockaddr_union sockaddr = {};
329 socklen_t l = sizeof(sockaddr);
332 assert_return(fd >= 0, -EBADF);
333 assert_return(addr, -EINVAL);
334 assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
335 assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
337 r = sd_is_socket_internal(fd, type, listening);
341 if (getsockname(fd, &sockaddr.sa, &l) < 0)
344 if (l < sizeof(sa_family_t))
347 if (sockaddr.sa.sa_family != addr->sa_family)
350 if (sockaddr.sa.sa_family == AF_INET) {
351 const struct sockaddr_in *in = (const struct sockaddr_in *) addr;
353 if (l < sizeof(struct sockaddr_in) || addr_len < sizeof(struct sockaddr_in))
356 if (in->sin_port != 0 &&
357 sockaddr.in.sin_port != in->sin_port)
360 return sockaddr.in.sin_addr.s_addr == in->sin_addr.s_addr;
363 const struct sockaddr_in6 *in = (const struct sockaddr_in6 *) addr;
365 if (l < sizeof(struct sockaddr_in6) || addr_len < sizeof(struct sockaddr_in6))
368 if (in->sin6_port != 0 &&
369 sockaddr.in6.sin6_port != in->sin6_port)
372 if (in->sin6_flowinfo != 0 &&
373 sockaddr.in6.sin6_flowinfo != in->sin6_flowinfo)
376 if (in->sin6_scope_id != 0 &&
377 sockaddr.in6.sin6_scope_id != in->sin6_scope_id)
380 return memcmp(sockaddr.in6.sin6_addr.s6_addr, in->sin6_addr.s6_addr,
381 sizeof(in->sin6_addr.s6_addr)) == 0;
385 _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
386 union sockaddr_union sockaddr = {};
387 socklen_t l = sizeof(sockaddr);
390 assert_return(fd >= 0, -EBADF);
392 r = sd_is_socket_internal(fd, type, listening);
396 if (getsockname(fd, &sockaddr.sa, &l) < 0)
399 if (l < sizeof(sa_family_t))
402 if (sockaddr.sa.sa_family != AF_UNIX)
407 length = strlen(path);
411 return l == offsetof(struct sockaddr_un, sun_path);
414 /* Normal path socket */
416 (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
417 memcmp(path, sockaddr.un.sun_path, length+1) == 0;
419 /* Abstract namespace socket */
421 (l == offsetof(struct sockaddr_un, sun_path) + length) &&
422 memcmp(path, sockaddr.un.sun_path, length) == 0;
428 #if 0 /// UNNEEDED by elogind
429 _public_ int sd_is_mq(int fd, const char *path) {
432 /* Check that the fd is valid */
433 assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
435 if (mq_getattr(fd, &attr) < 0) {
437 /* A non-mq fd (or an invalid one, but we ruled that out above) */
443 char fpath[PATH_MAX];
446 assert_return(path_is_absolute(path), -EINVAL);
448 if (fstat(fd, &a) < 0)
451 strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
452 fpath[sizeof(fpath)-1] = 0;
454 if (stat(fpath, &b) < 0)
457 if (a.st_dev != b.st_dev ||
458 a.st_ino != b.st_ino)
466 _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
467 union sockaddr_union sockaddr = {
468 .sa.sa_family = AF_UNIX,
470 struct iovec iovec = {
471 .iov_base = (char*) state,
473 struct msghdr msghdr = {
476 .msg_name = &sockaddr,
478 _cleanup_close_ int fd = -1;
479 struct cmsghdr *cmsg = NULL;
489 if (n_fds > 0 && !fds) {
494 e = getenv("NOTIFY_SOCKET");
498 /* Must be an abstract socket, or an absolute path */
499 if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
504 if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
509 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
515 fd_inc_sndbuf(fd, SNDBUF_SIZE);
517 iovec.iov_len = strlen(state);
519 strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
520 if (sockaddr.un.sun_path[0] == '@')
521 sockaddr.un.sun_path[0] = 0;
523 msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
525 have_pid = pid != 0 && pid != getpid_cached();
527 if (n_fds > 0 || have_pid) {
528 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
529 msghdr.msg_controllen =
530 (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
531 (have_pid ? CMSG_SPACE(sizeof(struct ucred)) : 0);
533 msghdr.msg_control = alloca0(msghdr.msg_controllen);
535 cmsg = CMSG_FIRSTHDR(&msghdr);
537 cmsg->cmsg_level = SOL_SOCKET;
538 cmsg->cmsg_type = SCM_RIGHTS;
539 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
541 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
544 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
550 cmsg->cmsg_level = SOL_SOCKET;
551 cmsg->cmsg_type = SCM_CREDENTIALS;
552 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
554 ucred = (struct ucred*) CMSG_DATA(cmsg);
556 ucred->uid = getuid();
557 ucred->gid = getgid();
561 /* First try with fake ucred data, as requested */
562 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
567 /* If that failed, try with our own ucred instead */
569 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
570 if (msghdr.msg_controllen == 0)
571 msghdr.msg_control = NULL;
573 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
582 if (unset_environment)
583 unsetenv("NOTIFY_SOCKET");
588 _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
589 return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
592 _public_ int sd_notify(int unset_environment, const char *state) {
593 return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
596 _public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
597 _cleanup_free_ char *p = NULL;
603 va_start(ap, format);
604 r = vasprintf(&p, format, ap);
611 return sd_pid_notify(pid, unset_environment, p);
614 _public_ int sd_notifyf(int unset_environment, const char *format, ...) {
615 _cleanup_free_ char *p = NULL;
621 va_start(ap, format);
622 r = vasprintf(&p, format, ap);
629 return sd_pid_notify(0, unset_environment, p);
632 _public_ int sd_booted(void) {
633 /* We test whether the runtime unit file directory has been
634 * created. This takes place in mount-setup.c, so is
635 * guaranteed to happen very early during boot. */
637 return laccess("/run/systemd/system/", F_OK) >= 0;
640 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
641 const char *s, *p = ""; /* p is set to dummy value to do unsetting */
645 s = getenv("WATCHDOG_USEC");
649 r = safe_atou64(s, &u);
652 if (u <= 0 || u >= USEC_INFINITY) {
657 p = getenv("WATCHDOG_PID");
661 r = parse_pid(p, &pid);
665 /* Is this for us? */
666 if (getpid_cached() != pid) {
678 if (unset_environment && s)
679 unsetenv("WATCHDOG_USEC");
680 if (unset_environment && p)
681 unsetenv("WATCHDOG_PID");