]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/oom/oomd.c
Merge pull request #30480 from keszybz/kernel-install-more-paths
[thirdparty/systemd.git] / src / oom / oomd.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "build.h"
6 #include "bus-log-control-api.h"
7 #include "bus-object.h"
8 #include "cgroup-util.h"
9 #include "conf-parser.h"
10 #include "daemon-util.h"
11 #include "fileio.h"
12 #include "log.h"
13 #include "main-func.h"
14 #include "oomd-manager-bus.h"
15 #include "oomd-manager.h"
16 #include "parse-util.h"
17 #include "pretty-print.h"
18 #include "psi-util.h"
19 #include "signal-util.h"
20
21 static bool arg_dry_run = false;
22 static int arg_swap_used_limit_permyriad = -1;
23 static int arg_mem_pressure_limit_permyriad = -1;
24 static usec_t arg_mem_pressure_usec = 0;
25
26 static int parse_config(void) {
27 static const ConfigTableItem items[] = {
28 { "OOM", "SwapUsedLimit", config_parse_permyriad, 0, &arg_swap_used_limit_permyriad },
29 { "OOM", "DefaultMemoryPressureLimit", config_parse_permyriad, 0, &arg_mem_pressure_limit_permyriad },
30 { "OOM", "DefaultMemoryPressureDurationSec", config_parse_sec, 0, &arg_mem_pressure_usec },
31 {}
32 };
33
34 return config_parse_standard_file_with_dropins(
35 "systemd/oomd.conf",
36 "OOM\0",
37 config_item_table_lookup, items,
38 CONFIG_PARSE_WARN,
39 /* userdata= */ NULL);
40 }
41
42 static int help(void) {
43 _cleanup_free_ char *link = NULL;
44 int r;
45
46 r = terminal_urlify_man("systemd-oomd", "8", &link);
47 if (r < 0)
48 return log_oom();
49
50 printf("%s [OPTIONS...]\n\n"
51 "Run the userspace out-of-memory (OOM) killer.\n\n"
52 " -h --help Show this help\n"
53 " --version Show package version\n"
54 " --dry-run Only print destructive actions instead of doing them\n"
55 " --bus-introspect=PATH Write D-Bus XML introspection data\n"
56 "\nSee the %s for details.\n",
57 program_invocation_short_name,
58 link);
59
60 return 0;
61 }
62
63 static int parse_argv(int argc, char *argv[]) {
64 enum {
65 ARG_VERSION = 0x100,
66 ARG_DRY_RUN,
67 ARG_BUS_INTROSPECT,
68 };
69
70 static const struct option options[] = {
71 { "help", no_argument, NULL, 'h' },
72 { "version", no_argument, NULL, ARG_VERSION },
73 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
74 { "bus-introspect", required_argument, NULL, ARG_BUS_INTROSPECT },
75 {}
76 };
77
78 int c;
79
80 assert(argc >= 0);
81 assert(argv);
82
83 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
84
85 switch (c) {
86
87 case 'h':
88 return help();
89
90 case ARG_VERSION:
91 return version();
92
93 case ARG_DRY_RUN:
94 arg_dry_run = true;
95 break;
96
97 case ARG_BUS_INTROSPECT:
98 return bus_introspect_implementations(
99 stdout,
100 optarg,
101 BUS_IMPLEMENTATIONS(&manager_object,
102 &log_control_object));
103
104 case '?':
105 return -EINVAL;
106
107 default:
108 assert_not_reached();
109 }
110
111 if (optind < argc)
112 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
113 "This program takes no arguments.");
114
115 return 1;
116 }
117
118 static int run(int argc, char *argv[]) {
119 _unused_ _cleanup_(notify_on_cleanup) const char *notify_msg = NULL;
120 _cleanup_(manager_freep) Manager *m = NULL;
121 _cleanup_free_ char *swap = NULL;
122 unsigned long long s = 0;
123 CGroupMask mask;
124 int r;
125
126 log_setup();
127
128 r = parse_argv(argc, argv);
129 if (r <= 0)
130 return r;
131
132 r = parse_config();
133 if (r < 0)
134 return r;
135
136 /* Do some basic requirement checks for running systemd-oomd. It's not exhaustive as some of the other
137 * requirements do not have a reliable means to check for in code. */
138
139 int n = sd_listen_fds(0);
140 if (n > 1)
141 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Received too many file descriptors");
142
143 int fd = n == 1 ? SD_LISTEN_FDS_START : -1;
144
145 /* SwapTotal is always available in /proc/meminfo and defaults to 0, even on swap-disabled kernels. */
146 r = get_proc_field("/proc/meminfo", "SwapTotal", WHITESPACE, &swap);
147 if (r < 0)
148 return log_error_errno(r, "Failed to get SwapTotal from /proc/meminfo: %m");
149
150 r = safe_atollu(swap, &s);
151 if (r < 0 || s == 0)
152 log_warning("No swap; memory pressure usage will be degraded");
153
154 if (!is_pressure_supported())
155 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Pressure Stall Information (PSI) is not supported");
156
157 r = cg_all_unified();
158 if (r < 0)
159 return log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
160 if (r == 0)
161 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the unified cgroups hierarchy");
162
163 r = cg_mask_supported(&mask);
164 if (r < 0)
165 return log_error_errno(r, "Failed to get supported cgroup controllers: %m");
166
167 if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
168 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the cgroup memory controller.");
169
170 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT) >= 0);
171
172 if (arg_mem_pressure_usec > 0 && arg_mem_pressure_usec < 1 * USEC_PER_SEC)
173 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "DefaultMemoryPressureDurationSec= must be 0 or at least 1s");
174
175 r = manager_new(&m);
176 if (r < 0)
177 return log_error_errno(r, "Failed to create manager: %m");
178
179 r = manager_start(
180 m,
181 arg_dry_run,
182 arg_swap_used_limit_permyriad,
183 arg_mem_pressure_limit_permyriad,
184 arg_mem_pressure_usec,
185 fd);
186 if (r < 0)
187 return log_error_errno(r, "Failed to start up daemon: %m");
188
189 notify_msg = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
190
191 log_debug("systemd-oomd started%s.", arg_dry_run ? " in dry run mode" : "");
192
193 r = sd_event_loop(m->event);
194 if (r < 0)
195 return log_error_errno(r, "Event loop failed: %m");
196
197 return 0;
198 }
199
200 DEFINE_MAIN_FUNCTION(run);