e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/***
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering***/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering#include <sys/mman.h>
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "bus-internal.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "bus-kernel.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "bus-policy.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "bus-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "busname.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "dbus-busname.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fd-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "formats-util.h"
3c70e3bb022f0de3317f3600c9366a2f4597339eLennart Poettering#include "kdbus.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include "parse-util.h"
7b3e062cb6872e28ef5a2e069810070e28bbe0cdLennart Poettering#include "process-util.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "service.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "signal-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "special.h"
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#include "string-table.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_DEAD] = UNIT_INACTIVE,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_MAKING] = UNIT_ACTIVATING,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack [BUSNAME_REGISTERED] = UNIT_ACTIVE,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_LISTENING] = UNIT_ACTIVE,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_RUNNING] = UNIT_ACTIVE,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_FAILED] = UNIT_FAILED
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_init(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u->load_state == UNIT_STUB);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->starter_fd = -1;
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering n->accept_fd = true;
4f10118016f9b2fd7e1d26c9ef7d91eb33fba694Lennart Poettering n->activating = true;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->timeout_usec = u->manager->default_timeout_start_usec;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_unwatch_control_pid(BusName *n) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->control_pid <= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unit_unwatch_pid(UNIT(n), n->control_pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->control_pid = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_free_policy(BusName *n) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering BusNamePolicy *p;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering while ((p = n->policy)) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_REMOVE(policy, n->policy, p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(p->name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_close_fd(BusName *n) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->starter_event_source = sd_event_source_unref(n->starter_event_source);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->starter_fd = safe_close(n->starter_fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_done(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a1e58e8ee1c84b633d6d6d651d5328d4dd4eba5bLennart Poettering n->name = mfree(n->name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_free_policy(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_unwatch_control_pid(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_close_fd(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unit_ref_unset(&n->service);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->timer_event_source = sd_event_source_unref(n->timer_event_source);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poetteringstatic int busname_arm_timer(BusName *n, usec_t usec) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->timer_event_source) {
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering r = sd_event_source_set_time(n->timer_event_source, usec);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering if (usec == USEC_INFINITY)
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering return 0;
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersen r = sd_event_add_time(
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering UNIT(n)->manager->event,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering &n->timer_event_source,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering CLOCK_MONOTONIC,
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering usec, 0,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_dispatch_timer, n);
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen if (r < 0)
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen return r;
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen (void) sd_event_source_set_description(n->timer_event_source, "busname-timer");
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_add_default_default_dependencies(BusName *n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering if (UNIT(n)->manager->running_as == MANAGER_SYSTEM) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_add_extras(BusName *n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Unit *u = UNIT(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!n->name) {
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering r = unit_name_to_prefix(u->id, &n->name);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if (r < 0)
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!u->description) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = unit_set_description(u, n->name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (n->activating) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (!UNIT_DEREF(n->service)) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack Unit *x;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack r = unit_load_related_unit(u, ".service", &x);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (r < 0)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack return r;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack unit_ref_set(&n->service, x);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack }
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (u->default_dependencies) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = busname_add_default_default_dependencies(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_verify(BusName *n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char *e;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (UNIT(n)->load_state != UNIT_LOADED)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!service_name_is_valid(n->name)) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_error(UNIT(n), "Name= setting is not a valid service name Refusing.");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -EINVAL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering e = strjoina(n->name, ".busname");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!unit_has_name(UNIT(n), e)) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_error(UNIT(n), "Name= setting doesn't match unit name. Refusing.");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -EINVAL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_load(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u->load_state == UNIT_STUB);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = unit_load_fragment_and_dropin(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (u->load_state == UNIT_LOADED) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* This is a new unit? Then let's add in some extras */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = busname_add_extras(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return busname_verify(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(f);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "%sBus Name State: %s\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "%sResult: %s\n"
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "%sName: %s\n"
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "%sActivating: %s\n"
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "%sAccept FD: %s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering prefix, busname_state_to_string(n->state),
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering prefix, busname_result_to_string(n->result),
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering prefix, n->name,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack prefix, yes_no(n->activating),
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering prefix, yes_no(n->accept_fd));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->control_pid > 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering fprintf(f,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering "%sControl PID: "PID_FMT"\n",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering prefix, n->control_pid);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_unwatch_fd(BusName *n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!n->starter_event_source)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug_errno(UNIT(n), r, "Failed to disable event source: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_watch_fd(BusName *n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->starter_fd < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering if (n->starter_event_source) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering if (r < 0)
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering goto fail;
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering } else {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering if (r < 0)
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering goto fail;
cfa9677bd164574600d29a9bf99f9d1f28a7a170Mantas MikulÄ—nas
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering (void) sd_event_source_set_description(n->starter_event_source, "busname-starter");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringfail:
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to watch starter fd: %m");
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering busname_unwatch_fd(n);
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_open_fd(BusName *n) {
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek _cleanup_free_ char *path = NULL;
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek const char *mode;
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->starter_fd >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering mode = UNIT(n)->manager->running_as == MANAGER_SYSTEM ? "system" : "user";
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt if (n->starter_fd < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering return log_unit_warning_errno(UNIT(n), n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_set_state(BusName *n, BusNameState state) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusNameState old_state;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering old_state = n->state;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->state = state;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->timer_event_source = sd_event_source_unref(n->timer_event_source);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_unwatch_control_pid(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (state != BUSNAME_LISTENING)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_unwatch_fd(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_close_fd(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (state != old_state)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(UNIT(n), "Changed %s -> %s", busname_state_to_string(old_state), busname_state_to_string(state));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
be847e82cf95bf8eb589778df2aa2b3d1d7ae99eLennart Poetteringstatic int busname_coldplug(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n->state == BUSNAME_DEAD);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->deserialized_state == n->state)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
c386f5886466de6022b3b4b1c8ac8df72871fbc7Lennart Poettering if (n->control_pid > 0 &&
c386f5886466de6022b3b4b1c8ac8df72871fbc7Lennart Poettering pid_is_unwaited(n->control_pid) &&
c386f5886466de6022b3b4b1c8ac8df72871fbc7Lennart Poettering IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = unit_watch_pid(UNIT(n), n->control_pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering r = busname_arm_timer(n, usec_add(u->state_change_timestamp.monotonic, n->timeout_usec));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = busname_open_fd(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->deserialized_state == BUSNAME_LISTENING) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = busname_watch_fd(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_set_state(n, n->deserialized_state);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int busname_make_starter(BusName *n, pid_t *_pid) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering pid_t pid;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering r = busname_arm_timer(n, usec_add(now(CLOCK_MONOTONIC), n->timeout_usec));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* We have to resolve the user/group names out-of-process,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * hence let's fork here. It's messy, but well, what can we
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * do? */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering pid = fork();
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (pid < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -errno;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (pid == 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int ret;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering (void) ignore_signals(SIGPIPE, -1);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_forget_fds();
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering ret = EXIT_MAKE_STARTER;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail_child;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _exit(0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering fail_child:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_open();
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _exit(ret);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = unit_watch_pid(UNIT(n), pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering *_pid = pid;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringfail:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->timer_event_source = sd_event_source_unref(n->timer_event_source);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_enter_dead(BusName *n, BusNameResult f) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (f != BUSNAME_SUCCESS)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->result = f;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering KillContext kill_context = {};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (f != BUSNAME_SUCCESS)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->result = f;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering kill_context_init(&kill_context);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = unit_kill_context(UNIT(n),
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering &kill_context,
db2cb23b5b179707000d28a11efb3d888d06ee80Umut Tezduyar Lindskog state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering -1,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->control_pid,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering false);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to kill control process: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering goto fail;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r > 0) {
36c16a7cdd6c33d7980efc2cd6a2211941f302b4Lennart Poettering r = busname_arm_timer(n, usec_add(now(CLOCK_MONOTONIC), n->timeout_usec));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to arm timer: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_set_state(n, state);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else if (state == BUSNAME_SIGTERM)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_dead(n, BUSNAME_SUCCESS);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringfail:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_enter_listening(BusName *n) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (n->activating) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack r = busname_watch_fd(n);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (r < 0) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to watch names: %m");
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack goto fail;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack }
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack busname_set_state(n, BUSNAME_LISTENING);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack } else
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack busname_set_state(n, BUSNAME_REGISTERED);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringfail:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_enter_making(BusName *n) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = busname_open_fd(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->policy) {
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt /* If there is a policy, we need to resolve user/group
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * names, which we can't do from PID1, hence let's
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * fork. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_unwatch_control_pid(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = busname_make_starter(n, &n->control_pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to fork 'making' task: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_set_state(n, BUSNAME_MAKING);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else {
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt /* If there is no policy, we can do everything
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * directly from PID 1, hence do so. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning_errno(UNIT(n), r, "Failed to make starter: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto fail;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_listening(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringfail:
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_enter_running(BusName *n) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering bool pending = false;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Unit *other;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Iterator i;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (!n->activating)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack return;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
05a08cb60f02970e8476306074c70ee4e6a57fb3Thomas Hindoe Paaboel Andersen /* We don't take connections anymore if we are supposed to
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * shut down anyway */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (unit_stop_pending(UNIT(n))) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(UNIT(n), "Suppressing activation request since unit stop is scheduled.");
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering /* Flush all queued activation reqeuest by closing and reopening the connection */
ff975efb2e88dcd5221a2f0d76c4c87e85b821a8Lennart Poettering bus_kernel_drop_one(n->starter_fd);
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering busname_enter_listening(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* If there's already a start pending don't bother to do
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * anything */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (unit_active_or_pending(other)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering pending = true;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering break;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!pending) {
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering if (!UNIT_ISSET(n->service)) {
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering log_unit_error(UNIT(n), "Service to activate vanished, refusing activation.");
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering r = -ENOENT;
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering goto fail;
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering }
995c5e96cdf9e6054a4df04960253d897f4d7cc2Lennart Poettering
4bd29fe5cec9d744a4e39240c76b85d999bd2cf7Lennart Poettering r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, &error, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering goto fail;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_set_state(n, BUSNAME_RUNNING);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringfail:
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning(UNIT(n), "Failed to queue service startup job: %s", bus_error_message(&error, r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_start(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* We cannot fulfill this request right now, try again later
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * please! */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -EAGAIN;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Already on it! */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->state == BUSNAME_MAKING)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (n->activating && UNIT_ISSET(n->service)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Service *service;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering service = SERVICE(UNIT_DEREF(n->service));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_error(u, "Bus service %s not loaded, refusing.", UNIT(service)->id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOENT;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->result = BUSNAME_SUCCESS;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_making(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
82a2b6bb5e4e5d294f09af778c48974a7857afb6Lennart Poettering return 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_stop(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Already on it */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* If there's already something running, we go directly into
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * kill mode. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->state == BUSNAME_MAKING) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -EAGAIN;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_dead(n, BUSNAME_SUCCESS);
82a2b6bb5e4e5d294f09af778c48974a7857afb6Lennart Poettering return 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
a34ceba66fc0e856d8f76f340389a4768b57a365Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(f);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(fds);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (n->control_pid > 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a34ceba66fc0e856d8f76f340389a4768b57a365Lennart Poettering r = unit_serialize_item_fd(u, f, fds, "starter-fd", n->starter_fd);
a34ceba66fc0e856d8f76f340389a4768b57a365Lennart Poettering if (r < 0)
a34ceba66fc0e856d8f76f340389a4768b57a365Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(key);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (streq(key, "state")) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusNameState state;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering state = busname_state_from_string(value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (state < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(u, "Failed to parse state value: %s", value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->deserialized_state = state;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else if (streq(key, "result")) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusNameResult f;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering f = busname_result_from_string(value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (f < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(u, "Failed to parse result value: %s", value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else if (f != BUSNAME_SUCCESS)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->result = f;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else if (streq(key, "control-pid")) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering pid_t pid;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (parse_pid(value, &pid) < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(u, "Failed to parse control-pid value: %s", value);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->control_pid = pid;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else if (streq(key, "starter-fd")) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int fd;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(u, "Failed to parse starter fd value: %s", value);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else {
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering safe_close(n->starter_fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->starter_fd = fdset_remove(fds, fd);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(u, "Unknown serialization key: %s", key);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering_pure_ static UnitActiveState busname_active_state(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return state_translation_table[BUSNAME(u)->state];
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering_pure_ static const char *busname_sub_state_to_string(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return busname_state_to_string(BUSNAME(u)->state);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poetteringstatic int busname_peek_message(BusName *n) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering struct kdbus_cmd_recv cmd_recv = {
94e15fdc4d9d96fa6607bfb4eaaea164a3aec417David Herrmann .size = sizeof(cmd_recv),
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering .flags = KDBUS_RECV_PEEK,
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering };
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack struct kdbus_cmd_free cmd_free = {
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack .size = sizeof(cmd_free),
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack };
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering const char *comm = NULL;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering struct kdbus_item *d;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering struct kdbus_msg *k;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering size_t start, ps, sz, delta;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering void *p = NULL;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering pid_t pid = 0;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering int r;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering /* Generate a friendly debug log message about which process
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering * caused triggering of this bus name. This simply peeks the
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering * metadata of the first queued message and logs it. */
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering assert(n);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering /* Let's shortcut things a bit, if debug logging is turned off
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering * anyway. */
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if (log_get_max_level() < LOG_DEBUG)
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering return 0;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
94e15fdc4d9d96fa6607bfb4eaaea164a3aec417David Herrmann r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (r < 0) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (errno == EINTR || errno == EAGAIN)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return 0;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering return log_unit_error_errno(UNIT(n), errno, "Failed to query activation message: %m");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering /* We map as late as possible, and unmap imemdiately after
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * use. On 32bit address space is scarce and we want to be
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * able to handle a lot of activator connections at the same
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * time, and hence shouldn't keep the mmap()s around for
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering * longer than necessary. */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering ps = page_size();
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack start = (cmd_recv.msg.offset / ps) * ps;
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack delta = cmd_recv.msg.offset - start;
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (p == MAP_FAILED) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering r = log_unit_error_errno(UNIT(n), errno, "Failed to map activation message: %m");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering goto finish;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering k = (struct kdbus_msg *) ((uint8_t *) p + delta);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering KDBUS_ITEM_FOREACH(d, k, items) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering switch (d->type) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering case KDBUS_ITEM_PIDS:
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering pid = d->pids.pid;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering break;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering case KDBUS_ITEM_PID_COMM:
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering comm = d->str;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering break;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (pid > 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(UNIT(n), "Activation triggered by process " PID_FMT " (%s)", pid, strna(comm));
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering r = 0;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poetteringfinish:
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (p)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering (void) munmap(p, sz);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack cmd_free.offset = cmd_recv.msg.offset;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning(UNIT(n), "Failed to free peeked message, ignoring: %m");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return r;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering}
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = userdata;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(fd >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->state != BUSNAME_LISTENING)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_debug(UNIT(n), "Activation request");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (revents != EPOLLIN) {
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_error(UNIT(n), "Got unexpected poll event (0x%x) on starter fd.", revents);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering goto fail;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering busname_peek_message(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_running(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringfail:
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering BusName *n = BUSNAME(u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering BusNameResult f;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(pid >= 0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (pid != n->control_pid)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->control_pid = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (is_clean_exit(code, status, NULL))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering f = BUSNAME_SUCCESS;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else if (code == CLD_EXITED)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering f = BUSNAME_FAILURE_EXIT_CODE;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else if (code == CLD_KILLED)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering f = BUSNAME_FAILURE_SIGNAL;
deffddf1df29a5ed047feff3a0f2b765006fb71bLukas Nykryn else if (code == CLD_DUMPED)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering f = BUSNAME_FAILURE_CORE_DUMP;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_not_reached("Unknown sigchld code");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_full(u, f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering "Control process exited, code=%s status=%i", sigchld_code_to_string(code), status);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (f != BUSNAME_SUCCESS)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering n->result = f;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering switch (n->state) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_MAKING:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (f == BUSNAME_SUCCESS)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_listening(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGTERM, f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering break;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_SIGTERM:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_SIGKILL:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_dead(n, f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering break;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering default:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_not_reached("Uh, control process died at wrong time.");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Notify clients about changed exit status */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unit_add_to_dbus_queue(u);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering BusName *n = BUSNAME(userdata);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(n->timer_event_source == source);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering switch (n->state) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_MAKING:
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning(UNIT(n), "Making timed out. Terminating.");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering break;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_SIGTERM:
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning(UNIT(n), "Stopping timed out. Killing.");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering break;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering case BUSNAME_SIGKILL:
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering log_unit_warning(UNIT(n), "Processes still around after SIGKILL. Ignoring.");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering break;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering default:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_not_reached("Timeout at wrong time.");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_reset_failed(Unit *u) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (n->state == BUSNAME_FAILED)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_set_state(n, BUSNAME_DEAD);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering n->result = BUSNAME_SUCCESS;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void busname_trigger_notify(Unit *u, Unit *other) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering BusName *n = BUSNAME(u);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(n);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(other);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering if (other->start_limit_hit) {
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return;
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering return;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering if (IN_SET(SERVICE(other)->state,
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering SERVICE_DEAD, SERVICE_FAILED,
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering SERVICE_AUTO_RESTART))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering busname_enter_listening(n);
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering if (SERVICE(other)->state == SERVICE_RUNNING)
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering busname_set_state(n, BUSNAME_RUNNING);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poetteringstatic int busname_get_timeout(Unit *u, usec_t *timeout) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering BusName *n = BUSNAME(u);
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poettering usec_t t;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!n->timer_event_source)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poettering r = sd_event_source_get_time(n->timer_event_source, &t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poettering if (t == USEC_INFINITY)
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
7a7821c878a6ddfb2e79268bb1cd8f7662a9b8f8Lennart Poettering *timeout = t;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
1c2e9646e4a1720fc8ad35c705c195ae1a2c5ce0Lennart Poetteringstatic bool busname_supported(void) {
d69a7ceaed77992105342a056fe68769c2e6d3bdJohannes Hölzl static int supported = -1;
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering if (supported < 0)
d79acc309327f8c0863bd3da8b93d926a1c93ba1David Herrmann supported = is_kdbus_available();
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering return supported;
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering}
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_SUCCESS] = "success",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [BUSNAME_FAILURE_RESOURCES] = "resources",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_FAILURE_TIMEOUT] = "timeout",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_FAILURE_SIGNAL] = "signal",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
6bf0f408e4833152197fb38fb10a9989c89f3a59Lennart Poettering [BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringconst UnitVTable busname_vtable = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .object_size = sizeof(BusName),
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .sections =
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "Unit\0"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "BusName\0"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "Install\0",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .private_section = "BusName",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e05ad7bcc87f652cea321224f8eeb32e21d18e0cLennart Poettering .no_alias = true,
e05ad7bcc87f652cea321224f8eeb32e21d18e0cLennart Poettering .no_instances = true,
e05ad7bcc87f652cea321224f8eeb32e21d18e0cLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .init = busname_init,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .done = busname_done,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .load = busname_load,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .coldplug = busname_coldplug,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .dump = busname_dump,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .start = busname_start,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .stop = busname_stop,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering .kill = busname_kill,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering .get_timeout = busname_get_timeout,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .serialize = busname_serialize,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .deserialize_item = busname_deserialize_item,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .active_state = busname_active_state,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .sub_state_to_string = busname_sub_state_to_string,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering .sigchld_event = busname_sigchld_event,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .trigger_notify = busname_trigger_notify,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .reset_failed = busname_reset_failed,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering .supported = busname_supported,
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .bus_vtable = bus_busname_vtable,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .status_message_formats = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .finished_start_job = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [JOB_DONE] = "Listening on %s.",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [JOB_FAILED] = "Failed to listen on %s.",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering .finished_stop_job = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [JOB_DONE] = "Closed %s.",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [JOB_FAILED] = "Failed stopping %s.",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering },
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering};