Debian Codegraph @

src/oom/oomd.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "bus-log-control-api.h"
8 #include "bus-object.h"
9 #include "cgroup-util.h"
10 #include "daemon-util.h"
11 #include "fileio.h"
12 #include "format-table.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "oomd-conf.h"
16 #include "oomd-manager.h"
17 #include "oomd-manager-bus.h"
18 #include "options.h"
19 #include "parse-util.h"
20 #include "pretty-print.h"
21 #include "psi-util.h"
22
23 static bool arg_dry_run = false;
24
25 static int help(void) {
26 _cleanup_free_ char *link = NULL;
27 _cleanup_(table_unrefp) Table *options = NULL;
28 int r;
29
30 r = terminal_urlify_man("systemd-oomd", "8", &link);
31 if (r < 0)
32 return log_oom();
33
34 r = option_parser_get_help_table(&options);
35 if (r < 0)
36 return r;
37
38 printf("%s [OPTIONS...]\n\n"
39 "Run the userspace out-of-memory (OOM) killer.\n\n",
40 program_invocation_short_name);
41
42 r = table_print_or_warn(options);
43 if (r < 0)
44 return r;
45
46 printf("\nSee the %s for details.\n", link);
47 return 0;
48 }
49
50 static int parse_argv(int argc, char *argv[]) {
51 assert(argc >= 0);
52 assert(argv);
53
54 OptionParser opts = { argc, argv };
55
56 FOREACH_OPTION_OR_RETURN(c, &opts)
57 switch (c) {
58
59 OPTION_COMMON_HELP:
60 return help();
61
62 OPTION_COMMON_VERSION:
63 return version();
64
65 OPTION_LONG("dry-run", NULL,
66 "Only print destructive actions instead of doing them"):
67 arg_dry_run = true;
68 break;
69
70 OPTION_LONG("bus-introspect", "PATH",
71 "Write D-Bus XML introspection data"):
72 return bus_introspect_implementations(
73 stdout,
74 opts.arg,
75 BUS_IMPLEMENTATIONS(&manager_object,
76 &log_control_object));
77 }
78
79 if (option_parser_get_n_args(&opts) > 0)
80 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
81 "This program takes no arguments.");
82
83 return 1;
84 }
85
86 static int run(int argc, char *argv[]) {
87 _unused_ _cleanup_(notify_on_cleanup) const char *notify_msg = NULL;
88 _cleanup_(manager_freep) Manager *m = NULL;
89 _cleanup_free_ char *swap = NULL;
90 unsigned long long s = 0;
91 CGroupMask mask;
92 int r;
93
94 log_setup();
95
96 r = parse_argv(argc, argv);
97 if (r <= 0)
98 return r;
99
100 /* Do some basic requirement checks for running systemd-oomd. It's not exhaustive as some of the other
101 * requirements do not have a reliable means to check for in code. */
102
103 int n = sd_listen_fds(0);
104 if (n < 0)
105 return log_error_errno(n, "Failed to determine number of listening fds: %m");
106 if (n > 1)
107 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Received too many file descriptors");
108
109 int fd = n == 1 ? SD_LISTEN_FDS_START : -EBADF;
110
111 /* SwapTotal is always available in /proc/meminfo and defaults to 0, even on swap-disabled kernels. */
112 r = get_proc_field("/proc/meminfo", "SwapTotal", &swap);
113 if (r < 0)
114 return log_error_errno(r, "Failed to get SwapTotal from /proc/meminfo: %m");
115
116 r = safe_atollu(swap, &s);
117 if (r < 0 || s == 0)
118 log_warning("No swap; memory pressure usage will be degraded");
119
120 if (!is_pressure_supported())
121 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Pressure Stall Information (PSI) is not supported");
122
123 r = cg_mask_supported(&mask);
124 if (r < 0)
125 return log_error_errno(r, "Failed to get supported cgroup controllers: %m");
126
127 if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
128 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the cgroup memory controller.");
129
130 r = manager_new(&m);
131 if (r < 0)
132 return log_error_errno(r, "Failed to create manager: %m");
133
134 r = manager_start(
135 m,
136 arg_dry_run,
137 fd);
138 if (r < 0)
139 return log_error_errno(r, "Failed to start up daemon: %m");
140
141 notify_msg = notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE);
142
143 log_debug("systemd-oomd started%s.", arg_dry_run ? " in dry run mode" : "");
144
145 r = sd_event_loop(m->event);
146 if (r < 0)
147 return log_error_errno(r, "Event loop failed: %m");
148
149 return 0;
150 }
151
152 DEFINE_MAIN_FUNCTION(run);