]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: add "architectures" verb that lists all known architectures
authorLennart Poettering <lennart@poettering.net>
Thu, 16 Nov 2023 15:08:15 +0000 (16:08 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 Dec 2023 21:18:14 +0000 (22:18 +0100)
man/systemd-analyze.xml
man/systemd.unit.xml
src/analyze/analyze-architectures.c [new file with mode: 0644]
src/analyze/analyze-architectures.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/meson.build
test/units/testsuite-65.sh

index 2f2873452ac2ab1ef804c7c816e50d27e019581d..ad6ba5c090ddd9c27700ea8d8f0101aa563d0d86 100644 (file)
       <arg choice="opt" rep="repeat">OPTIONS</arg>
       <arg choice="plain">srk</arg> &gt;<arg choice="plain"><replaceable>FILE</replaceable></arg>
     </cmdsynopsis>
+    <cmdsynopsis>
+      <command>systemd-analyze</command>
+      <arg choice="opt" rep="repeat">OPTIONS</arg>
+      <arg choice="plain">architectures</arg>
+      <arg choice="opt" rep="repeat"><replaceable>NAME</replaceable></arg>
+    </cmdsynopsis>
   </refsynopsisdiv>
 
   <refsect1>
@@ -941,6 +947,32 @@ NR NAME                SHA256
       <programlisting>systemd-analyze srk &gt; srk.tpm2b_public</programlisting>
     </refsect2>
 
+    <refsect2>
+      <title><command>systemd-analyze architectures <optional><replaceable>NAME</replaceable>...</optional></command></title>
+
+      <para>Lists all known CPU architectures, and which ones are native. The listed architecture names are
+      those <varname>ConditionArchitecture=</varname> supports, see
+      <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
+      details. If architecture names are specified only those specified are listed.</para>
+
+      <example>
+        <title>Table output</title>
+        <programlisting>$ 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</programlisting>
+      </example>
+    </refsect2>
+
   </refsect1>
 
   <refsect1>
index 190d4c1d2f76ab1f5389c77daab633907469c49c..7fed74a22730d11babf0a8d541e1981e844518bb 100644 (file)
           <literal>arc-be</literal>, or
           <literal>native</literal>.</para>
 
+          <para>Use
+          <citerefentry><refentrytitle>systemd-analyze</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+          for the complete list of known architectures.</para>
+
           <para>The architecture is determined from the information returned by
           <citerefentry project='man-pages'><refentrytitle>uname</refentrytitle><manvolnum>2</manvolnum></citerefentry>
           and is thus subject to
diff --git a/src/analyze/analyze-architectures.c b/src/analyze/analyze-architectures.c
new file mode 100644 (file)
index 0000000..2d155d5
--- /dev/null
@@ -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 (file)
index 0000000..06b9473
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int verb_architectures(int argc, char *argv[], void *userdata);
index d2be144f4f493061f36d79498e5f1fdf2c1ddecc..7bbaf10567e7f210995df5cbc6668cd65873bf14 100644 (file)
@@ -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     },
                 {}
         };
 
index a50544730b1668d1e6fa004bd0f42db629a415e8..f150ed7613b558c43f4f6f5d702f10b5a83827eb 100644 (file)
@@ -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',
index ae8cd98a4eb7c31dba8921b3031f48cdc315b776..265a07f01d810e87e1a2e06b6e287ea5563621ce 100755 (executable)
@@ -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