db_pipes.c 14 KB

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