shim_pipe.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_pipe.c
  15. *
  16. * Implementation of system call "pipe", "pipe2" and "socketpair".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_handle.h>
  22. #include <shim_fs.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <errno.h>
  26. #include <asm/fcntl.h>
  27. int create_pipes (IDTYPE * pipeid, PAL_HANDLE * srv, PAL_HANDLE * cli,
  28. struct shim_qstr * qstr, int flags)
  29. {
  30. PAL_HANDLE hdl0 = NULL, hdl1 = NULL, hdl2 = NULL;
  31. int ret = 0;
  32. char uri[PIPE_URI_SIZE];
  33. if ((ret = create_pipe(pipeid, uri, PIPE_URI_SIZE, &hdl0,
  34. qstr)) < 0) {
  35. debug("pipe creation failure\n");
  36. return ret;
  37. }
  38. if (!(hdl2 = DkStreamOpen(uri, 0, 0, 0,
  39. flags & O_NONBLOCK))) {
  40. ret = -PAL_ERRNO;
  41. debug("pipe connection failure\n");
  42. goto err;
  43. }
  44. if (!(hdl1 = DkStreamWaitForClient(hdl0))) {
  45. ret = -PAL_ERRNO;
  46. debug("pipe acception failure\n");
  47. goto err;
  48. }
  49. DkStreamDelete(hdl0, 0);
  50. DkObjectClose(hdl0);
  51. *srv = hdl1;
  52. *cli = hdl2;
  53. return 0;
  54. err:
  55. if (hdl1)
  56. DkObjectClose(hdl1);
  57. if (hdl2)
  58. DkObjectClose(hdl2);
  59. DkStreamDelete(hdl0, 0);
  60. DkObjectClose(hdl0);
  61. return ret;
  62. }
  63. int shim_do_pipe2 (int * filedes, int flags)
  64. {
  65. if (!filedes || test_user_memory(filedes, 2 * sizeof(int), true))
  66. return -EFAULT;
  67. int ret = 0;
  68. struct shim_handle * hdl1 = get_new_handle();
  69. struct shim_handle * hdl2 = get_new_handle();
  70. if (!hdl1 || !hdl2) {
  71. ret = -ENOMEM;
  72. goto out;
  73. }
  74. hdl1->type = TYPE_PIPE;
  75. set_handle_fs(hdl1, &pipe_builtin_fs);
  76. hdl1->flags = O_RDONLY;
  77. hdl1->acc_mode = MAY_READ;
  78. hdl2->type = TYPE_PIPE;
  79. set_handle_fs(hdl2, &pipe_builtin_fs);
  80. hdl2->flags = O_WRONLY;
  81. hdl2->acc_mode = MAY_WRITE;
  82. if ((ret = create_pipes(&hdl1->info.pipe.pipeid,
  83. &hdl1->pal_handle, &hdl2->pal_handle,
  84. &hdl1->uri, flags)) < 0)
  85. goto out;
  86. qstrcopy(&hdl2->uri, &hdl2->uri);
  87. flags = flags & O_CLOEXEC ? FD_CLOEXEC : 0;
  88. int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
  89. int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
  90. if (vfd1 < 0 || vfd2 < 0) {
  91. if (vfd1 >= 0) {
  92. struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
  93. if (tmp)
  94. close_handle(tmp);
  95. }
  96. if (vfd2 >= 0) {
  97. struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
  98. if (tmp)
  99. close_handle(tmp);
  100. }
  101. goto out;
  102. }
  103. filedes[0] = vfd1;
  104. filedes[1] = vfd2;
  105. out:
  106. if (hdl1)
  107. put_handle(hdl1);
  108. if (hdl2)
  109. put_handle(hdl2);
  110. return ret;
  111. }
  112. int shim_do_pipe (int * filedes)
  113. {
  114. return shim_do_pipe2(filedes, 0);
  115. }
  116. int shim_do_socketpair (int domain, int type, int protocol, int * sv)
  117. {
  118. if (domain != AF_UNIX)
  119. return -EAFNOSUPPORT;
  120. if (type != SOCK_STREAM)
  121. return -EPROTONOSUPPORT;
  122. if (!sv || test_user_memory(sv, 2 * sizeof(int), true))
  123. return -EFAULT;
  124. int ret = 0;
  125. struct shim_handle * hdl1 = get_new_handle();
  126. struct shim_handle * hdl2 = get_new_handle();
  127. if (!hdl1 || !hdl2) {
  128. ret = -ENOMEM;
  129. goto out;
  130. }
  131. struct shim_sock_handle * sock1 = &hdl1->info.sock;
  132. struct shim_sock_handle * sock2 = &hdl2->info.sock;
  133. hdl1->type = TYPE_SOCK;
  134. set_handle_fs(hdl1, &socket_builtin_fs);
  135. hdl1->flags = O_RDONLY;
  136. hdl1->acc_mode = MAY_READ|MAY_WRITE;
  137. sock1->domain = domain;
  138. sock1->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  139. sock1->protocol = protocol;
  140. sock1->sock_state = SOCK_ACCEPTED;
  141. hdl2->type = TYPE_SOCK;
  142. set_handle_fs(hdl2, &socket_builtin_fs);
  143. hdl1->flags = O_WRONLY;
  144. hdl2->acc_mode = MAY_READ|MAY_WRITE;
  145. sock2->domain = domain;
  146. sock2->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  147. sock2->protocol = protocol;
  148. sock2->sock_state = SOCK_CONNECTED;
  149. if ((ret = create_pipes(&sock1->addr.un.pipeid, &hdl1->pal_handle,
  150. &hdl2->pal_handle, &hdl1->uri,
  151. type & SOCK_NONBLOCK ? O_NONBLOCK : 0)) < 0)
  152. goto out;
  153. sock2->addr.un.pipeid = sock1->addr.un.pipeid;
  154. qstrcopy(&hdl2->uri, &hdl1->uri);
  155. int flags = type & SOCK_CLOEXEC ? FD_CLOEXEC : 0;
  156. int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
  157. int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
  158. if (vfd1 < 0 || vfd2 < 0) {
  159. if (vfd1 >= 0) {
  160. struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
  161. if (tmp)
  162. close_handle(tmp);
  163. }
  164. if (vfd2 >= 0) {
  165. struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
  166. if (tmp)
  167. close_handle(tmp);
  168. }
  169. goto out;
  170. }
  171. sv[0] = vfd1;
  172. sv[1] = vfd2;
  173. out:
  174. if (hdl1)
  175. put_handle(hdl1);
  176. if (hdl2)
  177. put_handle(hdl2);
  178. return ret;
  179. }