db_pipes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. *client = clnt;
  141. #endif
  142. return 0;
  143. }
  144. static int pipe_connect (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
  145. {
  146. int ret, fd;
  147. #if USE_PIPE_SYSCALL == 1
  148. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
  149. #else
  150. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|options,
  151. 0);
  152. #endif
  153. if (IS_ERR(fd))
  154. return -PAL_ERROR_DENIED;
  155. struct sockaddr_un addr;
  156. if ((ret = pipe_addr(pipeid, &addr)) < 0)
  157. return ret;
  158. ret = INLINE_SYSCALL(connect, 3, fd, &addr, sizeof(addr.sun_path) - 1);
  159. if (IS_ERR(ret)) {
  160. INLINE_SYSCALL(close, 1, fd);
  161. switch (ERRNO(ret)) {
  162. case ECONNREFUSED:
  163. return -PAL_ERROR_STREAMNOTEXIST;
  164. case EINTR:
  165. return -PAL_ERROR_TRYAGAIN;
  166. default:
  167. return -PAL_ERROR_DENIED;
  168. }
  169. }
  170. #if USE_PIPE_SYSCALL == 1
  171. int pipes[4], tmp;
  172. INLINE_SYSCALL(pipe2, 2, &pipes[0], O_CLOEXEC|options);
  173. INLINE_SYSCALL(pipe2, 2, &pipes[2], O_CLOEXEC|options);
  174. tmp = pipes[3];
  175. pipes[3] = pipes[1];
  176. pipes[1] = tmp;
  177. struct msghdr hdr;
  178. struct iovec iov;
  179. char cbuf[sizeof(struct cmsghdr) + 2 * sizeof(int)];
  180. char b = 0;
  181. memset(&hdr, 0, sizeof(struct msghdr));
  182. hdr.msg_iov = &iov;
  183. hdr.msg_iovlen = 1;
  184. hdr.msg_control = cbuf;
  185. hdr.msg_controllen = sizeof(cbuf);
  186. iov.iov_base = &b;
  187. iov.iov_len = 1;
  188. struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
  189. chdr->cmsg_level = SOL_SOCKET;
  190. chdr->cmsg_type = SCM_RIGHTS;
  191. chdr->cmsg_len = CMSG_LEN(sizeof(int) * 2);
  192. memcpy(CMSG_DATA(chdr), &pipes[2], sizeof(int) * 2);
  193. ret = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, 0);
  194. INLINE_SYSCALL(close, 1, fd);
  195. INLINE_SYSCALL(close, 1, pipes[2]);
  196. INLINE_SYSCALL(close, 1, pipes[3]);
  197. if (IS_ERR(ret)) {
  198. INLINE_SYSCALL(close, 1, pipes[0]);
  199. INLINE_SYSCALL(close, 1, pipes[1]);
  200. return -PAL_ERROR_DENIED;
  201. }
  202. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  203. SET_HANDLE_TYPE(hdl, pipeprv);
  204. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(1)|WRITEABLE(1);
  205. hdl->pipeprv.fds[0] = pipes[0];
  206. hdl->pipeprv.fds[1] = pipes[1];
  207. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ?
  208. PAL_TRUE : PAL_FALSE;
  209. #else
  210. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
  211. SET_HANDLE_TYPE(hdl, pipe);
  212. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  213. hdl->pipe.fd = fd;
  214. hdl->pipe.pipeid = pipeid;
  215. hdl->pipe.nonblocking = (options & PAL_OPTION_NONBLOCK) ?
  216. PAL_TRUE : PAL_FALSE;
  217. #endif
  218. *handle = hdl;
  219. return 0;
  220. }
  221. static int pipe_private (PAL_HANDLE * handle, int options)
  222. {
  223. int ret, fds[2];
  224. #if USE_PIPE_SYSCALL == 1
  225. ret = INLINE_SYSCALL(pipe2, 2, fds, O_CLOEXEC|options);
  226. #else
  227. ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
  228. SOCK_STREAM|SOCK_CLOEXEC|options, 0, fds);
  229. #endif
  230. if (IS_ERR(ret))
  231. return -PAL_ERROR_DENIED;
  232. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  233. SET_HANDLE_TYPE(hdl, pipeprv);
  234. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(1)|WRITEABLE(1);
  235. hdl->pipeprv.fds[0] = fds[0];
  236. hdl->pipeprv.fds[1] = fds[1];
  237. hdl->pipeprv.nonblocking = (options & PAL_OPTION_NONBLOCK) ?
  238. PAL_TRUE : PAL_FALSE;
  239. *handle = hdl;
  240. return 0;
  241. }
  242. /* 'open' operation of pipe stream. For each pipe stream, it is
  243. identified by a decimal number in URI. There could be two
  244. types: pipe and pipe.srv. They behave pretty much the same,
  245. except they are two ends of the pipe. */
  246. static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
  247. int access, int share, int create, int options)
  248. {
  249. options &= PAL_OPTION_MASK;
  250. if (strpartcmp_static(type, "pipe:") && !*uri)
  251. return pipe_private(handle, options);
  252. char * endptr;
  253. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  254. if (*endptr)
  255. return -PAL_ERROR_INVAL;
  256. if (strpartcmp_static(type, "pipe.srv:"))
  257. return pipe_listen(handle, pipeid, options);
  258. if (strpartcmp_static(type, "pipe:"))
  259. return pipe_connect(handle, pipeid, options);
  260. return -PAL_ERROR_INVAL;
  261. }
  262. /* 'read' operation of pipe stream. offset does not apply here. */
  263. static int pipe_read (PAL_HANDLE handle, int offset, int len,
  264. void * buffer)
  265. {
  266. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  267. !IS_HANDLE_TYPE(handle, pipeprv) &&
  268. !IS_HANDLE_TYPE(handle, pipe))
  269. return -PAL_ERROR_NOTCONNECTION;
  270. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
  271. handle->pipe.fd;
  272. int bytes = 0;
  273. #if USE_PIPE_SYSCALL == 1
  274. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  275. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  276. } else {
  277. #endif
  278. #if PIPE_USE_SENDMSG_RECVMSG == 1
  279. struct msghdr hdr;
  280. struct iovec iov;
  281. iov.iov_base = buffer;
  282. iov.iov_len = len;
  283. hdr.msg_name = NULL;
  284. hdr.msg_namelen = 0;
  285. hdr.msg_iov = &iov;
  286. hdr.msg_iovlen = 1;
  287. hdr.msg_control = NULL;
  288. hdr.msg_controllen = 0;
  289. hdr.msg_flags = 0;
  290. bytes = INLINE_SYSCALL(recvmsg, 3, fd, &hdr, 0);
  291. #else
  292. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  293. #endif
  294. #if USE_PIPE_SYSCALL == 1
  295. }
  296. #endif
  297. if (IS_ERR(bytes))
  298. switch(ERRNO(bytes)) {
  299. case EWOULDBLOCK:
  300. return-PAL_ERROR_TRYAGAIN;
  301. case EINTR:
  302. return -PAL_ERROR_INTERRUPTED;
  303. default:
  304. return -PAL_ERROR_DENIED;
  305. }
  306. if (!bytes)
  307. return -PAL_ERROR_ENDOFSTREAM;
  308. return bytes;
  309. }
  310. /* 'write' operation of pipe stream. offset does not apply here. */
  311. static int pipe_write (PAL_HANDLE handle, int offset, int len,
  312. const void * buffer)
  313. {
  314. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  315. !IS_HANDLE_TYPE(handle, pipeprv) &&
  316. !IS_HANDLE_TYPE(handle, pipe))
  317. return -PAL_ERROR_NOTCONNECTION;
  318. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
  319. handle->pipe.fd;
  320. int bytes = 0;
  321. #if USE_PIPE_SYSCALL == 1
  322. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  323. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  324. } else {
  325. #endif
  326. #if PIPE_USE_SENDMSG_RECVMSG == 1
  327. struct msghdr hdr;
  328. struct iovec iov;
  329. iov.iov_base = (void *) buffer;
  330. iov.iov_len = len;
  331. hdr.msg_name = NULL;
  332. hdr.msg_namelen = 0;
  333. hdr.msg_iov = &iov;
  334. hdr.msg_iovlen = 1;
  335. hdr.msg_control = NULL;
  336. hdr.msg_controllen = 0;
  337. hdr.msg_flags = 0;
  338. bytes = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, MSG_NOSIGNAL);
  339. #else
  340. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  341. #endif
  342. #if USE_PIPE_SYSCALL == 1
  343. }
  344. #endif
  345. PAL_FLG writeable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITEABLE(1) :
  346. WRITEABLE(0);
  347. if (IS_ERR(bytes))
  348. switch(ERRNO(bytes)) {
  349. case EWOULDBLOCK:
  350. HANDLE_HDR(handle)->flags &= ~writeable;
  351. return-PAL_ERROR_TRYAGAIN;
  352. case EINTR:
  353. return -PAL_ERROR_INTERRUPTED;
  354. default:
  355. return -PAL_ERROR_DENIED;
  356. }
  357. if (bytes == len)
  358. HANDLE_HDR(handle)->flags |= writeable;
  359. else
  360. HANDLE_HDR(handle)->flags &= ~writeable;
  361. return bytes;
  362. }
  363. /* 'close' operation of pipe stream. */
  364. static int pipe_close (PAL_HANDLE handle)
  365. {
  366. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  367. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  368. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  369. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  370. }
  371. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  372. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  373. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  374. }
  375. return 0;
  376. }
  377. if (handle->pipe.fd != PAL_IDX_POISON) {
  378. INLINE_SYSCALL(close, 1, handle->pipe.fd);
  379. handle->pipe.fd = PAL_IDX_POISON;
  380. }
  381. return 0;
  382. }
  383. /* 'delete' operation of pipe stream. */
  384. static int pipe_delete (PAL_HANDLE handle, int access)
  385. {
  386. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  387. switch (access) {
  388. case 0:
  389. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  390. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  391. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  392. }
  393. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  394. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  395. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  396. }
  397. break;
  398. case PAL_DELETE_RD:
  399. if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
  400. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
  401. handle->pipeprv.fds[0] = PAL_IDX_POISON;
  402. }
  403. break;
  404. case PAL_DELETE_WR:
  405. if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
  406. INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
  407. handle->pipeprv.fds[1] = PAL_IDX_POISON;
  408. }
  409. break;
  410. default:
  411. return -PAL_ERROR_INVAL;
  412. }
  413. }
  414. if (handle->pipe.fd == PAL_IDX_POISON)
  415. return 0;
  416. int shutdown;
  417. switch (access) {
  418. case 0:
  419. shutdown = SHUT_RDWR;
  420. break;
  421. case PAL_DELETE_RD:
  422. shutdown = SHUT_RD;
  423. break;
  424. case PAL_DELETE_WR:
  425. shutdown = SHUT_WR;
  426. break;
  427. default:
  428. return -PAL_ERROR_INVAL;
  429. }
  430. INLINE_SYSCALL(shutdown, 2, handle->pipe.fd, shutdown);
  431. return 0;
  432. }
  433. #ifndef FIONREAD
  434. # define FIONREAD 0x541B
  435. #endif
  436. static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  437. {
  438. int ret, val;
  439. if (handle->generic.fds[0] == PAL_IDX_POISON)
  440. return -PAL_ERROR_BADHANDLE;
  441. attr->handle_type = PAL_GET_TYPE(handle);
  442. if (attr->handle_type != pal_type_pipesrv) {
  443. ret = INLINE_SYSCALL(ioctl, 3, handle->generic.fds[0], FIONREAD, &val);
  444. if (IS_ERR(ret)) {
  445. return unix_to_pal_error(ERRNO(ret));
  446. }
  447. attr->pending_size = val;
  448. attr->writeable = HANDLE_HDR(handle)->flags & (
  449. IS_HANDLE_TYPE(handle, pipeprv) ? WRITEABLE(1) : WRITEABLE(0));
  450. } else {
  451. struct pollfd pfd = { .fd = handle->generic.fds[0], .events = POLLIN, .revents = 0 };
  452. struct timespec tp = { 0, 0 };
  453. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  454. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  455. attr->pending_size = 0;
  456. attr->writeable = PAL_FALSE;
  457. }
  458. struct pollfd pfd = { .fd = handle->generic.fds[0], .events = POLLIN, .revents = 0 };
  459. struct timespec tp = { 0, 0 };
  460. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  461. attr->readable = (ret == 1 && pfd.revents == POLLIN);
  462. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  463. attr->nonblocking = IS_HANDLE_TYPE(handle, pipeprv) ?
  464. handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  465. return 0;
  466. }
  467. static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  468. {
  469. if (handle->generic.fds[0] == PAL_IDX_POISON)
  470. return -PAL_ERROR_BADHANDLE;
  471. int ret;
  472. PAL_BOL * nonblocking = (HANDLE_HDR(handle)->type == pal_type_pipeprv) ?
  473. &handle->pipeprv.nonblocking :
  474. &handle->pipe.nonblocking;
  475. if (attr->nonblocking != *nonblocking) {
  476. ret = INLINE_SYSCALL(fcntl, 3, handle->generic.fds[0], F_SETFL,
  477. attr->nonblocking ? O_NONBLOCK : 0);
  478. if (IS_ERR(ret))
  479. return unix_to_pal_error(ERRNO(ret));
  480. *nonblocking = attr->nonblocking;
  481. }
  482. return 0;
  483. }
  484. static int pipe_getname (PAL_HANDLE handle, char * buffer, int count)
  485. {
  486. int old_count = count;
  487. int ret;
  488. const char * prefix = NULL;
  489. int prefix_len = 0;
  490. switch (PAL_GET_TYPE(handle)) {
  491. case pal_type_pipesrv:
  492. case pal_type_pipecli:
  493. prefix_len = 8;
  494. prefix = "pipe.srv";
  495. break;
  496. case pal_type_pipe:
  497. prefix_len = 4;
  498. prefix = "pipe";
  499. break;
  500. case pal_type_pipeprv:
  501. default:
  502. return -PAL_ERROR_INVAL;
  503. }
  504. if (prefix_len >= count)
  505. return -PAL_ERROR_OVERFLOW;
  506. memcpy(buffer, prefix, prefix_len);
  507. buffer[prefix_len] = ':';
  508. buffer += prefix_len + 1;
  509. count -= prefix_len + 1;
  510. ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
  511. if (buffer[ret - 1] != '\n') {
  512. memset(buffer, 0, count);
  513. return -PAL_ERROR_OVERFLOW;
  514. }
  515. buffer[ret - 1] = 0;
  516. buffer += ret - 1;
  517. count -= ret - 1;
  518. return old_count - count;
  519. }
  520. struct handle_ops pipe_ops = {
  521. .getname = &pipe_getname,
  522. .open = &pipe_open,
  523. .waitforclient = &pipe_waitforclient,
  524. .read = &pipe_read,
  525. .write = &pipe_write,
  526. .close = &pipe_close,
  527. .delete = &pipe_delete,
  528. .attrquerybyhdl = &pipe_attrquerybyhdl,
  529. .attrsetbyhdl = &pipe_attrsetbyhdl,
  530. };
  531. struct handle_ops pipeprv_ops = {
  532. .open = &pipe_open,
  533. .read = &pipe_read,
  534. .write = &pipe_write,
  535. .close = &pipe_close,
  536. .attrquerybyhdl = &pipe_attrquerybyhdl,
  537. .attrsetbyhdl = &pipe_attrsetbyhdl,
  538. };