chiark / gitweb /
util: when formatting timestamps return '0' for 0 timestamps instead of empty string
[elogind.git] / src / cgroup-util.c
index 26e8f691d3ebfd1598d676cbf44e573853c804cd..2f3fcb5981dac8a713bf5714992a285e608530bf 100644 (file)
@@ -25,6 +25,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "cgroup-util.h"
 #include "log.h"
@@ -189,7 +191,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
                 done = true;
 
                 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
-                        if (ret >= 0)
+                        if (ret >= 0 && r != -ENOENT)
                                 ret = r;
 
                         goto finish;
@@ -205,8 +207,8 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
 
                         /* If we haven't killed this process yet, kill
                          * it */
-                        if (kill(pid, sig) < 0 && errno != ESRCH) {
-                                if (ret >= 0)
+                        if (kill(pid, sig) < 0) {
+                                if (ret >= 0 && errno != ESRCH)
                                         ret = -errno;
                         } else if (ret == 0)
                                 ret = 1;
@@ -258,7 +260,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
         ret = cg_kill(controller, path, sig, ignore_self);
 
         if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
-                if (ret >= 0)
+                if (ret >= 0 && r != -ENOENT)
                         ret = r;
 
                 goto finish;
@@ -289,7 +291,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
 
         if (rem)
                 if ((r = cg_rmdir(controller, path)) < 0) {
-                        if (ret >= 0)
+                        if (ret >= 0 && r != -ENOENT)
                                 ret = r;
                 }
 
@@ -351,7 +353,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
                 done = true;
 
                 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
-                        if (ret >= 0)
+                        if (ret >= 0 && r != -ENOENT)
                                 ret = r;
 
                         goto finish;
@@ -369,7 +371,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
                                 continue;
 
                         if ((r = cg_attach(controller, to, pid)) < 0) {
-                                if (ret >= 0)
+                                if (ret >= 0 && r != -ESRCH)
                                         ret = r;
                         } else if (ret == 0)
                                 ret = 1;
@@ -417,7 +419,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t
         ret = cg_migrate(controller, from, to, ignore_self);
 
         if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
-                if (ret >= 0)
+                if (ret >= 0 && r != -ENOENT)
                         ret = r;
                 goto finish;
         }
@@ -447,7 +449,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t
 
         if (rem)
                 if ((r = cg_rmdir(controller, from)) < 0) {
-                        if (ret >= 0)
+                        if (ret >= 0 && r != -ENOENT)
                                 ret = r;
                 }
 
@@ -517,7 +519,7 @@ int cg_trim(const char *controller, const char *path, bool delete_root) {
         r = rm_rf(fs, true, delete_root);
         free(fs);
 
-        return r;
+        return r == -ENOENT ? 0 : r;
 }
 
 int cg_delete(const char *controller, const char *path) {
@@ -533,7 +535,7 @@ int cg_delete(const char *controller, const char *path) {
         r = cg_migrate_recursive(controller, path, parent, false, true);
         free(parent);
 
-        return r;
+        return r == -ENOENT ? 0 : r;
 }
 
 int cg_create(const char *controller, const char *path) {
@@ -546,7 +548,17 @@ int cg_create(const char *controller, const char *path) {
         if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
                 return r;
 
-        r = mkdir_p(fs, 0755);
+        r = mkdir_parents(fs, 0755);
+
+        if (r >= 0) {
+                if (mkdir(fs, 0755) >= 0)
+                        r = 1;
+                else if (errno == EEXIST)
+                        r = 0;
+                else
+                        r = -errno;
+        }
+
         free(fs);
 
         return r;
@@ -577,7 +589,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
 }
 
 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
-        int r;
+        int r, q;
 
         assert(controller);
         assert(path);
@@ -586,8 +598,8 @@ int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
         if ((r = cg_create(controller, path)) < 0)
                 return r;
 
-        if ((r = cg_attach(controller, path, pid)) < 0)
-                return r;
+        if ((q = cg_attach(controller, path, pid)) < 0)
+                return q;
 
         /* This does not remove the cgroup on failure */
 
@@ -646,6 +658,9 @@ int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
         f = fopen(fs, "re");
         free(fs);
 
+        if (!f)
+                return errno == ENOENT ? -ESRCH : -errno;
+
         cs = strlen(controller);
 
         while (!feof(f)) {
@@ -763,7 +778,7 @@ int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
         assert(path);
 
         if ((r = cg_enumerate_tasks(controller, path, &f)) < 0)
-                return r;
+                return r == -ENOENT ? 1 : r;
 
         while ((r = cg_read_pid(f, &pid)) > 0) {
 
@@ -794,7 +809,7 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_
                 return r;
 
         if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0)
-                return r;
+                return r == -ENOENT ? 1 : r;
 
         while ((r = cg_read_subgroup(d, &fn)) > 0) {
                 char *p = NULL;