db_pipes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 "pal_defs.h"
  20. #include "pal_freebsd_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_freebsd.h"
  24. #include "pal_error.h"
  25. #include "pal_security.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <sys/types.h>
  29. typedef __kernel_pid_t pid_t;
  30. #include <fcntl.h>
  31. #include <poll.h>
  32. #include <sys/socket.h>
  33. #include <sys/un.h>
  34. #include <errno.h>
  35. #include <sys/filio.h>
  36. static int pipe_path (int pipeid, char * path, int len)
  37. {
  38. /* use abstract UNIX sockets for pipes */
  39. memset(path, 0, len);
  40. if (pal_sec.pipe_prefix)
  41. return snprintf(path, len, GRAPHENE_PIPEDIR "/%08x/%s%08x",
  42. pal_sec.domain_id,
  43. pal_sec.pipe_prefix, pipeid);
  44. else
  45. return snprintf(path, len, GRAPHENE_PIPEDIR "/%08x/%08x",
  46. pal_sec.domain_id, pipeid);
  47. }
  48. static int pipe_addr (int pipeid, struct sockaddr_un * addr)
  49. {
  50. addr->sun_family = AF_UNIX;
  51. return pipe_path(pipeid, (char *) addr->sun_path, sizeof(addr->sun_path));
  52. }
  53. static int pipe_listen (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
  54. {
  55. int ret, fd;
  56. options = HOST_SOCKET_OPTIONS(options);
  57. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|options,
  58. 0);
  59. if (IS_ERR(fd))
  60. return -PAL_ERROR_DENIED;
  61. struct sockaddr_un addr;
  62. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  63. return ret;
  64. ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  65. if (IS_ERR(ret)) {
  66. INLINE_SYSCALL(close, 1, fd);
  67. switch(ERRNO(ret)) {
  68. case EINVAL:
  69. return -PAL_ERROR_INVAL;
  70. case EADDRINUSE:
  71. return -PAL_ERROR_STREAMEXIST;
  72. default:
  73. return -PAL_ERROR_DENIED;
  74. }
  75. }
  76. ret = INLINE_SYSCALL(listen, 2, fd, 1);
  77. if (IS_ERR(ret))
  78. return -PAL_ERROR_DENIED;
  79. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  80. SET_HANDLE_TYPE(hdl, pipesrv);
  81. hdl->hdr.flags |= RFD(0);
  82. hdl->pipe.fd = fd;
  83. hdl->pipe.pipeid = pipeid;
  84. hdl->pipe.nonblocking = options & O_NONBLOCK ?
  85. PAL_TRUE : PAL_FALSE;
  86. *handle = hdl;
  87. return 0;
  88. }
  89. static int pipe_waitforclient (PAL_HANDLE handle, PAL_HANDLE * client)
  90. {
  91. if (!IS_HANDLE_TYPE(handle, pipesrv))
  92. return -PAL_ERROR_NOTSERVER;
  93. if (handle->pipe.fd == PAL_IDX_POISON)
  94. return -PAL_ERROR_DENIED;
  95. int newfd = INLINE_SYSCALL(accept4, 4, handle->pipe.fd, NULL, NULL,
  96. SOCK_CLOEXEC);
  97. if (IS_ERR(newfd))
  98. switch (ERRNO(newfd)) {
  99. case EWOULDBLOCK:
  100. return -PAL_ERROR_TRYAGAIN;
  101. case ECONNABORTED:
  102. return -PAL_ERROR_CONNFAILED;
  103. default:
  104. return -PAL_ERROR_DENIED;
  105. }
  106. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
  107. SET_HANDLE_TYPE(clnt, pipecli);
  108. clnt->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
  109. clnt->pipe.fd = newfd;
  110. clnt->pipe.nonblocking = PAL_FALSE;
  111. clnt->pipe.pipeid = handle->pipe.pipeid;
  112. *client = clnt;
  113. return 0;
  114. }
  115. static int pipe_connect (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
  116. {
  117. int ret, fd;
  118. options = HOST_SOCKET_OPTIONS(options);
  119. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|options,
  120. 0);
  121. if (IS_ERR(fd))
  122. return -PAL_ERROR_DENIED;
  123. struct sockaddr_un addr;
  124. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  125. return ret;
  126. ret = INLINE_SYSCALL(connect, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  127. if (IS_ERR(ret)) {
  128. INLINE_SYSCALL(close, 1, fd);
  129. switch (ERRNO(ret)) {
  130. case ECONNREFUSED:
  131. return -PAL_ERROR_STREAMNOTEXIST;
  132. case EINTR:
  133. return -PAL_ERROR_TRYAGAIN;
  134. default:
  135. return -PAL_ERROR_DENIED;
  136. }
  137. }
  138. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  139. SET_HANDLE_TYPE(hdl, pipe);
  140. hdl->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
  141. hdl->pipe.fd = fd;
  142. hdl->pipe.pipeid = pipeid;
  143. hdl->pipe.nonblocking = (options & O_NONBLOCK) ?
  144. PAL_TRUE : PAL_FALSE;
  145. *handle = hdl;
  146. return 0;
  147. }
  148. static int pipe_private (PAL_HANDLE * handle, int options)
  149. {
  150. int ret, fds[2];
  151. options = HOST_SOCKET_OPTIONS(options);
  152. ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
  153. SOCK_STREAM|SOCK_CLOEXEC|options, 0, fds);
  154. if (IS_ERR(ret))
  155. return -PAL_ERROR_DENIED;
  156. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  157. SET_HANDLE_TYPE(hdl, pipeprv);
  158. hdl->hdr.flags |= RFD(0)|WFD(1)|WRITABLE(1);
  159. hdl->pipeprv.fds[0] = fds[0];
  160. hdl->pipeprv.fds[1] = fds[1];
  161. hdl->pipeprv.nonblocking = (options & O_NONBLOCK) ?
  162. PAL_TRUE : PAL_FALSE;
  163. *handle = hdl;
  164. return 0;
  165. }
  166. /* 'open' operation of pipe stream. For each pipe stream, it is
  167. identified by a decimal number in URI. There could be two
  168. types: pipe and pipe.srv. They behave pretty much the same,
  169. except they are two ends of the pipe. */
  170. static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
  171. int access, int share, int create, int options)
  172. {
  173. if (!strcmp_static(type, URI_TYPE_PIPE) && !*uri)
  174. return pipe_private(handle, options);
  175. char * endptr;
  176. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  177. if (*endptr)
  178. return -PAL_ERROR_INVAL;
  179. options = HOST_OPTIONS(options & PAL_OPTION_MASK);
  180. if (!strcmp_static(type, URI_TYPE_PIPE_SRV))
  181. return pipe_listen(handle, pipeid, options);
  182. if (!strcmp_static(type, URI_TYPE_PIPE))
  183. return pipe_connect(handle, pipeid, options);
  184. return -PAL_ERROR_INVAL;
  185. }
  186. /* 'read' operation of pipe stream. offset does not apply here. */
  187. static int pipe_read (PAL_HANDLE handle, int offset, int len,
  188. void * buffer)
  189. {
  190. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  191. !IS_HANDLE_TYPE(handle, pipeprv) &&
  192. !IS_HANDLE_TYPE(handle, pipe))
  193. return -PAL_ERROR_NOTCONNECTION;
  194. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
  195. handle->pipe.fd;
  196. int bytes = 0;
  197. struct msghdr hdr;
  198. struct iovec iov;
  199. iov.iov_base = buffer;
  200. iov.iov_len = len;
  201. hdr.msg_name = NULL;
  202. hdr.msg_namelen = 0;
  203. hdr.msg_iov = &iov;
  204. hdr.msg_iovlen = 1;
  205. hdr.msg_control = NULL;
  206. hdr.msg_controllen = 0;
  207. hdr.msg_flags = 0;
  208. bytes = INLINE_SYSCALL(recvmsg, 3, fd, &hdr, 0);
  209. if (IS_ERR(bytes))
  210. switch(ERRNO(bytes)) {
  211. case EWOULDBLOCK:
  212. return-PAL_ERROR_TRYAGAIN;
  213. case EINTR:
  214. return -PAL_ERROR_INTERRUPTED;
  215. default:
  216. return -PAL_ERROR_DENIED;
  217. }
  218. if (!bytes)
  219. return -PAL_ERROR_ENDOFSTREAM;
  220. return bytes;
  221. }
  222. /* 'write' operation of pipe stream. offset does not apply here. */
  223. static int pipe_write (PAL_HANDLE handle, int offset, int len,
  224. const void * buffer)
  225. {
  226. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  227. !IS_HANDLE_TYPE(handle, pipeprv) &&
  228. !IS_HANDLE_TYPE(handle, pipe))
  229. return -PAL_ERROR_NOTCONNECTION;
  230. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
  231. handle->pipe.fd;
  232. int bytes = 0;
  233. struct msghdr hdr;
  234. struct iovec iov;
  235. iov.iov_base = (void *) buffer;
  236. iov.iov_len = len;
  237. hdr.msg_name = NULL;
  238. hdr.msg_namelen = 0;
  239. hdr.msg_iov = &iov;
  240. hdr.msg_iovlen = 1;
  241. hdr.msg_control = NULL;
  242. hdr.msg_controllen = 0;
  243. hdr.msg_flags = 0;
  244. bytes = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, MSG_NOSIGNAL);
  245. PAL_FLG writable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) :
  246. WRITABLE(0);
  247. if (IS_ERR(bytes))
  248. switch(ERRNO(bytes)) {
  249. case EWOULDBLOCK:
  250. handle->hdr.flags &= ~writable;
  251. return-PAL_ERROR_TRYAGAIN;
  252. case EINTR:
  253. return -PAL_ERROR_INTERRUPTED;
  254. default:
  255. return -PAL_ERROR_DENIED;
  256. }
  257. if (bytes == len)
  258. handle->hdr.flags |= writable;
  259. else
  260. handle->hdr.flags &= ~writable;
  261. return bytes;
  262. }
  263. /* 'close' operation of pipe stream. */
  264. static int pipe_close (PAL_HANDLE handle)
  265. {
  266. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  267. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  268. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  269. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  270. }
  271. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  272. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  273. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  274. }
  275. return 0;
  276. }
  277. if (handle->pipe.fd != PAL_IDX_POISON) {
  278. INLINE_SYSCALL(close, 1, handle->pipe.fd);
  279. handle->pipe.fd = PAL_IDX_POISON;
  280. }
  281. return 0;
  282. }
  283. /* 'delete' operation of pipe stream. */
  284. static int pipe_delete (PAL_HANDLE handle, int access)
  285. {
  286. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  287. switch (access) {
  288. case 0:
  289. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  290. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  291. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  292. }
  293. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  294. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  295. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  296. }
  297. break;
  298. case PAL_DELETE_RD:
  299. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  300. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  301. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  302. }
  303. break;
  304. case PAL_DELETE_WR:
  305. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  306. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  307. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  308. }
  309. break;
  310. default:
  311. return -PAL_ERROR_INVAL;
  312. }
  313. }
  314. if (handle->pipe.fd == PAL_IDX_POISON)
  315. return 0;
  316. int shutdown;
  317. switch (access) {
  318. case 0:
  319. shutdown = SHUT_RDWR;
  320. break;
  321. case PAL_DELETE_RD:
  322. shutdown = SHUT_RD;
  323. break;
  324. case PAL_DELETE_WR:
  325. shutdown = SHUT_WR;
  326. break;
  327. default:
  328. return -PAL_ERROR_INVAL;
  329. }
  330. INLINE_SYSCALL(shutdown, 2, handle->pipe.fd, shutdown);
  331. return 0;
  332. }
  333. static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  334. {
  335. int ret, val;
  336. if (handle->hdr.fds[0] == PAL_IDX_POISON)
  337. return -PAL_ERROR_BADHANDLE;
  338. ret = INLINE_SYSCALL(ioctl, 3, handle->hdr.fds[0], FIONREAD, &val);
  339. if (IS_ERR(ret))
  340. return unix_to_pal_error(ERRNO(ret));
  341. attr->handle_type = pal_type_pipe;
  342. attr->disconnected = handle->hdr.flags & ERROR(0);
  343. attr->nonblocking = (handle->hdr.type == pal_type_pipeprv) ?
  344. handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  345. attr->readable = val > 0;
  346. if (PAL_GET_TYPE(handle) == pal_type_pipeprv)
  347. attr->writable = handle->hdr.flags & WRITABLE(1);
  348. else
  349. attr->writable = handle->hdr.flags & WRITABLE(0);
  350. attr->pending_size = val;
  351. return 0;
  352. }
  353. static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  354. {
  355. if (handle->hdr.fds[0] == PAL_IDX_POISON)
  356. return -PAL_ERROR_BADHANDLE;
  357. int ret;
  358. PAL_BOL * nonblocking = (handle->hdr.type == pal_type_pipeprv) ?
  359. &handle->pipeprv.nonblocking :
  360. &handle->pipe.nonblocking;
  361. if (attr->nonblocking != *nonblocking) {
  362. ret = INLINE_SYSCALL(fcntl, 3, handle->hdr.fds[0], F_SETFL,
  363. *nonblocking ? O_NONBLOCK : 0);
  364. if (IS_ERR(ret))
  365. return unix_to_pal_error(ERRNO(ret));
  366. *nonblocking = attr->nonblocking;
  367. }
  368. return 0;
  369. }
  370. static int pipe_getname (PAL_HANDLE handle, char * buffer, int count)
  371. {
  372. int old_count = count;
  373. int ret;
  374. const char * prefix = NULL;
  375. int prefix_len = 0;
  376. switch (PAL_GET_TYPE(handle)) {
  377. case pal_type_pipesrv:
  378. case pal_type_pipecli:
  379. prefix_len = static_strlen(URI_TYPE_PIPE_SRV);
  380. prefix = URI_TYPE_PIPE_SRV;
  381. break;
  382. case pal_type_pipe:
  383. prefix_len = static_strlen(URI_TYPE_PIPE);
  384. prefix = URI_TYPE_PIPE;
  385. break;
  386. case pal_type_pipeprv:
  387. default:
  388. return -PAL_ERROR_INVAL;
  389. }
  390. if (prefix_len >= count)
  391. return -PAL_ERROR_OVERFLOW;
  392. memcpy(buffer, prefix, prefix_len);
  393. buffer[prefix_len] = ':';
  394. buffer += prefix_len + 1;
  395. count -= prefix_len + 1;
  396. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  397. if (buffer[ret - 1] != '\n') {
  398. memset(buffer, 0, count);
  399. return -PAL_ERROR_OVERFLOW;
  400. }
  401. buffer[ret - 1] = 0;
  402. buffer += ret - 1;
  403. count -= ret - 1;
  404. return old_count - count;
  405. }
  406. struct handle_ops pipe_ops = {
  407. .getname = &pipe_getname,
  408. .open = &pipe_open,
  409. .waitforclient = &pipe_waitforclient,
  410. .read = &pipe_read,
  411. .write = &pipe_write,
  412. .close = &pipe_close,
  413. .delete = &pipe_delete,
  414. .attrquerybyhdl = &pipe_attrquerybyhdl,
  415. .attrsetbyhdl = &pipe_attrsetbyhdl,
  416. };
  417. struct handle_ops pipeprv_ops = {
  418. .open = &pipe_open,
  419. .read = &pipe_read,
  420. .write = &pipe_write,
  421. .close = &pipe_close,
  422. .attrquerybyhdl = &pipe_attrquerybyhdl,
  423. .attrsetbyhdl = &pipe_attrsetbyhdl,
  424. };