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>
|
1999-12-13 01:16:44 +01:00
|
|
|
#include <sys/poll.h>
|
1998-12-27 09:35:39 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
#include "object.h"
|
1999-11-08 06:31:47 +01:00
|
|
|
#include "thread.h"
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-12-13 01:16:44 +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
|
|
|
};
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
static struct object **poll_users; /* users array */
|
1999-12-13 01:16:44 +01:00
|
|
|
static struct pollfd *pollfd; /* poll fd array */
|
|
|
|
static int nb_users; /* count of array entries actually in use */
|
|
|
|
static int active_users; /* current number of active users */
|
|
|
|
static int allocated_users; /* count of allocated entries in the array */
|
2000-01-01 01:56:27 +01:00
|
|
|
static struct object **freelist; /* list of free entries in the array */
|
1999-12-13 01:16:44 +01:00
|
|
|
|
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-12-13 01:16:44 +01:00
|
|
|
/* add a user and return an opaque handle to it, or -1 on failure */
|
2000-01-01 01:56:27 +01:00
|
|
|
int add_select_user( struct object *obj )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
int ret;
|
|
|
|
if (freelist)
|
|
|
|
{
|
|
|
|
ret = freelist - poll_users;
|
2000-01-01 01:56:27 +01:00
|
|
|
freelist = (struct object **)poll_users[ret];
|
1999-12-13 01:16:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nb_users == allocated_users)
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
struct object **newusers;
|
1999-12-13 01:16:44 +01:00
|
|
|
struct pollfd *newpoll;
|
|
|
|
int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
|
|
|
|
if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
|
|
|
|
if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
|
|
|
|
{
|
|
|
|
free( newusers );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
poll_users = newusers;
|
|
|
|
pollfd = newpoll;
|
|
|
|
allocated_users = new_count;
|
|
|
|
}
|
|
|
|
ret = nb_users++;
|
|
|
|
}
|
2000-01-01 01:56:27 +01:00
|
|
|
pollfd[ret].fd = obj->fd;
|
1999-12-13 01:16:44 +01:00
|
|
|
pollfd[ret].events = 0;
|
|
|
|
pollfd[ret].revents = 0;
|
2000-01-01 01:56:27 +01:00
|
|
|
poll_users[ret] = obj;
|
|
|
|
obj->select = ret;
|
1999-12-13 01:16:44 +01:00
|
|
|
active_users++;
|
|
|
|
return ret;
|
|
|
|
}
|
1999-05-16 18:57:49 +02:00
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
/* remove an object from the select list and close its fd */
|
|
|
|
void remove_select_user( struct object *obj )
|
1999-12-13 01:16:44 +01:00
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
int user = obj->select;
|
|
|
|
assert( poll_users[user] == obj );
|
1999-12-13 01:16:44 +01:00
|
|
|
pollfd[user].fd = -1;
|
|
|
|
pollfd[user].events = 0;
|
|
|
|
pollfd[user].revents = 0;
|
2000-01-01 01:56:27 +01:00
|
|
|
poll_users[user] = (struct object *)freelist;
|
1999-12-13 01:16:44 +01:00
|
|
|
freelist = &poll_users[user];
|
2000-01-01 01:56:27 +01:00
|
|
|
close( obj->fd );
|
|
|
|
obj->fd = -1;
|
|
|
|
obj->select = -1;
|
1999-12-13 01:16:44 +01:00
|
|
|
active_users--;
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
/* change the fd of an object (the old fd is closed) */
|
|
|
|
void change_select_fd( struct object *obj, int fd )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
int user = obj->select;
|
|
|
|
assert( poll_users[user] == obj );
|
1999-12-13 01:16:44 +01:00
|
|
|
pollfd[user].fd = fd;
|
2000-01-01 01:56:27 +01:00
|
|
|
close( obj->fd );
|
|
|
|
obj->fd = fd;
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
1999-05-16 18:57:49 +02:00
|
|
|
/* set the events that select waits for on this fd */
|
2000-01-01 01:56:27 +01:00
|
|
|
void set_select_events( struct object *obj, int events )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
int user = obj->select;
|
|
|
|
assert( poll_users[user] == obj );
|
|
|
|
if (events == -1) /* stop waiting on this fd completely */
|
|
|
|
{
|
|
|
|
pollfd[user].fd = -1;
|
|
|
|
pollfd[user].events = 0;
|
|
|
|
pollfd[user].revents = 0;
|
|
|
|
}
|
|
|
|
else if (pollfd[user].fd != -1) pollfd[user].events = events;
|
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 */
|
1999-12-13 01:16:44 +01:00
|
|
|
int check_select_events( int fd, int events )
|
1999-05-16 18:57:49 +02:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = events;
|
|
|
|
return poll( &pfd, 1, 0 ) > 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)
|
1999-12-13 01:16:44 +01:00
|
|
|
if (!time_before( &pos->when, when )) break;
|
1998-12-27 09:35:39 +01:00
|
|
|
|
|
|
|
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-12-13 01:16:44 +01:00
|
|
|
/* add a timeout in milliseconds to an absolute time */
|
|
|
|
void add_timeout( struct timeval *when, int timeout )
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
if (timeout)
|
1999-05-16 18:57:49 +02:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
long sec = timeout / 1000;
|
|
|
|
if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
|
|
|
|
{
|
|
|
|
when->tv_usec -= 1000000;
|
|
|
|
when->tv_sec++;
|
|
|
|
}
|
|
|
|
when->tv_sec += sec;
|
1999-05-16 18:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-13 01:16:44 +01:00
|
|
|
/* handle the next expired timeout */
|
|
|
|
static void handle_timeout(void)
|
1999-05-16 18:57:49 +02:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
struct timeout_user *user = timeout_head;
|
|
|
|
timeout_head = user->next;
|
1999-05-16 18:57:49 +02:00
|
|
|
if (user->next) user->next->prev = user->prev;
|
|
|
|
else timeout_tail = user->prev;
|
|
|
|
user->callback( user->private );
|
|
|
|
free( user );
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
|
1999-06-22 19:27:58 +02:00
|
|
|
/* SIGHUP handler */
|
1999-12-13 01:16:44 +01:00
|
|
|
static void sighup_handler()
|
1999-06-22 19:27:58 +02:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
#ifdef DEBUG_OBJECTS
|
|
|
|
dump_objects();
|
1999-06-22 19:27:58 +02:00
|
|
|
#endif
|
1999-11-08 06:31:47 +01:00
|
|
|
}
|
|
|
|
|
1998-12-27 09:35:39 +01:00
|
|
|
/* server main loop */
|
|
|
|
void select_loop(void)
|
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
int ret;
|
|
|
|
sigset_t sigset;
|
|
|
|
struct sigaction action;
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-12-13 01:16:44 +01:00
|
|
|
/* block the signals we use */
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGCHLD );
|
|
|
|
sigaddset( &sigset, SIGHUP );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
|
|
|
|
|
|
|
/* set the handlers */
|
|
|
|
action.sa_mask = sigset;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = sigchld_handler;
|
|
|
|
sigaction( SIGCHLD, &action, NULL );
|
|
|
|
action.sa_handler = sighup_handler;
|
|
|
|
sigaction( SIGHUP, &action, NULL );
|
|
|
|
|
|
|
|
while (active_users)
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
long diff = -1;
|
1998-12-27 09:35:39 +01:00
|
|
|
if (timeout_head)
|
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
struct timeval now;
|
1998-12-27 09:35:39 +01:00
|
|
|
gettimeofday( &now, NULL );
|
1999-12-13 01:16:44 +01:00
|
|
|
while (timeout_head)
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
if (!time_before( &now, &timeout_head->when )) handle_timeout();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
|
|
|
|
+ (timeout_head->when.tv_usec - now.tv_usec) / 1000;
|
|
|
|
break;
|
|
|
|
}
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-13 01:16:44 +01:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
|
|
|
|
/* Note: we assume that the signal handlers do not manipulate the pollfd array
|
|
|
|
* or the timeout list, otherwise there is a race here.
|
|
|
|
*/
|
|
|
|
ret = poll( pollfd, nb_users, diff );
|
|
|
|
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
1998-12-27 09:35:39 +01:00
|
|
|
|
1999-12-13 01:16:44 +01:00
|
|
|
if (ret > 0)
|
1998-12-27 09:35:39 +01:00
|
|
|
{
|
1999-12-13 01:16:44 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_users; i++)
|
|
|
|
{
|
|
|
|
if (pollfd[i].revents)
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
poll_users[i]->ops->poll_event( poll_users[i], pollfd[i].revents );
|
1999-12-13 01:16:44 +01:00
|
|
|
if (!--ret) break;
|
|
|
|
}
|
|
|
|
}
|
1998-12-27 09:35:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|