chiark / gitweb /
coredump: make use of the cleanup macros
[elogind.git] / src / journal / coredump.c
index 300677bb92ff44964629bc90a9d04a53f8e620e2..021b4c665187b0998463b849bd3e3c6b029d5fa0 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "log.h"
 #include "util.h"
+#include "macro.h"
 #include "mkdir.h"
 #include "special.h"
 #include "cgroup-util.h"
@@ -49,8 +50,7 @@ enum {
 };
 
 static int divert_coredump(void) {
-        FILE *f;
-        int r;
+        _cleanup_fclose_ FILE *f = NULL;
 
         log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/.");
 
@@ -70,19 +70,16 @@ static int divert_coredump(void) {
                 if (l <= 0) {
                         if (ferror(f)) {
                                 log_error("Failed to read coredump: %m");
-                                r = -errno;
-                                goto finish;
+                                return -errno;
                         }
 
-                        r = 0;
                         break;
                 }
 
                 q = fwrite(buffer, 1, l, f);
                 if (q != l) {
                         log_error("Failed to write coredump: %m");
-                        r = -errno;
-                        goto finish;
+                        return -errno;
                 }
         }
 
@@ -90,25 +87,23 @@ static int divert_coredump(void) {
 
         if (ferror(f)) {
                 log_error("Failed to write coredump: %m");
-                r = -errno;
+                return -errno;
         }
 
-finish:
-        fclose(f);
-        return r;
+        return 0;
 }
 
 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 +138,11 @@ int main(int argc, char* argv[]) {
                 }
 
                 core_unit = strappend("COREDUMP_UNIT=", t);
-                free(t);
+        } else if (cg_pid_get_user_unit(pid, &t) >= 0)
+                core_unit = strappend("COREDUMP_USER_UNIT=", t);
 
-                if (core_unit)
-                        IOVEC_SET_STRING(iovec[j++], core_unit);
-        }
+        if (core_unit)
+                IOVEC_SET_STRING(iovec[j++], core_unit);
 
         /* OK, now we know it's not the journal, hence make use of
          * it */
@@ -205,7 +200,7 @@ int main(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[j++], core_exe);
         }
 
-        if (get_process_cmdline(pid, LINE_MAX, false, &t) >= 0) {
+        if (get_process_cmdline(pid, 0, false, &t) >= 0) {
                 core_cmdline = strappend("COREDUMP_CMDLINE=", t);
                 free(t);
 
@@ -213,14 +208,14 @@ int main(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[j++], core_cmdline);
         }
 
-        core_timestamp = join("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL);
+        core_timestamp = strjoin("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL);
         if (core_timestamp)
                 IOVEC_SET_STRING(iovec[j++], core_timestamp);
 
         IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
         IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
 
-        core_message = join("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL);
+        core_message = strjoin("MESSAGE=Process ", argv[ARG_PID], " (", argv[ARG_COMM], ") dumped core.", NULL);
         if (core_message)
                 IOVEC_SET_STRING(iovec[j++], core_message);
 
@@ -239,8 +234,7 @@ int main(int argc, char* argv[]) {
 
         p = malloc(9 + COREDUMP_MAX);
         if (!p) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }
 
@@ -262,18 +256,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;
 }