busname.c revision 1683342af1fc85e75ba21264bbcedc26aef0fa39
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "special.h"
#include "bus-kernel.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "service.h"
#include "dbus-busname.h"
#include "busname.h"
};
static void busname_init(Unit *u) {
assert(u);
n->starter_fd = -1;
}
static void busname_done(Unit *u) {
assert(u);
unit_ref_unset(&n->service);
if (n->starter_fd >= 0) {
n->starter_fd = -1;
}
}
static int busname_add_default_default_dependencies(BusName *n) {
int r;
assert(n);
if (r < 0)
return r;
r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
}
return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
static int busname_add_extras(BusName *n) {
int r;
assert(n);
if (!n->name) {
if (!n->name)
return -ENOMEM;
}
if (!u->description) {
r = unit_set_description(u, n->name);
if (r < 0)
return r;
}
if (!UNIT_DEREF(n->service)) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
unit_ref_set(&n->service, x);
}
if (r < 0)
return r;
if (u->default_dependencies) {
if (r < 0)
return r;
}
return 0;
}
static int busname_verify(BusName *n) {
char *e;
assert(n);
return 0;
if (!service_name_is_valid(n->name)) {
log_error_unit(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
return -EINVAL;
}
if (!unit_has_name(UNIT(n), e)) {
return -EINVAL;
}
return 0;
}
static int busname_load(Unit *u) {
int r;
assert(u);
r = unit_load_fragment_and_dropin(u);
if (r < 0)
return r;
if (u->load_state == UNIT_LOADED) {
/* This is a new unit? Then let's add in some extras */
r = busname_add_extras(n);
if (r < 0)
return r;
}
return busname_verify(n);
}
assert(n);
assert(f);
fprintf(f,
"%sBus Name State: %s\n"
"%sResult: %s\n"
"%sName: %s\n",
}
static void busname_unwatch_fd(BusName *n) {
int r;
assert(n);
if (n->event_source) {
if (r < 0)
}
}
static void busname_close_fd(BusName *n) {
assert(n);
if (n->starter_fd <= 0)
return;
n->starter_fd = -1;
}
static int busname_watch_fd(BusName *n) {
int r;
assert(n);
if (n->starter_fd < 0)
return 0;
if (n->event_source)
else
r = sd_event_add_io(UNIT(n)->manager->event, &n->event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
if (r < 0) {
return r;
}
return 0;
}
static int busname_open_fd(BusName *n) {
assert(n);
if (n->starter_fd >= 0)
return 0;
n->starter_fd = bus_kernel_create_starter(UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user",
if (n->starter_fd < 0) {
return n->starter_fd;
}
return 0;
}
assert(n);
if (state != BUSNAME_LISTENING)
busname_close_fd(n);
}
static int busname_coldplug(Unit *u) {
int r;
assert(n);
if (n->deserialized_state == n->state)
return 0;
r = busname_open_fd(n);
if (r < 0)
return r;
}
if (n->deserialized_state == BUSNAME_LISTENING) {
r = busname_watch_fd(n);
if (r < 0)
return r;
}
busname_set_state(n, n->deserialized_state);
return 0;
}
assert(n);
if (f != BUSNAME_SUCCESS)
n->result = f;
}
static void busname_enter_listening(BusName *n) {
int r;
assert(n);
r = busname_open_fd(n);
if (r < 0) {
goto fail;
}
r = busname_watch_fd(n);
if (r < 0) {
goto fail;
}
return;
fail:
}
static void busname_enter_running(BusName *n) {
bool pending = false;
Iterator i;
int r;
assert(n);
/* We don't take conenctions anymore if we are supposed to
* shut down anyway */
if (unit_stop_pending(UNIT(n))) {
log_debug_unit(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
/* Flush all queued activation reqeuest by closing and reopening the connection */
return;
}
/* If there's already a start pending don't bother to do
* anything */
if (unit_active_or_pending(other)) {
pending = true;
break;
}
if (!pending) {
r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
if (r < 0)
goto fail;
}
return;
fail:
log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
}
static int busname_start(Unit *u) {
assert(n);
if (UNIT_ISSET(n->service)) {
return -ENOENT;
}
}
n->result = BUSNAME_SUCCESS;
return 0;
}
static int busname_stop(Unit *u) {
assert(n);
return 0;
}
assert(n);
assert(f);
if (n->starter_fd >= 0) {
int copy;
if (copy < 0)
return copy;
}
return 0;
}
assert(n);
if (state < 0)
else
n->deserialized_state = state;
if (f < 0)
else if (f != BUSNAME_SUCCESS)
n->result = f;
int fd;
else {
if (n->starter_fd >= 0)
}
} else
return 0;
}
assert(u);
}
assert(u);
}
assert(n);
if (n->state != BUSNAME_LISTENING)
return 0;
goto fail;
}
return 0;
fail:
return 0;
}
static void busname_reset_failed(Unit *u) {
assert(n);
if (n->state == BUSNAME_FAILED)
n->result = BUSNAME_SUCCESS;
}
Service *s;
assert(n);
return;
return;
}
static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
[BUSNAME_DEAD] = "dead",
[BUSNAME_LISTENING] = "listening",
[BUSNAME_RUNNING] = "running",
[BUSNAME_FAILED] = "failed"
};
static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
[BUSNAME_SUCCESS] = "success",
[BUSNAME_FAILURE_RESOURCES] = "resources",
[BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
};
static const char* const busname_policy_access_table[_BUSNAME_POLICY_ACCESS_MAX] = {
[BUSNAME_POLICY_ACCESS_SEE] = "see",
[BUSNAME_POLICY_ACCESS_TALK] = "talk",
[BUSNAME_POLICY_ACCESS_OWN] = "own",
};
const UnitVTable busname_vtable = {
.object_size = sizeof(BusName),
.sections =
"Unit\0"
"BusName\0"
"Install\0",
.private_section = "BusName",
.init = busname_init,
.done = busname_done,
.load = busname_load,
.dump = busname_dump,
.start = busname_start,
.stop = busname_stop,
.bus_interface = "org.freedesktop.systemd1.BusName",
.finished_start_job = {
[JOB_DONE] = "Listening on %s.",
[JOB_FAILED] = "Failed to listen on %s.",
[JOB_DEPENDENCY] = "Dependency failed for %s.",
[JOB_TIMEOUT] = "Timed out starting %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Closed %s.",
[JOB_FAILED] = "Failed stopping %s.",
[JOB_TIMEOUT] = "Timed out stopping %s.",
},
},
};