Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

53 lines
1.1 KiB

  1. #ifdef __MINGW32__
  2. #include "io_internal.h"
  3. #include <errno.h>
  4. int io_passfd(int64 sock,int64 fd) {
  5. errno=EINVAL;
  6. return -1;
  7. }
  8. #else
  9. #include <stddef.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/uio.h>
  13. #include <errno.h>
  14. #include "io_internal.h"
  15. union fdmsg {
  16. struct cmsghdr h;
  17. /* on NetBSD, CMSG_SPACE is not constant */
  18. /* char buf[CMSG_SPACE(sizeof(int))]; */
  19. char buf[1000];
  20. };
  21. int io_passfd(int64 sock,int64 fd) {
  22. struct msghdr msg;
  23. struct iovec iov;
  24. #ifdef CMSG_LEN
  25. struct cmsghdr *cmsg;
  26. char buf[CMSG_SPACE(sizeof(int))];
  27. #endif
  28. iov.iov_len=1;
  29. iov.iov_base="x";
  30. msg.msg_iov=&iov;
  31. msg.msg_iovlen=1;
  32. msg.msg_name=0;
  33. msg.msg_namelen=0;
  34. #ifdef CMSG_LEN
  35. msg.msg_control = buf;
  36. msg.msg_controllen = sizeof(buf);
  37. cmsg = CMSG_FIRSTHDR(&msg);
  38. cmsg->cmsg_level = SOL_SOCKET;
  39. cmsg->cmsg_type = SCM_RIGHTS;
  40. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  41. msg.msg_controllen = cmsg->cmsg_len;
  42. *((int*)CMSG_DATA(cmsg))=fd;
  43. #else
  44. msg.msg_accrights = (char*)&fd;
  45. msg.msg_accrightslen = sizeof(fd);
  46. #endif
  47. return sendmsg(sock,&msg,0)>=0?0:-1;
  48. }
  49. #endif