1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "core-varlink.h"
4 #include "mkdir-label.h"
9 typedef struct LookupParameters
{
10 const char *user_name
;
11 const char *group_name
;
19 static const char* const managed_oom_mode_properties
[] = {
21 "ManagedOOMMemoryPressure",
24 static int build_user_json(const char *user_name
, uid_t uid
, JsonVariant
**ret
) {
26 assert(uid_is_valid(uid
));
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_CONST_STRING("Dynamic User")),
35 JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")),
36 JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN
)),
37 JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)),
38 JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")),
39 JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic"))))));
42 static bool user_match_lookup_parameters(LookupParameters
*p
, const char *name
, uid_t uid
) {
45 if (p
->user_name
&& !streq(name
, p
->user_name
))
48 if (uid_is_valid(p
->uid
) && uid
!= p
->uid
)
54 static int build_managed_oom_json_array_element(Unit
*u
, const char *property
, JsonVariant
**ret_v
) {
55 bool use_limit
= false;
63 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
66 c
= unit_get_cgroup_context(u
);
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
);
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
))));
89 int manager_varlink_send_managed_oom_update(Unit
*u
) {
90 _cleanup_(json_variant_unrefp
) JsonVariant
*arr
= NULL
, *v
= NULL
;
96 if (!UNIT_VTABLE(u
)->can_set_managed_oom
|| !u
->manager
|| !u
->cgroup_path
)
99 if (MANAGER_IS_SYSTEM(u
->manager
)) {
100 /* In system mode we can't send any notifications unless oomd connected back to us. In this
101 * mode oomd must initiate communication, not us. */
102 if (!u
->manager
->managed_oom_varlink
)
105 /* If we are in user mode, let's connect to oomd if we aren't connected yet. In this mode we
106 * must initiate communication to oomd, not the other way round. */
107 r
= manager_varlink_init(u
->manager
);
112 c
= unit_get_cgroup_context(u
);
116 r
= json_build(&arr
, JSON_BUILD_EMPTY_ARRAY
);
120 for (size_t i
= 0; i
< ELEMENTSOF(managed_oom_mode_properties
); i
++) {
121 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
;
123 r
= build_managed_oom_json_array_element(u
, managed_oom_mode_properties
[i
], &e
);
127 r
= json_variant_append_array(&arr
, e
);
132 r
= json_build(&v
, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr
))));
136 if (MANAGER_IS_SYSTEM(u
->manager
))
137 /* in system mode, oomd is our client, thus send out notifications as replies to the
138 * initiating method call from them. */
139 r
= varlink_notify(u
->manager
->managed_oom_varlink
, v
);
141 /* in user mode, we are oomd's client, thus send out notifications as method calls that do
142 * not expect a reply. */
143 r
= varlink_send(u
->manager
->managed_oom_varlink
, "io.systemd.oom.ReportManagedOOMCGroups", v
);
148 static int build_managed_oom_cgroups_json(Manager
*m
, JsonVariant
**ret
) {
149 static const UnitType supported_unit_types
[] = { UNIT_SLICE
, UNIT_SERVICE
, UNIT_SCOPE
};
150 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
, *arr
= NULL
;
156 r
= json_build(&arr
, JSON_BUILD_EMPTY_ARRAY
);
160 for (size_t i
= 0; i
< ELEMENTSOF(supported_unit_types
); i
++) {
163 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[supported_unit_types
[i
]]) {
166 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
169 c
= unit_get_cgroup_context(u
);
173 for (size_t j
= 0; j
< ELEMENTSOF(managed_oom_mode_properties
); j
++) {
174 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
;
176 /* For the initial varlink call we only care about units that enabled (i.e. mode is not
177 * set to "auto") oomd properties. */
178 if (!(streq(managed_oom_mode_properties
[j
], "ManagedOOMSwap") && c
->moom_swap
== MANAGED_OOM_KILL
) &&
179 !(streq(managed_oom_mode_properties
[j
], "ManagedOOMMemoryPressure") && c
->moom_mem_pressure
== MANAGED_OOM_KILL
))
182 r
= build_managed_oom_json_array_element(u
, managed_oom_mode_properties
[j
], &e
);
186 r
= json_variant_append_array(&arr
, e
);
193 r
= json_build(&v
, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr
))));
201 static int vl_method_subscribe_managed_oom_cgroups(
203 JsonVariant
*parameters
,
204 VarlinkMethodFlags flags
,
207 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
208 Manager
*m
= userdata
;
214 if (json_variant_elements(parameters
) > 0)
215 return varlink_error_invalid_parameter(link
, parameters
);
217 /* We only take one subscriber for this method so return an error if there's already an existing one.
218 * This shouldn't happen since systemd-oomd is the only client of this method. */
219 if (FLAGS_SET(flags
, VARLINK_METHOD_MORE
) && m
->managed_oom_varlink
)
220 return varlink_error(link
, VARLINK_ERROR_SUBSCRIPTION_TAKEN
, NULL
);
222 r
= build_managed_oom_cgroups_json(m
, &v
);
226 if (!FLAGS_SET(flags
, VARLINK_METHOD_MORE
))
227 return varlink_reply(link
, v
);
229 assert(!m
->managed_oom_varlink
);
230 m
->managed_oom_varlink
= varlink_ref(link
);
231 return varlink_notify(m
->managed_oom_varlink
, v
);
234 static int manager_varlink_send_managed_oom_initial(Manager
*m
) {
235 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
240 if (MANAGER_IS_SYSTEM(m
))
243 assert(m
->managed_oom_varlink
);
245 r
= build_managed_oom_cgroups_json(m
, &v
);
249 return varlink_send(m
->managed_oom_varlink
, "io.systemd.oom.ReportManagedOOMCGroups", v
);
252 static int vl_method_get_user_record(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
254 static const JsonDispatch dispatch_table
[] = {
255 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(LookupParameters
, uid
), 0 },
256 { "userName", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, user_name
), JSON_SAFE
},
257 { "service", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, service
), 0 },
261 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
262 LookupParameters p
= {
265 _cleanup_free_
char *found_name
= NULL
;
266 uid_t found_uid
= UID_INVALID
, uid
;
267 Manager
*m
= userdata
;
274 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
278 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
279 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
281 if (uid_is_valid(p
.uid
))
282 r
= dynamic_user_lookup_uid(m
, p
.uid
, &found_name
);
283 else if (p
.user_name
)
284 r
= dynamic_user_lookup_name(m
, p
.user_name
, &found_uid
);
288 HASHMAP_FOREACH(d
, m
->dynamic_users
) {
289 r
= dynamic_user_current(d
, &uid
);
290 if (r
== -EAGAIN
) /* not realized yet? */
295 if (!user_match_lookup_parameters(&p
, d
->name
, uid
))
299 r
= varlink_notify(link
, v
);
303 v
= json_variant_unref(v
);
306 r
= build_user_json(d
->name
, uid
, &v
);
312 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
314 return varlink_reply(link
, v
);
317 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
321 uid
= uid_is_valid(found_uid
) ? found_uid
: p
.uid
;
322 un
= found_name
?: p
.user_name
;
324 if (!user_match_lookup_parameters(&p
, un
, uid
))
325 return varlink_error(link
, "io.systemd.UserDatabase.ConflictingRecordFound", NULL
);
327 r
= build_user_json(un
, uid
, &v
);
331 return varlink_reply(link
, v
);
334 static int build_group_json(const char *group_name
, gid_t gid
, JsonVariant
**ret
) {
336 assert(gid_is_valid(gid
));
339 return json_build(ret
, JSON_BUILD_OBJECT(
340 JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT(
341 JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(group_name
)),
342 JSON_BUILD_PAIR("description", JSON_BUILD_CONST_STRING("Dynamic Group")),
343 JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid
)),
344 JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")),
345 JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic"))))));
348 static bool group_match_lookup_parameters(LookupParameters
*p
, const char *name
, gid_t gid
) {
351 if (p
->group_name
&& !streq(name
, p
->group_name
))
354 if (gid_is_valid(p
->gid
) && gid
!= p
->gid
)
360 static int vl_method_get_group_record(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
362 static const JsonDispatch dispatch_table
[] = {
363 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(LookupParameters
, gid
), 0 },
364 { "groupName", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, group_name
), JSON_SAFE
},
365 { "service", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, service
), 0 },
369 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
370 LookupParameters p
= {
373 _cleanup_free_
char *found_name
= NULL
;
374 uid_t found_gid
= GID_INVALID
, gid
;
375 Manager
*m
= userdata
;
382 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
386 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
387 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
389 if (gid_is_valid(p
.gid
))
390 r
= dynamic_user_lookup_uid(m
, (uid_t
) p
.gid
, &found_name
);
391 else if (p
.group_name
)
392 r
= dynamic_user_lookup_name(m
, p
.group_name
, (uid_t
*) &found_gid
);
396 HASHMAP_FOREACH(d
, m
->dynamic_users
) {
399 r
= dynamic_user_current(d
, &uid
);
405 if (!group_match_lookup_parameters(&p
, d
->name
, (gid_t
) uid
))
409 r
= varlink_notify(link
, v
);
413 v
= json_variant_unref(v
);
416 r
= build_group_json(d
->name
, (gid_t
) uid
, &v
);
422 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
424 return varlink_reply(link
, v
);
427 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
431 gid
= gid_is_valid(found_gid
) ? found_gid
: p
.gid
;
432 gn
= found_name
?: p
.group_name
;
434 if (!group_match_lookup_parameters(&p
, gn
, gid
))
435 return varlink_error(link
, "io.systemd.UserDatabase.ConflictingRecordFound", NULL
);
437 r
= build_group_json(gn
, gid
, &v
);
441 return varlink_reply(link
, v
);
444 static int vl_method_get_memberships(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
446 static const JsonDispatch dispatch_table
[] = {
447 { "userName", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, user_name
), JSON_SAFE
},
448 { "groupName", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, group_name
), JSON_SAFE
},
449 { "service", JSON_VARIANT_STRING
, json_dispatch_const_string
, offsetof(LookupParameters
, service
), 0 },
453 LookupParameters p
= {};
458 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
462 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
463 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
465 /* We don't support auxiliary groups with dynamic users. */
466 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
469 static void vl_disconnect(VarlinkServer
*s
, Varlink
*link
, void *userdata
) {
470 Manager
*m
= userdata
;
476 if (link
== m
->managed_oom_varlink
)
477 m
->managed_oom_varlink
= varlink_unref(link
);
480 static int manager_varlink_init_system(Manager
*m
) {
481 _cleanup_(varlink_server_unrefp
) VarlinkServer
*s
= NULL
;
486 if (m
->varlink_server
)
489 if (!MANAGER_IS_SYSTEM(m
))
492 r
= varlink_server_new(&s
, VARLINK_SERVER_ACCOUNT_UID
|VARLINK_SERVER_INHERIT_USERDATA
);
494 return log_error_errno(r
, "Failed to allocate varlink server object: %m");
496 varlink_server_set_userdata(s
, m
);
498 r
= varlink_server_bind_method_many(
500 "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record
,
501 "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record
,
502 "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships
,
503 "io.systemd.ManagedOOM.SubscribeManagedOOMCGroups", vl_method_subscribe_managed_oom_cgroups
);
505 return log_error_errno(r
, "Failed to register varlink methods: %m");
507 r
= varlink_server_bind_disconnect(s
, vl_disconnect
);
509 return log_error_errno(r
, "Failed to register varlink disconnect handler: %m");
511 if (!MANAGER_IS_TEST_RUN(m
)) {
512 (void) mkdir_p_label("/run/systemd/userdb", 0755);
514 r
= varlink_server_listen_address(s
, "/run/systemd/userdb/io.systemd.DynamicUser", 0666);
516 return log_error_errno(r
, "Failed to bind to varlink socket: %m");
518 r
= varlink_server_listen_address(s
, VARLINK_ADDR_PATH_MANAGED_OOM_SYSTEM
, 0666);
520 return log_error_errno(r
, "Failed to bind to varlink socket: %m");
523 r
= varlink_server_attach_event(s
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
525 return log_error_errno(r
, "Failed to attach varlink connection to event loop: %m");
527 m
->varlink_server
= TAKE_PTR(s
);
531 static int vl_reply(Varlink
*link
, JsonVariant
*parameters
, const char *error_id
, VarlinkReplyFlags flags
, void *userdata
) {
532 Manager
*m
= userdata
;
538 log_debug("varlink systemd-oomd client error: %s", error_id
);
540 if (FLAGS_SET(flags
, VARLINK_REPLY_ERROR
) && FLAGS_SET(flags
, VARLINK_REPLY_LOCAL
)) {
541 /* Varlink connection was closed, likely because of systemd-oomd restart. Let's try to
542 * reconnect and send the initial ManagedOOM update again. */
544 m
->managed_oom_varlink
= varlink_unref(link
);
546 log_debug("Reconnecting to %s", VARLINK_ADDR_PATH_MANAGED_OOM_USER
);
548 r
= manager_varlink_init(m
);
556 static int manager_varlink_init_user(Manager
*m
) {
557 _cleanup_(varlink_close_unrefp
) Varlink
*link
= NULL
;
562 if (m
->managed_oom_varlink
)
565 if (MANAGER_IS_TEST_RUN(m
))
568 r
= varlink_connect_address(&link
, VARLINK_ADDR_PATH_MANAGED_OOM_USER
);
569 if (r
== -ENOENT
|| ERRNO_IS_DISCONNECT(r
)) {
570 log_debug("systemd-oomd varlink unix socket not found, skipping user manager varlink setup");
574 return log_error_errno(r
, "Failed to connect to %s: %m", VARLINK_ADDR_PATH_MANAGED_OOM_USER
);
576 varlink_set_userdata(link
, m
);
578 r
= varlink_bind_reply(link
, vl_reply
);
582 r
= varlink_attach_event(link
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
584 return log_error_errno(r
, "Failed to attach varlink connection to event loop: %m");
586 m
->managed_oom_varlink
= TAKE_PTR(link
);
588 /* Queue the initial ManagedOOM update. */
589 (void) manager_varlink_send_managed_oom_initial(m
);
594 int manager_varlink_init(Manager
*m
) {
595 return MANAGER_IS_SYSTEM(m
) ? manager_varlink_init_system(m
) : manager_varlink_init_user(m
);
598 void manager_varlink_done(Manager
*m
) {
601 /* Explicitly close the varlink connection to oomd. Note we first take the varlink connection out of
602 * the manager, and only then disconnect it — in two steps – so that we don't end up accidentally
603 * unreffing it twice. After all, closing the connection might cause the disconnect handler we
604 * installed (vl_disconnect() above) to be called, where we will unref it too. */
605 varlink_close_unref(TAKE_PTR(m
->managed_oom_varlink
));
607 m
->varlink_server
= varlink_server_unref(m
->varlink_server
);
608 m
->managed_oom_varlink
= varlink_close_unref(m
->managed_oom_varlink
);