描述符传递

前端之家收集整理的这篇文章主要介绍了描述符传递前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Example.No.1:
通过socketpair打开两个连接起来的Unix套接字,一个用于进程本身读取内容,并输出到进程本身的标准输出,另一个则用于子进程传递通过Unix套接字发送excel的一个进程打开的文件描述符,通过sendmsg的辅助数据传递

进程本身代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/wait.h>
  8. #include <fcntl.h>
  9.  
  10. #define BUFSIZE 4096
  11.  
  12.  
  13. int read_fd(int fd,void* ptr,size_t nbytes,int* recvfd ) {
  14. struct msghdr msg;
  15. struct iovec iov[1];
  16. ssize_t n;
  17. union {
  18. struct cmsghdr cm;
  19. char control[CMSG_SPACE(sizeof(int))];
  20. }control_un;
  21.  
  22. msg.msg_control = control_un.control;
  23. msg.msg_controllen = sizeof(control_un.control);
  24. msg.msg_name = NULL;
  25. msg.msg_namelen = 0;
  26. iov[0].iov_base = ptr;
  27. iov[0].iov_len = nbytes;
  28. msg.msg_iov = iov;
  29. msg.msg_iovlen = 1;
  30. if ((n = recvmsg(fd,&msg,0)) <= 0) {
  31. return n;
  32. }
  33. struct cmsghdr* cmptr;
  34. if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
  35. if (cmptr->cmsg_level != SOL_SOCKET) {
  36. printf("control level != SOL_SOCKET\n");
  37. exit(1);
  38. }
  39.  
  40. if (cmptr->cmsg_type != SCM_RIGHTS) {
  41. printf("control level != SCM_RIGHTS\n");
  42. exit(1);
  43. }
  44. *recvfd = *(int*)CMSG_DATA(cmptr);
  45. }else {
  46. *recvfd = -1;
  47. }
  48. }
  49. int my_open(char* pathname,int mode) {
  50. int fd,sockfd[2],status;
  51. char c;
  52. if (socketpair(AF_LOCAL,SOCK_STREAM,0,sockfd) < 0) {
  53. printf("setsockpair error: %s\n",strerror(errno));
  54. exit(1);
  55. }
  56. int childpid;
  57. if ((childpid = fork()) < 0) {
  58. printf("fork error: %s\n",strerror(errno));
  59. exit(1);
  60. }else if (childpid == 0) {
  61. char argsockfd[10],argmode[10];
  62. snprintf(argsockfd,sizeof(argsockfd),"%d",sockfd[1]);
  63. snprintf(argmode,sizeof(argmode),mode);
  64. execl("./openfile","openfile",argsockfd,pathname,argmode,(char*)NULL);
  65. }
  66. close(sockfd[1]);
  67. if (waitpid(childpid,&status,0) < 0) {
  68. printf("waitpid error: %s\n",strerror(errno));
  69. exit(1);
  70. }
  71. if (WIFEXITED(status) == 0) {
  72. printf("child process did not terminate\n");
  73. exit(1);
  74. }
  75.  
  76. if ((status = WEXITSTATUS(status)) == 0) {
  77. if (read_fd(sockfd[0],&c,1,&fd) < 0) {
  78. printf("read_fd error\n");
  79. exit(1);
  80. }
  81. }else {
  82. errno = status;
  83. fd = -1;
  84. }
  85. close(sockfd[0]);
  86. return fd;
  87. }
  88. int main(int argc,char** argv) {
  89. if (argc != 2) {
  90. printf("please add <pathname>\n");
  91. exit(1);
  92. }
  93.  
  94. int fd;
  95. if ((fd = my_open(argv[1],O_RDONLY)) < 0) {
  96. printf("can't open %s\n",argv[1]);
  97. exit(1);
  98. }
  99. char buf[BUFSIZE];
  100. int n;
  101. while ((n = read(fd,buf,BUFSIZE)) > 0) {
  102. if (write(STDOUT_FILENO,n) != n) {
  103. printf("write error: %s\n",strerror(errno));
  104. exit(1);
  105. }
  106. }
  107. return 0;
  108. }

openfile进程代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. int write_fd(int fd,int sendfd) {
  10. struct msghdr msg;
  11. struct iovec iov[1];
  12.  
  13. union {
  14. struct cmsghdr cmsg;
  15. char controls[CMSG_SPACE(sizeof(int))];
  16. }control_un;
  17.  
  18. msg.msg_name = NULL;
  19. msg.msg_namelen = 0;
  20. msg.msg_control = control_un.controls;
  21. msg.msg_controllen = sizeof(control_un.controls);
  22. struct cmsghdr* cmptr;
  23. cmptr = CMSG_FIRSTHDR(&msg);
  24. cmptr->cmsg_len = CMSG_LEN(sizeof(int));
  25. cmptr->cmsg_level = SOL_SOCKET;
  26. cmptr->cmsg_type = SCM_RIGHTS;
  27. *(int*)(CMSG_DATA(cmptr)) = sendfd;
  28. iov[0].iov_base = ptr;
  29. iov[0].iov_len = nbytes;
  30. msg.msg_iov = iov;
  31. msg.msg_iovlen = 1;
  32. return sendmsg(fd,0);
  33. }
  34.  
  35. int main(int argc,char** argv) {
  36. if (argc != 4) {
  37. printf("please check your input-arguments\n");
  38. exit(1);
  39. }
  40.  
  41. int fd;
  42. if ((fd = open(argv[2],atoi(argv[3]))) < 0) {
  43. printf("open %s fail: error reason: %s\n",argv[2],strerror(errno));
  44. exit(1);
  45. }
  46.  
  47. int n;
  48. if ((n = write_fd(atoi(argv[1]),"",fd)) < 0) {
  49. exit(n);
  50. }
  51. exit(0);
  52. }

猜你在找的Bash相关文章