chiark / gitweb /
util: place a couple of _likely_ around cache TLS vars
[elogind.git] / src / cgroup-util.c
index ccc94f86a1fc3eb4c70da24675fca9f742f23763..fd0ac98e28faad081e1b2faa17425b592f2e9ea7 100644 (file)
@@ -166,7 +166,7 @@ int cg_rmdir(const char *controller, const char *path) {
         return r < 0 ? -errno : 0;
 }
 
         return r < 0 ? -errno : 0;
 }
 
-int cg_kill(const char *controller, const char *path, int sig, bool ignore_self, Set *s) {
+int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
         bool done = false;
         int r, ret = 0;
         pid_t my_pid;
         bool done = false;
         int r, ret = 0;
         pid_t my_pid;
@@ -188,7 +188,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self,
         my_pid = getpid();
 
         do {
         my_pid = getpid();
 
         do {
-                pid_t pid;
+                pid_t pid = 0;
                 done = true;
 
                 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
                 done = true;
 
                 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
@@ -211,8 +211,13 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self,
                         if (kill(pid, sig) < 0) {
                                 if (ret >= 0 && errno != ESRCH)
                                         ret = -errno;
                         if (kill(pid, sig) < 0) {
                                 if (ret >= 0 && errno != ESRCH)
                                         ret = -errno;
-                        } else if (ret == 0)
+                        } else if (ret == 0) {
+
+                                if (sigcont)
+                                        kill(pid, SIGCONT);
+
                                 ret = 1;
                                 ret = 1;
+                        }
 
                         done = false;
 
 
                         done = false;
 
@@ -250,7 +255,7 @@ finish:
         return ret;
 }
 
         return ret;
 }
 
-int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool rem, Set *s) {
+int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
         int r, ret = 0;
         DIR *d = NULL;
         char *fn;
         int r, ret = 0;
         DIR *d = NULL;
         char *fn;
@@ -264,7 +269,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
                 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
                         return -ENOMEM;
 
                 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
                         return -ENOMEM;
 
-        ret = cg_kill(controller, path, sig, ignore_self, s);
+        ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
 
         if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
                 if (ret >= 0 && r != -ENOENT)
 
         if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
                 if (ret >= 0 && r != -ENOENT)
@@ -286,7 +291,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
                         goto finish;
                 }
 
                         goto finish;
                 }
 
-                r = cg_kill_recursive(controller, p, sig, ignore_self, rem, s);
+                r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
                 free(p);
 
                 if (r != 0 && ret >= 0)
                 free(p);
 
                 if (r != 0 && ret >= 0)
