}
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] = ')';
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. */
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);
}
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;
}