busname.c revision ff975efb2e88dcd5221a2f0d76c4c87e85b821a8
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt/***
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt This file is part of systemd.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt Copyright 2013 Lennart Poettering
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt systemd is free software; you can redistribute it and/or modify it
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt under the terms of the GNU Lesser General Public License as published by
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt the Free Software Foundation; either version 2.1 of the License, or
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt (at your option) any later version.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt systemd is distributed in the hope that it will be useful, but
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt WITHOUT ANY WARRANTY; without even the implied warranty of
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt Lesser General Public License for more details.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt You should have received a copy of the GNU Lesser General Public License
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt along with systemd; If not, see <http://www.gnu.org/licenses/>.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt***/
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include "special.h"
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include "bus-kernel.h"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include "bus-internal.h"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include "bus-util.h"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include "service.h"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt#include "dbus-busname.h"
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt#include "busname.h"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt [BUSNAME_DEAD] = UNIT_INACTIVE,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt [BUSNAME_LISTENING] = UNIT_ACTIVE,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_RUNNING] = UNIT_ACTIVE,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt [BUSNAME_FAILED] = UNIT_FAILED
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt};
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic void busname_init(Unit *u) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt BusName *n = BUSNAME(u);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert(u);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert(u->load_state == UNIT_STUB);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt n->starter_fd = -1;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic void busname_done(Unit *u) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(u);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt free(n->name);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt n->name = NULL;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt unit_ref_unset(&n->service);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt n->event_source = sd_event_source_unref(n->event_source);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt if (n->starter_fd >= 0) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt close_nointr_nofail(n->starter_fd);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt n->starter_fd = -1;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt }
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt}
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic int busname_add_default_default_dependencies(BusName *n) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt int r;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt assert(n);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt if (r < 0)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt return r;
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt if (r < 0)
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt return r;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt }
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt}
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic int busname_add_extras(BusName *n) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt Unit *u = UNIT(n);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt int r;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert(n);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!n->name) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt n->name = unit_name_to_prefix(u->id);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!n->name)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return -ENOMEM;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!u->description) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt r = unit_set_description(u, n->name);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (r < 0)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return r;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!UNIT_DEREF(n->service)) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt Unit *x;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt r = unit_load_related_unit(u, ".service", &x);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (r < 0)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return r;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt unit_ref_set(&n->service, x);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (r < 0)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return r;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (u->default_dependencies) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt r = busname_add_default_default_dependencies(n);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (r < 0)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return r;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return 0;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt}
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic int busname_verify(BusName *n) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt char *e;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt assert(n);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (UNIT(n)->load_state != UNIT_LOADED)
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return 0;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!service_name_is_valid(n->name)) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return -EINVAL;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt e = strappenda(n->name, ".busname");
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt if (!unit_has_name(UNIT(n), e)) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return -EINVAL;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt }
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt return 0;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt}
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic int busname_load(Unit *u) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt BusName *n = BUSNAME(u);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt int r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(u);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(u->load_state == UNIT_STUB);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = unit_load_fragment_and_dropin(u);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (u->load_state == UNIT_LOADED) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt /* This is a new unit? Then let's add in some extras */
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = busname_add_extras(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return busname_verify(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt BusName *n = BUSNAME(u);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(f);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt fprintf(f,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt "%sBus Name State: %s\n"
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt "%sResult: %s\n"
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt "%sName: %s\n",
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt prefix, busname_state_to_string(n->state),
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt prefix, busname_result_to_string(n->result),
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt prefix, n->name);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt}
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flyktstatic void busname_unwatch_fd(BusName *n) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt int r;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt assert(n);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt if (n->event_source) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt if (r < 0)
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt }
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt}
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flyktstatic void busname_close_fd(BusName *n) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt assert(n);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt busname_unwatch_fd(n);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt if (n->starter_fd <= 0)
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt return;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt close_nointr_nofail(n->starter_fd);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt n->starter_fd = -1;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
44481a8b537839cd9ffead4d261491641f5b5260Zbigniew Jędrzejewski-Szmek
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int busname_watch_fd(BusName *n) {
d182960ae974a0074010a058d0d909846a2f3f79Patrik Flykt int r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->starter_fd < 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->event_source)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt else
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = sd_event_add_io(UNIT(n)->manager->event, n->starter_fd, EPOLLIN, busname_dispatch_io, n, &n->event_source);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt busname_unwatch_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int busname_open_fd(BusName *n) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->starter_fd >= 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt n->starter_fd = bus_kernel_create_starter(UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user", n->name);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->starter_fd < 0) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return n->starter_fd;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic void busname_set_state(BusName *n, BusNameState state) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt BusNameState old_state;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt old_state = n->state;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt n->state = state;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (state != BUSNAME_LISTENING)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt busname_unwatch_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt busname_close_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (state != old_state)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int busname_coldplug(Unit *u) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt BusName *n = BUSNAME(u);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt int r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n->state == BUSNAME_DEAD);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->deserialized_state == n->state)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, BUSNAME_RUNNING)) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = busname_open_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (n->deserialized_state == BUSNAME_LISTENING) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = busname_watch_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt busname_set_state(n, n->deserialized_state);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt return 0;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic void busname_enter_dead(BusName *n, BusNameResult f) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (f != BUSNAME_SUCCESS)
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt n->result = f;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt}
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic void busname_enter_listening(BusName *n) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt int r;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt assert(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = busname_open_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt goto fail;
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt }
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = busname_watch_fd(n);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (r < 0) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt goto fail;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_set_state(n, BUSNAME_LISTENING);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktfail:
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic void busname_enter_running(BusName *n) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt bool pending = false;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt Unit *other;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt Iterator i;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt int r;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* We don't take conenctions anymore if we are supposed to
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt * shut down anyway */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (unit_stop_pending(UNIT(n))) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* Flush all queued activation reqeuest by closing and reopening the connection */
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt bus_kernel_drop_one(n->starter_fd);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt busname_enter_listening(n);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt return;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt }
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt /* If there's already a start pending don't bother to do
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt * anything */
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt if (unit_active_or_pending(other)) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt pending = true;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt break;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt }
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt if (!pending) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (r < 0)
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt goto fail;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt busname_set_state(n, BUSNAME_RUNNING);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktfail:
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int busname_start(Unit *u) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (UNIT_ISSET(n->service)) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt Service *service;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt service = SERVICE(UNIT_DEREF(n->service));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (UNIT(service)->load_state != UNIT_LOADED) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return -ENOENT;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt n->result = BUSNAME_SUCCESS;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt busname_enter_listening(n);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int busname_stop(Unit *u) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt busname_enter_dead(n, BUSNAME_SUCCESS);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(f);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(fds);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (n->starter_fd >= 0) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt int copy;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt copy = fdset_put_dup(fds, n->starter_fd);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (copy < 0)
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return copy;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt }
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt return 0;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(key);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(value);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (streq(key, "state")) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusNameState state;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt state = busname_state_from_string(value);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (state < 0)
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_debug_unit(u->id, "Failed to parse state value %s", value);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt else
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt n->deserialized_state = state;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt } else if (streq(key, "result")) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusNameResult f;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt f = busname_result_from_string(value);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (f < 0)
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_debug_unit(u->id, "Failed to parse result value %s", value);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt else if (f != BUSNAME_SUCCESS)
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt n->result = f;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt } else if (streq(key, "starter-fd")) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt int fd;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt else {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (n->starter_fd >= 0)
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt close_nointr_nofail(n->starter_fd);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt n->starter_fd = fdset_remove(fds, fd);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt } else
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_debug_unit(u->id, "Unknown serialization key '%s'", key);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt_pure_ static UnitActiveState busname_active_state(Unit *u) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return state_translation_table[BUSNAME(u)->state];
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt_pure_ static const char *busname_sub_state_to_string(Unit *u) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(u);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return busname_state_to_string(BUSNAME(u)->state);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = userdata;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(fd >= 0);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (n->state != BUSNAME_LISTENING)
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (revents != EPOLLIN) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt UNIT(n)->id, revents);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt goto fail;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt }
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_enter_running(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktfail:
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return 0;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic void busname_reset_failed(Unit *u) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt BusName *n = BUSNAME(u);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (n->state == BUSNAME_FAILED)
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_set_state(n, BUSNAME_DEAD);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt n->result = BUSNAME_SUCCESS;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt}
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flyktstatic void busname_trigger_notify(Unit *u, Unit *other) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt BusName *n = BUSNAME(u);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt Service *s;
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt assert(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt assert(other);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt s = SERVICE(other);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt else if (IN_SET(s->state,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt SERVICE_DEAD, SERVICE_FAILED,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt SERVICE_AUTO_RESTART))
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt busname_enter_listening(n);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt}
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen [BUSNAME_DEAD] = "dead",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_LISTENING] = "listening",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_RUNNING] = "running",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_FAILED] = "failed"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt};
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik FlyktDEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_SUCCESS] = "success",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt [BUSNAME_FAILURE_RESOURCES] = "resources",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt};
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik FlyktDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktconst UnitVTable busname_vtable = {
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen .object_size = sizeof(BusName),
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .sections =
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt "Unit\0"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt "BusName\0"
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt "Install\0",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .private_section = "BusName",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .init = busname_init,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .done = busname_done,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .load = busname_load,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .coldplug = busname_coldplug,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .dump = busname_dump,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .start = busname_start,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .stop = busname_stop,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .serialize = busname_serialize,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .deserialize_item = busname_deserialize_item,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .active_state = busname_active_state,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .sub_state_to_string = busname_sub_state_to_string,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .trigger_notify = busname_trigger_notify,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt .reset_failed = busname_reset_failed,
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt .bus_interface = "org.freedesktop.systemd1.BusName",
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt .bus_vtable = bus_busname_vtable,
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .status_message_formats = {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt .finished_start_job = {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt [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.",
},
},
};