2005-06-06 13:40:32 +02:00
|
|
|
/* Implement entry point to select system call.
|
|
|
|
*
|
|
|
|
* The entry points into this file are
|
2005-06-17 15:41:12 +02:00
|
|
|
* do_select: perform the SELECT system call
|
|
|
|
* select_callback: notify select system of possible fd operation
|
|
|
|
* select_notified: low-level entry for device notifying select
|
2005-07-08 19:30:01 +02:00
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
* 6 june 2005 Created (Ben Gras)
|
2005-06-06 13:40:32 +02:00
|
|
|
*/
|
|
|
|
|
2005-07-27 16:30:25 +02:00
|
|
|
#define DEBUG_SELECT 0
|
2005-06-17 15:41:12 +02:00
|
|
|
|
2005-06-06 13:40:32 +02:00
|
|
|
#include "fs.h"
|
2005-06-17 15:41:12 +02:00
|
|
|
#include "select.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "inode.h"
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* max. number of simultaneously pending select() calls */
|
|
|
|
#define MAXSELECTS 25
|
|
|
|
|
|
|
|
PRIVATE struct selectentry {
|
|
|
|
struct fproc *requestor; /* slot is free iff this is NULL */
|
|
|
|
int req_procnr;
|
|
|
|
fd_set readfds, writefds, errorfds;
|
|
|
|
fd_set ready_readfds, ready_writefds, ready_errorfds;
|
|
|
|
fd_set *vir_readfds, *vir_writefds, *vir_errorfds;
|
|
|
|
struct filp *filps[FD_SETSIZE];
|
|
|
|
int type[FD_SETSIZE];
|
|
|
|
int nfds, nreadyfds;
|
|
|
|
clock_t expiry;
|
|
|
|
timer_t timer; /* if expiry > 0 */
|
|
|
|
} selecttab[MAXSELECTS];
|
|
|
|
|
|
|
|
#define SELFD_FILE 0
|
|
|
|
#define SELFD_PIPE 1
|
|
|
|
#define SELFD_TTY 2
|
|
|
|
#define SELFD_INET 3
|
2005-07-08 19:30:01 +02:00
|
|
|
#define SELFD_LOG 4
|
|
|
|
#define SEL_FDS 5
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
FORWARD _PROTOTYPE(int select_reevaluate, (struct filp *fp));
|
|
|
|
|
2005-08-29 18:47:18 +02:00
|
|
|
FORWARD _PROTOTYPE(int select_request_file,
|
|
|
|
(struct filp *f, int *ops, int block));
|
2005-06-17 15:41:12 +02:00
|
|
|
FORWARD _PROTOTYPE(int select_match_file, (struct filp *f));
|
|
|
|
|
2005-08-29 18:47:18 +02:00
|
|
|
FORWARD _PROTOTYPE(int select_request_general,
|
|
|
|
(struct filp *f, int *ops, int block));
|
|
|
|
FORWARD _PROTOTYPE(int select_major_match,
|
|
|
|
(int match_major, struct filp *file));
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
FORWARD _PROTOTYPE(void select_cancel_all, (struct selectentry *e));
|
2005-08-25 14:30:43 +02:00
|
|
|
FORWARD _PROTOTYPE(void select_wakeup, (struct selectentry *e));
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
/* The Open Group:
|
|
|
|
* "The pselect() and select() functions shall support
|
|
|
|
* regular files, terminal and pseudo-terminal devices,
|
|
|
|
* STREAMS-based files, FIFOs, pipes, and sockets."
|
|
|
|
*/
|
|
|
|
|
|
|
|
PRIVATE struct fdtype {
|
|
|
|
int (*select_request)(struct filp *, int *ops, int block);
|
|
|
|
int (*select_match)(struct filp *);
|
2005-07-08 19:30:01 +02:00
|
|
|
int select_major;
|
2005-06-17 15:41:12 +02:00
|
|
|
} fdtypes[SEL_FDS] = {
|
|
|
|
/* SELFD_FILE */
|
2005-07-08 19:30:01 +02:00
|
|
|
{ select_request_file, select_match_file, 0 },
|
2005-06-17 15:41:12 +02:00
|
|
|
/* SELFD_TTY (also PTY) */
|
2005-07-22 20:28:32 +02:00
|
|
|
{ select_request_general, NULL, TTY_MAJOR },
|
2005-06-17 15:41:12 +02:00
|
|
|
/* SELFD_INET */
|
2005-07-22 20:28:32 +02:00
|
|
|
{ select_request_general, NULL, INET_MAJOR },
|
2005-06-17 15:41:12 +02:00
|
|
|
/* SELFD_PIPE (pipe(2) pipes and FS FIFOs) */
|
2005-07-08 19:30:01 +02:00
|
|
|
{ select_request_pipe, select_match_pipe, 0 },
|
|
|
|
/* SELFD_LOG (/dev/klog) */
|
2005-07-22 20:28:32 +02:00
|
|
|
{ select_request_general, NULL, LOG_MAJOR },
|
2005-06-17 15:41:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Open Group:
|
|
|
|
* "File descriptors associated with regular files shall always select true
|
|
|
|
* for ready to read, ready to write, and error conditions."
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* select_request_file *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int select_request_file(struct filp *f, int *ops, int block)
|
|
|
|
{
|
|
|
|
/* output *ops is input *ops */
|
|
|
|
return SEL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* select_match_file *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int select_match_file(struct filp *file)
|
|
|
|
{
|
2005-09-11 18:45:46 +02:00
|
|
|
if (file && file->filp_ino && (file->filp_ino->i_mode & I_REGULAR))
|
2005-06-17 15:41:12 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-07-22 20:28:32 +02:00
|
|
|
* select_request_general *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
2005-07-22 20:28:32 +02:00
|
|
|
PRIVATE int select_request_general(struct filp *f, int *ops, int block)
|
2005-06-17 15:41:12 +02:00
|
|
|
{
|
2005-08-25 14:30:43 +02:00
|
|
|
int rops = *ops;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (block) rops |= SEL_NOTIFY;
|
2005-06-17 15:41:12 +02:00
|
|
|
*ops = dev_io(DEV_SELECT, f->filp_ino->i_zone[0], rops, NULL, 0, 0, 0);
|
2005-09-11 18:45:46 +02:00
|
|
|
if (*ops < 0)
|
2005-06-17 15:41:12 +02:00
|
|
|
return SEL_ERR;
|
|
|
|
return SEL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-07-08 19:30:01 +02:00
|
|
|
* select_major_match *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
2005-07-08 19:30:01 +02:00
|
|
|
PRIVATE int select_major_match(int match_major, struct filp *file)
|
2005-06-17 15:41:12 +02:00
|
|
|
{
|
|
|
|
int major;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!(file && file->filp_ino &&
|
2005-06-17 15:41:12 +02:00
|
|
|
(file->filp_ino->i_mode & I_TYPE) == I_CHAR_SPECIAL))
|
|
|
|
return 0;
|
|
|
|
major = (file->filp_ino->i_zone[0] >> MAJOR) & BYTE;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (major == match_major)
|
2005-06-28 16:59:00 +02:00
|
|
|
return 1;
|
2005-06-17 15:41:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* tab2ops *
|
|
|
|
*===========================================================================*/
|
2005-06-17 15:41:12 +02:00
|
|
|
PRIVATE int tab2ops(int fd, struct selectentry *e)
|
|
|
|
{
|
|
|
|
return (FD_ISSET(fd, &e->readfds) ? SEL_RD : 0) |
|
|
|
|
(FD_ISSET(fd, &e->writefds) ? SEL_WR : 0) |
|
|
|
|
(FD_ISSET(fd, &e->errorfds) ? SEL_ERR : 0);
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* ops2tab *
|
|
|
|
*===========================================================================*/
|
2005-06-17 15:41:12 +02:00
|
|
|
PRIVATE void ops2tab(int ops, int fd, struct selectentry *e)
|
|
|
|
{
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((ops & SEL_RD) && e->vir_readfds && FD_ISSET(fd, &e->readfds)
|
2005-07-08 19:30:01 +02:00
|
|
|
&& !FD_ISSET(fd, &e->ready_readfds)) {
|
2005-06-17 15:41:12 +02:00
|
|
|
FD_SET(fd, &e->ready_readfds);
|
|
|
|
e->nreadyfds++;
|
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((ops & SEL_WR) && e->vir_writefds && FD_ISSET(fd, &e->writefds)
|
2005-06-17 15:41:12 +02:00
|
|
|
&& !FD_ISSET(fd, &e->ready_writefds)) {
|
|
|
|
FD_SET(fd, &e->ready_writefds);
|
|
|
|
e->nreadyfds++;
|
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((ops & SEL_ERR) && e->vir_errorfds && FD_ISSET(fd, &e->errorfds)
|
2005-06-17 15:41:12 +02:00
|
|
|
&& !FD_ISSET(fd, &e->ready_errorfds)) {
|
|
|
|
FD_SET(fd, &e->ready_errorfds);
|
|
|
|
e->nreadyfds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* copy_fdsets *
|
|
|
|
*===========================================================================*/
|
2005-06-17 15:41:12 +02:00
|
|
|
PRIVATE void copy_fdsets(struct selectentry *e)
|
|
|
|
{
|
2005-09-11 18:45:46 +02:00
|
|
|
if (e->vir_readfds)
|
2005-06-17 15:41:12 +02:00
|
|
|
sys_vircopy(SELF, D, (vir_bytes) &e->ready_readfds,
|
|
|
|
e->req_procnr, D, (vir_bytes) e->vir_readfds, sizeof(fd_set));
|
2005-09-11 18:45:46 +02:00
|
|
|
if (e->vir_writefds)
|
2005-06-17 15:41:12 +02:00
|
|
|
sys_vircopy(SELF, D, (vir_bytes) &e->ready_writefds,
|
|
|
|
e->req_procnr, D, (vir_bytes) e->vir_writefds, sizeof(fd_set));
|
2005-09-11 18:45:46 +02:00
|
|
|
if (e->vir_errorfds)
|
2005-06-17 15:41:12 +02:00
|
|
|
sys_vircopy(SELF, D, (vir_bytes) &e->ready_errorfds,
|
|
|
|
e->req_procnr, D, (vir_bytes) e->vir_errorfds, sizeof(fd_set));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2005-06-06 13:40:32 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* do_select *
|
2005-06-06 13:40:32 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_select(void)
|
|
|
|
{
|
2005-06-17 15:41:12 +02:00
|
|
|
int r, nfds, is_timeout = 1, nonzero_timeout = 0,
|
|
|
|
fd, s, block = 0;
|
|
|
|
struct timeval timeout;
|
|
|
|
nfds = m_in.SEL_NFDS;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (nfds < 0 || nfds > FD_SETSIZE)
|
2005-06-17 15:41:12 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
for(s = 0; s < MAXSELECTS; s++)
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].requestor)
|
2005-06-17 15:41:12 +02:00
|
|
|
break;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (s >= MAXSELECTS)
|
2005-06-17 15:41:12 +02:00
|
|
|
return ENOSPC;
|
|
|
|
|
|
|
|
selecttab[s].req_procnr = who;
|
|
|
|
selecttab[s].nfds = 0;
|
|
|
|
selecttab[s].nreadyfds = 0;
|
|
|
|
memset(selecttab[s].filps, 0, sizeof(selecttab[s].filps));
|
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
FD_ZERO(&selecttab[s].readfds);
|
|
|
|
FD_ZERO(&selecttab[s].writefds);
|
|
|
|
FD_ZERO(&selecttab[s].errorfds);
|
|
|
|
FD_ZERO(&selecttab[s].ready_readfds);
|
|
|
|
FD_ZERO(&selecttab[s].ready_writefds);
|
|
|
|
FD_ZERO(&selecttab[s].ready_errorfds);
|
|
|
|
|
|
|
|
selecttab[s].vir_readfds = (fd_set *) m_in.SEL_READFDS;
|
|
|
|
selecttab[s].vir_writefds = (fd_set *) m_in.SEL_WRITEFDS;
|
|
|
|
selecttab[s].vir_errorfds = (fd_set *) m_in.SEL_ERRORFDS;
|
|
|
|
|
|
|
|
/* copy args */
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].vir_readfds
|
2005-08-29 18:47:18 +02:00
|
|
|
&& (r=sys_vircopy(who, D, (vir_bytes) m_in.SEL_READFDS,
|
2005-06-17 15:41:12 +02:00
|
|
|
SELF, D, (vir_bytes) &selecttab[s].readfds, sizeof(fd_set))) != OK)
|
|
|
|
return r;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].vir_writefds
|
2005-08-29 18:47:18 +02:00
|
|
|
&& (r=sys_vircopy(who, D, (vir_bytes) m_in.SEL_WRITEFDS,
|
2005-06-17 15:41:12 +02:00
|
|
|
SELF, D, (vir_bytes) &selecttab[s].writefds, sizeof(fd_set))) != OK)
|
|
|
|
return r;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].vir_errorfds
|
2005-08-29 18:47:18 +02:00
|
|
|
&& (r=sys_vircopy(who, D, (vir_bytes) m_in.SEL_ERRORFDS,
|
2005-06-17 15:41:12 +02:00
|
|
|
SELF, D, (vir_bytes) &selecttab[s].errorfds, sizeof(fd_set))) != OK)
|
|
|
|
return r;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!m_in.SEL_TIMEOUT)
|
2005-06-17 15:41:12 +02:00
|
|
|
is_timeout = nonzero_timeout = 0;
|
|
|
|
else
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((r=sys_vircopy(who, D, (vir_bytes) m_in.SEL_TIMEOUT,
|
2005-06-17 15:41:12 +02:00
|
|
|
SELF, D, (vir_bytes) &timeout, sizeof(timeout))) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* No nonsense in the timeval please. */
|
2005-09-11 18:45:46 +02:00
|
|
|
if (is_timeout && (timeout.tv_sec < 0 || timeout.tv_usec < 0))
|
2005-06-17 15:41:12 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* if is_timeout if 0, we block forever. otherwise, if nonzero_timeout
|
|
|
|
* is 0, we do a poll (don't block). otherwise, we block up to the
|
|
|
|
* specified time interval.
|
|
|
|
*/
|
2005-09-11 18:45:46 +02:00
|
|
|
if (is_timeout && (timeout.tv_sec > 0 || timeout.tv_usec > 0))
|
2005-06-17 15:41:12 +02:00
|
|
|
nonzero_timeout = 1;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (nonzero_timeout || !is_timeout)
|
2005-06-17 15:41:12 +02:00
|
|
|
block = 1;
|
|
|
|
else
|
|
|
|
block = 0; /* timeout set as (0,0) - this effects a poll */
|
|
|
|
|
|
|
|
/* no timeout set (yet) */
|
|
|
|
selecttab[s].expiry = 0;
|
|
|
|
|
|
|
|
for(fd = 0; fd < nfds; fd++) {
|
|
|
|
int orig_ops, ops, t, type = -1, r;
|
|
|
|
struct filp *filp;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!(orig_ops = ops = tab2ops(fd, &selecttab[s])))
|
2005-06-17 15:41:12 +02:00
|
|
|
continue;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!(filp = selecttab[s].filps[fd] = get_filp(fd))) {
|
2005-06-17 15:41:12 +02:00
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
return EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(t = 0; t < SEL_FDS; t++) {
|
2005-09-11 18:45:46 +02:00
|
|
|
if (fdtypes[t].select_match) {
|
|
|
|
if (fdtypes[t].select_match(filp)) {
|
2005-06-17 15:41:12 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select: fd %d is type %d ", fd, t);
|
|
|
|
#endif
|
2005-09-11 18:45:46 +02:00
|
|
|
if (type != -1)
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("select: double match\n");
|
|
|
|
type = t;
|
2005-07-08 19:30:01 +02:00
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
} else if (select_major_match(fdtypes[t].select_major, filp)) {
|
2005-07-08 19:30:01 +02:00
|
|
|
type = t;
|
2005-06-17 15:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open Group:
|
|
|
|
* "The pselect() and select() functions shall support
|
|
|
|
* regular files, terminal and pseudo-terminal devices,
|
|
|
|
* STREAMS-based files, FIFOs, pipes, and sockets. The
|
|
|
|
* behavior of pselect() and select() on file descriptors
|
|
|
|
* that refer to other types of file is unspecified."
|
|
|
|
*
|
|
|
|
* If all types are implemented, then this is another
|
|
|
|
* type of file and we get to do whatever we want.
|
|
|
|
*/
|
2005-09-11 18:45:46 +02:00
|
|
|
if (type == -1)
|
2005-06-28 16:59:00 +02:00
|
|
|
{
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
2005-06-28 16:59:00 +02:00
|
|
|
printf("do_select: bad type\n");
|
2005-07-08 19:30:01 +02:00
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
return EBADF;
|
2005-06-28 16:59:00 +02:00
|
|
|
}
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
selecttab[s].type[fd] = type;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((selecttab[s].filps[fd]->filp_select_ops & ops) != ops) {
|
2005-06-17 15:41:12 +02:00
|
|
|
int wantops;
|
|
|
|
/* Request the select on this fd. */
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("%p requesting ops %d -> ",
|
|
|
|
selecttab[s].filps[fd],
|
|
|
|
selecttab[s].filps[fd]->filp_select_ops);
|
|
|
|
#endif
|
|
|
|
wantops = (selecttab[s].filps[fd]->filp_select_ops |= ops);
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("%d\n", selecttab[s].filps[fd]->filp_select_ops);
|
|
|
|
#endif
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((r = fdtypes[type].select_request(filp,
|
2005-06-17 15:41:12 +02:00
|
|
|
&wantops, block)) != SEL_OK) {
|
|
|
|
/* error or bogus return code.. backpaddle */
|
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
printf("select: select_request returned error\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
if (wantops) {
|
|
|
|
if (wantops & ops) {
|
2005-06-17 15:41:12 +02:00
|
|
|
/* operations that were just requested
|
|
|
|
* are ready to go right away
|
|
|
|
*/
|
|
|
|
ops2tab(wantops, fd, &selecttab[s]);
|
|
|
|
}
|
|
|
|
/* if there are any other select()s blocking
|
|
|
|
* on these operations of this fp, they can
|
|
|
|
* be awoken too
|
|
|
|
*/
|
|
|
|
select_callback(filp, ops);
|
|
|
|
}
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select request ok; ops returned %d\n", wantops);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select already happening on that filp\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
selecttab[s].nfds = fd+1;
|
|
|
|
selecttab[s].filps[fd]->filp_selectors++;
|
|
|
|
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("[fd %d ops: %d] ", fd, ops);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].nreadyfds > 0 || !block) {
|
2005-06-17 15:41:12 +02:00
|
|
|
/* fd's were found that were ready to go right away, and/or
|
|
|
|
* we were instructed not to block at all. Must return
|
|
|
|
* immediately.
|
|
|
|
*/
|
|
|
|
copy_fdsets(&selecttab[s]);
|
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
selecttab[s].requestor = NULL;
|
|
|
|
|
|
|
|
/* Open Group:
|
|
|
|
* "Upon successful completion, the pselect() and select()
|
|
|
|
* functions shall return the total number of bits
|
|
|
|
* set in the bit masks."
|
|
|
|
*/
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("returning\n");
|
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
return selecttab[s].nreadyfds;
|
|
|
|
}
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("not returning (%d, %d)\n", selecttab[s].nreadyfds, block);
|
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
/* Convert timeval to ticks and set the timer. If it fails, undo
|
|
|
|
* all, return error.
|
|
|
|
*/
|
2005-09-11 18:45:46 +02:00
|
|
|
if (is_timeout) {
|
2005-06-17 15:41:12 +02:00
|
|
|
int ticks;
|
|
|
|
/* Open Group:
|
|
|
|
* "If the requested timeout interval requires a finer
|
|
|
|
* granularity than the implementation supports, the
|
|
|
|
* actual timeout interval shall be rounded up to the next
|
|
|
|
* supported value."
|
|
|
|
*/
|
|
|
|
#define USECPERSEC 1000000
|
|
|
|
while(timeout.tv_usec >= USECPERSEC) {
|
|
|
|
/* this is to avoid overflow with *HZ below */
|
|
|
|
timeout.tv_usec -= USECPERSEC;
|
|
|
|
timeout.tv_sec++;
|
|
|
|
}
|
|
|
|
ticks = timeout.tv_sec * HZ +
|
|
|
|
(timeout.tv_usec * HZ + USECPERSEC-1) / USECPERSEC;
|
|
|
|
selecttab[s].expiry = ticks;
|
|
|
|
fs_set_timer(&selecttab[s].timer, ticks, select_timeout_check, s);
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("%d: blocking %d ticks\n", s, ticks);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're blocking, the table entry is now valid. */
|
|
|
|
selecttab[s].requestor = fp;
|
|
|
|
|
|
|
|
/* process now blocked */
|
|
|
|
suspend(XSELECT);
|
|
|
|
return SUSPEND;
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* select_cancel_all *
|
|
|
|
*===========================================================================*/
|
2005-06-17 15:41:12 +02:00
|
|
|
PRIVATE void select_cancel_all(struct selectentry *e)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
for(fd = 0; fd < e->nfds; fd++) {
|
|
|
|
struct filp *fp;
|
|
|
|
fp = e->filps[fd];
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!fp) {
|
2005-06-17 15:41:12 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("[ fd %d/%d NULL ] ", fd, e->nfds);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
if (fp->filp_selectors < 1) {
|
2005-06-17 15:41:12 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select: %d selectors?!\n", fp->filp_selectors);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fp->filp_selectors--;
|
|
|
|
e->filps[fd] = NULL;
|
|
|
|
select_reevaluate(fp);
|
|
|
|
}
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (e->expiry > 0) {
|
2005-06-17 15:41:12 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("cancelling timer %d\n", e - selecttab);
|
|
|
|
#endif
|
|
|
|
fs_cancel_timer(&e->timer);
|
|
|
|
e->expiry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* select_wakeup *
|
|
|
|
*===========================================================================*/
|
2005-08-25 14:30:43 +02:00
|
|
|
PRIVATE void select_wakeup(struct selectentry *e)
|
2005-06-17 15:41:12 +02:00
|
|
|
{
|
|
|
|
/* Open Group:
|
|
|
|
* "Upon successful completion, the pselect() and select()
|
|
|
|
* functions shall return the total number of bits
|
|
|
|
* set in the bit masks."
|
|
|
|
*/
|
|
|
|
revive(e->req_procnr, e->nreadyfds);
|
|
|
|
}
|
|
|
|
|
2005-09-22 23:17:22 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* select_reevaluate *
|
|
|
|
*===========================================================================*/
|
2005-06-17 15:41:12 +02:00
|
|
|
PRIVATE int select_reevaluate(struct filp *fp)
|
|
|
|
{
|
|
|
|
int s, remain_ops = 0, fd, type = -1;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!fp) {
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("fs: select: reevalute NULL fp\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(s = 0; s < MAXSELECTS; s++) {
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].requestor)
|
2005-06-17 15:41:12 +02:00
|
|
|
continue;
|
|
|
|
for(fd = 0; fd < selecttab[s].nfds; fd++)
|
2005-09-11 18:45:46 +02:00
|
|
|
if (fp == selecttab[s].filps[fd]) {
|
2005-06-17 15:41:12 +02:00
|
|
|
remain_ops |= tab2ops(fd, &selecttab[s]);
|
|
|
|
type = selecttab[s].type[fd];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are any select()s open that want any operations on
|
|
|
|
* this fd that haven't been satisfied by this callback, then we're
|
|
|
|
* still in the market for it.
|
|
|
|
*/
|
|
|
|
fp->filp_select_ops = remain_ops;
|
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("remaining operations on fp are %d\n", fp->filp_select_ops);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return remain_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* select_callback *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int select_callback(struct filp *fp, int ops)
|
|
|
|
{
|
2005-08-25 14:30:43 +02:00
|
|
|
int s, fd, want_ops, type;
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
/* We are being notified that file pointer fp is available for
|
|
|
|
* operations 'ops'. We must re-register the select for
|
|
|
|
* operations that we are still interested in, if any.
|
|
|
|
*/
|
|
|
|
|
|
|
|
want_ops = 0;
|
|
|
|
type = -1;
|
|
|
|
for(s = 0; s < MAXSELECTS; s++) {
|
|
|
|
int wakehim = 0;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].requestor)
|
2005-06-17 15:41:12 +02:00
|
|
|
continue;
|
|
|
|
for(fd = 0; fd < selecttab[s].nfds; fd++) {
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].filps[fd])
|
2005-06-17 15:41:12 +02:00
|
|
|
continue;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].filps[fd] == fp) {
|
2005-06-17 15:41:12 +02:00
|
|
|
int this_want_ops;
|
|
|
|
this_want_ops = tab2ops(fd, &selecttab[s]);
|
|
|
|
want_ops |= this_want_ops;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (this_want_ops & ops) {
|
2005-06-17 15:41:12 +02:00
|
|
|
/* this select() has been satisfied. */
|
|
|
|
ops2tab(ops, fd, &selecttab[s]);
|
|
|
|
wakehim = 1;
|
|
|
|
}
|
|
|
|
type = selecttab[s].type[fd];
|
|
|
|
}
|
|
|
|
}
|
2005-09-11 18:45:46 +02:00
|
|
|
if (wakehim) {
|
2005-06-17 15:41:12 +02:00
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
copy_fdsets(&selecttab[s]);
|
|
|
|
selecttab[s].requestor = NULL;
|
|
|
|
select_wakeup(&selecttab[s]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* select_notified *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
2005-07-27 15:08:52 +02:00
|
|
|
PUBLIC int select_notified(int major, int minor, int selected_ops)
|
2005-06-17 15:41:12 +02:00
|
|
|
{
|
2005-07-27 15:08:52 +02:00
|
|
|
int s, f, t;
|
2005-06-17 15:41:12 +02:00
|
|
|
|
2005-07-27 15:08:52 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select callback: %d, %d: %d\n", major, minor, selected_ops);
|
|
|
|
#endif
|
2005-07-08 19:30:01 +02:00
|
|
|
|
|
|
|
for(t = 0; t < SEL_FDS; t++)
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!fdtypes[t].select_match && fdtypes[t].select_major == major)
|
2005-07-08 19:30:01 +02:00
|
|
|
break;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (t >= SEL_FDS) {
|
2005-07-27 15:08:52 +02:00
|
|
|
#if DEBUG_SELECT
|
|
|
|
printf("select callback: no fdtype found for device %d\n", major);
|
|
|
|
#endif
|
2005-07-08 19:30:01 +02:00
|
|
|
return OK;
|
2005-07-27 15:08:52 +02:00
|
|
|
}
|
2005-07-08 19:30:01 +02:00
|
|
|
|
|
|
|
/* We have a select callback from major device no.
|
|
|
|
* d, which corresponds to our select type t.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for(s = 0; s < MAXSELECTS; s++) {
|
2005-07-27 15:08:52 +02:00
|
|
|
int s_minor, ops;
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].requestor)
|
2005-07-08 19:30:01 +02:00
|
|
|
continue;
|
|
|
|
for(f = 0; f < selecttab[s].nfds; f++) {
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].filps[f] ||
|
2005-07-27 15:08:52 +02:00
|
|
|
!select_major_match(major, selecttab[s].filps[f]))
|
2005-07-08 19:30:01 +02:00
|
|
|
continue;
|
|
|
|
ops = tab2ops(f, &selecttab[s]);
|
2005-07-27 15:08:52 +02:00
|
|
|
s_minor = selecttab[s].filps[f]->filp_ino->i_zone[0] & BYTE;
|
2005-09-11 18:45:46 +02:00
|
|
|
if ((s_minor == minor) &&
|
2005-07-27 15:08:52 +02:00
|
|
|
(selected_ops & ops)) {
|
|
|
|
select_callback(selecttab[s].filps[f], (selected_ops & ops));
|
2005-06-28 16:59:00 +02:00
|
|
|
}
|
2005-07-08 19:30:01 +02:00
|
|
|
}
|
2005-06-17 15:41:12 +02:00
|
|
|
}
|
2005-07-08 19:30:01 +02:00
|
|
|
|
2005-06-06 13:40:32 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2005-07-27 15:08:52 +02:00
|
|
|
|
2005-07-22 20:28:32 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* init_select *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void init_select(void)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
for(s = 0; s < MAXSELECTS; s++)
|
|
|
|
fs_init_timer(&selecttab[s].timer);
|
|
|
|
}
|
2005-06-06 13:40:32 +02:00
|
|
|
|
2005-06-17 15:41:12 +02:00
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* select_forget *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void select_forget(int proc)
|
|
|
|
{
|
|
|
|
/* something has happened (e.g. signal delivered that interrupts
|
|
|
|
* select()). totally forget about the select().
|
|
|
|
*/
|
|
|
|
int s;
|
|
|
|
|
|
|
|
for(s = 0; s < MAXSELECTS; s++) {
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].requestor &&
|
2005-06-17 15:41:12 +02:00
|
|
|
selecttab[s].req_procnr == proc) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (s >= MAXSELECTS) {
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("select: cancelled select() not found");
|
2005-07-08 19:30:01 +02:00
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
selecttab[s].requestor = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* select_timeout_check *
|
2005-06-17 15:41:12 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void select_timeout_check(timer_t *timer)
|
|
|
|
{
|
2005-08-25 14:30:43 +02:00
|
|
|
int s;
|
2005-06-17 15:41:12 +02:00
|
|
|
|
|
|
|
s = tmr_arg(timer)->ta_int;
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (s < 0 || s >= MAXSELECTS) {
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("select: bogus slot arg to watchdog %d\n", s);
|
2005-07-08 19:30:01 +02:00
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (!selecttab[s].requestor) {
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("select: no requestor in watchdog\n");
|
2005-07-08 19:30:01 +02:00
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
if (selecttab[s].expiry <= 0) {
|
2005-07-08 19:30:01 +02:00
|
|
|
#if DEBUG_SELECT
|
2005-06-17 15:41:12 +02:00
|
|
|
printf("select: strange expiry value in watchdog\n", s);
|
2005-07-08 19:30:01 +02:00
|
|
|
#endif
|
2005-06-17 15:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
selecttab[s].expiry = 0;
|
|
|
|
copy_fdsets(&selecttab[s]);
|
|
|
|
select_cancel_all(&selecttab[s]);
|
|
|
|
selecttab[s].requestor = NULL;
|
|
|
|
select_wakeup(&selecttab[s]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|