libexec exec fix
[minix.git] / servers / vfs / select.c
blob0898229317341a80ed8b196a844d8ef18def8042
1 /* Implement entry point to select system call.
3 * The entry points into this file are
4 * do_select: perform the SELECT system call
5 * select_callback: notify select system of possible fd operation
6 * select_unsuspend_by_endpt: cancel a blocking select on exiting driver
7 */
9 #include "fs.h"
10 #include <sys/time.h>
11 #include <sys/select.h>
12 #include <sys/stat.h>
13 #include <minix/com.h>
14 #include <minix/u64.h>
15 #include <string.h>
16 #include <assert.h>
18 #include "file.h"
19 #include "fproc.h"
20 #include "dmap.h"
21 #include "vnode.h"
23 /* max. number of simultaneously pending select() calls */
24 #define MAXSELECTS 25
25 #define FROM_PROC 0
26 #define TO_PROC 1
28 static struct selectentry {
29 struct fproc *requestor; /* slot is free iff this is NULL */
30 endpoint_t req_endpt;
31 fd_set readfds, writefds, errorfds;
32 fd_set ready_readfds, ready_writefds, ready_errorfds;
33 fd_set *vir_readfds, *vir_writefds, *vir_errorfds;
34 struct filp *filps[OPEN_MAX];
35 int type[OPEN_MAX];
36 int nfds, nreadyfds;
37 int error;
38 char block;
39 clock_t expiry;
40 timer_t timer; /* if expiry > 0 */
41 } selecttab[MAXSELECTS];
43 static int copy_fdsets(struct selectentry *se, int nfds, int
44 direction);
45 static int do_select_request(struct selectentry *se, int fd, int *ops);
46 static void filp_status(struct filp *fp, int status);
47 static int is_deferred(struct selectentry *se);
48 static void restart_proc(struct selectentry *se);
49 static void ops2tab(int ops, int fd, struct selectentry *e);
50 static int is_regular_file(struct filp *f);
51 static int is_pipe(struct filp *f);
52 static int is_supported_major(struct filp *f);
53 static void select_lock_filp(struct filp *f, int ops);
54 static int select_request_async(struct filp *f, int *ops, int block);
55 static int select_request_file(struct filp *f, int *ops, int block);
56 static int select_request_major(struct filp *f, int *ops, int block);
57 static int select_request_pipe(struct filp *f, int *ops, int block);
58 static int select_request_sync(struct filp *f, int *ops, int block);
59 static void select_cancel_all(struct selectentry *e);
60 static void select_cancel_filp(struct filp *f);
61 static void select_return(struct selectentry *);
62 static void select_restart_filps(void);
63 static int tab2ops(int fd, struct selectentry *e);
64 static void wipe_select(struct selectentry *s);
66 static struct fdtype {
67 int (*select_request)(struct filp *, int *ops, int block);
68 int (*type_match)(struct filp *f);
69 } fdtypes[] = {
70 { select_request_major, is_supported_major },
71 { select_request_file, is_regular_file },
72 { select_request_pipe, is_pipe },
74 #define SEL_FDS (sizeof(fdtypes) / sizeof(fdtypes[0]))
75 static int select_majors[] = { /* List of majors that support selecting on */
76 TTY_MAJOR,
77 INET_MAJOR,
78 UDS_MAJOR,
79 LOG_MAJOR,
81 #define SEL_MAJORS (sizeof(select_majors) / sizeof(select_majors[0]))
83 /*===========================================================================*
84 * do_select *
85 *===========================================================================*/
86 int do_select(void)
88 /* Implement the select(nfds, readfds, writefds, errorfds, timeout) system
89 * call. First we copy the arguments and verify their sanity. Then we check
90 * whether there are file descriptors that satisfy the select call right of the
91 * bat. If so, or if there are no ready file descriptors but the process
92 * requested to return immediately, we return the result. Otherwise we set a
93 * timeout and wait for either the file descriptors to become ready or the
94 * timer to go off. If no timeout value was provided, we wait indefinitely. */
96 int r, nfds, do_timeout = 0, fd, s;
97 struct timeval timeout;
98 struct selectentry *se;
99 vir_bytes vtimeout;
101 nfds = job_m_in.SEL_NFDS;
102 vtimeout = (vir_bytes) job_m_in.SEL_TIMEOUT;
104 /* Sane amount of file descriptors? */
105 if (nfds < 0 || nfds > OPEN_MAX) return(EINVAL);
107 /* Find a slot to store this select request */
108 for (s = 0; s < MAXSELECTS; s++)
109 if (selecttab[s].requestor == NULL) /* Unused slot */
110 break;
111 if (s >= MAXSELECTS) return(ENOSPC);
113 se = &selecttab[s];
114 wipe_select(se); /* Clear results of previous usage */
115 se->requestor = fp;
116 se->req_endpt = who_e;
117 se->vir_readfds = (fd_set *) job_m_in.SEL_READFDS;
118 se->vir_writefds = (fd_set *) job_m_in.SEL_WRITEFDS;
119 se->vir_errorfds = (fd_set *) job_m_in.SEL_ERRORFDS;
121 /* Copy fdsets from the process */
122 if ((r = copy_fdsets(se, nfds, FROM_PROC)) != OK) {
123 se->requestor = NULL;
124 return(r);
127 /* Did the process set a timeout value? If so, retrieve it. */
128 if (vtimeout != 0) {
129 do_timeout = 1;
130 r = sys_vircopy(who_e, (vir_bytes) vtimeout, SELF,
131 (vir_bytes) &timeout, sizeof(timeout));
132 if (r != OK) {
133 se->requestor = NULL;
134 return(r);
138 /* No nonsense in the timeval */
139 if (do_timeout && (timeout.tv_sec < 0 || timeout.tv_usec < 0)) {
140 se->requestor = NULL;
141 return(EINVAL);
144 /* If there is no timeout, we block forever. Otherwise, we block up to the
145 * specified time interval.
147 if (!do_timeout) /* No timeout value set */
148 se->block = 1;
149 else if (do_timeout && (timeout.tv_sec > 0 || timeout.tv_usec > 0))
150 se->block = 1;
151 else /* timeout set as (0,0) - this effects a poll */
152 se->block = 0;
153 se->expiry = 0; /* no timer set (yet) */
155 /* Verify that file descriptors are okay to select on */
156 for (fd = 0; fd < nfds; fd++) {
157 struct filp *f;
158 unsigned int type, ops;
160 /* Because the select() interface implicitly includes file descriptors
161 * you might not want to select on, we have to figure out whether we're
162 * interested in them. Typically, these file descriptors include fd's
163 * inherited from the parent proc and file descriptors that have been
164 * close()d, but had a lower fd than one in the current set.
166 if (!(ops = tab2ops(fd, se)))
167 continue; /* No operations set; nothing to do for this fd */
169 /* Get filp belonging to this fd */
170 f = se->filps[fd] = get_filp(fd, VNODE_READ);
171 if (f == NULL) {
172 if (err_code == EBADF)
173 r = err_code;
174 else /* File descriptor is 'ready' to return EIO */
175 r = EINTR;
177 se->requestor = NULL;
178 return(r);
181 /* Check file types. According to POSIX 2008:
182 * "The pselect() and select() functions shall support regular files,
183 * terminal and pseudo-terminal devices, FIFOs, pipes, and sockets. The
184 * behavior of pselect() and select() on file descriptors that refer to
185 * other types of file is unspecified."
187 * In our case, terminal and pseudo-terminal devices are handled by the
188 * TTY major and sockets by either INET major (socket type AF_INET) or
189 * PFS major (socket type AF_UNIX). PFS acts as an FS when it handles
190 * pipes and as a driver when it handles sockets. Additionally, we
191 * support select on the LOG major to handle kernel logging, which is
192 * beyond the POSIX spec. */
194 se->type[fd] = -1;
195 for (type = 0; type < SEL_FDS; type++) {
196 if (fdtypes[type].type_match(f)) {
197 se->type[fd] = type;
198 se->nfds = fd+1;
199 se->filps[fd]->filp_selectors++;
200 break;
203 unlock_filp(f);
204 if (se->type[fd] == -1) { /* Type not found */
205 se->requestor = NULL;
206 return(EBADF);
210 /* Check all file descriptors in the set whether one is 'ready' now */
211 for (fd = 0; fd < nfds; fd++) {
212 int ops, r;
213 struct filp *f;
215 /* Again, check for involuntarily selected fd's */
216 if (!(ops = tab2ops(fd, se)))
217 continue; /* No operations set; nothing to do for this fd */
219 /* Test filp for select operations if not already done so. e.g.,
220 * processes sharing a filp and both doing a select on that filp. */
221 f = se->filps[fd];
222 if ((f->filp_select_ops & ops) != ops) {
223 int wantops;
225 wantops = (f->filp_select_ops |= ops);
226 r = do_select_request(se, fd, &wantops);
227 if (r != OK && r != SUSPEND)
228 break; /* Error or bogus return code; abort */
230 /* The select request above might have turned on/off some
231 * operations because they were 'ready' or not meaningful.
232 * Either way, we might have a result and we need to store them
233 * in the select table entry. */
234 if (wantops & ops) ops2tab(wantops, fd, se);
238 if ((se->nreadyfds > 0 || !se->block) && !is_deferred(se)) {
239 /* fd's were found that were ready to go right away, and/or
240 * we were instructed not to block at all. Must return
241 * immediately.
243 r = copy_fdsets(se, se->nfds, TO_PROC);
244 select_cancel_all(se);
245 se->requestor = NULL;
247 if (r != OK)
248 return(r);
249 else if (se->error != OK)
250 return(se->error);
252 return(se->nreadyfds);
255 /* Convert timeval to ticks and set the timer. If it fails, undo
256 * all, return error.
258 if (do_timeout) {
259 int ticks;
260 /* Open Group:
261 * "If the requested timeout interval requires a finer
262 * granularity than the implementation supports, the
263 * actual timeout interval shall be rounded up to the next
264 * supported value."
266 #define USECPERSEC 1000000
267 while(timeout.tv_usec >= USECPERSEC) {
268 /* this is to avoid overflow with *system_hz below */
269 timeout.tv_usec -= USECPERSEC;
270 timeout.tv_sec++;
272 ticks = timeout.tv_sec * system_hz +
273 (timeout.tv_usec * system_hz + USECPERSEC-1) / USECPERSEC;
274 se->expiry = ticks;
275 set_timer(&se->timer, ticks, select_timeout_check, s);
278 /* process now blocked */
279 suspend(FP_BLOCKED_ON_SELECT);
280 return(SUSPEND);
283 /*===========================================================================*
284 * is_deferred *
285 *===========================================================================*/
286 static int is_deferred(struct selectentry *se)
288 /* Find out whether this select has pending initial replies */
290 int fd;
291 struct filp *f;
293 for (fd = 0; fd < se->nfds; fd++) {
294 if ((f = se->filps[fd]) == NULL) continue;
295 if (f->filp_select_flags & (FSF_UPDATE|FSF_BUSY)) return(TRUE);
298 return(FALSE);
302 /*===========================================================================*
303 * is_regular_file *
304 *===========================================================================*/
305 static int is_regular_file(struct filp *f)
307 return(f && f->filp_vno && S_ISREG(f->filp_vno->v_mode));
310 /*===========================================================================*
311 * is_pipe *
312 *===========================================================================*/
313 static int is_pipe(struct filp *f)
315 /* Recognize either anonymous pipe or named pipe (FIFO) */
316 return(f && f->filp_vno && S_ISFIFO(f->filp_vno->v_mode));
319 /*===========================================================================*
320 * is_supported_major *
321 *===========================================================================*/
322 static int is_supported_major(struct filp *f)
324 /* See if this filp is a handle on a device on which we support select() */
325 unsigned int m;
327 if (!(f && f->filp_vno)) return(FALSE);
328 if (!S_ISCHR(f->filp_vno->v_mode)) return(FALSE);
330 for (m = 0; m < SEL_MAJORS; m++)
331 if (major(f->filp_vno->v_sdev) == select_majors[m])
332 return(TRUE);
334 return(FALSE);
337 /*===========================================================================*
338 * select_request_async *
339 *===========================================================================*/
340 static int select_request_async(struct filp *f, int *ops, int block)
342 int r, rops, major;
343 struct dmap *dp;
345 rops = *ops;
347 /* By default, nothing to do */
348 *ops = 0;
350 if (!block && (f->filp_select_flags & FSF_BLOCKED)) {
351 /* This filp is blocked waiting for a reply, but we don't want to
352 * block ourselves. Unless we're awaiting the initial reply, these
353 * operations won't be ready */
354 if (!(f->filp_select_flags & FSF_BUSY)) {
355 if ((rops & SEL_RD) && (f->filp_select_flags & FSF_RD_BLOCK))
356 rops &= ~SEL_RD;
357 if ((rops & SEL_WR) && (f->filp_select_flags & FSF_WR_BLOCK))
358 rops &= ~SEL_WR;
359 if ((rops & SEL_ERR) && (f->filp_select_flags & FSF_ERR_BLOCK))
360 rops &= ~SEL_ERR;
361 if (!(rops & (SEL_RD|SEL_WR|SEL_ERR)))
362 return(OK);
366 f->filp_select_flags |= FSF_UPDATE;
367 if (block) {
368 rops |= SEL_NOTIFY;
369 if (rops & SEL_RD) f->filp_select_flags |= FSF_RD_BLOCK;
370 if (rops & SEL_WR) f->filp_select_flags |= FSF_WR_BLOCK;
371 if (rops & SEL_ERR) f->filp_select_flags |= FSF_ERR_BLOCK;
374 if (f->filp_select_flags & FSF_BUSY)
375 return(SUSPEND);
377 major = major(f->filp_vno->v_sdev);
378 if (major < 0 || major >= NR_DEVICES) return(ENXIO);
379 dp = &dmap[major];
380 if (dp->dmap_sel_filp)
381 return(SUSPEND);
383 f->filp_select_flags &= ~FSF_UPDATE;
384 r = dev_io(VFS_DEV_SELECT, f->filp_vno->v_sdev, rops, NULL,
385 cvu64(0), 0, 0, FALSE);
386 if (r < 0 && r != SUSPEND)
387 return(r);
389 if (r != SUSPEND)
390 panic("select_request_asynch: expected SUSPEND got: %d", r);
392 dp->dmap_sel_filp = f;
393 f->filp_select_flags |= FSF_BUSY;
395 return(SUSPEND);
398 /*===========================================================================*
399 * select_request_file *
400 *===========================================================================*/
401 static int select_request_file(struct filp *UNUSED(f), int *UNUSED(ops),
402 int UNUSED(block))
404 /* Files are always ready, so output *ops is input *ops */
405 return(OK);
408 /*===========================================================================*
409 * select_request_major *
410 *===========================================================================*/
411 static int select_request_major(struct filp *f, int *ops, int block)
413 int major, r;
415 major = major(f->filp_vno->v_sdev);
416 if (major < 0 || major >= NR_DEVICES) return(ENXIO);
418 if (dmap[major].dmap_style == STYLE_DEVA ||
419 dmap[major].dmap_style == STYLE_CLONE_A)
420 r = select_request_async(f, ops, block);
421 else
422 r = select_request_sync(f, ops, block);
424 return(r);
427 /*===========================================================================*
428 * select_request_sync *
429 *===========================================================================*/
430 static int select_request_sync(struct filp *f, int *ops, int block)
432 int rops;
434 rops = *ops;
435 if (block) rops |= SEL_NOTIFY;
436 *ops = dev_io(VFS_DEV_SELECT, f->filp_vno->v_sdev, rops, NULL,
437 cvu64(0), 0, 0, FALSE);
438 if (*ops < 0)
439 return(*ops);
441 return(OK);
444 /*===========================================================================*
445 * select_request_pipe *
446 *===========================================================================*/
447 static int select_request_pipe(struct filp *f, int *ops, int block)
449 int orig_ops, r = 0, err;
451 orig_ops = *ops;
453 if ((*ops & (SEL_RD|SEL_ERR))) {
454 err = pipe_check(f->filp_vno, READING, 0, 1, f->filp_pos, 1);
456 if (err != SUSPEND)
457 r |= SEL_RD;
458 if (err < 0 && err != SUSPEND)
459 r |= SEL_ERR;
460 if (err == SUSPEND && !(f->filp_mode & R_BIT)) {
461 /* A "meaningless" read select, therefore ready
462 * for reading and no error set. */
463 r |= SEL_RD;
464 r &= ~SEL_ERR;
468 if ((*ops & (SEL_WR|SEL_ERR))) {
469 err = pipe_check(f->filp_vno, WRITING, 0, 1, f->filp_pos, 1);
471 if (err != SUSPEND)
472 r |= SEL_WR;
473 if (err < 0 && err != SUSPEND)
474 r |= SEL_ERR;
475 if (err == SUSPEND && !(f->filp_mode & W_BIT)) {
476 /* A "meaningless" write select, therefore ready
477 for writing and no error set. */
478 r |= SEL_WR;
479 r &= ~SEL_ERR;
483 /* Some options we collected might not be requested. */
484 *ops = r & orig_ops;
486 if (!*ops && block)
487 f->filp_pipe_select_ops |= orig_ops;
489 return(OK);
492 /*===========================================================================*
493 * tab2ops *
494 *===========================================================================*/
495 static int tab2ops(int fd, struct selectentry *e)
497 int ops = 0;
498 if (FD_ISSET(fd, &e->readfds)) ops |= SEL_RD;
499 if (FD_ISSET(fd, &e->writefds)) ops |= SEL_WR;
500 if (FD_ISSET(fd, &e->errorfds)) ops |= SEL_ERR;
502 return(ops);
506 /*===========================================================================*
507 * ops2tab *
508 *===========================================================================*/
509 static void ops2tab(int ops, int fd, struct selectentry *e)
511 if ((ops & SEL_RD) && e->vir_readfds && FD_ISSET(fd, &e->readfds) &&
512 !FD_ISSET(fd, &e->ready_readfds)) {
513 FD_SET(fd, &e->ready_readfds);
514 e->nreadyfds++;
517 if ((ops & SEL_WR) && e->vir_writefds && FD_ISSET(fd, &e->writefds) &&
518 !FD_ISSET(fd, &e->ready_writefds)) {
519 FD_SET(fd, &e->ready_writefds);
520 e->nreadyfds++;
523 if ((ops & SEL_ERR) && e->vir_errorfds && FD_ISSET(fd, &e->errorfds) &&
524 !FD_ISSET(fd, &e->ready_errorfds)) {
525 FD_SET(fd, &e->ready_errorfds);
526 e->nreadyfds++;
531 /*===========================================================================*
532 * copy_fdsets *
533 *===========================================================================*/
534 static int copy_fdsets(struct selectentry *se, int nfds, int direction)
536 int r;
537 size_t fd_setsize;
538 endpoint_t src_e, dst_e;
539 fd_set *src_fds, *dst_fds;
541 if (nfds < 0 || nfds > OPEN_MAX)
542 panic("select copy_fdsets: nfds wrong: %d", nfds);
544 /* Only copy back as many bits as the user expects. */
545 #ifdef __NBSD_LIBC
546 fd_setsize = (size_t) (howmany(nfds, __NFDBITS) * sizeof(__fd_mask));
547 #else
548 fd_setsize = (size_t) (_FDSETWORDS(nfds) * _FDSETBITSPERWORD/8);
549 #endif
551 /* Set source and destination endpoints */
552 src_e = (direction == FROM_PROC) ? se->req_endpt : SELF;
553 dst_e = (direction == FROM_PROC) ? SELF : se->req_endpt;
555 /* read set */
556 src_fds = (direction == FROM_PROC) ? se->vir_readfds : &se->ready_readfds;
557 dst_fds = (direction == FROM_PROC) ? &se->readfds : se->vir_readfds;
558 if (se->vir_readfds) {
559 r = sys_vircopy(src_e, (vir_bytes) src_fds, dst_e,
560 (vir_bytes) dst_fds, fd_setsize);
561 if (r != OK) return(r);
564 /* write set */
565 src_fds = (direction == FROM_PROC) ? se->vir_writefds : &se->ready_writefds;
566 dst_fds = (direction == FROM_PROC) ? &se->writefds : se->vir_writefds;
567 if (se->vir_writefds) {
568 r = sys_vircopy(src_e, (vir_bytes) src_fds, dst_e,
569 (vir_bytes) dst_fds, fd_setsize);
570 if (r != OK) return(r);
573 /* error set */
574 src_fds = (direction == FROM_PROC) ? se->vir_errorfds : &se->ready_errorfds;
575 dst_fds = (direction == FROM_PROC) ? &se->errorfds : se->vir_errorfds;
576 if (se->vir_errorfds) {
577 r = sys_vircopy(src_e, (vir_bytes) src_fds, dst_e,
578 (vir_bytes) dst_fds, fd_setsize);
579 if (r != OK) return(r);
582 return(OK);
586 /*===========================================================================*
587 * select_cancel_all *
588 *===========================================================================*/
589 static void select_cancel_all(struct selectentry *se)
591 /* Cancel select. Decrease select usage and cancel timer */
593 int fd;
594 struct filp *f;
596 for (fd = 0; fd < se->nfds; fd++) {
597 if ((f = se->filps[fd]) == NULL) continue;
598 se->filps[fd] = NULL;
599 select_cancel_filp(f);
602 if (se->expiry > 0) {
603 cancel_timer(&se->timer);
604 se->expiry = 0;
607 se->requestor = NULL;
610 /*===========================================================================*
611 * select_cancel_filp *
612 *===========================================================================*/
613 static void select_cancel_filp(struct filp *f)
615 /* Reduce number of select users of this filp */
617 assert(f);
618 assert(f->filp_selectors >= 0);
619 if (f->filp_selectors == 0) return;
620 if (f->filp_count == 0) return;
622 select_lock_filp(f, f->filp_select_ops);
624 f->filp_selectors--;
625 if (f->filp_selectors == 0) {
626 /* No one selecting on this filp anymore, forget about select state */
627 f->filp_select_ops = 0;
628 f->filp_select_flags = 0;
629 f->filp_pipe_select_ops = 0;
632 unlock_filp(f);
635 /*===========================================================================*
636 * select_return *
637 *===========================================================================*/
638 static void select_return(struct selectentry *se)
640 int r, r1;
642 assert(!is_deferred(se)); /* Not done yet, first wait for async reply */
644 select_cancel_all(se);
646 r1 = copy_fdsets(se, se->nfds, TO_PROC);
647 if (r1 != OK)
648 r = r1;
649 else if (se->error != OK)
650 r = se->error;
651 else
652 r = se->nreadyfds;
654 revive(se->req_endpt, r);
658 /*===========================================================================*
659 * select_callback *
660 *===========================================================================*/
661 void select_callback(struct filp *f, int status)
663 filp_status(f, status);
666 /*===========================================================================*
667 * init_select *
668 *===========================================================================*/
669 void init_select(void)
671 int s;
673 for (s = 0; s < MAXSELECTS; s++)
674 init_timer(&selecttab[s].timer);
678 /*===========================================================================*
679 * select_forget *
680 *===========================================================================*/
681 void select_forget(endpoint_t proc_e)
683 /* Something has happened (e.g. signal delivered that interrupts select()).
684 * Totally forget about the select(). */
686 int slot;
687 struct selectentry *se;
689 for (slot = 0; slot < MAXSELECTS; slot++) {
690 se = &selecttab[slot];
691 if (se->requestor != NULL && se->req_endpt == proc_e)
692 break;
695 if (slot >= MAXSELECTS) return; /* Entry not found */
696 se->error = EINTR;
697 if (is_deferred(se)) return; /* Still awaiting initial reply */
699 select_cancel_all(se);
703 /*===========================================================================*
704 * select_timeout_check *
705 *===========================================================================*/
706 void select_timeout_check(timer_t *timer)
708 int s;
709 struct selectentry *se;
711 s = tmr_arg(timer)->ta_int;
712 if (s < 0 || s >= MAXSELECTS) return; /* Entry does not exist */
714 se = &selecttab[s];
715 if (se->requestor == NULL) return;
716 fp = se->requestor;
717 if (se->expiry <= 0) return; /* Strange, did we even ask for a timeout? */
718 se->expiry = 0;
719 if (is_deferred(se)) return; /* Wait for initial replies to DEV_SELECT */
720 select_return(se);
724 /*===========================================================================*
725 * select_unsuspend_by_endpt *
726 *===========================================================================*/
727 void select_unsuspend_by_endpt(endpoint_t proc_e)
729 /* Revive blocked processes when a driver has disappeared */
731 int fd, s, major;
732 struct selectentry *se;
733 struct filp *f;
735 for (s = 0; s < MAXSELECTS; s++) {
736 int wakehim = 0;
737 se = &selecttab[s];
738 if (se->requestor == NULL) continue;
739 if (se->requestor->fp_endpoint == proc_e) {
740 assert(se->requestor->fp_flags & FP_EXITING);
741 select_cancel_all(se);
742 continue;
745 for (fd = 0; fd < se->nfds; fd++) {
746 if ((f = se->filps[fd]) == NULL || f->filp_vno == NULL)
747 continue;
749 major = major(f->filp_vno->v_sdev);
750 if (dmap_driver_match(proc_e, major)) {
751 se->filps[fd] = NULL;
752 se->error = EINTR;
753 select_cancel_filp(f);
754 wakehim = 1;
758 if (wakehim && !is_deferred(se))
759 select_return(se);
763 /*===========================================================================*
764 * select_reply1 *
765 *===========================================================================*/
766 void select_reply1(driver_e, minor, status)
767 endpoint_t driver_e;
768 int minor;
769 int status;
771 /* Handle reply to DEV_SELECT request */
773 int major;
774 dev_t dev;
775 struct filp *f;
776 struct dmap *dp;
777 struct vnode *vp;
779 /* Figure out which device is replying */
780 if ((dp = get_dmap(driver_e)) == NULL) return;
782 major = dp-dmap;
783 dev = makedev(major, minor);
785 /* Get filp belonging to character special file */
786 if ((f = dp->dmap_sel_filp) == NULL) {
787 printf("VFS (%s:%d): major %d was not expecting a DEV_SELECT reply\n",
788 __FILE__, __LINE__, major);
789 return;
792 /* Is the filp still in use and busy waiting for a reply? The owner might
793 * have vanished before the driver was able to reply. */
794 if (f->filp_count >= 1 && (f->filp_select_flags & FSF_BUSY)) {
795 /* Find vnode and check we got a reply from the device we expected */
796 vp = f->filp_vno;
797 assert(vp != NULL);
798 assert(S_ISCHR(vp->v_mode));
799 if (vp->v_sdev != dev) {
800 printf("VFS (%s:%d): expected reply from dev %d not %d\n",
801 __FILE__, __LINE__, vp->v_sdev, dev);
802 return;
806 /* No longer waiting for a reply from this device */
807 dp->dmap_sel_filp = NULL;
809 /* Process select result only if requestor is still around. That is, the
810 * corresponding filp is still in use.
812 if (f->filp_count >= 1) {
813 select_lock_filp(f, f->filp_select_ops);
814 f->filp_select_flags &= ~FSF_BUSY;
816 /* The select call is done now, except when
817 * - another process started a select on the same filp with possibly a
818 * different set of operations.
819 * - a process does a select on the same filp but using different file
820 * descriptors.
821 * - the select has a timeout. Upon receiving this reply the operations
822 * might not be ready yet, so we want to wait for that to ultimately
823 * happen.
824 * Therefore we need to keep remembering what the operations are.
826 if (!(f->filp_select_flags & (FSF_UPDATE|FSF_BLOCKED)))
827 f->filp_select_ops = 0; /* done selecting */
828 else if (!(f->filp_select_flags & FSF_UPDATE))
829 /* there may be operations pending */
830 f->filp_select_ops &= ~status;
832 /* Record new filp status */
833 if (!(status == 0 && (f->filp_select_flags & FSF_BLOCKED))) {
834 if (status > 0) { /* operations ready */
835 if (status & SEL_RD)
836 f->filp_select_flags &= ~FSF_RD_BLOCK;
837 if (status & SEL_WR)
838 f->filp_select_flags &= ~FSF_WR_BLOCK;
839 if (status & SEL_ERR)
840 f->filp_select_flags &= ~FSF_ERR_BLOCK;
841 } else if (status < 0) { /* error */
842 /* Always unblock upon error */
843 f->filp_select_flags &= ~FSF_BLOCKED;
847 unlock_filp(f);
848 filp_status(f, status); /* Tell filp owners about the results */
851 select_restart_filps();
855 /*===========================================================================*
856 * select_reply2 *
857 *===========================================================================*/
858 void select_reply2(driver_e, minor, status)
859 endpoint_t driver_e;
860 int minor;
861 int status;
863 /* Handle secondary reply to DEV_SELECT request. A secondary reply occurs when
864 * the select request is 'blocking' until an operation becomes ready. */
865 int major, slot, fd;
866 dev_t dev;
867 struct filp *f;
868 struct dmap *dp;
869 struct vnode *vp;
870 struct selectentry *se;
872 if (status == 0) {
873 printf("VFS (%s:%d): weird status (%d) to report\n",
874 __FILE__, __LINE__, status);
875 return;
878 /* Figure out which device is replying */
879 if ((dp = get_dmap(driver_e)) == NULL) {
880 printf("VFS (%s:%d): endpoint %d is not a known driver endpoint\n",
881 __FILE__, __LINE__, driver_e);
882 return;
884 major = dp-dmap;
885 dev = makedev(major, minor);
887 /* Find all file descriptors selecting for this device */
888 for (slot = 0; slot < MAXSELECTS; slot++) {
889 se = &selecttab[slot];
890 if (se->requestor == NULL) continue; /* empty slot */
892 for (fd = 0; fd < se->nfds; fd++) {
893 if ((f = se->filps[fd]) == NULL) continue;
894 if ((vp = f->filp_vno) == NULL) continue;
895 if (!S_ISCHR(vp->v_mode)) continue;
896 if (vp->v_sdev != dev) continue;
898 select_lock_filp(f, f->filp_select_ops);
899 if (status > 0) { /* Operations ready */
900 /* Clear the replied bits from the request
901 * mask unless FSF_UPDATE is set.
903 if (!(f->filp_select_flags & FSF_UPDATE))
904 f->filp_select_ops &= ~status;
905 if (status & SEL_RD)
906 f->filp_select_flags &= ~FSF_RD_BLOCK;
907 if (status & SEL_WR)
908 f->filp_select_flags &= ~FSF_WR_BLOCK;
909 if (status & SEL_ERR)
910 f->filp_select_flags &= ~FSF_ERR_BLOCK;
912 ops2tab(status, fd, se);
913 } else {
914 f->filp_select_flags &= ~FSF_BLOCKED;
915 ops2tab(SEL_RD|SEL_WR|SEL_ERR, fd, se);
917 unlock_filp(f);
918 if (se->nreadyfds > 0) restart_proc(se);
922 select_restart_filps();
925 /*===========================================================================*
926 * select_restart_filps *
927 *===========================================================================*/
928 static void select_restart_filps()
930 int fd, slot;
931 struct filp *f;
932 struct vnode *vp;
933 struct selectentry *se;
935 /* Locate filps that can be restarted */
936 for (slot = 0; slot < MAXSELECTS; slot++) {
937 se = &selecttab[slot];
938 if (se->requestor == NULL) continue; /* empty slot */
940 /* Only 'deferred' processes are eligible to restart */
941 if (!is_deferred(se)) continue;
943 /* Find filps that are not waiting for a reply, but have an updated
944 * status (i.e., another select on the same filp with possibly a
945 * different set of operations is to be done), and thus requires the
946 * select request to be sent again).
948 for (fd = 0; fd < se->nfds; fd++) {
949 int r, wantops, ops;
950 if ((f = se->filps[fd]) == NULL) continue;
951 if (f->filp_select_flags & FSF_BUSY) /* Still waiting for */
952 continue; /* initial reply */
953 if (!(f->filp_select_flags & FSF_UPDATE)) /* Must be in */
954 continue; /* 'update' state */
956 wantops = ops = f->filp_select_ops;
957 vp = f->filp_vno;
958 assert(S_ISCHR(vp->v_mode));
959 r = do_select_request(se, fd, &wantops);
960 if (r != OK && r != SUSPEND)
961 break; /* Error or bogus return code; abort */
962 if (wantops & ops) ops2tab(wantops, fd, se);
967 /*===========================================================================*
968 * do_select_request *
969 *===========================================================================*/
970 static int do_select_request(se, fd, ops)
971 struct selectentry *se;
972 int fd;
973 int *ops;
975 /* Perform actual select request for file descriptor fd */
977 int r, type;
978 struct filp *f;
980 type = se->type[fd];
981 f = se->filps[fd];
982 select_lock_filp(f, *ops);
983 r = fdtypes[type].select_request(f, ops, se->block);
984 unlock_filp(f);
985 if (r != OK && r != SUSPEND) {
986 se->error = EINTR;
987 se->block = 0; /* Stop blocking to return asap */
988 if (!is_deferred(se)) select_cancel_all(se);
991 return(r);
994 /*===========================================================================*
995 * filp_status *
996 *===========================================================================*/
997 static void filp_status(f, status)
998 struct filp *f;
999 int status;
1001 /* Tell processes that need to know about the status of this filp */
1002 int fd, slot;
1003 struct selectentry *se;
1005 for (slot = 0; slot < MAXSELECTS; slot++) {
1006 se = &selecttab[slot];
1007 if (se->requestor == NULL) continue; /* empty slot */
1009 for (fd = 0; fd < se->nfds; fd++) {
1010 if (se->filps[fd] != f) continue;
1011 if (status < 0)
1012 ops2tab(SEL_RD|SEL_WR|SEL_ERR, fd, se);
1013 else
1014 ops2tab(status, fd, se);
1015 restart_proc(se);
1020 /*===========================================================================*
1021 * restart_proc *
1022 *===========================================================================*/
1023 static void restart_proc(se)
1024 struct selectentry *se;
1026 /* Tell process about select results (if any) unless there are still results
1027 * pending. */
1029 if ((se->nreadyfds > 0 || !se->block) && !is_deferred(se))
1030 select_return(se);
1033 /*===========================================================================*
1034 * wipe_select *
1035 *===========================================================================*/
1036 static void wipe_select(struct selectentry *se)
1038 se->nfds = 0;
1039 se->nreadyfds = 0;
1040 se->error = OK;
1041 se->block = 0;
1042 memset(se->filps, 0, sizeof(se->filps));
1044 FD_ZERO(&se->readfds);
1045 FD_ZERO(&se->writefds);
1046 FD_ZERO(&se->errorfds);
1047 FD_ZERO(&se->ready_readfds);
1048 FD_ZERO(&se->ready_writefds);
1049 FD_ZERO(&se->ready_errorfds);
1052 /*===========================================================================*
1053 * select_lock_filp *
1054 *===========================================================================*/
1055 static void select_lock_filp(struct filp *f, int ops)
1057 /* Lock a filp and vnode based on which operations are requested */
1058 tll_access_t locktype;;
1060 locktype = VNODE_READ; /* By default */
1062 if (ops & (SEL_WR|SEL_ERR))
1063 /* Selecting for error or writing requires exclusive access */
1064 locktype = VNODE_WRITE;
1066 lock_filp(f, locktype);