busname.c revision 3f9da416457c4265b8f1179516a32ad1a987ff7d
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering This file is part of systemd.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Copyright 2013 Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is free software; you can redistribute it and/or modify it
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering under the terms of the GNU Lesser General Public License as published by
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (at your option) any later version.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is distributed in the hope that it will be useful, but
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Lesser General Public License for more details.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering You should have received a copy of the GNU Lesser General Public License
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->event_source = sd_event_source_unref(n->event_source);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = safe_close(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_add_default_default_dependencies(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_add_extras(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = busname_add_default_default_dependencies(n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers /* This is a new unit? Then let's add in some extras */
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sieversstatic void busname_dump(Unit *u, FILE *f, const char *prefix) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sBus Name State: %s\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sResult: %s\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "%sName: %s\n"
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering "%sAccept FD: %s\n",
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering prefix, busname_result_to_string(n->result),
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n->starter_fd = safe_close(n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_event_add_io(UNIT(n)->manager->event, &n->event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering n->starter_fd = bus_kernel_create_starter(
1a5613266a93ddb197bbae137cd61fae25600718Lennart Poettering UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_set_state(BusName *n, BusNameState state) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, BUSNAME_RUNNING)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n->deserialized_state == BUSNAME_LISTENING) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, n->deserialized_state);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_dead(BusName *n, BusNameResult f) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_listening(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_enter_running(BusName *n) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* We don't take conenctions anymore if we are supposed to
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * shut down anyway */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* Flush all queued activation reqeuest by closing and reopening the connection */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* If there's already a start pending don't bother to do
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering service = SERVICE(UNIT_DEREF(n->service));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering copy = fdset_put_dup(fds, n->starter_fd);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering state = busname_state_from_string(value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse state value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse result value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(u->id, "Unknown serialization key '%s'", key);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering_pure_ static UnitActiveState busname_active_state(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return state_translation_table[BUSNAME(u)->state];
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering_pure_ static const char *busname_sub_state_to_string(Unit *u) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return busname_state_to_string(BUSNAME(u)->state);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic void busname_reset_failed(Unit *u) {
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poetteringstatic void busname_trigger_notify(Unit *u, Unit *other) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
c978343015c787713651dff571acb5207367f5f2Lennart Poettering SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILURE_RESOURCES] = "resources",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic const char* const busname_policy_access_table[_BUSNAME_POLICY_ACCESS_MAX] = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(busname_policy_access, BusNamePolicyAccess);
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen "Install\0",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .deserialize_item = busname_deserialize_item,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .sub_state_to_string = busname_sub_state_to_string,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .trigger_notify = busname_trigger_notify,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .bus_interface = "org.freedesktop.systemd1.BusName",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_FAILED] = "Failed to listen on %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_DEPENDENCY] = "Dependency failed for %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_TIMEOUT] = "Timed out starting %s.",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering [JOB_TIMEOUT] = "Timed out stopping %s.",