#include "install.h"
#include "json-util.h"
#include "manager.h"
+#include "path-util.h"
#include "pidref.h"
#include "set.h"
#include "strv.h"
}
typedef struct UnitLookupParameters {
- const char *name;
+ const char *name, *cgroup;
PidRef pidref;
} UnitLookupParameters;
static int varlink_error_conflict_lookup_parameters(sd_varlink *v, const UnitLookupParameters *p) {
log_debug_errno(
ESRCH,
- "Searching unit by lookup parameters name='%s' pid="PID_FMT" resulted in multiple different units",
+ "Searching unit by lookup parameters name='%s' pid="PID_FMT" cgroup='%s' resulted in multiple different units",
p->name,
- p->pidref.pid);
+ p->pidref.pid,
+ p->cgroup);
return varlink_error_no_such_unit(v, /* name= */ NULL);
}
unit = pid_unit;
}
+ if (p->cgroup) {
+ if (!path_is_safe(p->cgroup))
+ return sd_varlink_error_invalid_parameter_name(link, "cgroup");
+
+ Unit *cgroup_unit = manager_get_unit_by_cgroup(manager, p->cgroup);
+ if (!cgroup_unit)
+ return varlink_error_no_such_unit(link, "cgroup");
+ if (cgroup_unit != unit && unit != NULL)
+ return varlink_error_conflict_lookup_parameters(link, p);
+
+ unit = cgroup_unit;
+ }
+
*ret_unit = unit;
return 0;
}
int vl_method_list_units(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
static const sd_json_dispatch_field dispatch_table[] = {
- { "name", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, offsetof(UnitLookupParameters, name), 0 /* allows UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE */ },
- { "pid", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref, offsetof(UnitLookupParameters, pidref), SD_JSON_RELAX /* allows PID_AUTOMATIC */ },
+ { "name", SD_JSON_VARIANT_STRING, json_dispatch_const_unit_name, offsetof(UnitLookupParameters, name), 0 /* allows UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE */ },
+ { "pid", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref, offsetof(UnitLookupParameters, pidref), SD_JSON_RELAX /* allows PID_AUTOMATIC */ },
+ { "cgroup", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(UnitLookupParameters, cgroup), SD_JSON_STRICT /* require normalized path */ },
{}
};
SD_VARLINK_DEFINE_INPUT(name, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("If non-null the PID of a unit. Special value 0 means to take pid of the caller."),
SD_VARLINK_DEFINE_INPUT_BY_TYPE(pid, ProcessId, SD_VARLINK_NULLABLE),
+ SD_VARLINK_FIELD_COMMENT("If non-null the cgroup of a unit"),
+ SD_VARLINK_DEFINE_INPUT(cgroup, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("Configuration of the unit"),
SD_VARLINK_DEFINE_OUTPUT_BY_TYPE(context, UnitContext, 0),
SD_VARLINK_FIELD_COMMENT("Runtime information of the unit"),