db_pipes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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.nonblocking = PAL_FALSE;
  143. clnt->pipe.pipeid = handle->pipe.pipeid;
  144. *client = clnt;
  145. #endif
  146. return 0;
  147. }
  148. static int pipe_connect (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
  149. {
  150. int ret, fd;
  151. #if USE_PIPE_SYSCALL == 1
  152. fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
  153. #else
  154. options = HOST_SOCKET_OPTIONS(options);
  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, sizeof(addr.sun_path) - 1);
  164. if (IS_ERR(ret)) {
  165. INLINE_SYSCALL(close, 1, fd);
  166. switch (ERRNO(ret)) {
  167. case ECONNREFUSED:
  168. return -PAL_ERROR_STREAMNOTEXIST;
  169. case EINTR:
  170. return -PAL_ERROR_TRYAGAIN;
  171. default:
  172. return -PAL_ERROR_DENIED;
  173. }
  174. }
  175. #if USE_PIPE_SYSCALL == 1
  176. int pipes[4], tmp;
  177. options = HOST_SOCKET_OPTIONS(options);
  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[CMSG_LEN(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 & O_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.nonblocking = (options & O_NONBLOCK) ?
  222. PAL_TRUE : PAL_FALSE;
  223. #endif
  224. *handle = hdl;
  225. return 0;
  226. }
  227. static int pipe_private (PAL_HANDLE * handle, int options)
  228. {
  229. int ret, fds[2];
  230. #if USE_PIPE_SYSCALL == 1
  231. options = HOST_OPTIONS(options);
  232. ret = INLINE_SYSCALL(pipe2, 2, fds, O_CLOEXEC|options);
  233. #else
  234. options = HOST_SOCKET_OPTIONS(options);
  235. ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
  236. SOCK_STREAM|SOCK_CLOEXEC|options, 0, fds);
  237. #endif
  238. if (IS_ERR(ret))
  239. return -PAL_ERROR_DENIED;
  240. PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
  241. SET_HANDLE_TYPE(hdl, pipeprv);
  242. hdl->__in.flags |= RFD(0)|WFD(1)|WRITEABLE(1);
  243. hdl->pipeprv.fds[0] = fds[0];
  244. hdl->pipeprv.fds[1] = fds[1];
  245. hdl->pipeprv.nonblocking = (options & O_NONBLOCK) ?
  246. PAL_TRUE : PAL_FALSE;
  247. *handle = hdl;
  248. return 0;
  249. }
  250. /* 'open' operation of pipe stream. For each pipe stream, it is
  251. identified by a decimal number in URI. There could be two
  252. types: pipe and pipe.srv. They behave pretty much the same,
  253. except they are two ends of the pipe. */
  254. static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
  255. int access, int share, int create, int options)
  256. {
  257. if (strpartcmp_static(type, "pipe:") && !*uri)
  258. return pipe_private(handle, options);
  259. char * endptr;
  260. PAL_NUM pipeid = strtol(uri, &endptr, 10);
  261. if (*endptr)
  262. return -PAL_ERROR_INVAL;
  263. options = HOST_OPTIONS(options & PAL_OPTION_MASK);
  264. if (strpartcmp_static(type, "pipe.srv:"))
  265. return pipe_listen(handle, pipeid, options);
  266. if (strpartcmp_static(type, "pipe:"))
  267. return pipe_connect(handle, pipeid, options);
  268. return -PAL_ERROR_INVAL;
  269. }
  270. /* 'read' operation of pipe stream. offset does not apply here. */
  271. static int pipe_read (PAL_HANDLE handle, int offset, int len,
  272. void * buffer)
  273. {
  274. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  275. !IS_HANDLE_TYPE(handle, pipeprv) &&
  276. !IS_HANDLE_TYPE(handle, pipe))
  277. return -PAL_ERROR_NOTCONNECTION;
  278. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
  279. handle->pipe.fd;
  280. int bytes = 0;
  281. #if USE_PIPE_SYSCALL == 1
  282. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  283. bytes = INLINE_SYSCALL(read, 3, fd, buffer, len);
  284. } else {
  285. #endif
  286. struct msghdr hdr;
  287. struct iovec iov;
  288. iov.iov_base = buffer;
  289. iov.iov_len = len;
  290. hdr.msg_name = NULL;
  291. hdr.msg_namelen = 0;
  292. hdr.msg_iov = &iov;
  293. hdr.msg_iovlen = 1;
  294. hdr.msg_control = NULL;
  295. hdr.msg_controllen = 0;
  296. hdr.msg_flags = 0;
  297. bytes = INLINE_SYSCALL(recvmsg, 3, fd, &hdr, 0);
  298. #if USE_PIPE_SYSCALL == 1
  299. }
  300. #endif
  301. if (IS_ERR(bytes))
  302. switch(ERRNO(bytes)) {
  303. case EWOULDBLOCK:
  304. return-PAL_ERROR_TRYAGAIN;
  305. case EINTR:
  306. return -PAL_ERROR_INTERRUPTED;
  307. default:
  308. return -PAL_ERROR_DENIED;
  309. }
  310. if (!bytes)
  311. return -PAL_ERROR_ENDOFSTREAM;
  312. return bytes;
  313. }
  314. /* 'write' operation of pipe stream. offset does not apply here. */
  315. static int pipe_write (PAL_HANDLE handle, int offset, int len,
  316. const void * buffer)
  317. {
  318. if (!IS_HANDLE_TYPE(handle, pipecli) &&
  319. !IS_HANDLE_TYPE(handle, pipeprv) &&
  320. !IS_HANDLE_TYPE(handle, pipe))
  321. return -PAL_ERROR_NOTCONNECTION;
  322. int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
  323. handle->pipe.fd;
  324. int bytes = 0;
  325. #if USE_PIPE_SYSCALL == 1
  326. if (IS_HANDLE_TYPE(handle, pipeprv)) {
  327. bytes = INLINE_SYSCALL(write, 3, fd, buffer, len);
  328. } else {
  329. #endif
  330. struct msghdr hdr;
  331. struct iovec iov;
  332. iov.iov_base = (void *) buffer;
  333. iov.iov_len = len;
  334. hdr.msg_name = NULL;
  335. hdr.msg_namelen = 0;
  336. hdr.msg_iov = &iov;
  337. hdr.msg_iovlen = 1;
  338. hdr.msg_control = NULL;
  339. hdr.msg_controllen = 0;
  340. hdr.msg_flags = 0;
  341. bytes = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, MSG_NOSIGNAL);
  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->__in.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->__in.flags |= writeable;
  359. else
  360. handle->__in.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. static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  434. {
  435. int ret, val;
  436. if (handle->__in.fds[0] == PAL_IDX_POISON)
  437. return -PAL_ERROR_BADHANDLE;
  438. ret = INLINE_SYSCALL(ioctl, 3, handle->__in.fds[0], FIONREAD, &val);
  439. if (IS_ERR(ret))
  440. return unix_to_pal_error(ERRNO(ret));
  441. attr->handle_type = pal_type_pipe;
  442. attr->disconnected = handle->__in.flags & ERROR(0);
  443. attr->nonblocking = (handle->__in.type == pal_type_pipeprv) ?
  444. handle->pipeprv.nonblocking : handle->pipe.nonblocking;
  445. attr->readable = val > 0;
  446. if (PAL_GET_TYPE(handle) == pal_type_pipeprv)
  447. attr->writeable = handle->__in.flags & WRITEABLE(1);
  448. else
  449. attr->writeable = handle->__in.flags & WRITEABLE(0);
  450. attr->pending_size = val;
  451. return 0;
  452. }
  453. static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  454. {
  455. if (handle->__in.fds[0] == PAL_IDX_POISON)
  456. return -PAL_ERROR_BADHANDLE;
  457. int ret;
  458. PAL_BOL * nonblocking = (handle->__in.type == pal_type_pipeprv) ?
  459. &handle->pipeprv.nonblocking :
  460. &handle->pipe.nonblocking;
  461. if (attr->nonblocking != *nonblocking) {
  462. ret = INLINE_SYSCALL(fcntl, 3, handle->__in.fds[0], F_SETFL,
  463. *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. };