s6-ipcclient.c (1971B)
1 /* ISC license. */ 2 3 #include <string.h> 4 5 #include <skalibs/sgetopt.h> 6 #include <skalibs/strerr.h> 7 #include <skalibs/djbunix.h> 8 #include <skalibs/socket.h> 9 #include <skalibs/exec.h> 10 11 #define USAGE "s6-ipcclient [ -q | -Q | -v ] [ -p bindpath ] [ -l localname ] path prog..." 12 13 int main (int argc, char const *const *argv) 14 { 15 char const *bindpath = 0 ; 16 char const *localname = 0 ; 17 unsigned int verbosity = 1 ; 18 PROG = "s6-ipcclient" ; 19 { 20 subgetopt l = SUBGETOPT_ZERO ; 21 for (;;) 22 { 23 int opt = subgetopt_r(argc, argv, "qQvp:l:", &l) ; 24 if (opt == -1) break ; 25 switch (opt) 26 { 27 case 'q' : if (verbosity) verbosity-- ; break ; 28 case 'Q' : verbosity = 1 ; break ; 29 case 'v' : verbosity++ ; break ; 30 case 'p' : bindpath = l.arg ; break ; 31 case 'l' : localname = l.arg ; break ; 32 default : strerr_dieusage(100, USAGE) ; 33 } 34 } 35 argc -= l.ind ; argv += l.ind ; 36 } 37 if (argc < 2) strerr_dieusage(100, USAGE) ; 38 { 39 char modif[24 + IPCPATH_MAX] = "PROTO=IPC\0IPCLOCALPATH=" ; 40 size_t i = 23 ; 41 int s = ipc_stream_b() ; 42 if (s < 0) strerr_diefu1sys(111, "create socket") ; 43 if (bindpath && (ipc_bind(s, bindpath) == -1)) 44 strerr_diefu2sys(111, "bind socket to ", bindpath) ; 45 if (!ipc_connect(s, argv[0])) 46 strerr_diefu2sys(111, "connect to ", argv[0]) ; 47 if (verbosity >= 2) strerr_warni3x(PROG, ": connected to ", argv[0]) ; 48 if (localname) 49 { 50 size_t n = strlen(localname) ; 51 if (n > IPCPATH_MAX) n = IPCPATH_MAX ; 52 memcpy(modif + i, localname, n) ; 53 i += n ; modif[i++] = 0 ; 54 } 55 else 56 { 57 int dummy ; 58 if (ipc_local(s, modif + i, IPCPATH_MAX, &dummy) < 0) modif[--i] = 0 ; 59 else i += strlen(modif + i) + 1 ; 60 } 61 if (fd_move(6, s) < 0) 62 strerr_diefu2sys(111, "set up fd ", "6") ; 63 if (fd_copy(7, 6) < 0) 64 strerr_diefu2sys(111, "set up fd ", "7") ; 65 xmexec_n(argv+1, modif, i, 2) ; 66 } 67 }