db_pipes.c 18 KB

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