_buf.c 9.4 KB

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