db_pipes.c 14 KB

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