chiark / gitweb /
execute: make sending of SIGKILL on shutdown optional
[elogind.git] / src / service.c
index d3c750e5441b7def316c34105f4bf54f195800bf..e765004e4fe4434df26f0d66aa7472cfef74e960 100644 (file)
@@ -65,7 +65,7 @@ static const struct {
         { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 #endif
 
-#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_FRUGALWARE)
         /* Debian style rcS.d */
         { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
 #endif
@@ -237,16 +237,20 @@ static char *sysv_translate_name(const char *name) {
         if (!(r = new(char, strlen(name) + sizeof(".service"))))
                 return NULL;
 
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
+        if (endswith(name, ".sh"))
+                /* Drop Debian-style .sh suffix */
+                strcpy(stpcpy(r, name) - 3, ".service");
+#endif
+#ifdef TARGET_SUSE
         if (startswith(name, "boot."))
                 /* Drop SuSE-style boot. prefix */
                 strcpy(stpcpy(r, name + 5), ".service");
-        else if (endswith(name, ".sh"))
-                /* Drop Debian-style .sh suffix */
-                strcpy(stpcpy(r, name) - 3, ".service");
-#ifdef TARGET_ARCH
-        else if (startswith(name, "@"))
-                /* Drop Arch-style background prefix */
-                strcpy(stpcpy(r, name + 1), ".service");
+#endif
+#ifdef TARGET_FRUGALWARE
+        if (startswith(name, "rc."))
+                /* Drop Frugalware-style rc. prefix */
+                strcpy(stpcpy(r, name + 3), ".service");
 #endif
         else
                 /* Normal init scripts */
@@ -273,23 +277,24 @@ static int sysv_translate_facility(const char *name, const char *filename, char
                 "syslog",               SPECIAL_SYSLOG_TARGET,
                 "time",                 SPECIAL_RTC_SET_TARGET,
 
-                /* Debian extensions */
+                /* common extensions */
+                "mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
+                "x-display-manager",    SPECIAL_DISPLAY_MANAGER_SERVICE,
+                "null",                 NULL,
+
 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
                 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
 #endif
-                "mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
-                "x-display-manager",    SPECIAL_DISPLAY_MANAGER_SERVICE,
 
 #ifdef TARGET_FEDORA
-                /* Fedora extensions */
                 "MTA",                  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
                 "smtpdaemon",           SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
                 "httpd",                SPECIAL_HTTP_DAEMON_TARGET,
 #endif
 
-                /* SuSE extensions */
-                "null",                 NULL
-
+#ifdef TARGET_SUSE
+                "smtp",                 SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
+#endif
         };
 
         unsigned i;
@@ -318,10 +323,13 @@ 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 a service alias. */
 
-        if (*name == '$')
+        if (*name == '$') {
+                if (!unit_prefix_is_valid(n))
+                        return -EINVAL;
+
                 /* Facilities starting with $ are most likely targets */
                 r = unit_name_build(n, NULL, ".target");
-        else if (filename && streq(name, filename))
+        else if (filename && streq(name, filename))
                 /* Names equalling the file name of the services are redundant */
                 return 0;
         else
@@ -674,10 +682,14 @@ static int service_load_sysv_path(Service *s, const char *path) {
                                         }
 
                                         r = sysv_translate_facility(n, file_name_from_path(path), &m);
-                                        free(n);
 
-                                        if (r < 0)
-                                                goto finish;
+                                        if (r < 0) {
+                                                log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
+                                                free(n);
+                                                continue;
+                                        }
+
+                                        free(n);
 
                                         if (r == 0)
                                                 continue;
@@ -849,11 +861,22 @@ static int service_load_sysv_name(Service *s, const char *name) {
         assert(s);
         assert(name);
 
-        /* For SysV services we strip the boot. or .sh
+        /* For SysV services we strip the boot.*, rc.* and *.sh
          * prefixes/suffixes. */
-        if (startswith(name, "boot.") ||
-            endswith(name, ".sh.service"))
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
+        if (endswith(name, ".sh.service"))
+                return -ENOENT;
+#endif
+
+#ifdef TARGET_SUSE
+        if (startswith(name, "boot."))
                 return -ENOENT;
+#endif
+
+#ifdef TARGET_FRUGALWARE
+        if (startswith(name, "rc."))
+                return -ENOENT;
+#endif
 
         STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
                 char *path;
@@ -867,24 +890,42 @@ static int service_load_sysv_name(Service *s, const char *name) {
 
                 r = service_load_sysv_path(s, path);
 
+#if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
-                        /* Try Debian style xxx.sh source'able init scripts */
+                        /* Try Debian style *.sh source'able init scripts */
                         strcat(path, ".sh");
                         r = service_load_sysv_path(s, path);
                 }
-
+#endif
                 free(path);
 
+#ifdef TARGET_SUSE
                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
-                        /* Try SUSE style boot.xxx init scripts */
+                        /* Try SUSE style boot.* init scripts */
 
                         if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
                                 return -ENOMEM;
 
+                        /* Drop .service suffix */
                         path[strlen(path)-8] = 0;
                         r = service_load_sysv_path(s, path);
                         free(path);
                 }
+#endif
+
+#ifdef TARGET_FRUGALWARE
+                if (r >= 0 && s->meta.load_state == UNIT_STUB) {
+                        /* Try Frugalware style rc.* init scripts */
+
+                        if (asprintf(&path, "%s/rc.%s", *p, name) < 0)
+                                return -ENOMEM;
+
+                        /* Drop .service suffix */
+                        path[strlen(path)-8] = 0;
+                        r = service_load_sysv_path(s, path);
+                        free(path);
+                }
+#endif
 
                 if (r < 0)
                         return r;
@@ -1014,7 +1055,7 @@ static int service_add_default_dependencies(Service *s) {
                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
                         return r;
 
-        } else if (s->meta.manager->running_as == MANAGER_SESSION) {
+        } else if (s->meta.manager->running_as == MANAGER_USER) {
 
                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
                         return r;
@@ -1055,7 +1096,7 @@ static int service_load(Unit *u) {
                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
                         return r;
 
-                if ((r = unit_add_default_cgroup(u)) < 0)
+                if ((r = unit_add_default_cgroups(u)) < 0)
                         return r;
 
 #ifdef HAVE_SYSV_COMPAT
@@ -1609,16 +1650,6 @@ static int service_spawn(
                         goto fail;
                 }
 
-#ifdef HAVE_SYSV_COMPAT
-        /* 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;
-                }
-#endif
-
         if (!(final_env = strv_env_merge(2,
                                          s->meta.manager->environment,
                                          our_env,
@@ -2039,6 +2070,13 @@ static void service_enter_restart(Service *s) {
         assert(s);
         dbus_error_init(&error);
 
+        if (s->meta.job) {
+                log_info("Job pending for unit, delaying automatic restart.");
+
+                if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
+                        goto fail;
+        }
+
         service_enter_dead(s, true, false);
 
         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
@@ -2674,8 +2712,14 @@ static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
                 break;
 
         case SERVICE_STOP_SIGTERM:
-                log_warning("%s stopping timed out. Killing.", u->meta.id);
-                service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
+                if (s->exec_context.send_sigkill) {
+                        log_warning("%s stopping timed out. Killing.", u->meta.id);
+                        service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
+                } else {
+                        log_warning("%s stopping timed out. Skipping SIGKILL.", u->meta.id);
+                        service_enter_stop_post(s, false);
+                }
+
                 break;
 
         case SERVICE_STOP_SIGKILL:
@@ -2693,8 +2737,14 @@ static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
                 break;
 
         case SERVICE_FINAL_SIGTERM:
-                log_warning("%s stopping timed out (2). Killing.", u->meta.id);
-                service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
+                if (s->exec_context.send_sigkill) {
+                        log_warning("%s stopping timed out (2). Killing.", u->meta.id);
+                        service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
+                } else {
+                        log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->meta.id);
+                        service_enter_dead(s, false, true);
+                }
+
                 break;
 
         case SERVICE_FINAL_SIGKILL:
@@ -2829,67 +2879,9 @@ static int service_enumerate(Manager *m) {
         Unit *service;
         Iterator j;
         int r;
-#ifdef TARGET_ARCH
-        Unit *previous = NULL;
-        char *arch_daemons = NULL;
-        char *arch_daemons_stripped = NULL;
-        char **arch_daemons_split = NULL;
-#endif
 
         assert(m);
 
-#ifdef TARGET_ARCH
-        if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
-                                "DAEMONS", &arch_daemons,
-                                NULL)) < 0) {
-
-                if (r != -ENOENT)
-                        log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
-
-        } else if (arch_daemons) {
-                if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
-                        arch_daemons_stripped = arch_daemons;
-                else
-                        arch_daemons_stripped++; /* strip start paren */
-
-                arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
-
-                if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
-
-                STRV_FOREACH(p, arch_daemons_split) {
-
-                        free(name);
-                        name = NULL;
-
-                        if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
-                                continue;
-
-                        if (!(name = sysv_translate_name(*p))) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
-
-                        if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
-                                log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
-                                continue;
-                        }
-
-                        if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
-                                goto finish;
-
-                        if (previous)
-                                if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
-                                        goto finish;
-
-                        if (**p != '@') /* daemons prefixed with @ can be started in the background */
-                                previous = service;
-                }
-        }
-#endif
-
         zero(runlevel_services);
 
         STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
@@ -3027,10 +3019,6 @@ finish:
         free(path);
         free(fpath);
         free(name);
-#ifdef TARGET_ARCH
-        free(arch_daemons);
-        free(arch_daemons_split);
-#endif
 
         for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
                 set_free(runlevel_services[i]);