]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
inhibit: add --json support for --list
authorAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Tue, 2 Dec 2025 15:20:48 +0000 (16:20 +0100)
committerLuca Boccassi <bluca@debian.org>
Wed, 17 Dec 2025 23:23:44 +0000 (23:23 +0000)
man/systemd-inhibit.xml
src/login/inhibit.c

index 0564864d81ee2aec14d8d0500e722e7de92f5430..0d5138e52c6ae52cfbc5a829590de4956ef56311 100644 (file)
       <xi:include href="standard-options.xml" xpointer="no-ask-password" />
       <xi:include href="standard-options.xml" xpointer="no-pager" />
       <xi:include href="standard-options.xml" xpointer="no-legend" />
+      <xi:include href="standard-options.xml" xpointer="json" />
       <xi:include href="standard-options.xml" xpointer="help" />
       <xi:include href="standard-options.xml" xpointer="version" />
     </variablelist>
index d97f206d2ab5115dc8ff7ff3efdb5454f9c59886..3535aa5d018094d71e658bf8108c9b1b4f7659d7 100644 (file)
@@ -18,6 +18,7 @@
 #include "log.h"
 #include "main-func.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "polkit-agent.h"
 #include "pretty-print.h"
 #include "process-util.h"
@@ -35,6 +36,7 @@ static const char *arg_mode = NULL;
 static bool arg_ask_password = true;
 static PagerFlags arg_pager_flags = 0;
 static bool arg_legend = true;
+static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
 
 static enum {
         ACTION_INHIBIT,
@@ -124,12 +126,12 @@ static int print_inhibitors(sd_bus *bus) {
 
                 table_set_header(table, arg_legend);
 
-                r = table_print(table, NULL);
+                r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
                 if (r < 0)
-                        return table_log_print_error(r);
+                        return r;
         }
 
-        if (arg_legend) {
+        if (arg_legend && !sd_json_format_enabled(arg_json_format_flags)) {
                 if (table_isempty(table))
                         printf("No inhibitors.\n");
                 else
@@ -154,6 +156,8 @@ static int help(void) {
                "     --no-ask-password    Do not attempt interactive authorization\n"
                "     --no-pager           Do not pipe output into a pager\n"
                "     --no-legend          Do not show the headers and footers\n"
+               "     --json=pretty|short|off\n"
+               "                          Generate JSON output\n"
                "     --what=WHAT          Operations to inhibit, colon separated list of:\n"
                "                          shutdown, sleep, idle, handle-power-key,\n"
                "                          handle-suspend-key, handle-hibernate-key,\n"
@@ -183,6 +187,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_NO_ASK_PASSWORD,
                 ARG_NO_PAGER,
                 ARG_NO_LEGEND,
+                ARG_JSON,
         };
 
         static const struct option options[] = {
@@ -196,10 +201,11 @@ static int parse_argv(int argc, char *argv[]) {
                 { "list",             no_argument,       NULL, ARG_LIST            },
                 { "no-pager",         no_argument,       NULL, ARG_NO_PAGER        },
                 { "no-legend",        no_argument,       NULL, ARG_NO_LEGEND       },
+                { "json",             required_argument, NULL, ARG_JSON            },
                 {}
         };
 
-        int c;
+        int c, r;
 
         assert(argc >= 0);
         assert(argv);
@@ -249,6 +255,13 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_legend = false;
                         break;
 
+                case ARG_JSON:
+                        r = parse_json_argument(optarg, &arg_json_format_flags);
+                        if (r <= 0)
+                                return r;
+
+                        break;
+
                 case '?':
                         return -EINVAL;