sys_pipe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /* $OpenBSD: sys_pipe.c,v 1.69 2015/02/10 21:56:10 miod Exp $ */
  2. /*
  3. * Copyright (c) 1996 John S. Dyson
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Absolutely no warranty of function or purpose is made by the author
  16. * John S. Dyson.
  17. * 4. Modifications may be freely made to this file if the above conditions
  18. * are met.
  19. */
  20. /*
  21. * This file contains a high-performance replacement for the socket-based
  22. * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
  23. * all features of sockets, but does do everything that pipes normally
  24. * do.
  25. */
  26. #include <sys/param.h>
  27. #include <sys/systm.h>
  28. #include <sys/proc.h>
  29. #include <sys/file.h>
  30. #include <sys/filedesc.h>
  31. #include <sys/pool.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/stat.h>
  34. #include <sys/signalvar.h>
  35. #include <sys/mount.h>
  36. #include <sys/syscallargs.h>
  37. #include <sys/event.h>
  38. #include <sys/lock.h>
  39. #include <sys/poll.h>
  40. #include <uvm/uvm_extern.h>
  41. #include <sys/pipe.h>
  42. /*
  43. * interfaces to the outside world
  44. */
  45. int pipe_read(struct file *, off_t *, struct uio *, struct ucred *);
  46. int pipe_write(struct file *, off_t *, struct uio *, struct ucred *);
  47. int pipe_close(struct file *, struct proc *);
  48. int pipe_poll(struct file *, int events, struct proc *);
  49. int pipe_kqfilter(struct file *fp, struct knote *kn);
  50. int pipe_ioctl(struct file *, u_long, caddr_t, struct proc *);
  51. int pipe_stat(struct file *fp, struct stat *ub, struct proc *p);
  52. static struct fileops pipeops = {
  53. pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
  54. pipe_stat, pipe_close
  55. };
  56. void filt_pipedetach(struct knote *kn);
  57. int filt_piperead(struct knote *kn, long hint);
  58. int filt_pipewrite(struct knote *kn, long hint);
  59. struct filterops pipe_rfiltops =
  60. { 1, NULL, filt_pipedetach, filt_piperead };
  61. struct filterops pipe_wfiltops =
  62. { 1, NULL, filt_pipedetach, filt_pipewrite };
  63. /*
  64. * Default pipe buffer size(s), this can be kind-of large now because pipe
  65. * space is pageable. The pipe code will try to maintain locality of
  66. * reference for performance reasons, so small amounts of outstanding I/O
  67. * will not wipe the cache.
  68. */
  69. #define MINPIPESIZE (PIPE_SIZE/3)
  70. /*
  71. * Limit the number of "big" pipes
  72. */
  73. #define LIMITBIGPIPES 32
  74. int nbigpipe;
  75. static int amountpipekva;
  76. struct pool pipe_pool;
  77. int dopipe(struct proc *, int *, int);
  78. void pipeclose(struct pipe *);
  79. void pipe_free_kmem(struct pipe *);
  80. int pipe_create(struct pipe *);
  81. int pipelock(struct pipe *);
  82. void pipeunlock(struct pipe *);
  83. void pipeselwakeup(struct pipe *);
  84. int pipespace(struct pipe *, u_int);
  85. /*
  86. * The pipe system call for the DTYPE_PIPE type of pipes
  87. */
  88. int
  89. sys_pipe(struct proc *p, void *v, register_t *retval)
  90. {
  91. struct sys_pipe_args /* {
  92. syscallarg(int *) fdp;
  93. } */ *uap = v;
  94. return (dopipe(p, SCARG(uap, fdp), 0));
  95. }
  96. int
  97. sys_pipe2(struct proc *p, void *v, register_t *retval)
  98. {
  99. struct sys_pipe2_args /* {
  100. syscallarg(int *) fdp;
  101. syscallarg(int) flags;
  102. } */ *uap = v;
  103. if (SCARG(uap, flags) & ~(O_CLOEXEC | FNONBLOCK))
  104. return (EINVAL);
  105. return (dopipe(p, SCARG(uap, fdp), SCARG(uap, flags)));
  106. }
  107. int
  108. dopipe(struct proc *p, int *ufds, int flags)
  109. {
  110. struct filedesc *fdp = p->p_fd;
  111. struct file *rf, *wf;
  112. struct pipe *rpipe, *wpipe = NULL;
  113. int fds[2], error;
  114. rpipe = pool_get(&pipe_pool, PR_WAITOK);
  115. error = pipe_create(rpipe);
  116. if (error != 0)
  117. goto free1;
  118. wpipe = pool_get(&pipe_pool, PR_WAITOK);
  119. error = pipe_create(wpipe);
  120. if (error != 0)
  121. goto free1;
  122. fdplock(fdp);
  123. error = falloc(p, &rf, &fds[0]);
  124. if (error != 0)
  125. goto free2;
  126. rf->f_flag = FREAD | FWRITE | (flags & FNONBLOCK);
  127. rf->f_type = DTYPE_PIPE;
  128. rf->f_data = rpipe;
  129. rf->f_ops = &pipeops;
  130. error = falloc(p, &wf, &fds[1]);
  131. if (error != 0)
  132. goto free3;
  133. wf->f_flag = FREAD | FWRITE | (flags & FNONBLOCK);
  134. wf->f_type = DTYPE_PIPE;
  135. wf->f_data = wpipe;
  136. wf->f_ops = &pipeops;
  137. if (flags & O_CLOEXEC) {
  138. fdp->fd_ofileflags[fds[0]] |= UF_EXCLOSE;
  139. fdp->fd_ofileflags[fds[1]] |= UF_EXCLOSE;
  140. }
  141. rpipe->pipe_peer = wpipe;
  142. wpipe->pipe_peer = rpipe;
  143. FILE_SET_MATURE(rf, p);
  144. FILE_SET_MATURE(wf, p);
  145. error = copyout(fds, ufds, sizeof(fds));
  146. if (error != 0) {
  147. fdrelease(p, fds[0]);
  148. fdrelease(p, fds[1]);
  149. }
  150. fdpunlock(fdp);
  151. return (error);
  152. free3:
  153. fdremove(fdp, fds[0]);
  154. closef(rf, p);
  155. rpipe = NULL;
  156. free2:
  157. fdpunlock(fdp);
  158. free1:
  159. pipeclose(wpipe);
  160. pipeclose(rpipe);
  161. return (error);
  162. }
  163. /*
  164. * Allocate kva for pipe circular buffer, the space is pageable.
  165. * This routine will 'realloc' the size of a pipe safely, if it fails
  166. * it will retain the old buffer.
  167. * If it fails it will return ENOMEM.
  168. */
  169. int
  170. pipespace(struct pipe *cpipe, u_int size)
  171. {
  172. caddr_t buffer;
  173. buffer = km_alloc(size, &kv_any, &kp_pageable, &kd_waitok);
  174. if (buffer == NULL) {
  175. return (ENOMEM);
  176. }
  177. /* free old resources if we are resizing */
  178. pipe_free_kmem(cpipe);
  179. cpipe->pipe_buffer.buffer = buffer;
  180. cpipe->pipe_buffer.size = size;
  181. cpipe->pipe_buffer.in = 0;
  182. cpipe->pipe_buffer.out = 0;
  183. cpipe->pipe_buffer.cnt = 0;
  184. amountpipekva += cpipe->pipe_buffer.size;
  185. return (0);
  186. }
  187. /*
  188. * initialize and allocate VM and memory for pipe
  189. */
  190. int
  191. pipe_create(struct pipe *cpipe)
  192. {
  193. int error;
  194. /* so pipe_free_kmem() doesn't follow junk pointer */
  195. cpipe->pipe_buffer.buffer = NULL;
  196. /*
  197. * protect so pipeclose() doesn't follow a junk pointer
  198. * if pipespace() fails.
  199. */
  200. memset(&cpipe->pipe_sel, 0, sizeof(cpipe->pipe_sel));
  201. cpipe->pipe_state = 0;
  202. cpipe->pipe_peer = NULL;
  203. cpipe->pipe_busy = 0;
  204. error = pipespace(cpipe, PIPE_SIZE);
  205. if (error != 0)
  206. return (error);
  207. getnanotime(&cpipe->pipe_ctime);
  208. cpipe->pipe_atime = cpipe->pipe_ctime;
  209. cpipe->pipe_mtime = cpipe->pipe_ctime;
  210. cpipe->pipe_pgid = NO_PID;
  211. return (0);
  212. }
  213. /*
  214. * lock a pipe for I/O, blocking other access
  215. */
  216. int
  217. pipelock(struct pipe *cpipe)
  218. {
  219. int error;
  220. while (cpipe->pipe_state & PIPE_LOCK) {
  221. cpipe->pipe_state |= PIPE_LWANT;
  222. if ((error = tsleep(cpipe, PRIBIO|PCATCH, "pipelk", 0)))
  223. return error;
  224. }
  225. cpipe->pipe_state |= PIPE_LOCK;
  226. return 0;
  227. }
  228. /*
  229. * unlock a pipe I/O lock
  230. */
  231. void
  232. pipeunlock(struct pipe *cpipe)
  233. {
  234. cpipe->pipe_state &= ~PIPE_LOCK;
  235. if (cpipe->pipe_state & PIPE_LWANT) {
  236. cpipe->pipe_state &= ~PIPE_LWANT;
  237. wakeup(cpipe);
  238. }
  239. }
  240. void
  241. pipeselwakeup(struct pipe *cpipe)
  242. {
  243. if (cpipe->pipe_state & PIPE_SEL) {
  244. cpipe->pipe_state &= ~PIPE_SEL;
  245. selwakeup(&cpipe->pipe_sel);
  246. } else
  247. KNOTE(&cpipe->pipe_sel.si_note, 0);
  248. if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_pgid != NO_PID)
  249. gsignal(cpipe->pipe_pgid, SIGIO);
  250. }
  251. /* ARGSUSED */
  252. int
  253. pipe_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
  254. {
  255. struct pipe *rpipe = (struct pipe *) fp->f_data;
  256. int error;
  257. int nread = 0;
  258. int size;
  259. error = pipelock(rpipe);
  260. if (error)
  261. return (error);
  262. ++rpipe->pipe_busy;
  263. while (uio->uio_resid) {
  264. /*
  265. * normal pipe buffer receive
  266. */
  267. if (rpipe->pipe_buffer.cnt > 0) {
  268. size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
  269. if (size > rpipe->pipe_buffer.cnt)
  270. size = rpipe->pipe_buffer.cnt;
  271. if (size > uio->uio_resid)
  272. size = uio->uio_resid;
  273. error = uiomovei(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
  274. size, uio);
  275. if (error) {
  276. break;
  277. }
  278. rpipe->pipe_buffer.out += size;
  279. if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
  280. rpipe->pipe_buffer.out = 0;
  281. rpipe->pipe_buffer.cnt -= size;
  282. /*
  283. * If there is no more to read in the pipe, reset
  284. * its pointers to the beginning. This improves
  285. * cache hit stats.
  286. */
  287. if (rpipe->pipe_buffer.cnt == 0) {
  288. rpipe->pipe_buffer.in = 0;
  289. rpipe->pipe_buffer.out = 0;
  290. }
  291. nread += size;
  292. } else {
  293. /*
  294. * detect EOF condition
  295. * read returns 0 on EOF, no need to set error
  296. */
  297. if (rpipe->pipe_state & PIPE_EOF)
  298. break;
  299. /*
  300. * If the "write-side" has been blocked, wake it up now.
  301. */
  302. if (rpipe->pipe_state & PIPE_WANTW) {
  303. rpipe->pipe_state &= ~PIPE_WANTW;
  304. wakeup(rpipe);
  305. }
  306. /*
  307. * Break if some data was read.
  308. */
  309. if (nread > 0)
  310. break;
  311. /*
  312. * Unlock the pipe buffer for our remaining processing.
  313. * We will either break out with an error or we will
  314. * sleep and relock to loop.
  315. */
  316. pipeunlock(rpipe);
  317. /*
  318. * Handle non-blocking mode operation or
  319. * wait for more data.
  320. */
  321. if (fp->f_flag & FNONBLOCK) {
  322. error = EAGAIN;
  323. } else {
  324. rpipe->pipe_state |= PIPE_WANTR;
  325. if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0)
  326. error = pipelock(rpipe);
  327. }
  328. if (error)
  329. goto unlocked_error;
  330. }
  331. }
  332. pipeunlock(rpipe);
  333. if (error == 0)
  334. getnanotime(&rpipe->pipe_atime);
  335. unlocked_error:
  336. --rpipe->pipe_busy;
  337. /*
  338. * PIPE_WANT processing only makes sense if pipe_busy is 0.
  339. */
  340. if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
  341. rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
  342. wakeup(rpipe);
  343. } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
  344. /*
  345. * Handle write blocking hysteresis.
  346. */
  347. if (rpipe->pipe_state & PIPE_WANTW) {
  348. rpipe->pipe_state &= ~PIPE_WANTW;
  349. wakeup(rpipe);
  350. }
  351. }
  352. if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
  353. pipeselwakeup(rpipe);
  354. return (error);
  355. }
  356. int
  357. pipe_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
  358. {
  359. int error = 0;
  360. int orig_resid;
  361. struct pipe *wpipe, *rpipe;
  362. rpipe = (struct pipe *) fp->f_data;
  363. wpipe = rpipe->pipe_peer;
  364. /*
  365. * detect loss of pipe read side, issue SIGPIPE if lost.
  366. */
  367. if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
  368. return (EPIPE);
  369. }
  370. ++wpipe->pipe_busy;
  371. /*
  372. * If it is advantageous to resize the pipe buffer, do
  373. * so.
  374. */
  375. if ((uio->uio_resid > PIPE_SIZE) &&
  376. (nbigpipe < LIMITBIGPIPES) &&
  377. (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
  378. (wpipe->pipe_buffer.cnt == 0)) {
  379. if ((error = pipelock(wpipe)) == 0) {
  380. if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
  381. nbigpipe++;
  382. pipeunlock(wpipe);
  383. }
  384. }
  385. /*
  386. * If an early error occurred unbusy and return, waking up any pending
  387. * readers.
  388. */
  389. if (error) {
  390. --wpipe->pipe_busy;
  391. if ((wpipe->pipe_busy == 0) &&
  392. (wpipe->pipe_state & PIPE_WANT)) {
  393. wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
  394. wakeup(wpipe);
  395. }
  396. return (error);
  397. }
  398. orig_resid = uio->uio_resid;
  399. while (uio->uio_resid) {
  400. int space;
  401. retrywrite:
  402. if (wpipe->pipe_state & PIPE_EOF) {
  403. error = EPIPE;
  404. break;
  405. }
  406. space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
  407. /* Writes of size <= PIPE_BUF must be atomic. */
  408. if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
  409. space = 0;
  410. if (space > 0) {
  411. if ((error = pipelock(wpipe)) == 0) {
  412. int size; /* Transfer size */
  413. int segsize; /* first segment to transfer */
  414. /*
  415. * If a process blocked in uiomove, our
  416. * value for space might be bad.
  417. *
  418. * XXX will we be ok if the reader has gone
  419. * away here?
  420. */
  421. if (space > wpipe->pipe_buffer.size -
  422. wpipe->pipe_buffer.cnt) {
  423. pipeunlock(wpipe);
  424. goto retrywrite;
  425. }
  426. /*
  427. * Transfer size is minimum of uio transfer
  428. * and free space in pipe buffer.
  429. */
  430. if (space > uio->uio_resid)
  431. size = uio->uio_resid;
  432. else
  433. size = space;
  434. /*
  435. * First segment to transfer is minimum of
  436. * transfer size and contiguous space in
  437. * pipe buffer. If first segment to transfer
  438. * is less than the transfer size, we've got
  439. * a wraparound in the buffer.
  440. */
  441. segsize = wpipe->pipe_buffer.size -
  442. wpipe->pipe_buffer.in;
  443. if (segsize > size)
  444. segsize = size;
  445. /* Transfer first segment */
  446. error = uiomovei(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
  447. segsize, uio);
  448. if (error == 0 && segsize < size) {
  449. /*
  450. * Transfer remaining part now, to
  451. * support atomic writes. Wraparound
  452. * happened.
  453. */
  454. #ifdef DIAGNOSTIC
  455. if (wpipe->pipe_buffer.in + segsize !=
  456. wpipe->pipe_buffer.size)
  457. panic("Expected pipe buffer wraparound disappeared");
  458. #endif
  459. error = uiomovei(&wpipe->pipe_buffer.buffer[0],
  460. size - segsize, uio);
  461. }
  462. if (error == 0) {
  463. wpipe->pipe_buffer.in += size;
  464. if (wpipe->pipe_buffer.in >=
  465. wpipe->pipe_buffer.size) {
  466. #ifdef DIAGNOSTIC
  467. if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
  468. panic("Expected wraparound bad");
  469. #endif
  470. wpipe->pipe_buffer.in = size - segsize;
  471. }
  472. wpipe->pipe_buffer.cnt += size;
  473. #ifdef DIAGNOSTIC
  474. if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
  475. panic("Pipe buffer overflow");
  476. #endif
  477. }
  478. pipeunlock(wpipe);
  479. }
  480. if (error)
  481. break;
  482. } else {
  483. /*
  484. * If the "read-side" has been blocked, wake it up now.
  485. */
  486. if (wpipe->pipe_state & PIPE_WANTR) {
  487. wpipe->pipe_state &= ~PIPE_WANTR;
  488. wakeup(wpipe);
  489. }
  490. /*
  491. * don't block on non-blocking I/O
  492. */
  493. if (fp->f_flag & FNONBLOCK) {
  494. error = EAGAIN;
  495. break;
  496. }
  497. /*
  498. * We have no more space and have something to offer,
  499. * wake up select/poll.
  500. */
  501. pipeselwakeup(wpipe);
  502. wpipe->pipe_state |= PIPE_WANTW;
  503. error = tsleep(wpipe, (PRIBIO + 1)|PCATCH,
  504. "pipewr", 0);
  505. if (error)
  506. break;
  507. /*
  508. * If read side wants to go away, we just issue a
  509. * signal to ourselves.
  510. */
  511. if (wpipe->pipe_state & PIPE_EOF) {
  512. error = EPIPE;
  513. break;
  514. }
  515. }
  516. }
  517. --wpipe->pipe_busy;
  518. if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
  519. wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
  520. wakeup(wpipe);
  521. } else if (wpipe->pipe_buffer.cnt > 0) {
  522. /*
  523. * If we have put any characters in the buffer, we wake up
  524. * the reader.
  525. */
  526. if (wpipe->pipe_state & PIPE_WANTR) {
  527. wpipe->pipe_state &= ~PIPE_WANTR;
  528. wakeup(wpipe);
  529. }
  530. }
  531. /*
  532. * Don't return EPIPE if I/O was successful
  533. */
  534. if ((wpipe->pipe_buffer.cnt == 0) &&
  535. (uio->uio_resid == 0) &&
  536. (error == EPIPE)) {
  537. error = 0;
  538. }
  539. if (error == 0)
  540. getnanotime(&wpipe->pipe_mtime);
  541. /*
  542. * We have something to offer, wake up select/poll.
  543. */
  544. if (wpipe->pipe_buffer.cnt)
  545. pipeselwakeup(wpipe);
  546. return (error);
  547. }
  548. /*
  549. * we implement a very minimal set of ioctls for compatibility with sockets.
  550. */
  551. int
  552. pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
  553. {
  554. struct pipe *mpipe = (struct pipe *)fp->f_data;
  555. switch (cmd) {
  556. case FIONBIO:
  557. return (0);
  558. case FIOASYNC:
  559. if (*(int *)data) {
  560. mpipe->pipe_state |= PIPE_ASYNC;
  561. } else {
  562. mpipe->pipe_state &= ~PIPE_ASYNC;
  563. }
  564. return (0);
  565. case FIONREAD:
  566. *(int *)data = mpipe->pipe_buffer.cnt;
  567. return (0);
  568. case SIOCSPGRP:
  569. mpipe->pipe_pgid = *(int *)data;
  570. return (0);
  571. case SIOCGPGRP:
  572. *(int *)data = mpipe->pipe_pgid;
  573. return (0);
  574. }
  575. return (ENOTTY);
  576. }
  577. int
  578. pipe_poll(struct file *fp, int events, struct proc *p)
  579. {
  580. struct pipe *rpipe = (struct pipe *)fp->f_data;
  581. struct pipe *wpipe;
  582. int revents = 0;
  583. wpipe = rpipe->pipe_peer;
  584. if (events & (POLLIN | POLLRDNORM)) {
  585. if ((rpipe->pipe_buffer.cnt > 0) ||
  586. (rpipe->pipe_state & PIPE_EOF))
  587. revents |= events & (POLLIN | POLLRDNORM);
  588. }
  589. /* NOTE: POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */
  590. if ((rpipe->pipe_state & PIPE_EOF) ||
  591. (wpipe == NULL) ||
  592. (wpipe->pipe_state & PIPE_EOF))
  593. revents |= POLLHUP;
  594. else if (events & (POLLOUT | POLLWRNORM)) {
  595. if ((wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)
  596. revents |= events & (POLLOUT | POLLWRNORM);
  597. }
  598. if (revents == 0) {
  599. if (events & (POLLIN | POLLRDNORM)) {
  600. selrecord(p, &rpipe->pipe_sel);
  601. rpipe->pipe_state |= PIPE_SEL;
  602. }
  603. if (events & (POLLOUT | POLLWRNORM)) {
  604. selrecord(p, &wpipe->pipe_sel);
  605. wpipe->pipe_state |= PIPE_SEL;
  606. }
  607. }
  608. return (revents);
  609. }
  610. int
  611. pipe_stat(struct file *fp, struct stat *ub, struct proc *p)
  612. {
  613. struct pipe *pipe = (struct pipe *)fp->f_data;
  614. memset(ub, 0, sizeof(*ub));
  615. ub->st_mode = S_IFIFO;
  616. ub->st_blksize = pipe->pipe_buffer.size;
  617. ub->st_size = pipe->pipe_buffer.cnt;
  618. ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
  619. ub->st_atim.tv_sec = pipe->pipe_atime.tv_sec;
  620. ub->st_atim.tv_nsec = pipe->pipe_atime.tv_nsec;
  621. ub->st_mtim.tv_sec = pipe->pipe_mtime.tv_sec;
  622. ub->st_mtim.tv_nsec = pipe->pipe_mtime.tv_nsec;
  623. ub->st_ctim.tv_sec = pipe->pipe_ctime.tv_sec;
  624. ub->st_ctim.tv_nsec = pipe->pipe_ctime.tv_nsec;
  625. ub->st_uid = fp->f_cred->cr_uid;
  626. ub->st_gid = fp->f_cred->cr_gid;
  627. /*
  628. * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
  629. * XXX (st_dev, st_ino) should be unique.
  630. */
  631. return (0);
  632. }
  633. /* ARGSUSED */
  634. int
  635. pipe_close(struct file *fp, struct proc *p)
  636. {
  637. struct pipe *cpipe = (struct pipe *)fp->f_data;
  638. fp->f_ops = NULL;
  639. fp->f_data = NULL;
  640. pipeclose(cpipe);
  641. return (0);
  642. }
  643. void
  644. pipe_free_kmem(struct pipe *cpipe)
  645. {
  646. if (cpipe->pipe_buffer.buffer != NULL) {
  647. if (cpipe->pipe_buffer.size > PIPE_SIZE)
  648. --nbigpipe;
  649. amountpipekva -= cpipe->pipe_buffer.size;
  650. km_free(cpipe->pipe_buffer.buffer, cpipe->pipe_buffer.size,
  651. &kv_any, &kp_pageable);
  652. cpipe->pipe_buffer.buffer = NULL;
  653. }
  654. }
  655. /*
  656. * shutdown the pipe
  657. */
  658. void
  659. pipeclose(struct pipe *cpipe)
  660. {
  661. struct pipe *ppipe;
  662. if (cpipe) {
  663. pipeselwakeup(cpipe);
  664. /*
  665. * If the other side is blocked, wake it up saying that
  666. * we want to close it down.
  667. */
  668. cpipe->pipe_state |= PIPE_EOF;
  669. while (cpipe->pipe_busy) {
  670. wakeup(cpipe);
  671. cpipe->pipe_state |= PIPE_WANT;
  672. tsleep(cpipe, PRIBIO, "pipecl", 0);
  673. }
  674. /*
  675. * Disconnect from peer
  676. */
  677. if ((ppipe = cpipe->pipe_peer) != NULL) {
  678. pipeselwakeup(ppipe);
  679. ppipe->pipe_state |= PIPE_EOF;
  680. wakeup(ppipe);
  681. ppipe->pipe_peer = NULL;
  682. }
  683. /*
  684. * free resources
  685. */
  686. pipe_free_kmem(cpipe);
  687. pool_put(&pipe_pool, cpipe);
  688. }
  689. }
  690. int
  691. pipe_kqfilter(struct file *fp, struct knote *kn)
  692. {
  693. struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
  694. struct pipe *wpipe = rpipe->pipe_peer;
  695. switch (kn->kn_filter) {
  696. case EVFILT_READ:
  697. kn->kn_fop = &pipe_rfiltops;
  698. SLIST_INSERT_HEAD(&rpipe->pipe_sel.si_note, kn, kn_selnext);
  699. break;
  700. case EVFILT_WRITE:
  701. if (wpipe == NULL) {
  702. /* other end of pipe has been closed */
  703. return (EPIPE);
  704. }
  705. kn->kn_fop = &pipe_wfiltops;
  706. SLIST_INSERT_HEAD(&wpipe->pipe_sel.si_note, kn, kn_selnext);
  707. break;
  708. default:
  709. return (EINVAL);
  710. }
  711. return (0);
  712. }
  713. void
  714. filt_pipedetach(struct knote *kn)
  715. {
  716. struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
  717. struct pipe *wpipe = rpipe->pipe_peer;
  718. switch (kn->kn_filter) {
  719. case EVFILT_READ:
  720. SLIST_REMOVE(&rpipe->pipe_sel.si_note, kn, knote, kn_selnext);
  721. break;
  722. case EVFILT_WRITE:
  723. if (wpipe == NULL)
  724. return;
  725. SLIST_REMOVE(&wpipe->pipe_sel.si_note, kn, knote, kn_selnext);
  726. break;
  727. }
  728. }
  729. /*ARGSUSED*/
  730. int
  731. filt_piperead(struct knote *kn, long hint)
  732. {
  733. struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
  734. struct pipe *wpipe = rpipe->pipe_peer;
  735. kn->kn_data = rpipe->pipe_buffer.cnt;
  736. if ((rpipe->pipe_state & PIPE_EOF) ||
  737. (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
  738. kn->kn_flags |= EV_EOF;
  739. return (1);
  740. }
  741. return (kn->kn_data > 0);
  742. }
  743. /*ARGSUSED*/
  744. int
  745. filt_pipewrite(struct knote *kn, long hint)
  746. {
  747. struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
  748. struct pipe *wpipe = rpipe->pipe_peer;
  749. if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
  750. kn->kn_data = 0;
  751. kn->kn_flags |= EV_EOF;
  752. return (1);
  753. }
  754. kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
  755. return (kn->kn_data >= PIPE_BUF);
  756. }
  757. void
  758. pipe_init(void)
  759. {
  760. pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, PR_WAITOK, "pipepl",
  761. NULL);
  762. }