busname.c revision c95f97a20f3b854109dc564da64950067b388aeb
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2013 Lennart Poettering
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c50a4525aa8151b180d5a325e88c5f3812e66c36Martin Pitt Lesser General Public License for more details.
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier***/
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <sys/mman.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "special.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "bus-kernel.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "bus-internal.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "bus-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "service.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "dbus-busname.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "busname.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "kdbus.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_DEAD] = UNIT_INACTIVE,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_MAKING] = UNIT_ACTIVATING,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_REGISTERED] = UNIT_ACTIVE,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_LISTENING] = UNIT_ACTIVE,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_RUNNING] = UNIT_ACTIVE,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier [BUSNAME_FAILED] = UNIT_FAILED
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier};
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyerstatic int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyerstatic void busname_init(Unit *u) {
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer BusName *n = BUSNAME(u);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer assert(u);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer assert(u->load_state == UNIT_STUB);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier n->starter_fd = -1;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier n->accept_fd = true;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier n->activating = true;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier n->timeout_usec = u->manager->default_timeout_start_usec;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier}
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic void busname_unwatch_control_pid(BusName *n) {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier assert(n);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt if (n->control_pid <= 0)
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier unit_unwatch_pid(UNIT(n), n->control_pid);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier n->control_pid = 0;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier}
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic void busname_free_policy(BusName *n) {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier BusNamePolicy *p;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier assert(n);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier while ((p = n->policy)) {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier LIST_REMOVE(policy, n->policy, p);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(p->name);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(p);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier }
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier}
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmekstatic void busname_close_fd(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->starter_event_source = sd_event_source_unref(n->starter_event_source);
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek n->starter_fd = safe_close(n->starter_fd);
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void busname_done(Unit *u) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier BusName *n = BUSNAME(u);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier free(n->name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->name = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_free_policy(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_unwatch_control_pid(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_close_fd(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unit_ref_unset(&n->service);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->timer_event_source = sd_event_source_unref(n->timer_event_source);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_arm_timer(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->timeout_usec <= 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->timer_event_source = sd_event_source_unref(n->timer_event_source);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->timer_event_source) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_event_add_time(
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek UNIT(n)->manager->event,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier &n->timer_event_source,
2375607039517c88df51ef16ddbb624ec1c10654Kay Sievers CLOCK_MONOTONIC,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier now(CLOCK_MONOTONIC) + n->timeout_usec, 0,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_dispatch_timer, n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_add_default_default_dependencies(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
b8667ee4162cd2510363602b417cecede9fd2ccaZbigniew Jędrzejewski-Szmek if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_add_extras(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Unit *u = UNIT(n);
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!n->name) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->name = unit_name_to_prefix(u->id);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!n->name)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -ENOMEM;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!u->description) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_set_description(u, n->name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->activating) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!UNIT_DEREF(n->service)) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Unit *x;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_load_related_unit(u, ".service", &x);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unit_ref_set(&n->service, x);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (u->default_dependencies) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = busname_add_default_default_dependencies(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_verify(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char *e;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (UNIT(n)->load_state != UNIT_LOADED)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!service_name_is_valid(n->name)) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_unit_error(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -EINVAL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier e = strappenda(n->name, ".busname");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!unit_has_name(UNIT(n), e)) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_unit_error(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -EINVAL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_load(Unit *u) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier BusName *n = BUSNAME(u);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(u);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(u->load_state == UNIT_STUB);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = unit_load_fragment_and_dropin(u);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (u->load_state == UNIT_LOADED) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* This is a new unit? Then let's add in some extras */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = busname_add_extras(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return busname_verify(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier BusName *n = BUSNAME(u);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt fprintf(f,
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt "%sBus Name State: %s\n"
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt "%sResult: %s\n"
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "%sName: %s\n"
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "%sActivating: %s\n"
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "%sAccept FD: %s\n",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, busname_state_to_string(n->state),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, busname_result_to_string(n->result),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, n->name,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, yes_no(n->activating),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, yes_no(n->accept_fd));
cffae62bcb6912fbaf1b7b282d9d170c9d308897Martin Pitt
99877b7e3782a51b31bf191825f0335500f52fe5Harald Hoyer if (n->control_pid > 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier fprintf(f,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "%sControl PID: "PID_FMT"\n",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier prefix, n->control_pid);
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt}
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void busname_unwatch_fd(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!n->starter_event_source)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_unit_debug(UNIT(n)->id, "Failed to disable event source.");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_watch_fd(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->starter_fd < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->starter_event_source)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_unit_warning_errno(UNIT(n)->id, r, "Failed to watch starter fd: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_unwatch_fd(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_open_fd(BusName *n) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_free_ char *path = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *mode;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->starter_fd >= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n->starter_fd < 0) {
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt log_unit_warning_errno(UNIT(n)->id, n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt return n->starter_fd;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void busname_set_state(BusName *n, BusNameState state) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier BusNameState old_state;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier old_state = n->state;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->state = state;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n->timer_event_source = sd_event_source_unref(n->timer_event_source);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_unwatch_control_pid(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (state != BUSNAME_LISTENING)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_unwatch_fd(n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier busname_close_fd(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (state != old_state)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(UNIT(n)->id, "%s changed %s -> %s",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_coldplug(Unit *u) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n->state == BUSNAME_DEAD);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->deserialized_state == n->state)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->control_pid <= 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EBADMSG;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = unit_watch_pid(UNIT(n), n->control_pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_arm_timer(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_open_fd(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->deserialized_state == BUSNAME_LISTENING) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_watch_fd(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, n->deserialized_state);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int busname_make_starter(BusName *n, pid_t *_pid) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier pid_t pid;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_arm_timer(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* We have to resolve the user/group names out-of-process,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * hence let's fork here. It's messy, but well, what can we
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * do? */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer pid = fork();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (pid < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -errno;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (pid == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int ret;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ignore_signals(SIGPIPE, -1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_forget_fds();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ret = EXIT_MAKE_STARTER;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail_child;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _exit(0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fail_child:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_open();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _exit(ret);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = unit_watch_pid(UNIT(n), pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *_pid = pid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->timer_event_source = sd_event_source_unref(n->timer_event_source);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void busname_enter_dead(BusName *n, BusNameResult f) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (f != BUSNAME_SUCCESS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->result = f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer KillContext kill_context = {};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (f != BUSNAME_SUCCESS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->result = f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer kill_context_init(&kill_context);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = unit_kill_context(UNIT(n),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer &kill_context,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer -1,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->control_pid,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer false);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning_errno(UNIT(n)->id, r, "%s failed to kill control process: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r > 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_arm_timer(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning_errno(UNIT(n)->id, r, "%s failed to arm timer: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, state);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (state == BUSNAME_SIGTERM)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_SUCCESS);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic void busname_enter_listening(BusName *n) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->activating) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_watch_fd(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning_errno(UNIT(n)->id, r, "%s failed to watch names: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, BUSNAME_LISTENING);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, BUSNAME_REGISTERED);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void busname_enter_making(BusName *n) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_open_fd(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->policy) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If there is a policy, we need to resolve user/group
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * names, which we can't do from PID1, hence let's
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * fork. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_unwatch_control_pid(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = busname_make_starter(n, &n->control_pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning_errno(UNIT(n)->id, r, "%s failed to fork 'making' task: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, BUSNAME_MAKING);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If there is no policy, we can do everything
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * directly from PID 1, hence do so. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning_errno(UNIT(n)->id, r, "%s failed to make starter: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_listening(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void busname_enter_running(BusName *n) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer bool pending = false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Unit *other;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Iterator i;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!n->activating)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* We don't take conenctions anymore if we are supposed to
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * shut down anyway */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (unit_stop_pending(UNIT(n))) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Flush all queued activation reqeuest by closing and reopening the connection */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer bus_kernel_drop_one(n->starter_fd);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_listening(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If there's already a start pending don't bother to do
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * anything */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (unit_active_or_pending(other)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer pending = true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!pending) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_set_state(n, BUSNAME_RUNNING);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_start(Unit *u) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* We cannot fulfill this request right now, try again later
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * please! */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EAGAIN;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Already on it! */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->state == BUSNAME_MAKING)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->activating && UNIT_ISSET(n->service)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Service *service;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer service = SERVICE(UNIT_DEREF(n->service));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (UNIT(service)->load_state != UNIT_LOADED) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_error(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOENT;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->result = BUSNAME_SUCCESS;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_making(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_stop(Unit *u) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Already on it */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If there's already something running, we go directly into
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * kill mode. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->state == BUSNAME_MAKING) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EAGAIN;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_SUCCESS);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(fds);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->control_pid > 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->starter_fd >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int copy;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer copy = fdset_put_dup(fds, n->starter_fd);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (copy < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return copy;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(key);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (streq(key, "state")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusNameState state;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer state = busname_state_from_string(value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (state < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(u->id, "Failed to parse state value %s", value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->deserialized_state = state;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (streq(key, "result")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusNameResult f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = busname_result_from_string(value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (f < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(u->id, "Failed to parse result value %s", value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (f != BUSNAME_SUCCESS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->result = f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (streq(key, "control-pid")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer pid_t pid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (parse_pid(value, &pid) < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->control_pid = pid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (streq(key, "starter-fd")) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int fd;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(u->id, "Failed to parse starter fd value %s", value);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer safe_close(n->starter_fd);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->starter_fd = fdset_remove(fds, fd);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(u->id, "Unknown serialization key '%s'", key);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer_pure_ static UnitActiveState busname_active_state(Unit *u) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return state_translation_table[BUSNAME(u)->state];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer_pure_ static const char *busname_sub_state_to_string(Unit *u) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return busname_state_to_string(BUSNAME(u)->state);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_peek_message(BusName *n) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct kdbus_cmd_recv cmd_recv = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer .flags = KDBUS_RECV_PEEK,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer };
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct kdbus_cmd_free cmd_free = {};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *comm = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct kdbus_item *d;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct kdbus_msg *k;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer size_t start, ps, sz, delta;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer void *p = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer pid_t pid = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Generate a friendly debug log message about which process
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * caused triggering of this bus name. This simply peeks the
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * metadata of the first queued message and logs it. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Let's shortcut things a bit, if debug logging is turned off
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * anyway. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (log_get_max_level() < LOG_DEBUG)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ioctl(n->starter_fd, KDBUS_CMD_MSG_RECV, &cmd_recv);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (errno == EINTR || errno == EAGAIN)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -errno;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* We map as late as possible, and unmap imemdiately after
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * use. On 32bit address space is scarce and we want to be
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * able to handle a lot of activator connections at the same
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * time, and hence shouldn't keep the mmap()s around for
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * longer than necessary. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ps = page_size();
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer start = (cmd_recv.offset / ps) * ps;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer delta = cmd_recv.offset - start;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sz = PAGE_ALIGN(delta + cmd_recv.msg_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (p == MAP_FAILED) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -errno;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto finish;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer k = (struct kdbus_msg *) ((uint8_t *) p + delta);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer KDBUS_ITEM_FOREACH(d, k, items) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer switch (d->type) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case KDBUS_ITEM_PIDS:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer pid = d->pids.pid;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case KDBUS_ITEM_PID_COMM:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer comm = d->str;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (pid > 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfinish:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (p)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (void) munmap(p, sz);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer cmd_free.offset = cmd_recv.offset;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(fd >= 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n->state != BUSNAME_LISTENING)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (revents != EPOLLIN) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer UNIT(n)->id, revents);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_peek_message(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_running(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusName *n = BUSNAME(u);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer BusNameResult f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(n);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(pid >= 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (pid != n->control_pid)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->control_pid = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (is_clean_exit(code, status, NULL))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = BUSNAME_SUCCESS;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (code == CLD_EXITED)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = BUSNAME_FAILURE_EXIT_CODE;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (code == CLD_KILLED)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = BUSNAME_FAILURE_SIGNAL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else if (code == CLD_DUMPED)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f = BUSNAME_FAILURE_CORE_DUMP;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert_not_reached("Unknown sigchld code");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_unit_full(u->id,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "%s control process exited, code=%s status=%i",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer u->id, sigchld_code_to_string(code), status);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (f != BUSNAME_SUCCESS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n->result = f;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer switch (n->state) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_MAKING:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (f == BUSNAME_SUCCESS)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_listening(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer else
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_signal(n, BUSNAME_SIGTERM, f);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_SIGTERM:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_SIGKILL:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_dead(n, f);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer default:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert_not_reached("Uh, control process died at wrong time.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Notify clients about changed exit status */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer unit_add_to_dbus_queue(u);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer BusName *n = BUSNAME(userdata);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(n->timer_event_source == source);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer switch (n->state) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_MAKING:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_unit_warning(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_SIGTERM:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_unit_warning(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer case BUSNAME_SIGKILL:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_unit_warning(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer default:
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert_not_reached("Timeout at wrong time.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic void busname_reset_failed(Unit *u) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer BusName *n = BUSNAME(u);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (n->state == BUSNAME_FAILED)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_set_state(n, BUSNAME_DEAD);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer n->result = BUSNAME_SUCCESS;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic void busname_trigger_notify(Unit *u, Unit *other) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer BusName *n = BUSNAME(u);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Service *s;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(other);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer s = SERVICE(other);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersen busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer else if (IN_SET(s->state,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer SERVICE_DEAD, SERVICE_FAILED,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer SERVICE_AUTO_RESTART))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer busname_enter_listening(n);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int busname_get_timeout(Unit *u, uint64_t *timeout) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer BusName *n = BUSNAME(u);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!n->timer_event_source)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_event_source_get_time(n->timer_event_source, timeout);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return 1;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_DEAD] = "dead",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_MAKING] = "making",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_REGISTERED] = "registered",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_LISTENING] = "listening",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_RUNNING] = "running",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_SIGTERM] = "sigterm",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_SIGKILL] = "sigkill",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILED] = "failed",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer};
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald HoyerDEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_SUCCESS] = "success",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_RESOURCES] = "resources",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_TIMEOUT] = "timeout",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_SIGNAL] = "signal",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer};
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald HoyerDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerconst UnitVTable busname_vtable = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .object_size = sizeof(BusName),
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .sections =
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer "Unit\0"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer "BusName\0"
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer "Install\0",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .private_section = "BusName",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .init = busname_init,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .done = busname_done,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .load = busname_load,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .coldplug = busname_coldplug,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .dump = busname_dump,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .start = busname_start,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .stop = busname_stop,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .kill = busname_kill,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .get_timeout = busname_get_timeout,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .serialize = busname_serialize,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .deserialize_item = busname_deserialize_item,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .active_state = busname_active_state,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .sub_state_to_string = busname_sub_state_to_string,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .sigchld_event = busname_sigchld_event,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .trigger_notify = busname_trigger_notify,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .reset_failed = busname_reset_failed,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .bus_interface = "org.freedesktop.systemd1.BusName",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .bus_vtable = bus_busname_vtable,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .status_message_formats = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .finished_start_job = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_DONE] = "Listening on %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_FAILED] = "Failed to listen on %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_DEPENDENCY] = "Dependency failed for %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_TIMEOUT] = "Timed out starting %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer },
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer .finished_stop_job = {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_DONE] = "Closed %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_FAILED] = "Failed stopping %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer [JOB_TIMEOUT] = "Timed out stopping %s.",
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer },
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer },
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer};
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer