_buf.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #define _BSDTIME_EXTENSION
  2. #define _LOCK_EXTENSION
  3. #include "lib.h"
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <lock.h>
  11. #include <sys/time.h>
  12. #include <sys/select.h>
  13. #include <unistd.h>
  14. #include "sys9.h"
  15. typedef struct Muxseg {
  16. Lock lock; /* for mutual exclusion access to buffer variables */
  17. int curfds; /* number of fds currently buffered */
  18. int selwait; /* true if selecting process is waiting */
  19. int waittime; /* time for timer process to wait */
  20. fd_set rwant; /* fd's that select wants to read */
  21. fd_set ewant; /* fd's that select wants to know eof info on */
  22. Muxbuf bufs[INITBUFS]; /* can grow, via segbrk() */
  23. } Muxseg;
  24. #define MUXADDR ((void*)0x6000000)
  25. static Muxseg *mux = 0; /* shared memory segment */
  26. /* _muxsid and _killmuxsid are known in libbsd's listen.c */
  27. int _muxsid = -1; /* group id of copy processes */
  28. static int _mainpid = -1;
  29. static int timerpid = -1; /* pid of a timer process */
  30. void _killmuxsid(void);
  31. static void _copyproc(int, Muxbuf*);
  32. static void _timerproc(void);
  33. static void _resettimer(void);
  34. static int copynotehandler(void *, char *);
  35. /* assume FD_SETSIZE is 96 */
  36. #define FD_ANYSET(p) ((p)->fds_bits[0] || (p)->fds_bits[1] || (p)->fds_bits[2])
  37. /*
  38. * Start making fd read-buffered: make the shared segment, if necessary,
  39. * allocate a slot (index into mux->bufs), and fork a child to read the fd
  40. * and write into the slot-indexed buffer.
  41. * Return -1 if we can't do it.
  42. */
  43. int
  44. _startbuf(int fd)
  45. {
  46. long i, n, slot;
  47. int pid, sid;
  48. Fdinfo *f;
  49. Muxbuf *b;
  50. if(mux == 0){
  51. _RFORK(RFREND);
  52. mux = (Muxseg*)_SEGATTACH(0, "shared", MUXADDR, sizeof(Muxseg));
  53. if((long)mux == -1){
  54. _syserrno();
  55. return -1;
  56. }
  57. /* segattach has returned zeroed memory */
  58. atexit(_killmuxsid);
  59. }
  60. if(fd == -1)
  61. return 0;
  62. lock(&mux->lock);
  63. slot = mux->curfds++;
  64. if(mux->curfds > INITBUFS) {
  65. if(_SEGBRK(mux, mux->bufs+mux->curfds) < 0){
  66. _syserrno();
  67. unlock(&mux->lock);
  68. return -1;
  69. }
  70. }
  71. f = &_fdinfo[fd];
  72. b = &mux->bufs[slot];
  73. b->n = 0;
  74. b->putnext = b->data;
  75. b->getnext = b->data;
  76. b->eof = 0;
  77. b->fd = fd;
  78. if(_mainpid == -1)
  79. _mainpid = getpid();
  80. if((pid = _RFORK(RFFDG|RFPROC|RFNOWAIT)) == 0){
  81. /* copy process ... */
  82. if(_muxsid == -1) {
  83. _RFORK(RFNOTEG);
  84. _muxsid = getpgrp();
  85. } else
  86. setpgid(getpid(), _muxsid);
  87. _NOTIFY(copynotehandler);
  88. for(i=0; i<OPEN_MAX; i++)
  89. if(i!=fd && (_fdinfo[i].flags&FD_ISOPEN))
  90. _CLOSE(i);
  91. _RENDEZVOUS(0, _muxsid);
  92. _copyproc(fd, b);
  93. }
  94. /* parent process continues ... */
  95. b->copypid = pid;
  96. f->buf = b;
  97. f->flags |= FD_BUFFERED;
  98. unlock(&mux->lock);
  99. _muxsid = _RENDEZVOUS(0, 0);
  100. /* leave fd open in parent so system doesn't reuse it */
  101. return 0;
  102. }
  103. /*
  104. * The given buffered fd is being closed.
  105. * Set the fd field in the shared buffer to -1 to tell copyproc
  106. * to exit, and kill the copyproc.
  107. */
  108. void
  109. _closebuf(int fd)
  110. {
  111. Muxbuf *b;
  112. b = _fdinfo[fd].buf;
  113. if(!b)
  114. return;
  115. lock(&mux->lock);
  116. b->fd = -1;
  117. unlock(&mux->lock);
  118. kill(b->copypid, SIGKILL);
  119. }
  120. /* child copy procs execute this until eof */
  121. static void
  122. _copyproc(int fd, Muxbuf *b)
  123. {
  124. unsigned char *e;
  125. int n;
  126. int nzeros;
  127. e = &b->data[PERFDMAX];
  128. for(;;) {
  129. /* make sure there's room */
  130. lock(&mux->lock);
  131. if(e - b->putnext < READMAX) {
  132. if(b->getnext == b->putnext) {
  133. b->getnext = b->putnext = b->data;
  134. unlock(&mux->lock);
  135. } else {
  136. /* sleep until there's room */
  137. b->roomwait = 1;
  138. unlock(&mux->lock);
  139. _RENDEZVOUS((unsigned long)&b->roomwait, 0);
  140. }
  141. } else
  142. unlock(&mux->lock);
  143. /*
  144. * A Zero-length _READ might mean a zero-length write
  145. * happened, or it might mean eof; try several times to
  146. * disambiguate (posix read() discards 0-length messages)
  147. */
  148. nzeros = 0;
  149. do {
  150. n = _READ(fd, b->putnext, READMAX);
  151. if(b->fd == -1) {
  152. _exit(0); /* we've been closed */
  153. }
  154. } while(n == 0 && ++nzeros < 3);
  155. lock(&mux->lock);
  156. if(n <= 0) {
  157. b->eof = 1;
  158. if(mux->selwait && FD_ISSET(fd, &mux->ewant)) {
  159. mux->selwait = 0;
  160. unlock(&mux->lock);
  161. _RENDEZVOUS((unsigned long)&mux->selwait, fd);
  162. } else if(b->datawait) {
  163. b->datawait = 0;
  164. unlock(&mux->lock);
  165. _RENDEZVOUS((unsigned long)&b->datawait, 0);
  166. } else if(mux->selwait && FD_ISSET(fd, &mux->rwant)) {
  167. mux->selwait = 0;
  168. unlock(&mux->lock);
  169. _RENDEZVOUS((unsigned long)&mux->selwait, fd);
  170. } else
  171. unlock(&mux->lock);
  172. _exit(0);
  173. } else {
  174. b->putnext += n;
  175. b->n += n;
  176. if(b->n > 0) {
  177. /* parent process cannot be both in datawait and selwait */
  178. if(b->datawait) {
  179. b->datawait = 0;
  180. unlock(&mux->lock);
  181. /* wake up _bufreading process */
  182. _RENDEZVOUS((unsigned long)&b->datawait, 0);
  183. } else if(mux->selwait && FD_ISSET(fd, &mux->rwant)) {
  184. mux->selwait = 0;
  185. unlock(&mux->lock);
  186. /* wake up selecting process */
  187. _RENDEZVOUS((unsigned long)&mux->selwait, fd);
  188. } else
  189. unlock(&mux->lock);
  190. } else
  191. unlock(&mux->lock);
  192. }
  193. }
  194. }
  195. /* like read(), for a buffered fd; extra arg noblock says don't wait for data if true */
  196. int
  197. _readbuf(int fd, void *addr, int nwant, int noblock)
  198. {
  199. Muxbuf *b;
  200. int ngot;
  201. b = _fdinfo[fd].buf;
  202. if(b->eof && b->n == 0) {
  203. goteof:
  204. return 0;
  205. }
  206. if(b->n == 0 && noblock) {
  207. errno = EAGAIN;
  208. return -1;
  209. }
  210. /* make sure there's data */
  211. lock(&mux->lock);
  212. ngot = b->putnext - b->getnext;
  213. if(ngot == 0) {
  214. /* maybe EOF just happened */
  215. if(b->eof) {
  216. unlock(&mux->lock);
  217. goto goteof;
  218. }
  219. /* sleep until there's data */
  220. b->datawait = 1;
  221. unlock(&mux->lock);
  222. _RENDEZVOUS((unsigned long)&b->datawait, 0);
  223. lock(&mux->lock);
  224. ngot = b->putnext - b->getnext;
  225. }
  226. if(ngot == 0) {
  227. unlock(&mux->lock);
  228. goto goteof;
  229. }
  230. if(ngot > nwant)
  231. ngot = nwant;
  232. memcpy(addr, b->getnext, ngot);
  233. b->getnext += ngot;
  234. b->n -= ngot;
  235. if(b->getnext == b->putnext && b->roomwait) {
  236. b->getnext = b->putnext = b->data;
  237. b->roomwait = 0;
  238. unlock(&mux->lock);
  239. /* wake up copy process */
  240. _RENDEZVOUS((unsigned long)&b->roomwait, 0);
  241. } else
  242. unlock(&mux->lock);
  243. return ngot;
  244. }
  245. int
  246. select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
  247. {
  248. int n, i, tmp, t, slots, fd, err;
  249. Fdinfo *f;
  250. Muxbuf *b;
  251. if(timeout)
  252. t = timeout->tv_sec*1000 + (timeout->tv_usec+999)/1000;
  253. else
  254. t = -1;
  255. if(!((rfds && FD_ANYSET(rfds)) || (wfds && FD_ANYSET(wfds))
  256. || (efds && FD_ANYSET(efds)))) {
  257. /* no requested fds */
  258. if(t > 0)
  259. _SLEEP(t);
  260. return 0;
  261. }
  262. _startbuf(-1);
  263. /* make sure all requested rfds and efds are buffered */
  264. if(nfds >= OPEN_MAX)
  265. nfds = OPEN_MAX;
  266. for(i = 0; i < nfds; i++)
  267. if((rfds && FD_ISSET(i, rfds)) || (efds && FD_ISSET(i, efds))){
  268. f = &_fdinfo[i];
  269. if(!(f->flags&FD_BUFFERED))
  270. if(_startbuf(i) != 0) {
  271. return -1;
  272. }
  273. b = f->buf;
  274. if(rfds && FD_ISSET(i,rfds) && b->eof && b->n == 0)
  275. if(efds == 0 || !FD_ISSET(i,efds)) {
  276. errno = EBADF; /* how X tells a client is gone */
  277. return -1;
  278. }
  279. }
  280. /* check wfds; for now, we'll say they are all ready */
  281. n = 0;
  282. if(wfds && FD_ANYSET(wfds)){
  283. for(i = 0; i<nfds; i++)
  284. if(FD_ISSET(i, wfds)) {
  285. n++;
  286. }
  287. }
  288. lock(&mux->lock);
  289. slots = mux->curfds;
  290. FD_ZERO(&mux->rwant);
  291. FD_ZERO(&mux->ewant);
  292. for(i = 0; i<slots; i++) {
  293. b = &mux->bufs[i];
  294. fd = b->fd;
  295. if(fd == -1)
  296. continue;
  297. err = 0;
  298. if(efds && FD_ISSET(fd, efds)) {
  299. if(b->eof && b->n == 0){
  300. err = 1;
  301. n++;
  302. }else{
  303. FD_CLR(fd, efds);
  304. FD_SET(fd, &mux->ewant);
  305. }
  306. }
  307. if(rfds && FD_ISSET(fd, rfds)) {
  308. if(!err && (b->n > 0 || b->eof))
  309. n++;
  310. else{
  311. FD_CLR(fd, rfds);
  312. FD_SET(fd, &mux->rwant);
  313. }
  314. }
  315. }
  316. if(n || !(FD_ANYSET(&mux->rwant) || FD_ANYSET(&mux->ewant)) || t == 0) {
  317. FD_ZERO(&mux->rwant);
  318. FD_ZERO(&mux->ewant);
  319. unlock(&mux->lock);
  320. return n;
  321. }
  322. if(timeout) {
  323. mux->waittime = t;
  324. if(timerpid == -1)
  325. _timerproc();
  326. else
  327. _resettimer();
  328. }
  329. mux->selwait = 1;
  330. unlock(&mux->lock);
  331. fd = _RENDEZVOUS((unsigned long)&mux->selwait, 0);
  332. if(fd >= 0) {
  333. b = _fdinfo[fd].buf;
  334. if(FD_ISSET(fd, &mux->rwant)) {
  335. FD_SET(fd, rfds);
  336. n = 1;
  337. } else if(FD_ISSET(fd, &mux->ewant) && b->eof && b->n == 0) {
  338. FD_SET(fd, efds);
  339. n = 1;
  340. }
  341. }
  342. FD_ZERO(&mux->rwant);
  343. FD_ZERO(&mux->ewant);
  344. return n;
  345. }
  346. static int timerreset;
  347. static int timerpid;
  348. static void
  349. alarmed(int v)
  350. {
  351. timerreset = 1;
  352. }
  353. /* a little over an hour */
  354. #define LONGWAIT 4000001
  355. static void
  356. _killtimerproc(void)
  357. {
  358. if(timerpid > 0)
  359. kill(timerpid, SIGKILL);
  360. }
  361. static void
  362. _timerproc(void)
  363. {
  364. int i;
  365. if((timerpid = _RFORK(RFFDG|RFPROC|RFNOWAIT)) == 0){
  366. /* timer process */
  367. setpgid(getpid(), _muxsid);
  368. signal(SIGALRM, alarmed);
  369. for(i=0; i<OPEN_MAX; i++)
  370. _CLOSE(i);
  371. _RENDEZVOUS(1, 0);
  372. for(;;) {
  373. _SLEEP(mux->waittime);
  374. if(timerreset) {
  375. timerreset = 0;
  376. } else {
  377. lock(&mux->lock);
  378. if(mux->selwait && mux->waittime != LONGWAIT) {
  379. mux->selwait = 0;
  380. mux->waittime = LONGWAIT;
  381. unlock(&mux->lock);
  382. _RENDEZVOUS((unsigned long)&mux->selwait, -2);
  383. } else {
  384. mux->waittime = LONGWAIT;
  385. unlock(&mux->lock);
  386. }
  387. }
  388. }
  389. }
  390. atexit(_killtimerproc);
  391. /* parent process continues */
  392. _RENDEZVOUS(1, 0);
  393. }
  394. static void
  395. _resettimer(void)
  396. {
  397. kill(timerpid, SIGALRM);
  398. }
  399. void
  400. _killmuxsid(void)
  401. {
  402. if(_muxsid != -1 && (_mainpid == getpid() || _mainpid == -1))
  403. kill(-_muxsid,SIGTERM);
  404. }
  405. /* call this on fork(), because reading a BUFFERED fd won't work in child */
  406. void
  407. _detachbuf(void)
  408. {
  409. int i;
  410. Fdinfo *f;
  411. if(mux == 0)
  412. return;
  413. _SEGDETACH(mux);
  414. for(i = 0; i < OPEN_MAX; i++){
  415. f = &_fdinfo[i];
  416. if(f->flags&FD_BUFFERED)
  417. f->flags = (f->flags&~FD_BUFFERED) | FD_BUFFEREDX;
  418. /* mark 'poisoned' */
  419. }
  420. mux = 0;
  421. _muxsid = -1;
  422. _mainpid = -1;
  423. timerpid = -1;
  424. }
  425. static int
  426. copynotehandler(void *u, char *msg)
  427. {
  428. int i;
  429. void(*f)(int);
  430. if(_finishing)
  431. _finish(0, 0);
  432. _NOTED(1);
  433. }