sd-daemon.c revision 31021ba0853d40ffdd8fdcd6611bddd4419a7357
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2010 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen_public_ int sd_listen_fds(int unset_environment) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen const char *e;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen /* Is this for us? */
7c16313f11e3953f3fe4dbf544f2d36f58d14138Tom Gundersen for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) n; fd ++) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen_public_ int sd_is_fifo(int fd, const char *path) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen_public_ int sd_is_special(int fd, const char *path) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersenstatic int sd_is_socket_internal(int fd, int type, int listening) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (l != sizeof(other_type))
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen if (l != sizeof(accepting))
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen_public_ int sd_is_socket(int fd, int family, int type, int listening) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen r = sd_is_socket_internal(fd, type, listening);
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen if (l < sizeof(sa_family_t))
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen_public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_is_socket_internal(fd, type, listening);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (l < sizeof(sa_family_t))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (l < sizeof(struct sockaddr_in))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (l < sizeof(struct sockaddr_in6))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen_public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = sd_is_socket_internal(fd, type, listening);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (l < sizeof(sa_family_t))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Unnamed socket */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return l == offsetof(struct sockaddr_un, sun_path);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Normal path socket */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen memcmp(path, sockaddr.un.sun_path, length+1) == 0;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Abstract namespace socket */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen (l == offsetof(struct sockaddr_un, sun_path) + length) &&
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen memcmp(path, sockaddr.un.sun_path, length) == 0;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen_public_ int sd_is_mq(int fd, const char *path) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert_return(path_is_absolute(path), -EINVAL);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen const char *e;
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen /* Must be an abstract socket, or an absolute path */
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen msghdr.msg_namelen = sizeof(struct sockaddr_un);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen msghdr.msg_controllen = (n_fds ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen msghdr.msg_control = alloca(msghdr.msg_controllen);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* First try with fake ucred data, as requested */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen /* If that failed, try with our own ucred instead */
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen_public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen_public_ int sd_notify(int unset_environment, const char *state) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen_public_ int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) {
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen if (r < 0 || !p)
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering return sd_pid_notify(pid, unset_environment, p);
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering_public_ int sd_notifyf(int unset_environment, const char *format, ...) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0 || !p)
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return sd_pid_notify(0, unset_environment, p);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen /* We test whether the runtime unit file directory has been
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen * created. This takes place in mount-setup.c, so is
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen * guaranteed to happen very early during boot. */
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen return laccess("/run/systemd/system/", F_OK) >= 0;
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen_public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen const char *s, *p = ""; /* p is set to dummy value to do unsetting */
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (u <= 0) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen /* Is this for us? */