chiark / gitweb /
core/path: use automatic cleanup
[elogind.git] / src / core / path.c
index d6fedc736ae21aafd6dfd64b329ca5a087457155..65913f87591cb41848c08381313fb6ec905f00a5 100644 (file)
@@ -33,6 +33,7 @@
 #include "special.h"
 #include "bus-errors.h"
 #include "path-util.h"
+#include "macro.h"
 
 static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
         [PATH_DEAD] = UNIT_INACTIVE,
@@ -52,7 +53,8 @@ int path_spec_watch(PathSpec *s, Unit *u) {
         };
 
         bool exists = false;
-        char *k, *slash;
+        char _cleanup_free_ *k = NULL;
+        char *slash;
         int r;
 
         assert(u);
@@ -60,27 +62,30 @@ int path_spec_watch(PathSpec *s, Unit *u) {
 
         path_spec_unwatch(s, u);
 
-        if (!(k = strdup(s->path)))
+        k = strdup(s->path);
+        if (!k)
                 return -ENOMEM;
 
-        if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
+        s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+        if (s->inotify_fd < 0) {
                 r = -errno;
                 goto fail;
         }
 
-        if (unit_watch_fd(u, s->inotify_fd, EPOLLIN, &s->watch) < 0) {
-                r = -errno;
+        r = unit_watch_fd(u, s->inotify_fd, EPOLLIN, &s->watch);
+        if (r < 0)
                 goto fail;
-        }
 
-        if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
+        s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type]);
+        if (s->primary_wd >= 0)
                 exists = true;
 
         do {
                 int flags;
 
                 /* This assumes the path was passed through path_kill_slashes()! */
-                if (!(slash = strrchr(k, '/')))
+                slash = strrchr(k, '/');
+                if (!slash)
                         break;
 
                 /* Trim the path at the last slash. Keep the slash if it's the root dir. */
@@ -97,8 +102,6 @@ int path_spec_watch(PathSpec *s, Unit *u) {
         return 0;
 
 fail:
-        free(k);
-
         path_spec_unwatch(s, u);
         return r;
 }
@@ -115,36 +118,32 @@ void path_spec_unwatch(PathSpec *s, Unit *u) {
 }
 
 int path_spec_fd_event(PathSpec *s, uint32_t events) {
-        uint8_t *buf = NULL;
+        uint8_t _cleanup_free_ *buf = NULL;
         struct inotify_event *e;
         ssize_t k;
         int l;
         int r = 0;
 
         if (events != EPOLLIN) {
-                log_error("Got Invalid poll event on inotify.");
-                r = -EINVAL;
-                goto out;
+                log_error("Got invalid poll event on inotify.");
+                return -EINVAL;
         }
 
         if (ioctl(s->inotify_fd, FIONREAD, &l) < 0) {
                 log_error("FIONREAD failed: %m");
-                r = -errno;
-                goto out;
+                return -errno;
         }
 
         assert(l > 0);
 
-        if (!(buf = malloc(l))) {
-                log_error("Failed to allocate buffer: %m");
-                r = -errno;
-                goto out;
-        }
+        buf = malloc(l);
+        if (!buf)
+                return log_oom();
 
-        if ((k = read(s->inotify_fd, buf, l)) < 0) {
+        k = read(s->inotify_fd, buf, l);
+        if (k < 0) {
                 log_error("Failed to read inotify event: %m");
-                r = -errno;
-                goto out;
+                return -errno;
         }
 
         e = (struct inotify_event*) buf;
@@ -162,8 +161,7 @@ int path_spec_fd_event(PathSpec *s, uint32_t events) {
                 e = (struct inotify_event*) ((uint8_t*) e + step);
                 k -= step;
         }
-out:
-        free(buf);
+
         return r;
 }
 
@@ -215,7 +213,8 @@ static void path_spec_mkdir(PathSpec *s, mode_t mode) {
         if (s->type == PATH_EXISTS || s->type == PATH_EXISTS_GLOB)
                 return;
 
-        if ((r = mkdir_p(s->path, mode)) < 0)
+        r = mkdir_p_label(s->path, mode);
+        if (r < 0)
                 log_warning("mkdir(%s) failed: %s", s->path, strerror(-r));
 }
 
@@ -243,22 +242,28 @@ static void path_init(Unit *u) {
         p->directory_mode = 0755;
 }
 
-static void path_done(Unit *u) {
-        Path *p = PATH(u);
+void path_free_specs(Path *p) {
         PathSpec *s;
 
         assert(p);
 
-        unit_ref_unset(&p->unit);
-
         while ((s = p->specs)) {
-                path_spec_unwatch(s, u);
+                path_spec_unwatch(s, UNIT(p));
                 LIST_REMOVE(PathSpec, spec, p->specs, s);
                 path_spec_done(s);
                 free(s);
         }
 }
 
+static void path_done(Unit *u) {
+        Path *p = PATH(u);
+
+        assert(p);
+
+        unit_ref_unset(&p->unit);
+        path_free_specs(p);
+}
+
 int path_add_one_mount_link(Path *p, Mount *m) {
         PathSpec *s;
         int r;
@@ -302,7 +307,8 @@ static int path_verify(Path *p) {
                 return 0;
 
         if (!p->specs) {
-                log_error("%s lacks path setting. Refusing.", UNIT(p)->id);
+                log_error_unit(UNIT(p)->id,
+                               "%s lacks path setting. Refusing.", UNIT(p)->id);
                 return -EINVAL;
         }
 
@@ -314,7 +320,7 @@ static int path_add_default_dependencies(Path *p) {
 
         assert(p);
 
-        if (UNIT(p)->manager->running_as == MANAGER_SYSTEM) {
+        if (UNIT(p)->manager->running_as == SYSTEMD_SYSTEM) {
                 if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
                         return r;
 
@@ -738,7 +744,6 @@ static const char* const path_result_table[_PATH_RESULT_MAX] = {
 DEFINE_STRING_TABLE_LOOKUP(path_result, PathResult);
 
 const UnitVTable path_vtable = {
-        .suffix = ".path",
         .object_size = sizeof(Path),
         .sections =
                 "Unit\0"