X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd%2Fsd-daemon%2Fsd-daemon.c;h=c9e2f2dba83f4590a831134f276125df90d70f9b;hb=2eec67acbb00593e414549a7e5b35eb7dd776b1b;hp=21fb346bf821829297a6ebcf3ed11b927d0917cc;hpb=0ebee8818404adb95a0b8a01416aad3a16f64ae1;p=elogind.git diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index 21fb346bf..c9e2f2dba 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -19,11 +19,9 @@ along with systemd; If not, see . ***/ -#include #include #include #include -#include #include #include #include @@ -36,13 +34,15 @@ #include #include "util.h" +#include "path-util.h" +#include "socket-util.h" #include "sd-daemon.h" _public_ int sd_listen_fds(int unset_environment) { - int r, fd; const char *e; - char *p = NULL; - unsigned long l; + unsigned n; + int r, fd; + pid_t pid; e = getenv("LISTEN_PID"); if (!e) { @@ -50,21 +50,12 @@ _public_ int sd_listen_fds(int unset_environment) { goto finish; } - errno = 0; - l = strtoul(e, &p, 10); - - if (errno > 0) { - r = -errno; + r = parse_pid(e, &pid); + if (r < 0) goto finish; - } - - if (!p || p == e || *p || l <= 0) { - r = -EINVAL; - goto finish; - } /* Is this for us? */ - if (getpid() != (pid_t) l) { + if (getpid() != pid) { r = 0; goto finish; } @@ -75,38 +66,17 @@ _public_ int sd_listen_fds(int unset_environment) { goto finish; } - errno = 0; - l = strtoul(e, &p, 10); - - if (errno > 0) { - r = -errno; - goto finish; - } - - if (!p || p == e || *p) { - r = -EINVAL; + r = safe_atou(e, &n); + if (r < 0) goto finish; - } - - for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) { - int flags; - flags = fcntl(fd, F_GETFD); - if (flags < 0) { - r = -errno; + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) n; fd ++) { + r = fd_cloexec(fd, true); + if (r < 0) goto finish; - } - - if (flags & FD_CLOEXEC) - continue; - - if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) { - r = -errno; - goto finish; - } } - r = (int) l; + r = (int) n; finish: if (unset_environment) { @@ -120,8 +90,7 @@ finish: _public_ int sd_is_fifo(int fd, const char *path) { struct stat st_fd; - if (fd < 0) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); if (fstat(fd, &st_fd) < 0) return -errno; @@ -151,8 +120,7 @@ _public_ int sd_is_fifo(int fd, const char *path) { _public_ int sd_is_special(int fd, const char *path) { struct stat st_fd; - if (fd < 0) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); if (fstat(fd, &st_fd) < 0) return -errno; @@ -187,8 +155,8 @@ _public_ int sd_is_special(int fd, const char *path) { static int sd_is_socket_internal(int fd, int type, int listening) { struct stat st_fd; - if (fd < 0 || type < 0) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); + assert_return(type >= 0, -EINVAL); if (fstat(fd, &st_fd) < 0) return -errno; @@ -227,19 +195,11 @@ static int sd_is_socket_internal(int fd, int type, int listening) { return 1; } -union sockaddr_union { - struct sockaddr sa; - struct sockaddr_in in4; - struct sockaddr_in6 in6; - struct sockaddr_un un; - struct sockaddr_storage storage; -}; - _public_ int sd_is_socket(int fd, int family, int type, int listening) { int r; - if (family < 0) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); + assert_return(family >= 0, -EINVAL); r = sd_is_socket_internal(fd, type, listening); if (r <= 0) @@ -266,8 +226,8 @@ _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint socklen_t l = sizeof(sockaddr); int r; - if (family != 0 && family != AF_INET && family != AF_INET6) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); + assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL); r = sd_is_socket_internal(fd, type, listening); if (r <= 0) @@ -283,7 +243,7 @@ _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint sockaddr.sa.sa_family != AF_INET6) return 0; - if (family > 0) + if (family != 0) if (sockaddr.sa.sa_family != family) return 0; @@ -292,7 +252,7 @@ _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint if (l < sizeof(struct sockaddr_in)) return -EINVAL; - return htons(port) == sockaddr.in4.sin_port; + return htons(port) == sockaddr.in.sin_port; } else { if (l < sizeof(struct sockaddr_in6)) return -EINVAL; @@ -309,6 +269,8 @@ _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path socklen_t l = sizeof(sockaddr); int r; + assert_return(fd >= 0, -EINVAL); + r = sd_is_socket_internal(fd, type, listening); if (r <= 0) return r; @@ -348,8 +310,7 @@ _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path _public_ int sd_is_mq(int fd, const char *path) { struct mq_attr attr; - if (fd < 0) - return -EINVAL; + assert_return(fd >= 0, -EINVAL); if (mq_getattr(fd, &attr) < 0) return -errno; @@ -358,8 +319,7 @@ _public_ int sd_is_mq(int fd, const char *path) { char fpath[PATH_MAX]; struct stat a, b; - if (path[0] != '/') - return -EINVAL; + assert_return(path_is_absolute(path), -EINVAL); if (fstat(fd, &a) < 0) return -errno; @@ -378,18 +338,40 @@ _public_ int sd_is_mq(int fd, const char *path) { return 1; } -_public_ int sd_notify(int unset_environment, const char *state) { - int fd = -1, r; - struct msghdr msghdr; - struct iovec iovec; - union sockaddr_union sockaddr; +_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) { + union sockaddr_union sockaddr = { + .sa.sa_family = AF_UNIX, + }; + struct iovec iovec = { + .iov_base = (char*) state, + }; + struct msghdr msghdr = { + .msg_iov = &iovec, + .msg_iovlen = 1, + .msg_name = &sockaddr, + }; + union { + struct cmsghdr cmsghdr; + uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) + + CMSG_SPACE(sizeof(int) * n_fds)]; + } control; + _cleanup_close_ int fd = -1; + struct cmsghdr *cmsg = NULL; const char *e; + size_t controllen_without_ucred = 0; + bool try_without_ucred = false; + int r; if (!state) { r = -EINVAL; goto finish; } + if (n_fds > 0 && !fds) { + r = -EINVAL; + goto finish; + } + e = getenv("NOTIFY_SOCKET"); if (!e) return 0; @@ -406,60 +388,121 @@ _public_ int sd_notify(int unset_environment, const char *state) { goto finish; } - memzero(&sockaddr, sizeof(sockaddr)); - sockaddr.sa.sa_family = AF_UNIX; - strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path)); + iovec.iov_len = strlen(state); + strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path)); if (sockaddr.un.sun_path[0] == '@') sockaddr.un.sun_path[0] = 0; - memzero(&iovec, sizeof(iovec)); - iovec.iov_base = (char*) state; - iovec.iov_len = strlen(state); - - memzero(&msghdr, sizeof(msghdr)); - msghdr.msg_name = &sockaddr; msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e); - if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) msghdr.msg_namelen = sizeof(struct sockaddr_un); - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; + if (n_fds > 0) { + msghdr.msg_control = &control; + msghdr.msg_controllen = CMSG_LEN(sizeof(int) * n_fds); - if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) { - r = -errno; + cmsg = CMSG_FIRSTHDR(&msghdr); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds); + + memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds); + } + + if (pid != 0 && pid != getpid()) { + struct ucred *ucred; + + try_without_ucred = true; + controllen_without_ucred = msghdr.msg_controllen; + + msghdr.msg_control = &control; + msghdr.msg_controllen += CMSG_LEN(sizeof(struct ucred)); + + if (cmsg) + cmsg = CMSG_NXTHDR(&msghdr, cmsg); + else + cmsg = CMSG_FIRSTHDR(&msghdr); + + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_CREDENTIALS; + cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); + + ucred = (struct ucred*) CMSG_DATA(cmsg); + ucred->pid = pid; + ucred->uid = getuid(); + ucred->gid = getgid(); + } + + /* First try with fake ucred data, as requested */ + if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) { + r = 1; goto finish; } - r = 1; + /* If that failed, try with our own ucred instead */ + if (try_without_ucred) { + if (controllen_without_ucred <= 0) + msghdr.msg_control = NULL; + msghdr.msg_controllen = controllen_without_ucred; + + if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) { + r = 1; + goto finish; + } + } + + r = -errno; finish: if (unset_environment) unsetenv("NOTIFY_SOCKET"); - if (fd >= 0) - close(fd); - return r; } +_public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) { + return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0); +} + +_public_ int sd_notify(int unset_environment, const char *state) { + return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0); +} + +_public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) { + _cleanup_free_ char *p = NULL; + int r; + + if (format) { + va_list ap; + + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); + + if (r < 0 || !p) + return -ENOMEM; + } + + return sd_pid_notify(pid, unset_environment, p); +} + _public_ int sd_notifyf(int unset_environment, const char *format, ...) { - va_list ap; - char *p = NULL; + _cleanup_free_ char *p = NULL; int r; - va_start(ap, format); - r = vasprintf(&p, format, ap); - va_end(ap); + if (format) { + va_list ap; - if (r < 0 || !p) - return -ENOMEM; + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); - r = sd_notify(unset_environment, p); - free(p); + if (r < 0 || !p) + return -ENOMEM; + } - return r; + return sd_pid_notify(0, unset_environment, p); } _public_ int sd_booted(void) { @@ -476,62 +519,47 @@ _public_ int sd_booted(void) { } _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) { - unsigned long long ll; - unsigned long l; - const char *e; - char *p = NULL; - int r; + const char *s, *p = ""; /* p is set to dummy value to do unsetting */ + uint64_t u; + int r = 0; - e = getenv("WATCHDOG_PID"); - if (!e) { - r = 0; + s = getenv("WATCHDOG_USEC"); + if (!s) goto finish; - } - errno = 0; - l = strtoul(e, &p, 10); - if (errno > 0) { - r = -errno; + r = safe_atou64(s, &u); + if (r < 0) goto finish; - } - if (!p || p == e || *p || l <= 0) { + if (u <= 0) { r = -EINVAL; goto finish; } - /* Is this for us? */ - if (getpid() != (pid_t) l) { - r = 0; - goto finish; - } + p = getenv("WATCHDOG_PID"); + if (p) { + pid_t pid; - e = getenv("WATCHDOG_USEC"); - if (!e) { - r = -EINVAL; - goto finish; - } + r = parse_pid(p, &pid); + if (r < 0) + goto finish; - errno = 0; - ll = strtoull(e, &p, 10); - if (errno > 0) { - r = -errno; - goto finish; - } - if (!p || p == e || *p || l <= 0) { - r = -EINVAL; - goto finish; + /* Is this for us? */ + if (getpid() != pid) { + r = 0; + goto finish; + } } if (usec) - *usec = ll; + *usec = u; r = 1; finish: - if (unset_environment) { - unsetenv("WATCHDOG_PID"); + if (unset_environment && s) unsetenv("WATCHDOG_USEC"); - } + if (unset_environment && p) + unsetenv("WATCHDOG_PID"); return r; }