]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/oom/oomd.c
Merge pull request #18318 from yuwata/network-route-table-name
[thirdparty/systemd.git] / src / oom / oomd.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "bus-log-control-api.h"
6 #include "bus-object.h"
7 #include "cgroup-util.h"
8 #include "conf-parser.h"
9 #include "daemon-util.h"
10 #include "log.h"
11 #include "main-func.h"
12 #include "oomd-manager.h"
13 #include "oomd-manager-bus.h"
14 #include "parse-util.h"
15 #include "pretty-print.c"
16 #include "psi-util.h"
17 #include "signal-util.h"
18
19 static bool arg_dry_run = false;
20 static int arg_swap_used_limit = -1;
21 static int arg_mem_pressure_limit_permyriad = -1;
22 static usec_t arg_mem_pressure_usec = 0;
23
24 static int parse_config(void) {
25 static const ConfigTableItem items[] = {
26 { "OOM", "SwapUsedLimitPercent", config_parse_percent, 0, &arg_swap_used_limit },
27 { "OOM", "DefaultMemoryPressureLimit", config_parse_permyriad, 0, &arg_mem_pressure_limit_permyriad },
28 { "OOM", "DefaultMemoryPressureDurationSec", config_parse_sec, 0, &arg_mem_pressure_usec },
29 {}
30 };
31
32 return config_parse_many_nulstr(PKGSYSCONFDIR "/oomd.conf",
33 CONF_PATHS_NULSTR("systemd/oomd.conf.d"),
34 "OOM\0",
35 config_item_table_lookup,
36 items,
37 CONFIG_PARSE_WARN,
38 NULL,
39 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", "1", &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("Unknown option code.");
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 _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 int r;
124
125 log_setup_service();
126
127 r = parse_argv(argc, argv);
128 if (r <= 0)
129 return r;
130
131 r = parse_config();
132 if (r < 0)
133 return r;
134
135 /* Do some basic requirement checks for running systemd-oomd. It's not exhaustive as some of the other
136 * requirements do not have a reliable means to check for in code. */
137
138 /* SwapTotal is always available in /proc/meminfo and defaults to 0, even on swap-disabled kernels. */
139 r = get_proc_field("/proc/meminfo", "SwapTotal", WHITESPACE, &swap);
140 if (r < 0)
141 return log_error_errno(r, "Failed to get SwapTotal from /proc/meminfo: %m");
142
143 r = safe_atollu(swap, &s);
144 if (r < 0 || s == 0)
145 log_warning("Swap is currently not detected; memory pressure usage will be degraded");
146
147 if (!is_pressure_supported())
148 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Pressure Stall Information (PSI) is not supported");
149
150 r = cg_all_unified();
151 if (r < 0)
152 return log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
153 if (r == 0)
154 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the unified cgroups hierarchy");
155
156 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
157
158 r = manager_new(&m);
159 if (r < 0)
160 return log_error_errno(r, "Failed to create manager: %m");
161
162 r = manager_start(m, arg_dry_run, arg_swap_used_limit, arg_mem_pressure_limit_permyriad, arg_mem_pressure_usec);
163 if (r < 0)
164 return log_error_errno(r, "Failed to start up daemon: %m");
165
166 notify_msg = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
167
168 log_info("systemd-oomd starting%s!", arg_dry_run ? " in dry run mode" : "");
169
170 r = sd_event_loop(m->event);
171 if (r < 0)
172 return log_error_errno(r, "Event loop failed: %m");
173
174 return 0;
175 }
176
177 DEFINE_MAIN_FUNCTION(run);