1998-12-27 09:35:39 +01:00
|
|
|
/*
|
|
|
|
* Server main select() loop
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
#include "object.h"
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
struct timeout_user
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
struct timeout_user *next; /* next in sorted timeout list */
|
|
|
|
struct timeout_user *prev; /* prev in sorted timeout list */
|
|
|
|
struct timeval when; /* timeout expiry (absolute time) */
|
|
|
|
timeout_callback callback; /* callback function */
|
|
|
|
void *private; /* callback private data */
|
1998-12-27 09:35:39 +01:00
|
|
|
};
|
|
|
|
|
1999-09-27 13:46:56 +02:00
|
|
|
static struct select_user *users[FD_SETSIZE]; /* users array */
|
|
|
|
static fd_set read_set, write_set, except_set; /* current select sets */
|
1998-12-27 09:35:39 +01:00
|
|
|
static int nb_users; /* current number of users */
|
|
|
|
static int max_fd; /* max fd in use */
|
1999-05-16 18:57:49 +02:00
|
|
|
static struct timeout_user *timeout_head; /* sorted timeouts list head */
|
|
|
|
static struct timeout_user *timeout_tail; /* sorted timeouts list tail */
|
1998-12-27 09:35:39 +01:00
|
|
|
|
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* register a user */
|
|
|
|
void register_select_user( struct select_user *user )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
assert( !users[user->fd] );
|
|
|
|
|
|
|
|
users[user->fd] = user;
|
|
|
|
if (user->fd > max_fd) max_fd = user->fd;
|
1998-12-27 09:35:39 +01:00
|
|
|
nb_users++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a user */
|
1999-05-16 18:57:49 +02:00
|
|
|
void unregister_select_user( struct select_user *user )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
assert( users[user->fd] == user );
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
FD_CLR( user->fd, &read_set );
|
|
|
|
FD_CLR( user->fd, &write_set );
|
1999-09-27 13:46:56 +02:00
|
|
|
FD_CLR( user->fd, &except_set );
|
1999-05-16 18:57:49 +02:00
|
|
|
users[user->fd] = NULL;
|
|
|
|
if (max_fd == user->fd) while (max_fd && !users[max_fd]) max_fd--;
|
1998-12-27 09:35:39 +01:00
|
|
|
nb_users--;
|
|
|
|
}
|
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* set the events that select waits for on this fd */
|
|
|
|
void set_select_events( struct select_user *user, int events )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
assert( users[user->fd] == user );
|
|
|
|
if (events & READ_EVENT) FD_SET( user->fd, &read_set );
|
|
|
|
else FD_CLR( user->fd, &read_set );
|
|
|
|
if (events & WRITE_EVENT) FD_SET( user->fd, &write_set );
|
|
|
|
else FD_CLR( user->fd, &write_set );
|
1999-09-27 13:46:56 +02:00
|
|
|
if (events & EXCEPT_EVENT) FD_SET( user->fd, &except_set );
|
|
|
|
else FD_CLR( user->fd, &except_set );
|
1999-05-16 18:57:49 +02:00
|
|
|
}
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* check if events are pending */
|
|
|
|
int check_select_events( struct select_user *user, int events )
|
|
|
|
{
|
1999-09-27 13:46:56 +02:00
|
|
|
fd_set read_fds, write_fds, except_fds;
|
1999-05-16 18:57:49 +02:00
|
|
|
struct timeval tv = { 0, 0 };
|
|
|
|
|
|
|
|
FD_ZERO( &read_fds );
|
|
|
|
FD_ZERO( &write_fds );
|
1999-09-27 13:46:56 +02:00
|
|
|
FD_ZERO( &except_fds );
|
1999-05-16 18:57:49 +02:00
|
|
|
if (events & READ_EVENT) FD_SET( user->fd, &read_fds );
|
|
|
|
if (events & WRITE_EVENT) FD_SET( user->fd, &write_fds );
|
1999-09-27 13:46:56 +02:00
|
|
|
if (events & EXCEPT_EVENT) FD_SET( user->fd, &except_fds );
|
|
|
|
return select( user->fd + 1, &read_fds, &write_fds, &except_fds, &tv ) > 0;
|
1999-05-16 18:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a timeout user */
|
|
|
|
struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
|
|
|
|
{
|
|
|
|
struct timeout_user *user;
|
|
|
|
struct timeout_user *pos;
|
|
|
|
|
|
|
|
if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
|
|
|
|
user->when = *when;
|
|
|
|
user->callback = func;
|
|
|
|
user->private = private;
|
1998-12-27 09:35:39 +01:00
|
|
|
|
|
|
|
/* Now insert it in the linked list */
|
|
|
|
|
|
|
|
for (pos = timeout_head; pos; pos = pos->next)
|
|
|
|
{
|
|
|
|
if (pos->when.tv_sec > user->when.tv_sec) break;
|
|
|
|
if ((pos->when.tv_sec == user->when.tv_sec) &&
|
|
|
|
(pos->when.tv_usec > user->when.tv_usec)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos) /* insert it before 'pos' */
|
|
|
|
{
|
|
|
|
if ((user->prev = pos->prev)) user->prev->next = user;
|
|
|
|
else timeout_head = user;
|
|
|
|
user->next = pos;
|
|
|
|
pos->prev = user;
|
|
|
|
}
|
|
|
|
else /* insert it at the tail */
|
|
|
|
{
|
|
|
|
user->next = NULL;
|
|
|
|
if (timeout_tail) timeout_tail->next = user;
|
|
|
|
else timeout_head = user;
|
|
|
|
user->prev = timeout_tail;
|
|
|
|
timeout_tail = user;
|
|
|
|
}
|
1999-05-16 18:57:49 +02:00
|
|
|
return user;
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* remove a timeout user */
|
|
|
|
void remove_timeout_user( struct timeout_user *user )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
if (user->next) user->next->prev = user->prev;
|
|
|
|
else timeout_tail = user->prev;
|
|
|
|
if (user->prev) user->prev->next = user->next;
|
|
|
|
else timeout_head = user->next;
|
|
|
|
free( user );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* make an absolute timeout value from a relative timeout in milliseconds */
|
|
|
|
void make_timeout( struct timeval *when, int timeout )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
gettimeofday( when, 0 );
|
|
|
|
if (!timeout) return;
|
|
|
|
if ((when->tv_usec += (timeout % 1000) * 1000) >= 1000000)
|
|
|
|
{
|
|
|
|
when->tv_usec -= 1000000;
|
|
|
|
when->tv_sec++;
|
|
|
|
}
|
|
|
|
when->tv_sec += timeout / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle an expired timeout */
|
|
|
|
static void handle_timeout( struct timeout_user *user )
|
|
|
|
{
|
|
|
|
if (user->next) user->next->prev = user->prev;
|
|
|
|
else timeout_tail = user->prev;
|
|
|
|
if (user->prev) user->prev->next = user->next;
|
|
|
|
else timeout_head = user->next;
|
|
|
|
user->callback( user->private );
|
|
|
|
free( user );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
1999-06-22 19:27:58 +02:00
|
|
|
#ifdef DEBUG_OBJECTS
|
|
|
|
static int do_dump_objects;
|
|
|
|
|
|
|
|
/* SIGHUP handler */
|
|
|
|
static void sighup()
|
|
|
|
{
|
|
|
|
do_dump_objects = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-12-27 09:35:39 +01:00
|
|
|
/* server main loop */
|
|
|
|
void select_loop(void)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
1999-06-22 19:27:58 +02:00
|
|
|
#ifdef DEBUG_OBJECTS
|
|
|
|
signal( SIGHUP, sighup );
|
|
|
|
#endif
|
1998-12-27 09:35:39 +01:00
|
|
|
|
|
|
|
while (nb_users)
|
|
|
|
{
|
1999-09-27 13:46:56 +02:00
|
|
|
fd_set read = read_set, write = write_set, except = except_set;
|
1998-12-27 09:35:39 +01:00
|
|
|
if (timeout_head)
|
|
|
|
{
|
|
|
|
struct timeval tv, now;
|
|
|
|
gettimeofday( &now, NULL );
|
|
|
|
if ((timeout_head->when.tv_sec < now.tv_sec) ||
|
|
|
|
((timeout_head->when.tv_sec == now.tv_sec) &&
|
|
|
|
(timeout_head->when.tv_usec < now.tv_usec)))
|
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
handle_timeout( timeout_head );
|
1998-12-27 09:35:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tv.tv_sec = timeout_head->when.tv_sec - now.tv_sec;
|
|
|
|
if ((tv.tv_usec = timeout_head->when.tv_usec - now.tv_usec) < 0)
|
|
|
|
{
|
|
|
|
tv.tv_usec += 1000000;
|
|
|
|
tv.tv_sec--;
|
|
|
|
}
|
1999-05-16 18:57:49 +02:00
|
|
|
#if 0
|
|
|
|
printf( "select: " );
|
|
|
|
for (i = 0; i <= max_fd; i++) printf( "%c", FD_ISSET( i, &read_set ) ? 'r' :
|
|
|
|
(FD_ISSET( i, &write_set ) ? 'w' : '-') );
|
|
|
|
printf( " timeout %d.%06d\n", tv.tv_sec, tv.tv_usec );
|
|
|
|
#endif
|
1999-09-27 13:46:56 +02:00
|
|
|
ret = select( max_fd + 1, &read, &write, &except, &tv );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
else /* no timeout */
|
|
|
|
{
|
1999-05-16 18:57:49 +02:00
|
|
|
#if 0
|
|
|
|
printf( "select: " );
|
|
|
|
for (i = 0; i <= max_fd; i++) printf( "%c", FD_ISSET( i, &read_set ) ? 'r' :
|
|
|
|
(FD_ISSET( i, &write_set ) ? 'w' : '-') );
|
|
|
|
printf( " no timeout\n" );
|
|
|
|
#endif
|
1999-09-27 13:46:56 +02:00
|
|
|
ret = select( max_fd + 1, &read, &write, &except, NULL );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) continue;
|
1999-05-09 18:00:35 +02:00
|
|
|
if (ret == -1) {
|
1999-06-22 19:27:58 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_OBJECTS
|
|
|
|
if (do_dump_objects) dump_objects();
|
|
|
|
do_dump_objects = 0;
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
1999-05-09 18:00:35 +02:00
|
|
|
perror("select");
|
|
|
|
}
|
1998-12-27 09:35:39 +01:00
|
|
|
|
|
|
|
for (i = 0; i <= max_fd; i++)
|
|
|
|
{
|
|
|
|
int event = 0;
|
1999-09-27 13:46:56 +02:00
|
|
|
if (FD_ISSET( i, &except )) event |= EXCEPT_EVENT;
|
|
|
|
if (FD_ISSET( i, &write )) event |= WRITE_EVENT;
|
|
|
|
if (FD_ISSET( i, &read )) event |= READ_EVENT;
|
1999-05-04 18:51:30 +02:00
|
|
|
|
|
|
|
/* Note: users[i] might be NULL here, because an event routine
|
|
|
|
called in an earlier pass of this loop might have removed
|
|
|
|
the current user ... */
|
|
|
|
if (event && users[i])
|
1999-05-16 18:57:49 +02:00
|
|
|
users[i]->func( event, users[i]->private );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|