busname.c revision 16ac401407959cbc62312e61c2dd76dbc3a0793b
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "special.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "bus-kernel.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "bus-internal.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "bus-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "service.h"
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include "dbus-busname.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "busname.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering [BUSNAME_DEAD] = UNIT_INACTIVE,
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering [BUSNAME_LISTENING] = UNIT_ACTIVE,
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering [BUSNAME_RUNNING] = UNIT_ACTIVE,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen [BUSNAME_FAILED] = UNIT_FAILED
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering};
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringstatic void busname_init(Unit *u) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering BusName *n = BUSNAME(u);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering assert(u);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering assert(u->load_state == UNIT_STUB);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering n->starter_fd = -1;
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering}
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poetteringstatic void busname_done(Unit *u) {
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering BusName *n = BUSNAME(u);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier assert(u);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering free(n->name);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering n->name = NULL;
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering unit_ref_unset(&n->service);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering n->event_source = sd_event_source_unref(n->event_source);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (n->starter_fd >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering close_nointr_nofail(n->starter_fd);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering n->starter_fd = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int busname_add_default_default_dependencies(BusName *n) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(n);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int busname_add_extras(BusName *n) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Unit *u = UNIT(n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!n->name) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering n->name = unit_name_to_prefix(u->id);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (!n->name)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return -ENOMEM;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering }
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (!u->description) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = unit_set_description(u, n->name);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (r < 0)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return r;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering }
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (!UNIT_DEREF(n->service)) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering Unit *x;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = unit_load_related_unit(u, ".service", &x);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r < 0)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering unit_ref_set(&n->service, x);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return r;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (u->default_dependencies) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = busname_add_default_default_dependencies(n);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return r;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return 0;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic int busname_verify(BusName *n) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char *e;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(n);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (UNIT(n)->load_state != UNIT_LOADED)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!service_name_is_valid(n->name)) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return -EINVAL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen e = strappenda(n->name, ".busname");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!unit_has_name(UNIT(n), e)) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return -EINVAL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int busname_load(Unit *u) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen BusName *n = BUSNAME(u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(u);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(u->load_state == UNIT_STUB);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering r = unit_load_fragment_and_dropin(u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return r;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (u->load_state == UNIT_LOADED) {
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering /* This is a new unit? Then let's add in some extras */
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering r = busname_add_extras(n);
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return r;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return busname_verify(n);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering BusName *n = BUSNAME(u);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(n);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(f);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering fprintf(f,
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "%sBus Name State: %s\n"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "%sResult: %s\n"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "%sName: %s\n",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering prefix, busname_state_to_string(n->state),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering prefix, busname_result_to_string(n->result),
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering prefix, n->name);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic void busname_unwatch_fd(BusName *n) {
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering int r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(n);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (n->event_source) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic void busname_close_fd(BusName *n) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(n);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering busname_unwatch_fd(n);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (n->starter_fd <= 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering close_nointr_nofail(n->starter_fd);
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen n->starter_fd = -1;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int busname_watch_fd(BusName *n) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (n->starter_fd < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (n->event_source)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering else
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_event_add_io(UNIT(n)->manager->event, n->starter_fd, EPOLLIN, busname_dispatch_io, n, &n->event_source);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering busname_unwatch_fd(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return 0;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringstatic int busname_open_fd(BusName *n) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (n->starter_fd >= 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering n->starter_fd = bus_kernel_create_starter(UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user", n->name);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (n->starter_fd < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return n->starter_fd;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic void busname_set_state(BusName *n, BusNameState state) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering BusNameState old_state;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering old_state = n->state;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering n->state = state;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (state != BUSNAME_LISTENING)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering busname_unwatch_fd(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering busname_close_fd(n);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (state != old_state)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int busname_coldplug(Unit *u) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering BusName *n = BUSNAME(u);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(n->state == BUSNAME_DEAD);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (n->deserialized_state == n->state)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, BUSNAME_RUNNING)) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = busname_open_fd(n);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return r;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (n->deserialized_state == BUSNAME_LISTENING) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = busname_watch_fd(n);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering busname_set_state(n, n->deserialized_state);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return 0;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering}
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poetteringstatic void busname_enter_dead(BusName *n, BusNameResult f) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert(n);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (f != BUSNAME_SUCCESS)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering n->result = f;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering}
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poetteringstatic void busname_enter_listening(BusName *n) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering int r;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = busname_open_fd(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto fail;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = busname_watch_fd(n);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto fail;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering busname_set_state(n, BUSNAME_LISTENING);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringfail:
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic void busname_enter_running(BusName *n) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering bool pending = false;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering Unit *other;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering Iterator i;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int r;
1fc464f6fbecfc5d8ba9f7b98d19e21fb324bfb9Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(n);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering /* We don't take conenctions anymore if we are supposed to
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * shut down anyway */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (unit_stop_pending(UNIT(n))) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Flush all queued activation reqeuest by closing and reopening the connection */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering busname_close_fd(n);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering busname_enter_listening(n);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* If there's already a start pending don't bother to do
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering * anything */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (unit_active_or_pending(other)) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering pending = true;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering break;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (!pending) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering goto fail;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering busname_set_state(n, BUSNAME_RUNNING);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringfail:
21b735e798c580e7af8c33ace9f8565860b7f8dfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poetteringstatic int busname_start(Unit *u) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering BusName *n = BUSNAME(u);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert(n);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (UNIT_ISSET(n->service)) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering Service *service;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering service = SERVICE(UNIT_DEREF(n->service));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
6f883237f1b8a96ec0ea354866e033b6fcea9506Lennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return -ENOENT;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering n->result = BUSNAME_SUCCESS;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering busname_enter_listening(n);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 0;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic int busname_stop(Unit *u) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering BusName *n = BUSNAME(u);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(n);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering busname_enter_dead(n, BUSNAME_SUCCESS);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering BusName *n = BUSNAME(u);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(n);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(f);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(fds);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (n->starter_fd >= 0) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int copy;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering copy = fdset_put_dup(fds, n->starter_fd);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (copy < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return copy;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering BusName *n = BUSNAME(u);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(n);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(key);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(value);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (streq(key, "state")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering BusNameState state;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering state = busname_state_from_string(value);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (state < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_debug_unit(u->id, "Failed to parse state value %s", value);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering else
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering n->deserialized_state = state;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering } else if (streq(key, "result")) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering BusNameResult f;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering f = busname_result_from_string(value);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (f < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_debug_unit(u->id, "Failed to parse result value %s", value);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering else if (f != BUSNAME_SUCCESS)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering n->result = f;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering } else if (streq(key, "starter-fd")) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int fd;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering else {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (n->starter_fd >= 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering close_nointr_nofail(n->starter_fd);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering n->starter_fd = fdset_remove(fds, fd);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering } else
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_debug_unit(u->id, "Unknown serialization key '%s'", key);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return 0;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering_pure_ static UnitActiveState busname_active_state(Unit *u) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(u);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return state_translation_table[BUSNAME(u)->state];
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering_pure_ static const char *busname_sub_state_to_string(Unit *u) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(u);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return busname_state_to_string(BUSNAME(u)->state);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering BusName *n = userdata;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(n);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(fd >= 0);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (n->state != BUSNAME_LISTENING)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return 0;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (revents != EPOLLIN) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering UNIT(n)->id, revents);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering goto fail;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers busname_enter_running(n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversfail:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic void busname_reset_failed(Unit *u) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering BusName *n = BUSNAME(u);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(n);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (n->state == BUSNAME_FAILED)
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann busname_set_state(n, BUSNAME_DEAD);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann n->result = BUSNAME_SUCCESS;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann}
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmannstatic void busname_trigger_notify(Unit *u, Unit *other) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann BusName *n = BUSNAME(u);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann Service *s;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann assert(n);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(other);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering s = SERVICE(other);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (IN_SET(s->state,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SERVICE_DEAD, SERVICE_FAILED,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering SERVICE_AUTO_RESTART))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering busname_enter_listening(n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_DEAD] = "dead",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_LISTENING] = "listening",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_RUNNING] = "running",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_FAILED] = "failed"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_SUCCESS] = "success",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering [BUSNAME_FAILURE_RESOURCES] = "resources",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringconst UnitVTable busname_vtable = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .object_size = sizeof(BusName),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .sections =
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "Unit\0"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "BusName\0"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "Install\0",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .private_section = "BusName",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .init = busname_init,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .done = busname_done,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .load = busname_load,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .coldplug = busname_coldplug,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .dump = busname_dump,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .start = busname_start,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .stop = busname_stop,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .serialize = busname_serialize,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .deserialize_item = busname_deserialize_item,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .active_state = busname_active_state,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .sub_state_to_string = busname_sub_state_to_string,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .trigger_notify = busname_trigger_notify,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .reset_failed = busname_reset_failed,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .bus_interface = "org.freedesktop.systemd1.BusName",
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .bus_vtable = bus_busname_vtable,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .bus_changing_properties = bus_busname_changing_properties,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .status_message_formats = {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering .finished_start_job = {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering [JOB_DONE] = "Listening on %s.",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering [JOB_FAILED] = "Failed to listen on %s.",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering [JOB_DEPENDENCY] = "Dependency failed for %s.",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering [JOB_TIMEOUT] = "Timed out starting %s.",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering .finished_stop_job = {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering [JOB_DONE] = "Closed %s.",
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering [JOB_FAILED] = "Failed stopping %s.",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering [JOB_TIMEOUT] = "Timed out stopping %s.",
ece174c5439021e32ebcc858842de9586072c006Lennart Poettering },
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering },
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering};
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering