Merge pull request #29569 from YHNdnzj/foreach-array
core: use FOREACH_ARRAY more and some other minor changes
This commit is contained in:
@@ -1330,13 +1330,13 @@ static int exec_parameters_serialize(const ExecParameters *p, FILE *f, FDSet *fd
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (p->n_socket_fds) {
|
||||
if (p->n_socket_fds > 0) {
|
||||
r = serialize_item_format(f, "exec-parameters-n-socket-fds", "%zu", p->n_socket_fds);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
if (p->n_storage_fds) {
|
||||
if (p->n_storage_fds > 0) {
|
||||
r = serialize_item_format(f, "exec-parameters-n-storage-fds", "%zu", p->n_storage_fds);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -1348,11 +1348,11 @@ static int exec_parameters_serialize(const ExecParameters *p, FILE *f, FDSet *fd
|
||||
if (!p->fds)
|
||||
return -EINVAL;
|
||||
|
||||
for (size_t i = 0; i < p->n_socket_fds + p->n_storage_fds; ++i) {
|
||||
FOREACH_ARRAY(fd, p->fds, p->n_socket_fds + p->n_storage_fds) {
|
||||
int copy = -EBADF;
|
||||
|
||||
if (p->fds[i] >= 0) {
|
||||
copy = fdset_put_dup(fds, p->fds[i]);
|
||||
if (*fd >= 0) {
|
||||
copy = fdset_put_dup(fds, *fd);
|
||||
if (copy < 0)
|
||||
return copy;
|
||||
}
|
||||
@@ -2118,20 +2118,20 @@ static int exec_context_serialize(const ExecContext *c, FILE *f) {
|
||||
if (asprintf(&value, "%04o", c->directories[dt].mode) < 0)
|
||||
return log_oom_debug();
|
||||
|
||||
for (size_t i = 0; i < c->directories[dt].n_items; i++) {
|
||||
FOREACH_ARRAY(i, c->directories[dt].items, c->directories[dt].n_items) {
|
||||
_cleanup_free_ char *path_escaped = NULL;
|
||||
|
||||
path_escaped = shell_escape(c->directories[dt].items[i].path, ":");
|
||||
path_escaped = shell_escape(i->path, ":");
|
||||
if (!path_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
if (!strextend(&value, " ", path_escaped))
|
||||
return log_oom_debug();
|
||||
|
||||
if (!strextend(&value, ":", yes_no(c->directories[dt].items[i].only_create)))
|
||||
if (!strextend(&value, ":", yes_no(i->only_create)))
|
||||
return log_oom_debug();
|
||||
|
||||
STRV_FOREACH(d, c->directories[dt].items[i].symlinks) {
|
||||
STRV_FOREACH(d, i->symlinks) {
|
||||
_cleanup_free_ char *link_escaped = NULL;
|
||||
|
||||
link_escaped = shell_escape(*d, ":");
|
||||
@@ -2368,8 +2368,8 @@ static int exec_context_serialize(const ExecContext *c, FILE *f) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
for (size_t j = 0; j < c->n_log_extra_fields; j++) {
|
||||
r = serialize_item(f, "exec-context-log-extra-fields", c->log_extra_fields[j].iov_base);
|
||||
FOREACH_ARRAY(field, c->log_extra_fields, c->n_log_extra_fields) {
|
||||
r = serialize_item(f, "exec-context-log-extra-fields", field->iov_base);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
@@ -2450,40 +2450,39 @@ static int exec_context_serialize(const ExecContext *c, FILE *f) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
for (size_t i = 0; i < c->n_bind_mounts; i++) {
|
||||
FOREACH_ARRAY(mount, c->bind_mounts, c->n_bind_mounts) {
|
||||
_cleanup_free_ char *src_escaped = NULL, *dst_escaped = NULL;
|
||||
|
||||
src_escaped = shell_escape(c->bind_mounts[i].source, ":");
|
||||
src_escaped = shell_escape(mount->source, ":");
|
||||
if (!src_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
dst_escaped = shell_escape(c->bind_mounts[i].destination, ":");
|
||||
dst_escaped = shell_escape(mount->destination, ":");
|
||||
if (!dst_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
r = serialize_item_format(f,
|
||||
c->bind_mounts[i].read_only ? "exec-context-bind-read-only-path" : "exec-context-bind-path",
|
||||
mount->read_only ? "exec-context-bind-read-only-path" : "exec-context-bind-path",
|
||||
"%s%s:%s:%s",
|
||||
c->bind_mounts[i].ignore_enoent ? "-" : "",
|
||||
mount->ignore_enoent ? "-" : "",
|
||||
src_escaped,
|
||||
dst_escaped,
|
||||
c->bind_mounts[i].recursive ? "rbind" : "norbind");
|
||||
mount->recursive ? "rbind" : "norbind");
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < c->n_temporary_filesystems; i++) {
|
||||
const TemporaryFileSystem *t = c->temporary_filesystems + i;
|
||||
FOREACH_ARRAY(tmpfs, c->temporary_filesystems, c->n_temporary_filesystems) {
|
||||
_cleanup_free_ char *escaped = NULL;
|
||||
|
||||
if (!isempty(t->options)) {
|
||||
escaped = shell_escape(t->options, ":");
|
||||
if (!isempty(tmpfs->options)) {
|
||||
escaped = shell_escape(tmpfs->options, ":");
|
||||
if (!escaped)
|
||||
return log_oom_debug();
|
||||
}
|
||||
|
||||
r = serialize_item_format(f, "exec-context-temporary-filesystems", "%s%s%s",
|
||||
t->path,
|
||||
tmpfs->path,
|
||||
isempty(escaped) ? "" : ":",
|
||||
strempty(escaped));
|
||||
if (r < 0)
|
||||
@@ -2642,25 +2641,25 @@ static int exec_context_serialize(const ExecContext *c, FILE *f) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
for (size_t i = 0; i < c->n_mount_images; i++) {
|
||||
FOREACH_ARRAY(mount, c->mount_images, c->n_mount_images) {
|
||||
_cleanup_free_ char *s = NULL, *source_escaped = NULL, *dest_escaped = NULL;
|
||||
|
||||
source_escaped = shell_escape(c->mount_images[i].source, " ");
|
||||
source_escaped = shell_escape(mount->source, " ");
|
||||
if (!source_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
dest_escaped = shell_escape(c->mount_images[i].destination, " ");
|
||||
dest_escaped = shell_escape(mount->destination, " ");
|
||||
if (!dest_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
s = strjoin(c->mount_images[i].ignore_enoent ? "-" : "",
|
||||
s = strjoin(mount->ignore_enoent ? "-" : "",
|
||||
source_escaped,
|
||||
" ",
|
||||
dest_escaped);
|
||||
if (!s)
|
||||
return log_oom_debug();
|
||||
|
||||
LIST_FOREACH(mount_options, o, c->mount_images[i].mount_options) {
|
||||
LIST_FOREACH(mount_options, o, mount->mount_options) {
|
||||
_cleanup_free_ char *escaped = NULL;
|
||||
|
||||
if (isempty(o->options))
|
||||
@@ -2683,19 +2682,19 @@ static int exec_context_serialize(const ExecContext *c, FILE *f) {
|
||||
return r;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < c->n_extension_images; i++) {
|
||||
FOREACH_ARRAY(mount, c->extension_images, c->n_extension_images) {
|
||||
_cleanup_free_ char *s = NULL, *source_escaped = NULL;
|
||||
|
||||
source_escaped = shell_escape(c->extension_images[i].source, ":");
|
||||
source_escaped = shell_escape(mount->source, ":");
|
||||
if (!source_escaped)
|
||||
return log_oom_debug();
|
||||
|
||||
s = strjoin(c->extension_images[i].ignore_enoent ? "-" : "",
|
||||
s = strjoin(mount->ignore_enoent ? "-" : "",
|
||||
source_escaped);
|
||||
if (!s)
|
||||
return log_oom_debug();
|
||||
|
||||
LIST_FOREACH(mount_options, o, c->extension_images[i].mount_options) {
|
||||
LIST_FOREACH(mount_options, o, mount->mount_options) {
|
||||
_cleanup_free_ char *escaped = NULL;
|
||||
|
||||
if (isempty(o->options))
|
||||
|
||||
+57
-54
@@ -427,7 +427,8 @@ int exec_spawn(Unit *unit,
|
||||
xsprintf(serialization_fd_number, "%i", fileno(f));
|
||||
|
||||
/* The executor binary is pinned, to avoid compatibility problems during upgrades. */
|
||||
r = posix_spawn_wrapper(FORMAT_PROC_FD_PATH(unit->manager->executor_fd),
|
||||
r = posix_spawn_wrapper(
|
||||
FORMAT_PROC_FD_PATH(unit->manager->executor_fd),
|
||||
STRV_MAKE(executor_path,
|
||||
"--deserialize", serialization_fd_number,
|
||||
"--log-level", log_level,
|
||||
@@ -589,13 +590,13 @@ int exec_context_destroy_runtime_directory(const ExecContext *c, const char *run
|
||||
if (!runtime_prefix)
|
||||
return 0;
|
||||
|
||||
for (size_t i = 0; i < c->directories[EXEC_DIRECTORY_RUNTIME].n_items; i++) {
|
||||
FOREACH_ARRAY(i, c->directories[EXEC_DIRECTORY_RUNTIME].items, c->directories[EXEC_DIRECTORY_RUNTIME].n_items) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
|
||||
if (exec_directory_is_private(c, EXEC_DIRECTORY_RUNTIME))
|
||||
p = path_join(runtime_prefix, "private", c->directories[EXEC_DIRECTORY_RUNTIME].items[i].path);
|
||||
p = path_join(runtime_prefix, "private", i->path);
|
||||
else
|
||||
p = path_join(runtime_prefix, c->directories[EXEC_DIRECTORY_RUNTIME].items[i].path);
|
||||
p = path_join(runtime_prefix, i->path);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -603,7 +604,7 @@ int exec_context_destroy_runtime_directory(const ExecContext *c, const char *run
|
||||
* service next. */
|
||||
(void) rm_rf(p, REMOVE_ROOT);
|
||||
|
||||
STRV_FOREACH(symlink, c->directories[EXEC_DIRECTORY_RUNTIME].items[i].symlinks) {
|
||||
STRV_FOREACH(symlink, i->symlinks) {
|
||||
_cleanup_free_ char *symlink_abs = NULL;
|
||||
|
||||
if (exec_directory_is_private(c, EXEC_DIRECTORY_RUNTIME))
|
||||
@@ -645,8 +646,8 @@ void exec_command_done(ExecCommand *c) {
|
||||
}
|
||||
|
||||
void exec_command_done_array(ExecCommand *c, size_t n) {
|
||||
for (size_t i = 0; i < n; i++)
|
||||
exec_command_done(c+i);
|
||||
FOREACH_ARRAY(i, c, n)
|
||||
exec_command_done(i);
|
||||
}
|
||||
|
||||
ExecCommand* exec_command_free_list(ExecCommand *c) {
|
||||
@@ -661,18 +662,18 @@ ExecCommand* exec_command_free_list(ExecCommand *c) {
|
||||
}
|
||||
|
||||
void exec_command_free_array(ExecCommand **c, size_t n) {
|
||||
for (size_t i = 0; i < n; i++)
|
||||
c[i] = exec_command_free_list(c[i]);
|
||||
FOREACH_ARRAY(i, c, n)
|
||||
*i = exec_command_free_list(*i);
|
||||
}
|
||||
|
||||
void exec_command_reset_status_array(ExecCommand *c, size_t n) {
|
||||
for (size_t i = 0; i < n; i++)
|
||||
exec_status_reset(&c[i].exec_status);
|
||||
FOREACH_ARRAY(i, c, n)
|
||||
exec_status_reset(&i->exec_status);
|
||||
}
|
||||
|
||||
void exec_command_reset_status_list_array(ExecCommand **c, size_t n) {
|
||||
for (size_t i = 0; i < n; i++)
|
||||
LIST_FOREACH(command, z, c[i])
|
||||
FOREACH_ARRAY(i, c, n)
|
||||
LIST_FOREACH(command, z, *i)
|
||||
exec_status_reset(&z->exec_status);
|
||||
}
|
||||
|
||||
@@ -749,10 +750,10 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
|
||||
/* When we don't match anything, -ENOENT should be returned */
|
||||
assert(pglob.gl_pathc > 0);
|
||||
|
||||
for (size_t n = 0; n < pglob.gl_pathc; n++) {
|
||||
FOREACH_ARRAY(path, pglob.gl_pathv, pglob.gl_pathc) {
|
||||
_cleanup_strv_free_ char **p = NULL;
|
||||
|
||||
r = load_env_file(NULL, pglob.gl_pathv[n], &p);
|
||||
r = load_env_file(NULL, *path, &p);
|
||||
if (r < 0) {
|
||||
if (ignore)
|
||||
continue;
|
||||
@@ -763,7 +764,7 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
|
||||
if (p) {
|
||||
InvalidEnvInfo info = {
|
||||
.unit = unit,
|
||||
.path = pglob.gl_pathv[n]
|
||||
.path = *path,
|
||||
};
|
||||
|
||||
p = strv_env_clean_with_callback(p, invalid_env, &info);
|
||||
@@ -943,6 +944,9 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
|
||||
prefix, protect_proc_to_string(c->protect_proc),
|
||||
prefix, proc_subset_to_string(c->proc_subset));
|
||||
|
||||
if (c->set_login_environment >= 0)
|
||||
fprintf(f, "%sSetLoginEnvironment: %s\n", prefix, yes_no(c->set_login_environment > 0));
|
||||
|
||||
if (c->root_image)
|
||||
fprintf(f, "%sRootImage: %s\n", prefix, c->root_image);
|
||||
|
||||
@@ -1007,6 +1011,9 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
|
||||
|
||||
fprintf(f, "%sTimeoutCleanSec: %s\n", prefix, FORMAT_TIMESPAN(c->timeout_clean_usec, USEC_PER_SEC));
|
||||
|
||||
if (c->memory_ksm >= 0)
|
||||
fprintf(f, "%sMemoryKSM: %s\n", prefix, yes_no(c->memory_ksm > 0));
|
||||
|
||||
if (c->nice_set)
|
||||
fprintf(f, "%sNice: %i\n", prefix, c->nice);
|
||||
|
||||
@@ -1160,11 +1167,9 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < c->n_log_extra_fields; j++) {
|
||||
FOREACH_ARRAY(field, c->log_extra_fields, c->n_log_extra_fields) {
|
||||
fprintf(f, "%sLogExtraFields: ", prefix);
|
||||
fwrite(c->log_extra_fields[j].iov_base,
|
||||
1, c->log_extra_fields[j].iov_len,
|
||||
f);
|
||||
fwrite(field->iov_base, 1, field->iov_len, f);
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
@@ -1214,22 +1219,19 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
|
||||
strv_dump(f, prefix, "NoExecPaths", c->no_exec_paths);
|
||||
strv_dump(f, prefix, "ExecSearchPath", c->exec_search_path);
|
||||
|
||||
for (size_t i = 0; i < c->n_bind_mounts; i++)
|
||||
FOREACH_ARRAY(mount, c->bind_mounts, c->n_bind_mounts)
|
||||
fprintf(f, "%s%s: %s%s:%s:%s\n", prefix,
|
||||
c->bind_mounts[i].read_only ? "BindReadOnlyPaths" : "BindPaths",
|
||||
c->bind_mounts[i].ignore_enoent ? "-": "",
|
||||
c->bind_mounts[i].source,
|
||||
c->bind_mounts[i].destination,
|
||||
c->bind_mounts[i].recursive ? "rbind" : "norbind");
|
||||
|
||||
for (size_t i = 0; i < c->n_temporary_filesystems; i++) {
|
||||
const TemporaryFileSystem *t = c->temporary_filesystems + i;
|
||||
mount->read_only ? "BindReadOnlyPaths" : "BindPaths",
|
||||
mount->ignore_enoent ? "-": "",
|
||||
mount->source,
|
||||
mount->destination,
|
||||
mount->recursive ? "rbind" : "norbind");
|
||||
|
||||
FOREACH_ARRAY(tmpfs, c->temporary_filesystems, c->n_temporary_filesystems)
|
||||
fprintf(f, "%sTemporaryFileSystem: %s%s%s\n", prefix,
|
||||
t->path,
|
||||
isempty(t->options) ? "" : ":",
|
||||
strempty(t->options));
|
||||
}
|
||||
tmpfs->path,
|
||||
isempty(tmpfs->options) ? "" : ":",
|
||||
strempty(tmpfs->options));
|
||||
|
||||
if (c->utmp_id)
|
||||
fprintf(f,
|
||||
@@ -1345,23 +1347,23 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < c->n_mount_images; i++) {
|
||||
FOREACH_ARRAY(mount, c->mount_images, c->n_mount_images) {
|
||||
fprintf(f, "%sMountImages: %s%s:%s", prefix,
|
||||
c->mount_images[i].ignore_enoent ? "-": "",
|
||||
c->mount_images[i].source,
|
||||
c->mount_images[i].destination);
|
||||
LIST_FOREACH(mount_options, o, c->mount_images[i].mount_options)
|
||||
mount->ignore_enoent ? "-": "",
|
||||
mount->source,
|
||||
mount->destination);
|
||||
LIST_FOREACH(mount_options, o, mount->mount_options)
|
||||
fprintf(f, ":%s:%s",
|
||||
partition_designator_to_string(o->partition_designator),
|
||||
strempty(o->options));
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < c->n_extension_images; i++) {
|
||||
FOREACH_ARRAY(mount, c->extension_images, c->n_extension_images) {
|
||||
fprintf(f, "%sExtensionImages: %s%s", prefix,
|
||||
c->extension_images[i].ignore_enoent ? "-": "",
|
||||
c->extension_images[i].source);
|
||||
LIST_FOREACH(mount_options, o, c->extension_images[i].mount_options)
|
||||
mount->ignore_enoent ? "-": "",
|
||||
mount->source);
|
||||
LIST_FOREACH(mount_options, o, mount->mount_options)
|
||||
fprintf(f, ":%s:%s",
|
||||
partition_designator_to_string(o->partition_designator),
|
||||
strempty(o->options));
|
||||
@@ -1418,8 +1420,9 @@ bool exec_context_get_effective_mount_apivfs(const ExecContext *c) {
|
||||
void exec_context_free_log_extra_fields(ExecContext *c) {
|
||||
assert(c);
|
||||
|
||||
for (size_t l = 0; l < c->n_log_extra_fields; l++)
|
||||
free(c->log_extra_fields[l].iov_base);
|
||||
FOREACH_ARRAY(field, c->log_extra_fields, c->n_log_extra_fields)
|
||||
free(field->iov_base);
|
||||
|
||||
c->log_extra_fields = mfree(c->log_extra_fields);
|
||||
c->n_log_extra_fields = 0;
|
||||
}
|
||||
@@ -1487,10 +1490,10 @@ int exec_context_get_clean_directories(
|
||||
if (!prefix[t])
|
||||
continue;
|
||||
|
||||
for (size_t i = 0; i < c->directories[t].n_items; i++) {
|
||||
FOREACH_ARRAY(i, c->directories[t].items, c->directories[t].n_items) {
|
||||
char *j;
|
||||
|
||||
j = path_join(prefix[t], c->directories[t].items[i].path);
|
||||
j = path_join(prefix[t], i->path);
|
||||
if (!j)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -1500,7 +1503,7 @@ int exec_context_get_clean_directories(
|
||||
|
||||
/* Also remove private directories unconditionally. */
|
||||
if (t != EXEC_DIRECTORY_CONFIGURATION) {
|
||||
j = path_join(prefix[t], "private", c->directories[t].items[i].path);
|
||||
j = path_join(prefix[t], "private", i->path);
|
||||
if (!j)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -1509,7 +1512,7 @@ int exec_context_get_clean_directories(
|
||||
return r;
|
||||
}
|
||||
|
||||
STRV_FOREACH(symlink, c->directories[t].items[i].symlinks) {
|
||||
STRV_FOREACH(symlink, i->symlinks) {
|
||||
j = path_join(prefix[t], *symlink);
|
||||
if (!j)
|
||||
return -ENOMEM;
|
||||
@@ -2303,9 +2306,9 @@ void exec_directory_done(ExecDirectory *d) {
|
||||
if (!d)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < d->n_items; i++) {
|
||||
free(d->items[i].path);
|
||||
strv_free(d->items[i].symlinks);
|
||||
FOREACH_ARRAY(i, d->items, d->n_items) {
|
||||
free(i->path);
|
||||
strv_free(i->symlinks);
|
||||
}
|
||||
|
||||
d->items = mfree(d->items);
|
||||
@@ -2317,9 +2320,9 @@ static ExecDirectoryItem *exec_directory_find(ExecDirectory *d, const char *path
|
||||
assert(d);
|
||||
assert(path);
|
||||
|
||||
for (size_t i = 0; i < d->n_items; i++)
|
||||
if (path_equal(d->items[i].path, path))
|
||||
return &d->items[i];
|
||||
FOREACH_ARRAY(i, d->items, d->n_items)
|
||||
if (path_equal(i->path, path))
|
||||
return i;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user