db_pipes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #if USE_PIPE_SYSCALL == 1
  37. #include <linux/msg.h>
  38. #endif
  39. static int pipe_path(int pipeid, char* path, int len) {
  40. /* use abstract UNIX sockets for pipes */
  41. memset(path, 0, len);
  42. if (pal_sec.pipe_prefix_id)
  43. return snprintf(path + 1, len - 1, GRAPHENE_UNIX_PREFIX_FMT "/%08x", pal_sec.pipe_prefix_id,
  44. pipeid);
  45. else
  46. return snprintf(path + 1, len - 1, "/graphene/%08x", pipeid);
  47. }
  48. static int pipe_addr(int pipeid, struct sockaddr_un* addr) {
  49. addr->sun_family = AF_UNIX;
  50. return pipe_path(pipeid, (char*)addr->sun_path, sizeof(addr->sun_path));
  51. }
  52. static int pipe_listen(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  53. int ret, fd;
  54. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0);
  55. if (IS_ERR(fd))
  56. return -PAL_ERROR_DENIED;
  57. struct sockaddr_un addr;
  58. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  59. return ret;
  60. ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  61. if (IS_ERR(ret)) {
  62. INLINE_SYSCALL(close, 1, fd);
  63. switch (ERRNO(ret)) {
  64. case EINVAL:
  65. return -PAL_ERROR_INVAL;
  66. case EADDRINUSE:
  67. return -PAL_ERROR_STREAMEXIST;
  68. default:
  69. return -PAL_ERROR_DENIED;
  70. }
  71. }
  72. ret = INLINE_SYSCALL(listen, 2, fd, 1);
  73. if (IS_ERR(ret))
  74. return -PAL_ERROR_DENIED;
  75. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  76. SET_HANDLE_TYPE(hdl, pipesrv);
  77. HANDLE_HDR(hdl)->flags |= RFD(0);
  78. hdl->pipe.fd = fd;
  79. hdl->pipe.pipeid = pipeid;
  80. hdl->pipe.nonblocking = options & PAL_OPTION_NONBLOCK ? PAL_TRUE : PAL_FALSE;
  81. *handle = hdl;
  82. return 0;
  83. }
  84. static int pipe_waitforclient(PAL_HANDLE handle, PAL_HANDLE* client) {
  85. if (!IS_HANDLE_TYPE(handle, pipesrv))
  86. return -PAL_ERROR_NOTSERVER;
  87. if (handle->pipe.fd == PAL_IDX_POISON)
  88. return -PAL_ERROR_DENIED;
  89. int newfd = INLINE_SYSCALL(accept4, 4, handle->pipe.fd, NULL, NULL, O_CLOEXEC);
  90. if (IS_ERR(newfd))
  91. switch (ERRNO(newfd)) {
  92. case EWOULDBLOCK:
  93. return -PAL_ERROR_TRYAGAIN;
  94. case ECONNABORTED:
  95. return -PAL_ERROR_CONNFAILED;
  96. default:
  97. return -PAL_ERROR_DENIED;
  98. }
  99. #if USE_PIPE_SYSCALL == 1
  100. int pipes[2];
  101. struct msghdr hdr;
  102. struct iovec iov;
  103. char cbuf[sizeof(struct cmsghdr) + 2 * sizeof(int)];
  104. char b = 0;
  105. int ret = 0;
  106. memset(&hdr, 0, sizeof(struct msghdr));
  107. hdr.msg_iov = &iov;
  108. hdr.msg_iovlen = 1;
  109. hdr.msg_control = cbuf;
  110. hdr.msg_controllen = sizeof(cbuf);
  111. iov.iov_base = &b;
  112. iov.iov_len = 1;
  113. ret = INLINE_SYSCALL(recvmsg, 3, newfd, &hdr, 0);
  114. INLINE_SYSCALL(close, 1, newfd);
  115. struct cmsghdr* chdr = CMSG_FIRSTHDR(&hdr);
  116. if (IS_ERR(ret) || chdr->cmsg_type != SCM_RIGHTS)
  117. return -PAL_ERROR_DENIED;
  118. memcpy(pipes, CMSG_DATA(chdr), sizeof(int) * 2);
  119. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipeprv));
  120. SET_HANDLE_TYPE(clnt, pipeprv);
  121. clnt->__in.flags |= RFD(0) | WFD(1) | WRITABLE(1);
  122. clnt->pipeprv.fds[0] = pipes[0];
  123. clnt->pipeprv.fds[1] = pipes[1];
  124. *client = clnt;
  125. #else
  126. PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
  127. SET_HANDLE_TYPE(clnt, pipecli);
  128. HANDLE_HDR(clnt)->flags |= RFD(0) | WFD(0) | WRITABLE(0);
  129. clnt->pipe.fd = newfd;
  130. clnt->pipe.pipeid = handle->pipe.pipeid;
  131. clnt->pipe.nonblocking = PAL_FALSE;
  132. *client = clnt;
  133. #endif
  134. return 0;
  135. }
  136. static int pipe_connect(PAL_HANDLE* handle, PAL_NUM pipeid, int options) {
  137. int ret, fd;
  138. #if USE_PIPE_SYSCALL == 1
  139. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  140. #else
  141. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0);
  142. #endif
  143. if (IS_ERR(fd))
  144. return -PAL_ERROR_DENIED;
  145. struct sockaddr_un addr;
  146. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  147. return ret;
  148. ret = INLINE_SYSCALL(connect, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  149. if (IS_ERR(ret)) {
  150. INLINE_SYSCALL(close, 1, fd);
  151. switch (ERRNO(ret)) {
  152. case ECONNREFUSED:
  153. return -PAL_ERROR_STREAMNOTEXIST;
  154. case EINTR:
  155. return -PAL_ERROR_TRYAGAIN;
  156. default:
  157. return -PAL_ERROR_DENIED;
  158. }
  159. }
  160. #if USE_PIPE_SYSCALL == 1
  161. int pipes[4], tmp;
  162. INLINE_SYSCALL(pipe2, 2, &pipes[0], O_CLOEXEC | options);
  163. INLINE_SYSCALL(pipe2, 2, &pipes[2], O_CLOEXEC | options);
  164. tmp = pipes[3];
  165. pipes[3] = pipes[1];
  166. pipes[1] = tmp;
  167. struct msghdr hdr;
  168. struct iovec iov;
  169. char cbuf[sizeof(struct cmsghdr) + 2 * sizeof(int)];
  170. char b = 0;
  171. memset(&hdr, 0, sizeof(struct msghdr));
  172. hdr.msg_iov = &iov;
  173. hdr.msg_iovlen = 1;
  174. hdr.msg_control = cbuf;
  175. hdr.msg_controllen = sizeof(cbuf);
  176. iov.iov_base = &b;
  177. iov.iov_len = 1;
  178. struct cmsghdr* chdr = CMSG_FIRSTHDR(&hdr);
  179. chdr->cmsg_level = SOL_SOCKET;
  180. chdr->cmsg_type = SCM_RIGHTS;
  181. chdr->cmsg_len = CMSG_LEN(sizeof(int) * 2);
  182. memcpy(CMSG_DATA(chdr), &pipes[2], sizeof(int) * 2);
  183. ret = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, 0);
  184. INLINE_SYSCALL(close, 1, fd);
  185. INLINE_SYSCALL(close, 1, pipes[2]);
  186. INLINE_SYSCALL(close, 1, pipes[3]);
  187. if (IS_ERR(ret)) {
  188. INLINE_SYSCALL(close, 1, pipes[0]);
  189. INLINE_SYSCALL(close, 1, pipes[1]);
  190. return -PAL_ERROR_DENIED;
  191. }
  192. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  193. SET_HANDLE_TYPE(hdl, pipeprv);
  194. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1) | WRITABLE(1);
  195. hdl->pipeprv.fds[0] = pipes[0];
  196. hdl->pipeprv.fds[1] = pipes[1];
  197. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  198. #else
  199. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  200. SET_HANDLE_TYPE(hdl, pipe);
  201. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0) | WRITABLE(0);
  202. hdl->pipe.fd = fd;
  203. hdl->pipe.pipeid = pipeid;
  204. hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  205. #endif
  206. *handle = hdl;
  207. return 0;
  208. }
  209. static int pipe_private(PAL_HANDLE* handle, int options) {
  210. int ret, fds[2];
  211. #if USE_PIPE_SYSCALL == 1
  212. ret = INLINE_SYSCALL(pipe2, 2, fds, O_CLOEXEC | options);
  213. #else
  214. ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | options, 0, fds);
  215. #endif
  216. if (IS_ERR(ret))
  217. return -PAL_ERROR_DENIED;
  218. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  219. SET_HANDLE_TYPE(hdl, pipeprv);
  220. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(1) | WRITABLE(1);
  221. hdl->pipeprv.fds[0] = fds[0];
  222. hdl->pipeprv.fds[1] = fds[1];
  223. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  224. *handle = hdl;
  225. return 0;
  226. }
  227. /* 'open' operation of pipe stream. For each pipe stream, it is
  228. identified by a decimal number in URI. There could be two
  229. types: pipe and pipe.srv. They behave pretty much the same,
  230. except they are two ends of the pipe. */
  231. static int pipe_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  232. int create, int options) {
  233. if (!WITHIN_MASK(access, PAL_ACCESS_MASK) || !WITHIN_MASK(share, PAL_SHARE_MASK) ||
  234. !WITHIN_MASK(create, PAL_CREATE_MASK) || !WITHIN_MASK(options, PAL_OPTION_MASK))
  235. return -PAL_ERROR_INVAL;
  236. if (strcmp_static(type, "pipe") && !*uri)
  237. return pipe_private(handle, options);
  238. char* endptr;
  239. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  240. if (*endptr)
  241. return -PAL_ERROR_INVAL;
  242. if (strcmp_static(type, "pipe.srv"))
  243. return pipe_listen(handle, pipeid, options);
  244. if (strcmp_static(type, "pipe"))
  245. return pipe_connect(handle, pipeid, options);
  246. return -PAL_ERROR_INVAL;
  247. }
  248. /* 'read' operation of pipe stream. offset does not apply here. */
  249. static int64_t pipe_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  250. if (offset)
  251. return -PAL_ERROR_INVAL;
  252. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  253. !IS_HANDLE_TYPE(handle, pipe))
  254. return -PAL_ERROR_NOTCONNECTION;
  255. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] : handle->pipe.fd;
  256. int64_t bytes = 0;
  257. #if USE_PIPE_SYSCALL == 1
  258. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  259. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  260. } else {
  261. #endif
  262. #if PIPE_USE_SENDMSG_RECVMSG == 1
  263. struct msghdr hdr;
  264. struct iovec iov;
  265. iov.iov_base = buffer;
  266. iov.iov_len = len;
  267. hdr.msg_name = NULL;
  268. hdr.msg_namelen = 0;
  269. hdr.msg_iov = &iov;
  270. hdr.msg_iovlen = 1;
  271. hdr.msg_control = NULL;
  272. hdr.msg_controllen = 0;
  273. hdr.msg_flags = 0;
  274. bytes = INLINE_SYSCALL(recvmsg, 3, fd, &hdr, 0);
  275. #else
  276. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  277. #endif
  278. #if USE_PIPE_SYSCALL == 1
  279. }
  280. #endif
  281. if (IS_ERR(bytes))
  282. bytes = unix_to_pal_error(ERRNO(bytes));
  283. if (!bytes)
  284. return -PAL_ERROR_ENDOFSTREAM;
  285. return bytes;
  286. }
  287. /* 'write' operation of pipe stream. offset does not apply here. */
  288. static int64_t pipe_write(PAL_HANDLE handle, uint64_t offset, size_t len, const void* buffer) {
  289. if (offset)
  290. return -PAL_ERROR_INVAL;
  291. if (!IS_HANDLE_TYPE(handle, pipecli) && !IS_HANDLE_TYPE(handle, pipeprv) &&
  292. !IS_HANDLE_TYPE(handle, pipe))
  293. return -PAL_ERROR_NOTCONNECTION;
  294. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] : handle->pipe.fd;
  295. int64_t bytes = 0;
  296. #if USE_PIPE_SYSCALL == 1
  297. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  298. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  299. } else {
  300. #endif
  301. #if PIPE_USE_SENDMSG_RECVMSG == 1
  302. struct msghdr hdr;
  303. struct iovec iov;
  304. iov.iov_base = (void*)buffer;
  305. iov.iov_len = len;
  306. hdr.msg_name = NULL;
  307. hdr.msg_namelen = 0;
  308. hdr.msg_iov = &iov;
  309. hdr.msg_iovlen = 1;
  310. hdr.msg_control = NULL;
  311. hdr.msg_controllen = 0;
  312. hdr.msg_flags = 0;
  313. bytes = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, MSG_NOSIGNAL);
  314. #else
  315. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  316. #endif
  317. #if USE_PIPE_SYSCALL == 1
  318. }
  319. #endif
  320. PAL_FLG writable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) : WRITABLE(0);
  321. if (!IS_ERR(bytes) && (size_t)bytes == len)
  322. HANDLE_HDR(handle)->flags |= writable;
  323. else
  324. HANDLE_HDR(handle)->flags &= ~writable;
  325. if (IS_ERR(bytes))
  326. bytes = unix_to_pal_error(ERRNO(bytes));
  327. return bytes;
  328. }
  329. /* 'close' operation of pipe stream. */
  330. static int pipe_close(PAL_HANDLE handle) {
  331. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  332. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  333. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  334. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  335. }
  336. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  337. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  338. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  339. }
  340. return 0;
  341. }
  342. if (handle->pipe.fd != PAL_IDX_POISON) {
  343. INLINE_SYSCALL(close, 1, handle->pipe.fd);
  344. handle->pipe.fd = PAL_IDX_POISON;
  345. }
  346. return 0;
  347. }
  348. /* 'delete' operation of pipe stream. */
  349. static int pipe_delete(PAL_HANDLE handle, int access) {
  350. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  351. switch (access) {
  352. case 0:
  353. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  354. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  355. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  356. }
  357. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  358. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  359. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  360. }
  361. break;
  362. case PAL_DELETE_RD:
  363. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  364. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  365. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  366. }
  367. break;
  368. case PAL_DELETE_WR:
  369. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  370. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  371. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  372. }
  373. break;
  374. default:
  375. return -PAL_ERROR_INVAL;
  376. }
  377. }
  378. if (handle->pipe.fd == PAL_IDX_POISON)
  379. return 0;
  380. int shutdown;
  381. switch (access) {
  382. case 0:
  383. shutdown = SHUT_RDWR;
  384. break;
  385. case PAL_DELETE_RD:
  386. shutdown = SHUT_RD;
  387. break;
  388. case PAL_DELETE_WR:
  389. shutdown = SHUT_WR;
  390. break;
  391. default:
  392. return -PAL_ERROR_INVAL;
  393. }
  394. INLINE_SYSCALL(shutdown, 2, handle->pipe.fd, shutdown);
  395. return 0;
  396. }
  397. #ifndef FIONREAD
  398. #define FIONREAD 0x541B
  399. #endif
  400. static int pipe_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  401. int ret, val;
  402. if (handle->generic.fds[0] == PAL_IDX_POISON)
  403. return -PAL_ERROR_BADHANDLE;
  404. attr->handle_type = PAL_GET_TYPE(handle);
  405. if (attr->handle_type != pal_type_pipesrv) {
  406. ret = INLINE_SYSCALL(ioctl, 3, handle->generic.fds[0], FIONREAD, &val);
  407. if (IS_ERR(ret)) {
  408. return unix_to_pal_error(ERRNO(ret));
  409. }
  410. attr->pending_size = val;
  411. attr->writable = HANDLE_HDR(handle)->flags &
  412. (IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) : WRITABLE(0));
  413. } else {
  414. struct pollfd pfd = {.fd = handle->generic.fds[0], .events = POLLIN, .revents = 0};
  415. struct timespec tp = {0, 0};
  416. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  417. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  418. attr->pending_size = 0;
  419. attr->writable = PAL_FALSE;
  420. }
  421. struct pollfd pfd = {.fd = handle->generic.fds[0], .events = POLLIN, .revents = 0};
  422. struct timespec tp = {0, 0};
  423. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  424. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  425. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  426. attr->nonblocking =
  427. IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  428. return 0;
  429. }
  430. static int pipe_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  431. if (handle->generic.fds[0] == PAL_IDX_POISON)
  432. return -PAL_ERROR_BADHANDLE;
  433. int ret;
  434. PAL_BOL* nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv)
  435. ? &handle->pipeprv.nonblocking
  436. : &handle->pipe.nonblocking;
  437. if (attr->nonblocking != *nonblocking) {
  438. ret = INLINE_SYSCALL(fcntl, 3, handle->generic.fds[0], F_SETFL,
  439. attr->nonblocking ? O_NONBLOCK : 0);
  440. if (IS_ERR(ret))
  441. return unix_to_pal_error(ERRNO(ret));
  442. *nonblocking = attr->nonblocking;
  443. }
  444. return 0;
  445. }
  446. static int pipe_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  447. size_t old_count = count;
  448. int ret;
  449. const char* prefix = NULL;
  450. size_t prefix_len = 0;
  451. switch (PAL_GET_TYPE(handle)) {
  452. case pal_type_pipesrv:
  453. case pal_type_pipecli:
  454. prefix_len = 8;
  455. prefix = "pipe.srv";
  456. break;
  457. case pal_type_pipe:
  458. prefix_len = 4;
  459. prefix = "pipe";
  460. break;
  461. case pal_type_pipeprv:
  462. default:
  463. return -PAL_ERROR_INVAL;
  464. }
  465. if (prefix_len >= count)
  466. return -PAL_ERROR_OVERFLOW;
  467. memcpy(buffer, prefix, prefix_len);
  468. buffer[prefix_len] = ':';
  469. buffer += prefix_len + 1;
  470. count -= prefix_len + 1;
  471. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  472. if (buffer[ret - 1] != '\n') {
  473. memset(buffer, 0, count);
  474. return -PAL_ERROR_OVERFLOW;
  475. }
  476. buffer[ret - 1] = 0;
  477. buffer += ret - 1;
  478. count -= ret - 1;
  479. return old_count - count;
  480. }
  481. struct handle_ops pipe_ops = {
  482. .getname = &pipe_getname,
  483. .open = &pipe_open,
  484. .waitforclient = &pipe_waitforclient,
  485. .read = &pipe_read,
  486. .write = &pipe_write,
  487. .close = &pipe_close,
  488. .delete = &pipe_delete,
  489. .attrquerybyhdl = &pipe_attrquerybyhdl,
  490. .attrsetbyhdl = &pipe_attrsetbyhdl,
  491. };
  492. struct handle_ops pipeprv_ops = {
  493. .open = &pipe_open,
  494. .read = &pipe_read,
  495. .write = &pipe_write,
  496. .close = &pipe_close,
  497. .attrquerybyhdl = &pipe_attrquerybyhdl,
  498. .attrsetbyhdl = &pipe_attrsetbyhdl,
  499. };