[SERVICE_DEAD] = "dead",
[SERVICE_START_PRE] = "start-pre",
[SERVICE_START] = "start",
- [SERVICE_START_POST] = "post",
+ [SERVICE_START_POST] = "start-post",
[SERVICE_RUNNING] = "running",
[SERVICE_RELOAD] = "reload",
[SERVICE_STOP] = "stop",
s->state = SERVICE_DEAD;
- /* Load a .service file */
- r = unit_load_fragment(u);
-
- /* Load a classic init script as a fallback */
- if (r == -ENOENT)
- r = service_load_sysv(s);
+ RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
- if (r < 0) {
+ /* Load a .service file */
+ if ((r = unit_load_fragment(u)) < 0) {
service_done(u);
return r;
}
+ /* Load a classic init script as a fallback, if we couldn*t find anything */
+ if (r == 0)
+ if ((r = service_load_sysv(s)) <= 0) {
+ service_done(u);
+ return r < 0 ? r : -ENOENT;
+ }
+
/* Load dropin directory data */
if ((r = unit_load_dropin(u)) < 0) {
service_done(u);
return 0;
}
+static int service_get_sockets(Service *s, Set **_set) {
+ Set *set;
+ Iterator i;
+ char *t;
+ int r;
+
+ assert(s);
+ assert(_set);
+
+ /* Collects all Socket objects that belong to this
+ * service. Note that a service might have multiple sockets
+ * via multiple names. */
+
+ if (!(set = set_new(NULL, NULL)))
+ return -ENOMEM;
+
+ SET_FOREACH(t, UNIT(s)->meta.names, i) {
+ char *k;
+ Unit *p;
+
+ /* Look for all socket objects that go by any of our
+ * units and collect their fds */
+
+ if (!(k = unit_name_change_suffix(t, ".socket"))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ p = manager_get_unit(UNIT(s)->meta.manager, k);
+ free(k);
+
+ if (!p) continue;
+
+ if ((r = set_put(set, p)) < 0)
+ goto fail;
+ }
+
+ *_set = set;
+ return 0;
+
+fail:
+ set_free(set);
+ return r;
+}
+
+
+static int service_notify_sockets(Service *s) {
+ Iterator i;
+ Set *set;
+ Socket *socket;
+ int r;
+
+ assert(s);
+
+ /* Notifies all our sockets when we die */
+
+ if ((r = service_get_sockets(s, &set)) < 0)
+ return r;
+
+ SET_FOREACH(socket, set, i)
+ socket_notify_service_dead(socket);
+
+ set_free(set);
+
+ return 0;
+}
+
static void service_set_state(Service *s, ServiceState state) {
ServiceState old_state;
assert(s);
state != SERVICE_STOP_POST)
s->control_command = NULL;
- log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
+ if (state == SERVICE_DEAD ||
+ state == SERVICE_STOP ||
+ state == SERVICE_STOP_SIGTERM ||
+ state == SERVICE_STOP_SIGKILL ||
+ state == SERVICE_STOP_POST ||
+ state == SERVICE_FINAL_SIGTERM ||
+ state == SERVICE_FINAL_SIGKILL ||
+ state == SERVICE_MAINTAINANCE ||
+ state == SERVICE_AUTO_RESTART)
+ service_notify_sockets(s);
+
+ log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
}
int r;
int *rfds = NULL;
unsigned rn_fds = 0;
- char *t;
+ Set *set;
+ Socket *socket;
assert(s);
assert(fds);
assert(n_fds);
- SET_FOREACH(t, UNIT(s)->meta.names, i) {
- char *k;
- Unit *p;
+ if ((r = service_get_sockets(s, &set)) < 0)
+ return r;
+
+ SET_FOREACH(socket, set, i) {
int *cfds;
unsigned cn_fds;
- /* Look for all socket objects that go by any of our
- * units and collect their fds */
-
- if (!(k = unit_name_change_suffix(t, ".socket"))) {
- r = -ENOMEM;
- goto fail;
- }
-
- p = manager_get_unit(UNIT(s)->meta.manager, k);
- free(k);
-
- if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
+ if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
goto fail;
if (!cfds)
*fds = rfds;
*n_fds = rn_fds;
+
+ set_free(set);
+
return 0;
fail:
+ set_free(set);
free(rfds);
+
return r;
}
if (!success)
s->failure = true;
- if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
-
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
goto fail;
- service_set_state(s, SERVICE_STOP_POST);
- } else
+
+ service_set_state(s, SERVICE_STOP_POST);
+
+ if (!s->control_command)
service_enter_dead(s, true, true);
return;
if (r < 0)
goto fail;
+ }
- service_set_state(s, state);
- } else
+ service_set_state(s, state);
+
+ if (s->main_pid <= 0 && s->control_pid <= 0)
service_enter_dead(s, true, true);
return;
if (!success)
s->failure = true;
- if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
-
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
goto fail;
- service_set_state(s, SERVICE_STOP);
- } else
+ service_set_state(s, SERVICE_STOP);
+
+ if (!s->control_command)
service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
return;
int r;
assert(s);
- if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
-
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
goto fail;
- service_set_state(s, SERVICE_START_POST);
- } else
+
+ service_set_state(s, SERVICE_START_POST);
+
+ if (!s->control_command)
service_set_state(s, SERVICE_RUNNING);
return;
if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
goto fail;
+ service_set_state(s, SERVICE_START);
+
if (s->type == SERVICE_SIMPLE) {
/* For simple services we immediately start
* the START_POST binaries. */
s->control_pid = pid;
s->control_command = s->exec_command[SERVICE_EXEC_START];
- service_set_state(s, SERVICE_START);
} else
assert_not_reached("Unknown service type");
assert(s);
- if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
-
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
goto fail;
- service_set_state(s, SERVICE_START_PRE);
- } else
+ service_set_state(s, SERVICE_START_PRE);
+
+ if (!s->control_command)
service_enter_start(s);
return;
assert(s);
- if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
-
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
goto fail;
- service_set_state(s, SERVICE_RELOAD);
- } else
+ service_set_state(s, SERVICE_RELOAD);
+
+ if (!s->control_command)
service_set_state(s, SERVICE_RUNNING);
return;
assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
+ /* Make sure we don't enter a busy loop of some kind. */
+ if (!ratelimit_test(&s->ratelimit)) {
+ log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
+ return -EAGAIN;
+ }
+
s->failure = false;
s->main_pid_known = false;
assert(s);
assert(pid >= 0);
- success = code == CLD_EXITED || status == 0;
+ success = code == CLD_EXITED && status == 0;
s->failure = s->failure || !success;
if (s->main_pid == pid) {
/* No further commands for this step, so let's
* figure out what to do next */
+ log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
+
switch (s->state) {
case SERVICE_START_PRE: