db_pipes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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, GRAPHENE_UNIX_PREFIX_FMT "/%08x",
  40. 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. struct sockopt sock_options;
  61. ret = ocall_sock_listen(AF_UNIX, pipe_type(options), 0,
  62. (void *) &addr, sizeof(struct sockaddr_un),
  63. &sock_options);
  64. if (ret < 0)
  65. return ret;
  66. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  67. SET_HANDLE_TYPE(hdl, pipesrv);
  68. HANDLE_HDR(hdl)->flags |= RFD(0);
  69. hdl->pipe.fd = ret;
  70. hdl->pipe.pipeid = pipeid;
  71. hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ?
  72. PAL_TRUE : PAL_FALSE;
  73. *handle = hdl;
  74. return 0;
  75. }
  76. static int pipe_waitforclient (PAL_HANDLE handle, PAL_HANDLE * client)
  77. {
  78. if (!IS_HANDLE_TYPE(handle, pipesrv))
  79. return -PAL_ERROR_NOTSERVER;
  80. if (handle->pipe.fd == PAL_IDX_POISON)
  81. return -PAL_ERROR_DENIED;
  82. struct sockopt sock_options;
  83. int ret = ocall_sock_accept(handle->pipe.fd, NULL, NULL, &sock_options);
  84. if (ret < 0)
  85. return ret;
  86. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
  87. SET_HANDLE_TYPE(clnt, pipecli);
  88. HANDLE_HDR(clnt)->flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  89. clnt->pipe.fd = ret;
  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 (ret < 0)
  105. return ret;
  106. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  107. SET_HANDLE_TYPE(hdl, pipe);
  108. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(0)|WRITEABLE(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 (ret < 0)
  124. return ret;
  125. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  126. SET_HANDLE_TYPE(hdl, pipeprv);
  127. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(1)|WRITEABLE(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. options &= PAL_OPTION_MASK;
  143. if (strpartcmp_static(type, "pipe:") && !*uri)
  144. return pipe_private(handle, options);
  145. char * endptr;
  146. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  147. if (*endptr)
  148. return -PAL_ERROR_INVAL;
  149. if (strpartcmp_static(type, "pipe.srv:"))
  150. return pipe_listen(handle, pipeid, options);
  151. if (strpartcmp_static(type, "pipe:"))
  152. return pipe_connect(handle, pipeid, options);
  153. return -PAL_ERROR_INVAL;
  154. }
  155. /* 'read' operation of pipe stream. offset does not apply here. */
  156. static int pipe_read (PAL_HANDLE handle, int offset, int len,
  157. void * buffer)
  158. {
  159. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  160. !IS_HANDLE_TYPE(handle, pipeprv) &&
  161. !IS_HANDLE_TYPE(handle, pipe))
  162. return -PAL_ERROR_NOTCONNECTION;
  163. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
  164. handle->pipe.fd;
  165. int bytes = ocall_sock_recv(fd, buffer, len, NULL, NULL);
  166. if (bytes < 0)
  167. return bytes;
  168. if (!bytes)
  169. return -PAL_ERROR_ENDOFSTREAM;
  170. return bytes;
  171. }
  172. /* 'write' operation of pipe stream. offset does not apply here. */
  173. static int pipe_write (PAL_HANDLE handle, int offset, int len,
  174. const void * buffer)
  175. {
  176. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  177. !IS_HANDLE_TYPE(handle, pipeprv) &&
  178. !IS_HANDLE_TYPE(handle, pipe))
  179. return -PAL_ERROR_NOTCONNECTION;
  180. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
  181. handle->pipe.fd;
  182. int bytes = ocall_sock_send(fd, buffer, len, NULL, 0);
  183. PAL_FLG writeable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITEABLE(1) :
  184. WRITEABLE(0);
  185. if (bytes == -PAL_ERROR_TRYAGAIN)
  186. HANDLE_HDR(handle)->flags &= ~writeable;
  187. if (bytes < 0)
  188. return bytes;
  189. if (bytes == len)
  190. HANDLE_HDR(handle)->flags |= writeable;
  191. else
  192. HANDLE_HDR(handle)->flags &= ~writeable;
  193. return bytes;
  194. }
  195. /* 'close' operation of pipe stream. */
  196. static int pipe_close (PAL_HANDLE handle)
  197. {
  198. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  199. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  200. ocall_close(handle->pipeprv.fds[0]);
  201. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  202. }
  203. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  204. ocall_close(handle->pipeprv.fds[1]);
  205. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  206. }
  207. return 0;
  208. }
  209. if (handle->pipe.fd != PAL_IDX_POISON) {
  210. ocall_close(handle->pipe.fd);
  211. handle->pipe.fd = PAL_IDX_POISON;
  212. }
  213. return 0;
  214. }
  215. /* 'delete' operation of pipe stream. */
  216. static int pipe_delete (PAL_HANDLE handle, int access)
  217. {
  218. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  219. switch (access) {
  220. case 0:
  221. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  222. ocall_close(handle->pipeprv.fds[0]);
  223. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  224. }
  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. case PAL_DELETE_RD:
  231. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  232. ocall_close(handle->pipeprv.fds[0]);
  233. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  234. }
  235. break;
  236. case PAL_DELETE_WR:
  237. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  238. ocall_close(handle->pipeprv.fds[1]);
  239. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  240. }
  241. break;
  242. default:
  243. return -PAL_ERROR_INVAL;
  244. }
  245. }
  246. if (IS_HANDLE_TYPE(handle, pipesrv)) {
  247. char buffer[108];
  248. pipe_path(handle->pipe.pipeid, buffer, 108);
  249. ocall_delete(buffer);
  250. return 0;
  251. }
  252. if (handle->pipe.fd == PAL_IDX_POISON)
  253. return 0;
  254. int shutdown;
  255. switch (access) {
  256. case 0:
  257. shutdown = SHUT_RDWR;
  258. break;
  259. case PAL_DELETE_RD:
  260. shutdown = SHUT_RD;
  261. break;
  262. case PAL_DELETE_WR:
  263. shutdown = SHUT_WR;
  264. break;
  265. default:
  266. return -PAL_ERROR_INVAL;
  267. }
  268. ocall_sock_shutdown(handle->pipe.fd, shutdown);
  269. return 0;
  270. }
  271. static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  272. {
  273. if (HANDLE_HDR(handle)->fds[0] == PAL_IDX_POISON)
  274. return -PAL_ERROR_BADHANDLE;
  275. int ret = ocall_fionread(HANDLE_HDR(handle)->fds[0]);
  276. if (ret < 0)
  277. return -ret;
  278. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  279. attr->pending_size = ret;
  280. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  281. attr->readable = (attr->pending_size > 0);
  282. attr->writeable = HANDLE_HDR(handle)->flags &
  283. ((HANDLE_TYPE(handle) == pal_type_pipeprv) ? WRITEABLE(1) : WRITEABLE(0));
  284. attr->nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) ?
  285. handle->pipeprv.nonblocking :
  286. handle->pipe.nonblocking;
  287. return 0;
  288. }
  289. static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  290. {
  291. if (HANDLE_HDR(handle)->fds[0] == PAL_IDX_POISON)
  292. return -PAL_ERROR_BADHANDLE;
  293. PAL_BOL * nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) ?
  294. &handle->pipeprv.nonblocking :
  295. &handle->pipe.nonblocking;
  296. if (attr->nonblocking != *nonblocking) {
  297. int ret = ocall_fsetnonblock(HANDLE_HDR(handle)->fds[0], *nonblocking);
  298. if (ret < 0)
  299. return ret;
  300. *nonblocking = attr->nonblocking;
  301. }
  302. return 0;
  303. }
  304. static int pipe_getname (PAL_HANDLE handle, char * buffer, int count)
  305. {
  306. int old_count = count;
  307. int ret;
  308. const char * prefix = NULL;
  309. int prefix_len = 0;
  310. switch (HANDLE_TYPE(handle)) {
  311. case pal_type_pipesrv:
  312. case pal_type_pipecli:
  313. prefix_len = 8;
  314. prefix = "pipe.srv";
  315. break;
  316. case pal_type_pipe:
  317. prefix_len = 4;
  318. prefix = "pipe";
  319. break;
  320. case pal_type_pipeprv:
  321. default:
  322. return -PAL_ERROR_INVAL;
  323. }
  324. if (prefix_len >= count)
  325. return -PAL_ERROR_OVERFLOW;
  326. memcpy(buffer, prefix, prefix_len);
  327. buffer[prefix_len] = ':';
  328. buffer += prefix_len + 1;
  329. count -= prefix_len + 1;
  330. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  331. if (buffer[ret - 1] != '\n') {
  332. memset(buffer, 0, count);
  333. return -PAL_ERROR_OVERFLOW;
  334. }
  335. buffer[ret - 1] = 0;
  336. buffer += ret - 1;
  337. count -= ret - 1;
  338. return old_count - count;
  339. }
  340. struct handle_ops pipe_ops = {
  341. .getname = &pipe_getname,
  342. .open = &pipe_open,
  343. .waitforclient = &pipe_waitforclient,
  344. .read = &pipe_read,
  345. .write = &pipe_write,
  346. .close = &pipe_close,
  347. .delete = &pipe_delete,
  348. .attrquerybyhdl = &pipe_attrquerybyhdl,
  349. .attrsetbyhdl = &pipe_attrsetbyhdl,
  350. };
  351. struct handle_ops pipeprv_ops = {
  352. .open = &pipe_open,
  353. .read = &pipe_read,
  354. .write = &pipe_write,
  355. .close = &pipe_close,
  356. .attrquerybyhdl = &pipe_attrquerybyhdl,
  357. .attrsetbyhdl = &pipe_attrsetbyhdl,
  358. };