mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 08:25:20 +09:00
core-varlink: Introduce manager_varlink_managed_oom_connect()
In the next commit, we'll introduce a varlink server for the user manager. As preparation for that, let's introduce a new function to initialize only the managed OOM connection whenever we send a managed OOM update.
This commit is contained in:
@@ -95,70 +95,6 @@ static int build_managed_oom_json_array_element(Unit *u, const char *property, s
|
||||
SD_JSON_BUILD_PAIR_CONDITION(use_limit, "limit", SD_JSON_BUILD_UNSIGNED(c->moom_mem_pressure_limit)));
|
||||
}
|
||||
|
||||
int manager_varlink_send_managed_oom_update(Unit *u) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *arr = NULL, *v = NULL;
|
||||
CGroupRuntime *crt;
|
||||
CGroupContext *c;
|
||||
int r;
|
||||
|
||||
assert(u);
|
||||
|
||||
if (!UNIT_VTABLE(u)->can_set_managed_oom || !u->manager)
|
||||
return 0;
|
||||
|
||||
crt = unit_get_cgroup_runtime(u);
|
||||
if (!crt || !crt->cgroup_path)
|
||||
return 0;
|
||||
|
||||
if (MANAGER_IS_SYSTEM(u->manager)) {
|
||||
/* In system mode we can't send any notifications unless oomd connected back to us. In this
|
||||
* mode oomd must initiate communication, not us. */
|
||||
if (!u->manager->managed_oom_varlink)
|
||||
return 0;
|
||||
} else {
|
||||
/* If we are in user mode, let's connect to oomd if we aren't connected yet. In this mode we
|
||||
* must initiate communication to oomd, not the other way round. */
|
||||
r = manager_varlink_init(u->manager);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
c = unit_get_cgroup_context(u);
|
||||
if (!c)
|
||||
return 0;
|
||||
|
||||
r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
FOREACH_ELEMENT(i, managed_oom_mode_properties) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL;
|
||||
|
||||
r = build_managed_oom_json_array_element(u, *i, &e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_json_variant_append_array(&arr, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = sd_json_buildo(&v, SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (MANAGER_IS_SYSTEM(u->manager))
|
||||
/* in system mode, oomd is our client, thus send out notifications as replies to the
|
||||
* initiating method call from them. */
|
||||
r = sd_varlink_notify(u->manager->managed_oom_varlink, v);
|
||||
else
|
||||
/* in user mode, we are oomd's client, thus send out notifications as method calls that do
|
||||
* not expect a reply. */
|
||||
r = sd_varlink_send(u->manager->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int build_managed_oom_cgroups_json(Manager *m, sd_json_variant **ret) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *arr = NULL;
|
||||
int r;
|
||||
@@ -213,6 +149,150 @@ static int build_managed_oom_cgroups_json(Manager *m, sd_json_variant **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int manager_varlink_send_managed_oom_initial(Manager *m) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
if (MANAGER_IS_SYSTEM(m))
|
||||
return 0;
|
||||
|
||||
assert(m->managed_oom_varlink);
|
||||
|
||||
r = build_managed_oom_cgroups_json(m, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return sd_varlink_send(m->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v);
|
||||
}
|
||||
|
||||
static int manager_varlink_managed_oom_connect(Manager *m);
|
||||
|
||||
static int managed_oom_vl_reply(sd_varlink *link, sd_json_variant *parameters, const char *error_id, sd_varlink_reply_flags_t flags, void *userdata) {
|
||||
Manager *m = ASSERT_PTR(userdata);
|
||||
int r;
|
||||
|
||||
if (error_id)
|
||||
log_debug("varlink systemd-oomd client error: %s", error_id);
|
||||
|
||||
if (FLAGS_SET(flags, SD_VARLINK_REPLY_ERROR|SD_VARLINK_REPLY_LOCAL)) {
|
||||
/* sd_varlink connection was closed, likely because of systemd-oomd restart. Let's try to
|
||||
* reconnect and send the initial ManagedOOM update again. */
|
||||
|
||||
m->managed_oom_varlink = sd_varlink_unref(link);
|
||||
|
||||
log_debug("Reconnecting to %s", VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
|
||||
r = manager_varlink_managed_oom_connect(m);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int manager_varlink_managed_oom_connect(Manager *m) {
|
||||
_cleanup_(sd_varlink_close_unrefp) sd_varlink *link = NULL;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
if (m->managed_oom_varlink)
|
||||
return 1;
|
||||
|
||||
r = sd_varlink_connect_address(&link, VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
if (r == -ENOENT)
|
||||
return 0;
|
||||
if (ERRNO_IS_NEG_DISCONNECT(r)) {
|
||||
log_debug_errno(r, "systemd-oomd varlink socket isn't available, skipping user manager varlink setup: %m");
|
||||
return 0;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect to '%s': %m", VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
|
||||
sd_varlink_set_userdata(link, m);
|
||||
|
||||
r = sd_varlink_bind_reply(link, managed_oom_vl_reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_varlink_attach_event(link, m->event, EVENT_PRIORITY_IPC);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
|
||||
|
||||
m->managed_oom_varlink = TAKE_PTR(link);
|
||||
|
||||
/* Queue the initial ManagedOOM update. */
|
||||
(void) manager_varlink_send_managed_oom_initial(m);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int manager_varlink_send_managed_oom_update(Unit *u) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *arr = NULL, *v = NULL;
|
||||
CGroupRuntime *crt;
|
||||
CGroupContext *c;
|
||||
int r;
|
||||
|
||||
assert(u);
|
||||
|
||||
if (!UNIT_VTABLE(u)->can_set_managed_oom || !u->manager)
|
||||
return 0;
|
||||
|
||||
crt = unit_get_cgroup_runtime(u);
|
||||
if (!crt || !crt->cgroup_path)
|
||||
return 0;
|
||||
|
||||
if (MANAGER_IS_SYSTEM(u->manager)) {
|
||||
/* In system mode we can't send any notifications unless oomd connected back to us. In this
|
||||
* mode oomd must initiate communication, not us. */
|
||||
if (!u->manager->managed_oom_varlink)
|
||||
return 0;
|
||||
} else {
|
||||
/* If we are in user mode, let's connect to oomd if we aren't connected yet. In this mode we
|
||||
* must initiate communication to oomd, not the other way round. */
|
||||
r = manager_varlink_managed_oom_connect(u->manager);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
c = unit_get_cgroup_context(u);
|
||||
if (!c)
|
||||
return 0;
|
||||
|
||||
r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
FOREACH_ELEMENT(i, managed_oom_mode_properties) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL;
|
||||
|
||||
r = build_managed_oom_json_array_element(u, *i, &e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_json_variant_append_array(&arr, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = sd_json_buildo(&v, SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (MANAGER_IS_SYSTEM(u->manager))
|
||||
/* in system mode, oomd is our client, thus send out notifications as replies to the
|
||||
* initiating method call from them. */
|
||||
r = sd_varlink_notify(u->manager->managed_oom_varlink, v);
|
||||
else
|
||||
/* in user mode, we are oomd's client, thus send out notifications as method calls that do
|
||||
* not expect a reply. */
|
||||
r = sd_varlink_send(u->manager->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int vl_method_subscribe_managed_oom_cgroups(
|
||||
sd_varlink *link,
|
||||
sd_json_variant *parameters,
|
||||
@@ -261,24 +341,6 @@ static int vl_method_subscribe_managed_oom_cgroups(
|
||||
return sd_varlink_notify(m->managed_oom_varlink, v);
|
||||
}
|
||||
|
||||
static int manager_varlink_send_managed_oom_initial(Manager *m) {
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
if (MANAGER_IS_SYSTEM(m))
|
||||
return 0;
|
||||
|
||||
assert(m->managed_oom_varlink);
|
||||
|
||||
r = build_managed_oom_cgroups_json(m, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return sd_varlink_send(m->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v);
|
||||
}
|
||||
|
||||
static int vl_method_get_user_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
|
||||
|
||||
static const sd_json_dispatch_field dispatch_table[] = {
|
||||
@@ -589,70 +651,16 @@ static int manager_varlink_init_system(Manager *m) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int vl_reply(sd_varlink *link, sd_json_variant *parameters, const char *error_id, sd_varlink_reply_flags_t flags, void *userdata) {
|
||||
Manager *m = ASSERT_PTR(userdata);
|
||||
int r;
|
||||
|
||||
if (error_id)
|
||||
log_debug("varlink systemd-oomd client error: %s", error_id);
|
||||
|
||||
if (FLAGS_SET(flags, SD_VARLINK_REPLY_ERROR|SD_VARLINK_REPLY_LOCAL)) {
|
||||
/* sd_varlink connection was closed, likely because of systemd-oomd restart. Let's try to
|
||||
* reconnect and send the initial ManagedOOM update again. */
|
||||
|
||||
m->managed_oom_varlink = sd_varlink_unref(link);
|
||||
|
||||
log_debug("Reconnecting to %s", VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
|
||||
r = manager_varlink_init(m);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int manager_varlink_init_user(Manager *m) {
|
||||
_cleanup_(sd_varlink_close_unrefp) sd_varlink *link = NULL;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
if (m->managed_oom_varlink)
|
||||
return 1;
|
||||
|
||||
if (!MANAGER_IS_USER(m))
|
||||
return 0;
|
||||
|
||||
if (MANAGER_IS_TEST_RUN(m))
|
||||
return 0;
|
||||
|
||||
r = sd_varlink_connect_address(&link, VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
if (r == -ENOENT)
|
||||
return 0;
|
||||
if (ERRNO_IS_NEG_DISCONNECT(r)) {
|
||||
log_debug_errno(r, "systemd-oomd varlink socket isn't available, skipping user manager varlink setup: %m");
|
||||
return 0;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to connect to '%s': %m", VARLINK_ADDR_PATH_MANAGED_OOM_USER);
|
||||
|
||||
sd_varlink_set_userdata(link, m);
|
||||
|
||||
r = sd_varlink_bind_reply(link, vl_reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_varlink_attach_event(link, m->event, EVENT_PRIORITY_IPC);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
|
||||
|
||||
m->managed_oom_varlink = TAKE_PTR(link);
|
||||
|
||||
/* Queue the initial ManagedOOM update. */
|
||||
(void) manager_varlink_send_managed_oom_initial(m);
|
||||
|
||||
return 1;
|
||||
return manager_varlink_managed_oom_connect(m);
|
||||
}
|
||||
|
||||
int manager_varlink_init(Manager *m) {
|
||||
|
||||
Reference in New Issue
Block a user