_sock_srv.c 883 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. void
  14. _sock_srvname(char *npath, char *path)
  15. {
  16. char *p;
  17. strcpy(npath, "/srv/UD.");
  18. p = strrchr(path, '/');
  19. if(p == 0)
  20. p = path;
  21. else
  22. p++;
  23. strcat(npath, p);
  24. }
  25. int
  26. _sock_srv(char *path, int fd)
  27. {
  28. int sfd;
  29. char msg[128];
  30. /* change the path to something in srv */
  31. _sock_srvname(msg, path);
  32. /* remove any previous instance */
  33. unlink(msg);
  34. /* put the fd in /srv and then close it */
  35. sfd = creat(msg, 0666);
  36. if(sfd < 0){
  37. close(fd);
  38. _syserrno();
  39. return -1;
  40. }
  41. sprintf(msg, "%d", fd);
  42. if(write(sfd, msg, strlen(msg)) < 0){
  43. _syserrno();
  44. close(sfd);
  45. close(fd);
  46. return -1;
  47. }
  48. close(sfd);
  49. close(fd);
  50. return 0;
  51. }