systemctl: fix editing of units with no fragment
"systemctl --user edit --force --full tmp.mount" would crash, when we'd do basename(NULL). Fix this by creating a new unit or a new override even if not path is found. Tested with: systemctl --user edit --force --full tmp.mount systemctl --user edit --force tmp.mount systemctl --user edit foo@.service systemctl --user edit foo@bar.service systemctl --user edit --full foo@.service systemctl --user edit --full foo@bar.service
This commit is contained in:
parent
b74df547a9
commit
b3734d9841
|
@ -6812,41 +6812,45 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
|
||||||
r = unit_find_paths(bus, *name, &lp, &path, NULL);
|
r = unit_find_paths(bus, *name, &lp, &path, NULL);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
else if (!arg_force) {
|
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
|
assert(!path);
|
||||||
|
|
||||||
|
if (!arg_force) {
|
||||||
log_error("Run 'systemctl edit --force %s' to create a new unit.", *name);
|
log_error("Run 'systemctl edit --force %s' to create a new unit.", *name);
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
} else if (!path) {
|
|
||||||
// FIXME: support units with path==NULL (no FragmentPath)
|
|
||||||
log_error("No fragment exists for %s.", *name);
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
unit_name = basename(path);
|
/* Create a new unit from scratch */
|
||||||
/* We follow unit aliases, but we need to propagate the instance */
|
unit_name = *name;
|
||||||
if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) &&
|
r = unit_file_create_new(&lp, unit_name,
|
||||||
unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
|
arg_full ? NULL : ".d/override.conf",
|
||||||
_cleanup_free_ char *instance = NULL;
|
&new_path, &tmp_path);
|
||||||
|
} else {
|
||||||
|
assert(path);
|
||||||
|
|
||||||
r = unit_name_to_instance(*name, &instance);
|
unit_name = basename(path);
|
||||||
if (r < 0)
|
/* We follow unit aliases, but we need to propagate the instance */
|
||||||
return r;
|
if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) &&
|
||||||
|
unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
|
||||||
|
_cleanup_free_ char *instance = NULL;
|
||||||
|
|
||||||
r = unit_name_replace_instance(unit_name, instance, &tmp_name);
|
r = unit_name_to_instance(*name, &instance);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
unit_name = tmp_name;
|
r = unit_name_replace_instance(unit_name, instance, &tmp_name);
|
||||||
}
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
unit_name = tmp_name;
|
||||||
|
}
|
||||||
|
|
||||||
if (path) {
|
|
||||||
if (arg_full)
|
if (arg_full)
|
||||||
r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path);
|
r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path);
|
||||||
else
|
else
|
||||||
r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path);
|
r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path);
|
||||||
} else
|
}
|
||||||
r = unit_file_create_new(&lp, *name, NULL, &new_path, &tmp_path);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue