From fb8cc599ed863502d67da038ca9b8bae7d1184e0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 16 Nov 2023 16:08:15 +0100 Subject: [PATCH] analyze: add "architectures" verb that lists all known architectures --- man/systemd-analyze.xml | 32 +++++++++++ man/systemd.unit.xml | 4 ++ src/analyze/analyze-architectures.c | 88 +++++++++++++++++++++++++++++ src/analyze/analyze-architectures.h | 4 ++ src/analyze/analyze.c | 9 ++- src/analyze/meson.build | 1 + test/units/testsuite-65.sh | 7 +++ 7 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 src/analyze/analyze-architectures.c create mode 100644 src/analyze/analyze-architectures.h diff --git a/man/systemd-analyze.xml b/man/systemd-analyze.xml index 2f2873452a..ad6ba5c090 100644 --- a/man/systemd-analyze.xml +++ b/man/systemd-analyze.xml @@ -179,6 +179,12 @@ OPTIONS srk >FILE + + systemd-analyze + OPTIONS + architectures + NAME + @@ -941,6 +947,32 @@ NR NAME SHA256 systemd-analyze srk > srk.tpm2b_public + + <command>systemd-analyze architectures <optional><replaceable>NAME</replaceable>...</optional></command> + + Lists all known CPU architectures, and which ones are native. The listed architecture names are + those ConditionArchitecture= supports, see + systemd.unit5 for + details. If architecture names are specified only those specified are listed. + + + Table output + $ systemd-analyze architectures +NAME SUPPORT +alpha foreign +arc foreign +arc-be foreign +arm foreign +arm64 foreign +… +sparc foreign +sparc64 foreign +tilegx foreign +x86 secondary +x86-64 native + + + diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 190d4c1d2f..7fed74a227 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -1321,6 +1321,10 @@ arc-be, or native. + Use + systemd-analyze1 + for the complete list of known architectures. + The architecture is determined from the information returned by uname2 and is thus subject to diff --git a/src/analyze/analyze-architectures.c b/src/analyze/analyze-architectures.c new file mode 100644 index 0000000000..2d155d57c1 --- /dev/null +++ b/src/analyze/analyze-architectures.c @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "analyze.h" +#include "analyze-architectures.h" +#include "format-table.h" + +static int add_arch(Table *t, Architecture a) { + const char *c, *color; + int r; + + assert(t); + + if (a == native_architecture()) { + c = "native"; + color = ANSI_HIGHLIGHT_GREEN; + } else if (a == uname_architecture()) { + c = "uname"; + color = ANSI_HIGHLIGHT; +#ifdef ARCHITECTURE_SECONDARY + } else if (a == ARCHITECTURE_SECONDARY) { + c = "secondary"; + color = NULL; +#endif + } else { + c = "foreign"; + color = ANSI_GREY; + } + + r = table_add_many(t, + TABLE_INT, (int) a, + TABLE_STRING, architecture_to_string(a), + TABLE_STRING, c, + TABLE_SET_COLOR, color); + if (r < 0) + return table_log_add_error(r); + + return 0; +} + +int verb_architectures(int argc, char *argv[], void *userdata) { + _cleanup_(table_unrefp) Table *table = NULL; + int r; + + table = table_new("id", "name", "support"); + if (!table) + return log_oom(); + + (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)) { + Architecture a; + + if (streq(*as, "native")) + a = native_architecture(); + else if (streq(*as, "uname")) + a = uname_architecture(); + else if (streq(*as, "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); + if (a < 0) + return log_error_errno(a, "Architecture \"%s\" not known.", *as); + + r = add_arch(table, a); + if (r < 0) + return r; + } + + (void) table_set_sort(table, (size_t) 0); + } + + r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); + if (r < 0) + return log_error_errno(r, "Failed to output table: %m"); + + return EXIT_SUCCESS; +} diff --git a/src/analyze/analyze-architectures.h b/src/analyze/analyze-architectures.h new file mode 100644 index 0000000000..06b9473784 --- /dev/null +++ b/src/analyze/analyze-architectures.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +int verb_architectures(int argc, char *argv[], void *userdata); diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index d2be144f4f..7bbaf10567 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -13,6 +13,7 @@ #include "alloc-util.h" #include "analyze.h" +#include "analyze-architectures.h" #include "analyze-blame.h" #include "analyze-calendar.h" #include "analyze-capability.h" @@ -25,6 +26,7 @@ #include "analyze-exit-status.h" #include "analyze-fdstore.h" #include "analyze-filesystems.h" +#include "analyze-image-policy.h" #include "analyze-inspect-elf.h" #include "analyze-log-control.h" #include "analyze-malloc.h" @@ -41,7 +43,6 @@ #include "analyze-unit-files.h" #include "analyze-unit-paths.h" #include "analyze-verify.h" -#include "analyze-image-policy.h" #include "build.h" #include "bus-error.h" #include "bus-locator.h" @@ -224,6 +225,7 @@ static int help(int argc, char *argv[], void *userdata) { " capability [CAP...] List capability definitions\n" " syscall-filter [NAME...] List syscalls in seccomp filters\n" " filesystems [NAME...] List known filesystems\n" + " architectures [NAME...] List known architectures\n" " condition CONDITION... Evaluate conditions and asserts\n" " compare-versions VERSION1 [OP] VERSION2\n" " Compare two version strings\n" @@ -556,9 +558,9 @@ static int parse_argv(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --offline= is only supported for security right now."); - if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs")) + if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures")) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs right now."); + "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs, architectures right now."); if (arg_threshold != 100 && !streq_ptr(argv[optind], "security")) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), @@ -649,6 +651,7 @@ static int run(int argc, char *argv[]) { { "image-policy", 2, 2, 0, verb_image_policy }, { "pcrs", VERB_ANY, VERB_ANY, 0, verb_pcrs }, { "srk", VERB_ANY, 1, 0, verb_srk }, + { "architectures", VERB_ANY, VERB_ANY, 0, verb_architectures }, {} }; diff --git a/src/analyze/meson.build b/src/analyze/meson.build index a50544730b..f150ed7613 100644 --- a/src/analyze/meson.build +++ b/src/analyze/meson.build @@ -1,6 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-or-later systemd_analyze_sources = files( + 'analyze-architectures.c', 'analyze-blame.c', 'analyze-calendar.c', 'analyze-capability.c', diff --git a/test/units/testsuite-65.sh b/test/units/testsuite-65.sh index ae8cd98a4e..265a07f01d 100755 --- a/test/units/testsuite-65.sh +++ b/test/units/testsuite-65.sh @@ -866,6 +866,13 @@ systemd-analyze pcrs systemd-analyze pcrs --json=pretty systemd-analyze pcrs 14 7 0 ima +systemd-analyze architectures +systemd-analyze architectures --json=pretty +systemd-analyze architectures x86 +systemd-analyze architectures x86-64 +systemd-analyze architectures native +systemd-analyze architectures uname + systemd-analyze log-level info touch /testok