blob: 90fb7af20d1f019c3ecb893eae1a527e948ae09d [file] [log] [blame]
Alexandre Julliard863637b2003-01-30 00:26:44 +00001/*
2 * Server-side file descriptor management
3 *
Alexandre Julliardce613492003-03-18 05:04:33 +00004 * Copyright (C) 2000, 2003 Alexandre Julliard
Alexandre Julliard863637b2003-01-30 00:26:44 +00005 *
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"
Alexandre Julliard49b7fdc2005-08-03 21:25:10 +000023#include "wine/port.h"
Alexandre Julliard863637b2003-01-30 00:26:44 +000024
25#include <assert.h>
Alexandre Julliardce613492003-03-18 05:04:33 +000026#include <errno.h>
Alexandre Julliard580da242003-03-12 22:38:14 +000027#include <fcntl.h>
Alexandre Julliardce613492003-03-18 05:04:33 +000028#include <limits.h>
Alexandre Julliarde66207e2003-02-19 00:33:32 +000029#include <signal.h>
Alexandre Julliard684b65c2004-04-16 04:31:35 +000030#include <stdarg.h>
Alexandre Julliard863637b2003-01-30 00:26:44 +000031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
Steven Edwards57279182005-03-04 12:38:36 +000034#ifdef HAVE_POLL_H
35#include <poll.h>
36#endif
Alexandre Julliarde66207e2003-02-19 00:33:32 +000037#ifdef HAVE_SYS_POLL_H
38#include <sys/poll.h>
39#endif
Alexandre Julliard969f57c2004-09-23 04:48:24 +000040#ifdef HAVE_STDINT_H
41#include <stdint.h>
42#endif
Alexandre Julliard580da242003-03-12 22:38:14 +000043#include <sys/stat.h>
Alexandre Julliarde66207e2003-02-19 00:33:32 +000044#include <sys/time.h>
45#include <sys/types.h>
Alexandre Julliard863637b2003-01-30 00:26:44 +000046#include <unistd.h>
47
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010048#include "ntstatus.h"
49#define WIN32_NO_STATUS
Alexandre Julliard863637b2003-01-30 00:26:44 +000050#include "object.h"
51#include "file.h"
52#include "handle.h"
53#include "process.h"
54#include "request.h"
55
Alexandre Julliard684b65c2004-04-16 04:31:35 +000056#include "winternl.h"
57
Alexandre Julliard969f57c2004-09-23 04:48:24 +000058#if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
Alexandre Julliard626aa332004-10-27 01:03:30 +000059# include <sys/epoll.h>
Alexandre Julliard969f57c2004-09-23 04:48:24 +000060# define USE_EPOLL
Alexandre Julliard626aa332004-10-27 01:03:30 +000061#elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
62# define USE_EPOLL
63# define EPOLLIN POLLIN
64# define EPOLLOUT POLLOUT
65# define EPOLLERR POLLERR
66# define EPOLLHUP POLLHUP
67# define EPOLL_CTL_ADD 1
68# define EPOLL_CTL_DEL 2
69# define EPOLL_CTL_MOD 3
70
71typedef union epoll_data
72{
73 void *ptr;
74 int fd;
75 uint32_t u32;
76 uint64_t u64;
77} epoll_data_t;
78
79struct epoll_event
80{
81 uint32_t events;
82 epoll_data_t data;
83};
84
85#define SYSCALL_RET(ret) do { \
86 if (ret < 0) { errno = -ret; ret = -1; } \
87 return ret; \
88 } while(0)
89
90static inline int epoll_create( int size )
91{
92 int ret;
93 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
Peter Chapman505dfde2004-12-02 18:19:25 +000094 : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
Alexandre Julliard626aa332004-10-27 01:03:30 +000095 SYSCALL_RET(ret);
96}
97
98static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
99{
100 int ret;
101 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
102 : "=a" (ret)
Peter Chapman505dfde2004-12-02 18:19:25 +0000103 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
Alexandre Julliard626aa332004-10-27 01:03:30 +0000104 SYSCALL_RET(ret);
105}
106
107static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
108{
109 int ret;
110 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
111 : "=a" (ret)
Peter Chapman505dfde2004-12-02 18:19:25 +0000112 : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
Alexandre Julliard626aa332004-10-27 01:03:30 +0000113 : "memory" );
114 SYSCALL_RET(ret);
115}
116#undef SYSCALL_RET
117
118#endif /* linux && __i386__ && HAVE_STDINT_H */
119
Alexandre Julliard969f57c2004-09-23 04:48:24 +0000120
Alexandre Julliardce613492003-03-18 05:04:33 +0000121/* Because of the stupid Posix locking semantics, we need to keep
122 * track of all file descriptors referencing a given file, and not
123 * close a single one until all the locks are gone (sigh).
124 */
125
Alexandre Julliard580da242003-03-12 22:38:14 +0000126/* file descriptor object */
127
128/* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
129struct closed_fd
130{
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000131 struct list entry; /* entry in inode closed list */
Alexandre Julliardc1325422005-07-28 10:48:57 +0000132 int unix_fd; /* the unix file descriptor */
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000133 char unlink[1]; /* name to unlink on close (if any) */
Alexandre Julliard580da242003-03-12 22:38:14 +0000134};
135
Alexandre Julliard863637b2003-01-30 00:26:44 +0000136struct fd
137{
Alexandre Julliard580da242003-03-12 22:38:14 +0000138 struct object obj; /* object header */
139 const struct fd_ops *fd_ops; /* file descriptor operations */
140 struct inode *inode; /* inode that this fd belongs to */
141 struct list inode_entry; /* entry in inode fd list */
142 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
143 struct object *user; /* object using this file descriptor */
Alexandre Julliardce613492003-03-18 05:04:33 +0000144 struct list locks; /* list of locks on this fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100145 unsigned int access; /* file access (FILE_READ_DATA etc.) */
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000146 unsigned int sharing; /* file sharing mode */
Alexandre Julliard580da242003-03-12 22:38:14 +0000147 int unix_fd; /* unix file descriptor */
Alexandre Julliard67505c02005-12-12 14:27:45 +0100148 int fs_locks :1; /* can we use filesystem locks for this fd? */
149 int unmounted :1;/* has the device been unmounted? */
Alexandre Julliard580da242003-03-12 22:38:14 +0000150 int poll_index; /* index of fd in poll array */
Robert Shearmand6a4e342005-06-07 20:09:01 +0000151 struct list read_q; /* async readers of this fd */
152 struct list write_q; /* async writers of this fd */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000153};
154
155static void fd_dump( struct object *obj, int verbose );
156static void fd_destroy( struct object *obj );
157
158static const struct object_ops fd_ops =
159{
160 sizeof(struct fd), /* size */
161 fd_dump, /* dump */
162 no_add_queue, /* add_queue */
163 NULL, /* remove_queue */
164 NULL, /* signaled */
165 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000166 no_signal, /* signal */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000167 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100168 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000169 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000170 no_close_handle, /* close_handle */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000171 fd_destroy /* destroy */
172};
173
Alexandre Julliardd4325152005-07-30 19:09:43 +0000174/* device object */
175
176#define DEVICE_HASH_SIZE 7
177#define INODE_HASH_SIZE 17
178
179struct device
180{
181 struct object obj; /* object header */
182 struct list entry; /* entry in device hash list */
183 dev_t dev; /* device number */
Alexandre Julliardf62f6e82005-08-24 18:33:50 +0000184 int removable; /* removable device? (or -1 if unknown) */
Alexandre Julliardd4325152005-07-30 19:09:43 +0000185 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
186};
187
188static void device_dump( struct object *obj, int verbose );
189static void device_destroy( struct object *obj );
190
191static const struct object_ops device_ops =
192{
193 sizeof(struct device), /* size */
194 device_dump, /* dump */
195 no_add_queue, /* add_queue */
196 NULL, /* remove_queue */
197 NULL, /* signaled */
198 NULL, /* satisfied */
199 no_signal, /* signal */
200 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100201 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000202 no_lookup_name, /* lookup_name */
Alexandre Julliardd4325152005-07-30 19:09:43 +0000203 no_close_handle, /* close_handle */
204 device_destroy /* destroy */
205};
206
Alexandre Julliard580da242003-03-12 22:38:14 +0000207/* inode object */
208
209struct inode
210{
211 struct object obj; /* object header */
212 struct list entry; /* inode hash list entry */
Alexandre Julliardd4325152005-07-30 19:09:43 +0000213 struct device *device; /* device containing this inode */
Alexandre Julliard580da242003-03-12 22:38:14 +0000214 ino_t ino; /* inode number */
215 struct list open; /* list of open file descriptors */
Alexandre Julliardce613492003-03-18 05:04:33 +0000216 struct list locks; /* list of file locks */
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000217 struct list closed; /* list of file descriptors to close at destroy time */
Alexandre Julliard580da242003-03-12 22:38:14 +0000218};
219
220static void inode_dump( struct object *obj, int verbose );
221static void inode_destroy( struct object *obj );
222
223static const struct object_ops inode_ops =
224{
225 sizeof(struct inode), /* size */
226 inode_dump, /* dump */
227 no_add_queue, /* add_queue */
228 NULL, /* remove_queue */
229 NULL, /* signaled */
230 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000231 no_signal, /* signal */
Alexandre Julliard580da242003-03-12 22:38:14 +0000232 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100233 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000234 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000235 no_close_handle, /* close_handle */
Alexandre Julliard580da242003-03-12 22:38:14 +0000236 inode_destroy /* destroy */
237};
238
Alexandre Julliardce613492003-03-18 05:04:33 +0000239/* file lock object */
240
241struct file_lock
242{
243 struct object obj; /* object header */
244 struct fd *fd; /* fd owning this lock */
245 struct list fd_entry; /* entry in list of locks on a given fd */
246 struct list inode_entry; /* entry in inode list of locks */
247 int shared; /* shared lock? */
248 file_pos_t start; /* locked region is interval [start;end) */
249 file_pos_t end;
250 struct process *process; /* process owning this lock */
251 struct list proc_entry; /* entry in list of locks owned by the process */
252};
253
254static void file_lock_dump( struct object *obj, int verbose );
255static int file_lock_signaled( struct object *obj, struct thread *thread );
256
257static const struct object_ops file_lock_ops =
258{
259 sizeof(struct file_lock), /* size */
260 file_lock_dump, /* dump */
261 add_queue, /* add_queue */
262 remove_queue, /* remove_queue */
263 file_lock_signaled, /* signaled */
264 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000265 no_signal, /* signal */
Alexandre Julliardce613492003-03-18 05:04:33 +0000266 no_get_fd, /* get_fd */
Alexandre Julliard28beba32005-12-12 14:57:40 +0100267 no_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000268 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000269 no_close_handle, /* close_handle */
Alexandre Julliardce613492003-03-18 05:04:33 +0000270 no_destroy /* destroy */
271};
272
273
274#define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
275#define FILE_POS_T_MAX (~(file_pos_t)0)
276
277static file_pos_t max_unix_offset = OFF_T_MAX;
278
Alexandre Julliard580da242003-03-12 22:38:14 +0000279#define DUMP_LONG_LONG(val) do { \
280 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
281 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
282 else \
283 fprintf( stderr, "%lx", (unsigned long)(val) ); \
284 } while (0)
285
286
Alexandre Julliard863637b2003-01-30 00:26:44 +0000287
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000288/****************************************************************/
289/* timeouts support */
290
291struct timeout_user
292{
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000293 struct list entry; /* entry in sorted timeout list */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000294 struct timeval when; /* timeout expiry (absolute time) */
295 timeout_callback callback; /* callback function */
296 void *private; /* callback private data */
297};
298
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000299static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000300
301/* add a timeout user */
Eric Pouech7001d6e2005-03-29 11:40:10 +0000302struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
303 void *private )
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000304{
305 struct timeout_user *user;
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000306 struct list *ptr;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000307
308 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
309 user->when = *when;
310 user->callback = func;
311 user->private = private;
312
313 /* Now insert it in the linked list */
314
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000315 LIST_FOR_EACH( ptr, &timeout_list )
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000316 {
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000317 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
318 if (!time_before( &timeout->when, when )) break;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000319 }
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000320 list_add_before( ptr, &user->entry );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000321 return user;
322}
323
324/* remove a timeout user */
325void remove_timeout_user( struct timeout_user *user )
326{
Alexandre Julliard15e12da2004-09-08 04:17:31 +0000327 list_remove( &user->entry );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000328 free( user );
329}
330
331/* add a timeout in milliseconds to an absolute time */
332void add_timeout( struct timeval *when, int timeout )
333{
334 if (timeout)
335 {
336 long sec = timeout / 1000;
337 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
338 {
339 when->tv_usec -= 1000000;
340 when->tv_sec++;
341 }
342 when->tv_sec += sec;
343 }
344}
345
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000346
347/****************************************************************/
348/* poll support */
349
350static struct fd **poll_users; /* users array */
351static struct pollfd *pollfd; /* poll fd array */
352static int nb_users; /* count of array entries actually in use */
353static int active_users; /* current number of active users */
354static int allocated_users; /* count of allocated entries in the array */
355static struct fd **freelist; /* list of free entries in the array */
356
Alexandre Julliard969f57c2004-09-23 04:48:24 +0000357#ifdef USE_EPOLL
358
359static int epoll_fd;
360static struct epoll_event *epoll_events;
361
362/* set the events that epoll waits for on this fd; helper for set_fd_events */
363static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
364{
365 struct epoll_event ev;
366 int ctl;
367
368 if (epoll_fd == -1) return;
369
370 if (events == -1) /* stop waiting on this fd completely */
371 {
372 if (pollfd[user].fd == -1) return; /* already removed */
373 ctl = EPOLL_CTL_DEL;
374 }
375 else if (pollfd[user].fd == -1)
376 {
377 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
378 ctl = EPOLL_CTL_ADD;
379 }
380 else
381 {
382 if (pollfd[user].events == events) return; /* nothing to do */
383 ctl = EPOLL_CTL_MOD;
384 }
385
386 ev.events = events;
387 ev.data.u32 = user;
388
389 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
390 {
391 if (errno == ENOMEM) /* not enough memory, give up on epoll */
392 {
393 close( epoll_fd );
394 epoll_fd = -1;
395 }
396 else perror( "epoll_ctl" ); /* should not happen */
397 }
398}
399
400#else /* USE_EPOLL */
401
402static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
403{
404}
405
406#endif /* USE_EPOLL */
407
408
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000409/* add a user in the poll array and return its index, or -1 on failure */
410static int add_poll_user( struct fd *fd )
411{
412 int ret;
413 if (freelist)
414 {
415 ret = freelist - poll_users;
416 freelist = (struct fd **)poll_users[ret];
417 }
418 else
419 {
420 if (nb_users == allocated_users)
421 {
422 struct fd **newusers;
423 struct pollfd *newpoll;
424 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
425 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
426 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
427 {
428 if (allocated_users)
429 poll_users = newusers;
430 else
431 free( newusers );
432 return -1;
433 }
434 poll_users = newusers;
435 pollfd = newpoll;
Alexandre Julliard969f57c2004-09-23 04:48:24 +0000436#ifdef USE_EPOLL
437 if (!allocated_users) epoll_fd = epoll_create( new_count );
438 if (epoll_fd != -1)
439 {
440 struct epoll_event *new_events;
441 if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) )))
442 return -1;
443 epoll_events = new_events;
444 }
445#endif
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000446 allocated_users = new_count;
447 }
448 ret = nb_users++;
449 }
450 pollfd[ret].fd = -1;
451 pollfd[ret].events = 0;
452 pollfd[ret].revents = 0;
453 poll_users[ret] = fd;
454 active_users++;
455 return ret;
456}
457
458/* remove a user from the poll list */
459static void remove_poll_user( struct fd *fd, int user )
460{
461 assert( user >= 0 );
462 assert( poll_users[user] == fd );
Alexandre Julliard969f57c2004-09-23 04:48:24 +0000463
464#ifdef USE_EPOLL
465 if (epoll_fd != -1 && pollfd[user].fd != -1)
466 {
467 struct epoll_event dummy;
468 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
469 }
470#endif
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000471 pollfd[user].fd = -1;
472 pollfd[user].events = 0;
473 pollfd[user].revents = 0;
474 poll_users[user] = (struct fd *)freelist;
475 freelist = &poll_users[user];
476 active_users--;
477}
478
Alexandre Julliard4949a712004-09-20 19:14:35 +0000479/* process pending timeouts and return the time until the next timeout, in milliseconds */
480static int get_next_timeout(void)
481{
482 if (!list_empty( &timeout_list ))
483 {
484 struct list expired_list, *ptr;
485 struct timeval now;
486
487 gettimeofday( &now, NULL );
488
489 /* first remove all expired timers from the list */
490
491 list_init( &expired_list );
492 while ((ptr = list_head( &timeout_list )) != NULL)
493 {
494 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
495
496 if (!time_before( &now, &timeout->when ))
497 {
498 list_remove( &timeout->entry );
499 list_add_tail( &expired_list, &timeout->entry );
500 }
501 else break;
502 }
503
504 /* now call the callback for all the removed timers */
505
506 while ((ptr = list_head( &expired_list )) != NULL)
507 {
508 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
509 list_remove( &timeout->entry );
510 timeout->callback( timeout->private );
511 free( timeout );
512 }
513
514 if ((ptr = list_head( &timeout_list )) != NULL)
515 {
516 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
517 int diff = (timeout->when.tv_sec - now.tv_sec) * 1000
518 + (timeout->when.tv_usec - now.tv_usec) / 1000;
519 if (diff < 0) diff = 0;
520 return diff;
521 }
522 }
523 return -1; /* no pending timeouts */
524}
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000525
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000526/* server main poll() loop */
527void main_loop(void)
528{
Alexandre Julliard4949a712004-09-20 19:14:35 +0000529 int i, ret, timeout;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000530
Alexandre Julliard969f57c2004-09-23 04:48:24 +0000531#ifdef USE_EPOLL
532 assert( POLLIN == EPOLLIN );
533 assert( POLLOUT == EPOLLOUT );
534 assert( POLLERR == EPOLLERR );
535 assert( POLLHUP == EPOLLHUP );
536
537 if (epoll_fd != -1)
538 {
539 while (active_users)
540 {
541 timeout = get_next_timeout();
542
543 if (!active_users) break; /* last user removed by a timeout */
544 if (epoll_fd == -1) break; /* an error occurred with epoll */
545
546 ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout );
547
548 /* put the events into the pollfd array first, like poll does */
549 for (i = 0; i < ret; i++)
550 {
551 int user = epoll_events[i].data.u32;
552 pollfd[user].revents = epoll_events[i].events;
553 }
554
555 /* read events from the pollfd array, as set_fd_events may modify them */
556 for (i = 0; i < ret; i++)
557 {
558 int user = epoll_events[i].data.u32;
559 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
560 }
561 }
562 }
563 /* fall through to normal poll loop */
564#endif /* USE_EPOLL */
565
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000566 while (active_users)
567 {
Alexandre Julliard4949a712004-09-20 19:14:35 +0000568 timeout = get_next_timeout();
Alexandre Julliard0a6af132004-09-07 23:28:14 +0000569
Alexandre Julliard4949a712004-09-20 19:14:35 +0000570 if (!active_users) break; /* last user removed by a timeout */
Alexandre Julliard0a6af132004-09-07 23:28:14 +0000571
Alexandre Julliard4949a712004-09-20 19:14:35 +0000572 ret = poll( pollfd, nb_users, timeout );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000573 if (ret > 0)
574 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000575 for (i = 0; i < nb_users; i++)
576 {
577 if (pollfd[i].revents)
578 {
579 fd_poll_event( poll_users[i], pollfd[i].revents );
580 if (!--ret) break;
581 }
582 }
583 }
584 }
585}
586
Alexandre Julliard580da242003-03-12 22:38:14 +0000587
588/****************************************************************/
Alexandre Julliardd4325152005-07-30 19:09:43 +0000589/* device functions */
590
591static struct list device_hash[DEVICE_HASH_SIZE];
592
593/* retrieve the device object for a given fd, creating it if needed */
Alexandre Julliardf8037bd2005-10-27 11:20:50 +0000594static struct device *get_device( dev_t dev, int create )
Alexandre Julliardd4325152005-07-30 19:09:43 +0000595{
596 struct device *device;
597 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
598
599 if (device_hash[hash].next)
600 {
601 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
602 if (device->dev == dev) return (struct device *)grab_object( device );
603 }
604 else list_init( &device_hash[hash] );
605
606 /* not found, create it */
Alexandre Julliardf8037bd2005-10-27 11:20:50 +0000607
608 if (!create) return NULL;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000609 if ((device = alloc_object( &device_ops )))
610 {
611 device->dev = dev;
Alexandre Julliardf62f6e82005-08-24 18:33:50 +0000612 device->removable = -1;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000613 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
614 list_add_head( &device_hash[hash], &device->entry );
615 }
616 return device;
617}
618
619static void device_dump( struct object *obj, int verbose )
620{
621 struct device *device = (struct device *)obj;
622 fprintf( stderr, "Device dev=" );
623 DUMP_LONG_LONG( device->dev );
624 fprintf( stderr, "\n" );
625}
626
627static void device_destroy( struct object *obj )
628{
629 struct device *device = (struct device *)obj;
630 unsigned int i;
631
632 for (i = 0; i < INODE_HASH_SIZE; i++)
633 assert( list_empty(&device->inode_hash[i]) );
634
635 list_remove( &device->entry ); /* remove it from the hash table */
636}
637
638
639/****************************************************************/
Alexandre Julliard580da242003-03-12 22:38:14 +0000640/* inode functions */
641
Alexandre Julliardce613492003-03-18 05:04:33 +0000642/* close all pending file descriptors in the closed list */
Alexandre Julliard964815b2005-08-08 15:11:03 +0000643static void inode_close_pending( struct inode *inode, int keep_unlinks )
Alexandre Julliardce613492003-03-18 05:04:33 +0000644{
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000645 struct list *ptr = list_head( &inode->closed );
646
647 while (ptr)
Alexandre Julliardce613492003-03-18 05:04:33 +0000648 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000649 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
650 struct list *next = list_next( &inode->closed, ptr );
651
Alexandre Julliardc1325422005-07-28 10:48:57 +0000652 if (fd->unix_fd != -1)
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000653 {
Alexandre Julliardc1325422005-07-28 10:48:57 +0000654 close( fd->unix_fd );
655 fd->unix_fd = -1;
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000656 }
Alexandre Julliard964815b2005-08-08 15:11:03 +0000657 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000658 {
659 list_remove( ptr );
660 free( fd );
661 }
662 ptr = next;
Alexandre Julliardce613492003-03-18 05:04:33 +0000663 }
664}
665
Alexandre Julliard580da242003-03-12 22:38:14 +0000666static void inode_dump( struct object *obj, int verbose )
667{
668 struct inode *inode = (struct inode *)obj;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000669 fprintf( stderr, "Inode device=%p ino=", inode->device );
Alexandre Julliard580da242003-03-12 22:38:14 +0000670 DUMP_LONG_LONG( inode->ino );
671 fprintf( stderr, "\n" );
672}
673
674static void inode_destroy( struct object *obj )
675{
676 struct inode *inode = (struct inode *)obj;
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000677 struct list *ptr;
Alexandre Julliard580da242003-03-12 22:38:14 +0000678
Alexandre Julliardce613492003-03-18 05:04:33 +0000679 assert( list_empty(&inode->open) );
680 assert( list_empty(&inode->locks) );
Alexandre Julliard580da242003-03-12 22:38:14 +0000681
682 list_remove( &inode->entry );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000683
684 while ((ptr = list_head( &inode->closed )))
685 {
686 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
687 list_remove( ptr );
Alexandre Julliardc1325422005-07-28 10:48:57 +0000688 if (fd->unix_fd != -1) close( fd->unix_fd );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000689 if (fd->unlink[0])
690 {
691 /* make sure it is still the same file */
692 struct stat st;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000693 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
Alexandre Julliard684b65c2004-04-16 04:31:35 +0000694 {
695 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
696 else unlink( fd->unlink );
697 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000698 }
699 free( fd );
700 }
Alexandre Julliardd4325152005-07-30 19:09:43 +0000701 release_object( inode->device );
Alexandre Julliard580da242003-03-12 22:38:14 +0000702}
703
704/* retrieve the inode object for a given fd, creating it if needed */
705static struct inode *get_inode( dev_t dev, ino_t ino )
706{
Alexandre Julliardd4325152005-07-30 19:09:43 +0000707 struct device *device;
Alexandre Julliard580da242003-03-12 22:38:14 +0000708 struct inode *inode;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000709 unsigned int hash = ino % INODE_HASH_SIZE;
Alexandre Julliard580da242003-03-12 22:38:14 +0000710
Alexandre Julliardf8037bd2005-10-27 11:20:50 +0000711 if (!(device = get_device( dev, 1 ))) return NULL;
Alexandre Julliardd4325152005-07-30 19:09:43 +0000712
713 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
Alexandre Julliard580da242003-03-12 22:38:14 +0000714 {
Alexandre Julliardd4325152005-07-30 19:09:43 +0000715 if (inode->ino == ino)
Alexandre Julliard580da242003-03-12 22:38:14 +0000716 {
Alexandre Julliardd4325152005-07-30 19:09:43 +0000717 release_object( device );
718 return (struct inode *)grab_object( inode );
Alexandre Julliard580da242003-03-12 22:38:14 +0000719 }
720 }
Alexandre Julliard580da242003-03-12 22:38:14 +0000721
722 /* not found, create it */
723 if ((inode = alloc_object( &inode_ops )))
724 {
Alexandre Julliardd4325152005-07-30 19:09:43 +0000725 inode->device = device;
Alexandre Julliard580da242003-03-12 22:38:14 +0000726 inode->ino = ino;
Alexandre Julliard580da242003-03-12 22:38:14 +0000727 list_init( &inode->open );
Alexandre Julliardce613492003-03-18 05:04:33 +0000728 list_init( &inode->locks );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000729 list_init( &inode->closed );
Alexandre Julliardd4325152005-07-30 19:09:43 +0000730 list_add_head( &device->inode_hash[hash], &inode->entry );
Alexandre Julliard580da242003-03-12 22:38:14 +0000731 }
Alexandre Julliardd4325152005-07-30 19:09:43 +0000732 else release_object( device );
733
Alexandre Julliard580da242003-03-12 22:38:14 +0000734 return inode;
735}
736
Alexandre Julliardc1325422005-07-28 10:48:57 +0000737/* add fd to the inode list of file descriptors to close */
Alexandre Julliard580da242003-03-12 22:38:14 +0000738static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
739{
Alexandre Julliardce613492003-03-18 05:04:33 +0000740 if (!list_empty( &inode->locks ))
741 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000742 list_add_head( &inode->closed, &fd->entry );
Alexandre Julliardce613492003-03-18 05:04:33 +0000743 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000744 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
745 {
Alexandre Julliardc1325422005-07-28 10:48:57 +0000746 if (fd->unix_fd != -1) close( fd->unix_fd );
747 fd->unix_fd = -1;
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000748 list_add_head( &inode->closed, &fd->entry );
749 }
750 else /* no locks on this inode and no unlink, get rid of the fd */
Alexandre Julliardce613492003-03-18 05:04:33 +0000751 {
Alexandre Julliardc1325422005-07-28 10:48:57 +0000752 if (fd->unix_fd != -1) close( fd->unix_fd );
Alexandre Julliardce613492003-03-18 05:04:33 +0000753 free( fd );
754 }
755}
756
757
758/****************************************************************/
759/* file lock functions */
760
761static void file_lock_dump( struct object *obj, int verbose )
762{
763 struct file_lock *lock = (struct file_lock *)obj;
764 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
765 lock->shared ? "shared" : "excl", lock->fd, lock->process );
766 DUMP_LONG_LONG( lock->start );
767 fprintf( stderr, " end=" );
768 DUMP_LONG_LONG( lock->end );
769 fprintf( stderr, "\n" );
770}
771
772static int file_lock_signaled( struct object *obj, struct thread *thread )
773{
774 struct file_lock *lock = (struct file_lock *)obj;
775 /* lock is signaled if it has lost its owner */
776 return !lock->process;
777}
778
779/* set (or remove) a Unix lock if possible for the given range */
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000780static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
Alexandre Julliardce613492003-03-18 05:04:33 +0000781{
782 struct flock fl;
783
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000784 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
Alexandre Julliardce613492003-03-18 05:04:33 +0000785 for (;;)
786 {
787 if (start == end) return 1; /* can't set zero-byte lock */
788 if (start > max_unix_offset) return 1; /* ignore it */
789 fl.l_type = type;
790 fl.l_whence = SEEK_SET;
791 fl.l_start = start;
792 if (!end || end > max_unix_offset) fl.l_len = 0;
793 else fl.l_len = end - start;
794 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
795
796 switch(errno)
797 {
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000798 case EACCES:
799 /* check whether locks work at all on this file system */
800 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
801 {
802 set_error( STATUS_FILE_LOCK_CONFLICT );
803 return 0;
804 }
805 /* fall through */
Alexandre Julliardb9327232003-05-11 02:45:33 +0000806 case EIO:
807 case ENOLCK:
808 /* no locking on this fs, just ignore it */
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000809 fd->fs_locks = 0;
Alexandre Julliardb9327232003-05-11 02:45:33 +0000810 return 1;
Alexandre Julliardce613492003-03-18 05:04:33 +0000811 case EAGAIN:
812 set_error( STATUS_FILE_LOCK_CONFLICT );
813 return 0;
Alexandre Julliardd71ba932003-03-21 21:31:35 +0000814 case EBADF:
815 /* this can happen if we try to set a write lock on a read-only file */
816 /* we just ignore that error */
817 if (fl.l_type == F_WRLCK) return 1;
818 set_error( STATUS_ACCESS_DENIED );
819 return 0;
Wim Lewis99a01c02004-01-02 20:11:35 +0000820#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000821 case EOVERFLOW:
Wim Lewis99a01c02004-01-02 20:11:35 +0000822#endif
Alexandre Julliardb9327232003-05-11 02:45:33 +0000823 case EINVAL:
Alexandre Julliardce613492003-03-18 05:04:33 +0000824 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
825 /* in that case we shrink the limit and retry */
826 if (max_unix_offset > INT_MAX)
827 {
828 max_unix_offset = INT_MAX;
829 break; /* retry */
830 }
831 /* fall through */
832 default:
833 file_set_error();
834 return 0;
835 }
836 }
837}
838
839/* check if interval [start;end) overlaps the lock */
840inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
841{
842 if (lock->end && start >= lock->end) return 0;
843 if (end && lock->start >= end) return 0;
844 return 1;
845}
846
847/* remove Unix locks for all bytes in the specified area that are no longer locked */
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000848static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
Alexandre Julliardce613492003-03-18 05:04:33 +0000849{
850 struct hole
851 {
852 struct hole *next;
853 struct hole *prev;
854 file_pos_t start;
855 file_pos_t end;
856 } *first, *cur, *next, *buffer;
857
858 struct list *ptr;
859 int count = 0;
860
861 if (!fd->inode) return;
Alexandre Julliardb23aa942003-07-03 18:12:02 +0000862 if (!fd->fs_locks) return;
Alexandre Julliardce613492003-03-18 05:04:33 +0000863 if (start == end || start > max_unix_offset) return;
864 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
865
866 /* count the number of locks overlapping the specified area */
867
868 LIST_FOR_EACH( ptr, &fd->inode->locks )
869 {
870 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
871 if (lock->start == lock->end) continue;
872 if (lock_overlaps( lock, start, end )) count++;
873 }
874
875 if (!count) /* no locks at all, we can unlock everything */
876 {
877 set_unix_lock( fd, start, end, F_UNLCK );
878 return;
879 }
880
881 /* allocate space for the list of holes */
882 /* max. number of holes is number of locks + 1 */
883
884 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
885 first = buffer;
886 first->next = NULL;
887 first->prev = NULL;
888 first->start = start;
889 first->end = end;
890 next = first + 1;
891
892 /* build a sorted list of unlocked holes in the specified area */
893
894 LIST_FOR_EACH( ptr, &fd->inode->locks )
895 {
896 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
897 if (lock->start == lock->end) continue;
898 if (!lock_overlaps( lock, start, end )) continue;
899
900 /* go through all the holes touched by this lock */
901 for (cur = first; cur; cur = cur->next)
902 {
903 if (cur->end <= lock->start) continue; /* hole is before start of lock */
904 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
905
906 /* now we know that lock is overlapping hole */
907
908 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
909 {
910 cur->start = lock->end;
911 if (cur->start && cur->start < cur->end) break; /* done with this lock */
912 /* now hole is empty, remove it */
913 if (cur->next) cur->next->prev = cur->prev;
914 if (cur->prev) cur->prev->next = cur->next;
915 else if (!(first = cur->next)) goto done; /* no more holes at all */
916 }
917 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
918 {
919 cur->end = lock->start;
920 assert( cur->start < cur->end );
921 }
922 else /* lock is in the middle of hole, split hole in two */
923 {
924 next->prev = cur;
925 next->next = cur->next;
926 cur->next = next;
927 next->start = lock->end;
928 next->end = cur->end;
929 cur->end = lock->start;
930 assert( next->start < next->end );
931 assert( cur->end < next->start );
932 next++;
933 break; /* done with this lock */
934 }
935 }
936 }
937
938 /* clear Unix locks for all the holes */
939
940 for (cur = first; cur; cur = cur->next)
941 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
942
943 done:
944 free( buffer );
945}
946
947/* create a new lock on a fd */
948static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
949{
950 struct file_lock *lock;
951
952 if (!fd->inode) /* not a regular file */
953 {
954 set_error( STATUS_INVALID_HANDLE );
955 return NULL;
956 }
957
958 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
959 lock->shared = shared;
960 lock->start = start;
961 lock->end = end;
962 lock->fd = fd;
963 lock->process = current->process;
964
965 /* now try to set a Unix lock */
966 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
967 {
968 release_object( lock );
969 return NULL;
970 }
971 list_add_head( &fd->locks, &lock->fd_entry );
972 list_add_head( &fd->inode->locks, &lock->inode_entry );
973 list_add_head( &lock->process->locks, &lock->proc_entry );
974 return lock;
975}
976
977/* remove an existing lock */
978static void remove_lock( struct file_lock *lock, int remove_unix )
979{
980 struct inode *inode = lock->fd->inode;
981
982 list_remove( &lock->fd_entry );
983 list_remove( &lock->inode_entry );
984 list_remove( &lock->proc_entry );
985 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
Alexandre Julliard964815b2005-08-08 15:11:03 +0000986 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
Alexandre Julliardce613492003-03-18 05:04:33 +0000987 lock->process = NULL;
988 wake_up( &lock->obj, 0 );
989 release_object( lock );
990}
991
992/* remove all locks owned by a given process */
993void remove_process_locks( struct process *process )
994{
995 struct list *ptr;
996
997 while ((ptr = list_head( &process->locks )))
998 {
999 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1000 remove_lock( lock, 1 ); /* this removes it from the list */
1001 }
1002}
1003
1004/* remove all locks on a given fd */
1005static void remove_fd_locks( struct fd *fd )
1006{
1007 file_pos_t start = FILE_POS_T_MAX, end = 0;
1008 struct list *ptr;
1009
1010 while ((ptr = list_head( &fd->locks )))
1011 {
1012 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1013 if (lock->start < start) start = lock->start;
1014 if (!lock->end || lock->end > end) end = lock->end - 1;
1015 remove_lock( lock, 0 );
1016 }
1017 if (start < end) remove_unix_locks( fd, start, end + 1 );
1018}
1019
1020/* add a lock on an fd */
1021/* returns handle to wait on */
1022obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1023{
1024 struct list *ptr;
1025 file_pos_t end = start + count;
1026
1027 /* don't allow wrapping locks */
1028 if (end && end < start)
1029 {
1030 set_error( STATUS_INVALID_PARAMETER );
1031 return 0;
1032 }
1033
1034 /* check if another lock on that file overlaps the area */
1035 LIST_FOR_EACH( ptr, &fd->inode->locks )
1036 {
1037 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1038 if (!lock_overlaps( lock, start, end )) continue;
1039 if (lock->shared && shared) continue;
1040 /* found one */
1041 if (!wait)
1042 {
1043 set_error( STATUS_FILE_LOCK_CONFLICT );
1044 return 0;
1045 }
1046 set_error( STATUS_PENDING );
1047 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1048 }
1049
1050 /* not found, add it */
1051 if (add_lock( fd, shared, start, end )) return 0;
1052 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1053 {
1054 /* Unix lock conflict -> tell client to wait and retry */
1055 if (wait) set_error( STATUS_PENDING );
1056 }
1057 return 0;
1058}
1059
1060/* remove a lock on an fd */
1061void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1062{
1063 struct list *ptr;
1064 file_pos_t end = start + count;
1065
1066 /* find an existing lock with the exact same parameters */
1067 LIST_FOR_EACH( ptr, &fd->locks )
1068 {
1069 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1070 if ((lock->start == start) && (lock->end == end))
1071 {
1072 remove_lock( lock, 1 );
1073 return;
1074 }
1075 }
1076 set_error( STATUS_FILE_LOCK_CONFLICT );
Alexandre Julliard580da242003-03-12 22:38:14 +00001077}
1078
1079
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001080/****************************************************************/
Eric Pouech46344472005-01-14 19:54:38 +00001081/* asynchronous operations support */
1082
1083struct async
1084{
Eric Pouech46344472005-01-14 19:54:38 +00001085 struct thread *thread;
1086 void *apc;
1087 void *user;
1088 void *sb;
Eric Pouech46344472005-01-14 19:54:38 +00001089 struct timeout_user *timeout;
Alexandre Julliarddd81ac52005-02-24 17:06:31 +00001090 struct list entry;
Eric Pouech46344472005-01-14 19:54:38 +00001091};
1092
Alexandre Julliarddd81ac52005-02-24 17:06:31 +00001093/* notifies client thread of new status of its async request */
1094/* destroys the server side of it */
1095static void async_terminate( struct async *async, int status )
1096{
1097 thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
1098 1, async->user, async->sb, (void *)status );
1099
1100 if (async->timeout) remove_timeout_user( async->timeout );
1101 async->timeout = NULL;
1102 list_remove( &async->entry );
1103 release_object( async->thread );
1104 free( async );
1105}
1106
Eric Pouech46344472005-01-14 19:54:38 +00001107/* cb for timeout on an async request */
1108static void async_callback(void *private)
1109{
1110 struct async *async = (struct async *)private;
1111
1112 /* fprintf(stderr, "async timeout out %p\n", async); */
1113 async->timeout = NULL;
1114 async_terminate( async, STATUS_TIMEOUT );
1115}
1116
1117/* create an async on a given queue of a fd */
Eric Pouech7001d6e2005-03-29 11:40:10 +00001118struct async *create_async(struct thread *thread, int* timeout, struct list *queue,
Eric Pouech46344472005-01-14 19:54:38 +00001119 void *io_apc, void *io_user, void* io_sb)
1120{
1121 struct async *async = mem_alloc( sizeof(struct async) );
Eric Pouech46344472005-01-14 19:54:38 +00001122
1123 if (!async) return NULL;
1124
Eric Pouech46344472005-01-14 19:54:38 +00001125 async->thread = (struct thread *)grab_object(thread);
1126 async->apc = io_apc;
1127 async->user = io_user;
1128 async->sb = io_sb;
Eric Pouech46344472005-01-14 19:54:38 +00001129
Alexandre Julliarddd81ac52005-02-24 17:06:31 +00001130 list_add_tail( queue, &async->entry );
Eric Pouech46344472005-01-14 19:54:38 +00001131
1132 if (timeout)
1133 {
Eric Pouech7001d6e2005-03-29 11:40:10 +00001134 struct timeval when;
1135
Robert Shearmanc5165712005-05-25 18:41:09 +00001136 gettimeofday( &when, NULL );
Eric Pouech7001d6e2005-03-29 11:40:10 +00001137 add_timeout( &when, *timeout );
1138 async->timeout = add_timeout_user( &when, async_callback, async );
Eric Pouech46344472005-01-14 19:54:38 +00001139 }
1140 else async->timeout = NULL;
1141
1142 return async;
1143}
1144
Alexandre Julliarddd81ac52005-02-24 17:06:31 +00001145/* terminate the async operation at the head of the queue */
1146void async_terminate_head( struct list *queue, int status )
Eric Pouech46344472005-01-14 19:54:38 +00001147{
Alexandre Julliarddd81ac52005-02-24 17:06:31 +00001148 struct list *ptr = list_head( queue );
1149 if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
Eric Pouech46344472005-01-14 19:54:38 +00001150}
1151
1152/****************************************************************/
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001153/* file descriptor functions */
1154
Alexandre Julliard863637b2003-01-30 00:26:44 +00001155static void fd_dump( struct object *obj, int verbose )
1156{
1157 struct fd *fd = (struct fd *)obj;
Alexandre Julliarde432d542005-02-03 10:45:18 +00001158 fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
1159 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1160 fprintf( stderr, "\n" );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001161}
1162
1163static void fd_destroy( struct object *obj )
1164{
Alexandre Julliard863637b2003-01-30 00:26:44 +00001165 struct fd *fd = (struct fd *)obj;
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001166
Robert Shearmand6a4e342005-06-07 20:09:01 +00001167 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1168 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1169
Alexandre Julliardce613492003-03-18 05:04:33 +00001170 remove_fd_locks( fd );
Alexandre Julliard580da242003-03-12 22:38:14 +00001171 list_remove( &fd->inode_entry );
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001172 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
Alexandre Julliard580da242003-03-12 22:38:14 +00001173 if (fd->inode)
1174 {
1175 inode_add_closed_fd( fd->inode, fd->closed );
1176 release_object( fd->inode );
1177 }
1178 else /* no inode, close it right away */
1179 {
1180 if (fd->unix_fd != -1) close( fd->unix_fd );
1181 }
Alexandre Julliard863637b2003-01-30 00:26:44 +00001182}
1183
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001184/* set the events that select waits for on this fd */
1185void set_fd_events( struct fd *fd, int events )
Alexandre Julliard863637b2003-01-30 00:26:44 +00001186{
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001187 int user = fd->poll_index;
1188 assert( poll_users[user] == fd );
Alexandre Julliard969f57c2004-09-23 04:48:24 +00001189
1190 set_fd_epoll_events( fd, user, events );
1191
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001192 if (events == -1) /* stop waiting on this fd completely */
1193 {
1194 pollfd[user].fd = -1;
1195 pollfd[user].events = POLLERR;
1196 pollfd[user].revents = 0;
1197 }
1198 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1199 {
1200 pollfd[user].fd = fd->unix_fd;
1201 pollfd[user].events = events;
1202 }
1203}
Alexandre Julliard863637b2003-01-30 00:26:44 +00001204
Alexandre Julliard964815b2005-08-08 15:11:03 +00001205/* prepare an fd for unmounting its corresponding device */
1206static inline void unmount_fd( struct fd *fd )
1207{
1208 assert( fd->inode );
1209
1210 async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1211 async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1212
1213 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1214
1215 if (fd->unix_fd != -1) close( fd->unix_fd );
1216
1217 fd->unix_fd = -1;
Alexandre Julliard67505c02005-12-12 14:27:45 +01001218 fd->unmounted = 1;
Alexandre Julliard964815b2005-08-08 15:11:03 +00001219 fd->closed->unix_fd = -1;
1220 fd->closed->unlink[0] = 0;
1221
1222 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1223 fd->fs_locks = 0;
1224}
1225
Alexandre Julliard580da242003-03-12 22:38:14 +00001226/* allocate an fd object, without setting the unix fd yet */
1227struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001228{
1229 struct fd *fd = alloc_object( &fd_ops );
1230
Alexandre Julliard580da242003-03-12 22:38:14 +00001231 if (!fd) return NULL;
1232
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001233 fd->fd_ops = fd_user_ops;
1234 fd->user = user;
Alexandre Julliard580da242003-03-12 22:38:14 +00001235 fd->inode = NULL;
1236 fd->closed = NULL;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001237 fd->access = 0;
1238 fd->sharing = 0;
Alexandre Julliard580da242003-03-12 22:38:14 +00001239 fd->unix_fd = -1;
Alexandre Julliardb23aa942003-07-03 18:12:02 +00001240 fd->fs_locks = 1;
Alexandre Julliard67505c02005-12-12 14:27:45 +01001241 fd->unmounted = 0;
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001242 fd->poll_index = -1;
Alexandre Julliard580da242003-03-12 22:38:14 +00001243 list_init( &fd->inode_entry );
Alexandre Julliardce613492003-03-18 05:04:33 +00001244 list_init( &fd->locks );
Robert Shearmand6a4e342005-06-07 20:09:01 +00001245 list_init( &fd->read_q );
1246 list_init( &fd->write_q );
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001247
Alexandre Julliard580da242003-03-12 22:38:14 +00001248 if ((fd->poll_index = add_poll_user( fd )) == -1)
Alexandre Julliard863637b2003-01-30 00:26:44 +00001249 {
1250 release_object( fd );
1251 return NULL;
1252 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001253 return fd;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001254}
1255
Alexandre Julliard67505c02005-12-12 14:27:45 +01001256/* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1257struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user )
1258{
1259 struct fd *fd = alloc_object( &fd_ops );
1260
1261 if (!fd) return NULL;
1262
1263 fd->fd_ops = fd_user_ops;
1264 fd->user = user;
1265 fd->inode = NULL;
1266 fd->closed = NULL;
1267 fd->access = 0;
1268 fd->sharing = 0;
1269 fd->unix_fd = -1;
1270 fd->fs_locks = 0;
1271 fd->unmounted = 0;
1272 fd->poll_index = -1;
1273 list_init( &fd->inode_entry );
1274 list_init( &fd->locks );
1275 list_init( &fd->read_q );
1276 list_init( &fd->write_q );
1277 return fd;
1278}
1279
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001280/* check if the desired access is possible without violating */
1281/* the sharing mode of other opens of the same file */
1282static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1283{
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001284 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001285 unsigned int existing_access = 0;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001286 int unlink = 0;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001287 struct list *ptr;
1288
1289 /* if access mode is 0, sharing mode is ignored */
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001290 if (!access) sharing = existing_sharing;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001291 fd->access = access;
1292 fd->sharing = sharing;
1293
1294 LIST_FOR_EACH( ptr, &fd->inode->open )
1295 {
1296 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1297 if (fd_ptr != fd)
1298 {
1299 existing_sharing &= fd_ptr->sharing;
1300 existing_access |= fd_ptr->access;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001301 if (fd_ptr->closed->unlink[0]) unlink = 1;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001302 }
1303 }
1304
Alexandre Julliarda510a7e2005-12-12 16:46:17 +01001305 if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1306 if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1307 if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0;
1308 if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001309 if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1310 if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001311 return 1;
1312}
1313
Alexandre Julliard580da242003-03-12 22:38:14 +00001314/* open() wrapper using a struct fd */
1315/* the fd must have been created with alloc_fd */
1316/* on error the fd object is released */
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001317struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001318 unsigned int access, unsigned int sharing, unsigned int options )
Alexandre Julliard580da242003-03-12 22:38:14 +00001319{
1320 struct stat st;
1321 struct closed_fd *closed_fd;
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001322 const char *unlink_name = "";
Alexandre Julliard580da242003-03-12 22:38:14 +00001323
1324 assert( fd->unix_fd == -1 );
1325
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001326 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001327 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
Alexandre Julliard580da242003-03-12 22:38:14 +00001328 {
1329 release_object( fd );
1330 return NULL;
1331 }
Alexandre Julliardad9b7992004-04-27 02:27:09 +00001332 /* create the directory if needed */
1333 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1334 {
1335 if (mkdir( name, 0777 ) == -1)
1336 {
1337 if (errno != EEXIST || (flags & O_EXCL))
1338 {
1339 file_set_error();
1340 release_object( fd );
1341 free( closed_fd );
1342 return NULL;
1343 }
1344 }
1345 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1346 }
Alexandre Julliard60287d02004-05-22 03:15:04 +00001347 if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
Alexandre Julliard580da242003-03-12 22:38:14 +00001348 {
1349 file_set_error();
1350 release_object( fd );
1351 free( closed_fd );
1352 return NULL;
1353 }
Alexandre Julliardc1325422005-07-28 10:48:57 +00001354 closed_fd->unix_fd = fd->unix_fd;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001355 closed_fd->unlink[0] = 0;
Alexandre Julliard580da242003-03-12 22:38:14 +00001356 fstat( fd->unix_fd, &st );
1357 *mode = st.st_mode;
1358
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001359 /* only bother with an inode for normal files and directories */
1360 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
Alexandre Julliard580da242003-03-12 22:38:14 +00001361 {
1362 struct inode *inode = get_inode( st.st_dev, st.st_ino );
1363
1364 if (!inode)
1365 {
1366 /* we can close the fd because there are no others open on the same file,
1367 * otherwise we wouldn't have failed to allocate a new inode
1368 */
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001369 goto error;
Alexandre Julliard580da242003-03-12 22:38:14 +00001370 }
1371 fd->inode = inode;
1372 fd->closed = closed_fd;
1373 list_add_head( &inode->open, &fd->inode_entry );
Alexandre Julliard17c775f2004-09-09 00:28:36 +00001374
1375 /* check directory options */
1376 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1377 {
1378 release_object( fd );
1379 set_error( STATUS_NOT_A_DIRECTORY );
1380 return NULL;
1381 }
1382 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1383 {
1384 release_object( fd );
1385 set_error( STATUS_FILE_IS_A_DIRECTORY );
1386 return NULL;
1387 }
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001388 if (!check_sharing( fd, access, sharing ))
1389 {
1390 release_object( fd );
1391 set_error( STATUS_SHARING_VIOLATION );
1392 return NULL;
1393 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001394 strcpy( closed_fd->unlink, unlink_name );
Alexandre Julliard60287d02004-05-22 03:15:04 +00001395 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
Alexandre Julliard580da242003-03-12 22:38:14 +00001396 }
Alexandre Julliard17c775f2004-09-09 00:28:36 +00001397 else /* special file */
Alexandre Julliard580da242003-03-12 22:38:14 +00001398 {
Alexandre Julliard17c775f2004-09-09 00:28:36 +00001399 if (options & FILE_DIRECTORY_FILE)
1400 {
1401 set_error( STATUS_NOT_A_DIRECTORY );
1402 goto error;
1403 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001404 if (unlink_name[0]) /* we can't unlink special files */
1405 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001406 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001407 goto error;
Alexandre Julliard9ff78722004-04-02 19:50:49 +00001408 }
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001409 free( closed_fd );
Alexandre Julliard580da242003-03-12 22:38:14 +00001410 }
1411 return fd;
Alexandre Julliard684b65c2004-04-16 04:31:35 +00001412
1413error:
1414 release_object( fd );
1415 free( closed_fd );
1416 return NULL;
Alexandre Julliard580da242003-03-12 22:38:14 +00001417}
1418
1419/* create an fd for an anonymous file */
1420/* if the function fails the unix fd is closed */
1421struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1422{
1423 struct fd *fd = alloc_fd( fd_user_ops, user );
1424
1425 if (fd)
1426 {
1427 fd->unix_fd = unix_fd;
1428 return fd;
1429 }
1430 close( unix_fd );
1431 return NULL;
1432}
1433
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001434/* retrieve the object that is using an fd */
1435void *get_fd_user( struct fd *fd )
Alexandre Julliard863637b2003-01-30 00:26:44 +00001436{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001437 return fd->user;
1438}
Alexandre Julliard863637b2003-01-30 00:26:44 +00001439
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001440/* retrieve the unix fd for an object */
1441int get_unix_fd( struct fd *fd )
1442{
Alexandre Julliard67505c02005-12-12 14:27:45 +01001443 if (fd->unix_fd == -1)
1444 {
1445 if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED );
1446 else set_error( STATUS_BAD_DEVICE_TYPE );
1447 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001448 return fd->unix_fd;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001449}
1450
Alexandre Julliard74bd1e42004-03-27 20:48:42 +00001451/* check if two file descriptors point to the same file */
1452int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1453{
1454 return fd1->inode == fd2->inode;
1455}
1456
Alexandre Julliard863637b2003-01-30 00:26:44 +00001457/* callback for event happening in the main poll() loop */
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001458void fd_poll_event( struct fd *fd, int event )
Alexandre Julliard863637b2003-01-30 00:26:44 +00001459{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001460 return fd->fd_ops->poll_event( fd, event );
1461}
1462
1463/* check if events are pending and if yes return which one(s) */
1464int check_fd_events( struct fd *fd, int events )
1465{
1466 struct pollfd pfd;
1467
Alexandre Julliard964815b2005-08-08 15:11:03 +00001468 if (fd->unix_fd == -1) return POLLERR;
1469
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001470 pfd.fd = fd->unix_fd;
1471 pfd.events = events;
1472 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1473 return pfd.revents;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001474}
1475
1476/* default add_queue() routine for objects that poll() on an fd */
1477int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1478{
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001479 struct fd *fd = get_obj_fd( obj );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001480
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001481 if (!fd) return 0;
Alexandre Julliard770c4a12005-12-12 17:20:03 +01001482 if (!fd->inode && list_empty( &obj->wait_queue )) /* first on the queue */
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001483 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001484 add_queue( obj, entry );
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001485 release_object( fd );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001486 return 1;
1487}
1488
1489/* default remove_queue() routine for objects that poll() on an fd */
1490void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1491{
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001492 struct fd *fd = get_obj_fd( obj );
1493
Alexandre Julliard863637b2003-01-30 00:26:44 +00001494 grab_object( obj );
1495 remove_queue( obj, entry );
Alexandre Julliard770c4a12005-12-12 17:20:03 +01001496 if (!fd->inode && list_empty( &obj->wait_queue )) /* last on the queue is gone */
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001497 set_fd_events( fd, 0 );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001498 release_object( obj );
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001499 release_object( fd );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001500}
1501
1502/* default signaled() routine for objects that poll() on an fd */
1503int default_fd_signaled( struct object *obj, struct thread *thread )
1504{
Alexandre Julliardb1aad5d2005-02-25 14:02:20 +00001505 int events, ret;
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001506 struct fd *fd = get_obj_fd( obj );
Alexandre Julliardb1aad5d2005-02-25 14:02:20 +00001507
1508 if (fd->inode) return 1; /* regular files are always signaled */
1509
1510 events = fd->fd_ops->get_poll_events( fd );
1511 ret = check_fd_events( fd, events ) != 0;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001512
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001513 if (ret)
1514 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
Alexandre Julliardaa347682005-03-01 11:49:58 +00001515 else if (!list_empty( &obj->wait_queue ))
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001516 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1517
1518 release_object( fd );
1519 return ret;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001520}
1521
Robert Shearmand6a4e342005-06-07 20:09:01 +00001522int default_fd_get_poll_events( struct fd *fd )
1523{
1524 int events = 0;
1525
Robert Shearman37957092005-06-10 19:54:46 +00001526 if (!list_empty( &fd->read_q ))
Robert Shearmand6a4e342005-06-07 20:09:01 +00001527 events |= POLLIN;
Robert Shearman37957092005-06-10 19:54:46 +00001528 if (!list_empty( &fd->write_q ))
Robert Shearmand6a4e342005-06-07 20:09:01 +00001529 events |= POLLOUT;
1530
1531 return events;
1532}
1533
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001534/* default handler for poll() events */
1535void default_poll_event( struct fd *fd, int event )
1536{
Robert Shearmand6a4e342005-06-07 20:09:01 +00001537 if (!list_empty( &fd->read_q ) && (POLLIN & event) )
1538 {
1539 async_terminate_head( &fd->read_q, STATUS_ALERTED );
1540 return;
1541 }
1542 if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
1543 {
1544 async_terminate_head( &fd->write_q, STATUS_ALERTED );
1545 return;
1546 }
1547
1548 /* if an error occurred, stop polling this fd to avoid busy-looping */
Alexandre Julliarde66207e2003-02-19 00:33:32 +00001549 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001550 wake_up( fd->user, 0 );
1551}
1552
Robert Shearman37773dd2005-07-14 12:18:05 +00001553void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout )
Robert Shearmand6a4e342005-06-07 20:09:01 +00001554{
1555 struct list *queue;
1556 int events;
1557
Robert Shearman37773dd2005-07-14 12:18:05 +00001558 if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
Robert Shearmand6a4e342005-06-07 20:09:01 +00001559 {
1560 set_error( STATUS_INVALID_HANDLE );
1561 return;
1562 }
1563
1564 switch (type)
1565 {
1566 case ASYNC_TYPE_READ:
1567 queue = &fd->read_q;
1568 break;
1569 case ASYNC_TYPE_WRITE:
1570 queue = &fd->write_q;
1571 break;
1572 default:
1573 set_error( STATUS_INVALID_PARAMETER );
1574 return;
1575 }
1576
Robert Shearman37773dd2005-07-14 12:18:05 +00001577 if (!create_async( current, timeout, queue, apc, user, io_sb ))
Robert Shearmand6a4e342005-06-07 20:09:01 +00001578 return;
1579
1580 /* Check if the new pending request can be served immediately */
1581 events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1582 if (events) fd->fd_ops->poll_event( fd, events );
1583
1584 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1585}
1586
Robert Shearman37773dd2005-07-14 12:18:05 +00001587void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
1588{
1589 fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
1590}
1591
Robert Shearmand6a4e342005-06-07 20:09:01 +00001592void default_fd_cancel_async( struct fd *fd )
1593{
1594 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1595 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1596}
1597
Alexandre Julliard863637b2003-01-30 00:26:44 +00001598/* default flush() routine */
Mike McCormackef8b9462003-05-15 04:22:45 +00001599int no_flush( struct fd *fd, struct event **event )
Alexandre Julliard863637b2003-01-30 00:26:44 +00001600{
1601 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1602 return 0;
1603}
1604
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001605/* default get_file_info() routine */
Alexandre Julliard6a27b482004-08-18 00:04:58 +00001606int no_get_file_info( struct fd *fd )
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001607{
1608 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliard6a27b482004-08-18 00:04:58 +00001609 return 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001610}
1611
Alexandre Julliard863637b2003-01-30 00:26:44 +00001612/* default queue_async() routine */
Eric Pouech46344472005-01-14 19:54:38 +00001613void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb,
1614 int type, int count)
1615{
1616 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1617}
1618
1619/* default cancel_async() routine */
1620void no_cancel_async( struct fd *fd )
Alexandre Julliard863637b2003-01-30 00:26:44 +00001621{
1622 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1623}
1624
Alexandre Julliard964815b2005-08-08 15:11:03 +00001625/* close all Unix file descriptors on a device to allow unmounting it */
Alexandre Julliardf8037bd2005-10-27 11:20:50 +00001626static void unmount_device( struct fd *device_fd )
Alexandre Julliard964815b2005-08-08 15:11:03 +00001627{
1628 unsigned int i;
Alexandre Julliardf8037bd2005-10-27 11:20:50 +00001629 struct stat st;
1630 struct device *device;
Alexandre Julliard964815b2005-08-08 15:11:03 +00001631 struct inode *inode;
1632 struct fd *fd;
Alexandre Julliard67505c02005-12-12 14:27:45 +01001633 int unix_fd = get_unix_fd( device_fd );
Alexandre Julliard964815b2005-08-08 15:11:03 +00001634
Alexandre Julliard67505c02005-12-12 14:27:45 +01001635 if (unix_fd == -1) return;
1636
1637 if (fstat( unix_fd, &st ) == -1 || !S_ISBLK( st.st_mode ))
Alexandre Julliardf8037bd2005-10-27 11:20:50 +00001638 {
1639 set_error( STATUS_INVALID_PARAMETER );
1640 return;
1641 }
1642
1643 if (!(device = get_device( st.st_rdev, 0 ))) return;
1644
Alexandre Julliard964815b2005-08-08 15:11:03 +00001645 for (i = 0; i < INODE_HASH_SIZE; i++)
1646 {
1647 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
1648 {
1649 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
1650 {
1651 unmount_fd( fd );
1652 }
1653 inode_close_pending( inode, 0 );
1654 }
1655 }
1656 /* remove it from the hash table */
1657 list_remove( &device->entry );
1658 list_init( &device->entry );
Alexandre Julliardf8037bd2005-10-27 11:20:50 +00001659 release_object( device );
Alexandre Julliard964815b2005-08-08 15:11:03 +00001660}
1661
Alexandre Julliard863637b2003-01-30 00:26:44 +00001662/* same as get_handle_obj but retrieve the struct fd associated to the object */
1663static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1664 unsigned int access )
1665{
1666 struct fd *fd = NULL;
1667 struct object *obj;
1668
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001669 if ((obj = get_handle_obj( process, handle, access, NULL )))
Alexandre Julliard863637b2003-01-30 00:26:44 +00001670 {
Bill Medland3f7c3ff2003-04-17 02:14:04 +00001671 fd = get_obj_fd( obj );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001672 release_object( obj );
1673 }
1674 return fd;
1675}
1676
Alexandre Julliard863637b2003-01-30 00:26:44 +00001677/* flush a file buffers */
1678DECL_HANDLER(flush_file)
1679{
1680 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
Mike McCormackef8b9462003-05-15 04:22:45 +00001681 struct event * event = NULL;
Alexandre Julliard863637b2003-01-30 00:26:44 +00001682
1683 if (fd)
1684 {
Mike McCormackef8b9462003-05-15 04:22:45 +00001685 fd->fd_ops->flush( fd, &event );
Eric Pouech46344472005-01-14 19:54:38 +00001686 if ( event )
Mike McCormackef8b9462003-05-15 04:22:45 +00001687 {
1688 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1689 }
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001690 release_object( fd );
1691 }
1692}
1693
Alexandre Julliard67505c02005-12-12 14:27:45 +01001694/* open a file object */
1695DECL_HANDLER(open_file_object)
1696{
1697 struct unicode_str name;
1698 struct directory *root = NULL;
1699 struct object *obj;
1700
1701 get_req_unicode_str( &name );
1702 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
1703 return;
1704
1705 if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
1706 {
1707 /* make sure this is a valid file object */
1708 struct fd *fd = get_obj_fd( obj );
1709 if (fd)
1710 {
1711 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1712 release_object( fd );
1713 }
1714 release_object( obj );
1715 }
1716
1717 if (root) release_object( root );
1718}
1719
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001720/* get a Unix fd to access a file */
1721DECL_HANDLER(get_handle_fd)
1722{
1723 struct fd *fd;
1724
1725 reply->fd = -1;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001726
1727 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1728 {
Alexandre Julliard964815b2005-08-08 15:11:03 +00001729 int unix_fd = get_unix_fd( fd );
1730 if (unix_fd != -1)
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001731 {
Alexandre Julliard964815b2005-08-08 15:11:03 +00001732 int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1733 if (cached_fd != -1) reply->fd = cached_fd;
1734 else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001735 }
Alexandre Julliardf62f6e82005-08-24 18:33:50 +00001736 if (fd->inode) reply->removable = fd->inode->device->removable;
Alexandre Julliard6a27b482004-08-18 00:04:58 +00001737 reply->flags = fd->fd_ops->get_file_info( fd );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001738 release_object( fd );
1739 }
1740}
1741
Alexandre Julliardf62f6e82005-08-24 18:33:50 +00001742/* set the cached file descriptor of a handle */
1743DECL_HANDLER(set_handle_fd)
1744{
1745 struct fd *fd;
1746
1747 reply->cur_fd = -1;
1748 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1749 {
1750 struct device *device = fd->inode ? fd->inode->device : NULL;
1751
1752 if (device && device->removable == -1) device->removable = req->removable;
1753
1754 /* only cache the fd on non-removable devices */
1755 if (!device || !device->removable)
1756 reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd );
1757 release_object( fd );
1758 }
1759}
1760
Alexandre Julliard964815b2005-08-08 15:11:03 +00001761/* get ready to unmount a Unix device */
1762DECL_HANDLER(unmount_device)
1763{
1764 struct fd *fd;
1765
1766 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1767 {
Alexandre Julliardf8037bd2005-10-27 11:20:50 +00001768 unmount_device( fd );
Alexandre Julliard964815b2005-08-08 15:11:03 +00001769 release_object( fd );
1770 }
1771}
1772
Alexandre Julliard863637b2003-01-30 00:26:44 +00001773/* create / reschedule an async I/O */
1774DECL_HANDLER(register_async)
1775{
1776 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1777
Eric Pouech46344472005-01-14 19:54:38 +00001778 /*
1779 * The queue_async method must do the following:
1780 *
1781 * 1. Get the async_queue for the request of given type.
1782 * 2. Create a new asynchronous request for the selected queue
1783 * 3. Carry out any operations necessary to adjust the object's poll events
1784 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1785 * 4. When the async request is triggered, then send back (with a proper APC)
1786 * the trigger (STATUS_ALERTED) to the thread that posted the request.
1787 * async_destroy() is to be called: it will both notify the sender about
1788 * the trigger and destroy the request by itself
1789 * See also the implementations in file.c, serial.c, and sock.c.
1790 */
Alexandre Julliard863637b2003-01-30 00:26:44 +00001791
1792 if (fd)
1793 {
Eric Pouech46344472005-01-14 19:54:38 +00001794 fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb,
1795 req->type, req->count );
Alexandre Julliard863637b2003-01-30 00:26:44 +00001796 release_object( fd );
1797 }
1798}
Eric Pouech46344472005-01-14 19:54:38 +00001799
1800/* cancels all async I/O */
1801DECL_HANDLER(cancel_async)
1802{
1803 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1804 if (fd)
1805 {
1806 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1807 * NtCancelIoFile() will force the pending APC to be run. Since,
1808 * Windows only guarantees that the current thread will have no async
1809 * operation on the current fd when NtCancelIoFile returns, this shall
1810 * do the work.
1811 */
1812 fd->fd_ops->cancel_async( fd );
1813 release_object( fd );
1814 }
1815}