s6-fdholder-retrieve.c (1546B)
1 /* ISC license. */ 2 3 #include <skalibs/types.h> 4 #include <skalibs/strerr.h> 5 #include <skalibs/sgetopt.h> 6 #include <skalibs/tai.h> 7 #include <skalibs/djbunix.h> 8 #include <skalibs/exec.h> 9 10 #include <s6/fdholder.h> 11 12 #define USAGE "s6-fdholder-retrieve [ -D ] [ -t timeout ] socket id prog..." 13 #define dieusage() strerr_dieusage(100, USAGE) 14 15 int main (int argc, char const *const *argv) 16 { 17 s6_fdholder_t a = S6_FDHOLDER_ZERO ; 18 tain deadline ; 19 int fd ; 20 int dodelete = 0 ; 21 PROG = "s6-fdholder-retrieve" ; 22 { 23 unsigned int t = 0 ; 24 subgetopt l = SUBGETOPT_ZERO ; 25 for (;;) 26 { 27 int opt = subgetopt_r(argc, argv, "Dt:", &l) ; 28 if (opt == -1) break ; 29 switch (opt) 30 { 31 case 'D' : dodelete = 1 ; break ; 32 case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; 33 default : dieusage() ; 34 } 35 } 36 argc -= l.ind ; argv += l.ind ; 37 if (t) tain_from_millisecs(&deadline, t) ; 38 else deadline = tain_infinite_relative ; 39 } 40 if (argc < 3) dieusage() ; 41 42 tain_now_set_stopwatch_g() ; 43 tain_add_g(&deadline, &deadline) ; 44 if (!s6_fdholder_start_g(&a, argv[0], &deadline)) 45 strerr_diefu2sys(111, "connect to a fd-holder daemon at ", argv[0]) ; 46 fd = s6_fdholder_retrieve_maybe_delete_g(&a, argv[1], dodelete, &deadline) ; 47 if (fd < 0) strerr_diefu2sys(1, "retrieve fd for id ", argv[1]) ; 48 s6_fdholder_end(&a) ; 49 if (!fd) 50 { 51 if (uncoe(0) < 0) strerr_diefu1sys(111, "uncoe stdin") ; 52 } 53 else if (fd_move(0, fd) < 0) strerr_diefu1sys(111, "move fd") ; 54 xexec(argv+2) ; 55 }