db_pipes.c 18 KB

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