s6-fdholder-transferdump.c (2228B)
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/genalloc.h> 8 #include <s6/fdholder.h> 9 10 #define USAGE "s6-fdholder-transferdump [ -t timeoutfrom:timeoutto ] socketfrom socketto" 11 #define dieusage() strerr_dieusage(100, USAGE) 12 13 int main (int argc, char const *const *argv) 14 { 15 s6_fdholder_t a = S6_FDHOLDER_ZERO ; 16 genalloc dump = GENALLOC_ZERO ; /* array of s6_fdholder_fd_t */ 17 tain deadline, totto ; 18 PROG = "s6-fdholder-transferdump" ; 19 { 20 unsigned int timeoutfrom = 0, timeoutto = 0 ; 21 subgetopt l = SUBGETOPT_ZERO ; 22 for (;;) 23 { 24 int opt = subgetopt_r(argc, argv, "t:", &l) ; 25 if (opt == -1) break ; 26 switch (opt) 27 { 28 case 't' : 29 { 30 size_t pos = uint_scan(l.arg, &timeoutfrom) ; 31 if (!pos) 32 { 33 if (l.arg[pos] != ':') dieusage() ; 34 timeoutfrom = 0 ; 35 } 36 if (!l.arg[pos]) timeoutto = 0 ; 37 else 38 { 39 if (l.arg[pos++] != ':') dieusage() ; 40 if (!l.arg[pos]) timeoutto = 0 ; 41 else if (!uint0_scan(l.arg + pos, &timeoutto)) dieusage() ; 42 } 43 break ; 44 } 45 default : dieusage() ; 46 } 47 } 48 argc -= l.ind ; argv += l.ind ; 49 if (timeoutfrom) tain_from_millisecs(&deadline, timeoutfrom) ; 50 else deadline = tain_infinite_relative ; 51 if (timeoutto) tain_from_millisecs(&totto, timeoutto) ; 52 else totto = tain_infinite_relative ; 53 } 54 if (argc < 2) dieusage() ; 55 56 tain_now_set_stopwatch_g() ; 57 tain_add_g(&deadline, &deadline) ; 58 if (!s6_fdholder_start_g(&a, argv[0], &deadline)) 59 strerr_diefu2sys(111, "connect to a source fd-holder daemon at ", argv[0]) ; 60 if (!s6_fdholder_getdump_g(&a, &dump, &deadline)) 61 strerr_diefu1sys(1, "get dump") ; 62 s6_fdholder_end(&a) ; 63 tain_add_g(&deadline, &totto) ; 64 if (!s6_fdholder_start_g(&a, argv[1], &deadline)) 65 strerr_diefu2sys(111, "connect to a destination fd-holder daemon at ", argv[1]) ; 66 if (!s6_fdholder_setdump_g(&a, genalloc_s(s6_fdholder_fd_t, &dump), genalloc_len(s6_fdholder_fd_t, &dump), &deadline)) 67 strerr_diefu1sys(1, "set dump") ; 68 return 0 ; 69 }