_sock_srv.c 976 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. /* socket extensions */
  10. #include <sys/uio.h>
  11. #include <sys/socket.h>
  12. #include "priv.h"
  13. /* we can't avoid overrunning npath because we don't know how big it is. */
  14. void
  15. _sock_srvname(char *npath, char *path)
  16. {
  17. char *p;
  18. strcpy(npath, "/srv/UD.");
  19. p = strrchr(path, '/');
  20. if(p == 0)
  21. p = path;
  22. else
  23. p++;
  24. strcat(npath, p);
  25. }
  26. int
  27. _sock_srv(char *path, int fd)
  28. {
  29. int sfd;
  30. char msg[8+256+1];
  31. /* change the path to something in srv */
  32. _sock_srvname(msg, path);
  33. /* remove any previous instance */
  34. unlink(msg);
  35. /* put the fd in /srv and then close it */
  36. sfd = creat(msg, 0666);
  37. if(sfd < 0){
  38. close(fd);
  39. _syserrno();
  40. return -1;
  41. }
  42. snprintf(msg, sizeof msg, "%d", fd);
  43. if(write(sfd, msg, strlen(msg)) < 0){
  44. _syserrno();
  45. close(sfd);
  46. close(fd);
  47. return -1;
  48. }
  49. close(sfd);
  50. close(fd);
  51. return 0;
  52. }