shim_pipe.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 <asm/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->flags = O_RDONLY;
  79. hdl1->acc_mode = MAY_READ;
  80. hdl2->type = TYPE_PIPE;
  81. set_handle_fs(hdl2, &pipe_builtin_fs);
  82. hdl2->flags = O_WRONLY;
  83. hdl2->acc_mode = MAY_WRITE;
  84. if ((ret = create_pipes(&hdl1->info.pipe.pipeid,
  85. &hdl1->pal_handle, &hdl2->pal_handle,
  86. &hdl1->uri, flags)) < 0)
  87. goto out;
  88. qstrcopy(&hdl2->uri, &hdl2->uri);
  89. flags = flags & O_CLOEXEC ? FD_CLOEXEC : 0;
  90. int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
  91. int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
  92. if (vfd1 < 0 || vfd2 < 0) {
  93. if (vfd1 >= 0) {
  94. struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
  95. if (tmp)
  96. close_handle(tmp);
  97. }
  98. if (vfd2 >= 0) {
  99. struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
  100. if (tmp)
  101. close_handle(tmp);
  102. }
  103. goto out;
  104. }
  105. filedes[0] = vfd1;
  106. filedes[1] = vfd2;
  107. out:
  108. if (hdl1)
  109. put_handle(hdl1);
  110. if (hdl2)
  111. put_handle(hdl2);
  112. return ret;
  113. }
  114. int shim_do_pipe (int * filedes)
  115. {
  116. return shim_do_pipe2(filedes, 0);
  117. }
  118. int shim_do_socketpair (int domain, int type, int protocol, int * sv)
  119. {
  120. if (domain != AF_UNIX)
  121. return -EAFNOSUPPORT;
  122. if (type != SOCK_STREAM)
  123. return -EPROTONOSUPPORT;
  124. if (!sv)
  125. return -EINVAL;
  126. int ret = 0;
  127. struct shim_handle * hdl1 = get_new_handle();
  128. struct shim_handle * hdl2 = get_new_handle();
  129. if (!hdl1 || !hdl2) {
  130. ret = -ENOMEM;
  131. goto out;
  132. }
  133. struct shim_sock_handle * sock1 = &hdl1->info.sock;
  134. struct shim_sock_handle * sock2 = &hdl2->info.sock;
  135. hdl1->type = TYPE_SOCK;
  136. set_handle_fs(hdl1, &socket_builtin_fs);
  137. hdl1->flags = O_RDONLY;
  138. hdl1->acc_mode = MAY_READ|MAY_WRITE;
  139. sock1->domain = domain;
  140. sock1->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  141. sock1->protocol = protocol;
  142. sock1->sock_state = SOCK_ACCEPTED;
  143. hdl2->type = TYPE_SOCK;
  144. set_handle_fs(hdl2, &socket_builtin_fs);
  145. hdl1->flags = O_WRONLY;
  146. hdl2->acc_mode = MAY_READ|MAY_WRITE;
  147. sock2->domain = domain;
  148. sock2->sock_type = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
  149. sock2->protocol = protocol;
  150. sock2->sock_state = SOCK_CONNECTED;
  151. if ((ret = create_pipes(&sock1->addr.un.pipeid, &hdl1->pal_handle,
  152. &hdl2->pal_handle, &hdl1->uri,
  153. type & SOCK_NONBLOCK ? O_NONBLOCK : 0)) < 0)
  154. goto out;
  155. sock2->addr.un.pipeid = sock1->addr.un.pipeid;
  156. qstrcopy(&hdl2->uri, &hdl1->uri);
  157. int flags = type & SOCK_CLOEXEC ? FD_CLOEXEC : 0;
  158. int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
  159. int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
  160. if (vfd1 < 0 || vfd2 < 0) {
  161. if (vfd1 >= 0) {
  162. struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
  163. if (tmp)
  164. close_handle(tmp);
  165. }
  166. if (vfd2 >= 0) {
  167. struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
  168. if (tmp)
  169. close_handle(tmp);
  170. }
  171. goto out;
  172. }
  173. sv[0] = vfd1;
  174. sv[1] = vfd2;
  175. out:
  176. if (hdl1)
  177. put_handle(hdl1);
  178. if (hdl2)
  179. put_handle(hdl2);
  180. return ret;
  181. }