busname.c revision 1c2e9646e4a1720fc8ad35c705c195ae1a2c5ce0
/*-*- 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 "kdbus.h"
#include "bus-policy.h"
#include "dbus-busname.h"
#include "busname.h"
#include "formats-util.h"
};
static void busname_init(Unit *u) {
assert(u);
n->starter_fd = -1;
n->accept_fd = true;
n->activating = true;
}
static void busname_unwatch_control_pid(BusName *n) {
assert(n);
if (n->control_pid <= 0)
return;
n->control_pid = 0;
}
static void busname_free_policy(BusName *n) {
BusNamePolicy *p;
assert(n);
while ((p = n->policy)) {
free(p);
}
}
static void busname_close_fd(BusName *n) {
assert(n);
}
static void busname_done(Unit *u) {
assert(n);
busname_close_fd(n);
unit_ref_unset(&n->service);
}
static int busname_arm_timer(BusName *n) {
int r;
assert(n);
if (n->timeout_usec <= 0) {
return 0;
}
if (n->timer_event_source) {
if (r < 0)
return r;
}
r = sd_event_add_time(
&n->timer_event_source,
if (r < 0)
return r;
return 0;
}
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 (n->activating) {
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_unit_error(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"
"%sActivating: %s\n"
"%sAccept FD: %s\n",
if (n->control_pid > 0)
fprintf(f,
prefix, n->control_pid);
}
static void busname_unwatch_fd(BusName *n) {
int r;
assert(n);
if (!n->starter_event_source)
return;
if (r < 0)
}
static int busname_watch_fd(BusName *n) {
int r;
assert(n);
if (n->starter_fd < 0)
return 0;
if (n->starter_event_source)
else
r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
if (r < 0) {
return r;
}
return 0;
}
static int busname_open_fd(BusName *n) {
const char *mode;
assert(n);
if (n->starter_fd >= 0)
return 0;
if (n->starter_fd < 0)
return log_unit_warning_errno(UNIT(n)->id, n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
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;
if (n->control_pid <= 0)
return -EBADMSG;
if (r < 0)
return r;
r = busname_arm_timer(n);
if (r < 0)
return r;
}
if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
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;
}
int r;
r = busname_arm_timer(n);
if (r < 0)
goto fail;
* hence let's fork here. It's messy, but well, what can we
* do? */
if (pid < 0)
return -errno;
if (pid == 0) {
int ret;
r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
if (r < 0) {
goto fail_child;
}
_exit(0);
log_open();
log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
}
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
assert(n);
if (f != BUSNAME_SUCCESS)
n->result = f;
}
KillContext kill_context = {};
int r;
assert(n);
if (f != BUSNAME_SUCCESS)
n->result = f;
r = unit_kill_context(UNIT(n),
-1,
n->control_pid,
false);
if (r < 0) {
goto fail;
}
if (r > 0) {
r = busname_arm_timer(n);
if (r < 0) {
goto fail;
}
busname_set_state(n, state);
} else if (state == BUSNAME_SIGTERM)
else
return;
fail:
}
static void busname_enter_listening(BusName *n) {
int r;
assert(n);
if (n->activating) {
r = busname_watch_fd(n);
if (r < 0) {
goto fail;
}
} else
return;
fail:
}
static void busname_enter_making(BusName *n) {
int r;
assert(n);
r = busname_open_fd(n);
if (r < 0)
goto fail;
if (n->policy) {
* names, which we can't do from PID1, hence let's
* fork. */
r = busname_make_starter(n, &n->control_pid);
if (r < 0) {
goto fail;
}
} else {
/* If there is no policy, we can do everything
* directly from PID 1, hence do so. */
r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
if (r < 0) {
goto fail;
}
}
return;
fail:
}
static void busname_enter_running(BusName *n) {
bool pending = false;
Iterator i;
int r;
assert(n);
if (!n->activating)
return;
/* We don't take connections anymore if we are supposed to
* shut down anyway */
if (unit_stop_pending(UNIT(n))) {
log_unit_debug(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_unit_warning(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);
/* We cannot fulfill this request right now, try again later
* please! */
return -EAGAIN;
/* Already on it! */
if (n->state == BUSNAME_MAKING)
return 0;
return -ENOENT;
}
}
n->result = BUSNAME_SUCCESS;
return 1;
}
static int busname_stop(Unit *u) {
assert(n);
/* Already on it */
return 0;
/* If there's already something running, we go directly into
* kill mode. */
if (n->state == BUSNAME_MAKING) {
return -EAGAIN;
}
return 1;
}
assert(n);
assert(f);
if (n->control_pid > 0)
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;
else
n->control_pid = pid;
int fd;
else {
safe_close(n->starter_fd);
}
} else
return 0;
}
assert(u);
}
assert(u);
}
static int busname_peek_message(BusName *n) {
struct kdbus_cmd_recv cmd_recv = {
.flags = KDBUS_RECV_PEEK,
};
struct kdbus_cmd_free cmd_free = {
};
struct kdbus_item *d;
struct kdbus_msg *k;
void *p = NULL;
int r;
/* Generate a friendly debug log message about which process
* caused triggering of this bus name. This simply peeks the
* metadata of the first queued message and logs it. */
assert(n);
/* Let's shortcut things a bit, if debug logging is turned off
* anyway. */
if (log_get_max_level() < LOG_DEBUG)
return 0;
if (r < 0) {
return 0;
return -errno;
}
/* We map as late as possible, and unmap imemdiately after
* use. On 32bit address space is scarce and we want to be
* able to handle a lot of activator connections at the same
* time, and hence shouldn't keep the mmap()s around for
* longer than necessary. */
if (p == MAP_FAILED) {
r = -errno;
goto finish;
}
KDBUS_ITEM_FOREACH(d, k, items) {
switch (d->type) {
case KDBUS_ITEM_PIDS:
break;
case KDBUS_ITEM_PID_COMM:
break;
}
}
if (pid > 0)
log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm));
r = 0;
if (p)
return r;
}
assert(n);
if (n->state != BUSNAME_LISTENING)
return 0;
goto fail;
}
return 0;
fail:
return 0;
}
assert(n);
if (pid != n->control_pid)
return;
n->control_pid = 0;
f = BUSNAME_SUCCESS;
else if (code == CLD_EXITED)
else if (code == CLD_KILLED)
else if (code == CLD_DUMPED)
else
assert_not_reached("Unknown sigchld code");
log_unit_full(u->id,
"%s control process exited, code=%s status=%i",
if (f != BUSNAME_SUCCESS)
n->result = f;
switch (n->state) {
case BUSNAME_MAKING:
if (f == BUSNAME_SUCCESS)
else
busname_enter_signal(n, BUSNAME_SIGTERM, f);
break;
case BUSNAME_SIGTERM:
case BUSNAME_SIGKILL:
busname_enter_dead(n, f);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
/* Notify clients about changed exit status */
}
assert(n);
switch (n->state) {
case BUSNAME_MAKING:
break;
case BUSNAME_SIGTERM:
break;
case BUSNAME_SIGKILL:
break;
default:
assert_not_reached("Timeout at wrong time.");
}
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;
}
}
int r;
if (!n->timer_event_source)
return 0;
if (r < 0)
return r;
return 1;
}
static bool busname_supported(void) {
static int supported = -1;
if (supported < 0)
return supported;
}
static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
[BUSNAME_DEAD] = "dead",
[BUSNAME_MAKING] = "making",
[BUSNAME_REGISTERED] = "registered",
[BUSNAME_LISTENING] = "listening",
[BUSNAME_RUNNING] = "running",
[BUSNAME_SIGTERM] = "sigterm",
[BUSNAME_SIGKILL] = "sigkill",
[BUSNAME_FAILED] = "failed",
};
static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
[BUSNAME_SUCCESS] = "success",
[BUSNAME_FAILURE_RESOURCES] = "resources",
[BUSNAME_FAILURE_TIMEOUT] = "timeout",
[BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
[BUSNAME_FAILURE_SIGNAL] = "signal",
[BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
[BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
};
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,
.kill = busname_kill,
.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.",
},
},
};