@@ -321,24 +326,25 @@ int cg_kill_recursive_and_wait(const char *controller, const char *path, bool re
         assert(controller);
 
         /* This safely kills all processes; first it sends a SIGTERM,
         assert(controller);
 
         /* This safely kills all processes; first it sends a SIGTERM,
-         * then checks 8 times after 50ms whether the group is
-         * now empty, and finally kills everything that is left with
-         * SIGKILL */
+         * then checks 8 times after 200ms whether the group is now
+         * empty, then kills everything that is left with SIGKILL and
+         * finally checks 5 times after 200ms each whether the group
+         * is finally empty. */
 
 
-        for (i = 0; i < 10; i++) {
+        for (i = 0; i < 15; i++) {
                 int sig, r;
 
                 if (i <= 0)
                         sig = SIGTERM;
                 int sig, r;
 
                 if (i <= 0)
                         sig = SIGTERM;
-                else if (i >= 9)
+                else if (i == 9)
                         sig = SIGKILL;
                 else
                         sig = 0;
 
                         sig = SIGKILL;
                 else
                         sig = 0;
 
-                if ((r = cg_kill_recursive(controller, path, sig, true, rem, NULL)) <= 0)
+                if ((r = cg_kill_recursive(controller, path, sig, true, true, rem, NULL)) <= 0)
                         return r;
 
                         return r;
 
-                usleep(50 * USEC_PER_MSEC);
+                usleep(200 * USEC_PER_MSEC);
         }
 
         return 0;
         }
 
         return 0;
@@ -361,7 +367,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
         my_pid = getpid();
 
         do {
         my_pid = getpid();
 
         do {
-                pid_t pid;
+                pid_t pid = 0;
                 done = true;
 
                 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
                 done = true;
 
                 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
@@ -476,12 +482,26 @@ finish:
 
 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
         const char *p;
 
 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
         const char *p;
-        char *mp;
-        int r;
+        char *t;
+        static __thread bool good = false;
 
         assert(controller);
         assert(fs);
 
 
         assert(controller);
         assert(fs);
 
+        if (_unlikely_(!good)) {
+                int r;
+
+                r = path_is_mount_point("/sys/fs/cgroup");
+                if (r <= 0)
+                        return r < 0 ? r : -ENOENT;
+
+                /* Cache this to save a few stat()s */
+                good = true;
+        }
+
+        if (isempty(controller))
+                return -EINVAL;
+
         /* This is a very minimal lookup from controller names to
          * paths. Since we have mounted most hierarchies ourselves
          * should be kinda safe, but eventually we might want to
         /* This is a very minimal lookup from controller names to
          * paths. Since we have mounted most hierarchies ourselves
          * should be kinda safe, but eventually we might want to
@@ -495,29 +515,22 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
         else
                 p = controller;
 
         else
                 p = controller;
 
-        if (asprintf(&mp, "/sys/fs/cgroup/%s", p) < 0)
-                return -ENOMEM;
-
-        if ((r = path_is_mount_point(mp)) <= 0) {
-                free(mp);
-                return r < 0 ? r : -ENOENT;
-        }
-
         if (path && suffix)
         if (path && suffix)
-                r = asprintf(fs, "%s/%s/%s", mp, path, suffix);
+                t = join("/sys/fs/cgroup/", p, "/", path, "/", suffix, NULL);
         else if (path)
         else if (path)
-                r = asprintf(fs, "%s/%s", mp, path);
+                t = join("/sys/fs/cgroup/", p, "/", path, NULL);
         else if (suffix)
         else if (suffix)
-                r = asprintf(fs, "%s/%s", mp, suffix);
-        else {
-                path_kill_slashes(mp);
-                *fs = mp;
-                return 0;
-        }
+                t = join("/sys/fs/cgroup/", p, "/", suffix, NULL);
+        else
+                t = join("/sys/fs/cgroup/", p, NULL);
 
 
-        free(mp);
-        path_kill_slashes(*fs);
-        return r < 0 ? -ENOMEM : 0;
+        if (!t)
+                return -ENOMEM;
+
+        path_kill_slashes(t);
+
+        *fs = t;
+        return 0;
 }
 
 int cg_trim(const char *controller, const char *path, bool delete_root) {
 }
 
 int cg_trim(const char *controller, const char *path, bool delete_root) {
@@ -781,9 +794,9 @@ finish:
 }
 
 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
 }
 
 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
-        pid_t pid;
+        pid_t pid = 0;
         int r;
         int r;
-        FILE *f;
+        FILE *f = NULL;
         bool found = false;
 
         assert(controller);
         bool found = false;
 
         assert(controller);
@@ -961,3 +974,31 @@ int cg_fix_path(const char *path, char **result) {
 
         return r;
 }
 
         return r;
 }
+
+int cg_get_user_path(char **path) {
+        char *root, *p;
+
+        assert(path);
+
+        /* Figure out the place to put user cgroups below. We use the
+         * same as PID 1 has but with the "/system" suffix replaced by
+         * "/user" */
+
+        if (cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root) < 0)
+                p = strdup("/user");
+        else {
+                if (endswith(root, "/system"))
+                        root[strlen(root) - 7] = 0;
+                else if (streq(root, "/"))
+                        root[0] = 0;
+
+                p = strappend(root, "/user");
+                free(root);
+        }
+
+        if (!p)
+                return -ENOMEM;
+
+        *path = p;
+        return 0;
+}