chiark / gitweb /
journal,shared: add _cleanup_journal_close_
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 18 Mar 2013 03:36:25 +0000 (23:36 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 18 Mar 2013 23:49:30 +0000 (19:49 -0400)
src/journal/coredumpctl.c
src/journal/journalctl.c
src/journal/journald-server.c
src/journal/test-journal-enum.c
src/journal/test-journal-match.c
src/journal/test-journal-stream.c
src/shared/logs-show.c
src/shared/macro.h
src/shared/util.h

index 8bfab0075f5438ee7ad79d080203455f253b4fc3..99ca26932799d41ab54d389d7e4d22045bcd0d8d 100644 (file)
@@ -34,6 +34,7 @@
 #include "log.h"
 #include "path-util.h"
 #include "pager.h"
+#include "macro.h"
 
 static enum {
         ACTION_NONE,
@@ -42,7 +43,6 @@ static enum {
         ACTION_GDB,
 } arg_action = ACTION_LIST;
 
-static Set *matches = NULL;
 static FILE* output = NULL;
 static char* field = NULL;
 
@@ -139,7 +139,7 @@ fail:
         return r;
 }
 
-static int parse_argv(int argc, char *argv[]) {
+static int parse_argv(int argc, char *argv[], Set *matches) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_NO_PAGER,
@@ -519,10 +519,11 @@ finish:
 }
 
 int main(int argc, char *argv[]) {
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         const char* match;
         Iterator it;
         int r = 0;
+        Set _cleanup_set_free_free_ *matches = NULL;
 
         setlocale(LC_ALL, "");
         log_parse_environment();
@@ -534,7 +535,7 @@ int main(int argc, char *argv[]) {
                 goto end;
         }
 
-        r = parse_argv(argc, argv);
+        r = parse_argv(argc, argv, matches);
         if (r < 0)
                 goto end;
 
@@ -578,11 +579,6 @@ int main(int argc, char *argv[]) {
         }
 
 end:
-        if (j)
-                sd_journal_close(j);
-
-        set_free_free(matches);
-
         pager_close();
 
         if (output)
index 65114b2ff8a219711c56d5d1adc4ba9bf41f5d7e..a6ad055dc67b5c66873ed0d3c6452fb7fa2df1b0 100644 (file)
@@ -897,7 +897,7 @@ static int access_check(void) {
 
 int main(int argc, char *argv[]) {
         int r;
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         bool need_seek = false;
         sd_id128_t previous_boot_id;
         bool previous_boot_id_valid = false, first_line = true;
@@ -937,7 +937,7 @@ int main(int argc, char *argv[]) {
 
         r = access_check();
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         if (arg_directory)
                 r = sd_journal_open_directory(&j, arg_directory, 0);
@@ -945,7 +945,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_open(&j, arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY);
         if (r < 0) {
                 log_error("Failed to open journal: %s", strerror(-r));
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         if (arg_action == ACTION_VERIFY) {
@@ -955,8 +955,7 @@ int main(int argc, char *argv[]) {
 
         if (arg_action == ACTION_PRINT_HEADER) {
                 journal_print_header(j);
-                r = 0;
-                goto finish;
+                return EXIT_SUCCESS;
         }
 
         if (arg_action == ACTION_DISK_USAGE) {
@@ -965,33 +964,33 @@ int main(int argc, char *argv[]) {
 
                 r = sd_journal_get_usage(j, &bytes);
                 if (r < 0)
-                        goto finish;
+                        return EXIT_FAILURE;
 
-                printf("Journals take up %s on disk.\n", format_bytes(sbytes, sizeof(sbytes), bytes));
-                r = 0;
-                goto finish;
+                printf("Journals take up %s on disk.\n",
+                       format_bytes(sbytes, sizeof(sbytes), bytes));
+                return EXIT_SUCCESS;
         }
 
         r = add_this_boot(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_unit(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_matches(j, argv + optind);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         r = add_priorities(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         /* Opening the fd now means the first sd_journal_wait() will actually wait */
         r = sd_journal_get_fd(j);
         if (r < 0)
-                goto finish;
+                return EXIT_FAILURE;
 
         if (arg_field) {
                 const void *data;
@@ -1000,7 +999,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_query_unique(j, arg_field);
                 if (r < 0) {
                         log_error("Failed to query unique data objects: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
@@ -1018,15 +1017,14 @@ int main(int argc, char *argv[]) {
                         n_shown ++;
                 }
 
-                r = 0;
-                goto finish;
+                return EXIT_SUCCESS;
         }
 
         if (arg_cursor) {
                 r = sd_journal_seek_cursor(j, arg_cursor);
                 if (r < 0) {
                         log_error("Failed to seek to cursor: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 if (!arg_reverse)
                         r = sd_journal_next(j);
@@ -1037,7 +1035,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_realtime_usec(j, arg_since);
                 if (r < 0) {
                         log_error("Failed to seek to date: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 r = sd_journal_next(j);
 
@@ -1045,7 +1043,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_realtime_usec(j, arg_until);
                 if (r < 0) {
                         log_error("Failed to seek to date: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
                 r = sd_journal_previous(j);
 
@@ -1053,7 +1051,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_tail(j);
                 if (r < 0) {
                         log_error("Failed to seek to tail: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_previous_skip(j, arg_lines);
@@ -1062,7 +1060,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_tail(j);
                 if (r < 0) {
                         log_error("Failed to seek to tail: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_previous(j);
@@ -1071,7 +1069,7 @@ int main(int argc, char *argv[]) {
                 r = sd_journal_seek_head(j);
                 if (r < 0) {
                         log_error("Failed to seek to head: %s", strerror(-r));
-                        goto finish;
+                        return EXIT_FAILURE;
                 }
 
                 r = sd_journal_next(j);
@@ -1079,7 +1077,7 @@ int main(int argc, char *argv[]) {
 
         if (r < 0) {
                 log_error("Failed to iterate through journal: %s", strerror(-r));
-                goto finish;
+                return EXIT_FAILURE;
         }
 
         if (!arg_no_pager && !arg_follow)
@@ -1189,9 +1187,6 @@ int main(int argc, char *argv[]) {
         }
 
 finish:
-        if (j)
-                sd_journal_close(j);
-
         pager_close();
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
index 364ab0f113f615bfc137a4723947ca67d710123c..855430a6ba285494e734ac9072b6f51a57bcd78a 100644 (file)
@@ -960,8 +960,7 @@ finish:
         if (r >= 0)
                 rm_rf("/run/log/journal", false, true, false);
 
-        if (j)
-                sd_journal_close(j);
+        sd_journal_close(j);
 
         return r;
 }
index 8a843ecdda7dfcdb4cfd141362e37feab47436f8..88f583e6c7f4696e74b3fc52422e72b82823d72c 100644 (file)
 
 #include "log.h"
 #include "sd-journal.h"
+#include "macro.h"
+#include "util.h"
 
 int main(int argc, char *argv[]) {
         unsigned n = 0;
-        sd_journal *j;
+        sd_journal _cleanup_journal_close_ *j = NULL;
 
         log_set_max_level(LOG_DEBUG);
 
@@ -48,6 +50,5 @@ int main(int argc, char *argv[]) {
                         break;
         }
 
-        sd_journal_close(j);
         return 0;
 }
index fa228144f5ead7da9b75de5cd835f8c906db01d9..2ca2337c44a3f9fd480439967e3bf68737521738 100644 (file)
@@ -28,8 +28,8 @@
 #include "log.h"
 
 int main(int argc, char *argv[]) {
-        sd_journal *j;
-        char *t;
+        sd_journal _cleanup_journal_close_ *j;
+        char _cleanup_free_ *t;
 
         log_set_max_level(LOG_DEBUG);
 
@@ -59,9 +59,6 @@ int main(int argc, char *argv[]) {
         assert_se(streq(t, "((TWO=two AND (ONE=two OR ONE=one)) OR (PIFF=paff AND (QUUX=yyyyy OR QUUX=xxxxx OR QUUX=mmmm) AND (HALLO= OR HALLO=WALDO)))"));
 
         printf("resulting match expression is: %s\n", t);
-        free(t);
-
-        sd_journal_close(j);
 
         return 0;
 }
index b3e816db7081bac9544dedcd6c639172dfa8eb70..4b73ac79069da55c221958649c2048a812aa5628 100644 (file)
@@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
         JournalFile *one, *two, *three;
         char t[] = "/tmp/journal-stream-XXXXXX";
         unsigned i;
-        sd_journal *j;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         char *z;
         const void *data;
         size_t l;
@@ -126,25 +126,23 @@ int main(int argc, char *argv[]) {
 
         assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
         SD_JOURNAL_FOREACH_BACKWARDS(j) {
-                char *c;
+                char _cleanup_free_ *c;
 
                 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
                 printf("\t%.*s\n", (int) l, (const char*) data);
 
                 assert_se(sd_journal_get_cursor(j, &c) >= 0);
                 assert_se(sd_journal_test_cursor(j, c) > 0);
-                free(c);
         }
 
         SD_JOURNAL_FOREACH(j) {
-                char *c;
+                char _cleanup_free_ *c;
 
                 assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0);
                 printf("\t%.*s\n", (int) l, (const char*) data);
 
                 assert_se(sd_journal_get_cursor(j, &c) >= 0);
                 assert_se(sd_journal_test_cursor(j, c) > 0);
-                free(c);
         }
 
         sd_journal_flush_matches(j);
@@ -177,8 +175,6 @@ int main(int argc, char *argv[]) {
         SD_JOURNAL_FOREACH_UNIQUE(j, data, l)
                 printf("%.*s\n", (int) l, (const char*) data);
 
-        sd_journal_close(j);
-
         assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
 
         return 0;
index 43386841bafba8b95360969e0ac100f57a1104d8..8897a10c2b427750742667a06247629c3bb8bce5 100644 (file)
@@ -941,7 +941,7 @@ int show_journal_by_unit(
                 OutputFlags flags,
                 bool system) {
 
-        sd_journal *j = NULL;
+        sd_journal _cleanup_journal_close_ *j = NULL;
         int r;
         int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM_ONLY;
 
@@ -954,24 +954,20 @@ int show_journal_by_unit(
 
         r = sd_journal_open(&j, jflags);
         if (r < 0)
-                goto finish;
+                return r;
 
         if (system)
                 r = add_matches_for_unit(j, unit);
         else
                 r = add_matches_for_user_unit(j, unit, uid);
         if (r < 0)
-                goto finish;
+                return r;
 
         r = show_journal(f, j, mode, n_columns, not_before, how_many, flags);
         if (r < 0)
-                goto finish;
-
-finish:
-        if (j)
-                sd_journal_close(j);
+                return r;
 
-        return r;
+        return 0;
 }
 
 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
index 055919048c90d8c203baf1ae1d8913d2c002714a..90a663b9cee5bcd4639a79aeca83e1eb358a858c 100644 (file)
@@ -203,6 +203,7 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
 #define _cleanup_set_free_ __attribute__((cleanup(set_freep)))
 #define _cleanup_set_free_free_ __attribute__((cleanup(set_free_freep)))
 #define _cleanup_strv_free_ __attribute__((cleanup(strv_freep)))
+#define _cleanup_journal_close_ __attribute__((cleanup(journal_closep)))
 
 #define VA_FORMAT_ADVANCE(format, ap)                                   \
 do {                                                                    \
index 8ac4bbc2499f0cc4ccec3233a4b0aa1d805a9ef3..4be0b617731ea7569b9343ac870df7c03bbc0f62 100644 (file)
@@ -36,6 +36,7 @@
 #include <dirent.h>
 #include <sys/resource.h>
 #include <stddef.h>
+#include <systemd/sd-journal.h>
 
 #include "macro.h"
 #include "time-util.h"
@@ -531,6 +532,10 @@ static inline void umaskp(mode_t *u) {
         umask(*u);
 }
 
+static inline void journal_closep(sd_journal **j) {
+        sd_journal_close(*j);
+}
+
 _malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;