ccx-utils

Miscellaneous utilities written in C
git clone https://ccx.te2000.cz/git/ccx-utils
Log | Files | Refs

fdsend.c (1066B)


      1 #include <errno.h>
      2 #include <sys/select.h>
      3 
      4 #include <skalibs/types.h>
      5 #include <skalibs/strerr.h>
      6 #include <skalibs/djbunix.h>
      7 #include <skalibs/exec.h>
      8 #include <skalibs/ancil.h>
      9 
     10 #define USAGE "fdsend socket_fd fd_to_send prog..."
     11 #define PROG "fdsend"
     12 
     13 void wait_writable(int fd) {
     14   fd_set wfds, xfds;
     15   int    retval = 0;
     16   FD_ZERO(&wfds);
     17   FD_ZERO(&xfds);
     18   while(retval < 1) {
     19     FD_SET(fd, &wfds);
     20     FD_SET(fd, &xfds);
     21     retval = select(fd+1, NULL, &wfds, &xfds, NULL);
     22     if (retval == -1 && errno != EINTR) {
     23       strerr_dief1sys(111, "select()");
     24     }
     25   }
     26 }
     27 
     28 
     29 int main (int argc, char const *const *argv)
     30 {
     31   unsigned int socket_fd, fd_to_send ;
     32   if ((argc < 4)) strerr_dieusage(100, USAGE) ;
     33   if (!uint0_scan(argv[1], &socket_fd)) strerr_dieusage(100, USAGE) ;
     34   if (!uint0_scan(argv[2], &fd_to_send)) strerr_dieusage(100, USAGE) ;
     35 
     36   wait_writable(socket_fd);
     37   while(!ancil_send_fd(socket_fd, fd_to_send, 42)) {
     38     if(errno != EAGAIN) {
     39       strerr_dief1sys(111, "sendmsg()");
     40     }
     41     wait_writable(socket_fd);
     42   }
     43 
     44   xexec(argv+3) ;
     45 }