winex11.drv: GetAsyncKeyState must check mouse buttons, too.
[wine.git] / server / fd.c
blob88fa6a44642b9378501ea141e78769247d2ba24d
1 /*
2 * Server-side file descriptor management
4 * Copyright (C) 2000, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_LINUX_MAJOR_H
41 #include <linux/major.h>
42 #endif
43 #ifdef HAVE_SYS_STATVFS_H
44 #include <sys/statvfs.h>
45 #endif
46 #ifdef HAVE_SYS_VFS_H
47 #include <sys/vfs.h>
48 #endif
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
52 #ifdef HAVE_SYS_MOUNT_H
53 #include <sys/mount.h>
54 #endif
55 #ifdef HAVE_SYS_STATFS_H
56 #include <sys/statfs.h>
57 #endif
58 #ifdef HAVE_SYS_EVENT_H
59 #include <sys/event.h>
60 #undef LIST_INIT
61 #undef LIST_ENTRY
62 #endif
63 #ifdef HAVE_STDINT_H
64 #include <stdint.h>
65 #endif
66 #include <sys/stat.h>
67 #include <sys/time.h>
68 #include <sys/types.h>
69 #include <unistd.h>
71 #include "ntstatus.h"
72 #define WIN32_NO_STATUS
73 #include "object.h"
74 #include "file.h"
75 #include "handle.h"
76 #include "process.h"
77 #include "request.h"
79 #include "winternl.h"
81 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
82 # include <sys/epoll.h>
83 # define USE_EPOLL
84 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
85 # define USE_EPOLL
86 # define EPOLLIN POLLIN
87 # define EPOLLOUT POLLOUT
88 # define EPOLLERR POLLERR
89 # define EPOLLHUP POLLHUP
90 # define EPOLL_CTL_ADD 1
91 # define EPOLL_CTL_DEL 2
92 # define EPOLL_CTL_MOD 3
94 typedef union epoll_data
96 void *ptr;
97 int fd;
98 uint32_t u32;
99 uint64_t u64;
100 } epoll_data_t;
102 struct epoll_event
104 uint32_t events;
105 epoll_data_t data;
108 #define SYSCALL_RET(ret) do { \
109 if (ret < 0) { errno = -ret; ret = -1; } \
110 return ret; \
111 } while(0)
113 static inline int epoll_create( int size )
115 int ret;
116 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
117 : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
118 SYSCALL_RET(ret);
121 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
123 int ret;
124 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
125 : "=a" (ret)
126 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
127 SYSCALL_RET(ret);
130 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
132 int ret;
133 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
134 : "=a" (ret)
135 : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
136 : "memory" );
137 SYSCALL_RET(ret);
139 #undef SYSCALL_RET
141 #endif /* linux && __i386__ && HAVE_STDINT_H */
144 /* Because of the stupid Posix locking semantics, we need to keep
145 * track of all file descriptors referencing a given file, and not
146 * close a single one until all the locks are gone (sigh).
149 /* file descriptor object */
151 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
152 struct closed_fd
154 struct list entry; /* entry in inode closed list */
155 int unix_fd; /* the unix file descriptor */
156 char unlink[1]; /* name to unlink on close (if any) */
159 struct fd
161 struct object obj; /* object header */
162 const struct fd_ops *fd_ops; /* file descriptor operations */
163 struct inode *inode; /* inode that this fd belongs to */
164 struct list inode_entry; /* entry in inode fd list */
165 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
166 struct object *user; /* object using this file descriptor */
167 struct list locks; /* list of locks on this fd */
168 unsigned int access; /* file access (FILE_READ_DATA etc.) */
169 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
170 unsigned int sharing; /* file sharing mode */
171 int unix_fd; /* unix file descriptor */
172 int signaled :1; /* is the fd signaled? */
173 int fs_locks :1; /* can we use filesystem locks for this fd? */
174 int unmounted :1;/* has the device been unmounted? */
175 int poll_index; /* index of fd in poll array */
176 struct async_queue *read_q; /* async readers of this fd */
177 struct async_queue *write_q; /* async writers of this fd */
178 struct async_queue *wait_q; /* other async waiters of this fd */
181 static void fd_dump( struct object *obj, int verbose );
182 static void fd_destroy( struct object *obj );
184 static const struct object_ops fd_ops =
186 sizeof(struct fd), /* size */
187 fd_dump, /* dump */
188 no_add_queue, /* add_queue */
189 NULL, /* remove_queue */
190 NULL, /* signaled */
191 NULL, /* satisfied */
192 no_signal, /* signal */
193 no_get_fd, /* get_fd */
194 no_map_access, /* map_access */
195 no_lookup_name, /* lookup_name */
196 no_open_file, /* open_file */
197 no_close_handle, /* close_handle */
198 fd_destroy /* destroy */
201 /* device object */
203 #define DEVICE_HASH_SIZE 7
204 #define INODE_HASH_SIZE 17
206 struct device
208 struct object obj; /* object header */
209 struct list entry; /* entry in device hash list */
210 dev_t dev; /* device number */
211 int removable; /* removable device? (or -1 if unknown) */
212 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
215 static void device_dump( struct object *obj, int verbose );
216 static void device_destroy( struct object *obj );
218 static const struct object_ops device_ops =
220 sizeof(struct device), /* size */
221 device_dump, /* dump */
222 no_add_queue, /* add_queue */
223 NULL, /* remove_queue */
224 NULL, /* signaled */
225 NULL, /* satisfied */
226 no_signal, /* signal */
227 no_get_fd, /* get_fd */
228 no_map_access, /* map_access */
229 no_lookup_name, /* lookup_name */
230 no_open_file, /* open_file */
231 no_close_handle, /* close_handle */
232 device_destroy /* destroy */
235 /* inode object */
237 struct inode
239 struct object obj; /* object header */
240 struct list entry; /* inode hash list entry */
241 struct device *device; /* device containing this inode */
242 ino_t ino; /* inode number */
243 struct list open; /* list of open file descriptors */
244 struct list locks; /* list of file locks */
245 struct list closed; /* list of file descriptors to close at destroy time */
248 static void inode_dump( struct object *obj, int verbose );
249 static void inode_destroy( struct object *obj );
251 static const struct object_ops inode_ops =
253 sizeof(struct inode), /* size */
254 inode_dump, /* dump */
255 no_add_queue, /* add_queue */
256 NULL, /* remove_queue */
257 NULL, /* signaled */
258 NULL, /* satisfied */
259 no_signal, /* signal */
260 no_get_fd, /* get_fd */
261 no_map_access, /* map_access */
262 no_lookup_name, /* lookup_name */
263 no_open_file, /* open_file */
264 no_close_handle, /* close_handle */
265 inode_destroy /* destroy */
268 /* file lock object */
270 struct file_lock
272 struct object obj; /* object header */
273 struct fd *fd; /* fd owning this lock */
274 struct list fd_entry; /* entry in list of locks on a given fd */
275 struct list inode_entry; /* entry in inode list of locks */
276 int shared; /* shared lock? */
277 file_pos_t start; /* locked region is interval [start;end) */
278 file_pos_t end;
279 struct process *process; /* process owning this lock */
280 struct list proc_entry; /* entry in list of locks owned by the process */
283 static void file_lock_dump( struct object *obj, int verbose );
284 static int file_lock_signaled( struct object *obj, struct thread *thread );
286 static const struct object_ops file_lock_ops =
288 sizeof(struct file_lock), /* size */
289 file_lock_dump, /* dump */
290 add_queue, /* add_queue */
291 remove_queue, /* remove_queue */
292 file_lock_signaled, /* signaled */
293 no_satisfied, /* satisfied */
294 no_signal, /* signal */
295 no_get_fd, /* get_fd */
296 no_map_access, /* map_access */
297 no_lookup_name, /* lookup_name */
298 no_open_file, /* open_file */
299 no_close_handle, /* close_handle */
300 no_destroy /* destroy */
304 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
305 #define FILE_POS_T_MAX (~(file_pos_t)0)
307 static file_pos_t max_unix_offset = OFF_T_MAX;
309 #define DUMP_LONG_LONG(val) do { \
310 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
311 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
312 else \
313 fprintf( stderr, "%lx", (unsigned long)(val) ); \
314 } while (0)
318 /****************************************************************/
319 /* timeouts support */
321 struct timeout_user
323 struct list entry; /* entry in sorted timeout list */
324 struct timeval when; /* timeout expiry (absolute time) */
325 timeout_callback callback; /* callback function */
326 void *private; /* callback private data */
329 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
330 struct timeval current_time;
332 /* add a timeout user */
333 struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
334 void *private )
336 struct timeout_user *user;
337 struct list *ptr;
339 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
340 user->when = *when;
341 user->callback = func;
342 user->private = private;
344 /* Now insert it in the linked list */
346 LIST_FOR_EACH( ptr, &timeout_list )
348 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
349 if (!time_before( &timeout->when, when )) break;
351 list_add_before( ptr, &user->entry );
352 return user;
355 /* remove a timeout user */
356 void remove_timeout_user( struct timeout_user *user )
358 list_remove( &user->entry );
359 free( user );
362 /* add a timeout in milliseconds to an absolute time */
363 void add_timeout( struct timeval *when, int timeout )
365 if (timeout)
367 long sec = timeout / 1000;
368 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
370 when->tv_usec -= 1000000;
371 when->tv_sec++;
373 when->tv_sec += sec;
378 /****************************************************************/
379 /* poll support */
381 static struct fd **poll_users; /* users array */
382 static struct pollfd *pollfd; /* poll fd array */
383 static int nb_users; /* count of array entries actually in use */
384 static int active_users; /* current number of active users */
385 static int allocated_users; /* count of allocated entries in the array */
386 static struct fd **freelist; /* list of free entries in the array */
388 static int get_next_timeout(void);
390 #ifdef USE_EPOLL
392 static int epoll_fd = -1;
394 static inline void init_epoll(void)
396 epoll_fd = epoll_create( 128 );
399 /* set the events that epoll waits for on this fd; helper for set_fd_events */
400 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
402 struct epoll_event ev;
403 int ctl;
405 if (epoll_fd == -1) return;
407 if (events == -1) /* stop waiting on this fd completely */
409 if (pollfd[user].fd == -1) return; /* already removed */
410 ctl = EPOLL_CTL_DEL;
412 else if (pollfd[user].fd == -1)
414 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
415 ctl = EPOLL_CTL_ADD;
417 else
419 if (pollfd[user].events == events) return; /* nothing to do */
420 ctl = EPOLL_CTL_MOD;
423 ev.events = events;
424 memset(&ev.data, 0, sizeof(ev.data));
425 ev.data.u32 = user;
427 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
429 if (errno == ENOMEM) /* not enough memory, give up on epoll */
431 close( epoll_fd );
432 epoll_fd = -1;
434 else perror( "epoll_ctl" ); /* should not happen */
438 static inline void remove_epoll_user( struct fd *fd, int user )
440 if (epoll_fd == -1) return;
442 if (pollfd[user].fd != -1)
444 struct epoll_event dummy;
445 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
449 static inline void main_loop_epoll(void)
451 int i, ret, timeout;
452 struct epoll_event events[128];
454 assert( POLLIN == EPOLLIN );
455 assert( POLLOUT == EPOLLOUT );
456 assert( POLLERR == EPOLLERR );
457 assert( POLLHUP == EPOLLHUP );
459 if (epoll_fd == -1) return;
461 while (active_users)
463 timeout = get_next_timeout();
465 if (!active_users) break; /* last user removed by a timeout */
466 if (epoll_fd == -1) break; /* an error occurred with epoll */
468 ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
469 gettimeofday( &current_time, NULL );
471 /* put the events into the pollfd array first, like poll does */
472 for (i = 0; i < ret; i++)
474 int user = events[i].data.u32;
475 pollfd[user].revents = events[i].events;
478 /* read events from the pollfd array, as set_fd_events may modify them */
479 for (i = 0; i < ret; i++)
481 int user = events[i].data.u32;
482 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
487 #elif defined(HAVE_KQUEUE)
489 static int kqueue_fd = -1;
491 static inline void init_epoll(void)
493 #ifndef __APPLE__ /* kqueue support is broken in the MacOS kernel so we can't use it */
494 kqueue_fd = kqueue();
495 #endif
498 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
500 struct kevent ev[2];
502 if (kqueue_fd == -1) return;
504 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
505 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
507 if (events == -1) /* stop waiting on this fd completely */
509 if (pollfd[user].fd == -1) return; /* already removed */
510 ev[0].flags |= EV_DELETE;
511 ev[1].flags |= EV_DELETE;
513 else if (pollfd[user].fd == -1)
515 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
516 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
517 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
519 else
521 if (pollfd[user].events == events) return; /* nothing to do */
522 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
523 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
526 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
528 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
530 close( kqueue_fd );
531 kqueue_fd = -1;
533 else perror( "kevent" ); /* should not happen */
537 static inline void remove_epoll_user( struct fd *fd, int user )
539 if (kqueue_fd == -1) return;
541 if (pollfd[user].fd != -1)
543 struct kevent ev[2];
545 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
546 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
547 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
551 static inline void main_loop_epoll(void)
553 int i, ret, timeout;
554 struct kevent events[128];
556 if (kqueue_fd == -1) return;
558 while (active_users)
560 timeout = get_next_timeout();
562 if (!active_users) break; /* last user removed by a timeout */
563 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
565 if (timeout != -1)
567 struct timespec ts;
569 ts.tv_sec = timeout / 1000;
570 ts.tv_nsec = (timeout % 1000) * 1000000;
571 ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
573 else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
575 gettimeofday( &current_time, NULL );
577 /* put the events into the pollfd array first, like poll does */
578 for (i = 0; i < ret; i++)
580 long user = (long)events[i].udata;
581 pollfd[user].revents = 0;
583 for (i = 0; i < ret; i++)
585 long user = (long)events[i].udata;
586 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
587 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
588 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
589 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
592 /* read events from the pollfd array, as set_fd_events may modify them */
593 for (i = 0; i < ret; i++)
595 long user = (long)events[i].udata;
596 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
597 pollfd[user].revents = 0;
602 #else /* HAVE_KQUEUE */
604 static inline void init_epoll(void) { }
605 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
606 static inline void remove_epoll_user( struct fd *fd, int user ) { }
607 static inline void main_loop_epoll(void) { }
609 #endif /* USE_EPOLL */
612 /* add a user in the poll array and return its index, or -1 on failure */
613 static int add_poll_user( struct fd *fd )
615 int ret;
616 if (freelist)
618 ret = freelist - poll_users;
619 freelist = (struct fd **)poll_users[ret];
621 else
623 if (nb_users == allocated_users)
625 struct fd **newusers;
626 struct pollfd *newpoll;
627 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
628 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
629 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
631 if (allocated_users)
632 poll_users = newusers;
633 else
634 free( newusers );
635 return -1;
637 poll_users = newusers;
638 pollfd = newpoll;
639 if (!allocated_users) init_epoll();
640 allocated_users = new_count;
642 ret = nb_users++;
644 pollfd[ret].fd = -1;
645 pollfd[ret].events = 0;
646 pollfd[ret].revents = 0;
647 poll_users[ret] = fd;
648 active_users++;
649 return ret;
652 /* remove a user from the poll list */
653 static void remove_poll_user( struct fd *fd, int user )
655 assert( user >= 0 );
656 assert( poll_users[user] == fd );
658 remove_epoll_user( fd, user );
659 pollfd[user].fd = -1;
660 pollfd[user].events = 0;
661 pollfd[user].revents = 0;
662 poll_users[user] = (struct fd *)freelist;
663 freelist = &poll_users[user];
664 active_users--;
667 /* process pending timeouts and return the time until the next timeout, in milliseconds */
668 static int get_next_timeout(void)
670 if (!list_empty( &timeout_list ))
672 struct list expired_list, *ptr;
674 /* first remove all expired timers from the list */
676 list_init( &expired_list );
677 while ((ptr = list_head( &timeout_list )) != NULL)
679 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
681 if (!time_before( &current_time, &timeout->when ))
683 list_remove( &timeout->entry );
684 list_add_tail( &expired_list, &timeout->entry );
686 else break;
689 /* now call the callback for all the removed timers */
691 while ((ptr = list_head( &expired_list )) != NULL)
693 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
694 list_remove( &timeout->entry );
695 timeout->callback( timeout->private );
696 free( timeout );
699 if ((ptr = list_head( &timeout_list )) != NULL)
701 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
702 int diff = (timeout->when.tv_sec - current_time.tv_sec) * 1000
703 + (timeout->when.tv_usec - current_time.tv_usec + 999) / 1000;
704 if (diff < 0) diff = 0;
705 return diff;
708 return -1; /* no pending timeouts */
711 /* server main poll() loop */
712 void main_loop(void)
714 int i, ret, timeout;
716 gettimeofday( &current_time, NULL );
718 main_loop_epoll();
719 /* fall through to normal poll loop */
721 while (active_users)
723 timeout = get_next_timeout();
725 if (!active_users) break; /* last user removed by a timeout */
727 ret = poll( pollfd, nb_users, timeout );
728 gettimeofday( &current_time, NULL );
730 if (ret > 0)
732 for (i = 0; i < nb_users; i++)
734 if (pollfd[i].revents)
736 fd_poll_event( poll_users[i], pollfd[i].revents );
737 if (!--ret) break;
745 /****************************************************************/
746 /* device functions */
748 static struct list device_hash[DEVICE_HASH_SIZE];
750 static int is_device_removable( dev_t dev, int unix_fd )
752 #if defined(linux) && defined(HAVE_FSTATFS)
753 struct statfs stfs;
755 /* check for floppy disk */
756 if (major(dev) == FLOPPY_MAJOR) return 1;
758 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
759 return (stfs.f_type == 0x9660 || /* iso9660 */
760 stfs.f_type == 0x9fa1 || /* supermount */
761 stfs.f_type == 0x15013346); /* udf */
762 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
763 struct statfs stfs;
765 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
766 return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
767 !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
768 #elif defined(__NetBSD__)
769 struct statvfs stfs;
771 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
772 return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
773 !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
774 #elif defined(sun)
775 # include <sys/dkio.h>
776 # include <sys/vtoc.h>
777 struct dk_cinfo dkinf;
778 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
779 return (dkinf.dki_ctype == DKC_CDROM ||
780 dkinf.dki_ctype == DKC_NCRFLOPPY ||
781 dkinf.dki_ctype == DKC_SMSFLOPPY ||
782 dkinf.dki_ctype == DKC_INTEL82072 ||
783 dkinf.dki_ctype == DKC_INTEL82077);
784 #else
785 return 0;
786 #endif
789 /* retrieve the device object for a given fd, creating it if needed */
790 static struct device *get_device( dev_t dev, int unix_fd )
792 struct device *device;
793 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
795 if (device_hash[hash].next)
797 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
798 if (device->dev == dev) return (struct device *)grab_object( device );
800 else list_init( &device_hash[hash] );
802 /* not found, create it */
804 if (unix_fd == -1) return NULL;
805 if ((device = alloc_object( &device_ops )))
807 device->dev = dev;
808 device->removable = is_device_removable( dev, unix_fd );
809 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
810 list_add_head( &device_hash[hash], &device->entry );
812 return device;
815 static void device_dump( struct object *obj, int verbose )
817 struct device *device = (struct device *)obj;
818 fprintf( stderr, "Device dev=" );
819 DUMP_LONG_LONG( device->dev );
820 fprintf( stderr, "\n" );
823 static void device_destroy( struct object *obj )
825 struct device *device = (struct device *)obj;
826 unsigned int i;
828 for (i = 0; i < INODE_HASH_SIZE; i++)
829 assert( list_empty(&device->inode_hash[i]) );
831 list_remove( &device->entry ); /* remove it from the hash table */
835 /****************************************************************/
836 /* inode functions */
838 /* close all pending file descriptors in the closed list */
839 static void inode_close_pending( struct inode *inode, int keep_unlinks )
841 struct list *ptr = list_head( &inode->closed );
843 while (ptr)
845 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
846 struct list *next = list_next( &inode->closed, ptr );
848 if (fd->unix_fd != -1)
850 close( fd->unix_fd );
851 fd->unix_fd = -1;
853 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
855 list_remove( ptr );
856 free( fd );
858 ptr = next;
862 static void inode_dump( struct object *obj, int verbose )
864 struct inode *inode = (struct inode *)obj;
865 fprintf( stderr, "Inode device=%p ino=", inode->device );
866 DUMP_LONG_LONG( inode->ino );
867 fprintf( stderr, "\n" );
870 static void inode_destroy( struct object *obj )
872 struct inode *inode = (struct inode *)obj;
873 struct list *ptr;
875 assert( list_empty(&inode->open) );
876 assert( list_empty(&inode->locks) );
878 list_remove( &inode->entry );
880 while ((ptr = list_head( &inode->closed )))
882 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
883 list_remove( ptr );
884 if (fd->unix_fd != -1) close( fd->unix_fd );
885 if (fd->unlink[0])
887 /* make sure it is still the same file */
888 struct stat st;
889 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
891 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
892 else unlink( fd->unlink );
895 free( fd );
897 release_object( inode->device );
900 /* retrieve the inode object for a given fd, creating it if needed */
901 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
903 struct device *device;
904 struct inode *inode;
905 unsigned int hash = ino % INODE_HASH_SIZE;
907 if (!(device = get_device( dev, unix_fd ))) return NULL;
909 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
911 if (inode->ino == ino)
913 release_object( device );
914 return (struct inode *)grab_object( inode );
918 /* not found, create it */
919 if ((inode = alloc_object( &inode_ops )))
921 inode->device = device;
922 inode->ino = ino;
923 list_init( &inode->open );
924 list_init( &inode->locks );
925 list_init( &inode->closed );
926 list_add_head( &device->inode_hash[hash], &inode->entry );
928 else release_object( device );
930 return inode;
933 /* add fd to the inode list of file descriptors to close */
934 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
936 if (!list_empty( &inode->locks ))
938 list_add_head( &inode->closed, &fd->entry );
940 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
942 if (fd->unix_fd != -1) close( fd->unix_fd );
943 fd->unix_fd = -1;
944 list_add_head( &inode->closed, &fd->entry );
946 else /* no locks on this inode and no unlink, get rid of the fd */
948 if (fd->unix_fd != -1) close( fd->unix_fd );
949 free( fd );
954 /****************************************************************/
955 /* file lock functions */
957 static void file_lock_dump( struct object *obj, int verbose )
959 struct file_lock *lock = (struct file_lock *)obj;
960 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
961 lock->shared ? "shared" : "excl", lock->fd, lock->process );
962 DUMP_LONG_LONG( lock->start );
963 fprintf( stderr, " end=" );
964 DUMP_LONG_LONG( lock->end );
965 fprintf( stderr, "\n" );
968 static int file_lock_signaled( struct object *obj, struct thread *thread )
970 struct file_lock *lock = (struct file_lock *)obj;
971 /* lock is signaled if it has lost its owner */
972 return !lock->process;
975 /* set (or remove) a Unix lock if possible for the given range */
976 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
978 struct flock fl;
980 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
981 for (;;)
983 if (start == end) return 1; /* can't set zero-byte lock */
984 if (start > max_unix_offset) return 1; /* ignore it */
985 fl.l_type = type;
986 fl.l_whence = SEEK_SET;
987 fl.l_start = start;
988 if (!end || end > max_unix_offset) fl.l_len = 0;
989 else fl.l_len = end - start;
990 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
992 switch(errno)
994 case EACCES:
995 /* check whether locks work at all on this file system */
996 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
998 set_error( STATUS_FILE_LOCK_CONFLICT );
999 return 0;
1001 /* fall through */
1002 case EIO:
1003 case ENOLCK:
1004 /* no locking on this fs, just ignore it */
1005 fd->fs_locks = 0;
1006 return 1;
1007 case EAGAIN:
1008 set_error( STATUS_FILE_LOCK_CONFLICT );
1009 return 0;
1010 case EBADF:
1011 /* this can happen if we try to set a write lock on a read-only file */
1012 /* we just ignore that error */
1013 if (fl.l_type == F_WRLCK) return 1;
1014 set_error( STATUS_ACCESS_DENIED );
1015 return 0;
1016 #ifdef EOVERFLOW
1017 case EOVERFLOW:
1018 #endif
1019 case EINVAL:
1020 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1021 /* in that case we shrink the limit and retry */
1022 if (max_unix_offset > INT_MAX)
1024 max_unix_offset = INT_MAX;
1025 break; /* retry */
1027 /* fall through */
1028 default:
1029 file_set_error();
1030 return 0;
1035 /* check if interval [start;end) overlaps the lock */
1036 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1038 if (lock->end && start >= lock->end) return 0;
1039 if (end && lock->start >= end) return 0;
1040 return 1;
1043 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1044 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1046 struct hole
1048 struct hole *next;
1049 struct hole *prev;
1050 file_pos_t start;
1051 file_pos_t end;
1052 } *first, *cur, *next, *buffer;
1054 struct list *ptr;
1055 int count = 0;
1057 if (!fd->inode) return;
1058 if (!fd->fs_locks) return;
1059 if (start == end || start > max_unix_offset) return;
1060 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1062 /* count the number of locks overlapping the specified area */
1064 LIST_FOR_EACH( ptr, &fd->inode->locks )
1066 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1067 if (lock->start == lock->end) continue;
1068 if (lock_overlaps( lock, start, end )) count++;
1071 if (!count) /* no locks at all, we can unlock everything */
1073 set_unix_lock( fd, start, end, F_UNLCK );
1074 return;
1077 /* allocate space for the list of holes */
1078 /* max. number of holes is number of locks + 1 */
1080 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1081 first = buffer;
1082 first->next = NULL;
1083 first->prev = NULL;
1084 first->start = start;
1085 first->end = end;
1086 next = first + 1;
1088 /* build a sorted list of unlocked holes in the specified area */
1090 LIST_FOR_EACH( ptr, &fd->inode->locks )
1092 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1093 if (lock->start == lock->end) continue;
1094 if (!lock_overlaps( lock, start, end )) continue;
1096 /* go through all the holes touched by this lock */
1097 for (cur = first; cur; cur = cur->next)
1099 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1100 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1102 /* now we know that lock is overlapping hole */
1104 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1106 cur->start = lock->end;
1107 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1108 /* now hole is empty, remove it */
1109 if (cur->next) cur->next->prev = cur->prev;
1110 if (cur->prev) cur->prev->next = cur->next;
1111 else if (!(first = cur->next)) goto done; /* no more holes at all */
1113 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1115 cur->end = lock->start;
1116 assert( cur->start < cur->end );
1118 else /* lock is in the middle of hole, split hole in two */
1120 next->prev = cur;
1121 next->next = cur->next;
1122 cur->next = next;
1123 next->start = lock->end;
1124 next->end = cur->end;
1125 cur->end = lock->start;
1126 assert( next->start < next->end );
1127 assert( cur->end < next->start );
1128 next++;
1129 break; /* done with this lock */
1134 /* clear Unix locks for all the holes */
1136 for (cur = first; cur; cur = cur->next)
1137 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1139 done:
1140 free( buffer );
1143 /* create a new lock on a fd */
1144 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1146 struct file_lock *lock;
1148 if (!fd->inode) /* not a regular file */
1150 set_error( STATUS_INVALID_HANDLE );
1151 return NULL;
1154 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1155 lock->shared = shared;
1156 lock->start = start;
1157 lock->end = end;
1158 lock->fd = fd;
1159 lock->process = current->process;
1161 /* now try to set a Unix lock */
1162 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1164 release_object( lock );
1165 return NULL;
1167 list_add_head( &fd->locks, &lock->fd_entry );
1168 list_add_head( &fd->inode->locks, &lock->inode_entry );
1169 list_add_head( &lock->process->locks, &lock->proc_entry );
1170 return lock;
1173 /* remove an existing lock */
1174 static void remove_lock( struct file_lock *lock, int remove_unix )
1176 struct inode *inode = lock->fd->inode;
1178 list_remove( &lock->fd_entry );
1179 list_remove( &lock->inode_entry );
1180 list_remove( &lock->proc_entry );
1181 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1182 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1183 lock->process = NULL;
1184 wake_up( &lock->obj, 0 );
1185 release_object( lock );
1188 /* remove all locks owned by a given process */
1189 void remove_process_locks( struct process *process )
1191 struct list *ptr;
1193 while ((ptr = list_head( &process->locks )))
1195 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1196 remove_lock( lock, 1 ); /* this removes it from the list */
1200 /* remove all locks on a given fd */
1201 static void remove_fd_locks( struct fd *fd )
1203 file_pos_t start = FILE_POS_T_MAX, end = 0;
1204 struct list *ptr;
1206 while ((ptr = list_head( &fd->locks )))
1208 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1209 if (lock->start < start) start = lock->start;
1210 if (!lock->end || lock->end > end) end = lock->end - 1;
1211 remove_lock( lock, 0 );
1213 if (start < end) remove_unix_locks( fd, start, end + 1 );
1216 /* add a lock on an fd */
1217 /* returns handle to wait on */
1218 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1220 struct list *ptr;
1221 file_pos_t end = start + count;
1223 /* don't allow wrapping locks */
1224 if (end && end < start)
1226 set_error( STATUS_INVALID_PARAMETER );
1227 return 0;
1230 /* check if another lock on that file overlaps the area */
1231 LIST_FOR_EACH( ptr, &fd->inode->locks )
1233 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1234 if (!lock_overlaps( lock, start, end )) continue;
1235 if (lock->shared && shared) continue;
1236 /* found one */
1237 if (!wait)
1239 set_error( STATUS_FILE_LOCK_CONFLICT );
1240 return 0;
1242 set_error( STATUS_PENDING );
1243 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1246 /* not found, add it */
1247 if (add_lock( fd, shared, start, end )) return 0;
1248 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1250 /* Unix lock conflict -> tell client to wait and retry */
1251 if (wait) set_error( STATUS_PENDING );
1253 return 0;
1256 /* remove a lock on an fd */
1257 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1259 struct list *ptr;
1260 file_pos_t end = start + count;
1262 /* find an existing lock with the exact same parameters */
1263 LIST_FOR_EACH( ptr, &fd->locks )
1265 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1266 if ((lock->start == start) && (lock->end == end))
1268 remove_lock( lock, 1 );
1269 return;
1272 set_error( STATUS_FILE_LOCK_CONFLICT );
1276 /****************************************************************/
1277 /* file descriptor functions */
1279 static void fd_dump( struct object *obj, int verbose )
1281 struct fd *fd = (struct fd *)obj;
1282 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1283 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1284 fprintf( stderr, "\n" );
1287 static void fd_destroy( struct object *obj )
1289 struct fd *fd = (struct fd *)obj;
1291 free_async_queue( fd->read_q );
1292 free_async_queue( fd->write_q );
1293 free_async_queue( fd->wait_q );
1295 remove_fd_locks( fd );
1296 list_remove( &fd->inode_entry );
1297 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1298 if (fd->inode)
1300 inode_add_closed_fd( fd->inode, fd->closed );
1301 release_object( fd->inode );
1303 else /* no inode, close it right away */
1305 if (fd->unix_fd != -1) close( fd->unix_fd );
1309 /* set the events that select waits for on this fd */
1310 void set_fd_events( struct fd *fd, int events )
1312 int user = fd->poll_index;
1313 assert( poll_users[user] == fd );
1315 set_fd_epoll_events( fd, user, events );
1317 if (events == -1) /* stop waiting on this fd completely */
1319 pollfd[user].fd = -1;
1320 pollfd[user].events = POLLERR;
1321 pollfd[user].revents = 0;
1323 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1325 pollfd[user].fd = fd->unix_fd;
1326 pollfd[user].events = events;
1330 /* prepare an fd for unmounting its corresponding device */
1331 static inline void unmount_fd( struct fd *fd )
1333 assert( fd->inode );
1335 async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
1336 async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
1338 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1340 if (fd->unix_fd != -1) close( fd->unix_fd );
1342 fd->unix_fd = -1;
1343 fd->unmounted = 1;
1344 fd->closed->unix_fd = -1;
1345 fd->closed->unlink[0] = 0;
1347 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1348 fd->fs_locks = 0;
1351 /* allocate an fd object, without setting the unix fd yet */
1352 static struct fd *alloc_fd_object(void)
1354 struct fd *fd = alloc_object( &fd_ops );
1356 if (!fd) return NULL;
1358 fd->fd_ops = NULL;
1359 fd->user = NULL;
1360 fd->inode = NULL;
1361 fd->closed = NULL;
1362 fd->access = 0;
1363 fd->options = 0;
1364 fd->sharing = 0;
1365 fd->unix_fd = -1;
1366 fd->signaled = 1;
1367 fd->fs_locks = 1;
1368 fd->unmounted = 0;
1369 fd->poll_index = -1;
1370 fd->read_q = NULL;
1371 fd->write_q = NULL;
1372 fd->wait_q = NULL;
1373 list_init( &fd->inode_entry );
1374 list_init( &fd->locks );
1376 if ((fd->poll_index = add_poll_user( fd )) == -1)
1378 release_object( fd );
1379 return NULL;
1381 return fd;
1384 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1385 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user )
1387 struct fd *fd = alloc_object( &fd_ops );
1389 if (!fd) return NULL;
1391 fd->fd_ops = fd_user_ops;
1392 fd->user = user;
1393 fd->inode = NULL;
1394 fd->closed = NULL;
1395 fd->access = 0;
1396 fd->sharing = 0;
1397 fd->unix_fd = -1;
1398 fd->signaled = 0;
1399 fd->fs_locks = 0;
1400 fd->unmounted = 0;
1401 fd->poll_index = -1;
1402 fd->read_q = NULL;
1403 fd->write_q = NULL;
1404 fd->wait_q = NULL;
1405 list_init( &fd->inode_entry );
1406 list_init( &fd->locks );
1407 return fd;
1410 /* check if the desired access is possible without violating */
1411 /* the sharing mode of other opens of the same file */
1412 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1414 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1415 unsigned int existing_access = 0;
1416 struct list *ptr;
1418 /* if access mode is 0, sharing mode is ignored */
1419 if (!access) sharing = existing_sharing;
1420 fd->access = access;
1421 fd->sharing = sharing;
1423 LIST_FOR_EACH( ptr, &fd->inode->open )
1425 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1426 if (fd_ptr != fd)
1428 existing_sharing &= fd_ptr->sharing;
1429 existing_access |= fd_ptr->access;
1433 if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1434 if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1435 if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1436 if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0;
1437 if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0;
1438 if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0;
1439 return 1;
1442 /* sets the user of an fd that previously had no user */
1443 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1445 assert( fd->fd_ops == NULL );
1446 fd->fd_ops = user_ops;
1447 fd->user = user;
1450 /* open() wrapper that returns a struct fd with no fd user set */
1451 struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access,
1452 unsigned int sharing, unsigned int options )
1454 struct stat st;
1455 struct closed_fd *closed_fd;
1456 struct fd *fd;
1457 const char *unlink_name = "";
1458 int rw_mode;
1460 if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE))
1462 set_error( STATUS_INVALID_PARAMETER );
1463 return NULL;
1466 if (!(fd = alloc_fd_object())) return NULL;
1468 fd->options = options;
1469 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1470 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1472 release_object( fd );
1473 return NULL;
1476 /* create the directory if needed */
1477 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1479 if (mkdir( name, 0777 ) == -1)
1481 if (errno != EEXIST || (flags & O_EXCL))
1483 file_set_error();
1484 goto error;
1487 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1490 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1492 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1493 else rw_mode = O_WRONLY;
1495 else rw_mode = O_RDONLY;
1497 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1499 /* if we tried to open a directory for write access, retry read-only */
1500 if (errno != EISDIR ||
1501 !(access & FILE_UNIX_WRITE_ACCESS) ||
1502 (fd->unix_fd = open( name, O_RDONLY | (flags & ~O_TRUNC), *mode )) == -1)
1504 file_set_error();
1505 goto error;
1509 closed_fd->unix_fd = fd->unix_fd;
1510 closed_fd->unlink[0] = 0;
1511 fstat( fd->unix_fd, &st );
1512 *mode = st.st_mode;
1514 /* only bother with an inode for normal files and directories */
1515 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1517 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1519 if (!inode)
1521 /* we can close the fd because there are no others open on the same file,
1522 * otherwise we wouldn't have failed to allocate a new inode
1524 goto error;
1526 fd->inode = inode;
1527 fd->closed = closed_fd;
1528 list_add_head( &inode->open, &fd->inode_entry );
1530 /* check directory options */
1531 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1533 release_object( fd );
1534 set_error( STATUS_NOT_A_DIRECTORY );
1535 return NULL;
1537 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1539 release_object( fd );
1540 set_error( STATUS_FILE_IS_A_DIRECTORY );
1541 return NULL;
1543 if (!check_sharing( fd, access, sharing ))
1545 release_object( fd );
1546 set_error( STATUS_SHARING_VIOLATION );
1547 return NULL;
1549 strcpy( closed_fd->unlink, unlink_name );
1550 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1552 else /* special file */
1554 if (options & FILE_DIRECTORY_FILE)
1556 set_error( STATUS_NOT_A_DIRECTORY );
1557 goto error;
1559 if (unlink_name[0]) /* we can't unlink special files */
1561 set_error( STATUS_INVALID_PARAMETER );
1562 goto error;
1564 free( closed_fd );
1566 return fd;
1568 error:
1569 release_object( fd );
1570 free( closed_fd );
1571 return NULL;
1574 /* create an fd for an anonymous file */
1575 /* if the function fails the unix fd is closed */
1576 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
1577 unsigned int options )
1579 struct fd *fd = alloc_fd_object();
1581 if (fd)
1583 set_fd_user( fd, fd_user_ops, user );
1584 fd->unix_fd = unix_fd;
1585 fd->options = options;
1586 return fd;
1588 close( unix_fd );
1589 return NULL;
1592 /* retrieve the object that is using an fd */
1593 void *get_fd_user( struct fd *fd )
1595 return fd->user;
1598 /* retrieve the opening options for the fd */
1599 unsigned int get_fd_options( struct fd *fd )
1601 return fd->options;
1604 /* retrieve the unix fd for an object */
1605 int get_unix_fd( struct fd *fd )
1607 if (fd->unix_fd == -1)
1609 if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED );
1610 else set_error( STATUS_BAD_DEVICE_TYPE );
1612 return fd->unix_fd;
1615 /* check if two file descriptors point to the same file */
1616 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1618 return fd1->inode == fd2->inode;
1621 /* check if fd is on a removable device */
1622 int is_fd_removable( struct fd *fd )
1624 return (fd->inode && fd->inode->device->removable);
1627 /* set or clear the fd signaled state */
1628 void set_fd_signaled( struct fd *fd, int signaled )
1630 fd->signaled = signaled;
1631 if (signaled) wake_up( fd->user, 0 );
1634 /* handler for close_handle that refuses to close fd-associated handles in other processes */
1635 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1637 return (!current || current->process == process);
1640 /* callback for event happening in the main poll() loop */
1641 void fd_poll_event( struct fd *fd, int event )
1643 return fd->fd_ops->poll_event( fd, event );
1646 /* check if events are pending and if yes return which one(s) */
1647 int check_fd_events( struct fd *fd, int events )
1649 struct pollfd pfd;
1651 if (fd->unix_fd == -1) return POLLERR;
1652 if (fd->inode) return events; /* regular files are always signaled */
1654 pfd.fd = fd->unix_fd;
1655 pfd.events = events;
1656 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1657 return pfd.revents;
1660 /* default signaled() routine for objects that poll() on an fd */
1661 int default_fd_signaled( struct object *obj, struct thread *thread )
1663 struct fd *fd = get_obj_fd( obj );
1664 int ret = fd->signaled;
1665 release_object( fd );
1666 return ret;
1669 int default_fd_get_poll_events( struct fd *fd )
1671 int events = 0;
1673 if (async_waiting( fd->read_q )) events |= POLLIN;
1674 if (async_waiting( fd->write_q )) events |= POLLOUT;
1675 return events;
1678 /* default handler for poll() events */
1679 void default_poll_event( struct fd *fd, int event )
1681 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
1682 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
1684 /* if an error occurred, stop polling this fd to avoid busy-looping */
1685 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1686 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1689 struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
1691 struct async_queue *queue;
1692 struct async *async;
1694 switch (type)
1696 case ASYNC_TYPE_READ:
1697 if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
1698 queue = fd->read_q;
1699 break;
1700 case ASYNC_TYPE_WRITE:
1701 if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
1702 queue = fd->write_q;
1703 break;
1704 case ASYNC_TYPE_WAIT:
1705 if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
1706 queue = fd->wait_q;
1707 break;
1708 default:
1709 assert(0);
1712 if ((async = create_async( current, queue, data )))
1714 if (!fd->inode)
1715 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1716 else /* regular files are always ready for read and write */
1717 if (type != ASYNC_TYPE_WAIT) async_wake_up( queue, STATUS_ALERTED );
1719 return async;
1722 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
1724 switch (type)
1726 case ASYNC_TYPE_READ:
1727 async_wake_up( fd->read_q, status );
1728 break;
1729 case ASYNC_TYPE_WRITE:
1730 async_wake_up( fd->write_q, status );
1731 break;
1732 case ASYNC_TYPE_WAIT:
1733 async_wake_up( fd->wait_q, status );
1734 break;
1735 default:
1736 assert(0);
1740 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
1742 fd->fd_ops->reselect_async( fd, queue );
1745 void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
1747 struct async *async;
1749 if ((async = fd_queue_async( fd, data, type, count )))
1751 release_object( async );
1752 set_error( STATUS_PENDING );
1756 /* default reselect_async() fd routine */
1757 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
1759 if (queue != fd->wait_q)
1761 int poll_events = fd->fd_ops->get_poll_events( fd );
1762 int events = check_fd_events( fd, poll_events );
1763 if (events) fd->fd_ops->poll_event( fd, events );
1764 else set_fd_events( fd, poll_events );
1768 /* default cancel_async() fd routine */
1769 void default_fd_cancel_async( struct fd *fd )
1771 async_wake_up( fd->read_q, STATUS_CANCELLED );
1772 async_wake_up( fd->write_q, STATUS_CANCELLED );
1773 async_wake_up( fd->wait_q, STATUS_CANCELLED );
1776 /* default flush() routine */
1777 void no_flush( struct fd *fd, struct event **event )
1779 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1782 static inline int is_valid_mounted_device( struct stat *st )
1784 #if defined(linux) || defined(__sun__)
1785 return S_ISBLK( st->st_mode );
1786 #else
1787 /* disks are char devices on *BSD */
1788 return S_ISCHR( st->st_mode );
1789 #endif
1792 /* close all Unix file descriptors on a device to allow unmounting it */
1793 static void unmount_device( struct fd *device_fd )
1795 unsigned int i;
1796 struct stat st;
1797 struct device *device;
1798 struct inode *inode;
1799 struct fd *fd;
1800 int unix_fd = get_unix_fd( device_fd );
1802 if (unix_fd == -1) return;
1804 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
1806 set_error( STATUS_INVALID_PARAMETER );
1807 return;
1810 if (!(device = get_device( st.st_rdev, -1 ))) return;
1812 for (i = 0; i < INODE_HASH_SIZE; i++)
1814 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
1816 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
1818 unmount_fd( fd );
1820 inode_close_pending( inode, 0 );
1823 /* remove it from the hash table */
1824 list_remove( &device->entry );
1825 list_init( &device->entry );
1826 release_object( device );
1829 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1830 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1831 unsigned int access )
1833 struct fd *fd = NULL;
1834 struct object *obj;
1836 if ((obj = get_handle_obj( process, handle, access, NULL )))
1838 fd = get_obj_fd( obj );
1839 release_object( obj );
1841 return fd;
1844 /* flush a file buffers */
1845 DECL_HANDLER(flush_file)
1847 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1848 struct event * event = NULL;
1850 if (fd)
1852 fd->fd_ops->flush( fd, &event );
1853 if ( event )
1855 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1857 release_object( fd );
1861 /* open a file object */
1862 DECL_HANDLER(open_file_object)
1864 struct unicode_str name;
1865 struct directory *root = NULL;
1866 struct object *obj, *result;
1868 get_req_unicode_str( &name );
1869 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
1870 return;
1872 if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
1874 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
1876 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
1877 release_object( result );
1879 release_object( obj );
1882 if (root) release_object( root );
1885 /* get a Unix fd to access a file */
1886 DECL_HANDLER(get_handle_fd)
1888 struct fd *fd;
1890 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1892 reply->type = fd->fd_ops->get_fd_type( fd );
1893 if (reply->type != FD_TYPE_INVALID)
1895 int unix_fd = get_unix_fd( fd );
1896 if (unix_fd != -1)
1898 send_client_fd( current->process, unix_fd, req->handle );
1899 reply->removable = is_fd_removable(fd);
1900 reply->options = fd->options;
1901 reply->access = get_handle_access( current->process, req->handle );
1904 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
1905 release_object( fd );
1909 /* get ready to unmount a Unix device */
1910 DECL_HANDLER(unmount_device)
1912 struct fd *fd;
1914 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1916 unmount_device( fd );
1917 release_object( fd );
1921 /* create / reschedule an async I/O */
1922 DECL_HANDLER(register_async)
1924 unsigned int access;
1925 struct fd *fd;
1927 switch(req->type)
1929 case ASYNC_TYPE_READ:
1930 access = FILE_READ_DATA;
1931 break;
1932 case ASYNC_TYPE_WRITE:
1933 access = FILE_WRITE_DATA;
1934 break;
1935 default:
1936 set_error( STATUS_INVALID_PARAMETER );
1937 return;
1940 if ((fd = get_handle_fd_obj( current->process, req->handle, access )))
1942 fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
1943 release_object( fd );
1947 /* cancels all async I/O */
1948 DECL_HANDLER(cancel_async)
1950 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1951 if (fd)
1953 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1954 * NtCancelIoFile() will force the pending APC to be run. Since,
1955 * Windows only guarantees that the current thread will have no async
1956 * operation on the current fd when NtCancelIoFile returns, this shall
1957 * do the work.
1959 fd->fd_ops->cancel_async( fd );
1960 release_object( fd );