db_pipes.c 13 KB

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