]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5c616ecf AZ |
2 | |
3 | #include <getopt.h> | |
4 | #include <unistd.h> | |
5 | ||
6 | #include "bus-error.h" | |
7 | #include "copy.h" | |
8 | #include "main-func.h" | |
9 | #include "pretty-print.h" | |
10 | #include "terminal-util.h" | |
11 | #include "verbs.h" | |
12 | ||
13 | static PagerFlags arg_pager_flags = 0; | |
14 | ||
15 | static int help(int argc, char *argv[], void *userdata) { | |
16 | _cleanup_free_ char *link = NULL; | |
17 | int r; | |
18 | ||
19 | (void) pager_open(arg_pager_flags); | |
20 | ||
21 | r = terminal_urlify_man("oomctl", "1", &link); | |
22 | if (r < 0) | |
23 | return log_oom(); | |
24 | ||
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() | |
37 | , link | |
38 | ); | |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
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; | |
47 | int fd = -1; | |
48 | int r; | |
49 | ||
50 | r = sd_bus_open_system(&bus); | |
51 | if (r < 0) | |
52 | return log_error_errno(r, "Failed to connect system bus: %m"); | |
53 | ||
54 | (void) pager_open(arg_pager_flags); | |
55 | ||
56 | r = sd_bus_call_method( | |
57 | bus, | |
58 | "org.freedesktop.oom1", | |
59 | "/org/freedesktop/oom1", | |
60 | "org.freedesktop.oom1.Manager", | |
61 | "DumpByFileDescriptor", | |
62 | &error, | |
63 | &reply, | |
64 | NULL); | |
65 | if (r < 0) | |
66 | return log_error_errno(r, "Failed to dump context: %s", bus_error_message(&error, r)); | |
67 | ||
68 | r = sd_bus_message_read(reply, "h", &fd); | |
69 | if (r < 0) | |
70 | return bus_log_parse_error(r); | |
71 | ||
72 | fflush(stdout); | |
73 | return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0); | |
74 | } | |
75 | ||
76 | static int parse_argv(int argc, char *argv[]) { | |
77 | enum { | |
78 | ARG_VERSION = 0x100, | |
79 | ARG_NO_PAGER, | |
80 | }; | |
81 | ||
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 }, | |
86 | {} | |
87 | }; | |
88 | ||
89 | int c; | |
90 | ||
91 | assert(argc >= 0); | |
92 | assert(argv); | |
93 | ||
94 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
95 | ||
96 | switch (c) { | |
97 | ||
98 | case 'h': | |
99 | return help(0, NULL, NULL); | |
100 | ||
101 | case ARG_VERSION: | |
102 | return version(); | |
103 | ||
104 | case ARG_NO_PAGER: | |
105 | arg_pager_flags |= PAGER_DISABLE; | |
106 | break; | |
107 | ||
108 | case '?': | |
109 | return -EINVAL; | |
110 | ||
111 | default: | |
112 | assert_not_reached("Invalid option passed."); | |
113 | } | |
114 | ||
115 | return 1; | |
116 | } | |
117 | ||
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 }, | |
122 | {} | |
123 | }; | |
124 | ||
125 | int r; | |
126 | ||
127 | log_show_color(true); | |
128 | log_parse_environment(); | |
129 | log_open(); | |
130 | ||
131 | r = parse_argv(argc, argv); | |
132 | if (r <= 0) | |
133 | return r; | |
134 | ||
135 | return dispatch_verb(argc, argv, verbs, NULL); | |
136 | } | |
137 | ||
138 | DEFINE_MAIN_FUNCTION(run); |