1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
35 , ansi_highlight(), ansi_normal()
36 , ansi_underline(), ansi_normal()
43 static int dump_state(int argc
, char *argv
[], void *userdata
) {
44 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
45 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
46 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
50 r
= sd_bus_open_system(&bus
);
52 return log_error_errno(r
, "Failed to connect system bus: %m");
54 (void) pager_open(arg_pager_flags
);
56 r
= sd_bus_call_method(
58 "org.freedesktop.oom1",
59 "/org/freedesktop/oom1",
60 "org.freedesktop.oom1.Manager",
61 "DumpByFileDescriptor",
66 return log_error_errno(r
, "Failed to dump context: %s", bus_error_message(&error
, r
));
68 r
= sd_bus_message_read(reply
, "h", &fd
);
70 return bus_log_parse_error(r
);
73 return copy_bytes(fd
, STDOUT_FILENO
, (uint64_t) -1, 0);
76 static int parse_argv(int argc
, char *argv
[]) {
82 static const struct option options
[] = {
83 { "help", no_argument
, NULL
, 'h' },
84 { "version", no_argument
, NULL
, ARG_VERSION
},
85 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
94 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
99 return help(0, NULL
, NULL
);
105 arg_pager_flags
|= PAGER_DISABLE
;
112 assert_not_reached("Invalid option passed.");
118 static int run(int argc
, char* argv
[]) {
119 static const Verb verbs
[] = {
120 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
121 { "dump", VERB_ANY
, 1, VERB_DEFAULT
, dump_state
},
127 log_show_color(true);
128 log_parse_environment();
131 r
= parse_argv(argc
, argv
);
135 return dispatch_verb(argc
, argv
, verbs
, NULL
);
138 DEFINE_MAIN_FUNCTION(run
);