[systemd-devel] [PATCH 3/3] coredump: make use of the cleanup macros

Mirco Tischler mt-ml at gmx.de
Mon Feb 4 06:13:25 PST 2013


---
 src/journal/coredump.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)

diff --git a/src/journal/coredump.c b/src/journal/coredump.c
index 91528d3..4f79c45 100644
--- a/src/journal/coredump.c
+++ b/src/journal/coredump.c
@@ -32,6 +32,7 @@
 
 #include "log.h"
 #include "util.h"
+#include "macro.h"
 #include "mkdir.h"
 #include "special.h"
 #include "cgroup-util.h"
@@ -49,7 +50,7 @@ enum {
 };
 
 static int divert_coredump(void) {
-        FILE *f;
+        _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/.");
@@ -94,21 +95,20 @@ static int divert_coredump(void) {
         }
 
 finish:
-        fclose(f);
         return r;
 }
 
 int main(int argc, char* argv[]) {
         int r, j = 0;
-        char *p = NULL;
+        _cleanup_free_ char *p = NULL;
         ssize_t n;
         pid_t pid;
         uid_t uid;
         gid_t gid;
         struct iovec iovec[14];
-        char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
+        _cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
                 *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
-                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t;
+                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *t = NULL;
 
         prctl(PR_SET_DUMPABLE, 0);
 
@@ -143,11 +143,8 @@ int main(int argc, char* argv[]) {
                 }
 
                 core_unit = strappend("COREDUMP_UNIT=", t);
-                free(t);
-        } else if (cg_pid_get_user_unit(pid, &t) >= 0) {
+        } else if (cg_pid_get_user_unit(pid, &t) >= 0)
                 core_unit = strappend("COREDUMP_USER_UNIT=", t);
-                free(t);
-        }
 
         if (core_unit)
                 IOVEC_SET_STRING(iovec[j++], core_unit);
@@ -264,18 +261,5 @@ int main(int argc, char* argv[]) {
                 log_error("Failed to send coredump: %s", strerror(-r));
 
 finish:
-        free(p);
-        free(core_pid);
-        free(core_uid);
-        free(core_gid);
-        free(core_signal);
-        free(core_timestamp);
-        free(core_comm);
-        free(core_exe);
-        free(core_cmdline);
-        free(core_unit);
-        free(core_session);
-        free(core_message);
-
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
-- 
1.8.1.2



More information about the systemd-devel mailing list