From 1c2d424e45286d06c725e5b8ea62c67a787fed59 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Oct 2023 06:01:11 +0800 Subject: [PATCH 1/4] core/execute-serialize: use FOREACH_ARRAY more --- src/core/execute-serialize.c | 61 ++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/core/execute-serialize.c b/src/core/execute-serialize.c index 0d9204fc14..3427e48f48 100644 --- a/src/core/execute-serialize.c +++ b/src/core/execute-serialize.c @@ -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)) From f7df8adb0cdfac2ea7bd4b7b4a1dffa7525fda07 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Oct 2023 06:17:11 +0800 Subject: [PATCH 2/4] core/execute: use FOREACH_ARRAY more --- src/core/execute.c | 102 ++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/src/core/execute.c b/src/core/execute.c index e4e4008cc8..192925e999 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -589,13 +589,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 +603,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 +645,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 +661,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 +749,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 +763,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); @@ -1160,11 +1160,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 +1212,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 +1340,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 +1413,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 +1483,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 +1496,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 +1505,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 +2299,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 +2313,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; } From 4520681da1f8cc080ff93401237d652abc447e59 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Oct 2023 06:02:17 +0800 Subject: [PATCH 3/4] core/execute: correct alignment --- src/core/execute.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/execute.c b/src/core/execute.c index 192925e999..d639223987 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -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, From 06120a157570eb699858da36fc52054d0e016ca3 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 14 Oct 2023 06:21:35 +0800 Subject: [PATCH 4/4] core: add MemoryKSM and SetLoginEnvironment to exec_context_dump --- src/core/execute.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/execute.c b/src/core/execute.c index d639223987..06fcc741f8 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -944,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); @@ -1008,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);