sd-daemon.c revision 24be98227483baf67094fa95b5633594446cd21a
/*-*- 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
#ifdef __BIONIC__
#else
#endif
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stddef.h>
#include <limits.h>
#if defined(__linux__)
# include <mqueue.h>
#endif
#include "sd-daemon.h"
#if (__GNUC__ >= 4)
# ifdef SD_EXPORT_SYMBOLS
/* Export symbols */
# else
/* Don't export the symbols */
# endif
#else
# define _sd_export_
#endif
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
return 0;
#else
int r, fd;
const char *e;
char *p = NULL;
unsigned long l;
e = getenv("LISTEN_PID");
if (!e) {
r = 0;
goto finish;
}
errno = 0;
l = strtoul(e, &p, 10);
if (errno != 0) {
r = -errno;
goto finish;
}
if (!p || p == e || *p || l <= 0) {
r = -EINVAL;
goto finish;
}
/* Is this for us? */
r = 0;
goto finish;
}
e = getenv("LISTEN_FDS");
if (!e) {
r = 0;
goto finish;
}
errno = 0;
l = strtoul(e, &p, 10);
if (errno != 0) {
r = -errno;
goto finish;
}
if (!p || p == e || *p) {
r = -EINVAL;
goto finish;
}
int flags;
if (flags < 0) {
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;
}
if (fd < 0)
return -EINVAL;
return -errno;
return 0;
if (path) {
return 0;
return -errno;
}
return
else
return 0;
}
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;
if (r <= 0)
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;
if (r <= 0)
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;
}
_sd_export_ int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
union sockaddr_union sockaddr;
socklen_t l;
int r;
if (r <= 0)
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 */
if (path[0])
/* Normal path socket */
return
else
/* Abstract namespace socket */
return
}
return 1;
}
#if !defined(__linux__)
return 0;
#else
if (fd < 0)
return -EINVAL;
return -errno;
if (path) {
struct stat a, b;
if (path[0] != '/')
return -EINVAL;
return -errno;
return -errno;
return 0;
}
return 1;
#endif
}
return 0;
#else
int fd = -1, r;
union sockaddr_union sockaddr;
const char *e;
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;
}
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
}
_sd_export_ 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
}