s6-svdt.c (2082B)
1 /* ISC license. */ 2 3 #include <sys/types.h> 4 #include <skalibs/types.h> 5 #include <skalibs/buffer.h> 6 #include <skalibs/strerr.h> 7 #include <skalibs/sgetopt.h> 8 #include <skalibs/sig.h> 9 #include <s6/supervise.h> 10 11 #define USAGE "s6-svdt [ -S | -s ] [ -n maxentries ] servicedir" 12 #define dieusage() strerr_dieusage(100, USAGE) 13 #define die1() strerr_diefu1sys(111, "write to stdout") 14 15 int main (int argc, char const *const *argv) 16 { 17 unsigned int n = S6_MAX_DEATH_TALLY ; 18 int num = 0 ; 19 PROG = "s6-svdt" ; 20 { 21 subgetopt l = SUBGETOPT_ZERO ; 22 for (;;) 23 { 24 int opt = subgetopt_r(argc, argv, "Ssn:", &l) ; 25 if (opt == -1) break ; 26 switch (opt) 27 { 28 case 'S' : num = 0 ; break ; 29 case 's' : num = 1 ; break ; 30 case 'n' : if (!uint0_scan(l.arg, &n)) dieusage() ; break ; 31 default : dieusage() ; 32 } 33 } 34 argc -= l.ind ; argv += l.ind ; 35 } 36 if (!argc) dieusage() ; 37 if (n > S6_MAX_DEATH_TALLY) n = S6_MAX_DEATH_TALLY ; 38 if (!n) return 0 ; 39 { 40 s6_dtally_t tab[n] ; 41 ssize_t r = s6_dtally_read(argv[0], tab, n) ; 42 if (r < 0) strerr_diefu2sys(111, "read death tally for service ", argv[0]) ; 43 for (size_t i = 0 ; i < r ; i++) 44 { 45 char fmt[TIMESTAMP + 1] ; 46 timestamp_fmt(fmt, &tab[i].stamp) ; 47 fmt[TIMESTAMP] = ' ' ; 48 if (buffer_put(buffer_1, fmt, TIMESTAMP + 1) < 0) die1() ; 49 if (tab[i].sig) 50 { 51 if (buffer_puts(buffer_1, "signal ") < 0) die1() ; 52 if (num) 53 { 54 char fmt[3] ; 55 if (buffer_put(buffer_1, fmt, uint_fmt(fmt, tab[i].sig)) < 0) die1() ; 56 } 57 else 58 { 59 if (buffer_puts(buffer_1, "SIG") < 0) die1() ; 60 if (buffer_puts(buffer_1, sig_name(tab[i].sig)) < 0) die1() ; 61 } 62 } 63 else 64 { 65 char fmt[3] ; 66 if (buffer_puts(buffer_1, "exitcode ") < 0) die1() ; 67 if (buffer_put(buffer_1, fmt, uint_fmt(fmt, tab[i].exitcode)) < 0) die1() ; 68 } 69 if (buffer_put(buffer_1, "\n", 1) < 0) die1() ; 70 } 71 } 72 if (!buffer_flush(buffer_1)) die1() ; 73 return 0 ; 74 }