chiark / gitweb /
Fixed gawk script for git-tar target.
[elogind.git] / src / basic / cgroup-util.c
index ec42e258ab5728b07e08203482c957d534af4c8f..5dc631eee410a97a8fa6e277f20a781a5dabd414 100644 (file)
@@ -35,9 +35,9 @@
 #include "formats-util.h"
 #include "process-util.h"
 #include "path-util.h"
-#include "unit-name.h"
+// #include "unit-name.h"
 #include "fileio.h"
-#include "special.h"
+// #include "special.h"
 #include "mkdir.h"
 #include "login-util.h"
 #include "cgroup-util.h"
@@ -297,6 +297,10 @@ int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char
 
         my_pid = getpid();
 
+        log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
+                          cfrom, pfrom, cto, pto,
+                          ignore_self ? "ignoring self" : "watching self");
+
         do {
                 _cleanup_fclose_ FILE *f = NULL;
                 pid_t pid = 0;
@@ -570,7 +574,7 @@ static int controller_is_accessible(const char *controller) {
                  * the unified hierarchy. */
 
                 if (streq(controller, ELOGIND_CGROUP_CONTROLLER))
-                return 0;
+                        return 0;
 
                 if (startswith(controller, "name="))
                         return -EOPNOTSUPP;
@@ -581,8 +585,8 @@ static int controller_is_accessible(const char *controller) {
                 dn = controller_to_dirname(controller);
                 cc = strjoina("/sys/fs/cgroup/", dn);
 
-        if (laccess(cc, F_OK) < 0)
-                return -errno;
+                if (laccess(cc, F_OK) < 0)
+                        return -errno;
         }
 
         return 0;
@@ -668,8 +672,6 @@ int cg_create(const char *controller, const char *path) {
         return 1;
 }
 
-/// UNNEEDED by elogind
-#if 0
 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
         int r, q;
 
@@ -686,7 +688,6 @@ int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
         /* This does not remove the cgroup on failure */
         return r;
 }
-#endif // 0
 
 int cg_attach(const char *controller, const char *path, pid_t pid) {
         _cleanup_free_ char *fs = NULL;
@@ -814,11 +815,11 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         if (unified < 0)
                 return unified;
         if (unified == 0) {
-        if (controller) {
-                if (!cg_controller_is_valid(controller))
-                        return -EINVAL;
-        } else
-                controller = ELOGIND_CGROUP_CONTROLLER;
+                if (controller) {
+                        if (!cg_controller_is_valid(controller))
+                                return -EINVAL;
+                } else
+                        controller = ELOGIND_CGROUP_CONTROLLER;
 
                 cs = strlen(controller);
         }
@@ -843,29 +844,29 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
                                 continue;
                 } else {
                         char *l;
-                size_t k;
-                const char *word, *state;
-                bool found = false;
+                        size_t k;
+                        const char *word, *state;
+                        bool found = false;
 
-                l = strchr(line, ':');
-                if (!l)
-                        continue;
+                        l = strchr(line, ':');
+                        if (!l)
+                                continue;
 
-                l++;
-                e = strchr(l, ':');
-                if (!e)
-                        continue;
+                        l++;
+                        e = strchr(l, ':');
+                        if (!e)
+                                continue;
 
-                *e = 0;
-                FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
-                        if (k == cs && memcmp(word, controller, cs) == 0) {
-                                found = true;
-                                break;
+                        *e = 0;
+                        FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
+                                if (k == cs && memcmp(word, controller, cs) == 0) {
+                                        found = true;
+                                        break;
+                                }
                         }
-                }
 
-                if (!found)
-                        continue;
+                        if (!found)
+                                continue;
                 }
 
                 p = strdup(e + 1);
@@ -879,8 +880,6 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         return -ENODATA;
 }
 
-/// UNNEEDED by elogind
-#if 0
 int cg_install_release_agent(const char *controller, const char *agent) {
         _cleanup_free_ char *fs = NULL, *contents = NULL;
         const char *sc;
@@ -965,7 +964,6 @@ int cg_uninstall_release_agent(const char *controller) {
 
         return 0;
 }
-#endif // 0
 
 int cg_is_empty(const char *controller, const char *path) {
         _cleanup_fclose_ FILE *f = NULL;
@@ -1161,6 +1159,8 @@ int cg_mangle_path(const char *path, char **result) {
 }
 
 int cg_get_root_path(char **path) {
+/// elogind does not support systemd scopes and slices
+#if 0
         char *p, *e;
         int r;
 
@@ -1180,6 +1180,10 @@ int cg_get_root_path(char **path) {
 
         *path = p;
         return 0;
+#else
+        assert(path);
+        return cg_pid_get_path(ELOGIND_CGROUP_CONTROLLER, 1, path);
+#endif // 0
 }
 
 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
@@ -1199,10 +1203,11 @@ int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
                         return r;
 
                 root = rt;
+                log_debug_elogind("Determined root path: \"%s\"", root);
         }
 
         p = path_startswith(cgroup, root);
-        if (p && p > cgroup)
+        if (p && p[0] && (p > cgroup))
                 *shifted = p - 1;
         else
                 *shifted = cgroup;
@@ -1222,6 +1227,8 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
         if (r < 0)
                 return r;
 
+        log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
+                          raw, pid, root ? root : "NULL");
         r = cg_shift_path(raw, root, &c);
         if (r < 0)
                 return r;
@@ -1238,10 +1245,13 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
 
                 *cgroup = n;
         }
