From: Lennart Poettering Date: Thu, 22 Dec 2022 17:45:27 +0000 (+0100) Subject: core: move some basename() use → path_extract_filename() X-Git-Tag: v253-rc1~214^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a99626c11a2a54a76104a8ee9d9c472296383d56;p=thirdparty%2Fsystemd.git core: move some basename() use → path_extract_filename() --- diff --git a/src/core/execute.c b/src/core/execute.c index 9f0c5a85bf4..439f491d024 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -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] = ')'; diff --git a/src/core/manager.c b/src/core/manager.c index 3332d5775b2..739933b97cd 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -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); diff --git a/src/core/unit.c b/src/core/unit.c index e14238b3bae..59e721d3e70 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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; }