busname.c revision db2cb23b5b179707000d28a11efb3d888d06ee80
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/***
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering This file is part of systemd.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Copyright 2013 Lennart Poettering
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is free software; you can redistribute it and/or modify it
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering under the terms of the GNU Lesser General Public License as published by
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (at your option) any later version.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is distributed in the hope that it will be useful, but
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Lesser General Public License for more details.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering You should have received a copy of the GNU Lesser General Public License
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering***/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "special.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-kernel.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-internal.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-util.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "service.h"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include "dbus-busname.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "busname.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen [BUSNAME_DEAD] = UNIT_INACTIVE,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen [BUSNAME_MAKING] = UNIT_ACTIVATING,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen [BUSNAME_REGISTERED] = UNIT_ACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_LISTENING] = UNIT_ACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_RUNNING] = UNIT_ACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILED] = UNIT_FAILED
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallacestatic void busname_init(Unit *u) {
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(u->load_state == UNIT_STUB);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = -1;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->accept_fd = true;
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer n->activating = true;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->timeout_usec = u->manager->default_timeout_start_usec;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_unwatch_control_pid(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->control_pid <= 0)
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering return;
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering unit_unwatch_pid(UNIT(n), n->control_pid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->control_pid = 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_free_policy(BusName *n) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers BusNamePolicy *p;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert(n);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers while ((p = n->policy)) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers LIST_REMOVE(policy, n->policy, p);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers free(p->name);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers free(p);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sieversstatic void busname_close_fd(BusName *n) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert(n);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers n->starter_event_source = sd_event_source_unref(n->starter_event_source);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers n->starter_fd = safe_close(n->starter_fd);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers}
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sieversstatic void busname_done(Unit *u) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers BusName *n = BUSNAME(u);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers assert(n);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers free(n->name);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers n->name = NULL;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers busname_free_policy(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers busname_unwatch_control_pid(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_close_fd(n);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_ref_unset(&n->service);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden n->timer_event_source = sd_event_source_unref(n->timer_event_source);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers}
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sieversstatic int busname_arm_timer(BusName *n) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering if (n->timeout_usec <= 0) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers n->timer_event_source = sd_event_source_unref(n->timer_event_source);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers return 0;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers }
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (n->timer_event_source) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (r < 0)
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden return r;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden }
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden return sd_event_add_time(
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden UNIT(n)->manager->event,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering &n->timer_event_source,
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden CLOCK_MONOTONIC,
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden now(CLOCK_MONOTONIC) + n->timeout_usec, 0,
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden busname_dispatch_timer, n);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden}
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_add_default_default_dependencies(BusName *n) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden int r;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden assert(n);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (r < 0)
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden return r;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
7f35b7bc4a241e9aa3b1512fd345cbf5b2e5a782Kay Sievers return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers}
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sieversstatic int busname_add_extras(BusName *n) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden Unit *u = UNIT(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering assert(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering if (!n->name) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->name = unit_name_to_prefix(u->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!n->name)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -ENOMEM;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers if (!u->description) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = unit_set_description(u, n->name);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers if (n->activating) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (!UNIT_DEREF(n->service)) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden Unit *x;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers r = unit_load_related_unit(u, ".service", &x);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers unit_ref_set(&n->service, x);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0)
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering return r;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering if (u->default_dependencies) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers r = busname_add_default_default_dependencies(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return 0;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers}
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int busname_verify(BusName *n) {
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering char *e;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(n);
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (UNIT(n)->load_state != UNIT_LOADED)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return 0;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (!service_name_is_valid(n->name)) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return -EINVAL;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
e9dd9f9547350c7dc0473583b5c2228dc8f0ab76Jason St. John e = strappenda(n->name, ".busname");
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!unit_has_name(UNIT(n), e)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -EINVAL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return 0;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic int busname_load(Unit *u) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers BusName *n = BUSNAME(u);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers int r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(u);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(u->load_state == UNIT_STUB);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers r = unit_load_fragment_and_dropin(u);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (r < 0)
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (u->load_state == UNIT_LOADED) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* This is a new unit? Then let's add in some extras */
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers r = busname_add_extras(n);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (r < 0)
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers return r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering return busname_verify(n);
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering}
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poetteringstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(n);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(f);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers fprintf(f,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering "%sBus Name State: %s\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering "%sResult: %s\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen "%sName: %s\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen "%sActivating: %s\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen "%sAccept FD: %s\n",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, busname_state_to_string(n->state),
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, busname_result_to_string(n->result),
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, n->name,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, yes_no(n->activating),
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, yes_no(n->accept_fd));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->control_pid > 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering "%sControl PID: "PID_FMT"\n",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering prefix, n->control_pid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_unwatch_fd(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (!n->starter_event_source)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (r < 0)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic int busname_watch_fd(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (n->starter_fd < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_event_source)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (r < 0) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen busname_unwatch_fd(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return r;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return 0;
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic int busname_open_fd(BusName *n) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen _cleanup_free_ char *path = NULL;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen const char *mode;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (n->starter_fd >= 0)
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "Failed to open %s: %s",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering path ?: "kdbus", strerror(-n->starter_fd));
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering return n->starter_fd;
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic void busname_set_state(BusName *n, BusNameState state) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen BusNameState old_state;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen old_state = n->state;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen n->state = state;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen n->timer_event_source = sd_event_source_unref(n->timer_event_source);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen busname_unwatch_control_pid(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (state != BUSNAME_LISTENING)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_unwatch_fd(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering busname_close_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (state != old_state)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering}
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_coldplug(Unit *u) {
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(n->state == BUSNAME_DEAD);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (n->deserialized_state == n->state)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return 0;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (n->control_pid <= 0)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return -EBADMSG;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = unit_watch_pid(UNIT(n), n->control_pid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_arm_timer(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
f75cb30bf97f623417cc7ee4b1bcc5c36cdbeb20Dave Reisner return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_open_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->deserialized_state == BUSNAME_LISTENING) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_watch_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, n->deserialized_state);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_make_starter(BusName *n, pid_t *_pid) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pid_t pid;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_arm_timer(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* We have to resolve the user/group names out-of-process,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * hence let's fork here. It's messy, but well, what can we
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * do? */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pid = fork();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (pid < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -errno;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (pid == 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int ret;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering ignore_signals(SIGPIPE, -1);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_forget_fds();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering ret = EXIT_MAKE_STARTER;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail_child;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _exit(0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fail_child:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_open();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to create starter connection at step %s: %s", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD), strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _exit(ret);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering r = unit_watch_pid(UNIT(n), pid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering *_pid = pid;
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringfail:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->timer_event_source = sd_event_source_unref(n->timer_event_source);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poetteringstatic void busname_enter_dead(BusName *n, BusNameResult f) {
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering assert(n);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (f != BUSNAME_SUCCESS)
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek n->result = f;
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek}
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek KillContext kill_context = {};
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek int r;
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek assert(n);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (f != BUSNAME_SUCCESS)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->result = f;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering kill_context_init(&kill_context);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_kill_context(UNIT(n),
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering &kill_context,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering -1,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->control_pid,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering false);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to kill control process: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
c978343015c787713651dff571acb5207367f5f2Lennart Poettering if (r > 0) {
c978343015c787713651dff571acb5207367f5f2Lennart Poettering r = busname_arm_timer(n);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering if (r < 0) {
c978343015c787713651dff571acb5207367f5f2Lennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to arm timer: %s", UNIT(n)->id, strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto fail;
c978343015c787713651dff571acb5207367f5f2Lennart Poettering }
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering busname_set_state(n, state);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else if (state == BUSNAME_SIGTERM)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_SUCCESS);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenfail:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_listening(BusName *n) {
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->activating) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_watch_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto fail;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, BUSNAME_LISTENING);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen } else
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen busname_set_state(n, BUSNAME_REGISTERED);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssenfail:
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen}
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic void busname_enter_making(BusName *n) {
c978343015c787713651dff571acb5207367f5f2Lennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_open_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->policy) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* If there is a policy, we need to resolve user/group
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * names, which we can't do from PID1, hence let's
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering * fork. */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_unwatch_control_pid(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_make_starter(n, &n->control_pid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to fork 'making' task: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, BUSNAME_MAKING);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* If there is no policy, we can do everything
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * directly from PID 1, hence do so. */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to make starter: %s", UNIT(n)->id, strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_listening(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringfail:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_running(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bool pending = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Unit *other;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Iterator i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!n->activating)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* We don't take conenctions anymore if we are supposed to
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * shut down anyway */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (unit_stop_pending(UNIT(n))) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* Flush all queued activation reqeuest by closing and reopening the connection */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bus_kernel_drop_one(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_listening(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* If there's already a start pending don't bother to do
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * anything */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (unit_active_or_pending(other)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pending = true;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!pending) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, BUSNAME_RUNNING);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringfail:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_start(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* We cannot fulfill this request right now, try again later
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * please! */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -EAGAIN;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* Already on it! */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->state == BUSNAME_MAKING)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return 0;
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->activating && UNIT_ISSET(n->service)) {
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner Service *service;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering service = SERVICE(UNIT_DEREF(n->service));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -ENOENT;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->result = BUSNAME_SUCCESS;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_making(n);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_stop(Unit *u) {
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
assert(n);
/* Already on it */
if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
return 0;
/* If there's already something running, we go directly into
* kill mode. */
if (n->state == BUSNAME_MAKING) {
busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS);
return -EAGAIN;
}
assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
busname_enter_dead(n, BUSNAME_SUCCESS);
return 0;
}
static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
BusName *n = BUSNAME(u);
assert(n);
assert(f);
assert(fds);
unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
if (n->control_pid > 0)
unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
if (n->starter_fd >= 0) {
int copy;
copy = fdset_put_dup(fds, n->starter_fd);
if (copy < 0)
return copy;
unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
}
return 0;
}
static int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
BusName *n = BUSNAME(u);
assert(n);
assert(key);
assert(value);
if (streq(key, "state")) {
BusNameState state;
state = busname_state_from_string(value);
if (state < 0)
log_debug_unit(u->id, "Failed to parse state value %s", value);
else
n->deserialized_state = state;
} else if (streq(key, "result")) {
BusNameResult f;
f = busname_result_from_string(value);
if (f < 0)
log_debug_unit(u->id, "Failed to parse result value %s", value);
else if (f != BUSNAME_SUCCESS)
n->result = f;
} else if (streq(key, "control-pid")) {
pid_t pid;
if (parse_pid(value, &pid) < 0)
log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
else
n->control_pid = pid;
} else if (streq(key, "starter-fd")) {
int fd;
if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
else {
safe_close(n->starter_fd);
n->starter_fd = fdset_remove(fds, fd);
}
} else
log_debug_unit(u->id, "Unknown serialization key '%s'", key);
return 0;
}
_pure_ static UnitActiveState busname_active_state(Unit *u) {
assert(u);
return state_translation_table[BUSNAME(u)->state];
}
_pure_ static const char *busname_sub_state_to_string(Unit *u) {
assert(u);
return busname_state_to_string(BUSNAME(u)->state);
}
static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
BusName *n = userdata;
assert(n);
assert(fd >= 0);
if (n->state != BUSNAME_LISTENING)
return 0;
log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
if (revents != EPOLLIN) {
log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
UNIT(n)->id, revents);
goto fail;
}
busname_enter_running(n);
return 0;
fail:
busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
return 0;
}
static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
BusName *n = BUSNAME(u);
BusNameResult f;
assert(n);
assert(pid >= 0);
if (pid != n->control_pid)
return;
n->control_pid = 0;
if (is_clean_exit(code, status, NULL))
f = BUSNAME_SUCCESS;
else if (code == CLD_EXITED)
f = BUSNAME_FAILURE_EXIT_CODE;
else if (code == CLD_KILLED)
f = BUSNAME_FAILURE_SIGNAL;
else if (code == CLD_DUMPED)
f = BUSNAME_FAILURE_CORE_DUMP;
else
assert_not_reached("Unknown sigchld code");
log_full_unit(f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
u->id, "%s control process exited, code=%s status=%i",
u->id, sigchld_code_to_string(code), status);
if (f != BUSNAME_SUCCESS)
n->result = f;
switch (n->state) {
case BUSNAME_MAKING:
if (f == BUSNAME_SUCCESS)
busname_enter_listening(n);
else
busname_enter_signal(n, BUSNAME_SIGTERM, f);
break;
case BUSNAME_SIGTERM:
case BUSNAME_SIGKILL:
busname_enter_dead(n, f);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
/* Notify clients about changed exit status */
unit_add_to_dbus_queue(u);
}
static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
BusName *n = BUSNAME(userdata);
assert(n);
assert(n->timer_event_source == source);
switch (n->state) {
case BUSNAME_MAKING:
log_warning_unit(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id);
busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
break;
case BUSNAME_SIGTERM:
log_warning_unit(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id);
busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
break;
case BUSNAME_SIGKILL:
log_warning_unit(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id);
busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
break;
default:
assert_not_reached("Timeout at wrong time.");
}
return 0;
}
static void busname_reset_failed(Unit *u) {
BusName *n = BUSNAME(u);
assert(n);
if (n->state == BUSNAME_FAILED)
busname_set_state(n, BUSNAME_DEAD);
n->result = BUSNAME_SUCCESS;
}
static void busname_trigger_notify(Unit *u, Unit *other) {
BusName *n = BUSNAME(u);
Service *s;
assert(n);
assert(other);
if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
return;
if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
return;
s = SERVICE(other);
if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
else if (IN_SET(s->state,
SERVICE_DEAD, SERVICE_FAILED,
SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
SERVICE_AUTO_RESTART))
busname_enter_listening(n);
}
static int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
}
static int busname_get_timeout(Unit *u, uint64_t *timeout) {
BusName *n = BUSNAME(u);
int r;
if (!n->timer_event_source)
return 0;
r = sd_event_source_get_time(n->timer_event_source, timeout);
if (r < 0)
return r;
return 1;
}
static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
[BUSNAME_DEAD] = "dead",
[BUSNAME_MAKING] = "making",
[BUSNAME_REGISTERED] = "registered",
[BUSNAME_LISTENING] = "listening",
[BUSNAME_RUNNING] = "running",
[BUSNAME_SIGTERM] = "sigterm",
[BUSNAME_SIGKILL] = "sigkill",
[BUSNAME_FAILED] = "failed",
};
DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
[BUSNAME_SUCCESS] = "success",
[BUSNAME_FAILURE_RESOURCES] = "resources",
[BUSNAME_FAILURE_TIMEOUT] = "timeout",
[BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
[BUSNAME_FAILURE_SIGNAL] = "signal",
[BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
[BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
};
DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
const UnitVTable busname_vtable = {
.object_size = sizeof(BusName),
.sections =
"Unit\0"
"BusName\0"
"Install\0",
.private_section = "BusName",
.init = busname_init,
.done = busname_done,
.load = busname_load,
.coldplug = busname_coldplug,
.dump = busname_dump,
.start = busname_start,
.stop = busname_stop,
.kill = busname_kill,
.get_timeout = busname_get_timeout,
.serialize = busname_serialize,
.deserialize_item = busname_deserialize_item,
.active_state = busname_active_state,
.sub_state_to_string = busname_sub_state_to_string,
.sigchld_event = busname_sigchld_event,
.trigger_notify = busname_trigger_notify,
.reset_failed = busname_reset_failed,
.bus_interface = "org.freedesktop.systemd1.BusName",
.bus_vtable = bus_busname_vtable,
.status_message_formats = {
.finished_start_job = {
[JOB_DONE] = "Listening on %s.",
[JOB_FAILED] = "Failed to listen on %s.",
[JOB_DEPENDENCY] = "Dependency failed for %s.",
[JOB_TIMEOUT] = "Timed out starting %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Closed %s.",
[JOB_FAILED] = "Failed stopping %s.",
[JOB_TIMEOUT] = "Timed out stopping %s.",
},
},
};