db_pipes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. * db_pipes.c
  15. *
  16. * This file contains oeprands to handle streams with URIs that start with
  17. * "pipe:" or "pipe.srv:".
  18. */
  19. #include <linux/types.h>
  20. #include "api.h"
  21. #include "pal.h"
  22. #include "pal_debug.h"
  23. #include "pal_defs.h"
  24. #include "pal_error.h"
  25. #include "pal_internal.h"
  26. #include "pal_linux.h"
  27. #include "pal_linux_defs.h"
  28. #include "pal_linux_error.h"
  29. #include "pal_security.h"
  30. typedef __kernel_pid_t pid_t;
  31. #include <asm/fcntl.h>
  32. #include <asm/poll.h>
  33. #include <linux/un.h>
  34. static int pipe_path(int pipeid, char* path, int len) {
  35. /* use abstrace UNIX sockets for pipes */
  36. memset(path, 0, len);
  37. return snprintf(path + 1, len - 1, "%s%08x", pal_sec.pipe_prefix, pipeid);
  38. }
  39. static int pipe_addr(int pipeid, struct sockaddr_un* addr) {
  40. addr->sun_family = AF_UNIX;
  41. return pipe_path(pipeid, (char*)addr->sun_path, sizeof(addr->sun_path));
  42. }
  43. static inline int pipe_type(int options) {
  44. int type = SOCK_STREAM;
  45. if (options & PAL_OPTION_NONBLOCK)
  46. type |= SOCK_NONBLOCK;
  47. return type;
  48. }
  49. static int pipe_listen(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  50. struct sockaddr_un addr;
  51. int ret;
  52. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  53. return ret;
  54. unsigned int addrlen = sizeof(struct sockaddr_un);
  55. struct sockopt sock_options;
  56. ret = ocall_listen(AF_UNIX, pipe_type(options), 0, /*ipv6_v6only=*/0,
  57. (struct sockaddr*)&addr, &addrlen, &sock_options);
  58. if (IS_ERR(ret))
  59. return unix_to_pal_error(ERRNO(ret));
  60. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  61. SET_HANDLE_TYPE(hdl, pipesrv);
  62. HANDLE_HDR(hdl)->flags |= RFD(0);
  63. hdl->pipe.fd = ret;
  64. hdl->pipe.pipeid = pipeid;
  65. hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ? PAL_TRUE : PAL_FALSE;
  66. *handle = hdl;
  67. return 0;
  68. }
  69. static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) {
  70. if (!IS_HANDLE_TYPE(handle, pipesrv))
  71. return -PAL_ERROR_NOTSERVER;
  72. if (handle->pipe.fd == PAL_IDX_POISON)
  73. return -PAL_ERROR_DENIED;
  74. struct sockopt sock_options;
  75. int ret = ocall_accept(handle->pipe.fd, NULL, NULL, &sock_options);
  76. if (IS_ERR(ret))
  77. return unix_to_pal_error(ERRNO(ret));
  78. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
  79. SET_HANDLE_TYPE(clnt, pipecli);
  80. HANDLE_HDR(clnt)->flags |= RFD(0) | WFD(0);
  81. clnt->pipe.fd = ret;
  82. clnt->pipe.nonblocking = PAL_FALSE;
  83. clnt->pipe.pipeid = handle->pipe.pipeid;
  84. *client = clnt;
  85. return 0;
  86. }
  87. static int pipe_connect(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  88. struct sockaddr_un addr;
  89. int ret;
  90. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  91. return ret;
  92. struct sockopt sock_options;
  93. ret = ocall_connect(AF_UNIX, pipe_type(options), 0, /*ipv6_v6only=*/0, (void*)&addr,
  94. sizeof(struct sockaddr_un), NULL, NULL, &sock_options);
  95. if (IS_ERR(ret))
  96. return unix_to_pal_error(ERRNO(ret));
  97. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  98. SET_HANDLE_TYPE(hdl, pipe);
  99. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0);
  100. hdl->pipe.fd = ret;
  101. hdl->pipe.pipeid = pipeid;
  102. hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  103. *handle = hdl;
  104. return 0;
  105. }
  106. static int pipe_private(PAL_HANDLE* handle, int options) {
  107. int ret, fds[2];
  108. int type = SOCK_STREAM;
  109. if (options & PAL_OPTION_NONBLOCK)
  110. type |= SOCK_NONBLOCK;
  111. ret = ocall_socketpair(AF_UNIX, type, 0, fds);
  112. if (IS_ERR(ret))
  113. return unix_to_pal_error(ERRNO(ret));
  114. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  115. SET_HANDLE_TYPE(hdl, pipeprv);
  116. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1);
  117. hdl->pipeprv.fds[0] = fds[0];
  118. hdl->pipeprv.fds[1] = fds[1];
  119. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  120. *handle = hdl;
  121. return 0;
  122. }
  123. /* 'open' operation of pipe stream. For each pipe stream, it is
  124. identified by a decimal number in URI. There could be two
  125. types: pipe and pipe.srv. They behave pretty much the same,
  126. except they are two ends of the pipe. */
  127. static int pipe_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  128. int create, int options) {
  129. if (!WITHIN_MASK(access, PAL_ACCESS_MASK) || !WITHIN_MASK(share, PAL_SHARE_MASK) ||
  130. !WITHIN_MASK(create, PAL_CREATE_MASK) || !WITHIN_MASK(options, PAL_OPTION_MASK))
  131. return -PAL_ERROR_INVAL;
  132. if (!strcmp_static(type, URI_TYPE_PIPE) && !*uri)
  133. return pipe_private(handle, options);
  134. char* endptr;
  135. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  136. if (*endptr)
  137. return -PAL_ERROR_INVAL;
  138. if (!strcmp_static(type, URI_TYPE_PIPE_SRV))
  139. return pipe_listen(handle, pipeid, options);
  140. if (!strcmp_static(type, URI_TYPE_PIPE))
  141. return pipe_connect(handle, pipeid, options);
  142. return -PAL_ERROR_INVAL;
  143. }
  144. /* 'read' operation of pipe stream. offset does not apply here. */
  145. static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  146. if (offset)
  147. return -PAL_ERROR_INVAL;
  148. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  149. !IS_HANDLE_TYPE(handle, pipe))
  150. return -PAL_ERROR_NOTCONNECTION;
  151. if (len != (uint32_t)len)
  152. return -PAL_ERROR_INVAL;
  153. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd;
  154. ssize_t bytes = ocall_recv(fd, buffer, len, NULL, NULL, NULL, NULL);
  155. if (IS_ERR(bytes))
  156. return unix_to_pal_error(ERRNO(bytes));
  157. if (!bytes)
  158. return -PAL_ERROR_ENDOFSTREAM;
  159. return bytes;
  160. }
  161. /* 'write' operation of pipe stream. offset does not apply here. */
  162. static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, uint64_t len, const void* buffer) {
  163. if (offset)
  164. return -PAL_ERROR_INVAL;
  165. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  166. !IS_HANDLE_TYPE(handle, pipe))
  167. return -PAL_ERROR_NOTCONNECTION;
  168. if (len != (uint32_t)len)
  169. return -PAL_ERROR_INVAL;
  170. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd;
  171. ssize_t bytes = ocall_send(fd, buffer, len, NULL, 0, NULL, 0);
  172. if (IS_ERR(bytes))
  173. return unix_to_pal_error(ERRNO(bytes));
  174. return bytes;
  175. }
  176. /* 'close' operation of pipe stream. */
  177. static int pipe_close(PAL_HANDLE handle) {
  178. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  179. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  180. ocall_close(handle->pipeprv.fds[0]);
  181. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  182. }
  183. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  184. ocall_close(handle->pipeprv.fds[1]);
  185. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  186. }
  187. return 0;
  188. }
  189. if (handle->pipe.fd != PAL_IDX_POISON) {
  190. ocall_close(handle->pipe.fd);
  191. handle->pipe.fd = PAL_IDX_POISON;
  192. }
  193. return 0;
  194. }
  195. /* 'delete' operation of pipe stream. */
  196. static int pipe_delete(PAL_HANDLE handle, int access) {
  197. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  198. switch (access) {
  199. case 0:
  200. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  201. ocall_close(handle->pipeprv.fds[0]);
  202. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  203. }
  204. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  205. ocall_close(handle->pipeprv.fds[1]);
  206. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  207. }
  208. break;
  209. case PAL_DELETE_RD:
  210. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  211. ocall_close(handle->pipeprv.fds[0]);
  212. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  213. }
  214. break;
  215. case PAL_DELETE_WR:
  216. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  217. ocall_close(handle->pipeprv.fds[1]);
  218. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  219. }
  220. break;
  221. default:
  222. return -PAL_ERROR_INVAL;
  223. }
  224. }
  225. if (handle->pipe.fd == PAL_IDX_POISON)
  226. return 0;
  227. int shutdown;
  228. switch (access) {
  229. case 0:
  230. shutdown = SHUT_RDWR;
  231. break;
  232. case PAL_DELETE_RD:
  233. shutdown = SHUT_RD;
  234. break;
  235. case PAL_DELETE_WR:
  236. shutdown = SHUT_WR;
  237. break;
  238. default:
  239. return -PAL_ERROR_INVAL;
  240. }
  241. ocall_shutdown(handle->pipe.fd, shutdown);
  242. return 0;
  243. }
  244. static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  245. int ret;
  246. if (handle->pipe.fd == PAL_IDX_POISON)
  247. return -PAL_ERROR_BADHANDLE;
  248. attr->handle_type = HANDLE_HDR(handle)->type;
  249. attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking
  250. : handle->pipe.nonblocking;
  251. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  252. /* get number of bytes available for reading (doesn't make sense for "listening" pipes) */
  253. attr->pending_size = 0;
  254. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  255. ret = ocall_fionread(handle->pipe.fd);
  256. if (IS_ERR(ret))
  257. return unix_to_pal_error(ERRNO(ret));
  258. attr->pending_size = ret;
  259. }
  260. /* query if there is data available for reading/writing */
  261. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  262. /* for private pipe, readable and writable are queried on different fds */
  263. struct pollfd pfd[2] = {{.fd = handle->pipeprv.fds[0], .events = POLLIN, .revents = 0},
  264. {.fd = handle->pipeprv.fds[1], .events = POLLOUT, .revents = 0}};
  265. ret = ocall_poll(&pfd[0], 2, 0);
  266. if (IS_ERR(ret))
  267. return unix_to_pal_error(ERRNO(ret));
  268. attr->readable = ret >= 1 && (pfd[0].revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  269. attr->writable = ret >= 1 && (pfd[1].revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  270. } else {
  271. /* for non-private pipes, both readable and writable are queried on the same fd */
  272. short pfd_events = POLLIN;
  273. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  274. /* querying for writing doesn't make sense for "listening" pipes */
  275. pfd_events |= POLLOUT;
  276. }
  277. struct pollfd pfd = {.fd = handle->pipe.fd, .events = pfd_events, .revents = 0};
  278. ret = ocall_poll(&pfd, 1, 0);
  279. if (IS_ERR(ret))
  280. return unix_to_pal_error(ERRNO(ret));
  281. attr->readable = ret == 1 && (pfd.revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  282. attr->writable = ret == 1 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  283. }
  284. return 0;
  285. }
  286. static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  287. if (handle->generic.fds[0] == PAL_IDX_POISON)
  288. return -PAL_ERROR_BADHANDLE;
  289. PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv)
  290. ? &handle->pipeprv.nonblocking
  291. : &handle->pipe.nonblocking;
  292. if (attr->nonblocking != *nonblocking) {
  293. int ret = ocall_fsetnonblock(handle->generic.fds[0], attr->nonblocking);
  294. if (IS_ERR(ret))
  295. return unix_to_pal_error(ERRNO(ret));
  296. *nonblocking = attr->nonblocking;
  297. }
  298. return 0;
  299. }
  300. static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  301. int old_count = count;
  302. int ret;
  303. const char* prefix = NULL;
  304. size_t prefix_len = 0;
  305. switch (HANDLE_TYPE(handle)) {
  306. case pal_type_pipesrv:
  307. case pal_type_pipecli:
  308. prefix_len = static_strlen(URI_TYPE_PIPE_SRV);
  309. prefix = URI_TYPE_PIPE_SRV;
  310. break;
  311. case pal_type_pipe:
  312. prefix_len = static_strlen(URI_TYPE_PIPE);
  313. prefix = URI_TYPE_PIPE;
  314. break;
  315. case pal_type_pipeprv:
  316. default:
  317. return -PAL_ERROR_INVAL;
  318. }
  319. if (prefix_len >= count)
  320. return -PAL_ERROR_OVERFLOW;
  321. memcpy(buffer, prefix, prefix_len);
  322. buffer[prefix_len] = ':';
  323. buffer += prefix_len + 1;
  324. count -= prefix_len + 1;
  325. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  326. if (buffer[ret - 1] != '\n') {
  327. memset(buffer, 0, count);
  328. return -PAL_ERROR_OVERFLOW;
  329. }
  330. buffer[ret - 1] = 0;
  331. buffer += ret - 1;
  332. count -= ret - 1;
  333. return old_count - count;
  334. }
  335. struct handle_ops pipe_ops = {
  336. .getname = &pipe_getname,
  337. .open = &pipe_open,
  338. .waitforclient = &pipe_waitforclient,
  339. .read = &pipe_read,
  340. .write = &pipe_write,
  341. .close = &pipe_close,
  342. .delete = &pipe_delete,
  343. .attrquerybyhdl = &pipe_attrquerybyhdl,
  344. .attrsetbyhdl = &pipe_attrsetbyhdl,
  345. };
  346. struct handle_ops pipeprv_ops = {
  347. .open = &pipe_open,
  348. .read = &pipe_read,
  349. .write = &pipe_write,
  350. .close = &pipe_close,
  351. .attrquerybyhdl = &pipe_attrquerybyhdl,
  352. .attrsetbyhdl = &pipe_attrsetbyhdl,
  353. };