sd-daemon.c revision 77d5f105bf63330350c7bddacdeda914f945590f
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
Copyright 2010 Lennart Poettering
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "sd-daemon.h"
int sd_listen_fds(int unset_environment) {
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
return 0;
#else
int r, fd;
const char *e;
char *p = NULL;
unsigned long l;
if (!(e = getenv("LISTEN_PID"))) {
r = 0;
goto finish;
}
errno = 0;
l = strtoul(e, &p, 10);
if (errno != 0) {
r = -errno;
goto finish;
}
if (!p || *p || l <= 0) {
r = -EINVAL;
goto finish;
}
/* Is this for us? */
r = 0;
goto finish;
}
if (!(e = getenv("LISTEN_FDS"))) {
r = 0;
goto finish;
}
errno = 0;
l = strtoul(e, &p, 10);
if (errno != 0) {
r = -errno;
goto finish;
}
if (!p || *p) {
r = -EINVAL;
goto finish;
}
int flags;
r = -errno;
goto finish;
}
if (flags & FD_CLOEXEC)
continue;
r = -errno;
goto finish;
}
}
r = (int) l;
if (unset_environment) {
unsetenv("LISTEN_PID");
unsetenv("LISTEN_FDS");
}
return r;
#endif
}
if (fd < 0)
return -EINVAL;
return -errno;
return 0;
if (path) {
return 0;
return -errno;
}
return
}
return 1;
}
return -EINVAL;
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;
}
union sockaddr_union {
struct sockaddr_in in4;
struct sockaddr_in6 in6;
struct sockaddr_un un;
struct sockaddr_storage storage;
};
int r;
if (family < 0)
return -EINVAL;
return r;
if (family > 0) {
union sockaddr_union sockaddr;
socklen_t l;
l = sizeof(sockaddr);
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
}
return 1;
}
union sockaddr_union sockaddr;
socklen_t l;
int r;
return -EINVAL;
return r;
l = sizeof(sockaddr);
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;
socklen_t l;
int r;
return r;
l = sizeof(sockaddr);
return -errno;
if (l < sizeof(sa_family_t))
return -EINVAL;
return 0;
if (path) {
if (length <= 0)
if (length <= 0)
/* Unnamed socket */
return l == sizeof(sa_family_t);
if (path[0])
/* Normal path socket */
return
else
/* Abstract namespace socket */
return
(l == sizeof(sa_family_t) + length) &&
}
return 1;
}
return 0;
#else
int fd = -1, r;
union sockaddr_union sockaddr;
union {
} control;
const char *e;
if (!state) {
r = -EINVAL;
goto finish;
}
if (!(e = getenv("NOTIFY_SOCKET")))
return 0;
/* Must be an abstract socket, or an absolute path */
if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
r = -EINVAL;
goto finish;
}
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
r = 1;
if (unset_environment)
unsetenv("NOTIFY_SOCKET");
if (fd >= 0)
return r;
#endif
}
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
return 0;
#else
char *p = NULL;
int r;
if (r < 0 || !p)
return -ENOMEM;
r = sd_notify(unset_environment, p);
free(p);
return r;
#endif
}
int sd_booted(void) {
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
return 0;
#else
struct stat a, b;
/* We simply test whether the systemd cgroup hierarchy is
* mounted */
return 0;
return 0;
#endif
}