busname.c revision 3f9da416457c4265b8f1179516a32ad1a987ff7d
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
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
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"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include "service.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "dbus-busname.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "busname.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_DEAD] = UNIT_INACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_LISTENING] = UNIT_ACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_RUNNING] = UNIT_ACTIVE,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILED] = UNIT_FAILED
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic void busname_init(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart 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;
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace}
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_done(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering free(n->name);
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer n->name = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_ref_unset(&n->service);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->event_source = sd_event_source_unref(n->event_source);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = safe_close(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_add_default_default_dependencies(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_add_extras(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Unit *u = UNIT(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (!n->name) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers n->name = unit_name_to_prefix(u->id);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (!n->name)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers return -ENOMEM;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers }
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (!u->description) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = unit_set_description(u, n->name);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (r < 0)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers return r;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers }
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (!UNIT_DEREF(n->service)) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers Unit *x;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = unit_load_related_unit(u, ".service", &x);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_ref_set(&n->service, x);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (u->default_dependencies) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_add_default_default_dependencies(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering return r;
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering }
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers return 0;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers}
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sieversstatic int busname_verify(BusName *n) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers char *e;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (UNIT(n)->load_state != UNIT_LOADED)
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering return 0;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (!service_name_is_valid(n->name)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return -EINVAL;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers 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
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int busname_load(Unit *u) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(u);
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering assert(u->load_state == UNIT_STUB);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering r = unit_load_fragment_and_dropin(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (u->load_state == UNIT_LOADED) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers /* This is a new unit? Then let's add in some extras */
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering r = busname_add_extras(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return busname_verify(n);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers}
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sieversstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers BusName *n = BUSNAME(u);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(f);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering fprintf(f,
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sBus Name State: %s\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sResult: %s\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sName: %s\n"
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering "%sAccept FD: %s\n",
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers prefix, busname_state_to_string(n->state),
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering prefix, busname_result_to_string(n->result),
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers prefix, n->name,
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers prefix, yes_no(n->accept_fd));
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering}
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic void busname_unwatch_fd(BusName *n) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers int r;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (n->event_source) {
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0)
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers}
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic void busname_close_fd(BusName *n) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers assert(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers busname_unwatch_fd(n);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd <= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = safe_close(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_watch_fd(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->event_source)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_add_io(UNIT(n)->manager->event, &n->event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_unwatch_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_open_fd(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd >= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering n->starter_fd = bus_kernel_create_starter(
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->name, n->accept_fd, n->policy);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return n->starter_fd;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_set_state(BusName *n, BusNameState state) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek BusNameState old_state;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering old_state = n->state;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->state = state;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (state != BUSNAME_LISTENING)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_unwatch_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart 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);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_coldplug(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n->state == BUSNAME_DEAD);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->deserialized_state == n->state)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, 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 void busname_enter_dead(BusName *n, BusNameResult f) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (f != BUSNAME_SUCCESS)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->result = f;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_listening(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart 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 log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_watch_fd(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto fail;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, BUSNAME_LISTENING);
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 /* 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);
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen 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));
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen 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 if (UNIT_ISSET(n->service)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Service *service;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering service = SERVICE(UNIT_DEREF(n->service));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering return -ENOENT;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->result = BUSNAME_SUCCESS;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_listening(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_stop(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen busname_enter_dead(n, BUSNAME_SUCCESS);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(fds);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->starter_fd >= 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int copy;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering copy = fdset_put_dup(fds, n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (copy < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return copy;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = BUSNAME(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(key);
f75cb30bf97f623417cc7ee4b1bcc5c36cdbeb20Dave Reisner assert(value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (streq(key, "state")) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusNameState state;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering state = busname_state_from_string(value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (state < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse state value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->deserialized_state = state;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else if (streq(key, "result")) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusNameResult f;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering f = busname_result_from_string(value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (f < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse result value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else if (f != BUSNAME_SUCCESS)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->result = f;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else if (streq(key, "starter-fd")) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int fd;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering safe_close(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = fdset_remove(fds, fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Unknown serialization key '%s'", key);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering_pure_ static UnitActiveState busname_active_state(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return state_translation_table[BUSNAME(u)->state];
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering_pure_ static const char *busname_sub_state_to_string(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(u);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return busname_state_to_string(BUSNAME(u)->state);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering BusName *n = userdata;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(fd >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->state != BUSNAME_LISTENING)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (revents != EPOLLIN) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering UNIT(n)->id, revents);
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering goto fail;
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_running(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringfail:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering return 0;
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_reset_failed(Unit *u) {
c978343015c787713651dff571acb5207367f5f2Lennart Poettering BusName *n = BUSNAME(u);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
2927b326ccf67236e148444679f582ea1437ef5aJan Janssen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->state == BUSNAME_FAILED)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, BUSNAME_DEAD);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering n->result = BUSNAME_SUCCESS;
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering}
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poetteringstatic void busname_trigger_notify(Unit *u, Unit *other) {
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering BusName *n = BUSNAME(u);
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering Service *s;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(other);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
c978343015c787713651dff571acb5207367f5f2Lennart Poettering s = SERVICE(other);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else if (IN_SET(s->state,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_DEAD, SERVICE_FAILED,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_AUTO_RESTART))
c978343015c787713651dff571acb5207367f5f2Lennart Poettering busname_enter_listening(n);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering}
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_DEAD] = "dead",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_LISTENING] = "listening",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_RUNNING] = "running",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILED] = "failed"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_SUCCESS] = "success",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILURE_RESOURCES] = "resources",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const char* const busname_policy_access_table[_BUSNAME_POLICY_ACCESS_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_POLICY_ACCESS_SEE] = "see",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_POLICY_ACCESS_TALK] = "talk",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_POLICY_ACCESS_OWN] = "own",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_policy_access, BusNamePolicyAccess);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringconst UnitVTable busname_vtable = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .object_size = sizeof(BusName),
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .sections =
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering "Unit\0"
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen "BusName\0"
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen "Install\0",
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen .private_section = "BusName",
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen
c978343015c787713651dff571acb5207367f5f2Lennart Poettering .init = busname_init,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering .done = busname_done,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .load = busname_load,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .coldplug = busname_coldplug,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .dump = busname_dump,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .start = busname_start,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .stop = busname_stop,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .serialize = busname_serialize,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .deserialize_item = busname_deserialize_item,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .active_state = busname_active_state,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .sub_state_to_string = busname_sub_state_to_string,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .trigger_notify = busname_trigger_notify,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .reset_failed = busname_reset_failed,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .bus_interface = "org.freedesktop.systemd1.BusName",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .bus_vtable = bus_busname_vtable,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .status_message_formats = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .finished_start_job = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_DONE] = "Listening on %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_FAILED] = "Failed to listen on %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_DEPENDENCY] = "Dependency failed for %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_TIMEOUT] = "Timed out starting %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .finished_stop_job = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_DONE] = "Closed %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_FAILED] = "Failed stopping %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_TIMEOUT] = "Timed out stopping %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering