From: Yu Watanabe Date: Mon, 17 Feb 2025 06:09:50 +0000 (+0900) Subject: nspawn: use FOREACH_ARRAY() where applicable X-Git-Tag: v258-rc1~1312^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1236f06c4245477553bb60694e5e5a4ba9971c01;p=thirdparty%2Fsystemd.git nspawn: use FOREACH_ARRAY() where applicable --- diff --git a/src/nspawn/nspawn-bind-user.c b/src/nspawn/nspawn-bind-user.c index 8964de22a11..4ec8393b02c 100644 --- a/src/nspawn/nspawn-bind-user.c +++ b/src/nspawn/nspawn-bind-user.c @@ -181,13 +181,11 @@ BindUserContext* bind_user_context_free(BindUserContext *c) { if (!c) return NULL; - assert(c->n_data == 0 || c->data); - - for (size_t i = 0; i < c->n_data; i++) { - user_record_unref(c->data[i].host_user); - group_record_unref(c->data[i].host_group); - user_record_unref(c->data[i].payload_user); - group_record_unref(c->data[i].payload_group); + FOREACH_ARRAY(d, c->data, c->n_data) { + user_record_unref(d->host_user); + group_record_unref(d->host_group); + user_record_unref(d->payload_user); + group_record_unref(d->payload_group); } return mfree(c); @@ -400,10 +398,9 @@ int bind_user_setup( if (r < 0) return log_error_errno(r, "Failed to create /run/host/userdb: %m"); - for (size_t i = 0; i < c->n_data; i++) { + FOREACH_ARRAY(d, c->data, c->n_data) { _cleanup_(group_record_unrefp) GroupRecord *stripped_group = NULL, *shadow_group = NULL; _cleanup_(user_record_unrefp) UserRecord *stripped_user = NULL, *shadow_user = NULL; - const BindUserData *d = c->data + i; /* First, write shadow (i.e. privileged) data for group record */ r = group_record_clone(d->payload_group, shadow_flags, &shadow_group); diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index 552d629a188..3e69781572f 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -48,9 +48,7 @@ CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) { } void custom_mount_free_all(CustomMount *l, size_t n) { - for (size_t i = 0; i < n; i++) { - CustomMount *m = l + i; - + FOREACH_ARRAY(m, l, n) { free(m->source); free(m->destination); free(m->options); @@ -1024,9 +1022,7 @@ int mount_custom( assert(dest); - for (size_t i = 0; i < n; i++) { - CustomMount *m = mounts + i; - + FOREACH_ARRAY(m, mounts, n) { if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns) continue; @@ -1070,8 +1066,8 @@ int mount_custom( } bool has_custom_root_mount(const CustomMount *mounts, size_t n) { - for (size_t i = 0; i < n; i++) - if (path_equal(mounts[i].destination, "/")) + FOREACH_ARRAY(m, mounts, n) + if (path_equal(m->destination, "/")) return true; return false; diff --git a/src/nspawn/nspawn-oci.c b/src/nspawn/nspawn-oci.c index 1b2fe76f8ac..ecbcaefcbb1 100644 --- a/src/nspawn/nspawn-oci.c +++ b/src/nspawn/nspawn-oci.c @@ -919,21 +919,17 @@ struct device_data { static int oci_cgroup_device_access(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct device_data *d = ASSERT_PTR(userdata); bool r = false, w = false, m = false; - const char *s; - size_t i; - - assert_se(s = sd_json_variant_string(v)); - for (i = 0; s[i]; i++) - if (s[i] == 'r') + for (const char *s = ASSERT_PTR(sd_json_variant_string(v)); *s; s++) + if (*s == 'r') r = true; - else if (s[i] == 'w') + else if (*s == 'w') w = true; - else if (s[i] == 'm') + else if (*s == 'm') m = true; else return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), - "Unknown device access character '%c'.", s[i]); + "Unknown device access character '%c'.", *s); d->r = r; d->w = w; @@ -945,7 +941,7 @@ static int oci_cgroup_device_access(const char *name, sd_json_variant *v, sd_jso static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_free_ struct device_data *list = NULL; Settings *s = ASSERT_PTR(userdata); - size_t n_list = 0, i; + size_t n_list = 0; bool noop = false; sd_json_variant *e; int r; @@ -1036,43 +1032,43 @@ static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_disp if (r < 0) return bus_log_create_error(r); - for (i = 0; i < n_list; i++) { + FOREACH_ARRAY(d, list, n_list) { _cleanup_free_ char *pattern = NULL; char access[4]; size_t n = 0; - if (list[i].minor == UINT_MAX) { + if (d->minor == UINT_MAX) { const char *t; - if (list[i].type == S_IFBLK) + if (d->type == S_IFBLK) t = "block"; else { - assert(list[i].type == S_IFCHR); + assert(d->type == S_IFCHR); t = "char"; } - if (list[i].major == UINT_MAX) { + if (d->major == UINT_MAX) { pattern = strjoin(t, "-*"); if (!pattern) return log_oom(); } else { - if (asprintf(&pattern, "%s-%u", t, list[i].major) < 0) + if (asprintf(&pattern, "%s-%u", t, d->major) < 0) return log_oom(); } } else { - assert(list[i].major != UINT_MAX); /* If a minor is specified, then a major also needs to be specified */ + assert(d->major != UINT_MAX); /* If a minor is specified, then a major also needs to be specified */ - r = device_path_make_major_minor(list[i].type, makedev(list[i].major, list[i].minor), &pattern); + r = device_path_make_major_minor(d->type, makedev(d->major, d->minor), &pattern); if (r < 0) return log_oom(); } - if (list[i].r) + if (d->r) access[n++] = 'r'; - if (list[i].w) + if (d->w) access[n++] = 'w'; - if (list[i].m) + if (d->m) access[n++] = 'm'; access[n] = 0; diff --git a/src/nspawn/nspawn-register.c b/src/nspawn/nspawn-register.c index 009f71f59fe..4193a338137 100644 --- a/src/nspawn/nspawn-register.c +++ b/src/nspawn/nspawn-register.c @@ -21,7 +21,6 @@ static int append_machine_properties( int kill_signal, bool coredump_receive) { - unsigned j; int r; assert(m); @@ -48,9 +47,7 @@ static int append_machine_properties( return bus_log_create_error(r); } - for (j = 0; j < n_mounts; j++) { - CustomMount *cm = mounts + j; - + FOREACH_ARRAY(cm, mounts, n_mounts) { if (cm->type != CUSTOM_MOUNT_BIND) continue; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 4c054b2dbb8..8fa05b9bc23 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -471,11 +471,7 @@ static int help(void) { } static int custom_mount_check_all(void) { - size_t i; - - for (i = 0; i < arg_n_custom_mounts; i++) { - CustomMount *m = &arg_custom_mounts[i]; - + FOREACH_ARRAY(m, arg_custom_mounts, arg_n_custom_mounts) if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) { if (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_OFF) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), @@ -485,7 +481,6 @@ static int custom_mount_check_all(void) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--private-users with automatic UID shift may not be combined with custom root mounts."); } - } return 0; } @@ -4154,12 +4149,12 @@ static int outer_child( /* Send the user maps we determined to the parent, so that it installs it in our user * namespace UID map table */ - for (size_t i = 0; i < bind_user_context->n_data; i++) { + FOREACH_ARRAY(d, bind_user_context->data, bind_user_context->n_data) { uid_t map[] = { - bind_user_context->data[i].payload_user->uid, - bind_user_context->data[i].host_user->uid, - (uid_t) bind_user_context->data[i].payload_group->gid, - (uid_t) bind_user_context->data[i].host_group->gid, + d->payload_user->uid, + d->host_user->uid, + (uid_t) d->payload_group->gid, + (uid_t) d->host_group->gid, }; l = send(fd_outer_socket, map, sizeof(map), MSG_NOSIGNAL);