skalibs

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

socketpair_internal.c (1171B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 #include <skalibs/nonposix.h>
      5 
      6 #include <sys/socket.h>
      7 #include <errno.h>
      8 
      9 #include <skalibs/fcntl.h>
     10 #include <skalibs/djbunix.h>
     11 #include <skalibs/socket.h>
     12 
     13 #ifdef SKALIBS_HASACCEPT4
     14 
     15 int socketpair_internal (int domain, int type, int protocol, unsigned int flags, int *sv)
     16 {
     17   return socketpair(domain, type | ((flags & O_NONBLOCK) ? SOCK_NONBLOCK : 0) | ((flags & O_CLOEXEC) ? SOCK_CLOEXEC : 0), protocol, sv) ;
     18 }
     19 
     20 #else
     21 
     22 int socketpair_internal (int domain, int type, int protocol, unsigned int flags, int *sv)
     23 {
     24   int fd[2] ;
     25   if (socketpair(domain, type, protocol, fd) < 0) return -1 ;
     26   if (flags & O_NONBLOCK)
     27   {
     28     if (ndelay_on(fd[0]) < 0) goto err ;
     29     if (ndelay_on(fd[1]) < 0) goto err ;
     30   }
     31   else
     32   {
     33     if (ndelay_off(fd[0]) < 0) goto err ;
     34     if (ndelay_off(fd[1]) < 0) goto err ;
     35   }
     36   if (flags & O_CLOEXEC)
     37   {
     38     if (coe(fd[0]) < 0) goto err ;
     39     if (coe(fd[1]) < 0) goto err ;
     40   }
     41   else
     42   {
     43     if (uncoe(fd[0]) < 0) goto err ;
     44     if (uncoe(fd[1]) < 0) goto err ;
     45   }
     46   sv[0] = fd[0] ;
     47   sv[1] = fd[1] ;
     48   return 0 ;
     49 
     50  err:
     51   fd_close(fd[1]) ;
     52   fd_close(fd[0]) ;
     53   return -1 ;
     54 }
     55 
     56 #endif