db_pipes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 int64_t pipe_read (PAL_HANDLE handle, uint64_t offset, uint64_t 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. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  163. return -PAL_ERROR_INVAL;
  164. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
  165. handle->pipe.fd;
  166. int bytes = ocall_sock_recv(fd, buffer, len, NULL, NULL);
  167. if (bytes < 0)
  168. return bytes;
  169. if (!bytes)
  170. return -PAL_ERROR_ENDOFSTREAM;
  171. return bytes;
  172. }
  173. /* 'write' operation of pipe stream. offset does not apply here. */
  174. static int64_t pipe_write (PAL_HANDLE handle, uint64_t offset, uint64_t len,
  175. const void * buffer)
  176. {
  177. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  178. !IS_HANDLE_TYPE(handle, pipeprv) &&
  179. !IS_HANDLE_TYPE(handle, pipe))
  180. return -PAL_ERROR_NOTCONNECTION;
  181. if (len >= (1ULL << (sizeof(unsigned int) * 8)))
  182. return -PAL_ERROR_INVAL;
  183. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
  184. handle->pipe.fd;
  185. int bytes = ocall_sock_send(fd, buffer, len, NULL, 0);
  186. PAL_FLG writeable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITEABLE(1) :
  187. WRITEABLE(0);
  188. if (bytes == -PAL_ERROR_TRYAGAIN)
  189. HANDLE_HDR(handle)->flags &= ~writeable;
  190. if (bytes < 0)
  191. return bytes;
  192. if (bytes == len)
  193. HANDLE_HDR(handle)->flags |= writeable;
  194. else
  195. HANDLE_HDR(handle)->flags &= ~writeable;
  196. return bytes;
  197. }
  198. /* 'close' operation of pipe stream. */
  199. static int pipe_close (PAL_HANDLE handle)
  200. {
  201. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  202. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  203. ocall_close(handle->pipeprv.fds[0]);
  204. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  205. }
  206. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  207. ocall_close(handle->pipeprv.fds[1]);
  208. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  209. }
  210. return 0;
  211. }
  212. if (handle->pipe.fd != PAL_IDX_POISON) {
  213. ocall_close(handle->pipe.fd);
  214. handle->pipe.fd = PAL_IDX_POISON;
  215. }
  216. return 0;
  217. }
  218. /* 'delete' operation of pipe stream. */
  219. static int pipe_delete (PAL_HANDLE handle, int access)
  220. {
  221. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  222. switch (access) {
  223. case 0:
  224. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  225. ocall_close(handle->pipeprv.fds[0]);
  226. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  227. }
  228. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  229. ocall_close(handle->pipeprv.fds[1]);
  230. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  231. }
  232. break;
  233. case PAL_DELETE_RD:
  234. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  235. ocall_close(handle->pipeprv.fds[0]);
  236. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  237. }
  238. break;
  239. case PAL_DELETE_WR:
  240. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  241. ocall_close(handle->pipeprv.fds[1]);
  242. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  243. }
  244. break;
  245. default:
  246. return -PAL_ERROR_INVAL;
  247. }
  248. }
  249. if (IS_HANDLE_TYPE(handle, pipesrv)) {
  250. char buffer[108];
  251. pipe_path(handle->pipe.pipeid, buffer, 108);
  252. ocall_delete(buffer);
  253. return 0;
  254. }
  255. if (handle->pipe.fd == PAL_IDX_POISON)
  256. return 0;
  257. int shutdown;
  258. switch (access) {
  259. case 0:
  260. shutdown = SHUT_RDWR;
  261. break;
  262. case PAL_DELETE_RD:
  263. shutdown = SHUT_RD;
  264. break;
  265. case PAL_DELETE_WR:
  266. shutdown = SHUT_WR;
  267. break;
  268. default:
  269. return -PAL_ERROR_INVAL;
  270. }
  271. ocall_sock_shutdown(handle->pipe.fd, shutdown);
  272. return 0;
  273. }
  274. static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  275. {
  276. if (handle->generic.fds[0] == PAL_IDX_POISON)
  277. return -PAL_ERROR_BADHANDLE;
  278. attr->handle_type = PAL_GET_TYPE(handle);
  279. int read_fd = handle->generic.fds[0];
  280. int flags = HANDLE_HDR(handle)->flags;
  281. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  282. int ret = ocall_fionread(read_fd);
  283. if (ret < 0)
  284. return -ret;
  285. attr->pending_size = ret;
  286. attr->writeable = flags & (
  287. IS_HANDLE_TYPE(handle, pipeprv) ? WRITEABLE(1) : WRITEABLE(0));
  288. } else {
  289. attr->pending_size = 0;
  290. attr->writeable = PAL_FALSE;
  291. }
  292. struct pollfd pfd = { .fd = read_fd, .events = POLLIN, .revents = 0 };
  293. unsigned long waittime = 0;
  294. int ret = ocall_poll(&pfd, 1, &waittime);
  295. if (ret < 0)
  296. return ret;
  297. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  298. attr->disconnected = flags & ERROR(0);
  299. attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ?
  300. handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  301. return 0;
  302. }
  303. static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  304. {
  305. if (handle->generic.fds[0] == PAL_IDX_POISON)
  306. return -PAL_ERROR_BADHANDLE;
  307. PAL_BOL * nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) ?
  308. &handle->pipeprv.nonblocking :
  309. &handle->pipe.nonblocking;
  310. if (attr->nonblocking != *nonblocking) {
  311. int ret = ocall_fsetnonblock(handle->generic.fds[0], attr->nonblocking);
  312. if (ret < 0)
  313. return ret;
  314. *nonblocking = attr->nonblocking;
  315. }
  316. return 0;
  317. }
  318. static int pipe_getname (PAL_HANDLE handle, char * buffer, int count)
  319. {
  320. int old_count = count;
  321. int ret;
  322. const char * prefix = NULL;
  323. int prefix_len = 0;
  324. switch (HANDLE_TYPE(handle)) {
  325. case pal_type_pipesrv:
  326. case pal_type_pipecli:
  327. prefix_len = 8;
  328. prefix = "pipe.srv";
  329. break;
  330. case pal_type_pipe:
  331. prefix_len = 4;
  332. prefix = "pipe";
  333. break;
  334. case pal_type_pipeprv:
  335. default:
  336. return -PAL_ERROR_INVAL;
  337. }
  338. if (prefix_len >= count)
  339. return -PAL_ERROR_OVERFLOW;
  340. memcpy(buffer, prefix, prefix_len);
  341. buffer[prefix_len] = ':';
  342. buffer += prefix_len + 1;
  343. count -= prefix_len + 1;
  344. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  345. if (buffer[ret - 1] != '\n') {
  346. memset(buffer, 0, count);
  347. return -PAL_ERROR_OVERFLOW;
  348. }
  349. buffer[ret - 1] = 0;
  350. buffer += ret - 1;
  351. count -= ret - 1;
  352. return old_count - count;
  353. }
  354. struct handle_ops pipe_ops = {
  355. .getname = &pipe_getname,
  356. .open = &pipe_open,
  357. .waitforclient = &pipe_waitforclient,
  358. .read = &pipe_read,
  359. .write = &pipe_write,
  360. .close = &pipe_close,
  361. .delete = &pipe_delete,
  362. .attrquerybyhdl = &pipe_attrquerybyhdl,
  363. .attrsetbyhdl = &pipe_attrsetbyhdl,
  364. };
  365. struct handle_ops pipeprv_ops = {
  366. .open = &pipe_open,
  367. .read = &pipe_read,
  368. .write = &pipe_write,
  369. .close = &pipe_close,
  370. .attrquerybyhdl = &pipe_attrquerybyhdl,
  371. .attrsetbyhdl = &pipe_attrsetbyhdl,
  372. };