chiark / gitweb /
systemctl: prefix list-units and list-machines output with a circle indicating a...
[elogind.git] / src / core / service.c
index 8e07bd0170c187513812185b1e21dac8fa7f41ba..386692a10aaa81c3a9bc3e837b04e9a4768c911a 100644 (file)
@@ -147,9 +147,9 @@ static void service_init(Unit *u) {
         kill_context_init(&s->kill_context);
         cgroup_context_init(&s->cgroup_context);
 
-        RATELIMIT_INIT(s->start_limit,
-                       u->manager->default_start_limit_interval,
-                       u->manager->default_start_limit_burst);
+        unit_cgroup_context_init_defaults(u, &s->cgroup_context);
+
+        RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
 
         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
 }
@@ -264,7 +264,7 @@ static void service_start_watchdog(Service *s) {
 
                 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
         } else {
-                r = sd_event_add_monotonic(UNIT(s)->manager->event, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s, &s->watchdog_event_source);
+                r = sd_event_add_monotonic(UNIT(s)->manager->event, &s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, service_dispatch_watchdog, s);
                 if (r < 0) {
                         log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r));
                         return;
@@ -354,14 +354,14 @@ static int service_arm_timer(Service *s, usec_t usec) {
                 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
         }
 
-        return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s, &s->timer_event_source);
+        return sd_event_add_monotonic(UNIT(s)->manager->event, &s->timer_event_source, now(CLOCK_MONOTONIC) + usec, 0, service_dispatch_timer, s);
 }
 
 #ifdef HAVE_SYSV_COMPAT
 static char *sysv_translate_name(const char *name) {
         char *r;
 
-        r = new(char, strlen(name) + sizeof(".service"));
+        r = new(char, strlen(name) + strlen(".service") + 1);
         if (!r)
                 return NULL;
 
@@ -1235,7 +1235,7 @@ static int service_load(Unit *u) {
                                 return r;
                 }
 
-                r = unit_exec_context_defaults(u, &s->exec_context);
+                r = unit_exec_context_patch_defaults(u, &s->exec_context);
                 if (r < 0)
                         return r;
         }
@@ -1368,25 +1368,10 @@ static int service_load_pid_file(Service *s, bool may_warn) {
                 return r;
         }
 
-        if (kill(pid, 0) < 0 && errno != EPERM) {
+        if (!pid_is_alive(pid)) {
                 if (may_warn)
-                        log_info_unit(UNIT(s)->id,
-                                      "PID "PID_FMT" read from file %s does not exist.",
-                                      pid, s->pid_file);
-                return -ESRCH;
-        }
+                        log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
 
-        r = get_process_state(pid);
-        if (r < 0) {
-                if (may_warn)
-                        log_info_unit(UNIT(s)->id, "Failed to read /proc/%d/stat: %s",
-                                      pid, strerror(-r));
-                return r;
-        } else if (r == 'Z') {
-                if (may_warn)
-                        log_info_unit(UNIT(s)->id,
-                                      "PID "PID_FMT" read from file %s is a zombie.",
-                                      pid, s->pid_file);
                 return -ESRCH;
         }
 
@@ -1451,9 +1436,7 @@ static int service_search_main_pid(Service *s) {
                 log_warning_unit(UNIT(s)->id,
                                  "Failed to watch PID "PID_FMT" from service %s",
                                  pid, UNIT(s)->id);
-                return r;
-
-        return 0;
+        return r;
 }
 
 static void service_set_state(Service *s, ServiceState state) {
@@ -1579,7 +1562,7 @@ static int service_coldplug(Unit *u) {
                                 return r;
                 }
 
-                if (pid_valid(s->main_pid) &&
+                if (pid_is_unwaited(s->main_pid) &&
                     ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
                      IN_SET(s->deserialized_state,
                             SERVICE_START, SERVICE_START_POST,
@@ -1592,7 +1575,7 @@ static int service_coldplug(Unit *u) {
                                 return r;
                 }
 
-                if (pid_valid(s->control_pid) &&
+                if (pid_is_unwaited(s->control_pid) &&
                     IN_SET(s->deserialized_state,
                            SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
                            SERVICE_RELOAD,
@@ -1787,6 +1770,7 @@ static int service_spawn(
                        UNIT(s)->manager->confirm_spawn,
                        UNIT(s)->manager->cgroup_supported,
                        path,
+                       manager_get_runtime_prefix(UNIT(s)->manager),
                        UNIT(s)->id,
                        s->watchdog_usec,
                        s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
@@ -1824,7 +1808,7 @@ static int main_pid_good(Service *s) {
                 /* If it's an alien child let's check if it is still
                  * alive ... */
                 if (s->main_pid_alien && s->main_pid > 0)
-                        return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
+                        return pid_is_alive(s->main_pid);
 
                 /* .. otherwise assume we'll get a SIGCHLD for it,
                  * which we really should wait for to collect exit
@@ -1888,10 +1872,13 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
 
         s->forbid_restart = false;
 
-        /* we want fresh tmpdirs in case service is started again immediately */
+        /* We want fresh tmpdirs in case service is started again immediately */
         exec_runtime_destroy(s->exec_runtime);
         s->exec_runtime = exec_runtime_unref(s->exec_runtime);
 
+        /* Also, remove the runtime directory in */
+        exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
+
         /* Try to delete the pid file. At this point it will be
          * out-of-date, and some software might be confused by it, so
          * let's remove it. */
@@ -3323,7 +3310,10 @@ static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *us
 
         case SERVICE_AUTO_RESTART:
                 log_info_unit(UNIT(s)->id,
-                              "%s holdoff time over, scheduling restart.", UNIT(s)->id);
+                              s->restart_usec > 0 ?
+                              "%s holdoff time over, scheduling restart." :
+                              "%s has no holdoff time, scheduling restart.",
+                              UNIT(s)->id);
                 service_enter_restart(s);
                 break;
 
@@ -3426,7 +3416,7 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
                         free(t);
         }
 
-        /* Interpet WATCHDOG= */
+        /* Interpret WATCHDOG= */
         if (strv_find(tags, "WATCHDOG=1")) {
                 log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id);
                 service_reset_watchdog(s);