1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "pretty-print.h"
10 #include "terminal-util.h"
13 static PagerFlags arg_pager_flags
= 0;
15 static int help(int argc
, char *argv
[], void *userdata
) {
16 _cleanup_free_
char *link
= NULL
;
19 (void) pager_open(arg_pager_flags
);
21 r
= terminal_urlify_man("oomctl", "1", &link
);
25 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
26 "%2$sManage or inspect the userspace OOM killer.%3$s\n"
27 "\n%4$sCommands:%5$s\n"
28 " dump Output the current state of systemd-oomd\n"
29 "\n%4$sOptions:%5$s\n"
30 " -h --help Show this help\n"
31 " --version Show package version\n"
32 " --no-pager Do not pipe output into a pager\n"
33 "\nSee the %6$s for details.\n",
34 program_invocation_short_name
,
44 static int dump_state(int argc
, char *argv
[], void *userdata
) {
45 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
46 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
47 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
51 r
= sd_bus_open_system(&bus
);
53 return log_error_errno(r
, "Failed to connect system bus: %m");
55 (void) pager_open(arg_pager_flags
);
57 r
= sd_bus_call_method(
59 "org.freedesktop.oom1",
60 "/org/freedesktop/oom1",
61 "org.freedesktop.oom1.Manager",
62 "DumpByFileDescriptor",
67 return log_error_errno(r
, "Failed to dump context: %s", bus_error_message(&error
, r
));
69 r
= sd_bus_message_read(reply
, "h", &fd
);
71 return bus_log_parse_error(r
);
74 return copy_bytes(fd
, STDOUT_FILENO
, (uint64_t) -1, 0);
77 static int parse_argv(int argc
, char *argv
[]) {
83 static const struct option options
[] = {
84 { "help", no_argument
, NULL
, 'h' },
85 { "version", no_argument
, NULL
, ARG_VERSION
},
86 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
95 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
100 return help(0, NULL
, NULL
);
106 arg_pager_flags
|= PAGER_DISABLE
;
113 assert_not_reached("Invalid option passed.");
119 static int run(int argc
, char* argv
[]) {
120 static const Verb verbs
[] = {
121 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
122 { "dump", VERB_ANY
, 1, VERB_DEFAULT
, dump_state
},
128 log_show_color(true);
129 log_parse_environment();
132 r
= parse_argv(argc
, argv
);
136 return dispatch_verb(argc
, argv
, verbs
, NULL
);
139 DEFINE_MAIN_FUNCTION(run
);