f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen/***
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen Copyright 2010 Lennart Poettering
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
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
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
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 SOFTWARE.
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen***/
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#ifndef _GNU_SOURCE
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#define _GNU_SOURCE
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#endif
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <sys/types.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <sys/stat.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <sys/socket.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <sys/un.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <sys/fcntl.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <netinet/in.h>
b087ad415f7870342835054fec3c5d9efcb98784Timo Sirainen#include <stdlib.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <errno.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <unistd.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <string.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <stdarg.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <stdio.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include <stddef.h>
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#include "sd-daemon.h"
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_listen_fds(int unset_environment) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#else
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int r, fd;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen const char *e;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen char *p = NULL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen unsigned long l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!(e = getenv("LISTEN_PID"))) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen errno = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen l = strtoul(e, &p, 10);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (errno != 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!p || *p || l <= 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Is this for us? */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getpid() != (pid_t) l) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!(e = getenv("LISTEN_FDS"))) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen errno = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen l = strtoul(e, &p, 10);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (errno != 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!p || *p) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int flags;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((flags = fcntl(fd, F_GETFD)) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (flags & FD_CLOEXEC)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen continue;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = (int) l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenfinish:
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (unset_environment) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen unsetenv("LISTEN_PID");
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen unsetenv("LISTEN_FDS");
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#endif
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_fifo(int fd, const char *path) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct stat st_fd;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fd < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&st_fd, 0, sizeof(st_fd));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fstat(fd, &st_fd) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!S_ISFIFO(st_fd.st_mode))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (path) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct stat st_path;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&st_path, 0, sizeof(st_path));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (stat(path, &st_path) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (errno == ENOENT || errno == ENOTDIR)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen st_path.st_dev == st_fd.st_dev &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen st_path.st_ino == st_fd.st_ino;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenstatic int sd_is_socket_internal(int fd, int type, int listening) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct stat st_fd;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fd < 0 || type < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fstat(fd, &st_fd) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!S_ISSOCK(st_fd.st_mode))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (type != 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int other_type = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen socklen_t l = sizeof(other_type);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l != sizeof(other_type))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (other_type != type)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (listening >= 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int accepting = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen socklen_t l = sizeof(accepting);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l != sizeof(accepting))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!accepting != !listening)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenunion sockaddr_union {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct sockaddr sa;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct sockaddr_in in4;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct sockaddr_in6 in6;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct sockaddr_un un;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct sockaddr_storage storage;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen};
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket(int fd, int family, int type, int listening) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (family < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (family > 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen union sockaddr_union sockaddr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen socklen_t l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&sockaddr, 0, sizeof(sockaddr));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen l = sizeof(sockaddr);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockname(fd, &sockaddr.sa, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return sockaddr.sa.sa_family == family;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen union sockaddr_union sockaddr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen socklen_t l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (family != 0 && family != AF_INET && family != AF_INET6)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&sockaddr, 0, sizeof(sockaddr));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen l = sizeof(sockaddr);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockname(fd, &sockaddr.sa, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sockaddr.sa.sa_family != AF_INET &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen sockaddr.sa.sa_family != AF_INET6)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (family > 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sockaddr.sa.sa_family != family)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (port > 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sockaddr.sa.sa_family == AF_INET) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(struct sockaddr_in))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return htons(port) == sockaddr.in4.sin_port;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen } else {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(struct sockaddr_in6))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return htons(port) == sockaddr.in6.sin6_port;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen union sockaddr_union sockaddr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen socklen_t l;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&sockaddr, 0, sizeof(sockaddr));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen l = sizeof(sockaddr);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (getsockname(fd, &sockaddr.sa, &l) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (l < sizeof(sa_family_t))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sockaddr.sa.sa_family != AF_UNIX)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (path) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (length <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen length = strlen(path);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (length <= 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Unnamed socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return l == offsetof(struct sockaddr_un, sun_path);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (path[0])
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Normal path socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen memcmp(path, sockaddr.un.sun_path, length+1) == 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen else
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Abstract namespace socket */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen (l == offsetof(struct sockaddr_un, sun_path) + length) &&
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen memcmp(path, sockaddr.un.sun_path, length) == 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_notify(int unset_environment, const char *state) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#else
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int fd = -1, r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct msghdr msghdr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct iovec iovec;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen union sockaddr_union sockaddr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen const char *e;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!state) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (!(e = getenv("NOTIFY_SOCKET")))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* Must be an abstract socket, or an absolute path */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -EINVAL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&sockaddr, 0, sizeof(sockaddr));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen sockaddr.sa.sa_family = AF_UNIX;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sockaddr.un.sun_path[0] == '@')
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen sockaddr.un.sun_path[0] = 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&iovec, 0, sizeof(iovec));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen iovec.iov_base = (char*) state;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen iovec.iov_len = strlen(state);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
27de85bc0361f267b500640e7092987be10daae5Martti Rannanjärvi memset(&msghdr, 0, sizeof(msghdr));
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_name = &sockaddr;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_namelen = sizeof(struct sockaddr_un);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_iov = &iovec;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen msghdr.msg_iovlen = 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = -errno;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen goto finish;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen }
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = 1;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenfinish:
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (unset_environment)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen unsetenv("NOTIFY_SOCKET");
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (fd >= 0)
c31803dcbdf3d4e3b836134a2a04ee2cd5251ce5Aki Tuomi (void)close(fd);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#endif
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_notifyf(int unset_environment, const char *format, ...) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#else
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen va_list ap;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen char *p = NULL;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen int r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen va_start(ap, format);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = vasprintf(&p, format, ap);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen va_end(ap);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (r < 0 || !p)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return -ENOMEM;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen r = sd_notify(unset_environment, p);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen free(p);
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return r;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#endif
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainenint sd_booted(void) {
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#else
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen struct stat a, b;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen /* We simply test whether the systemd cgroup hierarchy is
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen * mounted */
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (lstat("/sys/fs/cgroup", &a) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return 0;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen return a.st_dev != b.st_dev;
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen#endif
f6ba2de94f207839983e1b5b4bac930df465f572Timo Sirainen}