]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/core-varlink.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / core / core-varlink.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "core-varlink.h"
4 #include "mkdir.h"
5 #include "strv.h"
6 #include "user-util.h"
7 #include "varlink.h"
8
9 typedef struct LookupParameters {
10 const char *user_name;
11 const char *group_name;
12 union {
13 uid_t uid;
14 gid_t gid;
15 };
16 const char *service;
17 } LookupParameters;
18
19 static const char* const managed_oom_mode_properties[] = {
20 "ManagedOOMSwap",
21 "ManagedOOMMemoryPressure",
22 };
23
24 static int build_user_json(const char *user_name, uid_t uid, JsonVariant **ret) {
25 assert(user_name);
26 assert(uid_is_valid(uid));
27 assert(ret);
28
29 return json_build(ret, JSON_BUILD_OBJECT(
30 JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT(
31 JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(user_name)),
32 JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)),
33 JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(uid)),
34 JSON_BUILD_PAIR("realName", JSON_BUILD_STRING("Dynamic User")),
35 JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_STRING("/")),
36 JSON_BUILD_PAIR("shell", JSON_BUILD_STRING(NOLOGIN)),
37 JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)),
38 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.DynamicUser")),
39 JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING("dynamic"))))));
40 }
41
42 static bool user_match_lookup_parameters(LookupParameters *p, const char *name, uid_t uid) {
43 assert(p);
44
45 if (p->user_name && !streq(name, p->user_name))
46 return false;
47
48 if (uid_is_valid(p->uid) && uid != p->uid)
49 return false;
50
51 return true;
52 }
53
54 static int build_managed_oom_json_array_element(Unit *u, const char *property, JsonVariant **ret_v) {
55 bool use_limit = false;
56 CGroupContext *c;
57 const char *mode;
58
59 assert(u);
60 assert(property);
61 assert(ret_v);
62
63 if (!UNIT_VTABLE(u)->can_set_managed_oom)
64 return -EOPNOTSUPP;
65
66 c = unit_get_cgroup_context(u);
67 if (!c)
68 return -EINVAL;
69
70 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
71 /* systemd-oomd should always treat inactive units as though they didn't enable any action since they
72 * should not have a valid cgroup */
73 mode = managed_oom_mode_to_string(MANAGED_OOM_AUTO);
74 else if (streq(property, "ManagedOOMSwap"))
75 mode = managed_oom_mode_to_string(c->moom_swap);
76 else if (streq(property, "ManagedOOMMemoryPressure")) {
77 mode = managed_oom_mode_to_string(c->moom_mem_pressure);
78 use_limit = true;
79 } else
80 return -EINVAL;
81
82 return json_build(ret_v, JSON_BUILD_OBJECT(
83 JSON_BUILD_PAIR("mode", JSON_BUILD_STRING(mode)),
84 JSON_BUILD_PAIR("path", JSON_BUILD_STRING(u->cgroup_path)),
85 JSON_BUILD_PAIR("property", JSON_BUILD_STRING(property)),
86 JSON_BUILD_PAIR_CONDITION(use_limit, "limit", JSON_BUILD_UNSIGNED(c->moom_mem_pressure_limit))));
87 }
88
89 int manager_varlink_send_managed_oom_update(Unit *u) {
90 _cleanup_(json_variant_unrefp) JsonVariant *arr = NULL, *v = NULL;
91 CGroupContext *c;
92 int r;
93
94 assert(u);
95
96 if (!UNIT_VTABLE(u)->can_set_managed_oom || !u->manager || !u->manager->managed_oom_varlink_request || !u->cgroup_path)
97 return 0;
98
99 c = unit_get_cgroup_context(u);
100 if (!c)
101 return 0;
102
103 r = json_build(&arr, JSON_BUILD_EMPTY_ARRAY);
104 if (r < 0)
105 return r;
106
107 for (size_t i = 0; i < ELEMENTSOF(managed_oom_mode_properties); i++) {
108 _cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
109
110 r = build_managed_oom_json_array_element(u, managed_oom_mode_properties[i], &e);
111 if (r < 0)
112 return r;
113
114 r = json_variant_append_array(&arr, e);
115 if (r < 0)
116 return r;
117 }
118
119 r = json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr))));
120 if (r < 0)
121 return r;
122
123 return varlink_notify(u->manager->managed_oom_varlink_request, v);
124 }
125
126 static int vl_method_subscribe_managed_oom_cgroups(
127 Varlink *link,
128 JsonVariant *parameters,
129 VarlinkMethodFlags flags,
130 void *userdata) {
131 static const UnitType supported_unit_types[] = { UNIT_SLICE, UNIT_SERVICE, UNIT_SCOPE };
132 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *arr = NULL;
133 Manager *m = userdata;
134 int r;
135
136 assert(link);
137 assert(m);
138
139 if (json_variant_elements(parameters) > 0)
140 return varlink_error_invalid_parameter(link, parameters);
141
142 /* We only take one subscriber for this method so return an error if there's already an existing one.
143 * This shouldn't happen since systemd-oomd is the only client of this method. */
144 if (FLAGS_SET(flags, VARLINK_METHOD_MORE) && m->managed_oom_varlink_request)
145 return varlink_error(m->managed_oom_varlink_request, VARLINK_ERROR_SUBSCRIPTION_TAKEN, NULL);
146
147 r = json_build(&arr, JSON_BUILD_EMPTY_ARRAY);
148 if (r < 0)
149 return r;
150
151 for (size_t i = 0; i < ELEMENTSOF(supported_unit_types); i++) {
152 Unit *u;
153
154 LIST_FOREACH(units_by_type, u, m->units_by_type[supported_unit_types[i]]) {
155 CGroupContext *c;
156
157 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
158 continue;
159
160 c = unit_get_cgroup_context(u);
161 if (!c)
162 continue;
163
164 for (size_t j = 0; j < ELEMENTSOF(managed_oom_mode_properties); j++) {
165 _cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
166
167 /* For the initial varlink call we only care about units that enabled (i.e. mode is not
168 * set to "auto") oomd properties. */
169 if (!(streq(managed_oom_mode_properties[j], "ManagedOOMSwap") && c->moom_swap == MANAGED_OOM_KILL) &&
170 !(streq(managed_oom_mode_properties[j], "ManagedOOMMemoryPressure") && c->moom_mem_pressure == MANAGED_OOM_KILL))
171 continue;
172
173 r = build_managed_oom_json_array_element(u, managed_oom_mode_properties[j], &e);
174 if (r < 0)
175 return r;
176
177 r = json_variant_append_array(&arr, e);
178 if (r < 0)
179 return r;
180 }
181 }
182 }
183
184 r = json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr))));
185 if (r < 0)
186 return r;
187
188 if (!FLAGS_SET(flags, VARLINK_METHOD_MORE))
189 return varlink_reply(link, v);
190
191 m->managed_oom_varlink_request = varlink_ref(link);
192 return varlink_notify(m->managed_oom_varlink_request, v);
193 }
194
195 static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
196
197 static const JsonDispatch dispatch_table[] = {
198 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 },
199 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE },
200 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
201 {}
202 };
203
204 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
205 LookupParameters p = {
206 .uid = UID_INVALID,
207 };
208 _cleanup_free_ char *found_name = NULL;
209 uid_t found_uid = UID_INVALID, uid;
210 Manager *m = userdata;
211 const char *un;
212 int r;
213
214 assert(parameters);
215 assert(m);
216
217 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
218 if (r < 0)
219 return r;
220
221 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
222 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
223
224 if (uid_is_valid(p.uid))
225 r = dynamic_user_lookup_uid(m, p.uid, &found_name);
226 else if (p.user_name)
227 r = dynamic_user_lookup_name(m, p.user_name, &found_uid);
228 else {
229 DynamicUser *d;
230
231 HASHMAP_FOREACH(d, m->dynamic_users) {
232 r = dynamic_user_current(d, &uid);
233 if (r == -EAGAIN) /* not realized yet? */
234 continue;
235 if (r < 0)
236 return r;
237
238 if (!user_match_lookup_parameters(&p, d->name, uid))
239 continue;
240
241 if (v) {
242 r = varlink_notify(link, v);
243 if (r < 0)
244 return r;
245
246 v = json_variant_unref(v);
247 }
248
249 r = build_user_json(d->name, uid, &v);
250 if (r < 0)
251 return r;
252 }
253
254 if (!v)
255 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
256
257 return varlink_reply(link, v);
258 }
259 if (r == -ESRCH)
260 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
261 if (r < 0)
262 return r;
263
264 uid = uid_is_valid(found_uid) ? found_uid : p.uid;
265 un = found_name ?: p.user_name;
266
267 if (!user_match_lookup_parameters(&p, un, uid))
268 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
269
270 r = build_user_json(un, uid, &v);
271 if (r < 0)
272 return r;
273
274 return varlink_reply(link, v);
275 }
276
277 static int build_group_json(const char *group_name, gid_t gid, JsonVariant **ret) {
278 assert(group_name);
279 assert(gid_is_valid(gid));
280 assert(ret);
281
282 return json_build(ret, JSON_BUILD_OBJECT(
283 JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT(
284 JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(group_name)),
285 JSON_BUILD_PAIR("description", JSON_BUILD_STRING("Dynamic Group")),
286 JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)),
287 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.DynamicUser")),
288 JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING("dynamic"))))));
289 }
290
291 static bool group_match_lookup_parameters(LookupParameters *p, const char *name, gid_t gid) {
292 assert(p);
293
294 if (p->group_name && !streq(name, p->group_name))
295 return false;
296
297 if (gid_is_valid(p->gid) && gid != p->gid)
298 return false;
299
300 return true;
301 }
302
303 static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
304
305 static const JsonDispatch dispatch_table[] = {
306 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 },
307 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE },
308 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
309 {}
310 };
311
312 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
313 LookupParameters p = {
314 .gid = GID_INVALID,
315 };
316 _cleanup_free_ char *found_name = NULL;
317 uid_t found_gid = GID_INVALID, gid;
318 Manager *m = userdata;
319 const char *gn;
320 int r;
321
322 assert(parameters);
323 assert(m);
324
325 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
326 if (r < 0)
327 return r;
328
329 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
330 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
331
332 if (gid_is_valid(p.gid))
333 r = dynamic_user_lookup_uid(m, (uid_t) p.gid, &found_name);
334 else if (p.group_name)
335 r = dynamic_user_lookup_name(m, p.group_name, (uid_t*) &found_gid);
336 else {
337 DynamicUser *d;
338
339 HASHMAP_FOREACH(d, m->dynamic_users) {
340 uid_t uid;
341
342 r = dynamic_user_current(d, &uid);
343 if (r == -EAGAIN)
344 continue;
345 if (r < 0)
346 return r;
347
348 if (!group_match_lookup_parameters(&p, d->name, (gid_t) uid))
349 continue;
350
351 if (v) {
352 r = varlink_notify(link, v);
353 if (r < 0)
354 return r;
355
356 v = json_variant_unref(v);
357 }
358
359 r = build_group_json(d->name, (gid_t) uid, &v);
360 if (r < 0)
361 return r;
362 }
363
364 if (!v)
365 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
366
367 return varlink_reply(link, v);
368 }
369 if (r == -ESRCH)
370 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
371 if (r < 0)
372 return r;
373
374 gid = gid_is_valid(found_gid) ? found_gid : p.gid;
375 gn = found_name ?: p.group_name;
376
377 if (!group_match_lookup_parameters(&p, gn, gid))
378 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
379
380 r = build_group_json(gn, gid, &v);
381 if (r < 0)
382 return r;
383
384 return varlink_reply(link, v);
385 }
386
387 static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
388
389 static const JsonDispatch dispatch_table[] = {
390 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE },
391 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE },
392 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
393 {}
394 };
395
396 LookupParameters p = {};
397 int r;
398
399 assert(parameters);
400
401 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
402 if (r < 0)
403 return r;
404
405 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
406 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
407
408 /* We don't support auxiliary groups with dynamic users. */
409 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
410 }
411
412 static void vl_disconnect(VarlinkServer *s, Varlink *link, void *userdata) {
413 Manager *m = userdata;
414
415 assert(m);
416 assert(s);
417 assert(link);
418
419 if (link == m->managed_oom_varlink_request)
420 m->managed_oom_varlink_request = varlink_unref(link);
421 }
422
423 int manager_varlink_init(Manager *m) {
424 _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL;
425 int r;
426
427 assert(m);
428
429 if (m->varlink_server)
430 return 0;
431
432 if (!MANAGER_IS_SYSTEM(m))
433 return 0;
434
435 r = varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID);
436 if (r < 0)
437 return log_error_errno(r, "Failed to allocate varlink server object: %m");
438
439 varlink_server_set_userdata(s, m);
440
441 r = varlink_server_bind_method_many(
442 s,
443 "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record,
444 "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record,
445 "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships,
446 "io.systemd.ManagedOOM.SubscribeManagedOOMCGroups", vl_method_subscribe_managed_oom_cgroups);
447 if (r < 0)
448 return log_error_errno(r, "Failed to register varlink methods: %m");
449
450 r = varlink_server_bind_disconnect(s, vl_disconnect);
451 if (r < 0)
452 return log_error_errno(r, "Failed to register varlink disconnect handler: %m");
453
454 if (!MANAGER_IS_TEST_RUN(m)) {
455 (void) mkdir_p_label("/run/systemd/userdb", 0755);
456
457 r = varlink_server_listen_address(s, "/run/systemd/userdb/io.systemd.DynamicUser", 0666);
458 if (r < 0)
459 return log_error_errno(r, "Failed to bind to varlink socket: %m");
460
461 r = varlink_server_listen_address(s, VARLINK_ADDR_PATH_MANAGED_OOM, 0666);
462 if (r < 0)
463 return log_error_errno(r, "Failed to bind to varlink socket: %m");
464 }
465
466 r = varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL);
467 if (r < 0)
468 return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
469
470 m->varlink_server = TAKE_PTR(s);
471 return 0;
472 }
473
474 void manager_varlink_done(Manager *m) {
475 assert(m);
476
477 /* Send the final message if we still have a subscribe request open. */
478 if (m->managed_oom_varlink_request)
479 m->managed_oom_varlink_request = varlink_close_unref(m->managed_oom_varlink_request);
480
481 m->varlink_server = varlink_server_unref(m->varlink_server);
482 }