f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen Copyright 2010 Lennart Poettering
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen Permission is hereby granted, free of charge, to any person
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen obtaining a copy of this software and associated documentation files
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen (the "Software"), to deal in the Software without restriction,
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen including without limitation the rights to use, copy, modify, merge,
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen publish, distribute, sublicense, and/or sell copies of the Software,
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen and to permit persons to whom the Software is furnished to do so,
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen subject to the following conditions:
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen The above copyright notice and this permission notice shall be
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen included in all copies or substantial portions of the Software.
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen const char *e;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen unsigned long l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!p || *p || l <= 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Is this for us? */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!p || *p) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenstatic int sd_is_socket_internal(int fd, int type, int listening) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l != sizeof(other_type))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l != sizeof(accepting))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket(int fd, int family, int type, int listening) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (family != 0 && family != AF_INET && family != AF_INET6)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(struct sockaddr_in))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(struct sockaddr_in6))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Unnamed socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return l == offsetof(struct sockaddr_un, sun_path);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Normal path socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen memcmp(path, sockaddr.un.sun_path, length+1) == 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Abstract namespace socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen (l == offsetof(struct sockaddr_un, sun_path) + length) &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen memcmp(path, sockaddr.un.sun_path, length) == 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_notify(int unset_environment, const char *state) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen const char *e;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Must be an abstract socket, or an absolute path */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_namelen = sizeof(struct sockaddr_un);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_notifyf(int unset_environment, const char *format, ...) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (r < 0 || !p)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* We simply test whether the systemd cgroup hierarchy is