shim_pipe.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_pipe.c
  17. *
  18. * Implementation of system call "pipe", "pipe2" and "socketpair".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_table.h>
  23. #include <shim_handle.h>
  24. #include <shim_fs.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. int create_pipes (IDTYPE * pipeid, PAL_HANDLE * srv, PAL_HANDLE * cli,
  30. struct shim_qstr * qstr, int flags)
  31. {
  32. PAL_HANDLE hdl0 = NULL, hdl1 = NULL, hdl2 = NULL;
  33. int ret = 0;
  34. char * uri = __alloca(PIPE_URI_SIZE);
  35. if ((ret = create_pipe(pipeid, uri, PIPE_URI_SIZE, &hdl0,
  36. qstr)) < 0) {
  37. debug("pipe creation failure\n");
  38. return ret;
  39. }
  40. if (!(hdl2 = DkStreamOpen(uri, 0, 0, 0,
  41. flags & O_NONBLOCK))) {
  42. ret = -PAL_ERRNO;
  43. debug("pipe connection failure\n");
  44. goto err;
  45. }
  46. if (!(hdl1 = DkStreamWaitForClient(hdl0))) {
  47. ret = -PAL_ERRNO;
  48. debug("pipe acception failure\n");
  49. goto err;
  50. }
  51. DkStreamDelete(hdl0, 0);
  52. DkObjectClose(hdl0);
  53. *srv = hdl1;
  54. *cli = hdl2;
  55. return 0;
  56. err:
  57. if (hdl1)
  58. DkObjectClose(hdl1);
  59. if (hdl2)
  60. DkObjectClose(hdl2);
  61. DkStreamDelete(hdl0, 0);
  62. DkObjectClose(hdl0);
  63. return ret;
  64. }
  65. int shim_do_pipe2 (int * filedes, int flags)
  66. {
  67. if (!filedes)
  68. return -EINVAL;
  69. int ret = 0;
  70. struct shim_handle * hdl1 = get_new_handle();
  71. struct shim_handle * hdl2 = get_new_handle();
  72. if (!hdl1 || !hdl2) {
  73. ret = -ENOMEM;
  74. goto out;
  75. }
  76. hdl1->type = TYPE_PIPE;
  77. set_handle_fs(hdl1, &pipe_builtin_fs);
  78. hdl1->acc_mode = MAY_READ;
  79. hdl2->type = TYPE_PIPE;
  80. set_handle_fs(hdl2, &pipe_builtin_fs);
  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)
  123. return -EINVAL;
  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->acc_mode = MAY_READ|MAY_WRITE;
  136. sock1->domain = domain;
  137. sock1->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  138. sock1->protocol = protocol;
  139. sock1->sock_state = SOCK_ACCEPTED;
  140. hdl2->type = TYPE_SOCK;
  141. set_handle_fs(hdl2, &socket_builtin_fs);
  142. hdl2->acc_mode = MAY_READ|MAY_WRITE;
  143. sock2->domain = domain;
  144. sock2->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  145. sock2->protocol = protocol;
  146. sock2->sock_state = SOCK_CONNECTED;
  147. if ((ret = create_pipes(&sock1->addr.un.pipeid, &hdl1->pal_handle,
  148. &hdl2->pal_handle, &hdl1->uri,
  149. type & SOCK_NONBLOCK ? O_NONBLOCK : 0)) < 0)
  150. goto out;
  151. sock2->addr.un.pipeid = sock1->addr.un.pipeid;
  152. qstrcopy(&hdl2->uri, &hdl1->uri);
  153. int flags = type & SOCK_CLOEXEC ? FD_CLOEXEC : 0;
  154. int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
  155. int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
  156. if (vfd1 < 0 || vfd2 < 0) {
  157. if (vfd1 >= 0) {
  158. struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
  159. if (tmp)
  160. close_handle(tmp);
  161. }
  162. if (vfd2 >= 0) {
  163. struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
  164. if (tmp)
  165. close_handle(tmp);
  166. }
  167. goto out;
  168. }
  169. sv[0] = vfd1;
  170. sv[1] = vfd2;
  171. out:
  172. if (hdl1)
  173. put_handle(hdl1);
  174. if (hdl2)
  175. put_handle(hdl2);
  176. return ret;
  177. }