diff --git a/man/systemd-inhibit.xml b/man/systemd-inhibit.xml
index 0564864d81..5ffb8f6fb6 100644
--- a/man/systemd-inhibit.xml
+++ b/man/systemd-inhibit.xml
@@ -109,13 +109,15 @@
- Lists all active inhibition locks instead of
- acquiring one.
+ Lists all active inhibition locks instead of acquiring one. It can be filtered using
+ , , , or
+ .
+
diff --git a/src/login/inhibit.c b/src/login/inhibit.c
index d97f206d2a..51379edc37 100644
--- a/src/login/inhibit.c
+++ b/src/login/inhibit.c
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include
+#include
#include
#include
#include
@@ -18,6 +19,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"
@@ -28,13 +30,14 @@
#include "terminal-util.h"
#include "user-util.h"
-static const char *arg_what = "idle:sleep:shutdown";
+static const char *arg_what = NULL;
static const char *arg_who = NULL;
-static const char *arg_why = "Unknown reason";
+static const char *arg_why = NULL;
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,
@@ -63,6 +66,8 @@ static int print_inhibitors(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
+ _cleanup_strv_free_ char **what_filter = NULL;
+
int r;
pager_open(arg_pager_flags);
@@ -83,6 +88,12 @@ static int print_inhibitors(sd_bus *bus) {
if (r < 0)
return bus_log_parse_error(r);
+ if (arg_what) {
+ what_filter = strv_split(arg_what, ":");
+ if (!what_filter)
+ return log_oom();
+ }
+
for (;;) {
_cleanup_free_ char *comm = NULL, *u = NULL;
const char *what, *who, *why, *mode;
@@ -94,6 +105,25 @@ static int print_inhibitors(sd_bus *bus) {
if (r == 0)
break;
+ if (what_filter) {
+ bool skip = false;
+
+ STRV_FOREACH(op, what_filter)
+ if (!string_contains_word(what, ":", *op)) {
+ skip = true;
+ break;
+ }
+
+ if (skip)
+ continue;
+ }
+
+ if (arg_who && !streq(who, arg_who))
+ continue;
+
+ if (arg_why && fnmatch(arg_why, why, FNM_CASEFOLD) != 0)
+ continue;
+
if (arg_mode && !streq(mode, arg_mode))
continue;
@@ -124,12 +154,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 +184,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 +215,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 +229,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 +283,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;
@@ -294,6 +335,9 @@ static int run(int argc, char *argv[]) {
/* Ignore SIGINT and allow the forked process to receive it */
(void) ignore_signals(SIGINT);
+ if (!arg_what)
+ arg_what = "idle:sleep:shutdown";
+
if (!arg_who) {
w = strv_join(argv + optind, " ");
if (!w)
@@ -302,6 +346,9 @@ static int run(int argc, char *argv[]) {
arg_who = w;
}
+ if (!arg_why)
+ arg_why = "Unknown reason";
+
if (!arg_mode)
arg_mode = "block";