core: move some basename() use → path_extract_filename()

This commit is contained in:
Lennart Poettering
2022-12-23 15:04:19 +01:00
parent 0eb2f0a2fc
commit a99626c11a
3 changed files with 35 additions and 18 deletions
+12 -11
View 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);
p = basename(path);
if (isempty(p)) {
/* This resulting string must fit in 10 chars (i.e. the length of "/sbin/init") to look pretty in
* /bin/ps */
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] = ')';
+9 -5
View 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);
+14 -2
View 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;
}