[systemd-devel] [PATCH] Add avoid_cleanup macro to cancel _cleanup_ of a pointer

Josh Triplett josh at joshtriplett.org
Sat Mar 8 20:33:19 PST 2014


avoid_cleanup also returns a copy of the pointer, making it convenient
to use at the point where initialization completes, to hand the constructed
object off somewhere without freeing it.

Change all NULL assignments tagged with /* avoid cleanup */ to use this
instead.
---

Seems like a common pattern, and this makes it more self-documenting.
In particular, the use in systemctl.c's list_timers function now feels
like a single logical operation of "hand ownership of this object off to
something else and don't clean it up".

 src/shared/macro.h        | 7 +++++++
 src/systemctl/systemctl.c | 8 +++-----
 src/timedate/timedated.c  | 5 +----
 src/tmpfiles/tmpfiles.c   | 2 +-
 src/udev/udevadm-hwdb.c   | 2 +-
 5 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/shared/macro.h b/src/shared/macro.h
index 08a036b..1e521bc 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -337,6 +337,13 @@ do {                                                                    \
                 _found;                                                 \
         })
 
+#define avoid_cleanup(p)                                                \
+        ({                                                              \
+                typeof(p) _p = (p);                                     \
+                (p) = NULL;                                             \
+                _p;                                                     \
+        })
+
 /* Define C11 thread_local attribute even on older gcc compiler
  * version */
 #ifndef thread_local
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index f395265..d7983d1 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -749,8 +749,8 @@ static int list_sockets(sd_bus *bus, char **args) {
 
                 /* from this point on we will cleanup those socket_infos */
                 cs += c;
-                free(listening);
-                listening = triggered = NULL; /* avoid cleanup */
+                free(avoid_cleanup(listening));
+                avoid_cleanup(triggered);
         }
 
         qsort_safe(socket_infos, cs, sizeof(struct socket_info),
@@ -979,10 +979,8 @@ static int list_timers(sd_bus *bus, char **args) {
                 timer_infos[c++] = (struct timer_info) {
                         .id = u->id,
                         .next_elapse = m,
-                        .triggered = triggered,
+                        .triggered = avoid_cleanup(triggered),
                 };
-
-                triggered = NULL; /* avoid cleanup */
         }
 
         qsort_safe(timer_infos, c, sizeof(struct timer_info),
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index d85ce57..0494d53 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -281,10 +281,7 @@ static char** get_ntp_services(void) {
                 }
         }
 
-        i = r;
-        r = NULL; /* avoid cleanup */
-
-        return strv_uniq(i);
+        return strv_uniq(avoid_cleanup(r));
 }
 
 static int context_read_ntp(Context *c, sd_bus *bus) {
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 6e36dc7..45f79e2 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -1261,7 +1261,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 return r;
         }
 
-        i = NULL; /* avoid cleanup */
+        avoid_cleanup(i);
 
         return 0;
 }
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index 65cbf61..7b6e0af 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -219,7 +219,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         if (err)
                                 return err;
 
-                        new_child = NULL; /* avoid cleanup */
+                        avoid_cleanup(new_child);
                         break;
                 }
                 i += p;
-- 
1.9.0



More information about the systemd-devel mailing list