crypt32: Add support for CMSG_SIGNER_AUTH_ATTR_PARAM for a being decoded signed message.
[wine.git] / server / fd.c
blobedb59b0d54058a5f45a54d1e64fd6f7aa59a66c0
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 <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_POLL_H
39 #include <sys/poll.h>
40 #endif
41 #ifdef HAVE_LINUX_MAJOR_H
42 #include <linux/major.h>
43 #endif
44 #ifdef HAVE_SYS_STATVFS_H
45 #include <sys/statvfs.h>
46 #endif
47 #ifdef HAVE_SYS_VFS_H
48 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
49 #define list SYSLIST
50 #define list_next SYSLIST_NEXT
51 #define list_prev SYSLIST_PREV
52 #define list_head SYSLIST_HEAD
53 #define list_tail SYSLIST_TAIL
54 #define list_move_tail SYSLIST_MOVE_TAIL
55 #define list_remove SYSLIST_REMOVE
56 #include <sys/vfs.h>
57 #undef list
58 #undef list_next
59 #undef list_prev
60 #undef list_head
61 #undef list_tail
62 #undef list_move_tail
63 #undef list_remove
64 #endif
65 #ifdef HAVE_SYS_PARAM_H
66 #include <sys/param.h>
67 #endif
68 #ifdef HAVE_SYS_MOUNT_H
69 #include <sys/mount.h>
70 #endif
71 #ifdef HAVE_SYS_STATFS_H
72 #include <sys/statfs.h>
73 #endif
74 #ifdef HAVE_SYS_SYSCTL_H
75 #include <sys/sysctl.h>
76 #endif
77 #ifdef HAVE_SYS_EVENT_H
78 #include <sys/event.h>
79 #undef LIST_INIT
80 #undef LIST_ENTRY
81 #endif
82 #ifdef HAVE_STDINT_H
83 #include <stdint.h>
84 #endif
85 #include <sys/stat.h>
86 #include <sys/time.h>
87 #ifdef MAJOR_IN_MKDEV
88 #include <sys/mkdev.h>
89 #elif defined(MAJOR_IN_SYSMACROS)
90 #include <sys/sysmacros.h>
91 #endif
92 #include <sys/types.h>
93 #include <unistd.h>
94 #ifdef HAVE_SYS_SYSCALL_H
95 #include <sys/syscall.h>
96 #endif
98 #include "ntstatus.h"
99 #define WIN32_NO_STATUS
100 #include "object.h"
101 #include "file.h"
102 #include "handle.h"
103 #include "process.h"
104 #include "request.h"
106 #include "winternl.h"
107 #include "winioctl.h"
108 #include "ddk/wdm.h"
110 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
111 # include <sys/epoll.h>
112 # define USE_EPOLL
113 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
114 # define USE_EPOLL
115 # define EPOLLIN POLLIN
116 # define EPOLLOUT POLLOUT
117 # define EPOLLERR POLLERR
118 # define EPOLLHUP POLLHUP
119 # define EPOLL_CTL_ADD 1
120 # define EPOLL_CTL_DEL 2
121 # define EPOLL_CTL_MOD 3
123 typedef union epoll_data
125 void *ptr;
126 int fd;
127 uint32_t u32;
128 uint64_t u64;
129 } epoll_data_t;
131 struct epoll_event
133 uint32_t events;
134 epoll_data_t data;
137 static inline int epoll_create( int size )
139 return syscall( 254 /*NR_epoll_create*/, size );
142 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
144 return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
147 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
149 return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
152 #endif /* linux && __i386__ && HAVE_STDINT_H */
154 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
155 # include <port.h>
156 # define USE_EVENT_PORTS
157 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
159 /* Because of the stupid Posix locking semantics, we need to keep
160 * track of all file descriptors referencing a given file, and not
161 * close a single one until all the locks are gone (sigh).
164 /* file descriptor object */
166 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
167 struct closed_fd
169 struct list entry; /* entry in inode closed list */
170 int unix_fd; /* the unix file descriptor */
171 int unlink; /* whether to unlink on close: -1 - implicit FILE_DELETE_ON_CLOSE, 1 - explicit disposition */
172 char *unix_name; /* name to unlink on close, points to parent fd unix_name */
175 struct fd
177 struct object obj; /* object header */
178 const struct fd_ops *fd_ops; /* file descriptor operations */
179 struct inode *inode; /* inode that this fd belongs to */
180 struct list inode_entry; /* entry in inode fd list */
181 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
182 struct object *user; /* object using this file descriptor */
183 struct list locks; /* list of locks on this fd */
184 unsigned int access; /* file access (FILE_READ_DATA etc.) */
185 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
186 unsigned int sharing; /* file sharing mode */
187 char *unix_name; /* unix file name */
188 int unix_fd; /* unix file descriptor */
189 unsigned int no_fd_status;/* status to return when unix_fd is -1 */
190 unsigned int cacheable :1;/* can the fd be cached on the client side? */
191 unsigned int signaled :1; /* is the fd signaled? */
192 unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
193 int poll_index; /* index of fd in poll array */
194 struct async_queue read_q; /* async readers of this fd */
195 struct async_queue write_q; /* async writers of this fd */
196 struct async_queue wait_q; /* other async waiters of this fd */
197 struct completion *completion; /* completion object attached to this fd */
198 apc_param_t comp_key; /* completion key to set in completion events */
199 unsigned int comp_flags; /* completion flags */
202 static void fd_dump( struct object *obj, int verbose );
203 static void fd_destroy( struct object *obj );
205 static const struct object_ops fd_ops =
207 sizeof(struct fd), /* size */
208 fd_dump, /* dump */
209 no_get_type, /* get_type */
210 no_add_queue, /* add_queue */
211 NULL, /* remove_queue */
212 NULL, /* signaled */
213 NULL, /* satisfied */
214 no_signal, /* signal */
215 no_get_fd, /* get_fd */
216 no_map_access, /* map_access */
217 default_get_sd, /* get_sd */
218 default_set_sd, /* set_sd */
219 no_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 no_close_handle, /* close_handle */
226 fd_destroy /* destroy */
229 /* device object */
231 #define DEVICE_HASH_SIZE 7
232 #define INODE_HASH_SIZE 17
234 struct device
236 struct object obj; /* object header */
237 struct list entry; /* entry in device hash list */
238 dev_t dev; /* device number */
239 int removable; /* removable device? (or -1 if unknown) */
240 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
243 static void device_dump( struct object *obj, int verbose );
244 static void device_destroy( struct object *obj );
246 static const struct object_ops device_ops =
248 sizeof(struct device), /* size */
249 device_dump, /* dump */
250 no_get_type, /* get_type */
251 no_add_queue, /* add_queue */
252 NULL, /* remove_queue */
253 NULL, /* signaled */
254 NULL, /* satisfied */
255 no_signal, /* signal */
256 no_get_fd, /* get_fd */
257 no_map_access, /* map_access */
258 default_get_sd, /* get_sd */
259 default_set_sd, /* set_sd */
260 no_get_full_name, /* get_full_name */
261 no_lookup_name, /* lookup_name */
262 no_link_name, /* link_name */
263 NULL, /* unlink_name */
264 no_open_file, /* open_file */
265 no_kernel_obj_list, /* get_kernel_obj_list */
266 no_close_handle, /* close_handle */
267 device_destroy /* destroy */
270 /* inode object */
272 struct inode
274 struct object obj; /* object header */
275 struct list entry; /* inode hash list entry */
276 struct device *device; /* device containing this inode */
277 ino_t ino; /* inode number */
278 struct list open; /* list of open file descriptors */
279 struct list locks; /* list of file locks */
280 struct list closed; /* list of file descriptors to close at destroy time */
283 static void inode_dump( struct object *obj, int verbose );
284 static void inode_destroy( struct object *obj );
286 static const struct object_ops inode_ops =
288 sizeof(struct inode), /* size */
289 inode_dump, /* dump */
290 no_get_type, /* get_type */
291 no_add_queue, /* add_queue */
292 NULL, /* remove_queue */
293 NULL, /* signaled */
294 NULL, /* satisfied */
295 no_signal, /* signal */
296 no_get_fd, /* get_fd */
297 no_map_access, /* map_access */
298 default_get_sd, /* get_sd */
299 default_set_sd, /* set_sd */
300 no_get_full_name, /* get_full_name */
301 no_lookup_name, /* lookup_name */
302 no_link_name, /* link_name */
303 NULL, /* unlink_name */
304 no_open_file, /* open_file */
305 no_kernel_obj_list, /* get_kernel_obj_list */
306 no_close_handle, /* close_handle */
307 inode_destroy /* destroy */
310 /* file lock object */
312 struct file_lock
314 struct object obj; /* object header */
315 struct fd *fd; /* fd owning this lock */
316 struct list fd_entry; /* entry in list of locks on a given fd */
317 struct list inode_entry; /* entry in inode list of locks */
318 int shared; /* shared lock? */
319 file_pos_t start; /* locked region is interval [start;end) */
320 file_pos_t end;
321 struct process *process; /* process owning this lock */
322 struct list proc_entry; /* entry in list of locks owned by the process */
325 static void file_lock_dump( struct object *obj, int verbose );
326 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
328 static const struct object_ops file_lock_ops =
330 sizeof(struct file_lock), /* size */
331 file_lock_dump, /* dump */
332 no_get_type, /* get_type */
333 add_queue, /* add_queue */
334 remove_queue, /* remove_queue */
335 file_lock_signaled, /* signaled */
336 no_satisfied, /* satisfied */
337 no_signal, /* signal */
338 no_get_fd, /* get_fd */
339 no_map_access, /* map_access */
340 default_get_sd, /* get_sd */
341 default_set_sd, /* set_sd */
342 no_get_full_name, /* get_full_name */
343 no_lookup_name, /* lookup_name */
344 no_link_name, /* link_name */
345 NULL, /* unlink_name */
346 no_open_file, /* open_file */
347 no_kernel_obj_list, /* get_kernel_obj_list */
348 no_close_handle, /* close_handle */
349 no_destroy /* destroy */
353 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
354 #define FILE_POS_T_MAX (~(file_pos_t)0)
356 static file_pos_t max_unix_offset = OFF_T_MAX;
358 #define DUMP_LONG_LONG(val) do { \
359 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
360 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
361 else \
362 fprintf( stderr, "%lx", (unsigned long)(val) ); \
363 } while (0)
367 /****************************************************************/
368 /* timeouts support */
370 struct timeout_user
372 struct list entry; /* entry in sorted timeout list */
373 abstime_t when; /* timeout expiry */
374 timeout_callback callback; /* callback function */
375 void *private; /* callback private data */
378 static struct list abs_timeout_list = LIST_INIT(abs_timeout_list); /* sorted absolute timeouts list */
379 static struct list rel_timeout_list = LIST_INIT(rel_timeout_list); /* sorted relative timeouts list */
380 timeout_t current_time;
381 timeout_t monotonic_time;
383 struct _KUSER_SHARED_DATA *user_shared_data = NULL;
384 static const int user_shared_data_timeout = 16;
386 static void set_user_shared_data_time(void)
388 timeout_t tick_count = monotonic_time / 10000;
390 /* on X86 there should be total store order guarantees, so volatile is enough
391 * to ensure the stores aren't reordered by the compiler, and then they will
392 * always be seen in-order from other CPUs. On other archs, we need atomic
393 * intrinsics to guarantee that. */
394 #if defined(__i386__) || defined(__x86_64__)
395 user_shared_data->SystemTime.High2Time = current_time >> 32;
396 user_shared_data->SystemTime.LowPart = current_time;
397 user_shared_data->SystemTime.High1Time = current_time >> 32;
399 user_shared_data->InterruptTime.High2Time = monotonic_time >> 32;
400 user_shared_data->InterruptTime.LowPart = monotonic_time;
401 user_shared_data->InterruptTime.High1Time = monotonic_time >> 32;
403 user_shared_data->TickCount.High2Time = tick_count >> 32;
404 user_shared_data->TickCount.LowPart = tick_count;
405 user_shared_data->TickCount.High1Time = tick_count >> 32;
406 *(volatile ULONG *)&user_shared_data->TickCountLowDeprecated = tick_count;
407 #else
408 __atomic_store_n(&user_shared_data->SystemTime.High2Time, current_time >> 32, __ATOMIC_SEQ_CST);
409 __atomic_store_n(&user_shared_data->SystemTime.LowPart, current_time, __ATOMIC_SEQ_CST);
410 __atomic_store_n(&user_shared_data->SystemTime.High1Time, current_time >> 32, __ATOMIC_SEQ_CST);
412 __atomic_store_n(&user_shared_data->InterruptTime.High2Time, monotonic_time >> 32, __ATOMIC_SEQ_CST);
413 __atomic_store_n(&user_shared_data->InterruptTime.LowPart, monotonic_time, __ATOMIC_SEQ_CST);
414 __atomic_store_n(&user_shared_data->InterruptTime.High1Time, monotonic_time >> 32, __ATOMIC_SEQ_CST);
416 __atomic_store_n(&user_shared_data->TickCount.High2Time, tick_count >> 32, __ATOMIC_SEQ_CST);
417 __atomic_store_n(&user_shared_data->TickCount.LowPart, tick_count, __ATOMIC_SEQ_CST);
418 __atomic_store_n(&user_shared_data->TickCount.High1Time, tick_count >> 32, __ATOMIC_SEQ_CST);
419 __atomic_store_n(&user_shared_data->TickCountLowDeprecated, tick_count, __ATOMIC_SEQ_CST);
420 #endif
423 void set_current_time(void)
425 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
426 struct timeval now;
427 gettimeofday( &now, NULL );
428 current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
429 monotonic_time = monotonic_counter();
430 if (user_shared_data) set_user_shared_data_time();
433 /* add a timeout user */
434 struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
436 struct timeout_user *user;
437 struct list *ptr;
439 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
440 user->when = timeout_to_abstime( when );
441 user->callback = func;
442 user->private = private;
444 /* Now insert it in the linked list */
446 if (user->when > 0)
448 LIST_FOR_EACH( ptr, &abs_timeout_list )
450 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
451 if (timeout->when >= user->when) break;
454 else
456 LIST_FOR_EACH( ptr, &rel_timeout_list )
458 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
459 if (timeout->when <= user->when) break;
462 list_add_before( ptr, &user->entry );
463 return user;
466 /* remove a timeout user */
467 void remove_timeout_user( struct timeout_user *user )
469 list_remove( &user->entry );
470 free( user );
473 /* return a text description of a timeout for debugging purposes */
474 const char *get_timeout_str( timeout_t timeout )
476 static char buffer[64];
477 long secs, nsecs;
479 if (!timeout) return "0";
480 if (timeout == TIMEOUT_INFINITE) return "infinite";
482 if (timeout < 0) /* relative */
484 secs = -timeout / TICKS_PER_SEC;
485 nsecs = -timeout % TICKS_PER_SEC;
486 sprintf( buffer, "+%ld.%07ld", secs, nsecs );
488 else /* absolute */
490 secs = (timeout - current_time) / TICKS_PER_SEC;
491 nsecs = (timeout - current_time) % TICKS_PER_SEC;
492 if (nsecs < 0)
494 nsecs += TICKS_PER_SEC;
495 secs--;
497 if (secs >= 0)
498 sprintf( buffer, "%x%08x (+%ld.%07ld)",
499 (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
500 else
501 sprintf( buffer, "%x%08x (-%ld.%07ld)",
502 (unsigned int)(timeout >> 32), (unsigned int)timeout,
503 -(secs + 1), TICKS_PER_SEC - nsecs );
505 return buffer;
509 /****************************************************************/
510 /* poll support */
512 static struct fd **poll_users; /* users array */
513 static struct pollfd *pollfd; /* poll fd array */
514 static int nb_users; /* count of array entries actually in use */
515 static int active_users; /* current number of active users */
516 static int allocated_users; /* count of allocated entries in the array */
517 static struct fd **freelist; /* list of free entries in the array */
519 static int get_next_timeout(void);
521 static inline void fd_poll_event( struct fd *fd, int event )
523 fd->fd_ops->poll_event( fd, event );
526 #ifdef USE_EPOLL
528 static int epoll_fd = -1;
530 static inline void init_epoll(void)
532 epoll_fd = epoll_create( 128 );
535 /* set the events that epoll waits for on this fd; helper for set_fd_events */
536 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
538 struct epoll_event ev;
539 int ctl;
541 if (epoll_fd == -1) return;
543 if (events == -1) /* stop waiting on this fd completely */
545 if (pollfd[user].fd == -1) return; /* already removed */
546 ctl = EPOLL_CTL_DEL;
548 else if (pollfd[user].fd == -1)
550 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
551 ctl = EPOLL_CTL_ADD;
553 else
555 if (pollfd[user].events == events) return; /* nothing to do */
556 ctl = EPOLL_CTL_MOD;
559 ev.events = events;
560 memset(&ev.data, 0, sizeof(ev.data));
561 ev.data.u32 = user;
563 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
565 if (errno == ENOMEM) /* not enough memory, give up on epoll */
567 close( epoll_fd );
568 epoll_fd = -1;
570 else perror( "epoll_ctl" ); /* should not happen */
574 static inline void remove_epoll_user( struct fd *fd, int user )
576 if (epoll_fd == -1) return;
578 if (pollfd[user].fd != -1)
580 struct epoll_event dummy;
581 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
585 static inline void main_loop_epoll(void)
587 int i, ret, timeout;
588 struct epoll_event events[128];
590 assert( POLLIN == EPOLLIN );
591 assert( POLLOUT == EPOLLOUT );
592 assert( POLLERR == EPOLLERR );
593 assert( POLLHUP == EPOLLHUP );
595 if (epoll_fd == -1) return;
597 while (active_users)
599 timeout = get_next_timeout();
601 if (!active_users) break; /* last user removed by a timeout */
602 if (epoll_fd == -1) break; /* an error occurred with epoll */
604 ret = epoll_wait( epoll_fd, events, ARRAY_SIZE( events ), timeout );
605 set_current_time();
607 /* put the events into the pollfd array first, like poll does */
608 for (i = 0; i < ret; i++)
610 int user = events[i].data.u32;
611 pollfd[user].revents = events[i].events;
614 /* read events from the pollfd array, as set_fd_events may modify them */
615 for (i = 0; i < ret; i++)
617 int user = events[i].data.u32;
618 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
623 #elif defined(HAVE_KQUEUE)
625 static int kqueue_fd = -1;
627 static inline void init_epoll(void)
629 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
630 int mib[2];
631 char release[32];
632 size_t len = sizeof(release);
634 mib[0] = CTL_KERN;
635 mib[1] = KERN_OSRELEASE;
636 if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
637 if (atoi(release) < 9) return;
638 #endif
639 kqueue_fd = kqueue();
642 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
644 struct kevent ev[2];
646 if (kqueue_fd == -1) return;
648 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)(long)user );
649 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)(long)user );
651 if (events == -1) /* stop waiting on this fd completely */
653 if (pollfd[user].fd == -1) return; /* already removed */
654 ev[0].flags |= EV_DELETE;
655 ev[1].flags |= EV_DELETE;
657 else if (pollfd[user].fd == -1)
659 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
660 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
661 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
663 else
665 if (pollfd[user].events == events) return; /* nothing to do */
666 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
667 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
670 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
672 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
674 close( kqueue_fd );
675 kqueue_fd = -1;
677 else perror( "kevent" ); /* should not happen */
681 static inline void remove_epoll_user( struct fd *fd, int user )
683 if (kqueue_fd == -1) return;
685 if (pollfd[user].fd != -1)
687 struct kevent ev[2];
689 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
690 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
691 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
695 static inline void main_loop_epoll(void)
697 int i, ret, timeout;
698 struct kevent events[128];
700 if (kqueue_fd == -1) return;
702 while (active_users)
704 timeout = get_next_timeout();
706 if (!active_users) break; /* last user removed by a timeout */
707 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
709 if (timeout != -1)
711 struct timespec ts;
713 ts.tv_sec = timeout / 1000;
714 ts.tv_nsec = (timeout % 1000) * 1000000;
715 ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), &ts );
717 else ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), NULL );
719 set_current_time();
721 /* put the events into the pollfd array first, like poll does */
722 for (i = 0; i < ret; i++)
724 long user = (long)events[i].udata;
725 pollfd[user].revents = 0;
727 for (i = 0; i < ret; i++)
729 long user = (long)events[i].udata;
730 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
731 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
732 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
733 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
736 /* read events from the pollfd array, as set_fd_events may modify them */
737 for (i = 0; i < ret; i++)
739 long user = (long)events[i].udata;
740 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
741 pollfd[user].revents = 0;
746 #elif defined(USE_EVENT_PORTS)
748 static int port_fd = -1;
750 static inline void init_epoll(void)
752 port_fd = port_create();
755 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
757 int ret;
759 if (port_fd == -1) return;
761 if (events == -1) /* stop waiting on this fd completely */
763 if (pollfd[user].fd == -1) return; /* already removed */
764 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
766 else if (pollfd[user].fd == -1)
768 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
769 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
771 else
773 if (pollfd[user].events == events) return; /* nothing to do */
774 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
777 if (ret == -1)
779 if (errno == ENOMEM) /* not enough memory, give up on port_associate */
781 close( port_fd );
782 port_fd = -1;
784 else perror( "port_associate" ); /* should not happen */
788 static inline void remove_epoll_user( struct fd *fd, int user )
790 if (port_fd == -1) return;
792 if (pollfd[user].fd != -1)
794 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
798 static inline void main_loop_epoll(void)
800 int i, nget, ret, timeout;
801 port_event_t events[128];
803 if (port_fd == -1) return;
805 while (active_users)
807 timeout = get_next_timeout();
808 nget = 1;
810 if (!active_users) break; /* last user removed by a timeout */
811 if (port_fd == -1) break; /* an error occurred with event completion */
813 if (timeout != -1)
815 struct timespec ts;
817 ts.tv_sec = timeout / 1000;
818 ts.tv_nsec = (timeout % 1000) * 1000000;
819 ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, &ts );
821 else ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, NULL );
823 if (ret == -1) break; /* an error occurred with event completion */
825 set_current_time();
827 /* put the events into the pollfd array first, like poll does */
828 for (i = 0; i < nget; i++)
830 long user = (long)events[i].portev_user;
831 pollfd[user].revents = events[i].portev_events;
834 /* read events from the pollfd array, as set_fd_events may modify them */
835 for (i = 0; i < nget; i++)
837 long user = (long)events[i].portev_user;
838 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
839 /* if we are still interested, reassociate the fd */
840 if (pollfd[user].fd != -1) {
841 port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
847 #else /* HAVE_KQUEUE */
849 static inline void init_epoll(void) { }
850 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
851 static inline void remove_epoll_user( struct fd *fd, int user ) { }
852 static inline void main_loop_epoll(void) { }
854 #endif /* USE_EPOLL */
857 /* add a user in the poll array and return its index, or -1 on failure */
858 static int add_poll_user( struct fd *fd )
860 int ret;
861 if (freelist)
863 ret = freelist - poll_users;
864 freelist = (struct fd **)poll_users[ret];
866 else
868 if (nb_users == allocated_users)
870 struct fd **newusers;
871 struct pollfd *newpoll;
872 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
873 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
874 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
876 if (allocated_users)
877 poll_users = newusers;
878 else
879 free( newusers );
880 return -1;
882 poll_users = newusers;
883 pollfd = newpoll;
884 if (!allocated_users) init_epoll();
885 allocated_users = new_count;
887 ret = nb_users++;
889 pollfd[ret].fd = -1;
890 pollfd[ret].events = 0;
891 pollfd[ret].revents = 0;
892 poll_users[ret] = fd;
893 active_users++;
894 return ret;
897 /* remove a user from the poll list */
898 static void remove_poll_user( struct fd *fd, int user )
900 assert( user >= 0 );
901 assert( poll_users[user] == fd );
903 remove_epoll_user( fd, user );
904 pollfd[user].fd = -1;
905 pollfd[user].events = 0;
906 pollfd[user].revents = 0;
907 poll_users[user] = (struct fd *)freelist;
908 freelist = &poll_users[user];
909 active_users--;
912 /* process pending timeouts and return the time until the next timeout, in milliseconds */
913 static int get_next_timeout(void)
915 int ret = user_shared_data ? user_shared_data_timeout : -1;
917 if (!list_empty( &abs_timeout_list ) || !list_empty( &rel_timeout_list ))
919 struct list expired_list, *ptr;
921 /* first remove all expired timers from the list */
923 list_init( &expired_list );
924 while ((ptr = list_head( &abs_timeout_list )) != NULL)
926 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
928 if (timeout->when <= current_time)
930 list_remove( &timeout->entry );
931 list_add_tail( &expired_list, &timeout->entry );
933 else break;
935 while ((ptr = list_head( &rel_timeout_list )) != NULL)
937 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
939 if (-timeout->when <= monotonic_time)
941 list_remove( &timeout->entry );
942 list_add_tail( &expired_list, &timeout->entry );
944 else break;
947 /* now call the callback for all the removed timers */
949 while ((ptr = list_head( &expired_list )) != NULL)
951 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
952 list_remove( &timeout->entry );
953 timeout->callback( timeout->private );
954 free( timeout );
957 if ((ptr = list_head( &abs_timeout_list )) != NULL)
959 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
960 timeout_t diff = (timeout->when - current_time + 9999) / 10000;
961 if (diff > INT_MAX) diff = INT_MAX;
962 else if (diff < 0) diff = 0;
963 if (ret == -1 || diff < ret) ret = diff;
966 if ((ptr = list_head( &rel_timeout_list )) != NULL)
968 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
969 timeout_t diff = (-timeout->when - monotonic_time + 9999) / 10000;
970 if (diff > INT_MAX) diff = INT_MAX;
971 else if (diff < 0) diff = 0;
972 if (ret == -1 || diff < ret) ret = diff;
975 return ret;
978 /* server main poll() loop */
979 void main_loop(void)
981 int i, ret, timeout;
983 set_current_time();
984 server_start_time = current_time;
986 main_loop_epoll();
987 /* fall through to normal poll loop */
989 while (active_users)
991 timeout = get_next_timeout();
993 if (!active_users) break; /* last user removed by a timeout */
995 ret = poll( pollfd, nb_users, timeout );
996 set_current_time();
998 if (ret > 0)
1000 for (i = 0; i < nb_users; i++)
1002 if (pollfd[i].revents)
1004 fd_poll_event( poll_users[i], pollfd[i].revents );
1005 if (!--ret) break;
1013 /****************************************************************/
1014 /* device functions */
1016 static struct list device_hash[DEVICE_HASH_SIZE];
1018 static int is_device_removable( dev_t dev, int unix_fd )
1020 #if defined(linux) && defined(HAVE_FSTATFS)
1021 struct statfs stfs;
1023 /* check for floppy disk */
1024 if (major(dev) == FLOPPY_MAJOR) return 1;
1026 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
1027 return (stfs.f_type == 0x9660 || /* iso9660 */
1028 stfs.f_type == 0x9fa1 || /* supermount */
1029 stfs.f_type == 0x15013346); /* udf */
1030 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
1031 struct statfs stfs;
1033 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
1034 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
1035 #elif defined(__NetBSD__)
1036 struct statvfs stfs;
1038 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
1039 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
1040 #elif defined(sun)
1041 # include <sys/dkio.h>
1042 # include <sys/vtoc.h>
1043 struct dk_cinfo dkinf;
1044 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
1045 return (dkinf.dki_ctype == DKC_CDROM ||
1046 dkinf.dki_ctype == DKC_NCRFLOPPY ||
1047 dkinf.dki_ctype == DKC_SMSFLOPPY ||
1048 dkinf.dki_ctype == DKC_INTEL82072 ||
1049 dkinf.dki_ctype == DKC_INTEL82077);
1050 #else
1051 return 0;
1052 #endif
1055 /* retrieve the device object for a given fd, creating it if needed */
1056 static struct device *get_device( dev_t dev, int unix_fd )
1058 struct device *device;
1059 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
1061 if (device_hash[hash].next)
1063 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
1064 if (device->dev == dev) return (struct device *)grab_object( device );
1066 else list_init( &device_hash[hash] );
1068 /* not found, create it */
1070 if (unix_fd == -1) return NULL;
1071 if ((device = alloc_object( &device_ops )))
1073 device->dev = dev;
1074 device->removable = is_device_removable( dev, unix_fd );
1075 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
1076 list_add_head( &device_hash[hash], &device->entry );
1078 return device;
1081 static void device_dump( struct object *obj, int verbose )
1083 struct device *device = (struct device *)obj;
1084 fprintf( stderr, "Device dev=" );
1085 DUMP_LONG_LONG( device->dev );
1086 fprintf( stderr, "\n" );
1089 static void device_destroy( struct object *obj )
1091 struct device *device = (struct device *)obj;
1092 unsigned int i;
1094 for (i = 0; i < INODE_HASH_SIZE; i++)
1095 assert( list_empty(&device->inode_hash[i]) );
1097 list_remove( &device->entry ); /* remove it from the hash table */
1101 /****************************************************************/
1102 /* inode functions */
1104 /* close all pending file descriptors in the closed list */
1105 static void inode_close_pending( struct inode *inode, int keep_unlinks )
1107 struct list *ptr = list_head( &inode->closed );
1109 while (ptr)
1111 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1112 struct list *next = list_next( &inode->closed, ptr );
1114 if (fd->unix_fd != -1)
1116 close( fd->unix_fd );
1117 fd->unix_fd = -1;
1119 if (!keep_unlinks || !fd->unlink) /* get rid of it unless there's an unlink pending on that file */
1121 list_remove( ptr );
1122 free( fd->unix_name );
1123 free( fd );
1125 ptr = next;
1129 static void inode_dump( struct object *obj, int verbose )
1131 struct inode *inode = (struct inode *)obj;
1132 fprintf( stderr, "Inode device=%p ino=", inode->device );
1133 DUMP_LONG_LONG( inode->ino );
1134 fprintf( stderr, "\n" );
1137 static void inode_destroy( struct object *obj )
1139 struct inode *inode = (struct inode *)obj;
1140 struct list *ptr;
1142 assert( list_empty(&inode->open) );
1143 assert( list_empty(&inode->locks) );
1145 list_remove( &inode->entry );
1147 while ((ptr = list_head( &inode->closed )))
1149 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1150 list_remove( ptr );
1151 if (fd->unix_fd != -1) close( fd->unix_fd );
1152 if (fd->unlink)
1154 /* make sure it is still the same file */
1155 struct stat st;
1156 if (!stat( fd->unix_name, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
1158 if (S_ISDIR(st.st_mode)) rmdir( fd->unix_name );
1159 else unlink( fd->unix_name );
1162 free( fd->unix_name );
1163 free( fd );
1165 release_object( inode->device );
1168 /* retrieve the inode object for a given fd, creating it if needed */
1169 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
1171 struct device *device;
1172 struct inode *inode;
1173 unsigned int hash = ino % INODE_HASH_SIZE;
1175 if (!(device = get_device( dev, unix_fd ))) return NULL;
1177 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
1179 if (inode->ino == ino)
1181 release_object( device );
1182 return (struct inode *)grab_object( inode );
1186 /* not found, create it */
1187 if ((inode = alloc_object( &inode_ops )))
1189 inode->device = device;
1190 inode->ino = ino;
1191 list_init( &inode->open );
1192 list_init( &inode->locks );
1193 list_init( &inode->closed );
1194 list_add_head( &device->inode_hash[hash], &inode->entry );
1196 else release_object( device );
1198 return inode;
1201 /* add fd to the inode list of file descriptors to close */
1202 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
1204 if (!list_empty( &inode->locks ))
1206 list_add_head( &inode->closed, &fd->entry );
1208 else if (fd->unlink) /* close the fd but keep the structure around for unlink */
1210 if (fd->unix_fd != -1) close( fd->unix_fd );
1211 fd->unix_fd = -1;
1212 list_add_head( &inode->closed, &fd->entry );
1214 else /* no locks on this inode and no unlink, get rid of the fd */
1216 if (fd->unix_fd != -1) close( fd->unix_fd );
1217 free( fd->unix_name );
1218 free( fd );
1223 /****************************************************************/
1224 /* file lock functions */
1226 static void file_lock_dump( struct object *obj, int verbose )
1228 struct file_lock *lock = (struct file_lock *)obj;
1229 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
1230 lock->shared ? "shared" : "excl", lock->fd, lock->process );
1231 DUMP_LONG_LONG( lock->start );
1232 fprintf( stderr, " end=" );
1233 DUMP_LONG_LONG( lock->end );
1234 fprintf( stderr, "\n" );
1237 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
1239 struct file_lock *lock = (struct file_lock *)obj;
1240 /* lock is signaled if it has lost its owner */
1241 return !lock->process;
1244 /* set (or remove) a Unix lock if possible for the given range */
1245 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
1247 struct flock fl;
1249 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
1250 for (;;)
1252 if (start == end) return 1; /* can't set zero-byte lock */
1253 if (start > max_unix_offset) return 1; /* ignore it */
1254 fl.l_type = type;
1255 fl.l_whence = SEEK_SET;
1256 fl.l_start = start;
1257 if (!end || end > max_unix_offset) fl.l_len = 0;
1258 else fl.l_len = end - start;
1259 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
1261 switch(errno)
1263 case EACCES:
1264 /* check whether locks work at all on this file system */
1265 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
1267 set_error( STATUS_FILE_LOCK_CONFLICT );
1268 return 0;
1270 /* fall through */
1271 case EIO:
1272 case ENOLCK:
1273 case ENOTSUP:
1274 /* no locking on this fs, just ignore it */
1275 fd->fs_locks = 0;
1276 return 1;
1277 case EAGAIN:
1278 set_error( STATUS_FILE_LOCK_CONFLICT );
1279 return 0;
1280 case EBADF:
1281 /* this can happen if we try to set a write lock on a read-only file */
1282 /* try to at least grab a read lock */
1283 if (fl.l_type == F_WRLCK)
1285 type = F_RDLCK;
1286 break; /* retry */
1288 set_error( STATUS_ACCESS_DENIED );
1289 return 0;
1290 #ifdef EOVERFLOW
1291 case EOVERFLOW:
1292 #endif
1293 case EINVAL:
1294 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1295 /* in that case we shrink the limit and retry */
1296 if (max_unix_offset > INT_MAX)
1298 max_unix_offset = INT_MAX;
1299 break; /* retry */
1301 /* fall through */
1302 default:
1303 file_set_error();
1304 return 0;
1309 /* check if interval [start;end) overlaps the lock */
1310 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1312 if (lock->end && start >= lock->end) return 0;
1313 if (end && lock->start >= end) return 0;
1314 return 1;
1317 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1318 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1320 struct hole
1322 struct hole *next;
1323 struct hole *prev;
1324 file_pos_t start;
1325 file_pos_t end;
1326 } *first, *cur, *next, *buffer;
1328 struct list *ptr;
1329 int count = 0;
1331 if (!fd->inode) return;
1332 if (!fd->fs_locks) return;
1333 if (start == end || start > max_unix_offset) return;
1334 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1336 /* count the number of locks overlapping the specified area */
1338 LIST_FOR_EACH( ptr, &fd->inode->locks )
1340 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1341 if (lock->start == lock->end) continue;
1342 if (lock_overlaps( lock, start, end )) count++;
1345 if (!count) /* no locks at all, we can unlock everything */
1347 set_unix_lock( fd, start, end, F_UNLCK );
1348 return;
1351 /* allocate space for the list of holes */
1352 /* max. number of holes is number of locks + 1 */
1354 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1355 first = buffer;
1356 first->next = NULL;
1357 first->prev = NULL;
1358 first->start = start;
1359 first->end = end;
1360 next = first + 1;
1362 /* build a sorted list of unlocked holes in the specified area */
1364 LIST_FOR_EACH( ptr, &fd->inode->locks )
1366 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1367 if (lock->start == lock->end) continue;
1368 if (!lock_overlaps( lock, start, end )) continue;
1370 /* go through all the holes touched by this lock */
1371 for (cur = first; cur; cur = cur->next)
1373 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1374 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1376 /* now we know that lock is overlapping hole */
1378 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1380 cur->start = lock->end;
1381 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1382 /* now hole is empty, remove it */
1383 if (cur->next) cur->next->prev = cur->prev;
1384 if (cur->prev) cur->prev->next = cur->next;
1385 else if (!(first = cur->next)) goto done; /* no more holes at all */
1387 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1389 cur->end = lock->start;
1390 assert( cur->start < cur->end );
1392 else /* lock is in the middle of hole, split hole in two */
1394 next->prev = cur;
1395 next->next = cur->next;
1396 cur->next = next;
1397 next->start = lock->end;
1398 next->end = cur->end;
1399 cur->end = lock->start;
1400 assert( next->start < next->end );
1401 assert( cur->end < next->start );
1402 next++;
1403 break; /* done with this lock */
1408 /* clear Unix locks for all the holes */
1410 for (cur = first; cur; cur = cur->next)
1411 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1413 done:
1414 free( buffer );
1417 /* create a new lock on a fd */
1418 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1420 struct file_lock *lock;
1422 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1423 lock->shared = shared;
1424 lock->start = start;
1425 lock->end = end;
1426 lock->fd = fd;
1427 lock->process = current->process;
1429 /* now try to set a Unix lock */
1430 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1432 release_object( lock );
1433 return NULL;
1435 list_add_tail( &fd->locks, &lock->fd_entry );
1436 list_add_tail( &fd->inode->locks, &lock->inode_entry );
1437 list_add_tail( &lock->process->locks, &lock->proc_entry );
1438 return lock;
1441 /* remove an existing lock */
1442 static void remove_lock( struct file_lock *lock, int remove_unix )
1444 struct inode *inode = lock->fd->inode;
1446 list_remove( &lock->fd_entry );
1447 list_remove( &lock->inode_entry );
1448 list_remove( &lock->proc_entry );
1449 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1450 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1451 lock->process = NULL;
1452 wake_up( &lock->obj, 0 );
1453 release_object( lock );
1456 /* remove all locks owned by a given process */
1457 void remove_process_locks( struct process *process )
1459 struct list *ptr;
1461 while ((ptr = list_head( &process->locks )))
1463 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1464 remove_lock( lock, 1 ); /* this removes it from the list */
1468 /* remove all locks on a given fd */
1469 static void remove_fd_locks( struct fd *fd )
1471 file_pos_t start = FILE_POS_T_MAX, end = 0;
1472 struct list *ptr;
1474 while ((ptr = list_head( &fd->locks )))
1476 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1477 if (lock->start < start) start = lock->start;
1478 if (!lock->end || lock->end > end) end = lock->end - 1;
1479 remove_lock( lock, 0 );
1481 if (start < end) remove_unix_locks( fd, start, end + 1 );
1484 /* add a lock on an fd */
1485 /* returns handle to wait on */
1486 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1488 struct list *ptr;
1489 file_pos_t end = start + count;
1491 if (!fd->inode) /* not a regular file */
1493 set_error( STATUS_INVALID_DEVICE_REQUEST );
1494 return 0;
1497 /* don't allow wrapping locks */
1498 if (end && end < start)
1500 set_error( STATUS_INVALID_PARAMETER );
1501 return 0;
1504 /* check if another lock on that file overlaps the area */
1505 LIST_FOR_EACH( ptr, &fd->inode->locks )
1507 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1508 if (!lock_overlaps( lock, start, end )) continue;
1509 if (shared && (lock->shared || lock->fd == fd)) continue;
1510 /* found one */
1511 if (!wait)
1513 set_error( STATUS_FILE_LOCK_CONFLICT );
1514 return 0;
1516 set_error( STATUS_PENDING );
1517 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1520 /* not found, add it */
1521 if (add_lock( fd, shared, start, end )) return 0;
1522 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1524 /* Unix lock conflict -> tell client to wait and retry */
1525 if (wait) set_error( STATUS_PENDING );
1527 return 0;
1530 /* remove a lock on an fd */
1531 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1533 struct list *ptr;
1534 file_pos_t end = start + count;
1536 /* find an existing lock with the exact same parameters */
1537 LIST_FOR_EACH( ptr, &fd->locks )
1539 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1540 if ((lock->start == start) && (lock->end == end))
1542 remove_lock( lock, 1 );
1543 return;
1546 set_error( STATUS_FILE_LOCK_CONFLICT );
1550 /****************************************************************/
1551 /* file descriptor functions */
1553 static void fd_dump( struct object *obj, int verbose )
1555 struct fd *fd = (struct fd *)obj;
1556 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1557 if (fd->inode) fprintf( stderr, " inode=%p unlink=%d", fd->inode, fd->closed->unlink );
1558 fprintf( stderr, "\n" );
1561 static void fd_destroy( struct object *obj )
1563 struct fd *fd = (struct fd *)obj;
1565 free_async_queue( &fd->read_q );
1566 free_async_queue( &fd->write_q );
1567 free_async_queue( &fd->wait_q );
1569 if (fd->completion) release_object( fd->completion );
1570 remove_fd_locks( fd );
1571 list_remove( &fd->inode_entry );
1572 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1573 if (fd->inode)
1575 inode_add_closed_fd( fd->inode, fd->closed );
1576 release_object( fd->inode );
1578 else /* no inode, close it right away */
1580 if (fd->unix_fd != -1) close( fd->unix_fd );
1581 free( fd->unix_name );
1585 /* check if the desired access is possible without violating */
1586 /* the sharing mode of other opens of the same file */
1587 static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
1588 unsigned int open_flags, unsigned int options )
1590 /* only a few access bits are meaningful wrt sharing */
1591 const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
1592 const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
1593 const unsigned int all_access = read_access | write_access | DELETE;
1595 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1596 unsigned int existing_access = 0;
1597 struct list *ptr;
1599 fd->access = access;
1600 fd->sharing = sharing;
1602 LIST_FOR_EACH( ptr, &fd->inode->open )
1604 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1605 if (fd_ptr != fd)
1607 /* if access mode is 0, sharing mode is ignored */
1608 if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
1609 existing_access |= fd_ptr->access;
1613 if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
1614 ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
1615 ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
1616 return STATUS_SHARING_VIOLATION;
1617 if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
1618 ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
1619 return STATUS_SHARING_VIOLATION;
1620 if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
1621 return STATUS_CANNOT_DELETE;
1622 if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
1623 return STATUS_USER_MAPPED_FILE;
1624 if (!(access & all_access))
1625 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1626 if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
1627 ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
1628 ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
1629 return STATUS_SHARING_VIOLATION;
1630 return 0;
1633 /* set the events that select waits for on this fd */
1634 void set_fd_events( struct fd *fd, int events )
1636 int user = fd->poll_index;
1637 assert( poll_users[user] == fd );
1639 set_fd_epoll_events( fd, user, events );
1641 if (events == -1) /* stop waiting on this fd completely */
1643 pollfd[user].fd = -1;
1644 pollfd[user].events = POLLERR;
1645 pollfd[user].revents = 0;
1647 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1649 pollfd[user].fd = fd->unix_fd;
1650 pollfd[user].events = events;
1654 /* prepare an fd for unmounting its corresponding device */
1655 static inline void unmount_fd( struct fd *fd )
1657 assert( fd->inode );
1659 async_wake_up( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1660 async_wake_up( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1662 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1664 if (fd->unix_fd != -1) close( fd->unix_fd );
1666 fd->unix_fd = -1;
1667 fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
1668 fd->closed->unix_fd = -1;
1669 fd->closed->unlink = 0;
1671 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1672 fd->fs_locks = 0;
1675 /* allocate an fd object, without setting the unix fd yet */
1676 static struct fd *alloc_fd_object(void)
1678 struct fd *fd = alloc_object( &fd_ops );
1680 if (!fd) return NULL;
1682 fd->fd_ops = NULL;
1683 fd->user = NULL;
1684 fd->inode = NULL;
1685 fd->closed = NULL;
1686 fd->access = 0;
1687 fd->options = 0;
1688 fd->sharing = 0;
1689 fd->unix_fd = -1;
1690 fd->unix_name = NULL;
1691 fd->cacheable = 0;
1692 fd->signaled = 1;
1693 fd->fs_locks = 1;
1694 fd->poll_index = -1;
1695 fd->completion = NULL;
1696 fd->comp_flags = 0;
1697 init_async_queue( &fd->read_q );
1698 init_async_queue( &fd->write_q );
1699 init_async_queue( &fd->wait_q );
1700 list_init( &fd->inode_entry );
1701 list_init( &fd->locks );
1703 if ((fd->poll_index = add_poll_user( fd )) == -1)
1705 release_object( fd );
1706 return NULL;
1708 return fd;
1711 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1712 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
1714 struct fd *fd = alloc_object( &fd_ops );
1716 if (!fd) return NULL;
1718 fd->fd_ops = fd_user_ops;
1719 fd->user = user;
1720 fd->inode = NULL;
1721 fd->closed = NULL;
1722 fd->access = 0;
1723 fd->options = options;
1724 fd->sharing = 0;
1725 fd->unix_name = NULL;
1726 fd->unix_fd = -1;
1727 fd->cacheable = 0;
1728 fd->signaled = 0;
1729 fd->fs_locks = 0;
1730 fd->poll_index = -1;
1731 fd->completion = NULL;
1732 fd->comp_flags = 0;
1733 fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
1734 init_async_queue( &fd->read_q );
1735 init_async_queue( &fd->write_q );
1736 init_async_queue( &fd->wait_q );
1737 list_init( &fd->inode_entry );
1738 list_init( &fd->locks );
1739 return fd;
1742 /* duplicate an fd object for a different user */
1743 struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
1745 unsigned int err;
1746 struct fd *fd = alloc_fd_object();
1748 if (!fd) return NULL;
1750 fd->options = options;
1751 fd->cacheable = orig->cacheable;
1753 if (orig->unix_name)
1755 if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
1756 strcpy( fd->unix_name, orig->unix_name );
1759 if (orig->inode)
1761 struct closed_fd *closed = mem_alloc( sizeof(*closed) );
1762 if (!closed) goto failed;
1763 if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1765 file_set_error();
1766 free( closed );
1767 goto failed;
1769 closed->unix_fd = fd->unix_fd;
1770 closed->unlink = 0;
1771 closed->unix_name = fd->unix_name;
1772 fd->closed = closed;
1773 fd->inode = (struct inode *)grab_object( orig->inode );
1774 list_add_head( &fd->inode->open, &fd->inode_entry );
1775 if ((err = check_sharing( fd, access, sharing, 0, options )))
1777 set_error( err );
1778 goto failed;
1781 else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1783 file_set_error();
1784 goto failed;
1786 return fd;
1788 failed:
1789 release_object( fd );
1790 return NULL;
1793 /* find an existing fd object that can be reused for a mapping */
1794 struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
1796 struct fd *fd_ptr;
1798 if (!fd->inode) return NULL;
1800 LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
1801 if (fd_ptr->access == access && fd_ptr->sharing == sharing)
1802 return (struct fd *)grab_object( fd_ptr );
1804 return NULL;
1807 /* sets the user of an fd that previously had no user */
1808 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1810 assert( fd->fd_ops == NULL );
1811 fd->fd_ops = user_ops;
1812 fd->user = user;
1815 char *dup_fd_name( struct fd *root, const char *name )
1817 char *ret;
1819 if (!root) return strdup( name );
1820 if (!root->unix_name) return NULL;
1822 /* skip . prefix */
1823 if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
1825 if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
1827 strcpy( ret, root->unix_name );
1828 if (name[0] && name[0] != '/') strcat( ret, "/" );
1829 strcat( ret, name );
1831 return ret;
1834 /* open() wrapper that returns a struct fd with no fd user set */
1835 struct fd *open_fd( struct fd *root, const char *name, int flags, mode_t *mode, unsigned int access,
1836 unsigned int sharing, unsigned int options )
1838 struct stat st;
1839 struct closed_fd *closed_fd;
1840 struct fd *fd;
1841 int root_fd = -1;
1842 int rw_mode;
1843 char *path;
1845 if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
1846 ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
1848 set_error( STATUS_INVALID_PARAMETER );
1849 return NULL;
1852 if (!(fd = alloc_fd_object())) return NULL;
1854 fd->options = options;
1855 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
1857 release_object( fd );
1858 return NULL;
1861 if (root)
1863 if ((root_fd = get_unix_fd( root )) == -1) goto error;
1864 if (fchdir( root_fd ) == -1)
1866 file_set_error();
1867 root_fd = -1;
1868 goto error;
1872 /* create the directory if needed */
1873 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1875 if (mkdir( name, *mode ) == -1)
1877 if (errno != EEXIST || (flags & O_EXCL))
1879 file_set_error();
1880 goto error;
1883 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1886 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1888 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1889 else rw_mode = O_WRONLY;
1891 else rw_mode = O_RDONLY;
1893 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1895 /* if we tried to open a directory for write access, retry read-only */
1896 if (errno == EISDIR)
1898 if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
1899 fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
1902 if (fd->unix_fd == -1)
1904 file_set_error();
1905 goto error;
1909 fd->unix_name = NULL;
1910 if ((path = dup_fd_name( root, name )))
1912 fd->unix_name = realpath( path, NULL );
1913 free( path );
1916 closed_fd->unix_fd = fd->unix_fd;
1917 closed_fd->unlink = 0;
1918 closed_fd->unix_name = fd->unix_name;
1919 fstat( fd->unix_fd, &st );
1920 *mode = st.st_mode;
1922 /* only bother with an inode for normal files and directories */
1923 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1925 unsigned int err;
1926 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1928 if (!inode)
1930 /* we can close the fd because there are no others open on the same file,
1931 * otherwise we wouldn't have failed to allocate a new inode
1933 goto error;
1935 fd->inode = inode;
1936 fd->closed = closed_fd;
1937 fd->cacheable = !inode->device->removable;
1938 list_add_head( &inode->open, &fd->inode_entry );
1939 closed_fd = NULL;
1941 /* check directory options */
1942 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1944 set_error( STATUS_NOT_A_DIRECTORY );
1945 goto error;
1947 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1949 set_error( STATUS_FILE_IS_A_DIRECTORY );
1950 goto error;
1952 if ((err = check_sharing( fd, access, sharing, flags, options )))
1954 set_error( err );
1955 goto error;
1958 /* can't unlink files if we don't have permission to access */
1959 if ((options & FILE_DELETE_ON_CLOSE) && !(flags & O_CREAT) &&
1960 !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1962 set_error( STATUS_CANNOT_DELETE );
1963 goto error;
1966 fd->closed->unlink = (options & FILE_DELETE_ON_CLOSE) ? -1 : 0;
1967 if (flags & O_TRUNC)
1969 if (S_ISDIR(st.st_mode))
1971 set_error( STATUS_OBJECT_NAME_COLLISION );
1972 goto error;
1974 ftruncate( fd->unix_fd, 0 );
1977 else /* special file */
1979 if (options & FILE_DELETE_ON_CLOSE) /* we can't unlink special files */
1981 set_error( STATUS_INVALID_PARAMETER );
1982 goto error;
1984 free( closed_fd );
1985 fd->cacheable = 1;
1987 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1988 return fd;
1990 error:
1991 release_object( fd );
1992 free( closed_fd );
1993 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1994 return NULL;
1997 /* create an fd for an anonymous file */
1998 /* if the function fails the unix fd is closed */
1999 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
2000 unsigned int options )
2002 struct fd *fd = alloc_fd_object();
2004 if (fd)
2006 set_fd_user( fd, fd_user_ops, user );
2007 fd->unix_fd = unix_fd;
2008 fd->options = options;
2009 return fd;
2011 close( unix_fd );
2012 return NULL;
2015 /* retrieve the object that is using an fd */
2016 void *get_fd_user( struct fd *fd )
2018 return fd->user;
2021 /* retrieve the opening options for the fd */
2022 unsigned int get_fd_options( struct fd *fd )
2024 return fd->options;
2027 /* check if fd is in overlapped mode */
2028 int is_fd_overlapped( struct fd *fd )
2030 return !(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
2033 /* retrieve the unix fd for an object */
2034 int get_unix_fd( struct fd *fd )
2036 if (fd->unix_fd == -1) set_error( fd->no_fd_status );
2037 return fd->unix_fd;
2040 /* check if two file descriptors point to the same file */
2041 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
2043 return fd1->inode == fd2->inode;
2046 /* allow the fd to be cached (can't be reset once set) */
2047 void allow_fd_caching( struct fd *fd )
2049 fd->cacheable = 1;
2052 /* check if fd is on a removable device */
2053 int is_fd_removable( struct fd *fd )
2055 return (fd->inode && fd->inode->device->removable);
2058 /* set or clear the fd signaled state */
2059 void set_fd_signaled( struct fd *fd, int signaled )
2061 if (fd->comp_flags & FILE_SKIP_SET_EVENT_ON_HANDLE) return;
2062 fd->signaled = signaled;
2063 if (signaled) wake_up( fd->user, 0 );
2066 /* handler for close_handle that refuses to close fd-associated handles in other processes */
2067 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
2069 return (!current || current->process == process);
2072 /* check if events are pending and if yes return which one(s) */
2073 int check_fd_events( struct fd *fd, int events )
2075 struct pollfd pfd;
2077 if (fd->unix_fd == -1) return POLLERR;
2078 if (fd->inode) return events; /* regular files are always signaled */
2080 pfd.fd = fd->unix_fd;
2081 pfd.events = events;
2082 if (poll( &pfd, 1, 0 ) <= 0) return 0;
2083 return pfd.revents;
2086 /* default signaled() routine for objects that poll() on an fd */
2087 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
2089 struct fd *fd = get_obj_fd( obj );
2090 int ret = fd->signaled;
2091 release_object( fd );
2092 return ret;
2095 /* default map_access() routine for objects that behave like an fd */
2096 unsigned int default_fd_map_access( struct object *obj, unsigned int access )
2098 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2099 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2100 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2101 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2102 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2105 int default_fd_get_poll_events( struct fd *fd )
2107 int events = 0;
2109 if (async_waiting( &fd->read_q )) events |= POLLIN;
2110 if (async_waiting( &fd->write_q )) events |= POLLOUT;
2111 return events;
2114 /* default handler for poll() events */
2115 void default_poll_event( struct fd *fd, int event )
2117 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( &fd->read_q, STATUS_ALERTED );
2118 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( &fd->write_q, STATUS_ALERTED );
2120 /* if an error occurred, stop polling this fd to avoid busy-looping */
2121 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2122 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2125 void fd_queue_async( struct fd *fd, struct async *async, int type )
2127 struct async_queue *queue;
2129 switch (type)
2131 case ASYNC_TYPE_READ:
2132 queue = &fd->read_q;
2133 break;
2134 case ASYNC_TYPE_WRITE:
2135 queue = &fd->write_q;
2136 break;
2137 case ASYNC_TYPE_WAIT:
2138 queue = &fd->wait_q;
2139 break;
2140 default:
2141 queue = NULL;
2142 assert(0);
2145 queue_async( queue, async );
2147 if (type != ASYNC_TYPE_WAIT)
2149 if (!fd->inode)
2150 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2151 else /* regular files are always ready for read and write */
2152 async_wake_up( queue, STATUS_ALERTED );
2156 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2158 switch (type)
2160 case ASYNC_TYPE_READ:
2161 async_wake_up( &fd->read_q, status );
2162 break;
2163 case ASYNC_TYPE_WRITE:
2164 async_wake_up( &fd->write_q, status );
2165 break;
2166 case ASYNC_TYPE_WAIT:
2167 async_wake_up( &fd->wait_q, status );
2168 break;
2169 default:
2170 assert(0);
2174 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2176 fd->fd_ops->reselect_async( fd, queue );
2179 void no_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2181 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2184 void default_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2186 fd_queue_async( fd, async, type );
2187 set_error( STATUS_PENDING );
2190 /* default reselect_async() fd routine */
2191 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2193 if (queue == &fd->read_q || queue == &fd->write_q)
2195 int poll_events = fd->fd_ops->get_poll_events( fd );
2196 int events = check_fd_events( fd, poll_events );
2197 if (events) fd->fd_ops->poll_event( fd, events );
2198 else set_fd_events( fd, poll_events );
2202 static inline int is_valid_mounted_device( struct stat *st )
2204 #if defined(linux) || defined(__sun__)
2205 return S_ISBLK( st->st_mode );
2206 #else
2207 /* disks are char devices on *BSD */
2208 return S_ISCHR( st->st_mode );
2209 #endif
2212 /* close all Unix file descriptors on a device to allow unmounting it */
2213 static void unmount_device( struct fd *device_fd )
2215 unsigned int i;
2216 struct stat st;
2217 struct device *device;
2218 struct inode *inode;
2219 struct fd *fd;
2220 int unix_fd = get_unix_fd( device_fd );
2222 if (unix_fd == -1) return;
2224 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2226 set_error( STATUS_INVALID_PARAMETER );
2227 return;
2230 if (!(device = get_device( st.st_rdev, -1 ))) return;
2232 for (i = 0; i < INODE_HASH_SIZE; i++)
2234 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2236 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2238 unmount_fd( fd );
2240 inode_close_pending( inode, 0 );
2243 /* remove it from the hash table */
2244 list_remove( &device->entry );
2245 list_init( &device->entry );
2246 release_object( device );
2249 /* default read() routine */
2250 int no_fd_read( struct fd *fd, struct async *async, file_pos_t pos )
2252 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2253 return 0;
2256 /* default write() routine */
2257 int no_fd_write( struct fd *fd, struct async *async, file_pos_t pos )
2259 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2260 return 0;
2263 /* default flush() routine */
2264 int no_fd_flush( struct fd *fd, struct async *async )
2266 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2267 return 0;
2270 /* default get_file_info() routine */
2271 void no_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2273 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2276 /* default get_file_info() routine */
2277 void default_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2279 switch (info_class)
2281 case FileAccessInformation:
2283 FILE_ACCESS_INFORMATION info;
2284 if (get_reply_max_size() < sizeof(info))
2286 set_error( STATUS_INFO_LENGTH_MISMATCH );
2287 return;
2289 info.AccessFlags = get_handle_access( current->process, handle );
2290 set_reply_data( &info, sizeof(info) );
2291 break;
2293 case FileModeInformation:
2295 FILE_MODE_INFORMATION info;
2296 if (get_reply_max_size() < sizeof(info))
2298 set_error( STATUS_INFO_LENGTH_MISMATCH );
2299 return;
2301 info.Mode = fd->options & ( FILE_WRITE_THROUGH
2302 | FILE_SEQUENTIAL_ONLY
2303 | FILE_NO_INTERMEDIATE_BUFFERING
2304 | FILE_SYNCHRONOUS_IO_ALERT
2305 | FILE_SYNCHRONOUS_IO_NONALERT );
2306 set_reply_data( &info, sizeof(info) );
2307 break;
2309 case FileIoCompletionNotificationInformation:
2311 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info;
2312 if (get_reply_max_size() < sizeof(info))
2314 set_error( STATUS_INFO_LENGTH_MISMATCH );
2315 return;
2317 info.Flags = fd->comp_flags;
2318 set_reply_data( &info, sizeof(info) );
2319 break;
2321 default:
2322 set_error( STATUS_NOT_IMPLEMENTED );
2326 /* default get_volume_info() routine */
2327 void no_fd_get_volume_info( struct fd *fd, unsigned int info_class )
2329 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2332 /* default ioctl() routine */
2333 int no_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2335 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2336 return 0;
2339 /* default ioctl() routine */
2340 int default_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2342 switch(code)
2344 case FSCTL_DISMOUNT_VOLUME:
2345 unmount_device( fd );
2346 return 1;
2347 default:
2348 set_error( STATUS_NOT_SUPPORTED );
2349 return 0;
2353 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2354 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2355 unsigned int access )
2357 struct fd *fd = NULL;
2358 struct object *obj;
2360 if ((obj = get_handle_obj( process, handle, access, NULL )))
2362 fd = get_obj_fd( obj );
2363 release_object( obj );
2365 return fd;
2368 static int is_dir_empty( int fd )
2370 DIR *dir;
2371 int empty;
2372 struct dirent *de;
2374 if ((fd = dup( fd )) == -1)
2375 return -1;
2377 if (!(dir = fdopendir( fd )))
2379 close( fd );
2380 return -1;
2383 empty = 1;
2384 while (empty && (de = readdir( dir )))
2386 if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." )) continue;
2387 empty = 0;
2389 closedir( dir );
2390 return empty;
2393 /* set disposition for the fd */
2394 static void set_fd_disposition( struct fd *fd, int unlink )
2396 struct stat st;
2398 if (!fd->inode)
2400 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2401 return;
2404 if (fd->unix_fd == -1)
2406 set_error( fd->no_fd_status );
2407 return;
2410 if (unlink)
2412 if (fstat( fd->unix_fd, &st ) == -1)
2414 file_set_error();
2415 return;
2417 if (S_ISREG( st.st_mode )) /* can't unlink files we don't have permission to write */
2419 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2421 set_error( STATUS_CANNOT_DELETE );
2422 return;
2425 else if (S_ISDIR( st.st_mode )) /* can't remove non-empty directories */
2427 switch (is_dir_empty( fd->unix_fd ))
2429 case -1:
2430 file_set_error();
2431 return;
2432 case 0:
2433 set_error( STATUS_DIRECTORY_NOT_EMPTY );
2434 return;
2437 else /* can't unlink special files */
2439 set_error( STATUS_INVALID_PARAMETER );
2440 return;
2444 fd->closed->unlink = unlink ? 1 : 0;
2445 if (fd->options & FILE_DELETE_ON_CLOSE)
2446 fd->closed->unlink = -1;
2449 /* set new name for the fd */
2450 static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr,
2451 data_size_t len, int create_link, int replace )
2453 struct inode *inode;
2454 struct stat st, st2;
2455 char *name;
2457 if (!fd->inode || !fd->unix_name)
2459 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2460 return;
2462 if (fd->unix_fd == -1)
2464 set_error( fd->no_fd_status );
2465 return;
2468 if (!len || ((nameptr[0] == '/') ^ !root))
2470 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
2471 return;
2473 if (!(name = mem_alloc( len + 1 ))) return;
2474 memcpy( name, nameptr, len );
2475 name[len] = 0;
2477 if (root)
2479 char *combined_name = dup_fd_name( root, name );
2480 if (!combined_name)
2482 set_error( STATUS_NO_MEMORY );
2483 goto failed;
2485 free( name );
2486 name = combined_name;
2489 /* when creating a hard link, source cannot be a dir */
2490 if (create_link && !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
2492 set_error( STATUS_FILE_IS_A_DIRECTORY );
2493 goto failed;
2496 if (!stat( name, &st ))
2498 if (!fstat( fd->unix_fd, &st2 ) && st.st_ino == st2.st_ino && st.st_dev == st2.st_dev)
2500 if (create_link && !replace) set_error( STATUS_OBJECT_NAME_COLLISION );
2501 free( name );
2502 return;
2505 if (!replace)
2507 set_error( STATUS_OBJECT_NAME_COLLISION );
2508 goto failed;
2511 /* can't replace directories or special files */
2512 if (!S_ISREG( st.st_mode ))
2514 set_error( STATUS_ACCESS_DENIED );
2515 goto failed;
2518 /* can't replace an opened file */
2519 if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
2521 int is_empty = list_empty( &inode->open );
2522 release_object( inode );
2523 if (!is_empty)
2525 set_error( STATUS_ACCESS_DENIED );
2526 goto failed;
2530 /* link() expects that the target doesn't exist */
2531 /* rename() cannot replace files with directories */
2532 if (create_link || S_ISDIR( st2.st_mode ))
2534 if (unlink( name ))
2536 file_set_error();
2537 goto failed;
2542 if (create_link)
2544 if (link( fd->unix_name, name ))
2545 file_set_error();
2546 free( name );
2547 return;
2550 if (rename( fd->unix_name, name ))
2552 file_set_error();
2553 goto failed;
2556 if (is_file_executable( fd->unix_name ) != is_file_executable( name ) && !fstat( fd->unix_fd, &st ))
2558 if (is_file_executable( name ))
2559 /* set executable bit where read bit is set */
2560 st.st_mode |= (st.st_mode & 0444) >> 2;
2561 else
2562 st.st_mode &= ~0111;
2563 fchmod( fd->unix_fd, st.st_mode );
2566 free( fd->unix_name );
2567 fd->closed->unix_name = fd->unix_name = realpath( name, NULL );
2568 free( name );
2569 if (!fd->unix_name)
2570 set_error( STATUS_NO_MEMORY );
2571 return;
2573 failed:
2574 free( name );
2577 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2579 *p_key = fd->comp_key;
2580 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2583 void fd_copy_completion( struct fd *src, struct fd *dst )
2585 assert( !dst->completion );
2586 dst->completion = fd_get_completion( src, &dst->comp_key );
2587 dst->comp_flags = src->comp_flags;
2590 /* flush a file buffers */
2591 DECL_HANDLER(flush)
2593 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
2594 struct async *async;
2596 if (!fd) return;
2598 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2600 reply->event = async_handoff( async, fd->fd_ops->flush( fd, async ), NULL, 1 );
2601 release_object( async );
2603 release_object( fd );
2606 /* query file info */
2607 DECL_HANDLER(get_file_info)
2609 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2611 if (fd)
2613 fd->fd_ops->get_file_info( fd, req->handle, req->info_class );
2614 release_object( fd );
2618 /* query volume info */
2619 DECL_HANDLER(get_volume_info)
2621 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2623 if (fd)
2625 fd->fd_ops->get_volume_info( fd, req->info_class );
2626 release_object( fd );
2630 /* open a file object */
2631 DECL_HANDLER(open_file_object)
2633 struct unicode_str name = get_req_unicode_str();
2634 struct object *obj, *result, *root = NULL;
2636 if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
2638 obj = open_named_object( root, NULL, &name, req->attributes );
2639 if (root) release_object( root );
2640 if (!obj) return;
2642 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2644 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2645 release_object( result );
2647 release_object( obj );
2650 /* get the Unix name from a file handle */
2651 DECL_HANDLER(get_handle_unix_name)
2653 struct fd *fd;
2655 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2657 if (fd->unix_name)
2659 data_size_t name_len = strlen( fd->unix_name );
2660 reply->name_len = name_len;
2661 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2662 else set_error( STATUS_BUFFER_OVERFLOW );
2664 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2665 release_object( fd );
2669 /* get a Unix fd to access a file */
2670 DECL_HANDLER(get_handle_fd)
2672 struct fd *fd;
2674 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2676 int unix_fd = get_unix_fd( fd );
2677 reply->cacheable = fd->cacheable;
2678 if (unix_fd != -1)
2680 reply->type = fd->fd_ops->get_fd_type( fd );
2681 reply->options = fd->options;
2682 reply->access = get_handle_access( current->process, req->handle );
2683 send_client_fd( current->process, unix_fd, req->handle );
2685 release_object( fd );
2689 /* perform a read on a file object */
2690 DECL_HANDLER(read)
2692 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
2693 struct async *async;
2695 if (!fd) return;
2697 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2699 reply->wait = async_handoff( async, fd->fd_ops->read( fd, async, req->pos ), NULL, 0 );
2700 reply->options = fd->options;
2701 release_object( async );
2703 release_object( fd );
2706 /* perform a write on a file object */
2707 DECL_HANDLER(write)
2709 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
2710 struct async *async;
2712 if (!fd) return;
2714 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2716 reply->wait = async_handoff( async, fd->fd_ops->write( fd, async, req->pos ), &reply->size, 0 );
2717 reply->options = fd->options;
2718 release_object( async );
2720 release_object( fd );
2723 /* perform an ioctl on a file */
2724 DECL_HANDLER(ioctl)
2726 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2727 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2728 struct async *async;
2730 if (!fd) return;
2732 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2734 reply->wait = async_handoff( async, fd->fd_ops->ioctl( fd, req->code, async ), NULL, 0 );
2735 reply->options = fd->options;
2736 release_object( async );
2738 release_object( fd );
2741 /* create / reschedule an async I/O */
2742 DECL_HANDLER(register_async)
2744 unsigned int access;
2745 struct async *async;
2746 struct fd *fd;
2748 switch(req->type)
2750 case ASYNC_TYPE_READ:
2751 access = FILE_READ_DATA;
2752 break;
2753 case ASYNC_TYPE_WRITE:
2754 access = FILE_WRITE_DATA;
2755 break;
2756 default:
2757 set_error( STATUS_INVALID_PARAMETER );
2758 return;
2761 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2763 if (get_unix_fd( fd ) != -1 && (async = create_async( fd, current, &req->async, NULL )))
2765 fd->fd_ops->queue_async( fd, async, req->type, req->count );
2766 release_object( async );
2768 release_object( fd );
2772 /* attach completion object to a fd */
2773 DECL_HANDLER(set_completion_info)
2775 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2777 if (fd)
2779 if (is_fd_overlapped( fd ) && !fd->completion)
2781 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2782 fd->comp_key = req->ckey;
2784 else set_error( STATUS_INVALID_PARAMETER );
2785 release_object( fd );
2789 /* push new completion msg into a completion queue attached to the fd */
2790 DECL_HANDLER(add_fd_completion)
2792 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2793 if (fd)
2795 if (fd->completion && (req->async || !(fd->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
2796 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2797 release_object( fd );
2801 /* set fd completion information */
2802 DECL_HANDLER(set_fd_completion_mode)
2804 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2805 if (fd)
2807 if (is_fd_overlapped( fd ))
2809 if (req->flags & FILE_SKIP_SET_EVENT_ON_HANDLE)
2810 set_fd_signaled( fd, 0 );
2811 /* removing flags is not allowed */
2812 fd->comp_flags |= req->flags & ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
2813 | FILE_SKIP_SET_EVENT_ON_HANDLE
2814 | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO );
2816 else
2817 set_error( STATUS_INVALID_PARAMETER );
2818 release_object( fd );
2822 /* set fd disposition information */
2823 DECL_HANDLER(set_fd_disp_info)
2825 struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
2826 if (fd)
2828 set_fd_disposition( fd, req->unlink );
2829 release_object( fd );
2833 /* set fd name information */
2834 DECL_HANDLER(set_fd_name_info)
2836 struct fd *fd, *root_fd = NULL;
2838 if (req->rootdir)
2840 struct dir *root;
2842 if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
2843 root_fd = get_obj_fd( (struct object *)root );
2844 release_object( root );
2845 if (!root_fd) return;
2848 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2850 set_fd_name( fd, root_fd, get_req_data(), get_req_data_size(), req->link, req->replace );
2851 release_object( fd );
2853 if (root_fd) release_object( root_fd );