chiark / gitweb /
udev: set default rules permissions only at "add" events
[elogind.git] / src / core / service.c
index ebd0baea8961b84ddaa5073e9efa714d12a70966..914146383ca50c1b21ed2ce4b8d16288fbbbc080 100644 (file)
@@ -1521,6 +1521,9 @@ static void service_set_state(Service *s, ServiceState state) {
                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
         }
 
+        if (state == SERVICE_FAILED)
+                service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
+
         if (state == SERVICE_DEAD ||
             state == SERVICE_STOP ||
             state == SERVICE_STOP_SIGTERM ||
@@ -1528,7 +1531,6 @@ static void service_set_state(Service *s, ServiceState state) {
             state == SERVICE_STOP_POST ||
             state == SERVICE_FINAL_SIGTERM ||
             state == SERVICE_FINAL_SIGKILL ||
-            state == SERVICE_FAILED ||
             state == SERVICE_AUTO_RESTART)
                 service_notify_sockets_dead(s, false);
 
@@ -1548,7 +1550,7 @@ static void service_set_state(Service *s, ServiceState state) {
                 service_connection_unref(s);
         }
 
-        if (state == SERVICE_STOP)
+        if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
                 service_stop_watchdog(s);
 
         /* For the inactive states unit_notify() will trim the cgroup,
@@ -1953,64 +1955,23 @@ fail:
 
 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
         int r;
-        Set *pid_set = NULL;
-        bool wait_for_exit = false;
 
         assert(s);
 
         if (f != SERVICE_SUCCESS)
                 s->result = f;
 
-        if (s->kill_context.kill_mode != KILL_NONE) {
-                int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
-
-                if (s->main_pid > 0) {
-                        if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
-                                log_warning_unit(UNIT(s)->id,
-                                                 "Failed to kill main process %li: %m", (long) s->main_pid);
-                        else
-                                wait_for_exit = !s->main_pid_alien;
-                }
-
-                if (s->control_pid > 0) {
-                        if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
-                                log_warning_unit(UNIT(s)->id,
-                                                 "Failed to kill control process %li: %m", (long) s->control_pid);
-                        else
-                                wait_for_exit = true;
-                }
-
-                if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
-
-                        pid_set = set_new(trivial_hash_func, trivial_compare_func);
-                        if (!pid_set) {
-                                r = -ENOMEM;
-                                goto fail;
-                        }
-
-                        /* Exclude the main/control pids from being killed via the cgroup */
-                        if (s->main_pid > 0)
-                                if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
-                                        goto fail;
-
-                        if (s->control_pid > 0)
-                                if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
-                                        goto fail;
-
-                        r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
-                        if (r < 0) {
-                                if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
-                                        log_warning_unit(UNIT(s)->id,
-                                                         "Failed to kill control group: %s", strerror(-r));
-                        } else if (r > 0)
-                                wait_for_exit = true;
-
-                        set_free(pid_set);
-                        pid_set = NULL;
-                }
-        }
+        r = unit_kill_context(
+                        UNIT(s),
+                        &s->kill_context,
+                        state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
+                        s->main_pid,
+                        s->control_pid,
+                        s->main_pid_alien);
+        if (r < 0)
+                goto fail;
 
-        if (wait_for_exit) {
+        if (r > 0) {
                 if (s->timeout_stop_usec > 0) {
                         r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
                         if (r < 0)
@@ -2033,9 +1994,6 @@ fail:
                 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
         else
                 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
-
-        if (pid_set)
-                set_free(pid_set);
 }
 
 static void service_enter_stop(Service *s, ServiceResult f) {
@@ -2937,13 +2895,13 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                                 f = SERVICE_SUCCESS;
                 }
 
-                log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
+                log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
+                           u->id,
                            "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
                                   u->id, sigchld_code_to_string(code), status,
                                   strna(code == CLD_EXITED
                                         ? exit_status_to_string(status, EXIT_STATUS_FULL)
                                         : signal_to_string(status)),
-                           "UNIT=%s", u->id,
                            "EXIT_CODE=%s", sigchld_code_to_string(code),
                            "EXIT_STATUS=%i", status,
                            NULL);
@@ -3398,7 +3356,8 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
         if (strv_find(tags, "WATCHDOG=1")) {
                 log_debug_unit(u->id,
                                "%s: got WATCHDOG=1", u->id);
-                service_reset_watchdog(s);
+                if (dual_timestamp_is_set(&s->watchdog_timestamp))
+                        service_reset_watchdog(s);
         }
 
         /* Notify clients about changed status or main pid */
@@ -3830,13 +3789,15 @@ DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
 
 const UnitVTable service_vtable = {
         .object_size = sizeof(Service),
-        .exec_context_offset = offsetof(Service, exec_context),
 
         .sections =
                 "Unit\0"
                 "Service\0"
                 "Install\0",
 
+        .exec_context_offset = offsetof(Service, exec_context),
+        .exec_section = "Service",
+
         .init = service_init,
         .done = service_done,
         .load = service_load,