9fc0f6
From 735583e63b9c109e3aaa7aecbce8d7a477988ef6 Mon Sep 17 00:00:00 2001
9fc0f6
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
9fc0f6
Date: Fri, 11 Oct 2013 19:33:48 -0400
9fc0f6
Subject: [PATCH] Introduce _cleanup_fdset_free_
9fc0f6
9fc0f6
(cherry picked from commit 51d122af23533b0b8318911c4fc8b128ad8eafb7)
9fc0f6
9fc0f6
Related: #1098310
9fc0f6
---
9fc0f6
 src/core/manager.c  | 22 +++++++---------------
9fc0f6
 src/nspawn/nspawn.c |  4 +---
9fc0f6
 src/shared/fdset.h  |  7 +++++++
9fc0f6
 3 files changed, 15 insertions(+), 18 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/core/manager.c b/src/core/manager.c
9fc0f6
index 2829c95..57a88b0 100644
9fc0f6
--- a/src/core/manager.c
9fc0f6
+++ b/src/core/manager.c
9fc0f6
@@ -236,7 +236,7 @@ static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned po
9fc0f6
                 *p++ = '*';
9fc0f6
                 if (pos < width-1)
9fc0f6
                         p = mempset(p, ' ', width-1-pos);
9fc0f6
-                p = stpcpy(p, ANSI_HIGHLIGHT_OFF);
9fc0f6
+                strcpy(p, ANSI_HIGHLIGHT_OFF);
9fc0f6
         }
9fc0f6
 }
9fc0f6
 
9fc0f6
@@ -257,6 +257,7 @@ static void manager_print_jobs_in_progress(Manager *m) {
9fc0f6
         /* m->n_running_jobs must be consistent with the contents of m->jobs,
9fc0f6
          * so the above loop must have succeeded in finding j. */
9fc0f6
         assert(counter == print_nr + 1);
9fc0f6
+        assert(j);
9fc0f6
 
9fc0f6
         cylon_pos = m->jobs_in_progress_iteration % 14;
9fc0f6
         if (cylon_pos >= 8)
9fc0f6
@@ -2343,8 +2344,8 @@ int manager_distribute_fds(Manager *m, FDSet *fds) {
9fc0f6
 
9fc0f6
 int manager_reload(Manager *m) {
9fc0f6
         int r, q;
9fc0f6
-        FILE *f;
9fc0f6
-        FDSet *fds;
9fc0f6
+        _cleanup_fclose_ FILE *f = NULL;
9fc0f6
+        _cleanup_fdset_free_ FDSet *fds = NULL;
9fc0f6
 
9fc0f6
         assert(m);
9fc0f6
 
9fc0f6
@@ -2358,20 +2359,18 @@ int manager_reload(Manager *m) {
9fc0f6
         fds = fdset_new();
9fc0f6
         if (!fds) {
9fc0f6
                 m->n_reloading --;
9fc0f6
-                r = -ENOMEM;
9fc0f6
-                goto finish;
9fc0f6
+                return -ENOMEM;
9fc0f6
         }
9fc0f6
 
9fc0f6
         r = manager_serialize(m, f, fds, false);
9fc0f6
         if (r < 0) {
9fc0f6
                 m->n_reloading --;
9fc0f6
-                goto finish;
9fc0f6
+                return r;
9fc0f6
         }
9fc0f6
 
9fc0f6
         if (fseeko(f, 0, SEEK_SET) < 0) {
9fc0f6
                 m->n_reloading --;
9fc0f6
-                r = -errno;
9fc0f6
-                goto finish;
9fc0f6
+                return -errno;
9fc0f6
         }
9fc0f6
 
9fc0f6
         /* From here on there is no way back. */
9fc0f6
@@ -2415,13 +2414,6 @@ int manager_reload(Manager *m) {
9fc0f6
 
9fc0f6
         m->send_reloading_done = true;
9fc0f6
 
9fc0f6
-finish:
9fc0f6
-        if (f)
9fc0f6
-                fclose(f);
9fc0f6
-
9fc0f6
-        if (fds)
9fc0f6
-                fdset_free(fds);
9fc0f6
-
9fc0f6
         return r;
9fc0f6
 }
9fc0f6
 
9fc0f6
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
9fc0f6
index f326f99..1cf67d0 100644
9fc0f6
--- a/src/nspawn/nspawn.c
9fc0f6
+++ b/src/nspawn/nspawn.c
9fc0f6
@@ -1305,7 +1305,7 @@ int main(int argc, char *argv[]) {
9fc0f6
         bool saved_attr_valid = false;
9fc0f6
         struct winsize ws;
9fc0f6
         int kmsg_socket_pair[2] = { -1, -1 };
9fc0f6
-        FDSet *fds = NULL;
9fc0f6
+        _cleanup_fdset_free_ FDSet *fds = NULL;
9fc0f6
 
9fc0f6
         log_parse_environment();
9fc0f6
         log_open();
9fc0f6
@@ -1813,7 +1813,5 @@ finish:
9fc0f6
         free(arg_directory);
9fc0f6
         free(arg_machine);
9fc0f6
 
9fc0f6
-        fdset_free(fds);
9fc0f6
-
9fc0f6
         return r;
9fc0f6
 }
9fc0f6
diff --git a/src/shared/fdset.h b/src/shared/fdset.h
9fc0f6
index a7bd5e2..1a26005 100644
9fc0f6
--- a/src/shared/fdset.h
9fc0f6
+++ b/src/shared/fdset.h
9fc0f6
@@ -22,6 +22,7 @@
9fc0f6
 ***/
9fc0f6
 
9fc0f6
 #include "set.h"
9fc0f6
+#include "util.h"
9fc0f6
 
9fc0f6
 typedef struct FDSet FDSet;
9fc0f6
 
9fc0f6
@@ -47,3 +48,9 @@ int fdset_iterate(FDSet *s, Iterator *i);
9fc0f6
 
9fc0f6
 #define FDSET_FOREACH(fd, fds, i) \
9fc0f6
         for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
9fc0f6
+
9fc0f6
+static inline void fdset_freep(FDSet **fds) {
9fc0f6
+        if (*fds)
9fc0f6
+                fdset_free(*fds);
9fc0f6
+}
9fc0f6
+#define _cleanup_fdset_free_ _cleanup_(fdset_freep)