From: Zbigniew Jędrzejewski-Szmek Date: Tue, 19 Nov 2024 14:48:31 +0000 (+0100) Subject: analyze: use STRV_FOREACH in consistent fashion X-Git-Tag: v258-rc1~987^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bc602f32c978e042809b7b15f09ddc6ff4e15896;p=thirdparty%2Fsystemd.git analyze: use STRV_FOREACH in consistent fashion Also put positive condition first. --- diff --git a/src/analyze/analyze-architectures.c b/src/analyze/analyze-architectures.c index 08c62e62859..832d50c5b71 100644 --- a/src/analyze/analyze-architectures.c +++ b/src/analyze/analyze-architectures.c @@ -47,30 +47,25 @@ int verb_architectures(int argc, char *argv[], void *userdata) { (void) table_hide_column_from_display(table, (size_t) 0); - if (strv_isempty(strv_skip(argv, 1))) - for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) { - r = add_arch(table, a); - if (r < 0) - return r; - } - else { - STRV_FOREACH(as, strv_skip(argv, 1)) { + char **args = strv_skip(argv, 1); + if (args) { + STRV_FOREACH(arg, args) { Architecture a; - if (streq(*as, "native")) + if (streq(*arg, "native")) a = native_architecture(); - else if (streq(*as, "uname")) + else if (streq(*arg, "uname")) a = uname_architecture(); - else if (streq(*as, "secondary")) { + else if (streq(*arg, "secondary")) { #ifdef ARCHITECTURE_SECONDARY a = ARCHITECTURE_SECONDARY; #else return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No secondary architecture."); #endif } else - a = architecture_from_string(*as); + a = architecture_from_string(*arg); if (a < 0) - return log_error_errno(a, "Architecture \"%s\" not known.", *as); + return log_error_errno(a, "Architecture \"%s\" not known.", *arg); r = add_arch(table, a); if (r < 0) @@ -78,7 +73,12 @@ int verb_architectures(int argc, char *argv[], void *userdata) { } (void) table_set_sort(table, (size_t) 0); - } + } else + for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) { + r = add_arch(table, a); + if (r < 0) + return r; + } r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); if (r < 0) diff --git a/src/analyze/analyze-cat-config.c b/src/analyze/analyze-cat-config.c index b480d4a0097..8b86ef9328e 100644 --- a/src/analyze/analyze-cat-config.c +++ b/src/analyze/analyze-cat-config.c @@ -9,12 +9,11 @@ #include "strv.h" int verb_cat_config(int argc, char *argv[], void *userdata) { - char **list; int r; pager_open(arg_pager_flags); - list = strv_skip(argv, 1); + char **list = strv_skip(argv, 1); STRV_FOREACH(arg, list) { const char *t = NULL; diff --git a/src/analyze/analyze-exit-status.c b/src/analyze/analyze-exit-status.c index 1032f1a4b7a..eb60aa0051d 100644 --- a/src/analyze/analyze-exit-status.c +++ b/src/analyze/analyze-exit-status.c @@ -17,25 +17,14 @@ int verb_exit_status(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to right-align status: %m"); - if (strv_isempty(strv_skip(argv, 1))) - for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) { - if (!exit_status_mappings[i].name) - continue; - - r = table_add_many(table, - TABLE_STRING, exit_status_mappings[i].name, - TABLE_INT, (int) i, - TABLE_STRING, exit_status_class(i)); - if (r < 0) - return table_log_add_error(r); - } - else - for (int i = 1; i < argc; i++) { + char **args = strv_skip(argv, 1); + if (args) + STRV_FOREACH(arg, args) { int status; - status = exit_status_from_string(argv[i]); + status = exit_status_from_string(*arg); if (status < 0) - return log_error_errno(status, "Invalid exit status \"%s\".", argv[i]); + return log_error_errno(status, "Invalid exit status \"%s\".", *arg); assert(status >= 0 && (size_t) status < ELEMENTSOF(exit_status_mappings)); r = table_add_many(table, @@ -45,6 +34,18 @@ int verb_exit_status(int argc, char *argv[], void *userdata) { if (r < 0) return table_log_add_error(r); } + else + for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) { + if (!exit_status_mappings[i].name) + continue; + + r = table_add_many(table, + TABLE_STRING, exit_status_mappings[i].name, + TABLE_INT, (int) i, + TABLE_STRING, exit_status_class(i)); + if (r < 0) + return table_log_add_error(r); + } r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); if (r < 0) diff --git a/src/analyze/analyze-filesystems.c b/src/analyze/analyze-filesystems.c index b09cd9fa0ee..8ec542ec7a2 100644 --- a/src/analyze/analyze-filesystems.c +++ b/src/analyze/analyze-filesystems.c @@ -106,15 +106,30 @@ static void dump_filesystem_set(const FilesystemSet *set) { } int verb_filesystems(int argc, char *argv[], void *userdata) { - bool first = true; - #if ! HAVE_LIBBPF return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with libbpf support, sorry."); #endif pager_open(arg_pager_flags); - if (strv_isempty(strv_skip(argv, 1))) { + char **args = strv_skip(argv, 1); + if (args) + STRV_FOREACH(name, args) { + if (name != args) + puts(""); + + const FilesystemSet *set = filesystem_set_find(*name); + if (!set) { + /* make sure the error appears below normal output */ + fflush(stdout); + + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), + "Filesystem set \"%s\" not found.", *name); + } + + dump_filesystem_set(set); + } + else { _cleanup_set_free_ Set *kernel = NULL, *known = NULL; int k; @@ -126,27 +141,24 @@ int verb_filesystems(int argc, char *argv[], void *userdata) { for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++) { const FilesystemSet *set = filesystem_sets + i; - if (!first) + if (i > 0) puts(""); dump_filesystem_set(set); filesystem_set_remove(kernel, set); if (i != FILESYSTEM_SET_KNOWN) filesystem_set_remove(known, set); - first = false; } if (arg_quiet) /* Let's not show the extra stuff in quiet mode */ return 0; if (!set_isempty(known)) { - _cleanup_free_ char **l = NULL; - printf("\n" "# %sUngrouped filesystems%s (known but not included in any of the groups except @known):\n", ansi_highlight(), ansi_normal()); - l = set_get_strv(known); + _cleanup_free_ char **l = set_get_strv(known); if (!l) return log_oom(); @@ -197,25 +209,7 @@ int verb_filesystems(int argc, char *argv[], void *userdata) { STRV_FOREACH(filesystem, l) printf("# %s\n", *filesystem); } - } else - STRV_FOREACH(name, strv_skip(argv, 1)) { - const FilesystemSet *set; - - if (!first) - puts(""); - - set = filesystem_set_find(*name); - if (!set) { - /* make sure the error appears below normal output */ - fflush(stdout); - - return log_error_errno(SYNTHETIC_ERRNO(ENOENT), - "Filesystem set \"%s\" not found.", *name); - } - - dump_filesystem_set(set); - first = false; - } + } return EXIT_SUCCESS; } diff --git a/src/analyze/analyze-malloc.c b/src/analyze/analyze-malloc.c index 9eb234e6690..0438d03ae8e 100644 --- a/src/analyze/analyze-malloc.c +++ b/src/analyze/analyze-malloc.c @@ -35,11 +35,12 @@ int verb_malloc(int argc, char *argv[], void *userdata) { char **services = STRV_MAKE("org.freedesktop.systemd1"); int r; - if (!strv_isempty(strv_skip(argv, 1))) { - services = strv_skip(argv, 1); - STRV_FOREACH(service, services) + char **args = strv_skip(argv, 1); + if (args) { + STRV_FOREACH(service, args) if (!service_name_is_valid(*service)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service); + services = args; } r = acquire_bus(&bus, NULL); diff --git a/src/analyze/analyze-pcrs.c b/src/analyze/analyze-pcrs.c index fb67a733ddf..23f8f0773d8 100644 --- a/src/analyze/analyze-pcrs.c +++ b/src/analyze/analyze-pcrs.c @@ -114,19 +114,14 @@ int verb_pcrs(int argc, char *argv[], void *userdata) { if (!alg) /* hide hash column if we couldn't acquire it */ (void) table_set_display(table, 0, 1); - if (strv_isempty(strv_skip(argv, 1))) - for (uint32_t pi = 0; pi < _TPM2_PCR_INDEX_MAX_DEFINED; pi++) { - r = add_pcr_to_table(table, alg, pi); - if (r < 0) - return r; - } - else { - for (int i = 1; i < argc; i++) { + char **args = strv_skip(argv, 1); + if (args) { + STRV_FOREACH(arg, args) { int pi; - pi = tpm2_pcr_index_from_string(argv[i]); + pi = tpm2_pcr_index_from_string(*arg); if (pi < 0) - return log_error_errno(pi, "PCR index \"%s\" not known.", argv[i]); + return log_error_errno(pi, "PCR index \"%s\" not known.", *arg); r = add_pcr_to_table(table, alg, pi); if (r < 0) @@ -134,7 +129,12 @@ int verb_pcrs(int argc, char *argv[], void *userdata) { } (void) table_set_sort(table, (size_t) 0); - } + } else + for (uint32_t pi = 0; pi < _TPM2_PCR_INDEX_MAX_DEFINED; pi++) { + r = add_pcr_to_table(table, alg, pi); + if (r < 0) + return r; + } r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true); if (r < 0) diff --git a/src/analyze/analyze-syscall-filter.c b/src/analyze/analyze-syscall-filter.c index 66a52da8976..fceeff82753 100644 --- a/src/analyze/analyze-syscall-filter.c +++ b/src/analyze/analyze-syscall-filter.c @@ -103,12 +103,30 @@ static void dump_syscall_filter(const SyscallFilterSet *set) { } int verb_syscall_filters(int argc, char *argv[], void *userdata) { - bool first = true; int r; pager_open(arg_pager_flags); - if (strv_isempty(strv_skip(argv, 1))) { + char **args = strv_skip(argv, 1); + if (args) + STRV_FOREACH(name, args) { + const SyscallFilterSet *set; + + if (name != args) + puts(""); + + set = syscall_filter_set_find(*name); + if (!set) { + /* make sure the error appears below normal output */ + fflush(stdout); + + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), + "Filter set \"%s\" not found.", *name); + } + + dump_syscall_filter(set); + } + else { _cleanup_set_free_ Set *kernel = NULL, *known = NULL; int k = 0; /* explicit initialization to appease gcc */ @@ -121,27 +139,24 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) { for (int i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) { const SyscallFilterSet *set = syscall_filter_sets + i; - if (!first) + if (i > 0) puts(""); dump_syscall_filter(set); syscall_set_remove(kernel, set); if (i != SYSCALL_FILTER_SET_KNOWN) syscall_set_remove(known, set); - first = false; } if (arg_quiet) /* Let's not show the extra stuff in quiet mode */ return 0; if (!set_isempty(known)) { - _cleanup_free_ char **l = NULL; - printf("\n" "# %sUngrouped System Calls%s (known but not included in any of the groups except @known):\n", ansi_highlight(), ansi_normal()); - l = set_get_strv(known); + _cleanup_free_ char **l = set_get_strv(known); if (!l) return log_oom(); @@ -157,13 +172,11 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) { if (!arg_quiet) log_notice_errno(k, "# Not showing unlisted system calls, couldn't retrieve kernel system call list: %m"); } else if (!set_isempty(kernel)) { - _cleanup_free_ char **l = NULL; - printf("\n" "# %sUnlisted System Calls%s (supported by the local kernel, but not included in any of the groups listed above):\n", ansi_highlight(), ansi_normal()); - l = set_get_strv(kernel); + _cleanup_free_ char **l = set_get_strv(kernel); if (!l) return log_oom(); @@ -172,25 +185,7 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) { STRV_FOREACH(syscall, l) printf("# %s\n", *syscall); } - } else - STRV_FOREACH(name, strv_skip(argv, 1)) { - const SyscallFilterSet *set; - - if (!first) - puts(""); - - set = syscall_filter_set_find(*name); - if (!set) { - /* make sure the error appears below normal output */ - fflush(stdout); - - return log_error_errno(SYNTHETIC_ERRNO(ENOENT), - "Filter set \"%s\" not found.", *name); - } - - dump_syscall_filter(set); - first = false; - } + } return EXIT_SUCCESS; } diff --git a/src/analyze/analyze-timestamp.c b/src/analyze/analyze-timestamp.c index 8c96a8ec55d..791fa28b1ff 100644 --- a/src/analyze/analyze-timestamp.c +++ b/src/analyze/analyze-timestamp.c @@ -75,11 +75,12 @@ static int test_timestamp_one(const char *p) { int verb_timestamp(int argc, char *argv[], void *userdata) { int r = 0; - STRV_FOREACH(p, strv_skip(argv, 1)) { - RET_GATHER(r, test_timestamp_one(*p)); + char **args = strv_skip(argv, 1); + STRV_FOREACH(arg, args) { + if (arg != args) + puts(""); - if (p[1]) - putchar('\n'); + RET_GATHER(r, test_timestamp_one(*arg)); } return r;