db_pipes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, (struct sockaddr*)&addr, &addrlen,
  57. &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) | WRITABLE(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, (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) | WRITABLE(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) | WRITABLE(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, "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, "pipe.srv"))
  139. return pipe_listen(handle, pipeid, options);
  140. if (!strcmp_static(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 >= (1ULL << (sizeof(unsigned int) * 8)))
  152. return -PAL_ERROR_INVAL;
  153. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd;
  154. int 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 >= (1ULL << (sizeof(unsigned int) * 8)))
  169. return -PAL_ERROR_INVAL;
  170. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd;
  171. int bytes = ocall_send(fd, buffer, len, NULL, 0, NULL, 0);
  172. PAL_FLG writable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) : WRITABLE(0);
  173. if (IS_ERR(bytes)) {
  174. bytes = unix_to_pal_error(ERRNO(bytes));
  175. if (bytes == -PAL_ERROR_TRYAGAIN)
  176. HANDLE_HDR(handle)->flags &= ~writable;
  177. return bytes;
  178. }
  179. if ((uint64_t)bytes == len)
  180. HANDLE_HDR(handle)->flags |= writable;
  181. else
  182. HANDLE_HDR(handle)->flags &= ~writable;
  183. return bytes;
  184. }
  185. /* 'close' operation of pipe stream. */
  186. static int pipe_close(PAL_HANDLE handle) {
  187. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  188. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  189. ocall_close(handle->pipeprv.fds[0]);
  190. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  191. }
  192. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  193. ocall_close(handle->pipeprv.fds[1]);
  194. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  195. }
  196. return 0;
  197. }
  198. if (handle->pipe.fd != PAL_IDX_POISON) {
  199. ocall_close(handle->pipe.fd);
  200. handle->pipe.fd = PAL_IDX_POISON;
  201. }
  202. return 0;
  203. }
  204. /* 'delete' operation of pipe stream. */
  205. static int pipe_delete(PAL_HANDLE handle, int access) {
  206. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  207. switch (access) {
  208. case 0:
  209. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  210. ocall_close(handle->pipeprv.fds[0]);
  211. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  212. }
  213. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  214. ocall_close(handle->pipeprv.fds[1]);
  215. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  216. }
  217. break;
  218. case PAL_DELETE_RD:
  219. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  220. ocall_close(handle->pipeprv.fds[0]);
  221. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  222. }
  223. break;
  224. case PAL_DELETE_WR:
  225. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  226. ocall_close(handle->pipeprv.fds[1]);
  227. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  228. }
  229. break;
  230. default:
  231. return -PAL_ERROR_INVAL;
  232. }
  233. }
  234. if (handle->pipe.fd == PAL_IDX_POISON)
  235. return 0;
  236. int shutdown;
  237. switch (access) {
  238. case 0:
  239. shutdown = SHUT_RDWR;
  240. break;
  241. case PAL_DELETE_RD:
  242. shutdown = SHUT_RD;
  243. break;
  244. case PAL_DELETE_WR:
  245. shutdown = SHUT_WR;
  246. break;
  247. default:
  248. return -PAL_ERROR_INVAL;
  249. }
  250. ocall_shutdown(handle->pipe.fd, shutdown);
  251. return 0;
  252. }
  253. static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  254. if (handle->generic.fds[0] == PAL_IDX_POISON)
  255. return -PAL_ERROR_BADHANDLE;
  256. attr->handle_type = PAL_GET_TYPE(handle);
  257. int read_fd = handle->generic.fds[0];
  258. int flags = HANDLE_HDR(handle)->flags;
  259. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  260. int ret = ocall_fionread(read_fd);
  261. if (IS_ERR(ret))
  262. return unix_to_pal_error(ERRNO(ret));
  263. attr->pending_size = ret;
  264. attr->writable = flags & (IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) : WRITABLE(0));
  265. } else {
  266. attr->pending_size = 0;
  267. attr->writable = PAL_FALSE;
  268. }
  269. struct pollfd pfd = {.fd = read_fd, .events = POLLIN, .revents = 0};
  270. int ret = ocall_poll(&pfd, 1, 0);
  271. if (IS_ERR(ret))
  272. return unix_to_pal_error(ERRNO(ret));
  273. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  274. attr->disconnected = flags & ERROR(0);
  275. attr->nonblocking =
  276. IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  277. return 0;
  278. }
  279. static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  280. if (handle->generic.fds[0] == PAL_IDX_POISON)
  281. return -PAL_ERROR_BADHANDLE;
  282. PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv)
  283. ? &handle->pipeprv.nonblocking
  284. : &handle->pipe.nonblocking;
  285. if (attr->nonblocking != *nonblocking) {
  286. int ret = ocall_fsetnonblock(handle->generic.fds[0], attr->nonblocking);
  287. if (IS_ERR(ret))
  288. return unix_to_pal_error(ERRNO(ret));
  289. *nonblocking = attr->nonblocking;
  290. }
  291. return 0;
  292. }
  293. static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  294. int old_count = count;
  295. int ret;
  296. const char* prefix = NULL;
  297. size_t prefix_len = 0;
  298. switch (HANDLE_TYPE(handle)) {
  299. case pal_type_pipesrv:
  300. case pal_type_pipecli:
  301. prefix_len = 8;
  302. prefix = "pipe.srv";
  303. break;
  304. case pal_type_pipe:
  305. prefix_len = 4;
  306. prefix = "pipe";
  307. break;
  308. case pal_type_pipeprv:
  309. default:
  310. return -PAL_ERROR_INVAL;
  311. }
  312. if (prefix_len >= count)
  313. return -PAL_ERROR_OVERFLOW;
  314. memcpy(buffer, prefix, prefix_len);
  315. buffer[prefix_len] = ':';
  316. buffer += prefix_len + 1;
  317. count -= prefix_len + 1;
  318. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  319. if (buffer[ret - 1] != '\n') {
  320. memset(buffer, 0, count);
  321. return -PAL_ERROR_OVERFLOW;
  322. }
  323. buffer[ret - 1] = 0;
  324. buffer += ret - 1;
  325. count -= ret - 1;
  326. return old_count - count;
  327. }
  328. struct handle_ops pipe_ops = {
  329. .getname = &pipe_getname,
  330. .open = &pipe_open,
  331. .waitforclient = &pipe_waitforclient,
  332. .read = &pipe_read,
  333. .write = &pipe_write,
  334. .close = &pipe_close,
  335. .delete = &pipe_delete,
  336. .attrquerybyhdl = &pipe_attrquerybyhdl,
  337. .attrsetbyhdl = &pipe_attrsetbyhdl,
  338. };
  339. struct handle_ops pipeprv_ops = {
  340. .open = &pipe_open,
  341. .read = &pipe_read,
  342. .write = &pipe_write,
  343. .close = &pipe_close,
  344. .attrquerybyhdl = &pipe_attrquerybyhdl,
  345. .attrsetbyhdl = &pipe_attrsetbyhdl,
  346. };