]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace FOREACH_POINTER with FOREACH_ARGUMENT 31072/head
authorMike Yuan <me@yhndnzj.com>
Wed, 24 Jan 2024 08:51:58 +0000 (16:51 +0800)
committerMike Yuan <me@yhndnzj.com>
Wed, 24 Jan 2024 13:29:11 +0000 (21:29 +0800)
The latter is more generic and while being compatible with
the former.

src/basic/macro.h
src/basic/mountpoint-util.c
src/core/unit.c
src/home/homework-fscrypt.c
src/home/homework-luks.c
src/import/pull-common.c
src/import/pull-raw.c
src/import/pull-tar.c
src/network/wait-online/manager.c
src/shared/condition.c
src/test/test-macro.c

index a78cec9b4e641e6e2b5a20caa5d3f45d284af07b..fe78363b8615c9ccfa8e912c65681d861cccbaf5 100644 (file)
@@ -266,12 +266,6 @@ static inline int __coverity_check_and_return__(int condition) {
 /* Pointers range from NULL to POINTER_MAX */
 #define POINTER_MAX ((void*) UINTPTR_MAX)
 
-/* Iterates through a specified list of pointers. Accepts NULL pointers, but uses POINTER_MAX as internal marker for EOL. */
-#define FOREACH_POINTER(p, x, ...)                                                       \
-        for (typeof(p) *_l = (typeof(p)[]) { ({ p = x; }), ##__VA_ARGS__, POINTER_MAX }; \
-             p != (typeof(p)) POINTER_MAX;                                               \
-             p = *(++_l))
-
 #define _FOREACH_ARRAY(i, array, num, m, end)                           \
         for (typeof(array[0]) *i = (array), *end = ({                   \
                                 typeof(num) m = (num);                  \
index bf67f7e01a3e981d65add0b704b7f7c83b10f7ee..0ff1ed601878682d93a63d14af54823cf8291158 100644 (file)
@@ -446,14 +446,15 @@ bool fstype_needs_quota(const char *fstype) {
 }
 
 bool fstype_is_api_vfs(const char *fstype) {
-        const FilesystemSet *fs;
+        assert(fstype);
 
-        FOREACH_POINTER(fs,
-                filesystem_sets + FILESYSTEM_SET_BASIC_API,
-                filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
-                filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
-                filesystem_sets + FILESYSTEM_SET_TEMPORARY)
-            if (nulstr_contains(fs->value, fstype))
+        const FilesystemSet *fs;
+        FOREACH_ARGUMENT(fs,
+                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
+                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
+                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
+                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
+                if (nulstr_contains(fs->value, fstype))
                     return true;
 
         /* Filesystems not present in the internal database */
index c199318b6eb0e4b194b74824ee23f369ac3324ee..6496fc96d4152ca030e61d7701edf384868504ae 100644 (file)
@@ -3959,7 +3959,7 @@ static int unit_pid_set(Unit *u, Set **pid_set) {
         /* Exclude the main/control pids from being killed via the cgroup */
 
         PidRef *pid;
-        FOREACH_POINTER(pid, unit_main_pid(u), unit_control_pid(u))
+        FOREACH_ARGUMENT(pid, unit_main_pid(u), unit_control_pid(u))
                 if (pidref_is_set(pid)) {
                         r = set_ensure_put(pid_set, NULL, PID_TO_PTR(pid->pid));
                         if (r < 0)
index d737f6db08422fc573a6a7b474a68f1a7e487e85..a4357c2d7bca31c081d1861d3d0721658b544d43 100644 (file)
@@ -219,7 +219,6 @@ static int fscrypt_setup(
                 _cleanup_free_ char *value = NULL;
                 size_t salt_size, encrypted_size;
                 const char *nr, *e;
-                char **list;
                 int n;
 
                 /* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32-bit unsigned integer */
@@ -248,21 +247,19 @@ static int fscrypt_setup(
                         return log_error_errno(r, "Failed to decode encrypted key of %s: %m", xa);
 
                 r = -ENOANO;
-                FOREACH_POINTER(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
+                char **list;
+                FOREACH_ARGUMENT(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
                         r = fscrypt_slot_try_many(
                                         list,
                                         salt, salt_size,
                                         encrypted, encrypted_size,
                                         setup->fscrypt_key_descriptor,
                                         ret_volume_key, ret_volume_key_size);
-                        if (r != -ENOANO)
-                                break;
-                }
-                if (r < 0) {
+                        if (r >= 0)
+                                return 0;
                         if (r != -ENOANO)
                                 return r;
-                } else
-                        return 0;
+                }
         }
 
         return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to set up home directory with provided passwords.");
index a66ebb6ca5e48b7a61b2ac32034e8f7facd8fa50..e9c1933dfe9ec1813e5fe0f6c94bc468267ec829 100644 (file)
@@ -365,7 +365,6 @@ static int luks_setup(
         _cleanup_(erase_and_freep) void *vk = NULL;
         sd_id128_t p;
         size_t vks;
-        char **list;
         int r;
 
         assert(h);
@@ -419,11 +418,13 @@ static int luks_setup(
                 return log_oom();
 
         r = -ENOKEY;
-        FOREACH_POINTER(list,
-                        cache ? cache->keyring_passswords : NULL,
-                        cache ? cache->pkcs11_passwords : NULL,
-                        cache ? cache->fido2_passwords : NULL,
-                        passwords) {
+        char **list;
+        FOREACH_ARGUMENT(list,
+                         cache ? cache->keyring_passswords : NULL,
+                         cache ? cache->pkcs11_passwords : NULL,
+                         cache ? cache->fido2_passwords : NULL,
+                         passwords) {
+
                 r = luks_try_passwords(h, cd, list, vk, &vks, ret_key_serial ? &key_serial : NULL);
                 if (r != -ENOKEY)
                         break;
@@ -519,7 +520,6 @@ static int luks_open(
 
         _cleanup_(erase_and_freep) void *vk = NULL;
         sd_id128_t p;
-        char **list;
         size_t vks;
         int r;
 
@@ -560,11 +560,13 @@ static int luks_open(
                 return log_oom();
 
         r = -ENOKEY;
-        FOREACH_POINTER(list,
-                        cache ? cache->keyring_passswords : NULL,
-                        cache ? cache->pkcs11_passwords : NULL,
-                        cache ? cache->fido2_passwords : NULL,
-                        h->password) {
+        char **list;
+        FOREACH_ARGUMENT(list,
+                         cache ? cache->keyring_passswords : NULL,
+                         cache ? cache->pkcs11_passwords : NULL,
+                         cache ? cache->fido2_passwords : NULL,
+                         h->password) {
+
                 r = luks_try_passwords(h, setup->crypt_device, list, vk, &vks, NULL);
                 if (r != -ENOKEY)
                         break;
@@ -3583,7 +3585,6 @@ int home_passwd_luks(
         _cleanup_(erase_and_freep) void *volume_key = NULL;
         struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf;
         const char *type;
-        char **list;
         int r;
 
         assert(h);
@@ -3613,11 +3614,12 @@ int home_passwd_luks(
                 return log_oom();
 
         r = -ENOKEY;
-        FOREACH_POINTER(list,
-                        cache ? cache->keyring_passswords : NULL,
-                        cache ? cache->pkcs11_passwords : NULL,
-                        cache ? cache->fido2_passwords : NULL,
-                        h->password) {
+        char **list;
+        FOREACH_ARGUMENT(list,
+                         cache ? cache->keyring_passswords : NULL,
+                         cache ? cache->pkcs11_passwords : NULL,
+                         cache ? cache->fido2_passwords : NULL,
+                         h->password) {
 
                 r = luks_try_passwords(h, setup->crypt_device, list, volume_key, &volume_key_size, NULL);
                 if (r != -ENOKEY)
@@ -3736,7 +3738,6 @@ static int luks_try_resume(
 }
 
 int home_unlock_luks(UserRecord *h, HomeSetup *setup, const PasswordCache *cache) {
-        char **list;
         int r;
 
         assert(h);
@@ -3750,10 +3751,12 @@ int home_unlock_luks(UserRecord *h, HomeSetup *setup, const PasswordCache *cache
         log_info("Discovered used LUKS device %s.", setup->dm_node);
 
         r = -ENOKEY;
-        FOREACH_POINTER(list,
-                        cache ? cache->pkcs11_passwords : NULL,
-                        cache ? cache->fido2_passwords : NULL,
-                        h->password) {
+        char **list;
+        FOREACH_ARGUMENT(list,
+                         cache ? cache->pkcs11_passwords : NULL,
+                         cache ? cache->fido2_passwords : NULL,
+                         h->password) {
+
                 r = luks_try_resume(setup->crypt_device, setup->dm_name, list);
                 if (r != -ENOKEY)
                         break;
index 5e1ea20a032f03a9584e8efb4ea9c2e0a6601eb3..37387a76e024572d1e7d56ef5955c5a46190f0b0 100644 (file)
@@ -550,7 +550,6 @@ int pull_verify(ImportVerify verify,
                 log_debug("Main download is a checksum file, can't validate its checksum with itself, skipping.");
                 verify_job = main_job;
         } else {
-                PullJob *j;
                 assert(main_job->calc_checksum);
                 assert(main_job->checksum);
                 assert(checksum_job);
@@ -560,7 +559,8 @@ int pull_verify(ImportVerify verify,
                         return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
                                                "Checksum is empty, cannot verify.");
 
-                FOREACH_POINTER(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) {
+                PullJob *j;
+                FOREACH_ARGUMENT(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) {
                         r = verify_one(checksum_job, j);
                         if (r < 0)
                                 return r;
index 66c3f656656a8678e78a1057aa83b5a5249e3299..dc5434cd2d520ade31415423b5d1e67598324381 100644 (file)
@@ -495,7 +495,6 @@ static int raw_pull_rename_auxiliary_file(
 
 static void raw_pull_job_on_finished(PullJob *j) {
         RawPull *i;
-        PullJob *jj;
         int r;
 
         assert(j);
@@ -568,8 +567,9 @@ static void raw_pull_job_on_finished(PullJob *j) {
                 }
         }
 
+        PullJob *jj;
         /* Let's close these auxiliary files now, we don't need access to them anymore. */
-        FOREACH_POINTER(jj, i->settings_job, i->roothash_job, i->roothash_signature_job, i->verity_job)
+        FOREACH_ARGUMENT(jj, i->settings_job, i->roothash_job, i->roothash_signature_job, i->verity_job)
                 pull_job_close_disk_fd(jj);
 
         if (!i->raw_job->etag_exists) {
@@ -820,7 +820,6 @@ int raw_pull_start(
                 ImportVerify verify,
                 const char *checksum) {
 
-        PullJob *j;
         int r;
 
         assert(i);
@@ -959,14 +958,15 @@ int raw_pull_start(
                         return r;
         }
 
-        FOREACH_POINTER(j,
-                        i->raw_job,
-                        i->checksum_job,
-                        i->signature_job,
-                        i->settings_job,
-                        i->roothash_job,
-                        i->roothash_signature_job,
-                        i->verity_job) {
+        PullJob *j;
+        FOREACH_ARGUMENT(j,
+                         i->raw_job,
+                         i->checksum_job,
+                         i->signature_job,
+                         i->settings_job,
+                         i->roothash_job,
+                         i->roothash_signature_job,
+                         i->verity_job) {
 
                 if (!j)
                         continue;
index c32fc290143e37536d0acb7e6e30d0b6b587461e..9e185c255ab2c57f25efdc737b5544d05d9512d0 100644 (file)
@@ -581,7 +581,6 @@ int tar_pull_start(
                 ImportVerify verify,
                 const char *checksum) {
 
-        PullJob *j;
         int r;
 
         assert(i);
@@ -656,11 +655,12 @@ int tar_pull_start(
                         return r;
         }
 
-        FOREACH_POINTER(j,
-                        i->tar_job,
-                        i->checksum_job,
-                        i->signature_job,
-                        i->settings_job) {
+        PullJob *j;
+        FOREACH_ARGUMENT(j,
+                         i->tar_job,
+                         i->checksum_job,
+                         i->signature_job,
+                         i->settings_job) {
 
                 if (!j)
                         continue;
index da5eed5d73b9e2299559771b4be1c24e1abdef24..0d1fdc372f684cb7ec72f55003ce6e98275281bf 100644 (file)
@@ -92,7 +92,7 @@ static int manager_link_is_online(Manager *m, Link *l, const LinkOperationalStat
                                             l->state);
 
         const LinkOperationalStateRange *range;
-        FOREACH_POINTER(range, state_range, &m->required_operstate, &l->required_operstate)
+        FOREACH_ARGUMENT(range, state_range, &m->required_operstate, &l->required_operstate)
                 if (operational_state_range_is_valid(range))
                         break;
         assert(range != POINTER_MAX);
index 7563f29c94fbc737e7a254056c39bfbb570e86e8..b08cd959a0bb1bc3b9c838a4bf3232aa6f22599f 100644 (file)
@@ -141,7 +141,6 @@ static int condition_test_kernel_command_line(Condition *c, char **env) {
 }
 
 static int condition_test_credential(Condition *c, char **env) {
-        int (*gd)(const char **ret);
         int r;
 
         assert(c);
@@ -155,7 +154,8 @@ static int condition_test_credential(Condition *c, char **env) {
         if (!credential_name_valid(c->parameter)) /* credentials with invalid names do not exist */
                 return false;
 
-        FOREACH_POINTER(gd, get_credentials_dir, get_encrypted_credentials_dir) {
+        int (*gd)(const char **ret);
+        FOREACH_ARGUMENT(gd, get_credentials_dir, get_encrypted_credentials_dir) {
                 _cleanup_free_ char *j = NULL;
                 const char *cd;
 
index b35cf64b94f43469ca36972cc57fb1082df70488..a461f4974984f72fb92a68c0abcb52057c67c78d 100644 (file)
@@ -221,85 +221,6 @@ TEST(IN_SET) {
         assert_se(!IN_SET(t.x, 2, 3, 4));
 }
 
-TEST(FOREACH_POINTER) {
-        int a, b, c, *i;
-        size_t k = 0;
-
-        FOREACH_POINTER(i, &a, &b, &c) {
-                switch (k) {
-
-                case 0:
-                        assert_se(i == &a);
-                        break;
-
-                case 1:
-                        assert_se(i == &b);
-                        break;
-
-                case 2:
-                        assert_se(i == &c);
-                        break;
-
-                default:
-                        assert_not_reached();
-                        break;
-                }
-
-                k++;
-        }
-
-        assert_se(k == 3);
-
-        FOREACH_POINTER(i, &b) {
-                assert_se(k == 3);
-                assert_se(i == &b);
-                k = 4;
-        }
-
-        assert_se(k == 4);
-
-        FOREACH_POINTER(i, NULL, &c, NULL, &b, NULL, &a, NULL) {
-                switch (k) {
-
-                case 4:
-                        assert_se(i == NULL);
-                        break;
-
-                case 5:
-                        assert_se(i == &c);
-                        break;
-
-                case 6:
-                        assert_se(i == NULL);
-                        break;
-
-                case 7:
-                        assert_se(i == &b);
-                        break;
-
-                case 8:
-                        assert_se(i == NULL);
-                        break;
-
-                case 9:
-                        assert_se(i == &a);
-                        break;
-
-                case 10:
-                        assert_se(i == NULL);
-                        break;
-
-                default:
-                        assert_not_reached();
-                        break;
-                }
-
-                k++;
-        }
-
-        assert_se(k == 11);
-}
-
 TEST(FOREACH_ARGUMENT) {
         size_t i;