]> git.proxmox.com Git - proxmox-mini-journalreader.git/blobdiff - src/mini-journalreader.c
correctly check write return value
[proxmox-mini-journalreader.git] / src / mini-journalreader.c
index 850fe799da9f21da842a87a86dcd17ef48197f9d..508de0260ec263119e9cfecb62a949c063b7b471 100644 (file)
@@ -29,9 +29,9 @@
 #include <time.h>
 #include <unistd.h>
 
-#define BUFSIZE 4096
+#define BUFSIZE 4095
 
-static char buf[BUFSIZE+1];
+static char buf[BUFSIZE + 1];
 static size_t offset = 0;
 
 uint64_t get_timestamp(sd_journal *j) {
@@ -39,7 +39,7 @@ uint64_t get_timestamp(sd_journal *j) {
     int r = sd_journal_get_realtime_usec(j, &timestamp);
     if (r < 0) {
         fprintf(stderr, "Failed  %s\n", strerror(-r));
-        return 0xFFFFFFFFFFFFFFFF;
+        return -1;
     }
     return timestamp;
 }
@@ -51,13 +51,16 @@ void print_to_buf(const char * string, uint32_t length) {
     size_t string_offset = 0;
     size_t remaining = length;
     while (offset + remaining > BUFSIZE) {
-        strncpy(buf+offset, string+string_offset, BUFSIZE-offset);
-        string_offset += BUFSIZE-offset;
+        strncpy(buf + offset, string + string_offset, BUFSIZE - offset);
+        string_offset += BUFSIZE - offset;
         remaining = length - string_offset;
-        write (1, buf, BUFSIZE);
+        if (write (1, buf, BUFSIZE) <= 0) {
+            perror("write to stdout failed");
+            exit(1);
+        }
         offset = 0;
     }
-    strncpy(buf+offset, string+string_offset, remaining);
+    strncpy(buf + offset, string + string_offset, remaining);
     offset += remaining;
 }
 
@@ -132,7 +135,7 @@ void print_pid(sd_journal *j) {
     size_t l;
     int r = sd_journal_get_data(j, "_PID", (const void **)&d, &l);
     if (r < 0) {
-        // we sometimes have no pid
+        // we sometimes have no pid, e.g., kernel messages
         return;
     }
 
@@ -178,19 +181,27 @@ void print_line(sd_journal *j) {
     print_to_buf("\n", 1);
 }
 
-void usage(char *progname) {
+char *progname;
+
+void usage(char *error) {
+    if (error) {
+        fprintf(stderr, "ERROR: %s\n", error);
+    }
     fprintf(stderr, "usage: %s [OPTIONS]\n", progname);
-    fprintf(stderr, "  -b begin\tbegin at this epoch\n");
-    fprintf(stderr, "  -e end\tend at this epoch\n");
-    fprintf(stderr, "  -d directory\tpath to journal directory\n");
-    fprintf(stderr, "  -n number\tprint the last number entries\n");
-    fprintf(stderr, "  -f from\tprint from this cursor\n");
-    fprintf(stderr, "  -t to\tprint to this cursor\n");
-    fprintf(stderr, "  -h \t\tthis help\n");
-    fprintf(stderr, "\n");
-    fprintf(stderr, "giving a range conflicts with -n\n");
-    fprintf(stderr, "-b and -f conflict\n");
-    fprintf(stderr, "-e and -t conflict\n");
+    fprintf(stderr,
+        "  -b <timestamp>\tbegin at this UNIX epoch based timestamp\n"
+        "  -e <timestamp>\tend at this UNIX epoch based timestamp\n"
+        "  -d <directory>\tpath to a journal directory\n"
+        "  -n <integer>\t\tprint the last number entries logged\n"
+        "  -f <cursor>\t\tprint from this cursor\n"
+        "  -t <cursor>\t\tprint to this cursor\n"
+        "  -h \t\t\tthis help\n"
+        "\n"
+        "Passing no range option will dump all the available journal\n"
+        "Giving a range conflicts with -n\n"
+        "-b and -f conflict\n"
+        "-e and -t conflict\n");
+    exit(error ? 1 : 0);
 }
 
 static uint64_t arg_to_uint64(const char *argument) {
@@ -215,6 +226,8 @@ int main(int argc, char *argv[]) {
     uint64_t end = 0;
     char c;
 
+    progname = argv[0];
+
     while ((c = getopt (argc, argv, "b:e:d:n:f:t:h")) != -1) {
         switch (c) {
             case 'b':
@@ -238,34 +251,28 @@ int main(int argc, char *argv[]) {
                 endcursor = optarg;
                 break;
             case 'h':
-                usage(argv[0]);
-                exit(0);
+                usage(NULL);
                 break;
             case '?':
             default:
-                usage(argv[0]);
-                exit(1);
+                usage("invalid option or missing argument");
         }
     }
 
     if (number && (begin || startcursor)) {
-        usage(argv[0]);
-        exit(1);
+        usage("-n conflicts with -b and/or -f");
     }
 
     if (begin && startcursor) {
-        usage(argv[0]);
-        exit(1);
+        usage("-b and -f conflict");
     }
 
     if (end && endcursor) {
-        usage(argv[0]);
-        exit(1);
+        usage("-e and -t conflict");
     }
 
     if (argc > optind) {
-        usage(argv[0]);
-        exit(1);
+        usage("unkown, or to many arguments");
     }
 
     // to prevent calling it everytime we generate a timestamp
@@ -347,10 +354,13 @@ int main(int argc, char *argv[]) {
 
     // print final cursor
     print_cursor(j);
+    sd_journal_close(j);
 
     // print remaining buffer
-    write(1, buf, offset);
-    sd_journal_close(j);
+    if (write (1, buf, offset) <= 0) {
+        perror("write to stdout failed");
+        return 1;
+    }
 
     return 0;
 }