chiark / gitweb /
service: make sure to pass TERM=linux to all sysv scripts
[elogind.git] / src / service.c
index 310aa28c0c64fce036ee64e2533064f055cf7a24..dac4adaa6a0efc2cfb1c18fa38349517da97870e 100644 (file)
@@ -65,7 +65,7 @@ static const struct {
         { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 #endif
 
-#ifdef TARGET_DEBIAN
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
         /* Debian style rcS.d */
         { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 #endif
@@ -274,7 +274,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
                 "time",                 SPECIAL_RTC_SET_TARGET,
 
                 /* Debian extensions */
-#ifdef TARGET_DEBIAN
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
                 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
 #endif
                 "mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
@@ -316,7 +316,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
         }
 
         /* If we don't know this name, fallback heuristics to figure
-         * out whether something is a target or an service alias. */
+         * out whether something is a target or a service alias. */
 
         if (*name == '$')
                 /* Facilities starting with $ are most likely targets */
@@ -1568,7 +1568,7 @@ static int service_spawn(
                 goto fail;
         }
 
-        if (!(our_env = new0(char*, 3))) {
+        if (!(our_env = new0(char*, 4))) {
                 r = -ENOMEM;
                 goto fail;
         }
@@ -1585,6 +1585,14 @@ static int service_spawn(
                         goto fail;
                 }
 
+        /* Make sure we set TERM=linux for SysV scripts, since some
+         * require it to be set from the kernel */
+        if (s->sysv_path && !strv_env_get(s->meta.manager->environment, "TERM"))
+                if (!(our_env[n_env++] = strdup("TERM=linux"))) {
+                        r = -ENOMEM;
+                        goto fail;
+                }
+
         if (!(final_env = strv_env_merge(2,
                                          s->meta.manager->environment,
                                          our_env,
@@ -3145,6 +3153,62 @@ static void service_reset_failed(Unit *u) {
         s->failure = false;
 }
 
+static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
+        Service *s = SERVICE(u);
+        int r = 0;
+        Set *pid_set = NULL;
+
+        assert(s);
+
+        if (s->main_pid <= 0 && who == KILL_MAIN) {
+                dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
+                return -EINVAL;
+        }
+
+        if (s->control_pid <= 0 && who == KILL_CONTROL) {
+                dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
+                return -ENOENT;
+        }
+
+        if (s->control_pid > 0)
+                if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
+                        r = -errno;
+
+        if (s->main_pid > 0)
+                if (kill(mode == KILL_PROCESS_GROUP ? -s->main_pid : s->main_pid, signo) < 0)
+                        r = -errno;
+
+        if (mode == KILL_CONTROL_GROUP) {
+                int q;
+
+                if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
+                        return -ENOMEM;
+
+                /* Exclude the control/main pid from being killed via the cgroup */
+                if (s->control_pid > 0)
+                        if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
+                                r = q;
+                                goto finish;
+                        }
+
+                if (s->main_pid > 0)
+                        if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
+                                r = q;
+                                goto finish;
+                        }
+
+                if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0)
+                        if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
+                                r = q;
+        }
+
+finish:
+        if (pid_set)
+                set_free(pid_set);
+
+        return r;
+}
+
 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
         [SERVICE_DEAD] = "dead",
         [SERVICE_START_PRE] = "start-pre",
@@ -3222,6 +3286,8 @@ const UnitVTable service_vtable = {
 
         .can_reload = service_can_reload,
 
+        .kill = service_kill,
+
         .serialize = service_serialize,
         .deserialize_item = service_deserialize_item,