chiark / gitweb /
service: make start jobs wait, not fail, when an automatic restart is queued
[elogind.git] / src / core / service.c
index 936fee2636320f98705d1d42e1fa34698c228186..cf480ab979d3be55cea1a0e7b880a2c936b140a3 100644 (file)
@@ -722,7 +722,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                 char *d = NULL;
 
                                 if (chkconfig_description)
-                                        d = join(chkconfig_description, " ", j, NULL);
+                                        d = strjoin(chkconfig_description, " ", j, NULL);
                                 else
                                         d = strdup(j);
 
@@ -879,7 +879,7 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                                 char *d = NULL;
 
                                                 if (long_description)
-                                                        d = join(long_description, " ", t, NULL);
+                                                        d = strjoin(long_description, " ", t, NULL);
                                                 else
                                                         d = strdup(j);
 
@@ -928,10 +928,6 @@ static int service_load_sysv_path(Service *s, const char *path) {
         s->guess_main_pid = false;
         s->restart = SERVICE_RESTART_NO;
         s->exec_context.ignore_sigpipe = false;
-
-        if (UNIT(s)->manager->sysv_console)
-                s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
-
         s->exec_context.kill_mode = KILL_PROCESS;
 
         /* We use the long description only if
@@ -1005,7 +1001,7 @@ static int service_load_sysv_name(Service *s, const char *name) {
                 char *path;
                 int r;
 
-                path = join(*p, "/", name, NULL);
+                path = strjoin(*p, "/", name, NULL);
                 if (!path)
                         return -ENOMEM;
 
@@ -1027,7 +1023,7 @@ static int service_load_sysv_name(Service *s, const char *name) {
                 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
                         /* Try SUSE style boot.* init scripts */
 
-                        path = join(*p, "/boot.", name, NULL);
+                        path = strjoin(*p, "/boot.", name, NULL);
                         if (!path)
                                 return -ENOMEM;
 
@@ -1042,7 +1038,7 @@ static int service_load_sysv_name(Service *s, const char *name) {
                 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
                         /* Try Frugalware style rc.* init scripts */
 
-                        path = join(*p, "/rc.", name, NULL);
+                        path = strjoin(*p, "/rc.", name, NULL);
                         if (!path)
                                 return -ENOMEM;
 
@@ -1249,6 +1245,10 @@ static int service_load(Unit *u) {
                 if (s->type == _SERVICE_TYPE_INVALID)
                         s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
 
+                /* Oneshot services have disabled timeout by default */
+                if (s->type == SERVICE_ONESHOT && !s->timeout_defined)
+                        s->timeout_usec = 0;
+
                 service_fix_output(s);
 
                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
@@ -1282,6 +1282,10 @@ static int service_load(Unit *u) {
                 if (UNIT(s)->default_dependencies)
                         if ((r = service_add_default_dependencies(s)) < 0)
                                 return r;
+
+                r = unit_patch_working_directory(UNIT(s), &s->exec_context);
+                if (r < 0)
+                        return r;
         }
 
         return service_verify(s);
@@ -1806,6 +1810,7 @@ static int service_spawn(
                        UNIT(s)->cgroup_bondings,
                        UNIT(s)->cgroup_attributes,
                        is_control ? "control" : NULL,
+                       UNIT(s)->id,
                        s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
                        &pid);
 
@@ -1887,6 +1892,8 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
         if (f != SERVICE_SUCCESS)
                 s->result = f;
 
+        service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
+
         if (allow_restart &&
             !s->forbid_restart &&
             (s->restart == SERVICE_RESTART_ALWAYS ||
@@ -1900,8 +1907,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
                         goto fail;
 
                 service_set_state(s, SERVICE_AUTO_RESTART);
-        } else
-                service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
+        }
 
         s->forbid_restart = false;
 
@@ -2157,7 +2163,7 @@ static void service_enter_start(Service *s) {
 
         r = service_spawn(s,
                           c,
-                          s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
+                          s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
                           true,
                           true,
                           true,
@@ -2252,10 +2258,12 @@ static void service_enter_restart(Service *s) {
         assert(s);
         dbus_error_init(&error);
 
-        if (UNIT(s)->job) {
-                log_info("Job pending for unit, delaying automatic restart.");
+        if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
+                /* Don't restart things if we are going down anyway */
+                log_info("Stop job pending for unit, delaying automatic restart.");
 
-                if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
+                r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
+                if (r < 0)
                         goto fail;
 
                 return;
@@ -2269,6 +2277,10 @@ static void service_enter_restart(Service *s) {
         if (r < 0)
                 goto fail;
 
+        /* Note that we stay in the SERVICE_AUTO_RESTART state here,
+         * it will be canceled as part of the service_stop() call that
+         * is executed as part of JOB_RESTART. */
+
         log_debug("%s scheduled restart job.", UNIT(s)->id);
         return;
 
@@ -2372,7 +2384,7 @@ static void service_run_next_main(Service *s) {
 
         r = service_spawn(s,
                           s->main_command,
-                          false,
+                          true,
                           true,
                           true,
                           true,
@@ -2462,12 +2474,12 @@ static int service_start(Unit *u) {
                 return 0;
 
         /* A service that will be restarted must be stopped first to
-         * trigger BindTo and/or OnFailure dependencies. If a user
+         * trigger BindsTo and/or OnFailure dependencies. If a user
          * does not want to wait for the holdoff time to elapse, the
          * service should be manually restarted, not started. */
         if (s->state == SERVICE_AUTO_RESTART) {
                 log_warning("%s automatic restart is pending, must be stopped before issuing start request.", UNIT(s)->id);
-                return -ECANCELED;
+                return -EAGAIN;
         }
 
         assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
@@ -2508,7 +2520,7 @@ static int service_stop(Unit *u) {
 
         /* A restart will be scheduled or is in progress. */
         if (s->state == SERVICE_AUTO_RESTART) {
-                service_enter_dead(s, SERVICE_SUCCESS, false);
+                service_set_state(s, SERVICE_DEAD);
                 return 0;
         }
 
@@ -3226,7 +3238,7 @@ static void service_cgroup_notify_event(Unit *u) {
         case SERVICE_FINAL_SIGTERM:
         case SERVICE_FINAL_SIGKILL:
                 if (main_pid_good(s) <= 0 && !control_pid_good(s))
-                        service_enter_dead(s, SERVICE_SUCCESS, SERVICE_SUCCESS);
+                        service_enter_dead(s, SERVICE_SUCCESS, true);
 
                 break;
 
@@ -3405,7 +3417,7 @@ static int service_enumerate(Manager *m) {
                         struct dirent *de;
 
                         free(path);
-                        path = join(*p, "/", rcnd_table[i].path, NULL);
+                        path = strjoin(*p, "/", rcnd_table[i].path, NULL);
                         if (!path) {
                                 r = -ENOMEM;
                                 goto finish;
@@ -3440,7 +3452,7 @@ static int service_enumerate(Manager *m) {
                                         continue;
 
                                 free(fpath);
-                                fpath = join(path, "/", de->d_name, NULL);
+                                fpath = strjoin(path, "/", de->d_name, NULL);
                                 if (!fpath) {
                                         r = -ENOMEM;
                                         goto finish;
@@ -3653,6 +3665,8 @@ static void service_reset_failed(Unit *u) {
 
         s->result = SERVICE_SUCCESS;
         s->reload_result = SERVICE_SUCCESS;
+
+        RATELIMIT_RESET(s->start_limit);
 }
 
 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
@@ -3794,7 +3808,6 @@ static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
 DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
 
 const UnitVTable service_vtable = {
-        .suffix = ".service",
         .object_size = sizeof(Service),
         .sections =
                 "Unit\0"