along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
-#include <fcntl.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include "util.h"
#include "path-util.h"
+#include "socket-util.h"
#include "sd-daemon.h"
_public_ int sd_listen_fds(int unset_environment) {
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 (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;
return 1;
}
-_public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
- 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,
+ };
+ struct cmsghdr *control;
_cleanup_close_ int fd = -1;
- struct msghdr msghdr = {};
- struct iovec iovec = {};
+ struct cmsghdr *cmsg = NULL;
const char *e;
- union {
- struct cmsghdr cmsghdr;
- uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
- } control = {};
+ size_t controllen_without_ucred = 0;
+ bool try_without_ucred = false;
int r;
if (!state) {
goto finish;
}
+ if (n_fds > 0 && !fds) {
+ r = -EINVAL;
+ goto finish;
+ }
+
e = getenv("NOTIFY_SOCKET");
if (!e)
return 0;
goto finish;
}
- 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;
- iovec.iov_base = (char*) state;
- iovec.iov_len = strlen(state);
-
- 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;
+ control = alloca(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * n_fds));
+
+ if (n_fds > 0) {
+ msghdr.msg_control = control;
+ msghdr.msg_controllen = CMSG_LEN(sizeof(int) * n_fds);
+
+ 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 cmsghdr *cmsg;
- struct ucred ucred = {};
+ struct ucred *ucred;
- msghdr.msg_control = &control;
- msghdr.msg_controllen = sizeof(control);
+ 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_FIRSTHDR(&msghdr);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_CREDENTIALS;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
- ucred.pid = pid;
- ucred.uid = getuid();
- ucred.gid = getgid();
-
- memcpy(CMSG_DATA(cmsg), &ucred, sizeof(struct ucred));
- msghdr.msg_controllen = cmsg->cmsg_len;
+ ucred = (struct ucred*) CMSG_DATA(cmsg);
+ ucred->pid = pid;
+ ucred->uid = getuid();
+ ucred->gid = getgid();
}
/* First try with fake ucred data, as requested */
goto finish;
}
- /* If that failed, try with our own instead */
- if (msghdr.msg_control) {
- msghdr.msg_control = NULL;
- msghdr.msg_controllen = 0;
+ /* 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;
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(0, unset_environment, 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, ...) {
}
_public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
- const char *e;
+ const char *s, *p = ""; /* p is set to dummy value to do unsetting */
uint64_t u;
- pid_t pid;
- int r;
+ int r = 0;
- e = getenv("WATCHDOG_PID");
- if (!e) {
- r = 0;
+ s = getenv("WATCHDOG_USEC");
+ if (!s)
goto finish;
- }
- r = parse_pid(e, &pid);
+ r = safe_atou64(s, &u);
if (r < 0)
goto finish;
-
- /* Is this for us? */
- if (getpid() != pid) {
- r = 0;
- goto finish;
- }
-
- e = getenv("WATCHDOG_USEC");
- if (!e) {
+ if (u <= 0) {
r = -EINVAL;
goto finish;
}
- r = safe_atou64(e, &u);
- if (r < 0)
- goto finish;
- if (u <= 0) {
- r = -EINVAL;
- goto finish;
+ p = getenv("WATCHDOG_PID");
+ if (p) {
+ pid_t pid;
+
+ r = parse_pid(p, &pid);
+ if (r < 0)
+ goto finish;
+
+ /* Is this for us? */
+ if (getpid() != pid) {
+ r = 0;
+ goto finish;
+ }
}
if (usec)
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;
}