+
+char **cg_shorten_controllers(char **controllers) {
+ char **f, **t;
+
+ controllers = strv_uniq(controllers);
+
+ if (!controllers)
+ return controllers;
+
+ for (f = controllers, t = controllers; *f; f++) {
+ int r;
+ const char *p;
+
+ if (streq(*f, "systemd") || streq(*f, SYSTEMD_CGROUP_CONTROLLER)) {
+ free(*f);
+ continue;
+ }
+
+ p = normalize_controller(*f);
+
+ r = check(p);
+ if (r < 0) {
+ log_debug("Controller %s is not available, removing from controllers list.", *f);
+ free(*f);
+ continue;
+ }
+
+ *(t++) = *f;
+ }
+
+ *t = NULL;
+ return controllers;
+}
+
+int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
+ char *cg_process, *cg_init, *p;
+ int r;
+
+ assert(pid >= 0);
+
+ if (pid == 0)
+ pid = getpid();
+
+ r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
+ if (r < 0)
+ return r;
+
+ r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &cg_init);
+ if (r < 0) {
+ free(cg_process);
+ return r;
+ }
+
+ if (endswith(cg_init, "/system"))
+ cg_init[strlen(cg_init)-7] = 0;
+ else if (streq(cg_init, "/"))
+ cg_init[0] = 0;
+
+ if (startswith(cg_process, cg_init))
+ p = cg_process + strlen(cg_init);
+ else
+ p = cg_process;
+
+ free(cg_init);
+
+ if (cgroup) {
+ char* c;
+
+ c = strdup(p);
+ if (!c) {
+ free(cg_process);
+ return -ENOMEM;
+ }
+
+ *cgroup = c;
+ }
+
+ if (root) {
+ cg_process[p-cg_process] = 0;
+ *root = cg_process;
+ } else
+ free(cg_process);
+
+ return 0;
+}
+
+static int instance_unit_from_cgroup(char *cgroup){
+ char *at;
+
+ assert(cgroup);
+
+ at = strstr(cgroup, "@.");
+ if (at) {
+ /* This is a templated service */
+
+ char *i;
+ char _cleanup_free_ *i2 = NULL, *s = NULL;
+
+ i = strchr(at, '/');
+ if (!i || !i[1]) /* disallow empty instances */
+ return -EINVAL;
+
+ s = strndup(at + 1, i - at - 1);
+ i2 = strdup(i + 1);
+ if (!s || !i2)
+ return -ENOMEM;
+
+ strcpy(at + 1, i2);
+ strcat(at + 1, s);
+ }
+
+ return 0;
+}
+
+/* non-static only for testing purposes */
+int cgroup_to_unit(char *cgroup, char **unit){
+ int r;
+ char *p;
+
+ assert(cgroup);
+ assert(unit);
+
+ r = instance_unit_from_cgroup(cgroup);
+ if (r < 0)
+ return r;
+
+ p = strrchr(cgroup, '/');
+ assert(p);
+
+ r = unit_name_is_valid(p + 1, true);
+ if (!r)
+ return -EINVAL;
+
+ *unit = strdup(p + 1);
+ if (!*unit)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static int cg_pid_get(const char *prefix, pid_t pid, char **unit) {
+ int r;
+ char _cleanup_free_ *cgroup = NULL;
+
+ assert(pid >= 0);
+ assert(unit);
+
+ r = cg_pid_get_cgroup(pid, NULL, &cgroup);
+ if (r < 0)
+ return r;
+
+ if (!startswith(cgroup, prefix))
+ return -ENOENT;
+
+ r = cgroup_to_unit(cgroup, unit);
+ return r;
+}
+
+int cg_pid_get_unit(pid_t pid, char **unit) {
+ return cg_pid_get("/system/", pid, unit);
+}
+
+int cg_pid_get_user_unit(pid_t pid, char **unit) {
+ return cg_pid_get("/user/", pid, unit);
+}
+
+int cg_controller_from_attr(const char *attr, char **controller) {
+ const char *dot;
+ char *c;
+
+ assert(attr);
+ assert(controller);
+
+ if (!filename_is_safe(attr))
+ return -EINVAL;
+
+ dot = strchr(attr, '.');
+ if (!dot) {
+ *controller = NULL;
+ return 0;
+ }
+
+ c = strndup(attr, dot - attr);
+ if (!c)
+ return -ENOMEM;
+
+ if (!filename_is_safe(c)) {
+ free(c);
+ return -EINVAL;
+ }
+
+ *controller = c;
+ return 1;
+}