sd-daemon.c revision 3cb4674019567ef93e4a463dd145fd3af6242877
/*-*- 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 <fcntl.h>
#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;
}
union sockaddr_union sockaddr = {};
const char *e;
union {
} control = {};
int r;
if (!state) {
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;
}
}
/* First try with fake ucred data, as requested */
r = 1;
goto finish;
}
/* If that failed, try with our own instead */
if (msghdr.msg_control) {
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. */
return 0;
}
const char *e;
uint64_t u;
int r;
e = getenv("WATCHDOG_PID");
if (!e) {
r = 0;
goto finish;
}
if (r < 0)
goto finish;
/* Is this for us? */
r = 0;
goto finish;
}
e = getenv("WATCHDOG_USEC");
if (!e) {
r = -EINVAL;
goto finish;
}
r = safe_atou64(e, &u);
if (r < 0)
goto finish;
if (u <= 0) {
r = -EINVAL;
goto finish;
}
if (usec)
*usec = u;
r = 1;
if (unset_environment) {
unsetenv("WATCHDOG_PID");
unsetenv("WATCHDOG_USEC");
}
return r;
}