]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/oom/oomctl.c
7fcb4b82cc65f618abdf71773a3b12cc64bef068
[thirdparty/systemd.git] / src / oom / oomctl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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(),
36 ansi_normal(),
37 ansi_underline(),
38 ansi_normal(),
39 link);
40
41 return 0;
42 }
43
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;
48 int fd = -1;
49 int r;
50
51 r = sd_bus_open_system(&bus);
52 if (r < 0)
53 return log_error_errno(r, "Failed to connect system bus: %m");
54
55 (void) pager_open(arg_pager_flags);
56
57 r = sd_bus_call_method(
58 bus,
59 "org.freedesktop.oom1",
60 "/org/freedesktop/oom1",
61 "org.freedesktop.oom1.Manager",
62 "DumpByFileDescriptor",
63 &error,
64 &reply,
65 NULL);
66 if (r < 0)
67 return log_error_errno(r, "Failed to dump context: %s", bus_error_message(&error, r));
68
69 r = sd_bus_message_read(reply, "h", &fd);
70 if (r < 0)
71 return bus_log_parse_error(r);
72
73 fflush(stdout);
74 return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
75 }
76
77 static int parse_argv(int argc, char *argv[]) {
78 enum {
79 ARG_VERSION = 0x100,
80 ARG_NO_PAGER,
81 };
82
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 },
87 {}
88 };
89
90 int c;
91
92 assert(argc >= 0);
93 assert(argv);
94
95 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
96
97 switch (c) {
98
99 case 'h':
100 return help(0, NULL, NULL);
101
102 case ARG_VERSION:
103 return version();
104
105 case ARG_NO_PAGER:
106 arg_pager_flags |= PAGER_DISABLE;
107 break;
108
109 case '?':
110 return -EINVAL;
111
112 default:
113 assert_not_reached("Invalid option passed.");
114 }
115
116 return 1;
117 }
118
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 },
123 {}
124 };
125
126 int r;
127
128 log_show_color(true);
129 log_parse_environment();
130 log_open();
131
132 r = parse_argv(argc, argv);
133 if (r <= 0)
134 return r;
135
136 return dispatch_verb(argc, argv, verbs, NULL);
137 }
138
139 DEFINE_MAIN_FUNCTION(run);