]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn: use FOREACH_ARRAY() where applicable
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 17 Feb 2025 06:09:50 +0000 (15:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Feb 2025 14:24:20 +0000 (23:24 +0900)
src/nspawn/nspawn-bind-user.c
src/nspawn/nspawn-mount.c
src/nspawn/nspawn-oci.c
src/nspawn/nspawn-register.c
src/nspawn/nspawn.c

index 8964de22a1106bcad3d840a6aef2692359a41e4a..4ec8393b02c57b76d71295beff3d53b0ffa5a3ce 100644 (file)
@@ -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);
index 552d629a188efe4e1b92ab389f34c0688467dd93..3e69781572fe791371a819249f2363c10829d7d7 100644 (file)
@@ -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;
index 1b2fe76f8ac9d70ae31f8844ab980c1deb88520e..ecbcaefcbb1c4071b9a0c9f7140aec6eeb078d5f 100644 (file)
@@ -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;
 
index 009f71f59fe15b5dc8ac1ccb82ba4cf2fee21c42..4193a338137de3ba7fe917fffc465b3498b60fc1 100644 (file)
@@ -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;
 
index 4c054b2dbb85056193f052f4c723a2505f59333e..8fa05b9bc2346b32446bd2eb1a8305c9b6d59b35 100644 (file)
@@ -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);