Moved all references to file descriptors out of the generic object
[wine] / server / fd.c
1 /*
2  * Server-side file descriptor management
3  *
4  * Copyright (C) 2003 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #endif
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "object.h"
37 #include "file.h"
38 #include "handle.h"
39 #include "process.h"
40 #include "request.h"
41 #include "console.h"
42
43 struct fd
44 {
45     struct object        obj;        /* object header */
46     const struct fd_ops *fd_ops;     /* file descriptor operations */
47     struct object       *user;       /* object using this file descriptor */
48     int                  unix_fd;    /* unix file descriptor */
49     int                  poll_index; /* index of fd in poll array */
50     int                  mode;       /* file protection mode */
51 };
52
53 static void fd_dump( struct object *obj, int verbose );
54 static void fd_destroy( struct object *obj );
55
56 static const struct object_ops fd_ops =
57 {
58     sizeof(struct fd),        /* size */
59     fd_dump,                  /* dump */
60     no_add_queue,             /* add_queue */
61     NULL,                     /* remove_queue */
62     NULL,                     /* signaled */
63     NULL,                     /* satisfied */
64     no_get_fd,                /* get_fd */
65     fd_destroy                /* destroy */
66 };
67
68
69 /****************************************************************/
70 /* timeouts support */
71
72 struct timeout_user
73 {
74     struct timeout_user  *next;       /* next in sorted timeout list */
75     struct timeout_user  *prev;       /* prev in sorted timeout list */
76     struct timeval        when;       /* timeout expiry (absolute time) */
77     timeout_callback      callback;   /* callback function */
78     void                 *private;    /* callback private data */
79 };
80
81 static struct timeout_user *timeout_head;   /* sorted timeouts list head */
82 static struct timeout_user *timeout_tail;   /* sorted timeouts list tail */
83
84 /* add a timeout user */
85 struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
86 {
87     struct timeout_user *user;
88     struct timeout_user *pos;
89
90     if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
91     user->when     = *when;
92     user->callback = func;
93     user->private  = private;
94
95     /* Now insert it in the linked list */
96
97     for (pos = timeout_head; pos; pos = pos->next)
98         if (!time_before( &pos->when, when )) break;
99
100     if (pos)  /* insert it before 'pos' */
101     {
102         if ((user->prev = pos->prev)) user->prev->next = user;
103         else timeout_head = user;
104         user->next = pos;
105         pos->prev = user;
106     }
107     else  /* insert it at the tail */
108     {
109         user->next = NULL;
110         if (timeout_tail) timeout_tail->next = user;
111         else timeout_head = user;
112         user->prev = timeout_tail;
113         timeout_tail = user;
114     }
115     return user;
116 }
117
118 /* remove a timeout user */
119 void remove_timeout_user( struct timeout_user *user )
120 {
121     if (user->next) user->next->prev = user->prev;
122     else timeout_tail = user->prev;
123     if (user->prev) user->prev->next = user->next;
124     else timeout_head = user->next;
125     free( user );
126 }
127
128 /* add a timeout in milliseconds to an absolute time */
129 void add_timeout( struct timeval *when, int timeout )
130 {
131     if (timeout)
132     {
133         long sec = timeout / 1000;
134         if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
135         {
136             when->tv_usec -= 1000000;
137             when->tv_sec++;
138         }
139         when->tv_sec += sec;
140     }
141 }
142
143 /* handle the next expired timeout */
144 inline static void handle_timeout(void)
145 {
146     struct timeout_user *user = timeout_head;
147     timeout_head = user->next;
148     if (user->next) user->next->prev = user->prev;
149     else timeout_tail = user->prev;
150     user->callback( user->private );
151     free( user );
152 }
153
154
155 /****************************************************************/
156 /* poll support */
157
158 static struct fd **poll_users;              /* users array */
159 static struct pollfd *pollfd;               /* poll fd array */
160 static int nb_users;                        /* count of array entries actually in use */
161 static int active_users;                    /* current number of active users */
162 static int allocated_users;                 /* count of allocated entries in the array */
163 static struct fd **freelist;                /* list of free entries in the array */
164
165 /* add a user in the poll array and return its index, or -1 on failure */
166 static int add_poll_user( struct fd *fd )
167 {
168     int ret;
169     if (freelist)
170     {
171         ret = freelist - poll_users;
172         freelist = (struct fd **)poll_users[ret];
173     }
174     else
175     {
176         if (nb_users == allocated_users)
177         {
178             struct fd **newusers;
179             struct pollfd *newpoll;
180             int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
181             if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
182             if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
183             {
184                 if (allocated_users)
185                     poll_users = newusers;
186                 else
187                     free( newusers );
188                 return -1;
189             }
190             poll_users = newusers;
191             pollfd = newpoll;
192             allocated_users = new_count;
193         }
194         ret = nb_users++;
195     }
196     pollfd[ret].fd = -1;
197     pollfd[ret].events = 0;
198     pollfd[ret].revents = 0;
199     poll_users[ret] = fd;
200     active_users++;
201     return ret;
202 }
203
204 /* remove a user from the poll list */
205 static void remove_poll_user( struct fd *fd, int user )
206 {
207     assert( user >= 0 );
208     assert( poll_users[user] == fd );
209     pollfd[user].fd = -1;
210     pollfd[user].events = 0;
211     pollfd[user].revents = 0;
212     poll_users[user] = (struct fd *)freelist;
213     freelist = &poll_users[user];
214     active_users--;
215 }
216
217
218 /* SIGHUP handler */
219 static void sighup_handler()
220 {
221 #ifdef DEBUG_OBJECTS
222     dump_objects();
223 #endif
224 }
225
226 /* SIGTERM handler */
227 static void sigterm_handler()
228 {
229     flush_registry();
230     exit(1);
231 }
232
233 /* SIGINT handler */
234 static void sigint_handler()
235 {
236     kill_all_processes( NULL, 1 );
237     flush_registry();
238     exit(1);
239 }
240
241 /* server main poll() loop */
242 void main_loop(void)
243 {
244     int ret;
245     sigset_t sigset;
246     struct sigaction action;
247
248     /* block the signals we use */
249     sigemptyset( &sigset );
250     sigaddset( &sigset, SIGCHLD );
251     sigaddset( &sigset, SIGHUP );
252     sigaddset( &sigset, SIGINT );
253     sigaddset( &sigset, SIGQUIT );
254     sigaddset( &sigset, SIGTERM );
255     sigprocmask( SIG_BLOCK, &sigset, NULL );
256
257     /* set the handlers */
258     action.sa_mask = sigset;
259     action.sa_flags = 0;
260     action.sa_handler = sigchld_handler;
261     sigaction( SIGCHLD, &action, NULL );
262     action.sa_handler = sighup_handler;
263     sigaction( SIGHUP, &action, NULL );
264     action.sa_handler = sigint_handler;
265     sigaction( SIGINT, &action, NULL );
266     action.sa_handler = sigterm_handler;
267     sigaction( SIGQUIT, &action, NULL );
268     sigaction( SIGTERM, &action, NULL );
269
270     while (active_users)
271     {
272         long diff = -1;
273         if (timeout_head)
274         {
275             struct timeval now;
276             gettimeofday( &now, NULL );
277             while (timeout_head)
278             {
279                 if (!time_before( &now, &timeout_head->when )) handle_timeout();
280                 else
281                 {
282                     diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
283                             + (timeout_head->when.tv_usec - now.tv_usec) / 1000;
284                     break;
285                 }
286             }
287             if (!active_users) break;  /* last user removed by a timeout */
288         }
289
290         sigprocmask( SIG_UNBLOCK, &sigset, NULL );
291
292         /* Note: we assume that the signal handlers do not manipulate the pollfd array
293          *       or the timeout list, otherwise there is a race here.
294          */
295         ret = poll( pollfd, nb_users, diff );
296
297         sigprocmask( SIG_BLOCK, &sigset, NULL );
298
299         if (ret > 0)
300         {
301             int i;
302             for (i = 0; i < nb_users; i++)
303             {
304                 if (pollfd[i].revents)
305                 {
306                     fd_poll_event( poll_users[i], pollfd[i].revents );
307                     if (!--ret) break;
308                 }
309             }
310         }
311     }
312 }
313
314 /****************************************************************/
315 /* file descriptor functions */
316
317 static void fd_dump( struct object *obj, int verbose )
318 {
319     struct fd *fd = (struct fd *)obj;
320     fprintf( stderr, "Fd unix_fd=%d mode=%06o user=%p\n", fd->unix_fd, fd->mode, fd->user );
321 }
322
323 static void fd_destroy( struct object *obj )
324 {
325     struct fd *fd = (struct fd *)obj;
326
327     if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
328     close( fd->unix_fd );
329 }
330
331 /* set the events that select waits for on this fd */
332 void set_fd_events( struct fd *fd, int events )
333 {
334     int user = fd->poll_index;
335     assert( poll_users[user] == fd );
336     if (events == -1)  /* stop waiting on this fd completely */
337     {
338         pollfd[user].fd = -1;
339         pollfd[user].events = POLLERR;
340         pollfd[user].revents = 0;
341     }
342     else if (pollfd[user].fd != -1 || !pollfd[user].events)
343     {
344         pollfd[user].fd = fd->unix_fd;
345         pollfd[user].events = events;
346     }
347 }
348
349 /* allocate an fd object */
350 /* if the function fails the unix fd is closed */
351 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
352 {
353     struct fd *fd = alloc_object( &fd_ops );
354
355     if (!fd)
356     {
357         close( unix_fd );
358         return NULL;
359     }
360     fd->fd_ops     = fd_user_ops;
361     fd->user       = user;
362     fd->unix_fd    = unix_fd;
363     fd->poll_index = -1;
364     fd->mode       = 0;
365
366     if ((unix_fd != -1) && ((fd->poll_index = add_poll_user( fd )) == -1))
367     {
368         release_object( fd );
369         return NULL;
370     }
371     return fd;
372 }
373
374 /* retrieve the object that is using an fd */
375 void *get_fd_user( struct fd *fd )
376 {
377     return fd->user;
378 }
379
380 /* retrieve the unix fd for an object */
381 int get_unix_fd( struct fd *fd )
382 {
383     return fd->unix_fd;
384 }
385
386 /* callback for event happening in the main poll() loop */
387 void fd_poll_event( struct fd *fd, int event )
388 {
389     return fd->fd_ops->poll_event( fd, event );
390 }
391
392 /* check if events are pending and if yes return which one(s) */
393 int check_fd_events( struct fd *fd, int events )
394 {
395     struct pollfd pfd;
396
397     pfd.fd     = fd->unix_fd;
398     pfd.events = events;
399     if (poll( &pfd, 1, 0 ) <= 0) return 0;
400     return pfd.revents;
401 }
402
403 /* default add_queue() routine for objects that poll() on an fd */
404 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
405 {
406     struct fd *fd = get_obj_fd( obj );
407
408     if (!fd) return 0;
409     if (!obj->head)  /* first on the queue */
410         set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
411     add_queue( obj, entry );
412     release_object( fd );
413     return 1;
414 }
415
416 /* default remove_queue() routine for objects that poll() on an fd */
417 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
418 {
419     struct fd *fd = get_obj_fd( obj );
420
421     grab_object( obj );
422     remove_queue( obj, entry );
423     if (!obj->head)  /* last on the queue is gone */
424         set_fd_events( fd, 0 );
425     release_object( obj );
426     release_object( fd );
427 }
428
429 /* default signaled() routine for objects that poll() on an fd */
430 int default_fd_signaled( struct object *obj, struct thread *thread )
431 {
432     struct fd *fd = get_obj_fd( obj );
433     int events = fd->fd_ops->get_poll_events( fd );
434     int ret = check_fd_events( fd, events ) != 0;
435
436     if (ret)
437         set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
438     else if (obj->head)
439         set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
440
441     release_object( fd );
442     return ret;
443 }
444
445 /* default handler for poll() events */
446 void default_poll_event( struct fd *fd, int event )
447 {
448     /* an error occurred, stop polling this fd to avoid busy-looping */
449     if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
450     wake_up( fd->user, 0 );
451 }
452
453 /* default flush() routine */
454 int no_flush( struct fd *fd )
455 {
456     set_error( STATUS_OBJECT_TYPE_MISMATCH );
457     return 0;
458 }
459
460 /* default get_file_info() routine */
461 int no_get_file_info( struct fd *fd, struct get_file_info_reply *info, int *flags )
462 {
463     set_error( STATUS_OBJECT_TYPE_MISMATCH );
464     *flags = 0;
465     return FD_TYPE_INVALID;
466 }
467
468 /* default queue_async() routine */
469 void no_queue_async( struct fd *fd, void* ptr, unsigned int status, int type, int count )
470 {
471     set_error( STATUS_OBJECT_TYPE_MISMATCH );
472 }
473
474 /* same as get_handle_obj but retrieve the struct fd associated to the object */
475 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
476                                      unsigned int access )
477 {
478     struct fd *fd = NULL;
479     struct object *obj;
480
481     if ((obj = get_handle_obj( process, handle, access, NULL )))
482     {
483         if (!(fd = get_obj_fd( obj ))) set_error( STATUS_OBJECT_TYPE_MISMATCH );
484         release_object( obj );
485     }
486     return fd;
487 }
488
489 /* flush a file buffers */
490 DECL_HANDLER(flush_file)
491 {
492     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
493
494     if (fd)
495     {
496         fd->fd_ops->flush( fd );
497         release_object( fd );
498     }
499 }
500
501 /* get a Unix fd to access a file */
502 DECL_HANDLER(get_handle_fd)
503 {
504     struct fd *fd;
505
506     reply->fd = -1;
507     reply->type = FD_TYPE_INVALID;
508
509     if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
510     {
511         int unix_fd = get_handle_unix_fd( current->process, req->handle, req->access );
512         if (unix_fd != -1) reply->fd = unix_fd;
513         else if (!get_error())
514         {
515             unix_fd = fd->unix_fd;
516             if (unix_fd != -1) send_client_fd( current->process, unix_fd, req->handle );
517         }
518         reply->type = fd->fd_ops->get_file_info( fd, NULL, &reply->flags );
519         release_object( fd );
520     }
521     else  /* check for console handle (FIXME: should be done in the client) */
522     {
523         struct object *obj;
524
525         if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
526         {
527             if (is_console_object( obj )) reply->type = FD_TYPE_CONSOLE;
528             release_object( obj );
529         }
530     }
531 }
532
533 /* get a file information */
534 DECL_HANDLER(get_file_info)
535 {
536     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
537
538     if (fd)
539     {
540         int flags;
541         fd->fd_ops->get_file_info( fd, reply, &flags );
542         release_object( fd );
543     }
544 }
545
546 /* create / reschedule an async I/O */
547 DECL_HANDLER(register_async)
548 {
549     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
550
551 /*
552  * The queue_async method must do the following:
553  *
554  * 1. Get the async_queue for the request of given type.
555  * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
556  * 3. If status is STATUS_PENDING:
557  *      a) If no async request found in step 2 (new request): call create_async() to initialize one.
558  *      b) Set request's status to STATUS_PENDING.
559  *      c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
560  *    Otherwise:
561  *      If the async request was found in step 2, destroy it by calling destroy_async().
562  * 4. Carry out any operations necessary to adjust the object's poll events
563  *    Usually: set_elect_events (obj, obj->ops->get_poll_events()).
564  *
565  * See also the implementations in file.c, serial.c, and sock.c.
566 */
567
568     if (fd)
569     {
570         fd->fd_ops->queue_async( fd, req->overlapped, req->status, req->type, req->count );
571         release_object( fd );
572     }
573 }