chiark / gitweb /
sysv-generator: Replace Provides: symlinks with real units
[elogind.git] / src / sysv-generator / sysv-generator.c
index 6e4cdd694e5a877957d8e7bb980e531eea861ab0..3279c7f44482ef6f0e2ead2a17b25e4de985df8f 100644 (file)
@@ -112,6 +112,32 @@ static int add_symlink(const char *service, const char *where) {
         return 1;
 }
 
+static int add_alias(const char *service, const char *alias) {
+        _cleanup_free_ char *link = NULL;
+        int r;
+
+        assert(service);
+        assert(alias);
+
+        if (streq(service, alias)) {
+                log_error("Ignoring creation of an alias %s for itself", service);
+                return 0;
+        }
+
+        link = strjoin(arg_dest, "/", alias, NULL);
+        if (!link)
+                return log_oom();
+
+        r = symlink(service, link);
+        if (r < 0) {
+                if (errno == EEXIST)
+                        return 0;
+                return -errno;
+        }
+
+        return 1;
+}
+
 static int generate_unit_file(SysvStub *s) {
         char **p;
         _cleanup_fclose_ FILE *f = NULL;
@@ -121,6 +147,7 @@ static int generate_unit_file(SysvStub *s) {
         _cleanup_free_ char *wants = NULL;
         _cleanup_free_ char *conflicts = NULL;
         int r;
+        struct stat st;
 
         before = strv_join(s->before, " ");
         if (!before)
@@ -142,15 +169,22 @@ static int generate_unit_file(SysvStub *s) {
         if (!unit)
                 return log_oom();
 
-        f = fopen(unit, "wxe");
-        if (!f) {
-                log_error("Failed to create unit file %s: %m", unit);
-                return -errno;
+        /* We might already have a symlink with the same name from a Provides:,
+         * or from backup files like /etc/init.d/foo.bak. Real scripts always win,
+         * so remove an existing link */
+        if (lstat(unit, &st) == 0 && S_ISLNK(st.st_mode)) {
+                log_warning("Overwriting existing symlink %s with real service", unit);
+                unlink(unit);
         }
 
+        f = fopen(unit, "wxe");
+        if (!f)
+                return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
+
         fprintf(f,
                 "# Automatically generated by systemd-sysv-generator\n\n"
                 "[Unit]\n"
+                "Documentation=man:systemd-sysv-generator(8)\n"
                 "SourcePath=%s\n"
                 "Description=%s\n",
                 s->path, s->description);
@@ -175,9 +209,6 @@ static int generate_unit_file(SysvStub *s) {
                 "RemainAfterExit=%s\n",
                 yes_no(!s->pid_file));
 
-        if (s->sysv_start_priority > 0)
-                fprintf(f, "SysVStartPriority=%d\n", s->sysv_start_priority);
-
         if (s->pid_file)
                 fprintf(f, "PIDFile=%s\n", s->pid_file);
 
@@ -192,7 +223,7 @@ static int generate_unit_file(SysvStub *s) {
         STRV_FOREACH(p, s->wanted_by) {
                 r = add_symlink(s->name, *p);
                 if (r < 0)
-                        log_error_unit(s->name, "Failed to create 'Wants' symlink to %s: %s", *p, strerror(-r));
+                        log_unit_error_errno(s->name, r, "Failed to create 'Wants' symlink to %s: %m", *p);
         }
 
         return 0;
@@ -246,6 +277,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
         unsigned i;
         char *r;
         const char *n;
+        _cleanup_free_ char *filename_no_sh = NULL;
 
         assert(name);
         assert(_r);
@@ -267,6 +299,13 @@ static int sysv_translate_facility(const char *name, const char *filename, char
                 goto finish;
         }
 
+        /* strip ".sh" suffix from file name for comparison */
+        filename_no_sh = strdup(filename);
+        if (!filename_no_sh)
+                return -ENOMEM;
+        if (endswith(filename, ".sh"))
+                filename_no_sh[strlen(filename)-3] = '\0';
+
         /* If we don't know this name, fallback heuristics to figure
          * out whether something is a target or a service alias. */
 
@@ -276,7 +315,7 @@ static int sysv_translate_facility(const char *name, const char *filename, char
 
                 /* 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_no_sh))
                 /* Names equaling the file name of the services are redundant */
                 return 0;
         else
@@ -313,6 +352,8 @@ static int load_sysv(SysvStub *s) {
         if (!f)
                 return errno == ENOENT ? 0 : -errno;
 
+        log_debug("Loading SysV script %s", s->path);
+
         while (!feof(f)) {
                 char l[LINE_MAX], *t;
 
@@ -320,7 +361,7 @@ static int load_sysv(SysvStub *s) {
                         if (feof(f))
                                 break;
 
-                        log_error_unit(s->name,
+                        log_unit_error(s->name,
                                        "Failed to read configuration file '%s': %m",
                                        s->path);
                         return -errno;
@@ -395,7 +436,7 @@ static int load_sysv(SysvStub *s) {
 
                                 fn = strstrip(t+8);
                                 if (!path_is_absolute(fn)) {
-                                        log_error_unit(s->name,
+                                        log_unit_error(s->name,
                                                        "[%s:%u] PID file not absolute. Ignoring.",
                                                        s->path, line);
                                         continue;
@@ -461,7 +502,10 @@ static int load_sysv(SysvStub *s) {
                                         if (r == 0)
                                                 continue;
 
-                                        if (unit_name_to_type(m) != UNIT_SERVICE) {
+                                        if (unit_name_to_type(m) == UNIT_SERVICE) {
+                                                log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
+                                                r = add_alias(s->name, m);
+                                        } else {
                                                 /* NB: SysV targets
                                                  * which are provided
                                                  * by a service are
@@ -489,12 +533,12 @@ static int load_sysv(SysvStub *s) {
                                         }
 
                                         if (r < 0)
-                                                log_error_unit(s->name,
+                                                log_unit_error(s->name,
                                                                "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
                                                                s->path, line, m, strerror(-r));
                                 }
                                 if (!isempty(state_))
-                                        log_error_unit(s->name,
+                                        log_unit_error(s->name,
                                                        "[%s:%u] Trailing garbage in Provides, ignoring.",
                                                        s->path, line);
 
@@ -517,7 +561,7 @@ static int load_sysv(SysvStub *s) {
 
                                         r = sysv_translate_facility(n, basename(s->path), &m);
                                         if (r < 0) {
-                                                log_error_unit(s->name,
+                                                log_unit_error(s->name,
                                                                "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
                                                                s->path, line, n, strerror(-r));
                                                 continue;
@@ -551,12 +595,12 @@ static int load_sysv(SysvStub *s) {
                                         }
 
                                         if (r < 0)
-                                                log_error_unit(s->name,
+                                                log_unit_error(s->name,
                                                                "[%s:%u] Failed to add dependency on %s, ignoring: %s",
                                                                s->path, line, m, strerror(-r));
                                 }
                                 if (!isempty(state_))
-                                        log_error_unit(s->name,
+                                        log_unit_error(s->name,
                                                        "[%s:%u] Trailing garbage in %*s, ignoring.",
                                                        s->path, line,
                                                        (int)(strchr(t, ':') - t), t);
@@ -698,33 +742,38 @@ static int enumerate_sysv(LookupPaths lp, Hashmap *all_services) {
                 d = opendir(*path);
                 if (!d) {
                         if (errno != ENOENT)
-                                log_warning("opendir(%s) failed: %m", *path);
+                                log_warning_errno(errno, "opendir(%s) failed: %m", *path);
                         continue;
                 }
 
                 while ((de = readdir(d))) {
-                        SysvStub *service;
-                        struct stat st;
                         _cleanup_free_ char *fpath = NULL, *name = NULL;
+                        _cleanup_free_ SysvStub *service = NULL;
+                        struct stat st;
                         int r;
 
-                        if (ignore_file(de->d_name))
+                        if (hidden_file(de->d_name))
                                 continue;
 
-                        fpath = strjoin(*path, "/", de->d_name, NULL);
-                        if (!fpath)
-                                return log_oom();
-
-                        if (stat(fpath, &st) < 0)
+                        if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
+                                log_warning_errno(errno, "stat() failed on %s/%s: %m", *path, de->d_name);
                                 continue;
+                        }
 
                         if (!(st.st_mode & S_IXUSR))
                                 continue;
 
+                        if (!S_ISREG(st.st_mode))
+                                continue;
+
                         name = sysv_translate_name(de->d_name);
                         if (!name)
                                 return log_oom();
 
+                        fpath = strjoin(*path, "/", de->d_name, NULL);
+                        if (!fpath)
+                                return log_oom();
+
                         if (hashmap_contains(all_services, name))
                                 continue;
 
@@ -741,6 +790,7 @@ static int enumerate_sysv(LookupPaths lp, Hashmap *all_services) {
                                 return log_oom();
 
                         name = fpath = NULL;
+                        service = NULL;
                 }
         }
 
@@ -773,7 +823,7 @@ static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) {
                         d = opendir(path);
                         if (!d) {
                                 if (errno != ENOENT)
-                                        log_warning("opendir(%s) failed: %m", path);
+                                        log_warning_errno(errno, "opendir(%s) failed: %m", path);
 
                                 continue;
                         }
@@ -781,7 +831,7 @@ static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) {
                         while ((de = readdir(d))) {
                                 int a, b;
 
-                                if (ignore_file(de->d_name))
+                                if (hidden_file(de->d_name))
                                         continue;
 
                                 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
@@ -923,7 +973,9 @@ int main(int argc, char *argv[]) {
                 q = load_sysv(service);
                 if (q < 0)
                         continue;
+        }
 
+        HASHMAP_FOREACH(service, all_services, j) {
                 q = fix_order(service, all_services);
                 if (q < 0)
                         continue;