mirror of
https://github.com/morgan9e/systemd
synced 2026-04-15 08:56:15 +09:00
Merge pull request #27033 from dtardon/array-cleanup
Use CLEANUP_ARRAY more
This commit is contained in:
@@ -2234,7 +2234,7 @@ static int install_error(
|
||||
InstallChange *changes,
|
||||
size_t n_changes) {
|
||||
|
||||
int r;
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
for (size_t i = 0; i < n_changes; i++)
|
||||
|
||||
@@ -2246,83 +2246,65 @@ static int install_error(
|
||||
|
||||
case -EEXIST:
|
||||
if (changes[i].source)
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
|
||||
"File %s already exists and is a symlink to %s.",
|
||||
changes[i].path, changes[i].source);
|
||||
else
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
|
||||
"File %s already exists.",
|
||||
changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
|
||||
"File %s already exists and is a symlink to %s.",
|
||||
changes[i].path, changes[i].source);
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
|
||||
"File %s already exists.",
|
||||
changes[i].path);
|
||||
|
||||
case -ERFKILL:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED,
|
||||
"Unit file %s is masked.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED,
|
||||
"Unit file %s is masked.", changes[i].path);
|
||||
|
||||
case -EADDRNOTAVAIL:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED,
|
||||
"Unit %s is transient or generated.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED,
|
||||
"Unit %s is transient or generated.", changes[i].path);
|
||||
|
||||
case -ETXTBSY:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_BAD_PATH,
|
||||
"File %s is under the systemd unit hierarchy already.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_BAD_PATH,
|
||||
"File %s is under the systemd unit hierarchy already.", changes[i].path);
|
||||
|
||||
case -EBADSLT:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Invalid specifier in %s.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Invalid specifier in %s.", changes[i].path);
|
||||
|
||||
case -EIDRM:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Destination unit %s is a non-template unit.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Destination unit %s is a non-template unit.", changes[i].path);
|
||||
|
||||
case -EUCLEAN:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"\"%s\" is not a valid unit name.",
|
||||
changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"\"%s\" is not a valid unit name.",
|
||||
changes[i].path);
|
||||
|
||||
case -ELOOP:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_UNIT_LINKED,
|
||||
"Refusing to operate on alias name or linked unit file: %s",
|
||||
changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_UNIT_LINKED,
|
||||
"Refusing to operate on alias name or linked unit file: %s",
|
||||
changes[i].path);
|
||||
|
||||
case -EXDEV:
|
||||
if (changes[i].source)
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Cannot alias %s as %s.",
|
||||
changes[i].source, changes[i].path);
|
||||
else
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Invalid unit reference %s.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Cannot alias %s as %s.",
|
||||
changes[i].source, changes[i].path);
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Invalid unit reference %s.", changes[i].path);
|
||||
|
||||
case -ENOENT:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
|
||||
"Unit file %s does not exist.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
|
||||
"Unit file %s does not exist.", changes[i].path);
|
||||
|
||||
case -EUNATCH:
|
||||
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Cannot resolve specifiers in %s.", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
|
||||
"Cannot resolve specifiers in %s.", changes[i].path);
|
||||
|
||||
default:
|
||||
assert(changes[i].type < 0); /* other errors */
|
||||
r = sd_bus_error_set_errnof(error, changes[i].type, "File %s: %m", changes[i].path);
|
||||
goto found;
|
||||
return sd_bus_error_set_errnof(error, changes[i].type, "File %s: %m", changes[i].path);
|
||||
}
|
||||
|
||||
r = c < 0 ? c : -EINVAL;
|
||||
|
||||
found:
|
||||
install_changes_free(changes, n_changes);
|
||||
return r;
|
||||
return c < 0 ? c : -EINVAL;
|
||||
}
|
||||
|
||||
static int reply_install_changes_and_free(
|
||||
@@ -2726,6 +2708,8 @@ static int method_get_unit_file_links(sd_bus_message *message, void *userdata, s
|
||||
const char *name;
|
||||
int runtime, r;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
r = sd_bus_message_read(message, "sb", &name, &runtime);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -2741,27 +2725,21 @@ static int method_get_unit_file_links(sd_bus_message *message, void *userdata, s
|
||||
r = unit_file_disable(m->runtime_scope,
|
||||
UNIT_FILE_DRY_RUN | (runtime ? UNIT_FILE_RUNTIME : 0),
|
||||
NULL, STRV_MAKE(name), &changes, &n_changes);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to get file links for %s: %m", name);
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get file links for %s: %m", name);
|
||||
|
||||
for (i = 0; i < n_changes; i++)
|
||||
if (changes[i].type == INSTALL_CHANGE_UNLINK) {
|
||||
r = sd_bus_message_append(reply, "s", changes[i].path);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
r = sd_bus_message_close_container(reply);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = sd_bus_send(NULL, reply, NULL);
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
return r;
|
||||
return sd_bus_send(NULL, reply, NULL);
|
||||
}
|
||||
|
||||
static int method_get_job_waiting(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
|
||||
@@ -3446,7 +3446,7 @@ static int compile_bind_mounts(
|
||||
char ***ret_empty_directories) {
|
||||
|
||||
_cleanup_strv_free_ char **empty_directories = NULL;
|
||||
BindMount *bind_mounts;
|
||||
BindMount *bind_mounts = NULL;
|
||||
size_t n, h = 0;
|
||||
int r;
|
||||
|
||||
@@ -3456,6 +3456,8 @@ static int compile_bind_mounts(
|
||||
assert(ret_n_bind_mounts);
|
||||
assert(ret_empty_directories);
|
||||
|
||||
CLEANUP_ARRAY(bind_mounts, h, bind_mount_free_many);
|
||||
|
||||
n = context->n_bind_mounts;
|
||||
for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
|
||||
if (!params->prefix[t])
|
||||
@@ -3478,24 +3480,19 @@ static int compile_bind_mounts(
|
||||
|
||||
for (size_t i = 0; i < context->n_bind_mounts; i++) {
|
||||
BindMount *item = context->bind_mounts + i;
|
||||
char *s, *d;
|
||||
_cleanup_free_ char *s = NULL, *d = NULL;
|
||||
|
||||
s = strdup(item->source);
|
||||
if (!s) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
d = strdup(item->destination);
|
||||
if (!d) {
|
||||
free(s);
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
|
||||
bind_mounts[h++] = (BindMount) {
|
||||
.source = s,
|
||||
.destination = d,
|
||||
.source = TAKE_PTR(s),
|
||||
.destination = TAKE_PTR(d),
|
||||
.read_only = item->read_only,
|
||||
.recursive = item->recursive,
|
||||
.ignore_enoent = item->ignore_enoent,
|
||||
@@ -3518,18 +3515,16 @@ static int compile_bind_mounts(
|
||||
* tmpfs that makes it accessible and is empty except for the submounts we do this for. */
|
||||
|
||||
private_root = path_join(params->prefix[t], "private");
|
||||
if (!private_root) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!private_root)
|
||||
return -ENOMEM;
|
||||
|
||||
r = strv_consume(&empty_directories, private_root);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < context->directories[t].n_items; i++) {
|
||||
char *s, *d;
|
||||
_cleanup_free_ char *s = NULL, *d = NULL;
|
||||
|
||||
/* When one of the parent directories is in the list, we cannot create the symlink
|
||||
* for the child directory. See also the comments in setup_exec_directory(). */
|
||||
@@ -3540,10 +3535,8 @@ static int compile_bind_mounts(
|
||||
s = path_join(params->prefix[t], "private", context->directories[t].items[i].path);
|
||||
else
|
||||
s = path_join(params->prefix[t], context->directories[t].items[i].path);
|
||||
if (!s) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
if (exec_directory_is_private(context, t) &&
|
||||
exec_context_with_rootfs(context))
|
||||
@@ -3553,15 +3546,12 @@ static int compile_bind_mounts(
|
||||
d = path_join(params->prefix[t], context->directories[t].items[i].path);
|
||||
else
|
||||
d = strdup(s);
|
||||
if (!d) {
|
||||
free(s);
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
|
||||
bind_mounts[h++] = (BindMount) {
|
||||
.source = s,
|
||||
.destination = d,
|
||||
.source = TAKE_PTR(s),
|
||||
.destination = TAKE_PTR(d),
|
||||
.read_only = false,
|
||||
.nosuid = context->dynamic_user, /* don't allow suid/sgid when DynamicUser= is on */
|
||||
.recursive = true,
|
||||
@@ -3572,15 +3562,11 @@ static int compile_bind_mounts(
|
||||
|
||||
assert(h == n);
|
||||
|
||||
*ret_bind_mounts = bind_mounts;
|
||||
*ret_bind_mounts = TAKE_PTR(bind_mounts);
|
||||
*ret_n_bind_mounts = n;
|
||||
*ret_empty_directories = TAKE_PTR(empty_directories);
|
||||
|
||||
return (int) n;
|
||||
|
||||
finish:
|
||||
bind_mount_free_many(bind_mounts, h);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ret_symlinks will contain a list of pairs src:dest that describes
|
||||
@@ -3704,6 +3690,8 @@ static int apply_mount_namespace(
|
||||
|
||||
assert(context);
|
||||
|
||||
CLEANUP_ARRAY(bind_mounts, n_bind_mounts, bind_mount_free_many);
|
||||
|
||||
if (params->flags & EXEC_APPLY_CHROOT) {
|
||||
root_image = context->root_image;
|
||||
|
||||
@@ -3718,20 +3706,18 @@ static int apply_mount_namespace(
|
||||
/* Symlinks for exec dirs are set up after other mounts, before they are made read-only. */
|
||||
r = compile_symlinks(context, params, &symlinks);
|
||||
if (r < 0)
|
||||
goto finalize;
|
||||
return r;
|
||||
|
||||
/* We need to make the pressure path writable even if /sys/fs/cgroups is made read-only, as the
|
||||
* service will need to write to it in order to start the notifications. */
|
||||
if (context->protect_control_groups && memory_pressure_path && !streq(memory_pressure_path, "/dev/null")) {
|
||||
read_write_paths_cleanup = strv_copy(context->read_write_paths);
|
||||
if (!read_write_paths_cleanup) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (!read_write_paths_cleanup)
|
||||
return -ENOMEM;
|
||||
|
||||
r = strv_extend(&read_write_paths_cleanup, memory_pressure_path);
|
||||
if (r < 0)
|
||||
goto finalize;
|
||||
return r;
|
||||
|
||||
read_write_paths = read_write_paths_cleanup;
|
||||
} else
|
||||
@@ -3793,35 +3779,25 @@ static int apply_mount_namespace(
|
||||
params->prefix[EXEC_DIRECTORY_RUNTIME] &&
|
||||
FLAGS_SET(params->flags, EXEC_WRITE_CREDENTIALS)) {
|
||||
creds_path = path_join(params->prefix[EXEC_DIRECTORY_RUNTIME], "credentials", u->id);
|
||||
if (!creds_path) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (!creds_path)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if (MANAGER_IS_SYSTEM(u->manager)) {
|
||||
propagate_dir = path_join("/run/systemd/propagate/", u->id);
|
||||
if (!propagate_dir) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (!propagate_dir)
|
||||
return -ENOMEM;
|
||||
|
||||
incoming_dir = strdup("/run/systemd/incoming");
|
||||
if (!incoming_dir) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (!incoming_dir)
|
||||
return -ENOMEM;
|
||||
|
||||
extension_dir = strdup("/run/systemd/unit-extensions");
|
||||
if (!extension_dir) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (!extension_dir)
|
||||
return -ENOMEM;
|
||||
} else
|
||||
if (asprintf(&extension_dir, "/run/user/" UID_FMT "/systemd/unit-extensions", geteuid()) < 0) {
|
||||
r = -ENOMEM;
|
||||
goto finalize;
|
||||
}
|
||||
if (asprintf(&extension_dir, "/run/user/" UID_FMT "/systemd/unit-extensions", geteuid()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
r = setup_namespace(root_dir, root_image, context->root_image_options,
|
||||
&ns_info, read_write_paths,
|
||||
@@ -3863,20 +3839,22 @@ static int apply_mount_namespace(
|
||||
context,
|
||||
root_dir, root_image,
|
||||
bind_mounts,
|
||||
n_bind_mounts)) {
|
||||
log_unit_debug(u, "Failed to set up namespace, and refusing to continue since the selected namespacing options alter mount environment non-trivially.\n"
|
||||
"Bind mounts: %zu, temporary filesystems: %zu, root directory: %s, root image: %s, dynamic user: %s",
|
||||
n_bind_mounts, context->n_temporary_filesystems, yes_no(root_dir), yes_no(root_image), yes_no(context->dynamic_user));
|
||||
n_bind_mounts))
|
||||
return log_unit_debug_errno(u,
|
||||
SYNTHETIC_ERRNO(EOPNOTSUPP),
|
||||
"Failed to set up namespace, and refusing to continue since "
|
||||
"the selected namespacing options alter mount environment non-trivially.\n"
|
||||
"Bind mounts: %zu, temporary filesystems: %zu, root directory: %s, root image: %s, dynamic user: %s",
|
||||
n_bind_mounts,
|
||||
context->n_temporary_filesystems,
|
||||
yes_no(root_dir),
|
||||
yes_no(root_image),
|
||||
yes_no(context->dynamic_user));
|
||||
|
||||
r = -EOPNOTSUPP;
|
||||
} else {
|
||||
log_unit_debug(u, "Failed to set up namespace, assuming containerized execution and ignoring.");
|
||||
r = 0;
|
||||
}
|
||||
log_unit_debug(u, "Failed to set up namespace, assuming containerized execution and ignoring.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
finalize:
|
||||
bind_mount_free_many(bind_mounts, n_bind_mounts);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -3512,23 +3512,23 @@ static int bus_add_match_full(
|
||||
|
||||
struct bus_match_component *components = NULL;
|
||||
size_t n_components = 0;
|
||||
sd_bus_slot *s = NULL;
|
||||
int r = 0;
|
||||
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
|
||||
int r;
|
||||
|
||||
assert_return(bus, -EINVAL);
|
||||
assert_return(bus = bus_resolve(bus), -ENOPKG);
|
||||
assert_return(match, -EINVAL);
|
||||
assert_return(!bus_pid_changed(bus), -ECHILD);
|
||||
|
||||
CLEANUP_ARRAY(components, n_components, bus_match_parse_free);
|
||||
|
||||
r = bus_match_parse(match, &components, &n_components);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
|
||||
if (!s) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
s->match_callback.callback = callback;
|
||||
s->match_callback.install_callback = install_callback;
|
||||
@@ -3544,10 +3544,8 @@ static int bus_add_match_full(
|
||||
/* We store the original match string, so that we can use it to remove the match again. */
|
||||
|
||||
s->match_callback.match_string = strdup(match);
|
||||
if (!s->match_callback.match_string) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!s->match_callback.match_string)
|
||||
return -ENOMEM;
|
||||
|
||||
if (asynchronous) {
|
||||
r = bus_add_match_internal_async(bus,
|
||||
@@ -3557,7 +3555,7 @@ static int bus_add_match_full(
|
||||
s);
|
||||
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
/* Make the slot of the match call floating now. We need the reference, but we don't
|
||||
* want that this match pins the bus object, hence we first create it non-floating, but
|
||||
@@ -3566,7 +3564,7 @@ static int bus_add_match_full(
|
||||
} else
|
||||
r = bus_add_match_internal(bus, s->match_callback.match_string, &s->match_callback.after);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
s->match_added = true;
|
||||
}
|
||||
@@ -3575,17 +3573,13 @@ static int bus_add_match_full(
|
||||
bus->match_callbacks_modified = true;
|
||||
r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
if (slot)
|
||||
*slot = s;
|
||||
s = NULL;
|
||||
|
||||
finish:
|
||||
bus_match_parse_free(components, n_components);
|
||||
sd_bus_slot_unref(s);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_bus_add_match(
|
||||
|
||||
@@ -1760,10 +1760,14 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
|
||||
const char *method;
|
||||
sd_bus *bus = ASSERT_PTR(userdata);
|
||||
int r;
|
||||
bool enable;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
|
||||
|
||||
method = streq(argv[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
|
||||
enable = streq(argv[0], "enable");
|
||||
method = enable ? "EnableUnitFiles" : "DisableUnitFiles";
|
||||
|
||||
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method);
|
||||
if (r < 0)
|
||||
@@ -1773,7 +1777,7 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return bus_log_create_error(r);
|
||||
|
||||
if (streq(argv[0], "enable")) {
|
||||
if (enable) {
|
||||
r = sd_bus_message_append(m, "s", "machines.target");
|
||||
if (r < 0)
|
||||
return bus_log_create_error(r);
|
||||
@@ -1803,7 +1807,7 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return bus_log_create_error(r);
|
||||
|
||||
if (streq(argv[0], "enable"))
|
||||
if (enable)
|
||||
r = sd_bus_message_append(m, "bb", false, false);
|
||||
else
|
||||
r = sd_bus_message_append(m, "b", false);
|
||||
@@ -1814,7 +1818,7 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to enable or disable unit: %s", bus_error_message(&error, r));
|
||||
|
||||
if (streq(argv[0], "enable")) {
|
||||
if (enable) {
|
||||
r = sd_bus_message_read(reply, "b", NULL);
|
||||
if (r < 0)
|
||||
return bus_log_parse_error(r);
|
||||
@@ -1822,39 +1826,30 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = bus_call_method(bus, bus_systemd_mgr, "Reload", &error, NULL, NULL);
|
||||
if (r < 0) {
|
||||
log_error("Failed to reload daemon: %s", bus_error_message(&error, r));
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
|
||||
|
||||
if (arg_now) {
|
||||
_cleanup_strv_free_ char **new_args = NULL;
|
||||
|
||||
new_args = strv_new(streq(argv[0], "enable") ? "start" : "poweroff");
|
||||
if (!new_args) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
new_args = strv_new(enable ? "start" : "poweroff");
|
||||
if (!new_args)
|
||||
return log_oom();
|
||||
|
||||
r = strv_extend_strv(&new_args, argv + 1, /* filter_duplicates = */ false);
|
||||
if (r < 0) {
|
||||
log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
if (streq(argv[0], "enable"))
|
||||
r = start_machine(strv_length(new_args), new_args, userdata);
|
||||
else
|
||||
r = poweroff_machine(strv_length(new_args), new_args, userdata);
|
||||
if (enable)
|
||||
return start_machine(strv_length(new_args), new_args, userdata);
|
||||
|
||||
return poweroff_machine(strv_length(new_args), new_args, userdata);
|
||||
}
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int match_log_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
|
||||
@@ -561,6 +561,8 @@ static int maybe_enable_disable(sd_bus *bus, const char *path, bool enable) {
|
||||
size_t n_changes = 0;
|
||||
int r;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
if (!arg_enable)
|
||||
return 0;
|
||||
|
||||
@@ -598,7 +600,6 @@ static int maybe_enable_disable(sd_bus *bus, const char *path, bool enable) {
|
||||
}
|
||||
|
||||
(void) bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -281,6 +281,8 @@ static int method_detach_image(sd_bus_message *message, void *userdata, sd_bus_e
|
||||
|
||||
assert(message);
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
|
||||
|
||||
/* Note that we do not redirect detaching to the image object here, because we want to allow that users can
|
||||
* detach already deleted images too, in case the user already deleted an image before properly detaching
|
||||
* it. */
|
||||
@@ -339,13 +341,9 @@ static int method_detach_image(sd_bus_message *message, void *userdata, sd_bus_e
|
||||
&n_changes,
|
||||
error);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = reply_portable_changes(message, changes, n_changes);
|
||||
|
||||
finish:
|
||||
portable_changes_free(changes, n_changes);
|
||||
return r;
|
||||
return reply_portable_changes(message, changes, n_changes);
|
||||
}
|
||||
|
||||
static int method_reattach_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
|
||||
@@ -316,6 +316,8 @@ int bus_image_common_attach(
|
||||
assert(message);
|
||||
assert(name_or_path || image);
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
|
||||
|
||||
if (!m) {
|
||||
assert(image);
|
||||
m = image->userdata;
|
||||
@@ -390,13 +392,9 @@ int bus_image_common_attach(
|
||||
&n_changes,
|
||||
error);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = reply_portable_changes(message, changes, n_changes);
|
||||
|
||||
finish:
|
||||
portable_changes_free(changes, n_changes);
|
||||
return r;
|
||||
return reply_portable_changes(message, changes, n_changes);
|
||||
}
|
||||
|
||||
static int bus_image_method_attach(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
@@ -418,6 +416,8 @@ static int bus_image_method_detach(
|
||||
|
||||
assert(message);
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
|
||||
|
||||
if (sd_bus_message_is_method_call(message, NULL, "DetachWithExtensions")) {
|
||||
r = sd_bus_message_read_strv(message, &extension_images);
|
||||
if (r < 0)
|
||||
@@ -470,13 +470,9 @@ static int bus_image_method_detach(
|
||||
&n_changes,
|
||||
error);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = reply_portable_changes(message, changes, n_changes);
|
||||
|
||||
finish:
|
||||
portable_changes_free(changes, n_changes);
|
||||
return r;
|
||||
return reply_portable_changes(message, changes, n_changes);
|
||||
}
|
||||
|
||||
int bus_image_common_remove(
|
||||
@@ -648,6 +644,10 @@ int bus_image_common_reattach(
|
||||
assert(message);
|
||||
assert(name_or_path || image);
|
||||
|
||||
CLEANUP_ARRAY(changes_detached, n_changes_detached, portable_changes_free);
|
||||
CLEANUP_ARRAY(changes_attached, n_changes_attached, portable_changes_free);
|
||||
CLEANUP_ARRAY(changes_gone, n_changes_gone, portable_changes_free);
|
||||
|
||||
if (!m) {
|
||||
assert(image);
|
||||
m = image->userdata;
|
||||
@@ -721,7 +721,7 @@ int bus_image_common_reattach(
|
||||
&n_changes_detached,
|
||||
error);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = portable_attach(
|
||||
sd_bus_message_get_bus(message),
|
||||
@@ -734,7 +734,7 @@ int bus_image_common_reattach(
|
||||
&n_changes_attached,
|
||||
error);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
/* We want to return the list of units really removed by the detach,
|
||||
* and not added again by the attach */
|
||||
@@ -742,22 +742,14 @@ int bus_image_common_reattach(
|
||||
changes_detached, n_changes_detached,
|
||||
&changes_gone, &n_changes_gone);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
/* First, return the units that are gone (so that the caller can stop them)
|
||||
* Then, return the units that are changed/added (so that the caller can
|
||||
* start/restart/enable them) */
|
||||
r = reply_portable_changes_pair(message,
|
||||
changes_gone, n_changes_gone,
|
||||
changes_attached, n_changes_attached);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
finish:
|
||||
portable_changes_free(changes_detached, n_changes_detached);
|
||||
portable_changes_free(changes_attached, n_changes_attached);
|
||||
portable_changes_free(changes_gone, n_changes_gone);
|
||||
return r;
|
||||
return reply_portable_changes_pair(message,
|
||||
changes_gone, n_changes_gone,
|
||||
changes_attached, n_changes_attached);
|
||||
}
|
||||
|
||||
static int bus_image_method_reattach(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
|
||||
@@ -16,6 +16,8 @@ int verb_add_dependency(int argc, char *argv[], void *userdata) {
|
||||
UnitDependency dep;
|
||||
int r;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
if (!argv[1])
|
||||
return 0;
|
||||
|
||||
@@ -39,9 +41,8 @@ int verb_add_dependency(int argc, char *argv[], void *userdata) {
|
||||
if (install_client_side()) {
|
||||
r = unit_file_add_dependency(arg_runtime_scope, unit_file_flags_from_args(), arg_root, names, target, dep, &changes, &n_changes);
|
||||
install_changes_dump(r, "add dependency on", changes, n_changes, arg_quiet);
|
||||
|
||||
if (r > 0)
|
||||
r = 0;
|
||||
if (r < 0)
|
||||
return r;
|
||||
} else {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
@@ -71,20 +72,14 @@ int verb_add_dependency(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
if (arg_no_reload) {
|
||||
r = 0;
|
||||
goto finish;
|
||||
if (!arg_no_reload) {
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r > 0)
|
||||
r = 0;
|
||||
}
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@ int verb_enable(int argc, char *argv[], void *userdata) {
|
||||
bool ignore_carries_install_info = arg_quiet || arg_no_warn;
|
||||
int r;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
if (!argv[1])
|
||||
return 0;
|
||||
|
||||
@@ -130,8 +132,7 @@ int verb_enable(int argc, char *argv[], void *userdata) {
|
||||
|
||||
install_changes_dump(r, verb, changes, n_changes, arg_quiet);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = 0;
|
||||
return r;
|
||||
} else {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
@@ -236,15 +237,14 @@ int verb_enable(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
/* Try to reload if enabled */
|
||||
if (!arg_no_reload) {
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r > 0)
|
||||
r = 0;
|
||||
} else
|
||||
r = 0;
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
if (carries_install_info == 0 && !ignore_carries_install_info)
|
||||
@@ -306,7 +306,7 @@ int verb_enable(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = acquire_bus(BUS_MANAGER, &bus);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
len = strv_length(names);
|
||||
{
|
||||
@@ -321,8 +321,5 @@ int verb_enable(int argc, char *argv[], void *userdata) {
|
||||
}
|
||||
}
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ int verb_preset_all(int argc, char *argv[], void *userdata) {
|
||||
size_t n_changes = 0;
|
||||
int r;
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
if (install_client_side()) {
|
||||
r = unit_file_preset_all(arg_runtime_scope, unit_file_flags_from_args(), arg_root, arg_preset_mode, &changes, &n_changes);
|
||||
install_changes_dump(r, "preset", changes, n_changes, arg_quiet);
|
||||
@@ -44,20 +46,14 @@ int verb_preset_all(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
if (arg_no_reload) {
|
||||
r = 0;
|
||||
goto finish;
|
||||
if (!arg_no_reload) {
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r > 0)
|
||||
r = 0;
|
||||
}
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -102,6 +102,8 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
|
||||
assert(argc >= 2);
|
||||
assert(argv);
|
||||
|
||||
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
|
||||
|
||||
r = unit_name_mangle_with_suffix(argv[1], "set-default",
|
||||
arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
|
||||
".target", &unit);
|
||||
@@ -112,7 +114,7 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
|
||||
r = unit_file_set_default(arg_runtime_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes);
|
||||
install_changes_dump(r, "set default", changes, n_changes, arg_quiet);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
} else {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
@@ -130,13 +132,13 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
/* Try to reload if enabled */
|
||||
if (!arg_no_reload) {
|
||||
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,14 +149,11 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = determine_default(&final);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
if (!streq(final, unit))
|
||||
log_notice("Note: \"%s\" is the default unit (possibly a runtime override).", final);
|
||||
}
|
||||
|
||||
finish:
|
||||
install_changes_free(changes, n_changes);
|
||||
|
||||
return r < 0 ? r : 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user