2 * Server main select() loop
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifdef HAVE_SYS_POLL_H
32 #include <sys/types.h>
41 struct timeout_user *next; /* next in sorted timeout list */
42 struct timeout_user *prev; /* prev in sorted timeout list */
43 struct timeval when; /* timeout expiry (absolute time) */
44 timeout_callback callback; /* callback function */
45 void *private; /* callback private data */
48 static struct object **poll_users; /* users array */
49 static struct pollfd *pollfd; /* poll fd array */
50 static int nb_users; /* count of array entries actually in use */
51 static int active_users; /* current number of active users */
52 static int allocated_users; /* count of allocated entries in the array */
53 static struct object **freelist; /* list of free entries in the array */
55 static struct timeout_user *timeout_head; /* sorted timeouts list head */
56 static struct timeout_user *timeout_tail; /* sorted timeouts list tail */
59 /* add a user and return an opaque handle to it, or -1 on failure */
60 int add_select_user( struct object *obj )
65 ret = freelist - poll_users;
66 freelist = (struct object **)poll_users[ret];
70 if (nb_users == allocated_users)
72 struct object **newusers;
73 struct pollfd *newpoll;
74 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
75 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
76 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
79 poll_users = newusers;
85 poll_users = newusers;
87 allocated_users = new_count;
91 pollfd[ret].fd = obj->fd;
92 pollfd[ret].events = 0;
93 pollfd[ret].revents = 0;
94 poll_users[ret] = obj;
100 /* remove an object from the select list and close its fd */
101 void remove_select_user( struct object *obj )
103 int user = obj->select;
105 assert( poll_users[user] == obj );
106 pollfd[user].fd = -1;
107 pollfd[user].events = 0;
108 pollfd[user].revents = 0;
109 poll_users[user] = (struct object *)freelist;
110 freelist = &poll_users[user];
117 /* change the fd and events of an object */
118 void change_select_fd( struct object *obj, int fd, int events )
120 int user = obj->select;
121 assert( poll_users[user] == obj );
122 pollfd[user].fd = fd;
123 pollfd[user].events = events;
127 /* set the events that select waits for on this fd */
128 void set_select_events( struct object *obj, int events )
130 int user = obj->select;
131 assert( poll_users[user] == obj );
132 if (events == -1) /* stop waiting on this fd completely */
134 pollfd[user].fd = -1;
135 pollfd[user].events = 0;
136 pollfd[user].revents = 0;
138 else if (pollfd[user].fd != -1) pollfd[user].events = events;
141 /* add a timeout user */
142 struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
144 struct timeout_user *user;
145 struct timeout_user *pos;
147 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
149 user->callback = func;
150 user->private = private;
152 /* Now insert it in the linked list */
154 for (pos = timeout_head; pos; pos = pos->next)
155 if (!time_before( &pos->when, when )) break;
157 if (pos) /* insert it before 'pos' */
159 if ((user->prev = pos->prev)) user->prev->next = user;
160 else timeout_head = user;
164 else /* insert it at the tail */
167 if (timeout_tail) timeout_tail->next = user;
168 else timeout_head = user;
169 user->prev = timeout_tail;
175 /* remove a timeout user */
176 void remove_timeout_user( struct timeout_user *user )
178 if (user->next) user->next->prev = user->prev;
179 else timeout_tail = user->prev;
180 if (user->prev) user->prev->next = user->next;
181 else timeout_head = user->next;
185 /* add a timeout in milliseconds to an absolute time */
186 void add_timeout( struct timeval *when, int timeout )
190 long sec = timeout / 1000;
191 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
193 when->tv_usec -= 1000000;
200 /* handle the next expired timeout */
201 static void handle_timeout(void)
203 struct timeout_user *user = timeout_head;
204 timeout_head = user->next;
205 if (user->next) user->next->prev = user->prev;
206 else timeout_tail = user->prev;
207 user->callback( user->private );
212 static void sighup_handler()
219 /* SIGTERM handler */
220 static void sigterm_handler()
227 static void sigint_handler()
229 kill_all_processes( NULL, 1 );
234 /* server main loop */
235 void select_loop(void)
239 struct sigaction action;
241 /* block the signals we use */
242 sigemptyset( &sigset );
243 sigaddset( &sigset, SIGCHLD );
244 sigaddset( &sigset, SIGHUP );
245 sigaddset( &sigset, SIGINT );
246 sigaddset( &sigset, SIGQUIT );
247 sigaddset( &sigset, SIGTERM );
248 sigprocmask( SIG_BLOCK, &sigset, NULL );
250 /* set the handlers */
251 action.sa_mask = sigset;
253 action.sa_handler = sigchld_handler;
254 sigaction( SIGCHLD, &action, NULL );
255 action.sa_handler = sighup_handler;
256 sigaction( SIGHUP, &action, NULL );
257 action.sa_handler = sigint_handler;
258 sigaction( SIGINT, &action, NULL );
259 action.sa_handler = sigterm_handler;
260 sigaction( SIGQUIT, &action, NULL );
261 sigaction( SIGTERM, &action, NULL );
269 gettimeofday( &now, NULL );
272 if (!time_before( &now, &timeout_head->when )) handle_timeout();
275 diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
276 + (timeout_head->when.tv_usec - now.tv_usec) / 1000;
280 if (!active_users) break; /* last user removed by a timeout */
283 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
285 /* Note: we assume that the signal handlers do not manipulate the pollfd array
286 * or the timeout list, otherwise there is a race here.
288 ret = poll( pollfd, nb_users, diff );
290 sigprocmask( SIG_BLOCK, &sigset, NULL );
295 for (i = 0; i < nb_users; i++)
297 if (pollfd[i].revents)
299 fd_poll_event( poll_users[i], pollfd[i].revents );