socket.c revision 62bc4efc7a617791d43f4e6cbef857554e6dbe2e
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2010 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <arpa/inet.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <errno.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <fcntl.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <mqueue.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <netinet/tcp.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <signal.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <sys/epoll.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <sys/stat.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <unistd.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include <linux/sctp.h>
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "sd-event.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "alloc-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "bus-error.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "bus-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "copy.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "dbus-socket.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "def.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "exit-status.h"
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering#include "fd-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "formats-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "label.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "log.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "missing.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "mkdir.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "parse-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "path-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "process-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "selinux-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "signal-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "smack-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "socket.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "special.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "string-table.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "string-util.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "strv.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "unit-name.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "unit-printf.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "unit.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "user-util.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering [SOCKET_DEAD] = UNIT_INACTIVE,
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering [SOCKET_START_PRE] = UNIT_ACTIVATING,
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz [SOCKET_START_CHOWN] = UNIT_ACTIVATING,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [SOCKET_START_POST] = UNIT_ACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_LISTENING] = UNIT_ACTIVE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [SOCKET_RUNNING] = UNIT_ACTIVE,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering [SOCKET_FAILED] = UNIT_FAILED
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering};
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_init(Unit *u) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering Socket *s = SOCKET(u);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u->load_state == UNIT_STUB);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->backlog = SOMAXCONN;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->timeout_usec = u->manager->default_timeout_start_usec;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->directory_mode = 0755;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->socket_mode = 0666;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->max_connections = 64;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->priority = -1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->ip_tos = -1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->ip_ttl = -1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->mark = -1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->exec_context.std_output = u->manager->default_std_output;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->exec_context.std_error = u->manager->default_std_error;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_unwatch_control_pid(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->control_pid <= 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering unit_unwatch_pid(UNIT(s), s->control_pid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_pid = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_cleanup_fd_list(SocketPort *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering close_many(p->auxiliary_fds, p->n_auxiliary_fds);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering p->auxiliary_fds = mfree(p->auxiliary_fds);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->n_auxiliary_fds = 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid socket_free_ports(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while ((p = s->ports)) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering LIST_REMOVE(port, s->ports, p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_event_source_unref(p->event_source);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering socket_cleanup_fd_list(p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering safe_close(p->fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(p->path);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_done(Unit *u) {
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek Socket *s = SOCKET(u);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_free_ports(s);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->exec_runtime = exec_runtime_unref(s->exec_runtime);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->control_command = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering socket_unwatch_control_pid(s);
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering unit_ref_unset(&s->service);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->tcp_congestion = mfree(s->tcp_congestion);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->bind_to_device = mfree(s->bind_to_device);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->smack = mfree(s->smack);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->smack_ip_in = mfree(s->smack_ip_in);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->smack_ip_out = mfree(s->smack_ip_out);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering strv_free(s->symlinks);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->user = mfree(s->user);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->group = mfree(s->group);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->fdname = mfree(s->fdname);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->timer_event_source = sd_event_source_unref(s->timer_event_source);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_arm_timer(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->timeout_usec <= 0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->timer_event_source = sd_event_source_unref(s->timer_event_source);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->timer_event_source) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_time(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek UNIT(s)->manager->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &s->timer_event_source,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek CLOCK_MONOTONIC,
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering now(CLOCK_MONOTONIC) + s->timeout_usec, 0,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_dispatch_timer, s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) sd_event_source_set_description(s->timer_event_source, "socket-timer");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint socket_instantiate_service(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *prefix = NULL, *name = NULL;
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Unit *u;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt /* This fills in s->service if it isn't filled in yet. For
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Accept=yes sockets we create the next connection service
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * here. For Accept=no this is mostly a NOP since the service
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * is figured out at load time anyway. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (UNIT_DEREF(s->service))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!s->accept)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering r = unit_name_to_prefix(UNIT(s)->id, &prefix);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0)
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) if (r < 0)
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return r;
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->no_gc = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_ref_set(&s->service, u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool have_non_accept_socket(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!s->accept)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->type != SOCKET_SOCKET)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!socket_address_can_accept(&p->address))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false;
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_add_mount_links(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (p->type == SOCKET_SOCKET)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek path = socket_address_get_path(&p->address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path = p->path;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!path)
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_require_mounts_for(UNIT(s), path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_add_device_link(Socket *s) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt char *t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return unit_add_node_link(UNIT(s), t, false, UNIT_BINDS_TO);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering}
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poetteringstatic int socket_add_default_dependencies(Socket *s) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering int r;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert(s);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (!UNIT(s)->default_dependencies)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return 0;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek_pure_ static bool socket_has_exec(Socket *s) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek unsigned i;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->exec_command[i])
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_add_extras(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering Unit *u = UNIT(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(s);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (have_non_accept_socket(s)) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (!UNIT_DEREF(s->service)) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt Unit *x;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_load_related_unit(u, ".service", &x);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk unit_ref_set(&s->service, x);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = socket_add_mount_links(s);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt return r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = socket_add_device_link(s);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = unit_patch_contexts(u);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (socket_has_exec(s)) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = unit_add_exec_dependencies(u, &s->exec_context);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = unit_set_default_slice(u);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (r < 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = socket_add_default_dependencies(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekstatic const char *socket_find_symlink_target(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *found = NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering SocketPort *p;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering LIST_FOREACH(port, p, s->ports) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *f = NULL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek switch (p->type) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case SOCKET_FIFO:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f = p->path;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case SOCKET_SOCKET:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (p->address.sockaddr.un.sun_path[0] != 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek f = p->address.sockaddr.un.sun_path;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek default:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering break;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (found)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering found = f;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return found;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_verify(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (UNIT(s)->load_state != UNIT_LOADED)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!s->ports) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "Unit lacks Listen setting. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (s->accept && have_non_accept_socket(s)) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (s->accept && s->max_connections <= 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (s->accept && UNIT_DEREF(s->service)) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic int socket_load(Unit *u) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering Socket *s = SOCKET(u);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(u);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(u->load_state == UNIT_STUB);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = unit_load_fragment_and_dropin(u);
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (u->load_state == UNIT_LOADED) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* This is a new unit? Then let's add in some extras */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_add_extras(s);
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return socket_verify(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering_const_ static const char* listen_lookup(int family, int type) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (family == AF_NETLINK)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "ListenNetlink";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (type == SOCK_STREAM)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "ListenStream";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (type == SOCK_DGRAM)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "ListenDatagram";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (type == SOCK_SEQPACKET)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return "ListenSequentialPacket";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering assert_not_reached("Unknown socket type");
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering return NULL;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering}
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_dump(Unit *u, FILE *f, const char *prefix) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char time_string[FORMAT_TIMESPAN_MAX];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketExecCommand c;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Socket *s = SOCKET(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *prefix2;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix = strempty(prefix);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix2 = strjoina(prefix, "\t");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSocket State: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sResult: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sBindIPv6Only: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sBacklog: %u\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSocketMode: %04o\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sDirectoryMode: %04o\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sKeepAlive: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sNoDelay: %s\n"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering "%sFreeBind: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sTransparent: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sBroadcast: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sPassCredentials: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sPassSecurity: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sTCPCongestion: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sRemoveOnStop: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sWritable: %s\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sFDName: %s\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "%sSELinuxContextFromNet: %s\n",
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering prefix, socket_state_to_string(s->state),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, socket_result_to_string(s->result),
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->backlog,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->socket_mode,
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering prefix, s->directory_mode,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->keep_alive),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->no_delay),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->free_bind),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->transparent),
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering prefix, yes_no(s->broadcast),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->pass_cred),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->pass_sec),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, strna(s->tcp_congestion),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->remove_on_stop),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->writable),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, socket_fdname(s),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->selinux_context_from_net));
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if (s->control_pid > 0)
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering fprintf(f,
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering "%sControl PID: "PID_FMT"\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->control_pid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->bind_to_device)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sBindToDevice: %s\n",
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering prefix, s->bind_to_device);
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->accept)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek fprintf(f,
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering "%sAccepted: %u\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%sNConnections: %u\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sMaxConnections: %u\n",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, s->n_accepted,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, s->n_connections,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, s->max_connections);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler if (s->priority >= 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek fprintf(f,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%sPriority: %i\n",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, s->priority);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (s->receive_buffer > 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sReceiveBuffer: %zu\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, s->receive_buffer);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering if (s->send_buffer > 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fprintf(f,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%sSendBuffer: %zu\n",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, s->send_buffer);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (s->ip_tos >= 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fprintf(f,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering "%sIPTOS: %i\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, s->ip_tos);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->ip_ttl >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sIPTTL: %i\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->ip_ttl);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->pipe_size > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sPipeSize: %zu\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->pipe_size);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther if (s->mark >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "%sMark: %i\n",
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther prefix, s->mark);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (s->mq_maxmsg > 0)
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sMessageQueueMaxMessages: %li\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->mq_maxmsg);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (s->mq_msgsize > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%sMessageQueueMessageSize: %li\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->mq_msgsize);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->reuse_port)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering "%sReusePort: %s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, yes_no(s->reuse_port));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->smack)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn "%sSmackLabel: %s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek prefix, s->smack);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->smack_ip_in)
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden fprintf(f,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden "%sSmackLabelIPIn: %s\n",
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden prefix, s->smack_ip_in);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (s->smack_ip_out)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering fprintf(f,
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden "%sSmackLabelIPOut: %s\n",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, s->smack_ip_out);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!isempty(s->user) || !isempty(s->group))
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sOwnerUser: %s\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering "%sOwnerGroup: %s\n",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek prefix, strna(s->user),
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, strna(s->group));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive_time > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering "%sKeepAliveTimeSec: %s\n",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_time, USEC_PER_SEC));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive_interval)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sKeepAliveIntervalSec: %s\n",
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_interval, USEC_PER_SEC));
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (s->keep_alive_cnt)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek fprintf(f,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering "%sKeepAliveProbes: %u\n",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, s->keep_alive_cnt);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (s->defer_accept)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fprintf(f,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering "%sDeferAcceptSec: %s\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->defer_accept, USEC_PER_SEC));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering LIST_FOREACH(port, p, s->ports) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (p->type == SOCKET_SOCKET) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering const char *t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek char *k = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = socket_address_print(&p->address, &k);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering t = strerror(-r);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering else
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering t = k;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering free(k);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering } else if (p->type == SOCKET_SPECIAL)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering else if (p->type == SOCKET_USB_FUNCTION)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering else if (p->type == SOCKET_MQUEUE)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek else
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering }
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering exec_context_dump(&s->exec_context, f, prefix);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering kill_context_dump(&s->kill_context, f, prefix);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering if (!s->exec_command[c])
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering continue;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering fprintf(f, "%s-> %s:\n",
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering prefix, socket_exec_command_to_string(c));
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering exec_command_dump_list(s->exec_command[c], f, prefix2);
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee }
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int instance_from_socket(int fd, unsigned nr, char **instance) {
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering socklen_t l;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering char *r;
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett union sockaddr_union local, remote;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(fd >= 0);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(instance);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering l = sizeof(local);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (getsockname(fd, &local.sa, &l) < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return -errno;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman l = sizeof(remote);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (getpeername(fd, &remote.sa, &l) < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return -errno;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek switch (local.sa.sa_family) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case AF_INET: {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint32_t
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek a = ntohl(local.in.sin_addr.s_addr),
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek b = ntohl(remote.in.sin_addr.s_addr);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (asprintf(&r,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek nr,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(local.in.sin_port),
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(remote.in.sin_port)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -ENOMEM;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case AF_INET6: {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek static const unsigned char ipv4_prefix[] = {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek };
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const uint8_t
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *a = local.in6.sin6_addr.s6_addr+12,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering *b = remote.in6.sin6_addr.s6_addr+12;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (asprintf(&r,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek nr,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek a[0], a[1], a[2], a[3],
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(local.in6.sin6_port),
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering b[0], b[1], b[2], b[3],
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(remote.in6.sin6_port)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -ENOMEM;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek } else {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (asprintf(&r,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%u-%s:%u-%s:%u",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek nr,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(local.in6.sin6_port),
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ntohs(remote.in6.sin6_port)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -ENOMEM;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering case AF_UNIX: {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek struct ucred ucred;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering int k;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek k = getpeercred(fd, &ucred);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (k >= 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (asprintf(&r,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering "%u-"PID_FMT"-"UID_FMT,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek nr, ucred.pid, ucred.uid) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -ENOMEM;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek } else if (k == -ENODATA) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* This handles the case where somebody is
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * connecting from another pid/uid namespace
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek * (e.g. from outside of our container). */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (asprintf(&r,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "%u-unknown",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek nr) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -ENOMEM;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering } else
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return k;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering break;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering }
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering default:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_not_reached("Unhandled socket type.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *instance = r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic void socket_close_fds(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char **i;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->event_source = sd_event_source_unref(p->event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (p->fd < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p->fd = safe_close(p->fd);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering socket_cleanup_fd_list(p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* One little note: we should normally not delete any
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * sockets in the file system here! After all some
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * other process we spawned might still have a
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * reference of this fd and wants to continue to use
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * it. Therefore we delete sockets in the file system
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers * before we create a new one, not after we stopped
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering * using one! */
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering if (s->remove_on_stop) {
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering switch (p->type) {
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering case SOCKET_FIFO:
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering unlink(p->path);
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø break;
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers case SOCKET_MQUEUE:
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers mq_unlink(p->path);
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters break;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCKET_SOCKET:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_address_unlink(&p->address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek default:
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->remove_on_stop)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek STRV_FOREACH(i, s->symlinks)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unlink(*i);
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin}
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchaginstatic void socket_apply_socket_options(Socket *s, int fd) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(fd >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int b = s->keep_alive;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_KEEPALIVE failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive_time) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int value = s->keep_alive_time / USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPIDLE failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive_interval) {
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering int value = s->keep_alive_interval / USEC_PER_SEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPINTVL failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->keep_alive_cnt) {
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering int value = s->keep_alive_cnt;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "TCP_KEEPCNT failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (s->defer_accept) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int value = s->defer_accept / USEC_PER_SEC;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering if (setsockopt(fd, SOL_TCP, TCP_DEFER_ACCEPT, &value, sizeof(value)) < 0)
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer log_unit_warning_errno(UNIT(s), errno, "TCP_DEFER_ACCEPT failed: %m");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->no_delay) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int b = s->no_delay;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->socket_protocol == IPPROTO_SCTP) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SCTP, SCTP_NODELAY, &b, sizeof(b)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SCTP_NODELAY failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &b, sizeof(b)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "TCP_NODELAY failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther }
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther if (s->broadcast) {
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther int one = 1;
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_BROADCAST failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (s->pass_cred) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering int one = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_PASSCRED failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->pass_sec) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int one = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_PASSSEC failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->priority >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_PRIORITY failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->receive_buffer > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int value = (int) s->receive_buffer;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_RCVBUF failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->send_buffer > 0) {
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek int value = (int) s->send_buffer;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_SNDBUF failed: %m");
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->mark >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0)
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "SO_MARK failed: %m");
84267e4043cf88bf540b5bf9cd65e194670a4ffaLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->ip_tos >= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "IP_TOS failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek if (s->ip_ttl >= 0) {
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek int x;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (socket_ipv6_is_supported())
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek x = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek errno = EAFNOSUPPORT;
ac892057c2ddd8f06323c73ebd80423cc3ec7190Dimitri John Ledkov }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0 && x < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->tcp_congestion)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "TCP_CONGESTION failed: %m");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->smack_ip_in) {
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (r < 0)
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_in_fd: %m");
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn }
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->smack_ip_out) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_out_fd: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_apply_fifo_options(Socket *s, int fd) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(fd >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->pipe_size > 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m");
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->smack) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int fifo_address_create(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path,
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering mode_t directory_mode,
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering mode_t socket_mode) {
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering
fc1d70af2101e16c9e6f3c5bfd5ab315ee9e6daeLennart Poettering _cleanup_close_ int fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek mode_t old_mask;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt struct stat st;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek mkdir_parents_label(path, directory_mode);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = mac_selinux_create_file_prepare(path, S_IFIFO);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Enforce the right access mode for the fifo */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek old_mask = umask(~ socket_mode);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Include the original umask in our mask */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) umask(~socket_mode | old_mask);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering r = mkfifo(path, socket_mode);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering (void) umask(old_mask);
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && errno != EEXIST) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek mac_selinux_create_file_clear();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fstat(fd, &st) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering if (!S_ISFIFO(st.st_mode) ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
00a168618906bea43c3c57e20b9152582c324bf8Olivier Brunel st.st_uid != getuid() ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek st.st_gid != getgid()) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -EEXIST;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt }
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = fd;
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering fd = -1;
93b73b064c663d6248bebfbbbd82989b5ca10fc5Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek mac_selinux_create_file_clear();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poetteringstatic int special_address_create(const char *path, bool writable) {
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering _cleanup_close_ int fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct stat st;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fstat(fd, &st) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Check whether this is a /proc, /sys or /dev file or char device */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EEXIST;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = -1;
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering return r;
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering}
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poetteringstatic int usbffs_address_create(const char *path) {
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering _cleanup_close_ int fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct stat st;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (fstat(fd, &st) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Check whether this is a regular file (ffs endpoint)*/
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (!S_ISREG(st.st_mode))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EEXIST;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering r = fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = -1;
763c7aa288485cf5ab627fe1d25ff58e76f9dacbZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering}
fbb634117d0b0ebd5b105e65b141e75ae9af7f8fLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int mq_address_create(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek mode_t mq_mode,
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering long maxmsg,
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering long msgsize) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_close_ int fd = -1;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct stat st;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering mode_t old_mask;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering struct mq_attr _attr, *attr = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering int r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(path);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (maxmsg > 0 && msgsize > 0) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _attr = (struct mq_attr) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering .mq_flags = O_NONBLOCK,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering .mq_maxmsg = maxmsg,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering .mq_msgsize = msgsize,
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering };
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering attr = &_attr;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering /* Enforce the right access mode for the mq */
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering old_mask = umask(~ mq_mode);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering /* Include the original umask in our mask */
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering (void) umask(~mq_mode | old_mask);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering (void) umask(old_mask);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (fd < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -errno;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (fstat(fd, &st) < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -errno;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering st.st_uid != getuid() ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering st.st_gid != getgid())
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -EEXIST;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = fd;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering fd = -1;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering}
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic int socket_symlink(Socket *s) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering const char *p;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering char **i;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering assert(s);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = socket_find_symlink_target(s);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!p)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return 0;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering STRV_FOREACH(i, s->symlinks)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek symlink_label(p, *i);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int usbffs_write_descs(int fd, Service *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!s->usb_function_descriptors || !s->usb_function_strings)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -EINVAL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = copy_file_fd(s->usb_function_descriptors, fd, false);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return copy_file_fd(s->usb_function_strings, fd, false);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int usbffs_select_ep(const struct dirent *d) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return d->d_name[0] != '.' && !streq(d->d_name, "ep0");
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering}
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic int usbffs_dispatch_eps(SocketPort *p) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_free_ struct dirent **ent = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_free_ char *path = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering int r, i, n, k;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering path = dirname_malloc(p->path);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!path)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = scandir(path, &ent, usbffs_select_ep, alphasort);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek n = r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering p->auxiliary_fds = new(int, n);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!p->auxiliary_fds)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering p->n_auxiliary_fds = n;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering k = 0;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering for (i = 0; i < n; ++i) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering _cleanup_free_ char *ep = NULL;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering ep = path_make_absolute(ent[i]->d_name, path);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (!ep)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return -ENOMEM;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering path_kill_slashes(ep);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = usbffs_address_create(ep);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (r < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering goto fail;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering p->auxiliary_fds[k] = r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering ++k;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering free(ent[i]);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering return r;
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_many(p->auxiliary_fds, k);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p->auxiliary_fds = mfree(p->auxiliary_fds);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p->n_auxiliary_fds = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_open_fds(Socket *s) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering _cleanup_(mac_selinux_freep) char *label = NULL;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering bool know_label = false;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering SocketPort *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
74055aa76278232ff05574fc47c4e6b3560554a7Lennart Poettering
74055aa76278232ff05574fc47c4e6b3560554a7Lennart Poettering assert(s);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (p->fd >= 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering switch (p->type) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering case SOCKET_SOCKET:
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!know_label) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Figure out label, if we don't it know
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering * yet. We do it once, for the first
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering * socket where we need this and
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * remember it for the rest. */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (s->selinux_context_from_net) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Get it from the network label */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = mac_selinux_get_our_label(&label);
4daf54a851e4fb7ed1a13c3117bba12528fd2c7fZbigniew Jędrzejewski-Szmek if (r < 0 && r != -EOPNOTSUPP)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto rollback;
6203e07a83214a55bb1f88508fcda2005c601deaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Get it from the executable we are about to start */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = socket_instantiate_service(s);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto rollback;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (UNIT_ISSET(s->service) &&
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = mac_selinux_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering if (r < 0 && r != -EPERM && r != -EOPNOTSUPP)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto rollback;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering know_label = true;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering /* Apply the socket protocol */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering switch(p->address.type) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering case SOCK_STREAM:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCK_SEQPACKET:
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering if (p->socket->socket_protocol == IPPROTO_SCTP)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p->address.protocol = p->socket->socket_protocol;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCK_DGRAM:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->socket->socket_protocol == IPPROTO_UDPLITE)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->address.protocol = p->socket->socket_protocol;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek r = socket_address_listen(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &p->address,
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering SOCK_CLOEXEC|SOCK_NONBLOCK,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->backlog,
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering s->bind_ipv6_only,
b5884878a2874447b2a9f07f324a7cd909d96d48Lennart Poettering s->bind_to_device,
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt s->reuse_port,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->free_bind,
b5884878a2874447b2a9f07f324a7cd909d96d48Lennart Poettering s->transparent,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->directory_mode,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->socket_mode,
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer label);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto rollback;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek p->fd = r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_apply_socket_options(s, p->fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_symlink(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCKET_SPECIAL:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->fd = special_address_create(p->path, s->writable);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->fd < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = p->fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto rollback;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCKET_FIFO:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->fd = fifo_address_create(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->path,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->directory_mode,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->socket_mode);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (p->fd < 0) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen r = p->fd;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen goto rollback;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen }
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen socket_apply_fifo_options(s, p->fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_symlink(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCKET_MQUEUE:
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek p->fd = mq_address_create(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p->path,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->socket_mode,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->mq_maxmsg,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->mq_msgsize);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->fd < 0) {
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett r = p->fd;
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett goto rollback;
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett }
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett break;
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case SOCKET_USB_FUNCTION:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering p->fd = usbffs_address_create(p->path);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (p->fd < 0) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = p->fd;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto rollback;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service)));
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto rollback;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = usbffs_dispatch_eps(p);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto rollback;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering break;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering default:
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering assert_not_reached("Unknown port type");
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringrollback:
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering socket_close_fds(s);
ca2670162464b98f44d3f30a1d8b47b02609784cMichał Bartoszkiewicz return r;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering}
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic void socket_unwatch_fds(Socket *s) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk SocketPort *p;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering int r;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(s);
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering LIST_FOREACH(port, p, s->ports) {
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering if (p->fd < 0)
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering continue;
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering
6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222bLennart Poettering if (!p->event_source)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering continue;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m");
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering}
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic int socket_watch_fds(Socket *s) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering SocketPort *p;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering int r;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(s);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering LIST_FOREACH(port, p, s->ports) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (p->fd < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk continue;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (p->event_source) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto fail;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering } else {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto fail;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering (void) sd_event_source_set_description(p->event_source, "socket-port-io");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringfail:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt socket_unwatch_fds(s);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic void socket_set_state(Socket *s, SocketState state) {
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner SocketState old_state;
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner assert(s);
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering old_state = s->state;
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering s->state = state;
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner if (!IN_SET(state,
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner SOCKET_START_PRE,
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt SOCKET_START_CHOWN,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SOCKET_START_POST,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SOCKET_STOP_PRE,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SOCKET_STOP_PRE_SIGTERM,
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt SOCKET_STOP_PRE_SIGKILL,
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt SOCKET_STOP_POST,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SOCKET_FINAL_SIGTERM,
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SOCKET_FINAL_SIGKILL)) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering s->timer_event_source = sd_event_source_unref(s->timer_event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_unwatch_control_pid(s);
13790add4bf648fed816361794d8277a75253410Lennart Poettering s->control_command = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (state != SOCKET_LISTENING)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_unwatch_fds(s);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!IN_SET(state,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_START_CHOWN,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_START_POST,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk SOCKET_LISTENING,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk SOCKET_RUNNING,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk SOCKET_STOP_PRE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE_SIGTERM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE_SIGKILL))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_close_fds(s);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (state != old_state)
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state));
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_coldplug(Unit *u) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Socket *s = SOCKET(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s->state == SOCKET_DEAD);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->deserialized_state == s->state)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek
b1389b0d0805392570085acc7cb10eafcf885405Zbigniew Jędrzejewski-Szmek if (s->control_pid > 0 &&
b1389b0d0805392570085acc7cb10eafcf885405Zbigniew Jędrzejewski-Szmek pid_is_unwaited(s->control_pid) &&
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek IN_SET(s->deserialized_state,
d288f79fb4a2fe4a93cf99f74dacd2cebd3f2440Zbigniew Jędrzejewski-Szmek SOCKET_START_PRE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_START_CHOWN,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering SOCKET_START_POST,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt SOCKET_STOP_PRE_SIGTERM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE_SIGKILL,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_POST,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_FINAL_SIGTERM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_FINAL_SIGKILL)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_watch_pid(UNIT(s), s->control_pid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt r = socket_arm_timer(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return r;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt if (IN_SET(s->deserialized_state,
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt SOCKET_START_CHOWN,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_START_POST,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_LISTENING,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_RUNNING,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE_SIGTERM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek SOCKET_STOP_PRE_SIGKILL)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_open_fds(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->deserialized_state == SOCKET_LISTENING) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_watch_fds(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_set_state(s, s->deserialized_state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poetteringstatic int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering _cleanup_free_ char **argv = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek pid_t pid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ExecParameters exec_params = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .apply_permissions = true,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .apply_chroot = true,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .apply_tty_stdin = true,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .bus_endpoint_fd = -1,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .stdin_fd = -1,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering .stdout_fd = -1,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering .stderr_fd = -1,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering };
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(s);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(c);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(_pid);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (void) unit_realize_cgroup(UNIT(s));
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering if (s->reset_cpu_usage) {
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering (void) unit_reset_cpu_usage(UNIT(s));
13790add4bf648fed816361794d8277a75253410Lennart Poettering s->reset_cpu_usage = false;
13790add4bf648fed816361794d8277a75253410Lennart Poettering }
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering r = unit_setup_exec_runtime(UNIT(s));
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (r < 0)
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering goto fail;
13790add4bf648fed816361794d8277a75253410Lennart Poettering
13790add4bf648fed816361794d8277a75253410Lennart Poettering r = socket_arm_timer(s);
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (r < 0)
4ec3cd7391e119b597375c547cf4ed50fce9f115Lennart Poettering goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
13790add4bf648fed816361794d8277a75253410Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek exec_params.argv = argv;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.environment = UNIT(s)->manager->environment;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.cgroup_path = UNIT(s)->cgroup_path;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.cgroup_delegate = s->cgroup_context.delegate;
13790add4bf648fed816361794d8277a75253410Lennart Poettering exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = exec_spawn(UNIT(s),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek c,
13790add4bf648fed816361794d8277a75253410Lennart Poettering &s->exec_context,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &exec_params,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->exec_runtime,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &pid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_watch_pid(UNIT(s), pid);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (r < 0)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* FIXME: we need to do something here */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering goto fail;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *_pid = pid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering s->timer_event_source = sd_event_source_unref(s->timer_event_source);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic int socket_chown(Socket *s, pid_t *_pid) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek pid_t pid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_arm_timer(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /* We have to resolve the user names out-of-process, hence
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * let's fork here. It's messy, but well, what can we do? */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek pid = fork();
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (pid < 0)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return -errno;
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (pid == 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering SocketPort *p;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering uid_t uid = UID_INVALID;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering gid_t gid = GID_INVALID;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int ret;
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) ignore_signals(SIGPIPE, -1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_forget_fds();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!isempty(s->user)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *user = s->user;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_user_creds(&user, &uid, &gid, NULL, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ret = EXIT_USER;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail_child;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!isempty(s->group)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *group = s->group;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_group_creds(&group, &gid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ret = EXIT_GROUP;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto fail_child;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *path = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->type == SOCKET_SOCKET)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path = socket_address_get_path(&p->address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (p->type == SOCKET_FIFO)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path = p->path;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!path)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek continue;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (chown(path, uid, gid) < 0) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = -errno;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering ret = EXIT_CHOWN;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto fail_child;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering _exit(0);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering fail_child:
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering log_open();
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_error_errno(r, "Failed to chown socket at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _exit(ret);
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering }
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering r = unit_watch_pid(UNIT(s), pid);
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering if (r < 0)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering goto fail;
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering *_pid = pid;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfail:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->timer_event_source = sd_event_source_unref(s->timer_event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_enter_dead(Socket *s, SocketResult f) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering assert(s);
99d0966e75a984bed4f117c888ecc93e16e7b7b6Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (f != SOCKET_SUCCESS)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->result = f;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek exec_runtime_destroy(s->exec_runtime);
3e044c492e3ebe64f4e3175c94f9db8a62557b82Markus Elfring s->exec_runtime = exec_runtime_unref(s->exec_runtime);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_enter_signal(Socket *s, SocketState state, SocketResult f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_enter_stop_post(Socket *s, SocketResult f) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (f != SOCKET_SUCCESS)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->result = f;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering socket_unwatch_control_pid(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->control_command_id = SOCKET_EXEC_STOP_POST;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->control_command) {
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
socket_set_state(s, SOCKET_STOP_POST);
} else
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS);
return;
fail:
log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m");
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
int r;
assert(s);
if (f != SOCKET_SUCCESS)
s->result = f;
r = unit_kill_context(
UNIT(s),
&s->kill_context,
(state != SOCKET_STOP_PRE_SIGTERM && state != SOCKET_FINAL_SIGTERM) ?
KILL_KILL : KILL_TERMINATE,
-1,
s->control_pid,
false);
if (r < 0)
goto fail;
if (r > 0) {
r = socket_arm_timer(s);
if (r < 0)
goto fail;
socket_set_state(s, state);
} else if (state == SOCKET_STOP_PRE_SIGTERM)
socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS);
else if (state == SOCKET_STOP_PRE_SIGKILL)
socket_enter_stop_post(s, SOCKET_SUCCESS);
else if (state == SOCKET_FINAL_SIGTERM)
socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
else
socket_enter_dead(s, SOCKET_SUCCESS);
return;
fail:
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
else
socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_stop_pre(Socket *s, SocketResult f) {
int r;
assert(s);
if (f != SOCKET_SUCCESS)
s->result = f;
socket_unwatch_control_pid(s);
s->control_command_id = SOCKET_EXEC_STOP_PRE;
s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
if (s->control_command) {
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
socket_set_state(s, SOCKET_STOP_PRE);
} else
socket_enter_stop_post(s, SOCKET_SUCCESS);
return;
fail:
log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-pre' task: %m");
socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_listening(Socket *s) {
int r;
assert(s);
r = socket_watch_fds(s);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
goto fail;
}
socket_set_state(s, SOCKET_LISTENING);
return;
fail:
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_start_post(Socket *s) {
int r;
assert(s);
socket_unwatch_control_pid(s);
s->control_command_id = SOCKET_EXEC_START_POST;
s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
if (s->control_command) {
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
goto fail;
}
socket_set_state(s, SOCKET_START_POST);
} else
socket_enter_listening(s);
return;
fail:
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_start_chown(Socket *s) {
int r;
assert(s);
r = socket_open_fds(s);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
goto fail;
}
if (!isempty(s->user) || !isempty(s->group)) {
socket_unwatch_control_pid(s);
s->control_command_id = SOCKET_EXEC_START_CHOWN;
s->control_command = NULL;
r = socket_chown(s, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to fork 'start-chown' task: %m");
goto fail;
}
socket_set_state(s, SOCKET_START_CHOWN);
} else
socket_enter_start_post(s);
return;
fail:
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_start_pre(Socket *s) {
int r;
assert(s);
socket_unwatch_control_pid(s);
s->control_command_id = SOCKET_EXEC_START_PRE;
s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
if (s->control_command) {
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
goto fail;
}
socket_set_state(s, SOCKET_START_PRE);
} else
socket_enter_start_chown(s);
return;
fail:
socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
}
static void socket_enter_running(Socket *s, int cfd) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(s);
/* We don't take connections anymore if we are supposed to
* shut down anyway */
if (unit_stop_pending(UNIT(s))) {
log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled.");
if (cfd >= 0)
safe_close(cfd);
else {
/* Flush all sockets by closing and reopening them */
socket_close_fds(s);
r = socket_open_fds(s);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
return;
}
r = socket_watch_fds(s);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
}
}
return;
}
if (cfd < 0) {
Iterator i;
Unit *other;
bool pending = false;
/* If there's already a start pending don't bother to
* do anything */
SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
if (unit_active_or_pending(other)) {
pending = true;
break;
}
if (!pending) {
if (!UNIT_ISSET(s->service)) {
log_unit_error(UNIT(s), "Service to activate vanished, refusing activation.");
r = -ENOENT;
goto fail;
}
r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, &error, NULL);
if (r < 0)
goto fail;
}
socket_set_state(s, SOCKET_RUNNING);
} else {
_cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL;
Service *service;
if (s->n_connections >= s->max_connections) {
log_unit_warning(UNIT(s), "Too many incoming connections (%u)", s->n_connections);
safe_close(cfd);
return;
}
r = socket_instantiate_service(s);
if (r < 0)
goto fail;
r = instance_from_socket(cfd, s->n_accepted, &instance);
if (r < 0) {
if (r != -ENOTCONN)
goto fail;
/* ENOTCONN is legitimate if TCP RST was received.
* This connection is over, but the socket unit lives on. */
safe_close(cfd);
return;
}
r = unit_name_to_prefix(UNIT(s)->id, &prefix);
if (r < 0)
goto fail;
r = unit_name_build(prefix, instance, ".service", &name);
if (r < 0)
goto fail;
r = unit_add_name(UNIT_DEREF(s->service), name);
if (r < 0)
goto fail;
service = SERVICE(UNIT_DEREF(s->service));
unit_ref_unset(&s->service);
s->n_accepted ++;
UNIT(service)->no_gc = false;
unit_choose_id(UNIT(service), name);
r = service_set_socket_fd(service, cfd, s, s->selinux_context_from_net);
if (r < 0)
goto fail;
cfd = -1;
s->n_connections ++;
r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, &error, NULL);
if (r < 0)
goto fail;
/* Notify clients about changed counters */
unit_add_to_dbus_queue(UNIT(s));
}
return;
fail:
log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
cfd >= 0 ? "template" : "non-template",
bus_error_message(&error, r));
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
safe_close(cfd);
}
static void socket_run_next(Socket *s) {
int r;
assert(s);
assert(s->control_command);
assert(s->control_command->command_next);
socket_unwatch_control_pid(s);
s->control_command = s->control_command->command_next;
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
return;
fail:
log_unit_warning_errno(UNIT(s), r, "Failed to run next task: %m");
if (s->state == SOCKET_START_POST)
socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
else if (s->state == SOCKET_STOP_POST)
socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
else
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
}
static int socket_start(Unit *u) {
Socket *s = SOCKET(u);
assert(s);
/* We cannot fulfill this request right now, try again later
* please! */
if (IN_SET(s->state,
SOCKET_STOP_PRE,
SOCKET_STOP_PRE_SIGKILL,
SOCKET_STOP_PRE_SIGTERM,
SOCKET_STOP_POST,
SOCKET_FINAL_SIGTERM,
SOCKET_FINAL_SIGKILL))
return -EAGAIN;
/* Already on it! */
if (IN_SET(s->state,
SOCKET_START_PRE,
SOCKET_START_CHOWN,
SOCKET_START_POST))
return 0;
/* Cannot run this without the service being around */
if (UNIT_ISSET(s->service)) {
Service *service;
service = SERVICE(UNIT_DEREF(s->service));
if (UNIT(service)->load_state != UNIT_LOADED) {
log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id);
return -ENOENT;
}
/* If the service is already active we cannot start the
* socket */
if (service->state != SERVICE_DEAD &&
service->state != SERVICE_FAILED &&
service->state != SERVICE_AUTO_RESTART) {
log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id);
return -EBUSY;
}
}
assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
s->result = SOCKET_SUCCESS;
s->reset_cpu_usage = true;
socket_enter_start_pre(s);
return 1;
}
static int socket_stop(Unit *u) {
Socket *s = SOCKET(u);
assert(s);
/* Already on it */
if (IN_SET(s->state,
SOCKET_STOP_PRE,
SOCKET_STOP_PRE_SIGTERM,
SOCKET_STOP_PRE_SIGKILL,
SOCKET_STOP_POST,
SOCKET_FINAL_SIGTERM,
SOCKET_FINAL_SIGKILL))
return 0;
/* If there's already something running we go directly into
* kill mode. */
if (IN_SET(s->state,
SOCKET_START_PRE,
SOCKET_START_CHOWN,
SOCKET_START_POST)) {
socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
return -EAGAIN;
}
assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
socket_enter_stop_pre(s, SOCKET_SUCCESS);
return 1;
}
static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
Socket *s = SOCKET(u);
SocketPort *p;
int r;
assert(u);
assert(f);
assert(fds);
unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
unit_serialize_item(u, f, "result", socket_result_to_string(s->result));
unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
if (s->control_pid > 0)
unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
if (s->control_command_id >= 0)
unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id));
LIST_FOREACH(port, p, s->ports) {
int copy;
if (p->fd < 0)
continue;
copy = fdset_put_dup(fds, p->fd);
if (copy < 0)
return copy;
if (p->type == SOCKET_SOCKET) {
_cleanup_free_ char *t = NULL;
r = socket_address_print(&p->address, &t);
if (r < 0)
return r;
if (socket_address_family(&p->address) == AF_NETLINK)
unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
else
unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
} else if (p->type == SOCKET_SPECIAL)
unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
else if (p->type == SOCKET_MQUEUE)
unit_serialize_item_format(u, f, "mqueue", "%i %s", copy, p->path);
else if (p->type == SOCKET_USB_FUNCTION)
unit_serialize_item_format(u, f, "ffs", "%i %s", copy, p->path);
else {
assert(p->type == SOCKET_FIFO);
unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
}
}
return 0;
}
static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
Socket *s = SOCKET(u);
assert(u);
assert(key);
assert(value);
if (streq(key, "state")) {
SocketState state;
state = socket_state_from_string(value);
if (state < 0)
log_unit_debug(u, "Failed to parse state value: %s", value);
else
s->deserialized_state = state;
} else if (streq(key, "result")) {
SocketResult f;
f = socket_result_from_string(value);
if (f < 0)
log_unit_debug(u, "Failed to parse result value: %s", value);
else if (f != SOCKET_SUCCESS)
s->result = f;
} else if (streq(key, "n-accepted")) {
unsigned k;
if (safe_atou(value, &k) < 0)
log_unit_debug(u, "Failed to parse n-accepted value: %s", value);
else
s->n_accepted += k;
} else if (streq(key, "control-pid")) {
pid_t pid;
if (parse_pid(value, &pid) < 0)
log_unit_debug(u, "Failed to parse control-pid value: %s", value);
else
s->control_pid = pid;
} else if (streq(key, "control-command")) {
SocketExecCommand id;
id = socket_exec_command_from_string(value);
if (id < 0)
log_unit_debug(u, "Failed to parse exec-command value: %s", value);
else {
s->control_command_id = id;
s->control_command = s->exec_command[id];
}
} else if (streq(key, "fifo")) {
int fd, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse fifo value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (p->type == SOCKET_FIFO &&
path_equal_or_files_same(p->path, value+skip))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else if (streq(key, "special")) {
int fd, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse special value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (p->type == SOCKET_SPECIAL &&
path_equal_or_files_same(p->path, value+skip))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else if (streq(key, "mqueue")) {
int fd, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse mqueue value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (p->type == SOCKET_MQUEUE &&
streq(p->path, value+skip))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else if (streq(key, "socket")) {
int fd, type, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse socket value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (socket_address_is(&p->address, value+skip, type))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else if (streq(key, "netlink")) {
int fd, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse socket value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (socket_address_is_netlink(&p->address, value+skip))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else if (streq(key, "ffs")) {
int fd, skip = 0;
SocketPort *p;
if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
log_unit_debug(u, "Failed to parse ffs value: %s", value);
else {
LIST_FOREACH(port, p, s->ports)
if (p->type == SOCKET_USB_FUNCTION &&
path_equal_or_files_same(p->path, value+skip))
break;
if (p) {
safe_close(p->fd);
p->fd = fdset_remove(fds, fd);
}
}
} else
log_unit_debug(UNIT(s), "Unknown serialization key: %s", key);
return 0;
}
static void socket_distribute_fds(Unit *u, FDSet *fds) {
Socket *s = SOCKET(u);
SocketPort *p;
assert(u);
LIST_FOREACH(port, p, s->ports) {
Iterator i;
int fd;
if (p->type != SOCKET_SOCKET)
continue;
if (p->fd >= 0)
continue;
FDSET_FOREACH(fd, fds, i) {
if (socket_address_matches_fd(&p->address, fd)) {
p->fd = fdset_remove(fds, fd);
s->deserialized_state = SOCKET_LISTENING;
break;
}
}
}
}
_pure_ static UnitActiveState socket_active_state(Unit *u) {
assert(u);
return state_translation_table[SOCKET(u)->state];
}
_pure_ static const char *socket_sub_state_to_string(Unit *u) {
assert(u);
return socket_state_to_string(SOCKET(u)->state);
}
const char* socket_port_type_to_string(SocketPort *p) {
assert(p);
switch (p->type) {
case SOCKET_SOCKET:
switch (p->address.type) {
case SOCK_STREAM:
return "Stream";
case SOCK_DGRAM:
return "Datagram";
case SOCK_SEQPACKET:
return "SequentialPacket";
case SOCK_RAW:
if (socket_address_family(&p->address) == AF_NETLINK)
return "Netlink";
default:
return NULL;
}
case SOCKET_SPECIAL:
return "Special";
case SOCKET_MQUEUE:
return "MessageQueue";
case SOCKET_FIFO:
return "FIFO";
case SOCKET_USB_FUNCTION:
return "USBFunction";
default:
return NULL;
}
}
_pure_ static bool socket_check_gc(Unit *u) {
Socket *s = SOCKET(u);
assert(u);
return s->n_connections > 0;
}
static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
SocketPort *p = userdata;
int cfd = -1;
assert(p);
assert(fd >= 0);
if (p->socket->state != SOCKET_LISTENING)
return 0;
log_unit_debug(UNIT(p->socket), "Incoming traffic");
if (revents != EPOLLIN) {
if (revents & EPOLLHUP)
log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.");
else
log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents);
goto fail;
}
if (p->socket->accept &&
p->type == SOCKET_SOCKET &&
socket_address_can_accept(&p->address)) {
for (;;) {
cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
if (cfd < 0) {
if (errno == EINTR)
continue;
log_unit_error_errno(UNIT(p->socket), errno, "Failed to accept socket: %m");
goto fail;
}
break;
}
socket_apply_socket_options(p->socket, cfd);
}
socket_enter_running(p->socket, cfd);
return 0;
fail:
socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES);
return 0;
}
static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Socket *s = SOCKET(u);
SocketResult f;
assert(s);
assert(pid >= 0);
if (pid != s->control_pid)
return;
s->control_pid = 0;
if (is_clean_exit(code, status, NULL))
f = SOCKET_SUCCESS;
else if (code == CLD_EXITED)
f = SOCKET_FAILURE_EXIT_CODE;
else if (code == CLD_KILLED)
f = SOCKET_FAILURE_SIGNAL;
else if (code == CLD_DUMPED)
f = SOCKET_FAILURE_CORE_DUMP;
else
assert_not_reached("Unknown sigchld code");
if (s->control_command) {
exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
if (s->control_command->ignore)
f = SOCKET_SUCCESS;
}
log_unit_full(u, f == SOCKET_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
"Control process exited, code=%s status=%i",
sigchld_code_to_string(code), status);
if (f != SOCKET_SUCCESS)
s->result = f;
if (s->control_command &&
s->control_command->command_next &&
f == SOCKET_SUCCESS) {
log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state));
socket_run_next(s);
} else {
s->control_command = NULL;
s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
/* No further commands for this step, so let's figure
* out what to do next */
log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state));
switch (s->state) {
case SOCKET_START_PRE:
if (f == SOCKET_SUCCESS)
socket_enter_start_chown(s);
else
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f);
break;
case SOCKET_START_CHOWN:
if (f == SOCKET_SUCCESS)
socket_enter_start_post(s);
else
socket_enter_stop_pre(s, f);
break;
case SOCKET_START_POST:
if (f == SOCKET_SUCCESS)
socket_enter_listening(s);
else
socket_enter_stop_pre(s, f);
break;
case SOCKET_STOP_PRE:
case SOCKET_STOP_PRE_SIGTERM:
case SOCKET_STOP_PRE_SIGKILL:
socket_enter_stop_post(s, f);
break;
case SOCKET_STOP_POST:
case SOCKET_FINAL_SIGTERM:
case SOCKET_FINAL_SIGKILL:
socket_enter_dead(s, f);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
}
/* Notify clients about changed exit status */
unit_add_to_dbus_queue(u);
}
static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
Socket *s = SOCKET(userdata);
assert(s);
assert(s->timer_event_source == source);
switch (s->state) {
case SOCKET_START_PRE:
log_unit_warning(UNIT(s), "Starting timed out. Terminating.");
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
break;
case SOCKET_START_CHOWN:
case SOCKET_START_POST:
log_unit_warning(UNIT(s), "Starting timed out. Stopping.");
socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT);
break;
case SOCKET_STOP_PRE:
log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT);
break;
case SOCKET_STOP_PRE_SIGTERM:
if (s->kill_context.send_sigkill) {
log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT);
} else {
log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring.");
socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
}
break;
case SOCKET_STOP_PRE_SIGKILL:
log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
break;
case SOCKET_STOP_POST:
log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating.");
socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
break;
case SOCKET_FINAL_SIGTERM:
if (s->kill_context.send_sigkill) {
log_unit_warning(UNIT(s), "Stopping timed out (2). Killing.");
socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT);
} else {
log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
}
break;
case SOCKET_FINAL_SIGKILL:
log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode.");
socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
break;
default:
assert_not_reached("Timeout at wrong time.");
}
return 0;
}
int socket_collect_fds(Socket *s, int **fds) {
int *rfds, k = 0, n = 0;
SocketPort *p;
assert(s);
assert(fds);
/* Called from the service code for requesting our fds */
LIST_FOREACH(port, p, s->ports) {
if (p->fd >= 0)
n++;
n += p->n_auxiliary_fds;
}
if (n <= 0) {
*fds = NULL;
return 0;
}
rfds = new(int, n);
if (!rfds)
return -ENOMEM;
LIST_FOREACH(port, p, s->ports) {
int i;
if (p->fd >= 0)
rfds[k++] = p->fd;
for (i = 0; i < p->n_auxiliary_fds; ++i)
rfds[k++] = p->auxiliary_fds[i];
}
assert(k == n);
*fds = rfds;
return n;
}
static void socket_reset_failed(Unit *u) {
Socket *s = SOCKET(u);
assert(s);
if (s->state == SOCKET_FAILED)
socket_set_state(s, SOCKET_DEAD);
s->result = SOCKET_SUCCESS;
}
static void socket_notify_service_dead(Socket *s, bool failed_permanent) {
assert(s);
/* The service is dead. Dang!
*
* This is strictly for one-instance-for-all-connections
* services. */
if (s->state == SOCKET_RUNNING) {
log_unit_debug(UNIT(s), "Got notified about service death (failed permanently: %s)", yes_no(failed_permanent));
if (failed_permanent)
socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT);
else
socket_enter_listening(s);
}
}
void socket_connection_unref(Socket *s) {
assert(s);
/* The service is dead. Yay!
*
* This is strictly for one-instance-per-connection
* services. */
assert(s->n_connections > 0);
s->n_connections--;
log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections);
}
static void socket_trigger_notify(Unit *u, Unit *other) {
Socket *s = SOCKET(u);
Service *se;
assert(u);
assert(other);
/* Don't propagate state changes from the service if we are
already down or accepting connections */
if ((s->state != SOCKET_RUNNING &&
s->state != SOCKET_LISTENING) ||
s->accept)
return;
if (other->load_state != UNIT_LOADED ||
other->type != UNIT_SERVICE)
return;
se = SERVICE(other);
if (se->state == SERVICE_FAILED)
socket_notify_service_dead(s, se->result == SERVICE_FAILURE_START_LIMIT);
if (se->state == SERVICE_DEAD ||
se->state == SERVICE_FINAL_SIGTERM ||
se->state == SERVICE_FINAL_SIGKILL ||
se->state == SERVICE_AUTO_RESTART)
socket_notify_service_dead(s, false);
if (se->state == SERVICE_RUNNING)
socket_set_state(s, SOCKET_RUNNING);
}
static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error);
}
static int socket_get_timeout(Unit *u, uint64_t *timeout) {
Socket *s = SOCKET(u);
int r;
if (!s->timer_event_source)
return 0;
r = sd_event_source_get_time(s->timer_event_source, timeout);
if (r < 0)
return r;
return 1;
}
char *socket_fdname(Socket *s) {
assert(s);
/* Returns the name to use for $LISTEN_NAMES. If the user
* didn't specify anything specifically, use the socket unit's
* name as fallback. */
if (s->fdname)
return s->fdname;
return UNIT(s)->id;
}
static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
[SOCKET_EXEC_START_PRE] = "StartPre",
[SOCKET_EXEC_START_CHOWN] = "StartChown",
[SOCKET_EXEC_START_POST] = "StartPost",
[SOCKET_EXEC_STOP_PRE] = "StopPre",
[SOCKET_EXEC_STOP_POST] = "StopPost"
};
DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
static const char* const socket_result_table[_SOCKET_RESULT_MAX] = {
[SOCKET_SUCCESS] = "success",
[SOCKET_FAILURE_RESOURCES] = "resources",
[SOCKET_FAILURE_TIMEOUT] = "timeout",
[SOCKET_FAILURE_EXIT_CODE] = "exit-code",
[SOCKET_FAILURE_SIGNAL] = "signal",
[SOCKET_FAILURE_CORE_DUMP] = "core-dump",
[SOCKET_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent"
};
DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult);
const UnitVTable socket_vtable = {
.object_size = sizeof(Socket),
.exec_context_offset = offsetof(Socket, exec_context),
.cgroup_context_offset = offsetof(Socket, cgroup_context),
.kill_context_offset = offsetof(Socket, kill_context),
.exec_runtime_offset = offsetof(Socket, exec_runtime),
.sections =
"Unit\0"
"Socket\0"
"Install\0",
.private_section = "Socket",
.init = socket_init,
.done = socket_done,
.load = socket_load,
.coldplug = socket_coldplug,
.dump = socket_dump,
.start = socket_start,
.stop = socket_stop,
.kill = socket_kill,
.get_timeout = socket_get_timeout,
.serialize = socket_serialize,
.deserialize_item = socket_deserialize_item,
.distribute_fds = socket_distribute_fds,
.active_state = socket_active_state,
.sub_state_to_string = socket_sub_state_to_string,
.check_gc = socket_check_gc,
.sigchld_event = socket_sigchld_event,
.trigger_notify = socket_trigger_notify,
.reset_failed = socket_reset_failed,
.bus_vtable = bus_socket_vtable,
.bus_set_property = bus_socket_set_property,
.bus_commit_properties = bus_socket_commit_properties,
.status_message_formats = {
/*.starting_stopping = {
[0] = "Starting socket %s...",
[1] = "Stopping socket %s...",
},*/
.finished_start_job = {
[JOB_DONE] = "Listening on %s.",
[JOB_FAILED] = "Failed to listen on %s.",
[JOB_TIMEOUT] = "Timed out starting %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Closed %s.",
[JOB_FAILED] = "Failed stopping %s.",
[JOB_TIMEOUT] = "Timed out stopping %s.",
},
},
};