db_pipes.c 18 KB

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