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