X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fbusname.c;h=99641ec418db79c813a8a9b6d888af30e5722ef3;hb=affc3d834347076e8616948978e70ed1fca84db4;hp=ccef32e9e7daea96536399d01be4da73939cd104;hpb=3f9da416457c4265b8f1179516a32ad1a987ff7d;p=elogind.git
diff --git a/src/core/busname.c b/src/core/busname.c
index ccef32e9e..99641ec41 100644
--- a/src/core/busname.c
+++ b/src/core/busname.c
@@ -19,6 +19,8 @@
along with systemd; If not, see .
***/
+#include
+
#include "special.h"
#include "bus-kernel.h"
#include "bus-internal.h"
@@ -26,15 +28,21 @@
#include "service.h"
#include "dbus-busname.h"
#include "busname.h"
+#include "kdbus.h"
static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
[BUSNAME_DEAD] = UNIT_INACTIVE,
+ [BUSNAME_MAKING] = UNIT_ACTIVATING,
+ [BUSNAME_REGISTERED] = UNIT_ACTIVE,
[BUSNAME_LISTENING] = UNIT_ACTIVE,
[BUSNAME_RUNNING] = UNIT_ACTIVE,
+ [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
+ [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
[BUSNAME_FAILED] = UNIT_FAILED
};
static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
+static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
static void busname_init(Unit *u) {
BusName *n = BUSNAME(u);
@@ -44,20 +52,82 @@ static void busname_init(Unit *u) {
n->starter_fd = -1;
n->accept_fd = true;
+ n->activating = true;
+
+ n->timeout_usec = u->manager->default_timeout_start_usec;
+}
+
+static void busname_unwatch_control_pid(BusName *n) {
+ assert(n);
+
+ if (n->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(n), n->control_pid);
+ n->control_pid = 0;
+}
+
+static void busname_free_policy(BusName *n) {
+ BusNamePolicy *p;
+
+ assert(n);
+
+ while ((p = n->policy)) {
+ LIST_REMOVE(policy, n->policy, p);
+
+ free(p->name);
+ free(p);
+ }
+}
+
+static void busname_close_fd(BusName *n) {
+ assert(n);
+
+ n->starter_event_source = sd_event_source_unref(n->starter_event_source);
+ n->starter_fd = safe_close(n->starter_fd);
}
static void busname_done(Unit *u) {
BusName *n = BUSNAME(u);
- assert(u);
+ assert(n);
free(n->name);
n->name = NULL;
+ busname_free_policy(n);
+ busname_unwatch_control_pid(n);
+ busname_close_fd(n);
+
unit_ref_unset(&n->service);
- n->event_source = sd_event_source_unref(n->event_source);
- n->starter_fd = safe_close(n->starter_fd);
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+}
+
+static int busname_arm_timer(BusName *n) {
+ int r;
+
+ assert(n);
+
+ if (n->timeout_usec <= 0) {
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+ return 0;
+ }
+
+ if (n->timer_event_source) {
+ r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec);
+ if (r < 0)
+ return r;
+
+ return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
+ }
+
+ return sd_event_add_time(
+ UNIT(n)->manager->event,
+ &n->timer_event_source,
+ CLOCK_MONOTONIC,
+ now(CLOCK_MONOTONIC) + n->timeout_usec, 0,
+ busname_dispatch_timer, n);
}
static int busname_add_default_default_dependencies(BusName *n) {
@@ -96,20 +166,22 @@ static int busname_add_extras(BusName *n) {
return r;
}
- if (!UNIT_DEREF(n->service)) {
- Unit *x;
+ 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);
+ }
- r = unit_load_related_unit(u, ".service", &x);
+ r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
if (r < 0)
return r;
-
- unit_ref_set(&n->service, x);
}
- r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
- if (r < 0)
- return r;
-
if (u->default_dependencies) {
r = busname_add_default_default_dependencies(n);
if (r < 0)
@@ -128,13 +200,13 @@ static int busname_verify(BusName *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);
+ log_unit_error(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
return -EINVAL;
}
e = strappenda(n->name, ".busname");
if (!unit_has_name(UNIT(n), e)) {
- log_error_unit(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
+ log_unit_error(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
return -EINVAL;
}
@@ -172,11 +244,18 @@ static void busname_dump(Unit *u, FILE *f, const char *prefix) {
"%sBus Name State: %s\n"
"%sResult: %s\n"
"%sName: %s\n"
+ "%sActivating: %s\n"
"%sAccept FD: %s\n",
prefix, busname_state_to_string(n->state),
prefix, busname_result_to_string(n->result),
prefix, n->name,
+ prefix, yes_no(n->activating),
prefix, yes_no(n->accept_fd));
+
+ if (n->control_pid > 0)
+ fprintf(f,
+ "%sControl PID: "PID_FMT"\n",
+ prefix, n->control_pid);
}
static void busname_unwatch_fd(BusName *n) {
@@ -184,22 +263,12 @@ static void busname_unwatch_fd(BusName *n) {
assert(n);
- if (n->event_source) {
- r = sd_event_source_set_enabled(n->event_source, SD_EVENT_OFF);
- if (r < 0)
- log_debug_unit(UNIT(n)->id, "Failed to disable event source.");
- }
-}
-
-static void busname_close_fd(BusName *n) {
- assert(n);
-
- busname_unwatch_fd(n);
-
- if (n->starter_fd <= 0)
+ if (!n->starter_event_source)
return;
- n->starter_fd = safe_close(n->starter_fd);
+ r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
+ if (r < 0)
+ log_unit_debug(UNIT(n)->id, "Failed to disable event source.");
}
static int busname_watch_fd(BusName *n) {
@@ -210,12 +279,12 @@ static int busname_watch_fd(BusName *n) {
if (n->starter_fd < 0)
return 0;
- if (n->event_source)
- r = sd_event_source_set_enabled(n->event_source, SD_EVENT_ON);
+ if (n->starter_event_source)
+ r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
else
- r = sd_event_add_io(UNIT(n)->manager->event, &n->event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
+ r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
if (r < 0) {
- log_warning_unit(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
+ log_unit_warning_errno(UNIT(n)->id, r, "Failed to watch starter fd: %m");
busname_unwatch_fd(n);
return r;
}
@@ -224,19 +293,18 @@ static int busname_watch_fd(BusName *n) {
}
static int busname_open_fd(BusName *n) {
+ _cleanup_free_ char *path = NULL;
+ const char *mode;
+
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",
- n->name, n->accept_fd, n->policy);
-
- if (n->starter_fd < 0) {
- log_warning_unit(UNIT(n)->id, "Failed to create starter fd: %s", strerror(-n->starter_fd));
- return n->starter_fd;
- }
+ mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
+ n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
+ 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;
}
@@ -248,14 +316,19 @@ static void busname_set_state(BusName *n, BusNameState state) {
old_state = n->state;
n->state = state;
+ if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+ busname_unwatch_control_pid(n);
+ }
+
if (state != BUSNAME_LISTENING)
busname_unwatch_fd(n);
- if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_RUNNING))
+ if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
busname_close_fd(n);
if (state != old_state)
- log_debug_unit(UNIT(n)->id, "%s changed %s -> %s",
+ log_unit_debug(UNIT(n)->id, "%s changed %s -> %s",
UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
@@ -271,7 +344,21 @@ static int busname_coldplug(Unit *u) {
if (n->deserialized_state == n->state)
return 0;
- if (IN_SET(n->deserialized_state, BUSNAME_LISTENING, BUSNAME_RUNNING)) {
+ if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
+
+ if (n->control_pid <= 0)
+ return -EBADMSG;
+
+ r = unit_watch_pid(UNIT(n), n->control_pid);
+ 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;
@@ -287,6 +374,56 @@ static int busname_coldplug(Unit *u) {
return 0;
}
+static int busname_make_starter(BusName *n, pid_t *_pid) {
+ pid_t pid;
+ int r;
+
+ r = busname_arm_timer(n);
+ if (r < 0)
+ goto fail;
+
+ /* We have to resolve the user/group names out-of-process,
+ * hence let's fork here. It's messy, but well, what can we
+ * do? */
+
+ pid = fork();
+ if (pid < 0)
+ return -errno;
+
+ if (pid == 0) {
+ int ret;
+
+ default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
+ ignore_signals(SIGPIPE, -1);
+ log_forget_fds();
+
+ r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
+ if (r < 0) {
+ ret = EXIT_MAKE_STARTER;
+ goto fail_child;
+ }
+
+ _exit(0);
+
+ fail_child:
+ log_open();
+ log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
+
+ _exit(ret);
+ }
+
+ r = unit_watch_pid(UNIT(n), pid);
+ if (r < 0)
+ goto fail;
+
+ *_pid = pid;
+ return 0;
+
+fail:
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+ return r;
+}
+
static void busname_enter_dead(BusName *n, BusNameResult f) {
assert(n);
@@ -296,24 +433,104 @@ static void busname_enter_dead(BusName *n, BusNameResult f) {
busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
}
-static void busname_enter_listening(BusName *n) {
+static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
+ KillContext kill_context = {};
int r;
assert(n);
- r = busname_open_fd(n);
+ if (f != BUSNAME_SUCCESS)
+ n->result = f;
+
+ kill_context_init(&kill_context);
+
+ r = unit_kill_context(UNIT(n),
+ &kill_context,
+ state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
+ -1,
+ n->control_pid,
+ false);
if (r < 0) {
- log_warning_unit(UNIT(n)->id, "%s failed to listen on bus names: %s", UNIT(n)->id, strerror(-r));
+ log_unit_warning_errno(UNIT(n)->id, r, "%s failed to kill control process: %m", UNIT(n)->id);
goto fail;
}
- r = busname_watch_fd(n);
- if (r < 0) {
- log_warning_unit(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
+ if (r > 0) {
+ r = busname_arm_timer(n);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n)->id, r, "%s failed to arm timer: %m", UNIT(n)->id);
+ goto fail;
+ }
+
+ busname_set_state(n, state);
+ } else if (state == BUSNAME_SIGTERM)
+ busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
+ else
+ busname_enter_dead(n, BUSNAME_SUCCESS);
+
+ return;
+
+fail:
+ busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_listening(BusName *n) {
+ int r;
+
+ assert(n);
+
+ if (n->activating) {
+ r = busname_watch_fd(n);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n)->id, r, "%s failed to watch names: %m", UNIT(n)->id);
+ goto fail;
+ }
+
+ busname_set_state(n, BUSNAME_LISTENING);
+ } else
+ busname_set_state(n, BUSNAME_REGISTERED);
+
+ return;
+
+fail:
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_making(BusName *n) {
+ int r;
+
+ assert(n);
+
+ r = busname_open_fd(n);
+ if (r < 0)
goto fail;
+
+ if (n->policy) {
+ /* If there is a policy, we need to resolve user/group
+ * names, which we can't do from PID1, hence let's
+ * fork. */
+ busname_unwatch_control_pid(n);
+
+ r = busname_make_starter(n, &n->control_pid);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n)->id, r, "%s failed to fork 'making' task: %m", UNIT(n)->id);
+ goto fail;
+ }
+
+ busname_set_state(n, BUSNAME_MAKING);
+ } 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) {
+ log_unit_warning_errno(UNIT(n)->id, r, "%s failed to make starter: %m", UNIT(n)->id);
+ goto fail;
+ }
+
+ busname_enter_listening(n);
}
- busname_set_state(n, BUSNAME_LISTENING);
return;
fail:
@@ -329,11 +546,14 @@ static void busname_enter_running(BusName *n) {
assert(n);
+ if (!n->activating)
+ return;
+
/* 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);
+ 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 */
bus_kernel_drop_one(n->starter_fd);
@@ -360,7 +580,7 @@ static void busname_enter_running(BusName *n) {
return;
fail:
- log_warning_unit(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
+ log_unit_warning(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
}
@@ -369,13 +589,22 @@ static int busname_start(Unit *u) {
assert(n);
- if (UNIT_ISSET(n->service)) {
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
+ return -EAGAIN;
+
+ /* Already on it! */
+ if (n->state == BUSNAME_MAKING)
+ return 0;
+
+ if (n->activating && UNIT_ISSET(n->service)) {
Service *service;
service = SERVICE(UNIT_DEREF(n->service));
if (UNIT(service)->load_state != UNIT_LOADED) {
- log_error_unit(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
+ log_unit_error(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
return -ENOENT;
}
}
@@ -383,7 +612,7 @@ static int busname_start(Unit *u) {
assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
n->result = BUSNAME_SUCCESS;
- busname_enter_listening(n);
+ busname_enter_making(n);
return 0;
}
@@ -392,7 +621,20 @@ static int busname_stop(Unit *u) {
BusName *n = BUSNAME(u);
assert(n);
- assert(n->state == BUSNAME_LISTENING || n->state == BUSNAME_RUNNING);
+
+ /* Already on it */
+ if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
+ return 0;
+
+ /* If there's already something running, we go directly into
+ * kill mode. */
+
+ if (n->state == BUSNAME_MAKING) {
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS);
+ return -EAGAIN;
+ }
+
+ assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
busname_enter_dead(n, BUSNAME_SUCCESS);
return 0;
@@ -408,6 +650,9 @@ static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
unit_serialize_item(u, f, "state", busname_state_to_string(n->state));
unit_serialize_item(u, f, "result", busname_result_to_string(n->result));
+ if (n->control_pid > 0)
+ unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
+
if (n->starter_fd >= 0) {
int copy;
@@ -433,7 +678,7 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
state = busname_state_from_string(value);
if (state < 0)
- log_debug_unit(u->id, "Failed to parse state value %s", value);
+ log_unit_debug(u->id, "Failed to parse state value %s", value);
else
n->deserialized_state = state;
@@ -442,21 +687,28 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
f = busname_result_from_string(value);
if (f < 0)
- log_debug_unit(u->id, "Failed to parse result value %s", value);
+ log_unit_debug(u->id, "Failed to parse result value %s", value);
else if (f != BUSNAME_SUCCESS)
n->result = f;
+ } else if (streq(key, "control-pid")) {
+ pid_t pid;
+
+ if (parse_pid(value, &pid) < 0)
+ log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
+ else
+ n->control_pid = pid;
} else if (streq(key, "starter-fd")) {
int fd;
if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
- log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
+ log_unit_debug(u->id, "Failed to parse starter fd value %s", value);
else {
safe_close(n->starter_fd);
n->starter_fd = fdset_remove(fds, fd);
}
} else
- log_debug_unit(u->id, "Unknown serialization key '%s'", key);
+ log_unit_debug(u->id, "Unknown serialization key '%s'", key);
return 0;
}
@@ -473,6 +725,88 @@ _pure_ static const char *busname_sub_state_to_string(Unit *u) {
return busname_state_to_string(BUSNAME(u)->state);
}
+static int busname_peek_message(BusName *n) {
+ struct kdbus_cmd_recv cmd_recv = {
+ .flags = KDBUS_RECV_PEEK,
+ };
+ struct kdbus_cmd_free cmd_free = {};
+ const char *comm = NULL;
+ struct kdbus_item *d;
+ struct kdbus_msg *k;
+ size_t start, ps, sz, delta;
+ void *p = NULL;
+ pid_t pid = 0;
+ 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;
+
+ r = ioctl(n->starter_fd, KDBUS_CMD_MSG_RECV, &cmd_recv);
+ if (r < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ return 0;
+
+ log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id);
+ 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. */
+
+ ps = page_size();
+ start = (cmd_recv.offset / ps) * ps;
+ delta = cmd_recv.offset - start;
+ sz = PAGE_ALIGN(delta + cmd_recv.msg_size);
+
+ p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
+ if (p == MAP_FAILED) {
+ log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id);
+ r = -errno;
+ goto finish;
+ }
+
+ k = (struct kdbus_msg *) ((uint8_t *) p + delta);
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ switch (d->type) {
+
+ case KDBUS_ITEM_PIDS:
+ pid = d->pids.pid;
+ break;
+
+ case KDBUS_ITEM_PID_COMM:
+ comm = d->str;
+ 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;
+
+finish:
+ if (p)
+ (void) munmap(p, sz);
+
+ cmd_free.offset = cmd_recv.offset;
+ if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
+ log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m");
+
+ return r;
+}
+
static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
BusName *n = userdata;
@@ -482,14 +816,15 @@ static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents
if (n->state != BUSNAME_LISTENING)
return 0;
- log_debug_unit(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
+ log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
if (revents != EPOLLIN) {
- log_error_unit(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
+ log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
UNIT(n)->id, revents);
goto fail;
}
+ busname_peek_message(n);
busname_enter_running(n);
return 0;
fail:
@@ -498,6 +833,89 @@ fail:
return 0;
}
+static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
+ BusName *n = BUSNAME(u);
+ BusNameResult f;
+
+ assert(n);
+ assert(pid >= 0);
+
+ if (pid != n->control_pid)
+ return;
+
+ n->control_pid = 0;
+
+ if (is_clean_exit(code, status, NULL))
+ f = BUSNAME_SUCCESS;
+ else if (code == CLD_EXITED)
+ f = BUSNAME_FAILURE_EXIT_CODE;
+ else if (code == CLD_KILLED)
+ f = BUSNAME_FAILURE_SIGNAL;
+ else if (code == CLD_DUMPED)
+ f = BUSNAME_FAILURE_CORE_DUMP;
+ else
+ assert_not_reached("Unknown sigchld code");
+
+ log_unit_full(u->id,
+ f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
+ "%s control process exited, code=%s status=%i",
+ u->id, sigchld_code_to_string(code), status);
+
+ if (f != BUSNAME_SUCCESS)
+ n->result = f;
+
+ switch (n->state) {
+
+ case BUSNAME_MAKING:
+ if (f == BUSNAME_SUCCESS)
+ busname_enter_listening(n);
+ 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 */
+ unit_add_to_dbus_queue(u);
+}
+
+static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
+ BusName *n = BUSNAME(userdata);
+
+ assert(n);
+ assert(n->timer_event_source == source);
+
+ switch (n->state) {
+
+ case BUSNAME_MAKING:
+ log_unit_warning(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id);
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ case BUSNAME_SIGTERM:
+ log_unit_warning(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id);
+ busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ case BUSNAME_SIGKILL:
+ log_unit_warning(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id);
+ busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ default:
+ assert_not_reached("Timeout at wrong time.");
+ }
+
+ return 0;
+}
+
static void busname_reset_failed(Unit *u) {
BusName *n = BUSNAME(u);
@@ -534,11 +952,33 @@ static void busname_trigger_notify(Unit *u, Unit *other) {
busname_enter_listening(n);
}
+static int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
+ return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
+}
+
+static int busname_get_timeout(Unit *u, uint64_t *timeout) {
+ BusName *n = BUSNAME(u);
+ int r;
+
+ if (!n->timer_event_source)
+ return 0;
+
+ r = sd_event_source_get_time(n->timer_event_source, timeout);
+ if (r < 0)
+ return r;
+
+ return 1;
+}
+
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_FAILED] = "failed"
+ [BUSNAME_SIGTERM] = "sigterm",
+ [BUSNAME_SIGKILL] = "sigkill",
+ [BUSNAME_FAILED] = "failed",
};
DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
@@ -546,19 +986,15 @@ DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
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",
};
DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
-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",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(busname_policy_access, BusNamePolicyAccess);
-
const UnitVTable busname_vtable = {
.object_size = sizeof(BusName),
@@ -579,12 +1015,18 @@ const UnitVTable busname_vtable = {
.start = busname_start,
.stop = busname_stop,
+ .kill = busname_kill,
+
+ .get_timeout = busname_get_timeout,
+
.serialize = busname_serialize,
.deserialize_item = busname_deserialize_item,
.active_state = busname_active_state,
.sub_state_to_string = busname_sub_state_to_string,
+ .sigchld_event = busname_sigchld_event,
+
.trigger_notify = busname_trigger_notify,
.reset_failed = busname_reset_failed,