+        log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
 
         return 0;
 }
 
+/// UNNEEDED by elogind
+#if 0
 int cg_path_decode_unit(const char *cgroup, char **unit){
         char *c, *s;
         size_t n;
@@ -1490,8 +1500,12 @@ int cg_pid_get_machine_name(pid_t pid, char **machine) {
 
         return cg_path_get_machine_name(cgroup, machine);
 }
+#endif // 0
 
 int cg_path_get_session(const char *path, char **session) {
+        /* Elogind uses a flat hierarchy, just "/SESSION".  The only
+           wrinkle is that SESSION might be escaped.  */
+#if 0
         _cleanup_free_ char *unit = NULL;
         char *start, *end;
         int r;
@@ -1512,10 +1526,29 @@ int cg_path_get_session(const char *path, char **session) {
         *end = 0;
         if (!session_id_valid(start))
                 return -ENXIO;
+#else
+        const char *e, *n, *start;
+
+        assert(path);
+        log_debug_elogind("path is \"%s\"", path);
+        assert(path[0] == '/');
+
+        e = path + 1;
+        n = strchrnul(e, '/');
+        if (e == n)
+                return -ENOENT;
+
+        start = strndupa(e, n - e);
+        start = cg_unescape(start);
+
+        if (!start[0])
+                return -ENOENT;
+#endif // 0
 
         if (session) {
                 char *rr;
 
+                log_debug_elogind("found session: \"%s\"", start);
                 rr = strdup(start);
                 if (!rr)
                         return -ENOMEM;
@@ -1537,6 +1570,8 @@ int cg_pid_get_session(pid_t pid, char **session) {
         return cg_path_get_session(cgroup, session);
 }
 
+/// UNNEEDED by elogind
+#if 0
 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
         _cleanup_free_ char *slice = NULL;
         char *start, *end;
@@ -1648,6 +1683,7 @@ int cg_pid_get_user_slice(pid_t pid, char **slice) {
 
         return cg_path_get_user_slice(cgroup, slice);
 }
+#endif // 0
 
 char *cg_escape(const char *p) {
         bool need_prefix = false;
@@ -1810,6 +1846,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
 
         return 0;
 }
+#endif // 0
 
 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
         _cleanup_free_ char *p = NULL;
@@ -1822,6 +1859,8 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri
         return write_string_file_no_create(p, value);
 }
 
+/// UNNEEDED by elogind
+#if 0
 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
         _cleanup_free_ char *p = NULL;
         int r;
@@ -1832,10 +1871,7 @@ int cg_get_attribute(const char *controller, const char *path, const char *attri
 
         return read_one_line_file(p, ret);
 }
-#endif // 0
 
-/// UNNEEDED by elogind
-#if 0
 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
         CGroupController c;
         int r, unified;
@@ -1871,7 +1907,6 @@ int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path
 
         return 0;
 }
-#endif // 0
 
 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
         CGroupController c;
@@ -1906,8 +1941,6 @@ int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_m
         return 0;
 }
 
-/// UNNEEDED by elogind
-#if 0
 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
         Iterator i;
         void *pidp;
@@ -1985,6 +2018,7 @@ int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root)
 
         return 0;
 }
+#endif // 0
 
 int cg_mask_supported(CGroupMask *ret) {
         CGroupMask mask = 0;
@@ -2058,6 +2092,8 @@ int cg_mask_supported(CGroupMask *ret) {
         return 0;
 }
 
+/// UNNEEDED by elogind
+#if 0
 int cg_kernel_controllers(Set *controllers) {
         _cleanup_fclose_ FILE *f = NULL;
         char buf[LINE_MAX];
@@ -2131,9 +2167,18 @@ int cg_unified(void) {
         if (statfs("/sys/fs/cgroup/", &fs) < 0)
                 return -errno;
 
+/// elogind can not support the unified hierarchy as a controller,
+/// so always assume a classical hierarchy.
+/// If, ond only *if*, someone really wants to substitute systemd-login
+/// in an environment managed by systemd with elogin, we might have to
+/// add such a support.
+#if 0
         if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
                 unified_cache = true;
         else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
+#else
+        if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
+#endif // 0
                 unified_cache = false;
         else
                 return -ENOEXEC;
@@ -2141,6 +2186,8 @@ int cg_unified(void) {
         return unified_cache;
 }
 
+/// UNNEEDED by elogind
+#if 0
 void cg_unified_flush(void) {
         unified_cache = -1;
 }
@@ -2223,6 +2270,7 @@ bool cg_is_unified_wanted(void) {
 bool cg_is_legacy_wanted(void) {
         return !cg_is_unified_wanted();
 }
+#endif // 0
 
 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
         [CGROUP_CONTROLLER_CPU] = "cpu",