sd-daemon.c revision d4a144fadf89bca681724c6c9a65b4a165fa0f90
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
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 <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stddef.h>
#include <limits.h>
#include <mqueue.h>
#include "util.h"
#include "path-util.h"
#include "socket-util.h"
#include "sd-daemon.h"
const char *e;
unsigned n;
int 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_atou(e, &n);
if (r < 0)
goto finish;
r = fd_cloexec(fd, true);
if (r < 0)
goto finish;
}
r = (int) n;
if (unset_environment) {
unsetenv("LISTEN_PID");
unsetenv("LISTEN_FDS");
}
return r;
}
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) {
union sockaddr_union sockaddr = {};
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
}
return 1;
}
union sockaddr_union sockaddr = {};
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;
}
union sockaddr_union sockaddr = {};
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;
}
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) {
union sockaddr_union sockaddr = {
};
};
.msg_iovlen = 1,
};
const char *e;
bool try_without_ucred = false;
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;
}
if (fd < 0) {
r = -errno;
goto finish;
}
if (n_fds > 0) {
}
try_without_ucred = true;
if (cmsg)
else
}
/* First try with fake ucred data, as requested */
r = 1;
goto finish;
}
/* If that failed, try with our own ucred instead */
if (try_without_ucred) {
if (controllen_without_ucred <= 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. */
return 0;
}
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) {
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;
}