1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "core-varlink.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_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"))))));
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_permyriad
))));
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
->manager
->managed_oom_varlink_request
|| !u
->cgroup_path
)
99 c
= unit_get_cgroup_context(u
);
103 r
= json_build(&arr
, JSON_BUILD_EMPTY_ARRAY
);
107 for (size_t i
= 0; i
< ELEMENTSOF(managed_oom_mode_properties
); i
++) {
108 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
;
110 r
= build_managed_oom_json_array_element(u
, managed_oom_mode_properties
[i
], &e
);
114 r
= json_variant_append_array(&arr
, e
);
119 r
= json_build(&v
, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr
))));
123 return varlink_notify(u
->manager
->managed_oom_varlink_request
, v
);
126 static int vl_method_subscribe_managed_oom_cgroups(
128 JsonVariant
*parameters
,
129 VarlinkMethodFlags flags
,
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
;
139 if (json_variant_elements(parameters
) > 0)
140 return varlink_error_invalid_parameter(link
, parameters
);
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
);
147 r
= json_build(&arr
, JSON_BUILD_EMPTY_ARRAY
);
151 for (size_t i
= 0; i
< ELEMENTSOF(supported_unit_types
); i
++) {
154 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[supported_unit_types
[i
]]) {
157 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)))
160 c
= unit_get_cgroup_context(u
);
164 for (size_t j
= 0; j
< ELEMENTSOF(managed_oom_mode_properties
); j
++) {
165 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
;
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
))
173 r
= build_managed_oom_json_array_element(u
, managed_oom_mode_properties
[j
], &e
);
177 r
= json_variant_append_array(&arr
, e
);
184 r
= json_build(&v
, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr
))));
188 if (!FLAGS_SET(flags
, VARLINK_METHOD_MORE
))
189 return varlink_reply(link
, v
);
191 m
->managed_oom_varlink_request
= varlink_ref(link
);
192 return varlink_notify(m
->managed_oom_varlink_request
, v
);
195 static int vl_method_get_user_record(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
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 },
204 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
205 LookupParameters p
= {
208 _cleanup_free_
char *found_name
= NULL
;
209 uid_t found_uid
= UID_INVALID
, uid
;
210 Manager
*m
= userdata
;
217 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
221 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
222 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
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
);
231 HASHMAP_FOREACH(d
, m
->dynamic_users
) {
232 r
= dynamic_user_current(d
, &uid
);
233 if (r
== -EAGAIN
) /* not realized yet? */
238 if (!user_match_lookup_parameters(&p
, d
->name
, uid
))
242 r
= varlink_notify(link
, v
);
246 v
= json_variant_unref(v
);
249 r
= build_user_json(d
->name
, uid
, &v
);
255 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
257 return varlink_reply(link
, v
);
260 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
264 uid
= uid_is_valid(found_uid
) ? found_uid
: p
.uid
;
265 un
= found_name
?: p
.user_name
;
267 if (!user_match_lookup_parameters(&p
, un
, uid
))
268 return varlink_error(link
, "io.systemd.UserDatabase.ConflictingRecordFound", NULL
);
270 r
= build_user_json(un
, uid
, &v
);
274 return varlink_reply(link
, v
);
277 static int build_group_json(const char *group_name
, gid_t gid
, JsonVariant
**ret
) {
279 assert(gid_is_valid(gid
));
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"))))));
291 static bool group_match_lookup_parameters(LookupParameters
*p
, const char *name
, gid_t gid
) {
294 if (p
->group_name
&& !streq(name
, p
->group_name
))
297 if (gid_is_valid(p
->gid
) && gid
!= p
->gid
)
303 static int vl_method_get_group_record(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
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 },
312 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
313 LookupParameters p
= {
316 _cleanup_free_
char *found_name
= NULL
;
317 uid_t found_gid
= GID_INVALID
, gid
;
318 Manager
*m
= userdata
;
325 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
329 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
330 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
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
);
339 HASHMAP_FOREACH(d
, m
->dynamic_users
) {
342 r
= dynamic_user_current(d
, &uid
);
348 if (!group_match_lookup_parameters(&p
, d
->name
, (gid_t
) uid
))
352 r
= varlink_notify(link
, v
);
356 v
= json_variant_unref(v
);
359 r
= build_group_json(d
->name
, (gid_t
) uid
, &v
);
365 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
367 return varlink_reply(link
, v
);
370 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
374 gid
= gid_is_valid(found_gid
) ? found_gid
: p
.gid
;
375 gn
= found_name
?: p
.group_name
;
377 if (!group_match_lookup_parameters(&p
, gn
, gid
))
378 return varlink_error(link
, "io.systemd.UserDatabase.ConflictingRecordFound", NULL
);
380 r
= build_group_json(gn
, gid
, &v
);
384 return varlink_reply(link
, v
);
387 static int vl_method_get_memberships(Varlink
*link
, JsonVariant
*parameters
, VarlinkMethodFlags flags
, void *userdata
) {
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 },
396 LookupParameters p
= {};
401 r
= json_dispatch(parameters
, dispatch_table
, NULL
, 0, &p
);
405 if (!streq_ptr(p
.service
, "io.systemd.DynamicUser"))
406 return varlink_error(link
, "io.systemd.UserDatabase.BadService", NULL
);
408 /* We don't support auxiliary groups with dynamic users. */
409 return varlink_error(link
, "io.systemd.UserDatabase.NoRecordFound", NULL
);
412 static void vl_disconnect(VarlinkServer
*s
, Varlink
*link
, void *userdata
) {
413 Manager
*m
= userdata
;
419 if (link
== m
->managed_oom_varlink_request
)
420 m
->managed_oom_varlink_request
= varlink_unref(link
);
423 int manager_varlink_init(Manager
*m
) {
424 _cleanup_(varlink_server_unrefp
) VarlinkServer
*s
= NULL
;
429 if (m
->varlink_server
)
432 if (!MANAGER_IS_SYSTEM(m
))
435 r
= varlink_server_new(&s
, VARLINK_SERVER_ACCOUNT_UID
|VARLINK_SERVER_INHERIT_USERDATA
);
437 return log_error_errno(r
, "Failed to allocate varlink server object: %m");
439 varlink_server_set_userdata(s
, m
);
441 r
= varlink_server_bind_method_many(
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
);
448 return log_error_errno(r
, "Failed to register varlink methods: %m");
450 r
= varlink_server_bind_disconnect(s
, vl_disconnect
);
452 return log_error_errno(r
, "Failed to register varlink disconnect handler: %m");
454 if (!MANAGER_IS_TEST_RUN(m
)) {
455 (void) mkdir_p_label("/run/systemd/userdb", 0755);
457 r
= varlink_server_listen_address(s
, "/run/systemd/userdb/io.systemd.DynamicUser", 0666);
459 return log_error_errno(r
, "Failed to bind to varlink socket: %m");
461 r
= varlink_server_listen_address(s
, VARLINK_ADDR_PATH_MANAGED_OOM
, 0666);
463 return log_error_errno(r
, "Failed to bind to varlink socket: %m");
466 r
= varlink_server_attach_event(s
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
468 return log_error_errno(r
, "Failed to attach varlink connection to event loop: %m");
470 m
->varlink_server
= TAKE_PTR(s
);
474 void manager_varlink_done(Manager
*m
) {
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
);
481 m
->varlink_server
= varlink_server_unref(m
->varlink_server
);