skalibs

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

ipc_recv.c (837B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/nonposix.h>
      4 #include <string.h>
      5 #include <errno.h>
      6 #include <sys/socket.h>
      7 #include <sys/un.h>
      8 
      9 #include <skalibs/socket.h>
     10 #include <skalibs/posixishard.h>
     11 
     12 ssize_t ipc_recv (int fd, char *s, size_t len, char *path)
     13 {
     14   struct sockaddr_un sa = { .sun_family = AF_UNIX } ;
     15   socklen_t total = sizeof sa ;
     16   char tmp[len] ;
     17   ssize_t r ;
     18   do r = recvfrom(fd, tmp, len, 0, (struct sockaddr *)&sa, &total) ;
     19   while ((r == -1) && (errno == EINTR)) ;
     20   if (r < 0) return r ;
     21   if (sa.sun_family != AF_UNIX) return (errno = EPROTO, -1) ;
     22   if (path)
     23   {
     24     if (total == sizeof(sa_family_t)) path[0] = 0 ;
     25     else
     26     {
     27       size_t l = strlen(sa.sun_path) ;
     28       if (l > IPCPATH_MAX) return (errno = EPROTO, -1) ;
     29       memcpy(path, sa.sun_path, l+1) ;
     30     }
     31   }
     32   memcpy(s, tmp, r) ;
     33   return r ;
     34 }