db_pipes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_pipes.c
  15. *
  16. * This file contains oeprands to handle streams with URIs that start with
  17. * "pipe:" or "pipe.srv:".
  18. */
  19. #include <linux/types.h>
  20. #include "api.h"
  21. #include "pal.h"
  22. #include "pal_debug.h"
  23. #include "pal_defs.h"
  24. #include "pal_error.h"
  25. #include "pal_internal.h"
  26. #include "pal_linux.h"
  27. #include "pal_linux_defs.h"
  28. #include "pal_security.h"
  29. typedef __kernel_pid_t pid_t;
  30. #include <asm/errno.h>
  31. #include <asm/fcntl.h>
  32. #include <asm/poll.h>
  33. #include <linux/time.h>
  34. #include <linux/un.h>
  35. #include <sys/socket.h>
  36. static int pipe_path(int pipeid, char* path, int len) {
  37. /* use abstract UNIX sockets for pipes */
  38. memset(path, 0, len);
  39. if (pal_sec.pipe_prefix_id)
  40. return snprintf(path + 1, len - 1, GRAPHENE_UNIX_PREFIX_FMT "/%08x", pal_sec.pipe_prefix_id,
  41. pipeid);
  42. else
  43. return snprintf(path + 1, len - 1, "/graphene/%08x", pipeid);
  44. }
  45. static int pipe_addr(int pipeid, struct sockaddr_un* addr) {
  46. addr->sun_family = AF_UNIX;
  47. return pipe_path(pipeid, (char*)addr->sun_path, sizeof(addr->sun_path));
  48. }
  49. static int pipe_listen(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  50. int ret, fd;
  51. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0);
  52. if (IS_ERR(fd))
  53. return -PAL_ERROR_DENIED;
  54. struct sockaddr_un addr;
  55. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  56. return ret;
  57. ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  58. if (IS_ERR(ret)) {
  59. INLINE_SYSCALL(close, 1, fd);
  60. switch (ERRNO(ret)) {
  61. case EINVAL:
  62. return -PAL_ERROR_INVAL;
  63. case EADDRINUSE:
  64. return -PAL_ERROR_STREAMEXIST;
  65. default:
  66. return -PAL_ERROR_DENIED;
  67. }
  68. }
  69. ret = INLINE_SYSCALL(listen, 2, fd, 1);
  70. if (IS_ERR(ret))
  71. return -PAL_ERROR_DENIED;
  72. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  73. SET_HANDLE_TYPE(hdl, pipesrv);
  74. HANDLE_HDR(hdl)->flags |= RFD(0);
  75. hdl->pipe.fd = fd;
  76. hdl->pipe.pipeid = pipeid;
  77. hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ? PAL_TRUE : PAL_FALSE;
  78. *handle = hdl;
  79. return 0;
  80. }
  81. static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) {
  82. if (!IS_HANDLE_TYPE(handle, pipesrv))
  83. return -PAL_ERROR_NOTSERVER;
  84. if (handle->pipe.fd == PAL_IDX_POISON)
  85. return -PAL_ERROR_DENIED;
  86. int newfd = INLINE_SYSCALL(accept4, 4, handle->pipe.fd, NULL, NULL, O_CLOEXEC);
  87. if (IS_ERR(newfd))
  88. switch (ERRNO(newfd)) {
  89. case EWOULDBLOCK:
  90. return -PAL_ERROR_TRYAGAIN;
  91. case ECONNABORTED:
  92. return -PAL_ERROR_CONNFAILED;
  93. default:
  94. return -PAL_ERROR_DENIED;
  95. }
  96. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
  97. SET_HANDLE_TYPE(clnt, pipecli);
  98. HANDLE_HDR(clnt)->flags |= RFD(0) | WFD(0);
  99. clnt->pipe.fd = newfd;
  100. clnt->pipe.pipeid = handle->pipe.pipeid;
  101. clnt->pipe.nonblocking = PAL_FALSE;
  102. *client = clnt;
  103. return 0;
  104. }
  105. static int pipe_connect(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  106. int ret, fd;
  107. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0);
  108. if (IS_ERR(fd))
  109. return -PAL_ERROR_DENIED;
  110. struct sockaddr_un addr;
  111. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  112. return ret;
  113. ret = INLINE_SYSCALL(connect, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  114. if (IS_ERR(ret)) {
  115. INLINE_SYSCALL(close, 1, fd);
  116. switch (ERRNO(ret)) {
  117. case ECONNREFUSED:
  118. return -PAL_ERROR_STREAMNOTEXIST;
  119. case EINTR:
  120. return -PAL_ERROR_TRYAGAIN;
  121. default:
  122. return -PAL_ERROR_DENIED;
  123. }
  124. }
  125. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  126. SET_HANDLE_TYPE(hdl, pipe);
  127. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0);
  128. hdl->pipe.fd = fd;
  129. hdl->pipe.pipeid = pipeid;
  130. hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  131. *handle = hdl;
  132. return 0;
  133. }
  134. static int pipe_private(PAL_HANDLE* handle, int options) {
  135. int ret, fds[2];
  136. ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0, fds);
  137. if (IS_ERR(ret))
  138. return -PAL_ERROR_DENIED;
  139. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  140. SET_HANDLE_TYPE(hdl, pipeprv);
  141. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1);
  142. hdl->pipeprv.fds[0] = fds[0];
  143. hdl->pipeprv.fds[1] = fds[1];
  144. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  145. *handle = hdl;
  146. return 0;
  147. }
  148. /* 'open' operation of pipe stream. For each pipe stream, it is
  149. identified by a decimal number in URI. There could be two
  150. types: pipe and pipe.srv. They behave pretty much the same,
  151. except they are two ends of the pipe. */
  152. static int pipe_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  153. int create, int options) {
  154. if (!WITHIN_MASK(access, PAL_ACCESS_MASK) || !WITHIN_MASK(share, PAL_SHARE_MASK) ||
  155. !WITHIN_MASK(create, PAL_CREATE_MASK) || !WITHIN_MASK(options, PAL_OPTION_MASK))
  156. return -PAL_ERROR_INVAL;
  157. if (!strcmp_static(type, URI_TYPE_PIPE) && !*uri)
  158. return pipe_private(handle, options);
  159. char* endptr;
  160. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  161. if (*endptr)
  162. return -PAL_ERROR_INVAL;
  163. if (!strcmp_static(type, URI_TYPE_PIPE_SRV))
  164. return pipe_listen(handle, pipeid, options);
  165. if (!strcmp_static(type, URI_TYPE_PIPE))
  166. return pipe_connect(handle, pipeid, options);
  167. return -PAL_ERROR_INVAL;
  168. }
  169. /* 'read' operation of pipe stream. offset does not apply here. */
  170. static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  171. if (offset)
  172. return -PAL_ERROR_INVAL;
  173. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  174. !IS_HANDLE_TYPE(handle, pipe))
  175. return -PAL_ERROR_NOTCONNECTION;
  176. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd;
  177. int64_t bytes = 0;
  178. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  179. if (IS_ERR(bytes))
  180. bytes = unix_to_pal_error(ERRNO(bytes));
  181. if (!bytes)
  182. return -PAL_ERROR_ENDOFSTREAM;
  183. return bytes;
  184. }
  185. /* 'write' operation of pipe stream. offset does not apply here. */
  186. static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, size_t len, const void* buffer) {
  187. if (offset)
  188. return -PAL_ERROR_INVAL;
  189. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  190. !IS_HANDLE_TYPE(handle, pipe))
  191. return -PAL_ERROR_NOTCONNECTION;
  192. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd;
  193. int64_t bytes = 0;
  194. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  195. if (IS_ERR(bytes))
  196. bytes = unix_to_pal_error(ERRNO(bytes));
  197. return bytes;
  198. }
  199. /* 'close' operation of pipe stream. */
  200. static int pipe_close(PAL_HANDLE handle) {
  201. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  202. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  203. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  204. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  205. }
  206. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  207. INLINE_SYSCALL(close, 1, 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. INLINE_SYSCALL(close, 1, 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. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  221. switch (access) {
  222. case 0:
  223. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  224. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  225. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  226. }
  227. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  228. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  229. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  230. }
  231. break;
  232. case PAL_DELETE_RD:
  233. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  234. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  235. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  236. }
  237. break;
  238. case PAL_DELETE_WR:
  239. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  240. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  241. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  242. }
  243. break;
  244. default:
  245. return -PAL_ERROR_INVAL;
  246. }
  247. }
  248. if (handle->pipe.fd == PAL_IDX_POISON)
  249. return 0;
  250. int shutdown;
  251. switch (access) {
  252. case 0:
  253. shutdown = SHUT_RDWR;
  254. break;
  255. case PAL_DELETE_RD:
  256. shutdown = SHUT_RD;
  257. break;
  258. case PAL_DELETE_WR:
  259. shutdown = SHUT_WR;
  260. break;
  261. default:
  262. return -PAL_ERROR_INVAL;
  263. }
  264. INLINE_SYSCALL(shutdown, 2, handle->pipe.fd, shutdown);
  265. return 0;
  266. }
  267. static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  268. int ret;
  269. int val;
  270. if (handle->pipe.fd == PAL_IDX_POISON)
  271. return -PAL_ERROR_BADHANDLE;
  272. attr->handle_type = HANDLE_HDR(handle)->type;
  273. attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking
  274. : handle->pipe.nonblocking;
  275. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  276. /* get number of bytes available for reading (doesn't make sense for "listening" pipes) */
  277. attr->pending_size = 0;
  278. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  279. ret = INLINE_SYSCALL(ioctl, 3, handle->pipe.fd, FIONREAD, &val);
  280. if (IS_ERR(ret))
  281. return unix_to_pal_error(ERRNO(ret));
  282. attr->pending_size = val;
  283. }
  284. /* query if there is data available for reading/writing */
  285. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  286. /* for private pipe, readable and writable are queried on different fds */
  287. struct pollfd pfd[2] = {{.fd = handle->pipeprv.fds[0], .events = POLLIN, .revents = 0},
  288. {.fd = handle->pipeprv.fds[1], .events = POLLOUT, .revents = 0}};
  289. struct timespec tp = {0, 0};
  290. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 2, &tp, NULL, 0);
  291. if (IS_ERR(ret))
  292. return unix_to_pal_error(ERRNO(ret));
  293. attr->readable = ret >= 1 && (pfd[0].revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  294. attr->writable = ret >= 1 && (pfd[1].revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  295. } else {
  296. /* for non-private pipes, both readable and writable are queried on the same fd */
  297. short pfd_events = POLLIN;
  298. if (!IS_HANDLE_TYPE(handle, pipesrv)) {
  299. /* querying for writing doesn't make sense for "listening" pipes */
  300. pfd_events |= POLLOUT;
  301. }
  302. struct pollfd pfd = {.fd = handle->pipe.fd, .events = pfd_events, .revents = 0};
  303. struct timespec tp = {0, 0};
  304. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  305. if (IS_ERR(ret))
  306. return unix_to_pal_error(ERRNO(ret));
  307. attr->readable = ret == 1 && (pfd.revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  308. attr->writable = ret == 1 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  309. }
  310. return 0;
  311. }
  312. static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  313. if (handle->generic.fds[0] == PAL_IDX_POISON)
  314. return -PAL_ERROR_BADHANDLE;
  315. int ret;
  316. PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv)
  317. ? &handle->pipeprv.nonblocking
  318. : &handle->pipe.nonblocking;
  319. if (attr->nonblocking != *nonblocking) {
  320. ret = INLINE_SYSCALL(fcntl, 3, handle->generic.fds[0], F_SETFL,
  321. attr->nonblocking ? O_NONBLOCK : 0);
  322. if (IS_ERR(ret))
  323. return unix_to_pal_error(ERRNO(ret));
  324. *nonblocking = attr->nonblocking;
  325. }
  326. return 0;
  327. }
  328. static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  329. size_t old_count = count;
  330. int ret;
  331. const char* prefix = NULL;
  332. size_t prefix_len = 0;
  333. switch (PAL_GET_TYPE(handle)) {
  334. case pal_type_pipesrv:
  335. case pal_type_pipecli:
  336. prefix_len = static_strlen(URI_TYPE_PIPE_SRV);
  337. prefix = URI_TYPE_PIPE_SRV;
  338. break;
  339. case pal_type_pipe:
  340. prefix_len = static_strlen(URI_TYPE_PIPE);
  341. prefix = URI_TYPE_PIPE;
  342. break;
  343. case pal_type_pipeprv:
  344. default:
  345. return -PAL_ERROR_INVAL;
  346. }
  347. if (prefix_len >= count)
  348. return -PAL_ERROR_OVERFLOW;
  349. memcpy(buffer, prefix, prefix_len);
  350. buffer[prefix_len] = ':';
  351. buffer += prefix_len + 1;
  352. count -= prefix_len + 1;
  353. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  354. if (buffer[ret - 1] != '\n') {
  355. memset(buffer, 0, count);
  356. return -PAL_ERROR_OVERFLOW;
  357. }
  358. buffer[ret - 1] = 0;
  359. buffer += ret - 1;
  360. count -= ret - 1;
  361. return old_count - count;
  362. }
  363. struct handle_ops pipe_ops = {
  364. .getname = &pipe_getname,
  365. .open = &pipe_open,
  366. .waitforclient = &pipe_waitforclient,
  367. .read = &pipe_read,
  368. .write = &pipe_write,
  369. .close = &pipe_close,
  370. .delete = &pipe_delete,
  371. .attrquerybyhdl = &pipe_attrquerybyhdl,
  372. .attrsetbyhdl = &pipe_attrsetbyhdl,
  373. };
  374. struct handle_ops pipeprv_ops = {
  375. .open = &pipe_open,
  376. .read = &pipe_read,
  377. .write = &pipe_write,
  378. .close = &pipe_close,
  379. .attrquerybyhdl = &pipe_attrquerybyhdl,
  380. .attrsetbyhdl = &pipe_attrsetbyhdl,
  381. };