/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <limits.h>
#include <mqueue.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sd-daemon.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "socket-util.h"
#include "strv.h"
#include "util.h"
if (!unset_environment)
return;
unsetenv("LISTEN_PID");
unsetenv("LISTEN_FDS");
unsetenv("LISTEN_FDNAMES");
}
const char *e;
int n, r, fd;
e = getenv("LISTEN_PID");
if (!e) {
r = 0;
goto finish;
}
if (r < 0)
goto finish;
/* Is this for us? */
r = 0;
goto finish;
}
e = getenv("LISTEN_FDS");
if (!e) {
r = 0;
goto finish;
}
r = safe_atoi(e, &n);
if (r < 0)
goto finish;
if (n <= 0 || n > INT_MAX - SD_LISTEN_FDS_START) {
r = -EINVAL;
goto finish;
}
r = fd_cloexec(fd, true);
if (r < 0)
goto finish;
}
r = n;
return r;
}
_cleanup_strv_free_ char **l = NULL;
bool have_names;
const char *e;
int r;
if (!names)
return sd_listen_fds(unset_environment);
e = getenv("LISTEN_FDNAMES");
if (e) {
if (n_names < 0) {
return n_names;
}
have_names = true;
} else
have_names = false;
if (n_fds <= 0)
return n_fds;
if (have_names) {
return -EINVAL;
} else {
if (r < 0)
return r;
}
*names = l;
l = NULL;
return n_fds;
}
return -errno;
return 0;
if (path) {
return 0;
return -errno;
}
return
}
return 1;
}
return -errno;
return 0;
if (path) {
return 0;
return -errno;
}
return
else
return 0;
}
return 1;
}
return -errno;
return 0;
if (type != 0) {
int other_type = 0;
socklen_t l = sizeof(other_type);
return -errno;
if (l != sizeof(other_type))
return -EINVAL;
if (other_type != type)
return 0;
}
if (listening >= 0) {
int accepting = 0;
return -errno;
if (l != sizeof(accepting))
return -EINVAL;
return 0;
}
return 1;
}
int r;
if (r <= 0)
return r;
if (family > 0) {
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
}
return 1;
}
int r;
if (r <= 0)
return r;
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
return 0;
if (family != 0)
return 0;
if (port > 0) {
if (l < sizeof(struct sockaddr_in))
return -EINVAL;
} else {
if (l < sizeof(struct sockaddr_in6))
return -EINVAL;
}
}
return 1;
}
int r;
if (r <= 0)
return r;
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
return 0;
if (path) {
if (length == 0)
if (length == 0)
/* Unnamed socket */
if (path[0])
/* Normal path socket */
return
else
/* Abstract namespace socket */
return
}
return 1;
}
/* Check that the fd is valid */
/* A non-mq fd (or an invalid one, but we ruled that out above) */
return 0;
return -errno;
}
if (path) {
struct stat a, b;
return -errno;
return -errno;
return 0;
}
return 1;
}
_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
};
};
.msg_iovlen = 1,
};
const char *e;
bool have_pid;
int r;
if (!state) {
r = -EINVAL;
goto finish;
}
r = -EINVAL;
goto finish;
}
e = getenv("NOTIFY_SOCKET");
if (!e)
return 0;
/* Must be an abstract socket, or an absolute path */
if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
r = -EINVAL;
goto finish;
}
r = -EINVAL;
goto finish;
}
if (fd < 0) {
r = -errno;
goto finish;
}
/* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
if (n_fds > 0) {
if (have_pid)
}
if (have_pid) {
}
}
/* First try with fake ucred data, as requested */
r = 1;
goto finish;
}
/* If that failed, try with our own ucred instead */
if (have_pid) {
if (msghdr.msg_controllen == 0)
r = 1;
goto finish;
}
}
r = -errno;
if (unset_environment)
unsetenv("NOTIFY_SOCKET");
return r;
}
}
}
_cleanup_free_ char *p = NULL;
int r;
if (format) {
if (r < 0 || !p)
return -ENOMEM;
}
}
_cleanup_free_ char *p = NULL;
int r;
if (format) {
if (r < 0 || !p)
return -ENOMEM;
}
return sd_pid_notify(0, unset_environment, p);
}
/* We test whether the runtime unit file directory has been
* created. This takes place in mount-setup.c, so is
* guaranteed to happen very early during boot. */
}
const char *s, *p = ""; /* p is set to dummy value to do unsetting */
uint64_t u;
int r = 0;
s = getenv("WATCHDOG_USEC");
if (!s)
goto finish;
r = safe_atou64(s, &u);
if (r < 0)
goto finish;
if (u <= 0 || u >= USEC_INFINITY) {
r = -EINVAL;
goto finish;
}
p = getenv("WATCHDOG_PID");
if (p) {
if (r < 0)
goto finish;
/* Is this for us? */
r = 0;
goto finish;
}
}
if (usec)
*usec = u;
r = 1;
if (unset_environment && s)
unsetenv("WATCHDOG_USEC");
if (unset_environment && p)
unsetenv("WATCHDOG_PID");
return r;
}