]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: move some basename() use → path_extract_filename()
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Dec 2022 17:45:27 +0000 (18:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Dec 2022 14:04:19 +0000 (15:04 +0100)
src/core/execute.c
src/core/manager.c
src/core/unit.c

index 9f0c5a85bf45716a04a7c4e05ed515af27b9040f..439f491d024682e6cfd0619c8a47c64b4afd4a56 100644 (file)
@@ -1376,28 +1376,29 @@ fail:
 }
 
 static void rename_process_from_path(const char *path) {
-        char process_name[11];
+        _cleanup_free_ char *buf = NULL;
         const char *p;
-        size_t l;
 
-        /* This resulting string must fit in 10 chars (i.e. the length
-         * of "/sbin/init") to look pretty in /bin/ps */
+        assert(path);
+
+        /* This resulting string must fit in 10 chars (i.e. the length of "/sbin/init") to look pretty in
+         * /bin/ps */
 
-        p = basename(path);
-        if (isempty(p)) {
+        if (path_extract_filename(path, &buf) < 0) {
                 rename_process("(...)");
                 return;
         }
 
-        l = strlen(p);
+        size_t l = strlen(buf);
         if (l > 8) {
-                /* The end of the process name is usually more
-                 * interesting, since the first bit might just be
+                /* The end of the process name is usually more interesting, since the first bit might just be
                  * "systemd-" */
-                p = p + l - 8;
+                p = buf + l - 8;
                 l = 8;
-        }
+        } else
+                p = buf;
 
+        char process_name[11];
         process_name[0] = '(';
         memcpy(process_name+1, p, l);
         process_name[1+l] = ')';
index 3332d5775b2dfdbc5f6d1676d1749fc0d3df0223..739933b97cdedcf3971d028ca7a580b50ea4245f 100644 (file)
@@ -2122,10 +2122,12 @@ int manager_load_unit_prepare(
                 Unit **ret) {
 
         _cleanup_(unit_freep) Unit *cleanup_unit = NULL;
+        _cleanup_free_ char *nbuf = NULL;
         int r;
 
         assert(m);
         assert(ret);
+        assert(name || path);
 
         /* This will prepare the unit for loading, but not actually load anything from disk. */
 
@@ -2133,11 +2135,13 @@ int manager_load_unit_prepare(
                 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
 
         if (!name) {
-                /* 'name' and 'path' must not both be null. Check here 'path' using assert_se() to
-                 * workaround a bug in gcc that generates a -Wnonnull warning when calling basename(),
-                 * but this cannot be possible in any code path (See #6119). */
-                assert_se(path);
-                name = basename(path);
+                r = path_extract_filename(path, &nbuf);
+                if (r < 0)
+                        return r;
+                if (r == O_DIRECTORY)
+                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' refers to directory, refusing.", path);
+
+                name = nbuf;
         }
 
         UnitType t = unit_name_to_type(name);
index e14238b3bae681f46ddc033ff3d9a5b189a95bc3..59e721d3e7066ac7161e158c6772c3b03d1c86b1 100644 (file)
@@ -3976,14 +3976,26 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
 }
 
 int unit_get_unit_file_preset(Unit *u) {
+        int r;
+
         assert(u);
 
-        if (u->unit_file_preset < 0 && u->fragment_path)
+        if (u->unit_file_preset < 0 && u->fragment_path) {
+                _cleanup_free_ char *bn = NULL;
+
+                r = path_extract_filename(u->fragment_path, &bn);
+                if (r < 0)
+                        return (u->unit_file_preset = r);
+
+                if (r == O_DIRECTORY)
+                        return (u->unit_file_preset = -EISDIR);
+
                 u->unit_file_preset = unit_file_query_preset(
                                 u->manager->unit_file_scope,
                                 NULL,
-                                basename(u->fragment_path),
+                                bn,
                                 NULL);
+        }
 
         return u->unit_file_preset;
 }