]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/sysv-generator/sysv-generator.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / sysv-generator / sysv-generator.c
index 0c839c2c538b69c4de16c44f73046e6171ed2440..0a0b9269b3f91c59af82b9657eda50bb8203def2 100644 (file)
 #include <stdio.h>
 #include <unistd.h>
 
-#include "util.h"
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "hashmap.h"
+#include "hexdecoct.h"
+#include "install.h"
+#include "log.h"
 #include "mkdir.h"
-#include "strv.h"
-#include "path-util.h"
 #include "path-lookup.h"
-#include "log.h"
-#include "unit.h"
-#include "unit-name.h"
+#include "path-util.h"
+#include "set.h"
 #include "special.h"
-#include "hashmap.h"
+#include "stat-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "unit-name.h"
+#include "util.h"
 
 typedef enum RunlevelType {
         RUNLEVEL_UP,
@@ -223,7 +229,7 @@ static int generate_unit_file(SysvStub *s) {
         STRV_FOREACH(p, s->wanted_by) {
                 r = add_symlink(s->name, *p);
                 if (r < 0)
-                        log_unit_error_errno(s->name, r, "Failed to create 'Wants' symlink to %s: %m", *p);
+                        log_error_errno(r, "Failed to create 'Wants' symlink to %s: %m", *p);
         }
 
         return 0;
@@ -239,20 +245,21 @@ static bool usage_contains_reload(const char *line) {
 }
 
 static char *sysv_translate_name(const char *name) {
-        char *r;
+        _cleanup_free_ char *c = NULL;
+        char *res;
 
-        r = new(char, strlen(name) + strlen(".service") + 1);
-        if (!r)
+        c = strdup(name);
+        if (!c)
                 return NULL;
 
-        if (endswith(name, ".sh"))
-                /* Drop .sh suffix */
-                strcpy(stpcpy(r, name) - 3, ".service");
-        else
-                /* Normal init script name */
-                strcpy(stpcpy(r, name), ".service");
+        res = endswith(c, ".sh");
+        if (res)
+                *res = 0;
 
-        return r;
+        if (unit_name_mangle(c, UNIT_NAME_NOGLOB, &res) < 0)
+                return NULL;
+
+        return res;
 }
 
 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
@@ -339,6 +346,7 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
 
         FOREACH_WORD_QUOTED(word, z, text, state_) {
                 _cleanup_free_ char *n = NULL, *m = NULL;
+                UnitType t;
 
                 n = strndup(word, z);
                 if (!n)
@@ -350,14 +358,13 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
                 if (r == 0)
                         continue;
 
-                if (unit_name_to_type(m) == UNIT_SERVICE) {
+                t = unit_name_to_type(m);
+                if (t == UNIT_SERVICE) {
                         log_debug("Adding Provides: alias '%s' for '%s'", m, s->name);
                         r = add_alias(s->name, m);
                         if (r < 0)
-                                log_unit_warning_errno(s->name, r,
-                                                       "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m",
-                                                       s->path, line, m);
-                } else {
+                                log_warning_errno(r, "[%s:%u] Failed to add LSB Provides name %s, ignoring: %m", s->path, line, m);
+                } else if (t == UNIT_TARGET) {
                         /* NB: SysV targets which are provided by a
                          * service are pulled in by the services, as
                          * an indication that the generic service is
@@ -374,12 +381,13 @@ static int handle_provides(SysvStub *s, unsigned line, const char *full_text, co
                                 if (r < 0)
                                         return log_oom();
                         }
-                }
+                } else if (t == _UNIT_TYPE_INVALID)
+                        log_warning("Unit name '%s' is invalid", m);
+                else
+                        log_warning("Unknown unit type for unit '%s'", m);
         }
         if (!isempty(state_))
-                log_unit_error(s->name,
-                               "[%s:%u] Trailing garbage in Provides, ignoring.",
-                               s->path, line);
+                log_error("[%s:%u] Trailing garbage in Provides, ignoring.", s->path, line);
         return 0;
 }
 
@@ -398,9 +406,7 @@ static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text
 
                 r = sysv_translate_facility(n, basename(s->path), &m);
                 if (r < 0) {
-                        log_unit_warning_errno(s->name, r,
-                                               "[%s:%u] Failed to translate LSB dependency %s, ignoring: %m",
-                                               s->path, line, n);
+                        log_warning_errno(r, "[%s:%u] Failed to translate LSB dependency %s, ignoring: %m", s->path, line, n);
                         continue;
                 }
                 if (r == 0)
@@ -421,9 +427,7 @@ static int handle_dependencies(SysvStub *s, unsigned line, const char *full_text
                         return log_oom();
         }
         if (!isempty(state_))
-                log_unit_warning(s->name,
-                                 "[%s:%u] Trailing garbage in %*s, ignoring.",
-                                 s->path, line, (int)(strchr(full_text, ':') - full_text), full_text);
+                log_warning("[%s:%u] Trailing garbage in %*s, ignoring.", s->path, line, (int)(strchr(full_text, ':') - full_text), full_text);
         return 0;
 }
 
@@ -457,9 +461,7 @@ static int load_sysv(SysvStub *s) {
                         if (feof(f))
                                 break;
 
-                        return log_unit_error_errno(s->name, errno,
-                                                    "Failed to read configuration file '%s': %m",
-                                                    s->path);
+                        return log_error_errno(errno, "Failed to read configuration file '%s': %m", s->path);
                 }
 
                 line++;
@@ -531,9 +533,7 @@ static int load_sysv(SysvStub *s) {
 
                                 fn = strstrip(t+8);
                                 if (!path_is_absolute(fn)) {
-                                        log_unit_error(s->name,
-                                                       "[%s:%u] PID file not absolute. Ignoring.",
-                                                       s->path, line);
+                                        log_error("[%s:%u] PID file not absolute. Ignoring.", s->path, line);
                                         continue;
                                 }
 
@@ -744,7 +744,7 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
                         if (hidden_file(de->d_name))
                                 continue;
 
-                        if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 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;
                         }
@@ -811,8 +811,7 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
                         if (!path)
                                 return -ENOMEM;
 
-                        if (d)
-                                closedir(d);
+                        safe_closedir(d);
 
                         d = opendir(path);
                         if (!d) {
@@ -942,7 +941,7 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        r = lookup_paths_init(&lp, SYSTEMD_SYSTEM, true, NULL, NULL, NULL, NULL);
+        r = lookup_paths_init(&lp, MANAGER_SYSTEM, true, NULL, NULL, NULL, NULL);
         if (r < 0) {
                 log_error("Failed to find lookup paths.");
                 return EXIT_FAILURE;