| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "bus-error.h" |
| 8 |
#include "bus-locator.h" |
| 9 |
#include "bus-message-util.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "log.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "options.h" |
| 14 |
#include "pager.h" |
| 15 |
#include "pretty-print.h" |
| 16 |
#include "verbs.h" |
| 17 |
|
| 18 |
static PagerFlags arg_pager_flags = 0; |
| 19 |
|
| 20 |
static int help(void) { |
| 21 |
_cleanup_free_ char *link = NULL; |
| 22 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 23 |
int r; |
| 24 |
|
| 25 |
pager_open(arg_pager_flags); |
| 26 |
|
| 27 |
r = terminal_urlify_man("oomctl", "1", &link); |
| 28 |
if (r < 0) |
| 29 |
return log_oom(); |
| 30 |
|
| 31 |
r = verbs_get_help_table(&verbs); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
r = option_parser_get_help_table(&options); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
(void) table_sync_column_widths(0, verbs, options); |
| 40 |
|
| 41 |
printf("%s [OPTIONS...] COMMAND ...\n\n" |
| 42 |
"%sManage or inspect the userspace OOM killer.%s\n" |
| 43 |
"\n%sCommands:%s\n", |
| 44 |
program_invocation_short_name, |
| 45 |
ansi_highlight(), |
| 46 |
ansi_normal(), |
| 47 |
ansi_underline(), |
| 48 |
ansi_normal()); |
| 49 |
|
| 50 |
r = table_print_or_warn(verbs); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
printf("\n%sOptions:%s\n", |
| 55 |
ansi_underline(), |
| 56 |
ansi_normal()); |
| 57 |
|
| 58 |
r = table_print_or_warn(options); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
printf("\nSee the %s for details.\n", link); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
VERB_COMMON_HELP_HIDDEN(help); |
| 67 |
|
| 68 |
VERB_DEFAULT_NOARG(verb_dump_state, "dump", "Output the current state of systemd-oomd"); |
| 69 |
static int verb_dump_state(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 70 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 71 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 72 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 73 |
int r; |
| 74 |
|
| 75 |
r = sd_bus_open_system(&bus); |
| 76 |
if (r < 0) |
| 77 |
return log_error_errno(r, "Failed to connect system bus: %m"); |
| 78 |
|
| 79 |
pager_open(arg_pager_flags); |
| 80 |
|
| 81 |
r = bus_call_method(bus, bus_oom_mgr, "DumpByFileDescriptor", &error, &reply, NULL); |
| 82 |
if (r < 0) |
| 83 |
return log_error_errno(r, "Failed to dump context: %s", bus_error_message(&error, r)); |
| 84 |
|
| 85 |
return bus_message_dump_fd(reply); |
| 86 |
} |
| 87 |
|
| 88 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 89 |
assert(argc >= 0); |
| 90 |
assert(argv); |
| 91 |
assert(ret_args); |
| 92 |
|
| 93 |
OptionParser opts = { argc, argv }; |
| 94 |
|
| 95 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 96 |
switch (c) { |
| 97 |
|
| 98 |
OPTION_COMMON_HELP: |
| 99 |
return help(); |
| 100 |
|
| 101 |
OPTION_COMMON_VERSION: |
| 102 |
return version(); |
| 103 |
|
| 104 |
OPTION_COMMON_NO_PAGER: |
| 105 |
arg_pager_flags |= PAGER_DISABLE; |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
*ret_args = option_parser_get_args(&opts); |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
static int run(int argc, char* argv[]) { |
| 114 |
int r; |
| 115 |
|
| 116 |
log_setup(); |
| 117 |
|
| 118 |
char **args = NULL; |
| 119 |
r = parse_argv(argc, argv, &args); |
| 120 |
if (r <= 0) |
| 121 |
return r; |
| 122 |
|
| 123 |
return dispatch_verb(args, NULL); |
| 124 |
} |
| 125 |
|
| 126 |
DEFINE_MAIN_FUNCTION(run); |