chiark / gitweb /
journal: introduce sd_journal_wait() to simplify writing synchronous clients
authorLennart Poettering <lennart@poettering.net>
Tue, 10 Jul 2012 19:46:11 +0000 (21:46 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 10 Jul 2012 19:46:11 +0000 (21:46 +0200)
src/journal/journalctl.c
src/journal/libsystemd-journal.sym
src/journal/sd-journal.c
src/shared/logs-show.c
src/systemd/sd-journal.h

index 6929b7601867302d466abf960f3324eb4acb5016..7d8b8e51c82f7beb0316c0e745e2ffbb643a199f 100644 (file)
@@ -210,7 +210,7 @@ static int generate_new_id128(void) {
 }
 
 int main(int argc, char *argv[]) {
-        int r, i, fd;
+        int r, i;
         sd_journal *j = NULL;
         unsigned line = 0;
         bool need_seek = false;
@@ -301,12 +301,6 @@ int main(int argc, char *argv[]) {
                 }
         }
 
-        fd = sd_journal_get_fd(j);
-        if (fd < 0) {
-                log_error("Failed to get wakeup fd: %s", strerror(-fd));
-                goto finish;
-        }
-
         if (!arg_quiet) {
                 usec_t start, end;
                 char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX];
@@ -385,15 +379,9 @@ int main(int argc, char *argv[]) {
                 if (!arg_follow)
                         break;
 
-                r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
-                if (r < 0) {
-                        log_error("Couldn't wait for event: %s", strerror(-r));
-                        goto finish;
-                }
-
-                r = sd_journal_process(j);
+                r = sd_journal_wait(j, (uint64_t) -1);
                 if (r < 0) {
-                        log_error("Failed to process: %s", strerror(-r));
+                        log_error("Couldn't wait for log event: %s", strerror(-r));
                         goto finish;
                 }
         }
index d291084262318c1bffc9c0ccf2429d1172e254dd..d1ba9e8e31cbd5e054491bbbf07deecb5d433e80 100644 (file)
@@ -57,3 +57,8 @@ global:
         sd_journal_get_cutoff_realtime_usec;
         sd_journal_get_cutoff_monotonic_usec;
 } LIBSYSTEMD_JOURNAL_183;
+
+LIBSYSTEMD_JOURNAL_187 {
+global:
+        sd_journal_wait;
+} LIBSYSTEMD_JOURNAL_184;
index 75884594a19346819e683748d2fb65e6e6f2288d..149dc10bdf60dd6767a7e9ab8adc9b3eb9ce82a4 100644 (file)
@@ -24,6 +24,7 @@
 #include <stddef.h>
 #include <unistd.h>
 #include <sys/inotify.h>
+#include <sys/poll.h>
 
 #include "sd-journal.h"
 #include "journal-def.h"
@@ -1622,6 +1623,20 @@ _public_ int sd_journal_process(sd_journal *j) {
         }
 }
 
+_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
+        int r, k;
+
+        assert(j);
+
+        r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
+        k = sd_journal_process(j);
+
+        if (r < 0)
+                return r;
+
+        return k;
+}
+
 _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
         Iterator i;
         JournalFile *f;
index 697b5cf4b734c58e848114e0b2ca45beec2670e5..540b5a2a2cb3770416afea05745df470996c3846 100644 (file)
@@ -557,7 +557,6 @@ int show_journal_by_unit(
         char *m = NULL;
         sd_journal *j = NULL;
         int r;
-        int fd;
         unsigned line = 0;
         bool need_seek = false;
 
@@ -583,10 +582,6 @@ int show_journal_by_unit(
         if (r < 0)
                 goto finish;
 
-        fd = sd_journal_get_fd(j);
-        if (fd < 0)
-                goto finish;
-
         r = sd_journal_add_match(j, m, strlen(m));
         if (r < 0)
                 goto finish;
@@ -663,11 +658,7 @@ int show_journal_by_unit(
                 if (!follow)
                         break;
 
-                r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
-                if (r < 0)
-                        goto finish;
-
-                r = sd_journal_process(j);
+                r = sd_journal_wait(j, (usec_t) -1);
                 if (r < 0)
                         goto finish;
 
index a69aa25a685245178ef37f89852227d54be86068..ee4acffd653d279dfcc9a6702eb6579b184aa014 100644 (file)
@@ -113,6 +113,7 @@ enum {
 
 int sd_journal_get_fd(sd_journal *j);
 int sd_journal_process(sd_journal *j);
+int sd_journal_wait(sd_journal *j, uint64_t timeout_usec);
 
 #define SD_JOURNAL_FOREACH(j)                                           \
         if (sd_journal_seek_head(j) >= 0)                               \