db_pipes.c 13 KB

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