skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

ipc_accept.c (1217B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 #include <skalibs/nonposix.h>
      5 #include <sys/socket.h>
      6 #include <sys/un.h>
      7 #include <string.h>
      8 #include <errno.h>
      9 
     10 #include <skalibs/bytestr.h>
     11 #include <skalibs/fcntl.h>
     12 #include <skalibs/djbunix.h>
     13 #include <skalibs/socket.h>
     14 
     15 int ipc_accept_internal (int s, char *p, size_t l, int *trunc, unsigned int options)
     16 {
     17   struct sockaddr_un sa ;
     18   socklen_t dummy = sizeof sa ;
     19   int fd ;
     20   memset(&sa, 0, dummy) ;
     21   do
     22 #ifdef SKALIBS_HASACCEPT4
     23     fd = accept4(s, (struct sockaddr *)&sa, &dummy, ((options & O_NONBLOCK) ? SOCK_NONBLOCK : 0) | ((options & O_CLOEXEC) ? SOCK_CLOEXEC : 0)) ;
     24 #else
     25     fd = accept(s, (struct sockaddr *)&sa, &dummy) ;
     26 #endif
     27   while ((fd == -1) && (errno == EINTR)) ;
     28   if (fd == -1) return -1 ;
     29 #ifndef SKALIBS_HASACCEPT4
     30   if ((((options & O_NONBLOCK) ? ndelay_on(fd) : ndelay_off(fd)) < 0)
     31    || (((options & O_CLOEXEC) ? coe(fd) : uncoe(fd)) < 0))
     32   {
     33     fd_close(fd) ;
     34     return -1 ;
     35   }
     36 #endif
     37 
     38   if (p)
     39   {
     40     dummy = byte_chr(sa.sun_path, dummy, 0) ;
     41     *trunc = 1 ;
     42     if (!l) return fd ;
     43     if (l < (dummy + 1)) dummy = l - 1 ;
     44     else *trunc = 0 ;
     45     memcpy(p, sa.sun_path, dummy) ;
     46     p[dummy] = 0 ;
     47   }
     48   return fd ;
     49 }