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
35 #ifdef HAVE_LINUX_MAJOR_H
36 #include <linux/major.h>
38 #ifdef HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
42 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
44 #define list_next SYSLIST_NEXT
45 #define list_prev SYSLIST_PREV
46 #define list_head SYSLIST_HEAD
47 #define list_tail SYSLIST_TAIL
48 #define list_move_tail SYSLIST_MOVE_TAIL
49 #define list_remove SYSLIST_REMOVE
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
68 #ifdef HAVE_SYS_SYSCTL_H
69 #include <sys/sysctl.h>
71 #ifdef HAVE_SYS_EVENT_H
72 #include <sys/event.h>
82 #include <sys/mkdev.h>
83 #elif defined(MAJOR_IN_SYSMACROS)
84 #include <sys/sysmacros.h>
86 #include <sys/types.h>
88 #ifdef HAVE_SYS_SYSCALL_H
89 #include <sys/syscall.h>
93 #define WIN32_NO_STATUS
100 #include "winternl.h"
101 #include "winioctl.h"
104 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
105 # include <sys/epoll.h>
107 #endif /* HAVE_SYS_EPOLL_H && HAVE_EPOLL_CREATE */
109 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
111 # define USE_EVENT_PORTS
112 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
114 /* Because of the stupid Posix locking semantics, we need to keep
115 * track of all file descriptors referencing a given file, and not
116 * close a single one until all the locks are gone (sigh).
119 /* file descriptor object */
121 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
124 struct list entry
; /* entry in inode closed list */
125 int unix_fd
; /* the unix file descriptor */
126 unsigned int disp_flags
; /* the disposition flags */
127 char *unix_name
; /* name to unlink on close, points to parent fd unix_name */
132 struct object obj
; /* object header */
133 const struct fd_ops
*fd_ops
; /* file descriptor operations */
134 struct inode
*inode
; /* inode that this fd belongs to */
135 struct list inode_entry
; /* entry in inode fd list */
136 struct closed_fd
*closed
; /* structure to store the unix fd at destroy time */
137 struct object
*user
; /* object using this file descriptor */
138 struct list locks
; /* list of locks on this fd */
139 unsigned int access
; /* file access (FILE_READ_DATA etc.) */
140 unsigned int options
; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
141 unsigned int sharing
; /* file sharing mode */
142 char *unix_name
; /* unix file name */
143 WCHAR
*nt_name
; /* NT file name */
144 data_size_t nt_namelen
; /* length of NT file name */
145 int unix_fd
; /* unix file descriptor */
146 unsigned int no_fd_status
;/* status to return when unix_fd is -1 */
147 unsigned int cacheable
:1;/* can the fd be cached on the client side? */
148 unsigned int signaled
:1; /* is the fd signaled? */
149 unsigned int fs_locks
:1; /* can we use filesystem locks for this fd? */
150 int poll_index
; /* index of fd in poll array */
151 struct async_queue read_q
; /* async readers of this fd */
152 struct async_queue write_q
; /* async writers of this fd */
153 struct async_queue wait_q
; /* other async waiters of this fd */
154 struct completion
*completion
; /* completion object attached to this fd */
155 apc_param_t comp_key
; /* completion key to set in completion events */
156 unsigned int comp_flags
; /* completion flags */
159 static void fd_dump( struct object
*obj
, int verbose
);
160 static void fd_destroy( struct object
*obj
);
162 static const struct object_ops fd_ops
=
164 sizeof(struct fd
), /* size */
167 no_add_queue
, /* add_queue */
168 NULL
, /* remove_queue */
170 NULL
, /* satisfied */
171 no_signal
, /* signal */
172 no_get_fd
, /* get_fd */
173 default_map_access
, /* map_access */
174 default_get_sd
, /* get_sd */
175 default_set_sd
, /* set_sd */
176 no_get_full_name
, /* get_full_name */
177 no_lookup_name
, /* lookup_name */
178 no_link_name
, /* link_name */
179 NULL
, /* unlink_name */
180 no_open_file
, /* open_file */
181 no_kernel_obj_list
, /* get_kernel_obj_list */
182 no_close_handle
, /* close_handle */
183 fd_destroy
/* destroy */
188 #define DEVICE_HASH_SIZE 7
189 #define INODE_HASH_SIZE 17
193 struct object obj
; /* object header */
194 struct list entry
; /* entry in device hash list */
195 dev_t dev
; /* device number */
196 int removable
; /* removable device? (or -1 if unknown) */
197 struct list inode_hash
[INODE_HASH_SIZE
]; /* inodes hash table */
200 static void device_dump( struct object
*obj
, int verbose
);
201 static void device_destroy( struct object
*obj
);
203 static const struct object_ops device_ops
=
205 sizeof(struct device
), /* size */
207 device_dump
, /* dump */
208 no_add_queue
, /* add_queue */
209 NULL
, /* remove_queue */
211 NULL
, /* satisfied */
212 no_signal
, /* signal */
213 no_get_fd
, /* get_fd */
214 default_map_access
, /* map_access */
215 default_get_sd
, /* get_sd */
216 default_set_sd
, /* set_sd */
217 no_get_full_name
, /* get_full_name */
218 no_lookup_name
, /* lookup_name */
219 no_link_name
, /* link_name */
220 NULL
, /* unlink_name */
221 no_open_file
, /* open_file */
222 no_kernel_obj_list
, /* get_kernel_obj_list */
223 no_close_handle
, /* close_handle */
224 device_destroy
/* destroy */
231 struct object obj
; /* object header */
232 struct list entry
; /* inode hash list entry */
233 struct device
*device
; /* device containing this inode */
234 ino_t ino
; /* inode number */
235 struct list open
; /* list of open file descriptors */
236 struct list locks
; /* list of file locks */
237 struct list closed
; /* list of file descriptors to close at destroy time */
240 static void inode_dump( struct object
*obj
, int verbose
);
241 static void inode_destroy( struct object
*obj
);
243 static const struct object_ops inode_ops
=
245 sizeof(struct inode
), /* size */
247 inode_dump
, /* dump */
248 no_add_queue
, /* add_queue */
249 NULL
, /* remove_queue */
251 NULL
, /* satisfied */
252 no_signal
, /* signal */
253 no_get_fd
, /* get_fd */
254 default_map_access
, /* map_access */
255 default_get_sd
, /* get_sd */
256 default_set_sd
, /* set_sd */
257 no_get_full_name
, /* get_full_name */
258 no_lookup_name
, /* lookup_name */
259 no_link_name
, /* link_name */
260 NULL
, /* unlink_name */
261 no_open_file
, /* open_file */
262 no_kernel_obj_list
, /* get_kernel_obj_list */
263 no_close_handle
, /* close_handle */
264 inode_destroy
/* destroy */
267 /* file lock object */
271 struct object obj
; /* object header */
272 struct fd
*fd
; /* fd owning this lock */
273 struct list fd_entry
; /* entry in list of locks on a given fd */
274 struct list inode_entry
; /* entry in inode list of locks */
275 int shared
; /* shared lock? */
276 file_pos_t start
; /* locked region is interval [start;end) */
278 struct process
*process
; /* process owning this lock */
279 struct list proc_entry
; /* entry in list of locks owned by the process */
282 static void file_lock_dump( struct object
*obj
, int verbose
);
283 static int file_lock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
285 static const struct object_ops file_lock_ops
=
287 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 default_map_access
, /* map_access */
297 default_get_sd
, /* get_sd */
298 default_set_sd
, /* set_sd */
299 no_get_full_name
, /* get_full_name */
300 no_lookup_name
, /* lookup_name */
301 no_link_name
, /* link_name */
302 NULL
, /* unlink_name */
303 no_open_file
, /* open_file */
304 no_kernel_obj_list
, /* get_kernel_obj_list */
305 no_close_handle
, /* close_handle */
306 no_destroy
/* destroy */
310 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
311 #define FILE_POS_T_MAX (~(file_pos_t)0)
313 static file_pos_t max_unix_offset
= OFF_T_MAX
;
315 #define DUMP_LONG_LONG(val) do { \
316 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
317 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
319 fprintf( stderr, "%lx", (unsigned long)(val) ); \
324 /****************************************************************/
325 /* timeouts support */
329 struct list entry
; /* entry in sorted timeout list */
330 abstime_t when
; /* timeout expiry */
331 timeout_callback callback
; /* callback function */
332 void *private; /* callback private data */
335 static struct list abs_timeout_list
= LIST_INIT(abs_timeout_list
); /* sorted absolute timeouts list */
336 static struct list rel_timeout_list
= LIST_INIT(rel_timeout_list
); /* sorted relative timeouts list */
337 timeout_t current_time
;
338 timeout_t monotonic_time
;
340 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
341 static const int user_shared_data_timeout
= 16;
343 static void atomic_store_ulong(volatile ULONG
*ptr
, ULONG value
)
345 /* on x86 there should be total store order guarantees, so volatile is
346 * enough to ensure the stores aren't reordered by the compiler, and then
347 * they will always be seen in-order from other CPUs. On other archs, we
348 * need atomic intrinsics to guarantee that. */
349 #if defined(__i386__) || defined(__x86_64__)
352 __atomic_store_n(ptr
, value
, __ATOMIC_SEQ_CST
);
356 static void atomic_store_long(volatile LONG
*ptr
, LONG value
)
358 #if defined(__i386__) || defined(__x86_64__)
361 __atomic_store_n(ptr
, value
, __ATOMIC_SEQ_CST
);
365 static void set_user_shared_data_time(void)
367 timeout_t tick_count
= monotonic_time
/ 10000;
368 static timeout_t last_timezone_update
;
369 timeout_t timezone_bias
;
373 if (monotonic_time
- last_timezone_update
> TICKS_PER_SEC
)
377 timezone_bias
= mktime( tm
) - now
;
378 tm
= localtime( &now
);
379 if (tm
->tm_isdst
) timezone_bias
-= 3600;
380 timezone_bias
*= TICKS_PER_SEC
;
382 atomic_store_long(&user_shared_data
->TimeZoneBias
.High2Time
, timezone_bias
>> 32);
383 atomic_store_ulong(&user_shared_data
->TimeZoneBias
.LowPart
, timezone_bias
);
384 atomic_store_long(&user_shared_data
->TimeZoneBias
.High1Time
, timezone_bias
>> 32);
386 last_timezone_update
= monotonic_time
;
389 atomic_store_long(&user_shared_data
->SystemTime
.High2Time
, current_time
>> 32);
390 atomic_store_ulong(&user_shared_data
->SystemTime
.LowPart
, current_time
);
391 atomic_store_long(&user_shared_data
->SystemTime
.High1Time
, current_time
>> 32);
393 atomic_store_long(&user_shared_data
->InterruptTime
.High2Time
, monotonic_time
>> 32);
394 atomic_store_ulong(&user_shared_data
->InterruptTime
.LowPart
, monotonic_time
);
395 atomic_store_long(&user_shared_data
->InterruptTime
.High1Time
, monotonic_time
>> 32);
397 atomic_store_long(&user_shared_data
->TickCount
.High2Time
, tick_count
>> 32);
398 atomic_store_ulong(&user_shared_data
->TickCount
.LowPart
, tick_count
);
399 atomic_store_long(&user_shared_data
->TickCount
.High1Time
, tick_count
>> 32);
400 atomic_store_ulong(&user_shared_data
->TickCountLowDeprecated
, tick_count
);
403 void set_current_time(void)
405 static const timeout_t ticks_1601_to_1970
= (timeout_t
)86400 * (369 * 365 + 89) * TICKS_PER_SEC
;
407 gettimeofday( &now
, NULL
);
408 current_time
= (timeout_t
)now
.tv_sec
* TICKS_PER_SEC
+ now
.tv_usec
* 10 + ticks_1601_to_1970
;
409 monotonic_time
= monotonic_counter();
410 if (user_shared_data
) set_user_shared_data_time();
413 /* add a timeout user */
414 struct timeout_user
*add_timeout_user( timeout_t when
, timeout_callback func
, void *private )
416 struct timeout_user
*user
;
419 if (!(user
= mem_alloc( sizeof(*user
) ))) return NULL
;
420 user
->when
= timeout_to_abstime( when
);
421 user
->callback
= func
;
422 user
->private = private;
424 /* Now insert it in the linked list */
428 LIST_FOR_EACH( ptr
, &abs_timeout_list
)
430 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
431 if (timeout
->when
>= user
->when
) break;
436 LIST_FOR_EACH( ptr
, &rel_timeout_list
)
438 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
439 if (timeout
->when
<= user
->when
) break;
442 list_add_before( ptr
, &user
->entry
);
446 /* remove a timeout user */
447 void remove_timeout_user( struct timeout_user
*user
)
449 list_remove( &user
->entry
);
453 /* return a text description of a timeout for debugging purposes */
454 const char *get_timeout_str( timeout_t timeout
)
456 static char buffer
[64];
459 if (!timeout
) return "0";
460 if (timeout
== TIMEOUT_INFINITE
) return "infinite";
462 if (timeout
< 0) /* relative */
464 secs
= -timeout
/ TICKS_PER_SEC
;
465 nsecs
= -timeout
% TICKS_PER_SEC
;
466 sprintf( buffer
, "+%ld.%07ld", secs
, nsecs
);
470 secs
= (timeout
- current_time
) / TICKS_PER_SEC
;
471 nsecs
= (timeout
- current_time
) % TICKS_PER_SEC
;
474 nsecs
+= TICKS_PER_SEC
;
478 sprintf( buffer
, "%x%08x (+%ld.%07ld)",
479 (unsigned int)(timeout
>> 32), (unsigned int)timeout
, secs
, nsecs
);
481 sprintf( buffer
, "%x%08x (-%ld.%07ld)",
482 (unsigned int)(timeout
>> 32), (unsigned int)timeout
,
483 -(secs
+ 1), TICKS_PER_SEC
- nsecs
);
489 /****************************************************************/
492 static struct fd
**poll_users
; /* users array */
493 static struct pollfd
*pollfd
; /* poll fd array */
494 static int nb_users
; /* count of array entries actually in use */
495 static int active_users
; /* current number of active users */
496 static int allocated_users
; /* count of allocated entries in the array */
497 static struct fd
**freelist
; /* list of free entries in the array */
499 static int get_next_timeout(void);
501 static inline void fd_poll_event( struct fd
*fd
, int event
)
503 fd
->fd_ops
->poll_event( fd
, event
);
508 static int epoll_fd
= -1;
510 static inline void init_epoll(void)
512 epoll_fd
= epoll_create( 128 );
515 /* set the events that epoll waits for on this fd; helper for set_fd_events */
516 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
518 struct epoll_event ev
;
521 if (epoll_fd
== -1) return;
523 if (events
== -1) /* stop waiting on this fd completely */
525 if (pollfd
[user
].fd
== -1) return; /* already removed */
528 else if (pollfd
[user
].fd
== -1)
534 if (pollfd
[user
].events
== events
) return; /* nothing to do */
539 memset(&ev
.data
, 0, sizeof(ev
.data
));
542 if (epoll_ctl( epoll_fd
, ctl
, fd
->unix_fd
, &ev
) == -1)
544 if (errno
== ENOMEM
) /* not enough memory, give up on epoll */
549 else perror( "epoll_ctl" ); /* should not happen */
553 static inline void remove_epoll_user( struct fd
*fd
, int user
)
555 if (epoll_fd
== -1) return;
557 if (pollfd
[user
].fd
!= -1)
559 struct epoll_event dummy
;
560 epoll_ctl( epoll_fd
, EPOLL_CTL_DEL
, fd
->unix_fd
, &dummy
);
564 static inline void main_loop_epoll(void)
567 struct epoll_event events
[128];
569 assert( POLLIN
== EPOLLIN
);
570 assert( POLLOUT
== EPOLLOUT
);
571 assert( POLLERR
== EPOLLERR
);
572 assert( POLLHUP
== EPOLLHUP
);
574 if (epoll_fd
== -1) return;
578 timeout
= get_next_timeout();
580 if (!active_users
) break; /* last user removed by a timeout */
581 if (epoll_fd
== -1) break; /* an error occurred with epoll */
583 ret
= epoll_wait( epoll_fd
, events
, ARRAY_SIZE( events
), timeout
);
586 /* put the events into the pollfd array first, like poll does */
587 for (i
= 0; i
< ret
; i
++)
589 int user
= events
[i
].data
.u32
;
590 pollfd
[user
].revents
= events
[i
].events
;
593 /* read events from the pollfd array, as set_fd_events may modify them */
594 for (i
= 0; i
< ret
; i
++)
596 int user
= events
[i
].data
.u32
;
597 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
602 #elif defined(HAVE_KQUEUE)
604 static int kqueue_fd
= -1;
606 static inline void init_epoll(void)
608 kqueue_fd
= kqueue();
611 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
615 if (kqueue_fd
== -1) return;
617 EV_SET( &ev
[0], fd
->unix_fd
, EVFILT_READ
, 0, NOTE_LOWAT
, 1, (void *)(long)user
);
618 EV_SET( &ev
[1], fd
->unix_fd
, EVFILT_WRITE
, 0, NOTE_LOWAT
, 1, (void *)(long)user
);
620 if (events
== -1) /* stop waiting on this fd completely */
622 if (pollfd
[user
].fd
== -1) return; /* already removed */
623 ev
[0].flags
|= EV_DELETE
;
624 ev
[1].flags
|= EV_DELETE
;
626 else if (pollfd
[user
].fd
== -1)
628 ev
[0].flags
|= EV_ADD
| ((events
& POLLIN
) ? EV_ENABLE
: EV_DISABLE
);
629 ev
[1].flags
|= EV_ADD
| ((events
& POLLOUT
) ? EV_ENABLE
: EV_DISABLE
);
633 if (pollfd
[user
].events
== events
) return; /* nothing to do */
634 ev
[0].flags
|= (events
& POLLIN
) ? EV_ENABLE
: EV_DISABLE
;
635 ev
[1].flags
|= (events
& POLLOUT
) ? EV_ENABLE
: EV_DISABLE
;
638 if (kevent( kqueue_fd
, ev
, 2, NULL
, 0, NULL
) == -1)
640 if (errno
== ENOMEM
) /* not enough memory, give up on kqueue */
645 else perror( "kevent" ); /* should not happen */
649 static inline void remove_epoll_user( struct fd
*fd
, int user
)
651 if (kqueue_fd
== -1) return;
653 if (pollfd
[user
].fd
!= -1)
657 EV_SET( &ev
[0], fd
->unix_fd
, EVFILT_READ
, EV_DELETE
, 0, 0, 0 );
658 EV_SET( &ev
[1], fd
->unix_fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, 0 );
659 kevent( kqueue_fd
, ev
, 2, NULL
, 0, NULL
);
663 static inline void main_loop_epoll(void)
666 struct kevent events
[128];
668 if (kqueue_fd
== -1) return;
672 timeout
= get_next_timeout();
674 if (!active_users
) break; /* last user removed by a timeout */
675 if (kqueue_fd
== -1) break; /* an error occurred with kqueue */
681 ts
.tv_sec
= timeout
/ 1000;
682 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
683 ret
= kevent( kqueue_fd
, NULL
, 0, events
, ARRAY_SIZE( events
), &ts
);
685 else ret
= kevent( kqueue_fd
, NULL
, 0, events
, ARRAY_SIZE( events
), NULL
);
689 /* put the events into the pollfd array first, like poll does */
690 for (i
= 0; i
< ret
; i
++)
692 long user
= (long)events
[i
].udata
;
693 pollfd
[user
].revents
= 0;
695 for (i
= 0; i
< ret
; i
++)
697 long user
= (long)events
[i
].udata
;
698 if (events
[i
].filter
== EVFILT_READ
) pollfd
[user
].revents
|= POLLIN
;
699 else if (events
[i
].filter
== EVFILT_WRITE
) pollfd
[user
].revents
|= POLLOUT
;
700 if (events
[i
].flags
& EV_EOF
) pollfd
[user
].revents
|= POLLHUP
;
701 if (events
[i
].flags
& EV_ERROR
) pollfd
[user
].revents
|= POLLERR
;
704 /* read events from the pollfd array, as set_fd_events may modify them */
705 for (i
= 0; i
< ret
; i
++)
707 long user
= (long)events
[i
].udata
;
708 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
709 pollfd
[user
].revents
= 0;
714 #elif defined(USE_EVENT_PORTS)
716 static int port_fd
= -1;
718 static inline void init_epoll(void)
720 port_fd
= port_create();
723 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
727 if (port_fd
== -1) return;
729 if (events
== -1) /* stop waiting on this fd completely */
731 if (pollfd
[user
].fd
== -1) return; /* already removed */
732 port_dissociate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
);
734 else if (pollfd
[user
].fd
== -1)
736 ret
= port_associate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
, events
, (void *)user
);
740 if (pollfd
[user
].events
== events
) return; /* nothing to do */
741 ret
= port_associate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
, events
, (void *)user
);
746 if (errno
== ENOMEM
) /* not enough memory, give up on port_associate */
751 else perror( "port_associate" ); /* should not happen */
755 static inline void remove_epoll_user( struct fd
*fd
, int user
)
757 if (port_fd
== -1) return;
759 if (pollfd
[user
].fd
!= -1)
761 port_dissociate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
);
765 static inline void main_loop_epoll(void)
767 int i
, nget
, ret
, timeout
;
768 port_event_t events
[128];
770 if (port_fd
== -1) return;
774 timeout
= get_next_timeout();
777 if (!active_users
) break; /* last user removed by a timeout */
778 if (port_fd
== -1) break; /* an error occurred with event completion */
784 ts
.tv_sec
= timeout
/ 1000;
785 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
786 ret
= port_getn( port_fd
, events
, ARRAY_SIZE( events
), &nget
, &ts
);
788 else ret
= port_getn( port_fd
, events
, ARRAY_SIZE( events
), &nget
, NULL
);
790 if (ret
== -1) break; /* an error occurred with event completion */
794 /* put the events into the pollfd array first, like poll does */
795 for (i
= 0; i
< nget
; i
++)
797 long user
= (long)events
[i
].portev_user
;
798 pollfd
[user
].revents
= events
[i
].portev_events
;
801 /* read events from the pollfd array, as set_fd_events may modify them */
802 for (i
= 0; i
< nget
; i
++)
804 long user
= (long)events
[i
].portev_user
;
805 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
806 /* if we are still interested, reassociate the fd */
807 if (pollfd
[user
].fd
!= -1) {
808 port_associate( port_fd
, PORT_SOURCE_FD
, pollfd
[user
].fd
, pollfd
[user
].events
, (void *)user
);
814 #else /* HAVE_KQUEUE */
816 static inline void init_epoll(void) { }
817 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
) { }
818 static inline void remove_epoll_user( struct fd
*fd
, int user
) { }
819 static inline void main_loop_epoll(void) { }
821 #endif /* USE_EPOLL */
824 /* add a user in the poll array and return its index, or -1 on failure */
825 static int add_poll_user( struct fd
*fd
)
830 ret
= freelist
- poll_users
;
831 freelist
= (struct fd
**)poll_users
[ret
];
835 if (nb_users
== allocated_users
)
837 struct fd
**newusers
;
838 struct pollfd
*newpoll
;
839 int new_count
= allocated_users
? (allocated_users
+ allocated_users
/ 2) : 16;
840 if (!(newusers
= realloc( poll_users
, new_count
* sizeof(*poll_users
) ))) return -1;
841 if (!(newpoll
= realloc( pollfd
, new_count
* sizeof(*pollfd
) )))
844 poll_users
= newusers
;
849 poll_users
= newusers
;
851 if (!allocated_users
) init_epoll();
852 allocated_users
= new_count
;
857 pollfd
[ret
].events
= 0;
858 pollfd
[ret
].revents
= 0;
859 poll_users
[ret
] = fd
;
864 /* remove a user from the poll list */
865 static void remove_poll_user( struct fd
*fd
, int user
)
868 assert( poll_users
[user
] == fd
);
870 remove_epoll_user( fd
, user
);
871 pollfd
[user
].fd
= -1;
872 pollfd
[user
].events
= 0;
873 pollfd
[user
].revents
= 0;
874 poll_users
[user
] = (struct fd
*)freelist
;
875 freelist
= &poll_users
[user
];
879 /* process pending timeouts and return the time until the next timeout, in milliseconds */
880 static int get_next_timeout(void)
882 int ret
= user_shared_data
? user_shared_data_timeout
: -1;
884 if (!list_empty( &abs_timeout_list
) || !list_empty( &rel_timeout_list
))
886 struct list expired_list
, *ptr
;
888 /* first remove all expired timers from the list */
890 list_init( &expired_list
);
891 while ((ptr
= list_head( &abs_timeout_list
)) != NULL
)
893 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
895 if (timeout
->when
<= current_time
)
897 list_remove( &timeout
->entry
);
898 list_add_tail( &expired_list
, &timeout
->entry
);
902 while ((ptr
= list_head( &rel_timeout_list
)) != NULL
)
904 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
906 if (-timeout
->when
<= monotonic_time
)
908 list_remove( &timeout
->entry
);
909 list_add_tail( &expired_list
, &timeout
->entry
);
914 /* now call the callback for all the removed timers */
916 while ((ptr
= list_head( &expired_list
)) != NULL
)
918 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
919 list_remove( &timeout
->entry
);
920 timeout
->callback( timeout
->private );
924 if ((ptr
= list_head( &abs_timeout_list
)) != NULL
)
926 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
927 timeout_t diff
= (timeout
->when
- current_time
+ 9999) / 10000;
928 if (diff
> INT_MAX
) diff
= INT_MAX
;
929 else if (diff
< 0) diff
= 0;
930 if (ret
== -1 || diff
< ret
) ret
= diff
;
933 if ((ptr
= list_head( &rel_timeout_list
)) != NULL
)
935 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
936 timeout_t diff
= (-timeout
->when
- monotonic_time
+ 9999) / 10000;
937 if (diff
> INT_MAX
) diff
= INT_MAX
;
938 else if (diff
< 0) diff
= 0;
939 if (ret
== -1 || diff
< ret
) ret
= diff
;
945 /* server main poll() loop */
951 server_start_time
= current_time
;
954 /* fall through to normal poll loop */
958 timeout
= get_next_timeout();
960 if (!active_users
) break; /* last user removed by a timeout */
962 ret
= poll( pollfd
, nb_users
, timeout
);
967 for (i
= 0; i
< nb_users
; i
++)
969 if (pollfd
[i
].revents
)
971 fd_poll_event( poll_users
[i
], pollfd
[i
].revents
);
980 /****************************************************************/
981 /* device functions */
983 static struct list device_hash
[DEVICE_HASH_SIZE
];
985 static int is_device_removable( dev_t dev
, int unix_fd
)
987 #if defined(linux) && defined(HAVE_FSTATFS)
990 /* check for floppy disk */
991 if (major(dev
) == FLOPPY_MAJOR
) return 1;
993 if (fstatfs( unix_fd
, &stfs
) == -1) return 0;
994 return (stfs
.f_type
== 0x9660 || /* iso9660 */
995 stfs
.f_type
== 0x9fa1 || /* supermount */
996 stfs
.f_type
== 0x15013346); /* udf */
997 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
1000 if (fstatfs( unix_fd
, &stfs
) == -1) return 0;
1001 return (!strcmp("cd9660", stfs
.f_fstypename
) || !strcmp("udf", stfs
.f_fstypename
));
1002 #elif defined(__NetBSD__)
1003 struct statvfs stfs
;
1005 if (fstatvfs( unix_fd
, &stfs
) == -1) return 0;
1006 return (!strcmp("cd9660", stfs
.f_fstypename
) || !strcmp("udf", stfs
.f_fstypename
));
1008 # include <sys/dkio.h>
1009 # include <sys/vtoc.h>
1010 struct dk_cinfo dkinf
;
1011 if (ioctl( unix_fd
, DKIOCINFO
, &dkinf
) == -1) return 0;
1012 return (dkinf
.dki_ctype
== DKC_CDROM
||
1013 dkinf
.dki_ctype
== DKC_NCRFLOPPY
||
1014 dkinf
.dki_ctype
== DKC_SMSFLOPPY
||
1015 dkinf
.dki_ctype
== DKC_INTEL82072
||
1016 dkinf
.dki_ctype
== DKC_INTEL82077
);
1022 /* retrieve the device object for a given fd, creating it if needed */
1023 static struct device
*get_device( dev_t dev
, int unix_fd
)
1025 struct device
*device
;
1026 unsigned int i
, hash
= dev
% DEVICE_HASH_SIZE
;
1028 if (device_hash
[hash
].next
)
1030 LIST_FOR_EACH_ENTRY( device
, &device_hash
[hash
], struct device
, entry
)
1031 if (device
->dev
== dev
) return (struct device
*)grab_object( device
);
1033 else list_init( &device_hash
[hash
] );
1035 /* not found, create it */
1037 if (unix_fd
== -1) return NULL
;
1038 if ((device
= alloc_object( &device_ops
)))
1041 device
->removable
= is_device_removable( dev
, unix_fd
);
1042 for (i
= 0; i
< INODE_HASH_SIZE
; i
++) list_init( &device
->inode_hash
[i
] );
1043 list_add_head( &device_hash
[hash
], &device
->entry
);
1048 static void device_dump( struct object
*obj
, int verbose
)
1050 struct device
*device
= (struct device
*)obj
;
1051 fprintf( stderr
, "Device dev=" );
1052 DUMP_LONG_LONG( device
->dev
);
1053 fprintf( stderr
, "\n" );
1056 static void device_destroy( struct object
*obj
)
1058 struct device
*device
= (struct device
*)obj
;
1061 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
1062 assert( list_empty(&device
->inode_hash
[i
]) );
1064 list_remove( &device
->entry
); /* remove it from the hash table */
1067 /****************************************************************/
1068 /* inode functions */
1070 static void unlink_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
1072 /* make sure it is still the same file */
1074 if (!stat( fd
->unix_name
, &st
) && st
.st_dev
== inode
->device
->dev
&& st
.st_ino
== inode
->ino
)
1076 if (S_ISDIR(st
.st_mode
)) rmdir( fd
->unix_name
);
1077 else unlink( fd
->unix_name
);
1081 /* close all pending file descriptors in the closed list */
1082 static void inode_close_pending( struct inode
*inode
, int keep_unlinks
)
1084 struct list
*ptr
= list_head( &inode
->closed
);
1088 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
1089 struct list
*next
= list_next( &inode
->closed
, ptr
);
1091 if (fd
->unix_fd
!= -1)
1093 close( fd
->unix_fd
);
1097 /* get rid of it unless there's an unlink pending on that file */
1098 if (!keep_unlinks
|| !(fd
->disp_flags
& FILE_DISPOSITION_DELETE
))
1101 free( fd
->unix_name
);
1108 static void inode_dump( struct object
*obj
, int verbose
)
1110 struct inode
*inode
= (struct inode
*)obj
;
1111 fprintf( stderr
, "Inode device=%p ino=", inode
->device
);
1112 DUMP_LONG_LONG( inode
->ino
);
1113 fprintf( stderr
, "\n" );
1116 static void inode_destroy( struct object
*obj
)
1118 struct inode
*inode
= (struct inode
*)obj
;
1121 assert( list_empty(&inode
->open
) );
1122 assert( list_empty(&inode
->locks
) );
1124 list_remove( &inode
->entry
);
1126 while ((ptr
= list_head( &inode
->closed
)))
1128 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
1130 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1131 if (fd
->disp_flags
& FILE_DISPOSITION_DELETE
)
1132 unlink_closed_fd( inode
, fd
);
1133 free( fd
->unix_name
);
1136 release_object( inode
->device
);
1139 /* retrieve the inode object for a given fd, creating it if needed */
1140 static struct inode
*get_inode( dev_t dev
, ino_t ino
, int unix_fd
)
1142 struct device
*device
;
1143 struct inode
*inode
;
1144 unsigned int hash
= ino
% INODE_HASH_SIZE
;
1146 if (!(device
= get_device( dev
, unix_fd
))) return NULL
;
1148 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[hash
], struct inode
, entry
)
1150 if (inode
->ino
== ino
)
1152 release_object( device
);
1153 return (struct inode
*)grab_object( inode
);
1157 /* not found, create it */
1158 if ((inode
= alloc_object( &inode_ops
)))
1160 inode
->device
= device
;
1162 list_init( &inode
->open
);
1163 list_init( &inode
->locks
);
1164 list_init( &inode
->closed
);
1165 list_add_head( &device
->inode_hash
[hash
], &inode
->entry
);
1167 else release_object( device
);
1172 /* add fd to the inode list of file descriptors to close */
1173 static void inode_add_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
1175 if (!list_empty( &inode
->locks
))
1177 list_add_head( &inode
->closed
, &fd
->entry
);
1179 else if ((fd
->disp_flags
& FILE_DISPOSITION_DELETE
) &&
1180 (fd
->disp_flags
& FILE_DISPOSITION_POSIX_SEMANTICS
))
1182 /* close the fd and unlink it at once */
1183 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1184 unlink_closed_fd( inode
, fd
);
1185 free( fd
->unix_name
);
1188 else if (fd
->disp_flags
& FILE_DISPOSITION_DELETE
)
1190 /* close the fd but keep the structure around for unlink */
1191 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1193 list_add_head( &inode
->closed
, &fd
->entry
);
1195 else /* no locks on this inode and no unlink, get rid of the fd */
1197 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1198 free( fd
->unix_name
);
1204 /****************************************************************/
1205 /* file lock functions */
1207 static void file_lock_dump( struct object
*obj
, int verbose
)
1209 struct file_lock
*lock
= (struct file_lock
*)obj
;
1210 fprintf( stderr
, "Lock %s fd=%p proc=%p start=",
1211 lock
->shared
? "shared" : "excl", lock
->fd
, lock
->process
);
1212 DUMP_LONG_LONG( lock
->start
);
1213 fprintf( stderr
, " end=" );
1214 DUMP_LONG_LONG( lock
->end
);
1215 fprintf( stderr
, "\n" );
1218 static int file_lock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
1220 struct file_lock
*lock
= (struct file_lock
*)obj
;
1221 /* lock is signaled if it has lost its owner */
1222 return !lock
->process
;
1225 /* set (or remove) a Unix lock if possible for the given range */
1226 static int set_unix_lock( struct fd
*fd
, file_pos_t start
, file_pos_t end
, int type
)
1230 if (!fd
->fs_locks
) return 1; /* no fs locks possible for this fd */
1233 if (start
== end
) return 1; /* can't set zero-byte lock */
1234 if (start
> max_unix_offset
) return 1; /* ignore it */
1236 fl
.l_whence
= SEEK_SET
;
1238 if (!end
|| end
> max_unix_offset
) fl
.l_len
= 0;
1239 else fl
.l_len
= end
- start
;
1240 if (fcntl( fd
->unix_fd
, F_SETLK
, &fl
) != -1) return 1;
1245 /* check whether locks work at all on this file system */
1246 if (fcntl( fd
->unix_fd
, F_GETLK
, &fl
) != -1)
1248 set_error( STATUS_FILE_LOCK_CONFLICT
);
1255 /* no locking on this fs, just ignore it */
1259 set_error( STATUS_FILE_LOCK_CONFLICT
);
1262 /* this can happen if we try to set a write lock on a read-only file */
1263 /* try to at least grab a read lock */
1264 if (fl
.l_type
== F_WRLCK
)
1269 set_error( STATUS_ACCESS_DENIED
);
1275 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1276 /* in that case we shrink the limit and retry */
1277 if (max_unix_offset
> INT_MAX
)
1279 max_unix_offset
= INT_MAX
;
1290 /* check if interval [start;end) overlaps the lock */
1291 static inline int lock_overlaps( struct file_lock
*lock
, file_pos_t start
, file_pos_t end
)
1293 if (lock
->end
&& start
>= lock
->end
) return 0;
1294 if (end
&& lock
->start
>= end
) return 0;
1298 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1299 static void remove_unix_locks( struct fd
*fd
, file_pos_t start
, file_pos_t end
)
1307 } *first
, *cur
, *next
, *buffer
;
1312 if (!fd
->inode
) return;
1313 if (!fd
->fs_locks
) return;
1314 if (start
== end
|| start
> max_unix_offset
) return;
1315 if (!end
|| end
> max_unix_offset
) end
= max_unix_offset
+ 1;
1317 /* count the number of locks overlapping the specified area */
1319 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1321 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1322 if (lock
->start
== lock
->end
) continue;
1323 if (lock_overlaps( lock
, start
, end
)) count
++;
1326 if (!count
) /* no locks at all, we can unlock everything */
1328 set_unix_lock( fd
, start
, end
, F_UNLCK
);
1332 /* allocate space for the list of holes */
1333 /* max. number of holes is number of locks + 1 */
1335 if (!(buffer
= malloc( sizeof(*buffer
) * (count
+1) ))) return;
1339 first
->start
= start
;
1343 /* build a sorted list of unlocked holes in the specified area */
1345 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1347 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1348 if (lock
->start
== lock
->end
) continue;
1349 if (!lock_overlaps( lock
, start
, end
)) continue;
1351 /* go through all the holes touched by this lock */
1352 for (cur
= first
; cur
; cur
= cur
->next
)
1354 if (cur
->end
<= lock
->start
) continue; /* hole is before start of lock */
1355 if (lock
->end
&& cur
->start
>= lock
->end
) break; /* hole is after end of lock */
1357 /* now we know that lock is overlapping hole */
1359 if (cur
->start
>= lock
->start
) /* lock starts before hole, shrink from start */
1361 cur
->start
= lock
->end
;
1362 if (cur
->start
&& cur
->start
< cur
->end
) break; /* done with this lock */
1363 /* now hole is empty, remove it */
1364 if (cur
->next
) cur
->next
->prev
= cur
->prev
;
1365 if (cur
->prev
) cur
->prev
->next
= cur
->next
;
1366 else if (!(first
= cur
->next
)) goto done
; /* no more holes at all */
1368 else if (!lock
->end
|| cur
->end
<= lock
->end
) /* lock larger than hole, shrink from end */
1370 cur
->end
= lock
->start
;
1371 assert( cur
->start
< cur
->end
);
1373 else /* lock is in the middle of hole, split hole in two */
1376 next
->next
= cur
->next
;
1378 next
->start
= lock
->end
;
1379 next
->end
= cur
->end
;
1380 cur
->end
= lock
->start
;
1381 assert( next
->start
< next
->end
);
1382 assert( cur
->end
< next
->start
);
1384 break; /* done with this lock */
1389 /* clear Unix locks for all the holes */
1391 for (cur
= first
; cur
; cur
= cur
->next
)
1392 set_unix_lock( fd
, cur
->start
, cur
->end
, F_UNLCK
);
1398 /* create a new lock on a fd */
1399 static struct file_lock
*add_lock( struct fd
*fd
, int shared
, file_pos_t start
, file_pos_t end
)
1401 struct file_lock
*lock
;
1403 if (!(lock
= alloc_object( &file_lock_ops
))) return NULL
;
1404 lock
->shared
= shared
;
1405 lock
->start
= start
;
1408 lock
->process
= current
->process
;
1410 /* now try to set a Unix lock */
1411 if (!set_unix_lock( lock
->fd
, lock
->start
, lock
->end
, lock
->shared
? F_RDLCK
: F_WRLCK
))
1413 release_object( lock
);
1416 list_add_tail( &fd
->locks
, &lock
->fd_entry
);
1417 list_add_tail( &fd
->inode
->locks
, &lock
->inode_entry
);
1418 list_add_tail( &lock
->process
->locks
, &lock
->proc_entry
);
1422 /* remove an existing lock */
1423 static void remove_lock( struct file_lock
*lock
, int remove_unix
)
1425 struct inode
*inode
= lock
->fd
->inode
;
1427 list_remove( &lock
->fd_entry
);
1428 list_remove( &lock
->inode_entry
);
1429 list_remove( &lock
->proc_entry
);
1430 if (remove_unix
) remove_unix_locks( lock
->fd
, lock
->start
, lock
->end
);
1431 if (list_empty( &inode
->locks
)) inode_close_pending( inode
, 1 );
1432 lock
->process
= NULL
;
1433 wake_up( &lock
->obj
, 0 );
1434 release_object( lock
);
1437 /* remove all locks owned by a given process */
1438 void remove_process_locks( struct process
*process
)
1442 while ((ptr
= list_head( &process
->locks
)))
1444 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, proc_entry
);
1445 remove_lock( lock
, 1 ); /* this removes it from the list */
1449 /* remove all locks on a given fd */
1450 static void remove_fd_locks( struct fd
*fd
)
1452 file_pos_t start
= FILE_POS_T_MAX
, end
= 0;
1455 while ((ptr
= list_head( &fd
->locks
)))
1457 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1458 if (lock
->start
< start
) start
= lock
->start
;
1459 if (!lock
->end
|| lock
->end
> end
) end
= lock
->end
- 1;
1460 remove_lock( lock
, 0 );
1462 if (start
< end
) remove_unix_locks( fd
, start
, end
+ 1 );
1465 /* add a lock on an fd */
1466 /* returns handle to wait on */
1467 obj_handle_t
lock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
, int shared
, int wait
)
1470 file_pos_t end
= start
+ count
;
1472 if (!fd
->inode
) /* not a regular file */
1474 set_error( STATUS_INVALID_DEVICE_REQUEST
);
1478 /* don't allow wrapping locks */
1479 if (end
&& end
< start
)
1481 set_error( STATUS_INVALID_PARAMETER
);
1485 /* check if another lock on that file overlaps the area */
1486 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1488 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1489 if (!lock_overlaps( lock
, start
, end
)) continue;
1490 if (shared
&& (lock
->shared
|| lock
->fd
== fd
)) continue;
1494 set_error( STATUS_FILE_LOCK_CONFLICT
);
1497 set_error( STATUS_PENDING
);
1498 return alloc_handle( current
->process
, lock
, SYNCHRONIZE
, 0 );
1501 /* not found, add it */
1502 if (add_lock( fd
, shared
, start
, end
)) return 0;
1503 if (get_error() == STATUS_FILE_LOCK_CONFLICT
)
1505 /* Unix lock conflict -> tell client to wait and retry */
1506 if (wait
) set_error( STATUS_PENDING
);
1511 /* remove a lock on an fd */
1512 void unlock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
)
1515 file_pos_t end
= start
+ count
;
1517 /* find an existing lock with the exact same parameters */
1518 LIST_FOR_EACH( ptr
, &fd
->locks
)
1520 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1521 if ((lock
->start
== start
) && (lock
->end
== end
))
1523 remove_lock( lock
, 1 );
1527 set_error( STATUS_FILE_LOCK_CONFLICT
);
1531 /****************************************************************/
1532 /* file descriptor functions */
1534 static void fd_dump( struct object
*obj
, int verbose
)
1536 struct fd
*fd
= (struct fd
*)obj
;
1537 fprintf( stderr
, "Fd unix_fd=%d user=%p options=%08x", fd
->unix_fd
, fd
->user
, fd
->options
);
1538 if (fd
->inode
) fprintf( stderr
, " inode=%p disp_flags=%x", fd
->inode
, fd
->closed
->disp_flags
);
1539 fprintf( stderr
, "\n" );
1542 static void fd_destroy( struct object
*obj
)
1544 struct fd
*fd
= (struct fd
*)obj
;
1546 free_async_queue( &fd
->read_q
);
1547 free_async_queue( &fd
->write_q
);
1548 free_async_queue( &fd
->wait_q
);
1550 if (fd
->completion
) release_object( fd
->completion
);
1551 remove_fd_locks( fd
);
1552 list_remove( &fd
->inode_entry
);
1553 if (fd
->poll_index
!= -1) remove_poll_user( fd
, fd
->poll_index
);
1554 free( fd
->nt_name
);
1557 inode_add_closed_fd( fd
->inode
, fd
->closed
);
1558 release_object( fd
->inode
);
1560 else /* no inode, close it right away */
1562 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1563 free( fd
->unix_name
);
1567 /* check if the desired access is possible without violating */
1568 /* the sharing mode of other opens of the same file */
1569 static unsigned int check_sharing( struct fd
*fd
, unsigned int access
, unsigned int sharing
,
1570 unsigned int open_flags
, unsigned int options
)
1572 /* only a few access bits are meaningful wrt sharing */
1573 const unsigned int read_access
= FILE_READ_DATA
| FILE_EXECUTE
;
1574 const unsigned int write_access
= FILE_WRITE_DATA
| FILE_APPEND_DATA
;
1575 const unsigned int all_access
= read_access
| write_access
| DELETE
;
1577 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
1578 unsigned int existing_access
= 0;
1581 fd
->access
= access
;
1582 fd
->sharing
= sharing
;
1584 LIST_FOR_EACH( ptr
, &fd
->inode
->open
)
1586 struct fd
*fd_ptr
= LIST_ENTRY( ptr
, struct fd
, inode_entry
);
1589 /* if access mode is 0, sharing mode is ignored */
1590 if (fd_ptr
->access
& all_access
) existing_sharing
&= fd_ptr
->sharing
;
1591 existing_access
|= fd_ptr
->access
;
1595 if (((access
& read_access
) && !(existing_sharing
& FILE_SHARE_READ
)) ||
1596 ((access
& write_access
) && !(existing_sharing
& FILE_SHARE_WRITE
)) ||
1597 ((access
& DELETE
) && !(existing_sharing
& FILE_SHARE_DELETE
)))
1598 return STATUS_SHARING_VIOLATION
;
1599 if (((existing_access
& FILE_MAPPING_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) ||
1600 ((existing_access
& FILE_MAPPING_IMAGE
) && (access
& FILE_WRITE_DATA
)))
1601 return STATUS_SHARING_VIOLATION
;
1602 if ((existing_access
& FILE_MAPPING_IMAGE
) && (options
& FILE_DELETE_ON_CLOSE
))
1603 return STATUS_CANNOT_DELETE
;
1604 if ((existing_access
& FILE_MAPPING_ACCESS
) && (open_flags
& O_TRUNC
))
1605 return STATUS_USER_MAPPED_FILE
;
1606 if (!(access
& all_access
))
1607 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1608 if (((existing_access
& read_access
) && !(sharing
& FILE_SHARE_READ
)) ||
1609 ((existing_access
& write_access
) && !(sharing
& FILE_SHARE_WRITE
)) ||
1610 ((existing_access
& DELETE
) && !(sharing
& FILE_SHARE_DELETE
)))
1611 return STATUS_SHARING_VIOLATION
;
1615 /* set the events that select waits for on this fd */
1616 void set_fd_events( struct fd
*fd
, int events
)
1618 int user
= fd
->poll_index
;
1619 assert( poll_users
[user
] == fd
);
1621 set_fd_epoll_events( fd
, user
, events
);
1623 if (events
== -1) /* stop waiting on this fd completely */
1625 pollfd
[user
].fd
= -1;
1626 pollfd
[user
].events
= POLLERR
;
1627 pollfd
[user
].revents
= 0;
1631 pollfd
[user
].fd
= fd
->unix_fd
;
1632 pollfd
[user
].events
= events
;
1636 /* prepare an fd for unmounting its corresponding device */
1637 static inline void unmount_fd( struct fd
*fd
)
1639 assert( fd
->inode
);
1641 async_wake_up( &fd
->read_q
, STATUS_VOLUME_DISMOUNTED
);
1642 async_wake_up( &fd
->write_q
, STATUS_VOLUME_DISMOUNTED
);
1644 if (fd
->poll_index
!= -1) set_fd_events( fd
, -1 );
1646 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1649 fd
->no_fd_status
= STATUS_VOLUME_DISMOUNTED
;
1650 fd
->closed
->unix_fd
= -1;
1651 fd
->closed
->disp_flags
= 0;
1653 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1657 /* allocate an fd object, without setting the unix fd yet */
1658 static struct fd
*alloc_fd_object(void)
1660 struct fd
*fd
= alloc_object( &fd_ops
);
1662 if (!fd
) return NULL
;
1672 fd
->unix_name
= NULL
;
1678 fd
->poll_index
= -1;
1679 fd
->completion
= NULL
;
1681 init_async_queue( &fd
->read_q
);
1682 init_async_queue( &fd
->write_q
);
1683 init_async_queue( &fd
->wait_q
);
1684 list_init( &fd
->inode_entry
);
1685 list_init( &fd
->locks
);
1687 if ((fd
->poll_index
= add_poll_user( fd
)) == -1)
1689 release_object( fd
);
1695 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1696 struct fd
*alloc_pseudo_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
, unsigned int options
)
1698 struct fd
*fd
= alloc_object( &fd_ops
);
1700 if (!fd
) return NULL
;
1702 fd
->fd_ops
= fd_user_ops
;
1707 fd
->options
= options
;
1709 fd
->unix_name
= NULL
;
1716 fd
->poll_index
= -1;
1717 fd
->completion
= NULL
;
1719 fd
->no_fd_status
= STATUS_BAD_DEVICE_TYPE
;
1720 init_async_queue( &fd
->read_q
);
1721 init_async_queue( &fd
->write_q
);
1722 init_async_queue( &fd
->wait_q
);
1723 list_init( &fd
->inode_entry
);
1724 list_init( &fd
->locks
);
1728 /* duplicate an fd object for a different user */
1729 struct fd
*dup_fd_object( struct fd
*orig
, unsigned int access
, unsigned int sharing
, unsigned int options
)
1732 struct fd
*fd
= alloc_fd_object();
1734 if (!fd
) return NULL
;
1736 fd
->options
= options
;
1737 fd
->cacheable
= orig
->cacheable
;
1739 if (orig
->unix_name
)
1741 if (!(fd
->unix_name
= mem_alloc( strlen(orig
->unix_name
) + 1 ))) goto failed
;
1742 strcpy( fd
->unix_name
, orig
->unix_name
);
1744 if (orig
->nt_namelen
)
1746 if (!(fd
->nt_name
= memdup( orig
->nt_name
, orig
->nt_namelen
))) goto failed
;
1747 fd
->nt_namelen
= orig
->nt_namelen
;
1752 struct closed_fd
*closed
= mem_alloc( sizeof(*closed
) );
1753 if (!closed
) goto failed
;
1754 if ((fd
->unix_fd
= dup( orig
->unix_fd
)) == -1)
1760 closed
->unix_fd
= fd
->unix_fd
;
1761 closed
->disp_flags
= 0;
1762 closed
->unix_name
= fd
->unix_name
;
1763 fd
->closed
= closed
;
1764 fd
->inode
= (struct inode
*)grab_object( orig
->inode
);
1765 list_add_head( &fd
->inode
->open
, &fd
->inode_entry
);
1766 if ((err
= check_sharing( fd
, access
, sharing
, 0, options
)))
1772 else if ((fd
->unix_fd
= dup( orig
->unix_fd
)) == -1)
1780 release_object( fd
);
1784 /* find an existing fd object that can be reused for a mapping */
1785 struct fd
*get_fd_object_for_mapping( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
1789 if (!fd
->inode
) return NULL
;
1791 LIST_FOR_EACH_ENTRY( fd_ptr
, &fd
->inode
->open
, struct fd
, inode_entry
)
1792 if (fd_ptr
->access
== access
&& fd_ptr
->sharing
== sharing
)
1793 return (struct fd
*)grab_object( fd_ptr
);
1798 /* sets the user of an fd that previously had no user */
1799 void set_fd_user( struct fd
*fd
, const struct fd_ops
*user_ops
, struct object
*user
)
1801 assert( fd
->fd_ops
== NULL
);
1802 fd
->fd_ops
= user_ops
;
1806 char *dup_fd_name( struct fd
*root
, const char *name
)
1810 if (!root
) return strdup( name
);
1811 if (!root
->unix_name
) return NULL
;
1814 if (name
[0] == '.' && (!name
[1] || name
[1] == '/')) name
++;
1816 if ((ret
= malloc( strlen(root
->unix_name
) + strlen(name
) + 2 )))
1818 strcpy( ret
, root
->unix_name
);
1819 if (name
[0] && name
[0] != '/') strcat( ret
, "/" );
1820 strcat( ret
, name
);
1825 static WCHAR
*dup_nt_name( struct fd
*root
, struct unicode_str name
, data_size_t
*len
)
1833 if (!name
.len
) return NULL
;
1834 return memdup( name
.str
, name
.len
);
1836 if (!root
->nt_namelen
) return NULL
;
1837 retlen
= root
->nt_namelen
;
1840 if (name
.len
&& name
.str
[0] == '.' && (name
.len
== sizeof(WCHAR
) || name
.str
[1] == '\\'))
1843 name
.len
-= sizeof(WCHAR
);
1845 if ((ret
= malloc( retlen
+ name
.len
+ sizeof(WCHAR
) )))
1847 memcpy( ret
, root
->nt_name
, root
->nt_namelen
);
1848 if (name
.len
&& name
.str
[0] != '\\' &&
1849 root
->nt_namelen
&& root
->nt_name
[root
->nt_namelen
/ sizeof(WCHAR
) - 1] != '\\')
1851 ret
[retlen
/ sizeof(WCHAR
)] = '\\';
1852 retlen
+= sizeof(WCHAR
);
1854 memcpy( ret
+ retlen
/ sizeof(WCHAR
), name
.str
, name
.len
);
1855 *len
= retlen
+ name
.len
;
1860 void get_nt_name( struct fd
*fd
, struct unicode_str
*name
)
1862 name
->str
= fd
->nt_name
;
1863 name
->len
= fd
->nt_namelen
;
1866 /* open() wrapper that returns a struct fd with no fd user set */
1867 struct fd
*open_fd( struct fd
*root
, const char *name
, struct unicode_str nt_name
,
1868 int flags
, mode_t
*mode
, unsigned int access
,
1869 unsigned int sharing
, unsigned int options
)
1872 struct closed_fd
*closed_fd
;
1878 if (((options
& FILE_DELETE_ON_CLOSE
) && !(access
& DELETE
)) ||
1879 ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_TRUNC
)))
1881 set_error( STATUS_INVALID_PARAMETER
);
1885 if (!(fd
= alloc_fd_object())) return NULL
;
1887 fd
->options
= options
;
1888 if (!(closed_fd
= mem_alloc( sizeof(*closed_fd
) )))
1890 release_object( fd
);
1896 if ((root_fd
= get_unix_fd( root
)) == -1) goto error
;
1897 if (fchdir( root_fd
) == -1)
1905 /* create the directory if needed */
1906 if ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_CREAT
))
1908 if (mkdir( name
, *mode
) == -1)
1910 if (errno
!= EEXIST
|| (flags
& O_EXCL
))
1916 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
1919 if ((access
& FILE_UNIX_WRITE_ACCESS
) && !(options
& FILE_DIRECTORY_FILE
))
1921 if (access
& FILE_UNIX_READ_ACCESS
) rw_mode
= O_RDWR
;
1922 else rw_mode
= O_WRONLY
;
1924 else rw_mode
= O_RDONLY
;
1926 if ((fd
->unix_fd
= open( name
, rw_mode
| (flags
& ~O_TRUNC
), *mode
)) == -1)
1928 /* if we tried to open a directory for write access, retry read-only */
1929 if (errno
== EISDIR
)
1931 if ((access
& FILE_UNIX_WRITE_ACCESS
) || (flags
& O_CREAT
))
1932 fd
->unix_fd
= open( name
, O_RDONLY
| (flags
& ~(O_TRUNC
| O_CREAT
| O_EXCL
)), *mode
);
1935 if (fd
->unix_fd
== -1)
1937 /* check for trailing slash on file path */
1938 if ((errno
== ENOENT
|| errno
== ENOTDIR
) && name
[strlen(name
) - 1] == '/')
1939 set_error( STATUS_OBJECT_NAME_INVALID
);
1946 fd
->nt_name
= dup_nt_name( root
, nt_name
, &fd
->nt_namelen
);
1947 fd
->unix_name
= NULL
;
1948 if ((path
= dup_fd_name( root
, name
)))
1950 fd
->unix_name
= realpath( path
, NULL
);
1954 closed_fd
->unix_fd
= fd
->unix_fd
;
1955 closed_fd
->disp_flags
= 0;
1956 closed_fd
->unix_name
= fd
->unix_name
;
1957 fstat( fd
->unix_fd
, &st
);
1960 /* only bother with an inode for normal files and directories */
1961 if (S_ISREG(st
.st_mode
) || S_ISDIR(st
.st_mode
))
1964 struct inode
*inode
= get_inode( st
.st_dev
, st
.st_ino
, fd
->unix_fd
);
1968 /* we can close the fd because there are no others open on the same file,
1969 * otherwise we wouldn't have failed to allocate a new inode
1974 fd
->closed
= closed_fd
;
1975 fd
->cacheable
= !inode
->device
->removable
;
1976 list_add_head( &inode
->open
, &fd
->inode_entry
);
1979 /* check directory options */
1980 if ((options
& FILE_DIRECTORY_FILE
) && !S_ISDIR(st
.st_mode
))
1982 set_error( STATUS_NOT_A_DIRECTORY
);
1985 if ((options
& FILE_NON_DIRECTORY_FILE
) && S_ISDIR(st
.st_mode
))
1987 set_error( STATUS_FILE_IS_A_DIRECTORY
);
1990 if ((err
= check_sharing( fd
, access
, sharing
, flags
, options
)))
1996 /* can't unlink files if we don't have permission to access */
1997 if ((options
& FILE_DELETE_ON_CLOSE
) && !(flags
& O_CREAT
) &&
1998 !(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
2000 set_error( STATUS_CANNOT_DELETE
);
2004 fd
->closed
->disp_flags
= (options
& FILE_DELETE_ON_CLOSE
) ?
2005 FILE_DISPOSITION_DELETE
: 0;
2006 if (flags
& O_TRUNC
)
2008 if (S_ISDIR(st
.st_mode
))
2010 set_error( STATUS_OBJECT_NAME_COLLISION
);
2013 ftruncate( fd
->unix_fd
, 0 );
2016 else /* special file */
2018 if (options
& FILE_DELETE_ON_CLOSE
) /* we can't unlink special files */
2020 set_error( STATUS_INVALID_PARAMETER
);
2027 #ifdef HAVE_POSIX_FADVISE
2028 switch (options
& (FILE_SEQUENTIAL_ONLY
| FILE_RANDOM_ACCESS
))
2030 case FILE_SEQUENTIAL_ONLY
:
2031 posix_fadvise( fd
->unix_fd
, 0, 0, POSIX_FADV_SEQUENTIAL
);
2033 case FILE_RANDOM_ACCESS
:
2034 posix_fadvise( fd
->unix_fd
, 0, 0, POSIX_FADV_RANDOM
);
2039 if (root_fd
!= -1) fchdir( server_dir_fd
); /* go back to the server dir */
2043 release_object( fd
);
2045 if (root_fd
!= -1) fchdir( server_dir_fd
); /* go back to the server dir */
2049 /* create an fd for an anonymous file */
2050 /* if the function fails the unix fd is closed */
2051 struct fd
*create_anonymous_fd( const struct fd_ops
*fd_user_ops
, int unix_fd
, struct object
*user
,
2052 unsigned int options
)
2054 struct fd
*fd
= alloc_fd_object();
2058 set_fd_user( fd
, fd_user_ops
, user
);
2059 fd
->unix_fd
= unix_fd
;
2060 fd
->options
= options
;
2067 /* retrieve the object that is using an fd */
2068 void *get_fd_user( struct fd
*fd
)
2073 /* retrieve the opening options for the fd */
2074 unsigned int get_fd_options( struct fd
*fd
)
2079 /* retrieve the completion flags for the fd */
2080 unsigned int get_fd_comp_flags( struct fd
*fd
)
2082 return fd
->comp_flags
;
2085 /* check if fd is in overlapped mode */
2086 int is_fd_overlapped( struct fd
*fd
)
2088 return !(fd
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
2091 /* retrieve the unix fd for an object */
2092 int get_unix_fd( struct fd
*fd
)
2094 if (fd
->unix_fd
== -1) set_error( fd
->no_fd_status
);
2098 /* check if two file descriptors point to the same file */
2099 int is_same_file_fd( struct fd
*fd1
, struct fd
*fd2
)
2101 return fd1
->inode
== fd2
->inode
;
2104 /* allow the fd to be cached (can't be reset once set) */
2105 void allow_fd_caching( struct fd
*fd
)
2110 /* check if fd is on a removable device */
2111 int is_fd_removable( struct fd
*fd
)
2113 return (fd
->inode
&& fd
->inode
->device
->removable
);
2116 /* set or clear the fd signaled state */
2117 void set_fd_signaled( struct fd
*fd
, int signaled
)
2119 if (fd
->comp_flags
& FILE_SKIP_SET_EVENT_ON_HANDLE
) return;
2120 fd
->signaled
= signaled
;
2121 if (signaled
) wake_up( fd
->user
, 0 );
2124 /* check if events are pending and if yes return which one(s) */
2125 int check_fd_events( struct fd
*fd
, int events
)
2129 if (fd
->unix_fd
== -1) return POLLERR
;
2130 if (fd
->inode
) return events
; /* regular files are always signaled */
2132 pfd
.fd
= fd
->unix_fd
;
2133 pfd
.events
= events
;
2134 if (poll( &pfd
, 1, 0 ) <= 0) return 0;
2138 /* default signaled() routine for objects that poll() on an fd */
2139 int default_fd_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
2141 struct fd
*fd
= get_obj_fd( obj
);
2142 int ret
= fd
->signaled
;
2143 release_object( fd
);
2147 int default_fd_get_poll_events( struct fd
*fd
)
2151 if (async_waiting( &fd
->read_q
)) events
|= POLLIN
;
2152 if (async_waiting( &fd
->write_q
)) events
|= POLLOUT
;
2156 /* default handler for poll() events */
2157 void default_poll_event( struct fd
*fd
, int event
)
2159 if (event
& (POLLIN
| POLLERR
| POLLHUP
)) async_wake_up( &fd
->read_q
, STATUS_ALERTED
);
2160 if (event
& (POLLOUT
| POLLERR
| POLLHUP
)) async_wake_up( &fd
->write_q
, STATUS_ALERTED
);
2162 /* if an error occurred, stop polling this fd to avoid busy-looping */
2163 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
2164 else if (!fd
->inode
) set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
2167 void fd_queue_async( struct fd
*fd
, struct async
*async
, int type
)
2169 struct async_queue
*queue
;
2173 case ASYNC_TYPE_READ
:
2174 queue
= &fd
->read_q
;
2176 case ASYNC_TYPE_WRITE
:
2177 queue
= &fd
->write_q
;
2179 case ASYNC_TYPE_WAIT
:
2180 queue
= &fd
->wait_q
;
2187 queue_async( queue
, async
);
2189 if (type
!= ASYNC_TYPE_WAIT
)
2192 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
2193 else /* regular files are always ready for read and write */
2194 async_wake_up( queue
, STATUS_ALERTED
);
2198 void fd_async_wake_up( struct fd
*fd
, int type
, unsigned int status
)
2202 case ASYNC_TYPE_READ
:
2203 async_wake_up( &fd
->read_q
, status
);
2205 case ASYNC_TYPE_WRITE
:
2206 async_wake_up( &fd
->write_q
, status
);
2208 case ASYNC_TYPE_WAIT
:
2209 async_wake_up( &fd
->wait_q
, status
);
2216 void fd_cancel_async( struct fd
*fd
, struct async
*async
)
2218 fd
->fd_ops
->cancel_async( fd
, async
);
2221 void fd_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
2223 fd
->fd_ops
->reselect_async( fd
, queue
);
2226 void no_fd_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
2228 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2231 void default_fd_cancel_async( struct fd
*fd
, struct async
*async
)
2233 async_terminate( async
, STATUS_CANCELLED
);
2236 void default_fd_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
2238 fd_queue_async( fd
, async
, type
);
2239 set_error( STATUS_PENDING
);
2242 /* default reselect_async() fd routine */
2243 void default_fd_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
2245 if (queue
== &fd
->read_q
|| queue
== &fd
->write_q
)
2247 int poll_events
= fd
->fd_ops
->get_poll_events( fd
);
2248 int events
= check_fd_events( fd
, poll_events
);
2249 if (events
) fd
->fd_ops
->poll_event( fd
, events
);
2250 else set_fd_events( fd
, poll_events
);
2254 static inline int is_valid_mounted_device( struct stat
*st
)
2256 #if defined(linux) || defined(__sun__)
2257 return S_ISBLK( st
->st_mode
);
2259 /* disks are char devices on *BSD */
2260 return S_ISCHR( st
->st_mode
);
2264 /* close all Unix file descriptors on a device to allow unmounting it */
2265 static void unmount_device( struct fd
*device_fd
)
2269 struct device
*device
;
2270 struct inode
*inode
;
2272 int unix_fd
= get_unix_fd( device_fd
);
2274 if (unix_fd
== -1) return;
2276 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
2278 set_error( STATUS_INVALID_PARAMETER
);
2282 if (!(device
= get_device( st
.st_rdev
, -1 ))) return;
2284 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
2286 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[i
], struct inode
, entry
)
2288 LIST_FOR_EACH_ENTRY( fd
, &inode
->open
, struct fd
, inode_entry
)
2292 inode_close_pending( inode
, 0 );
2295 /* remove it from the hash table */
2296 list_remove( &device
->entry
);
2297 list_init( &device
->entry
);
2298 release_object( device
);
2301 /* default read() routine */
2302 void no_fd_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
2304 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2307 /* default write() routine */
2308 void no_fd_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
2310 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2313 /* default flush() routine */
2314 void no_fd_flush( struct fd
*fd
, struct async
*async
)
2316 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2319 /* default get_file_info() routine */
2320 void no_fd_get_file_info( struct fd
*fd
, obj_handle_t handle
, unsigned int info_class
)
2322 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2325 /* default get_file_info() routine */
2326 void default_fd_get_file_info( struct fd
*fd
, obj_handle_t handle
, unsigned int info_class
)
2330 case FileAccessInformation
:
2332 FILE_ACCESS_INFORMATION info
;
2333 if (get_reply_max_size() < sizeof(info
))
2335 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2338 info
.AccessFlags
= get_handle_access( current
->process
, handle
);
2339 set_reply_data( &info
, sizeof(info
) );
2342 case FileModeInformation
:
2344 FILE_MODE_INFORMATION info
;
2345 if (get_reply_max_size() < sizeof(info
))
2347 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2350 info
.Mode
= fd
->options
& ( FILE_WRITE_THROUGH
2351 | FILE_SEQUENTIAL_ONLY
2352 | FILE_NO_INTERMEDIATE_BUFFERING
2353 | FILE_SYNCHRONOUS_IO_ALERT
2354 | FILE_SYNCHRONOUS_IO_NONALERT
);
2355 set_reply_data( &info
, sizeof(info
) );
2358 case FileIoCompletionNotificationInformation
:
2360 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info
;
2361 if (get_reply_max_size() < sizeof(info
))
2363 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2366 info
.Flags
= fd
->comp_flags
;
2367 set_reply_data( &info
, sizeof(info
) );
2371 set_error( STATUS_NOT_IMPLEMENTED
);
2375 /* default get_volume_info() routine */
2376 void no_fd_get_volume_info( struct fd
*fd
, struct async
*async
, unsigned int info_class
)
2378 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2381 /* default ioctl() routine */
2382 void no_fd_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2384 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2387 /* default ioctl() routine */
2388 void default_fd_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2392 case FSCTL_DISMOUNT_VOLUME
:
2393 unmount_device( fd
);
2397 set_error( STATUS_NOT_SUPPORTED
);
2401 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2402 static struct fd
*get_handle_fd_obj( struct process
*process
, obj_handle_t handle
,
2403 unsigned int access
)
2405 struct fd
*fd
= NULL
;
2408 if ((obj
= get_handle_obj( process
, handle
, access
, NULL
)))
2410 fd
= get_obj_fd( obj
);
2411 release_object( obj
);
2416 static int is_dir_empty( int fd
)
2422 if ((fd
= dup( fd
)) == -1)
2425 if (!(dir
= fdopendir( fd
)))
2432 while (empty
&& (de
= readdir( dir
)))
2434 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2441 /* set disposition for the fd */
2442 static void set_fd_disposition( struct fd
*fd
, unsigned int flags
)
2448 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2452 if (fd
->unix_fd
== -1)
2454 set_error( fd
->no_fd_status
);
2458 if (flags
& FILE_DISPOSITION_DELETE
)
2462 LIST_FOR_EACH_ENTRY( fd_ptr
, &fd
->inode
->open
, struct fd
, inode_entry
)
2464 if (fd_ptr
->access
& FILE_MAPPING_ACCESS
)
2466 set_error( STATUS_CANNOT_DELETE
);
2471 if (fstat( fd
->unix_fd
, &st
) == -1)
2476 if (S_ISREG( st
.st_mode
)) /* can't unlink files we don't have permission to write */
2478 if (!(flags
& FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE
) &&
2479 !(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
2481 set_error( STATUS_CANNOT_DELETE
);
2485 else if (S_ISDIR( st
.st_mode
)) /* can't remove non-empty directories */
2487 switch (is_dir_empty( fd
->unix_fd
))
2493 set_error( STATUS_DIRECTORY_NOT_EMPTY
);
2497 else /* can't unlink special files */
2499 set_error( STATUS_INVALID_PARAMETER
);
2504 if (flags
& FILE_DISPOSITION_ON_CLOSE
)
2505 fd
->options
&= ~FILE_DELETE_ON_CLOSE
;
2507 fd
->closed
->disp_flags
=
2508 (flags
& (FILE_DISPOSITION_DELETE
| FILE_DISPOSITION_POSIX_SEMANTICS
)) |
2509 ((fd
->options
& FILE_DELETE_ON_CLOSE
) ? FILE_DISPOSITION_DELETE
: 0);
2512 /* set new name for the fd */
2513 static void set_fd_name( struct fd
*fd
, struct fd
*root
, const char *nameptr
, data_size_t len
,
2514 struct unicode_str nt_name
, int create_link
, int replace
)
2516 struct inode
*inode
;
2517 struct stat st
, st2
;
2520 if (!fd
->inode
|| !fd
->unix_name
)
2522 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2525 if (fd
->unix_fd
== -1)
2527 set_error( fd
->no_fd_status
);
2531 if (!len
|| ((nameptr
[0] == '/') ^ !root
))
2533 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
2536 if (!(name
= mem_alloc( len
+ 1 ))) return;
2537 memcpy( name
, nameptr
, len
);
2542 char *combined_name
= dup_fd_name( root
, name
);
2545 set_error( STATUS_NO_MEMORY
);
2549 name
= combined_name
;
2552 /* when creating a hard link, source cannot be a dir */
2553 if (create_link
&& !fstat( fd
->unix_fd
, &st
) && S_ISDIR( st
.st_mode
))
2555 set_error( STATUS_FILE_IS_A_DIRECTORY
);
2559 if (!stat( name
, &st
))
2561 if (!fstat( fd
->unix_fd
, &st2
) && st
.st_ino
== st2
.st_ino
&& st
.st_dev
== st2
.st_dev
)
2563 if (create_link
&& !replace
) set_error( STATUS_OBJECT_NAME_COLLISION
);
2570 set_error( STATUS_OBJECT_NAME_COLLISION
);
2574 /* can't replace directories or special files */
2575 if (!S_ISREG( st
.st_mode
))
2577 set_error( STATUS_ACCESS_DENIED
);
2581 /* can't replace an opened file */
2582 if ((inode
= get_inode( st
.st_dev
, st
.st_ino
, -1 )))
2584 int is_empty
= list_empty( &inode
->open
);
2585 release_object( inode
);
2588 set_error( STATUS_ACCESS_DENIED
);
2593 /* link() expects that the target doesn't exist */
2594 /* rename() cannot replace files with directories */
2595 if (create_link
|| S_ISDIR( st2
.st_mode
))
2607 if (link( fd
->unix_name
, name
))
2613 if (rename( fd
->unix_name
, name
))
2619 if (is_file_executable( fd
->unix_name
) != is_file_executable( name
) && !fstat( fd
->unix_fd
, &st
))
2621 if (is_file_executable( name
))
2622 /* set executable bit where read bit is set */
2623 st
.st_mode
|= (st
.st_mode
& 0444) >> 2;
2625 st
.st_mode
&= ~0111;
2626 fchmod( fd
->unix_fd
, st
.st_mode
);
2629 free( fd
->nt_name
);
2630 fd
->nt_name
= dup_nt_name( root
, nt_name
, &fd
->nt_namelen
);
2631 free( fd
->unix_name
);
2632 fd
->closed
->unix_name
= fd
->unix_name
= realpath( name
, NULL
);
2635 set_error( STATUS_NO_MEMORY
);
2642 static void set_fd_eof( struct fd
*fd
, file_pos_t eof
)
2648 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2652 if (fd
->unix_fd
== -1)
2654 set_error( fd
->no_fd_status
);
2657 if (fstat( fd
->unix_fd
, &st
) == -1)
2662 if (eof
< st
.st_size
)
2665 LIST_FOR_EACH_ENTRY( fd_ptr
, &fd
->inode
->open
, struct fd
, inode_entry
)
2667 if (fd_ptr
->access
& FILE_MAPPING_ACCESS
)
2669 set_error( STATUS_USER_MAPPED_FILE
);
2673 if (ftruncate( fd
->unix_fd
, eof
) == -1) file_set_error();
2675 else grow_file( fd
->unix_fd
, eof
);
2678 struct completion
*fd_get_completion( struct fd
*fd
, apc_param_t
*p_key
)
2680 *p_key
= fd
->comp_key
;
2681 return fd
->completion
? (struct completion
*)grab_object( fd
->completion
) : NULL
;
2684 void fd_copy_completion( struct fd
*src
, struct fd
*dst
)
2686 assert( !dst
->completion
);
2687 dst
->completion
= fd_get_completion( src
, &dst
->comp_key
);
2688 dst
->comp_flags
= src
->comp_flags
;
2691 /* flush a file buffers */
2694 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, 0 );
2695 struct async
*async
;
2699 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2701 fd
->fd_ops
->flush( fd
, async
);
2702 reply
->event
= async_handoff( async
, NULL
, 1 );
2703 release_object( async
);
2705 release_object( fd
);
2708 /* query file info */
2709 DECL_HANDLER(get_file_info
)
2711 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2715 fd
->fd_ops
->get_file_info( fd
, req
->handle
, req
->info_class
);
2716 release_object( fd
);
2720 /* query volume info */
2721 DECL_HANDLER(get_volume_info
)
2723 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2724 struct async
*async
;
2728 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2730 fd
->fd_ops
->get_volume_info( fd
, async
, req
->info_class
);
2731 reply
->wait
= async_handoff( async
, NULL
, 1 );
2732 release_object( async
);
2734 release_object( fd
);
2737 /* open a file object */
2738 DECL_HANDLER(open_file_object
)
2740 struct unicode_str name
= get_req_unicode_str();
2741 struct object
*obj
, *result
, *root
= NULL
;
2743 if (req
->rootdir
&& !(root
= get_handle_obj( current
->process
, req
->rootdir
, 0, NULL
))) return;
2745 obj
= open_named_object( root
, NULL
, &name
, req
->attributes
);
2746 if (root
) release_object( root
);
2749 if ((result
= obj
->ops
->open_file( obj
, req
->access
, req
->sharing
, req
->options
)))
2751 reply
->handle
= alloc_handle( current
->process
, result
, req
->access
, req
->attributes
);
2752 release_object( result
);
2754 release_object( obj
);
2757 /* get the Unix name from a file handle */
2758 DECL_HANDLER(get_handle_unix_name
)
2762 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2766 data_size_t name_len
= strlen( fd
->unix_name
);
2767 reply
->name_len
= name_len
;
2768 if (name_len
<= get_reply_max_size()) set_reply_data( fd
->unix_name
, name_len
);
2769 else set_error( STATUS_BUFFER_OVERFLOW
);
2771 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2772 release_object( fd
);
2776 /* get a Unix fd to access a file */
2777 DECL_HANDLER(get_handle_fd
)
2781 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2783 int unix_fd
= get_unix_fd( fd
);
2784 reply
->cacheable
= fd
->cacheable
;
2787 reply
->type
= fd
->fd_ops
->get_fd_type( fd
);
2788 reply
->options
= fd
->options
;
2789 reply
->access
= get_handle_access( current
->process
, req
->handle
);
2790 send_client_fd( current
->process
, unix_fd
, req
->handle
);
2792 release_object( fd
);
2796 /* perform a read on a file object */
2799 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, FILE_READ_DATA
);
2800 struct async
*async
;
2804 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2806 fd
->fd_ops
->read( fd
, async
, req
->pos
);
2807 reply
->wait
= async_handoff( async
, NULL
, 0 );
2808 reply
->options
= fd
->options
;
2809 release_object( async
);
2811 release_object( fd
);
2814 /* perform a write on a file object */
2817 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, FILE_WRITE_DATA
);
2818 struct async
*async
;
2822 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2824 fd
->fd_ops
->write( fd
, async
, req
->pos
);
2825 reply
->wait
= async_handoff( async
, &reply
->size
, 0 );
2826 reply
->options
= fd
->options
;
2827 release_object( async
);
2829 release_object( fd
);
2832 /* perform an ioctl on a file */
2835 unsigned int access
= (req
->code
>> 14) & (FILE_READ_DATA
|FILE_WRITE_DATA
);
2836 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, access
);
2837 struct async
*async
;
2841 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2843 fd
->fd_ops
->ioctl( fd
, req
->code
, async
);
2844 reply
->wait
= async_handoff( async
, NULL
, 0 );
2845 reply
->options
= fd
->options
;
2846 release_object( async
);
2848 release_object( fd
);
2851 /* create / reschedule an async I/O */
2852 DECL_HANDLER(register_async
)
2854 unsigned int access
;
2855 struct async
*async
;
2860 case ASYNC_TYPE_READ
:
2861 access
= FILE_READ_DATA
;
2863 case ASYNC_TYPE_WRITE
:
2864 access
= FILE_WRITE_DATA
;
2867 set_error( STATUS_INVALID_PARAMETER
);
2871 if ((fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, access
)))
2873 if (get_unix_fd( fd
) != -1 && (async
= create_async( fd
, current
, &req
->async
, NULL
)))
2875 fd
->fd_ops
->queue_async( fd
, async
, req
->type
, req
->count
);
2876 release_object( async
);
2878 release_object( fd
);
2882 /* attach completion object to a fd */
2883 DECL_HANDLER(set_completion_info
)
2885 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2889 if (is_fd_overlapped( fd
) && !fd
->completion
)
2891 fd
->completion
= get_completion_obj( current
->process
, req
->chandle
, IO_COMPLETION_MODIFY_STATE
);
2892 fd
->comp_key
= req
->ckey
;
2894 else set_error( STATUS_INVALID_PARAMETER
);
2895 release_object( fd
);
2899 /* push new completion msg into a completion queue attached to the fd */
2900 DECL_HANDLER(add_fd_completion
)
2902 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2905 if (fd
->completion
&& (req
->async
|| !(fd
->comp_flags
& FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
)))
2906 add_completion( fd
->completion
, fd
->comp_key
, req
->cvalue
, req
->status
, req
->information
);
2907 release_object( fd
);
2911 /* set fd completion information */
2912 DECL_HANDLER(set_fd_completion_mode
)
2914 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2917 if (is_fd_overlapped( fd
))
2919 if (req
->flags
& FILE_SKIP_SET_EVENT_ON_HANDLE
)
2920 set_fd_signaled( fd
, 0 );
2921 /* removing flags is not allowed */
2922 fd
->comp_flags
|= req
->flags
& ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
2923 | FILE_SKIP_SET_EVENT_ON_HANDLE
2924 | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO
);
2927 set_error( STATUS_INVALID_PARAMETER
);
2928 release_object( fd
);
2932 /* set fd disposition information */
2933 DECL_HANDLER(set_fd_disp_info
)
2935 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, DELETE
);
2938 set_fd_disposition( fd
, req
->flags
);
2939 release_object( fd
);
2943 /* set fd name information */
2944 DECL_HANDLER(set_fd_name_info
)
2946 struct fd
*fd
, *root_fd
= NULL
;
2947 struct unicode_str nt_name
;
2949 if (req
->namelen
> get_req_data_size())
2951 set_error( STATUS_INVALID_PARAMETER
);
2954 nt_name
.str
= get_req_data();
2955 nt_name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2961 if (!(root
= get_dir_obj( current
->process
, req
->rootdir
, 0 ))) return;
2962 root_fd
= get_obj_fd( (struct object
*)root
);
2963 release_object( root
);
2964 if (!root_fd
) return;
2967 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2969 set_fd_name( fd
, root_fd
, (const char *)get_req_data() + req
->namelen
,
2970 get_req_data_size() - req
->namelen
, nt_name
, req
->link
, req
->replace
);
2971 release_object( fd
);
2973 if (root_fd
) release_object( root_fd
);
2976 /* set fd eof information */
2977 DECL_HANDLER(set_fd_eof_info
)
2979 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2982 set_fd_eof( fd
, req
->eof
);
2983 release_object( fd
);