s6-fdholder-list.c (1814B)
1 /* ISC license. */ 2 3 #include <string.h> 4 #include <skalibs/types.h> 5 #include <skalibs/buffer.h> 6 #include <skalibs/strerr.h> 7 #include <skalibs/sgetopt.h> 8 #include <skalibs/tai.h> 9 #include <skalibs/stralloc.h> 10 #include <skalibs/skamisc.h> 11 #include <s6/fdholder.h> 12 13 #define USAGE "s6-fdholder-list [ -t timeout ] socket" 14 #define dieusage() strerr_dieusage(100, USAGE) 15 16 int main (int argc, char const *const *argv) 17 { 18 s6_fdholder_t a = S6_FDHOLDER_ZERO ; 19 stralloc sa = STRALLOC_ZERO, sb = STRALLOC_ZERO ; 20 size_t pos = 0 ; 21 int n ; 22 tain deadline ; 23 PROG = "s6-fdholder-list" ; 24 { 25 unsigned int t = 0 ; 26 subgetopt l = SUBGETOPT_ZERO ; 27 for (;;) 28 { 29 int opt = subgetopt_r(argc, argv, "t:", &l) ; 30 if (opt == -1) break ; 31 switch (opt) 32 { 33 case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; 34 default : dieusage() ; 35 } 36 } 37 argc -= l.ind ; argv += l.ind ; 38 if (t) tain_from_millisecs(&deadline, t) ; 39 else deadline = tain_infinite_relative ; 40 } 41 42 if (!argc) dieusage() ; 43 tain_now_set_stopwatch_g() ; 44 tain_add_g(&deadline, &deadline) ; 45 if (!s6_fdholder_start_g(&a, argv[0], &deadline)) 46 strerr_diefu2sys(111, "connect to a fd-holder daemon at ", argv[0]) ; 47 n = s6_fdholder_list_g(&a, &sa, &deadline) ; 48 if (n < 0) strerr_diefu1sys(1, "get fd list") ; 49 while (n--) 50 { 51 size_t len = strlen(sa.s + pos) ; 52 sb.len = 0 ; 53 if (!string_quote_nodelim_mustquote(&sb, sa.s + pos, len, 0, 0)) 54 strerr_diefu1sys(111, "quote string") ; 55 if (buffer_put(buffer_1, sb.s, sb.len) < sb.len || buffer_put(buffer_1, "\n", 1) < 1) 56 strerr_diefu1sys(111, "buffer_put") ; 57 pos += len+1 ; 58 } 59 stralloc_free(&sb) ; 60 stralloc_free(&sa) ; 61 if (!buffer_flush(buffer_1)) strerr_diefu1sys(111, "write to stdout") ; 62 return 0 ; 63 }