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
23 #include "wine/port.h"
37 #ifdef HAVE_SYS_POLL_H
40 #ifdef HAVE_LINUX_MAJOR_H
41 #include <linux/major.h>
43 #ifdef HAVE_SYS_STATVFS_H
44 #include <sys/statvfs.h>
47 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
49 #define list_next SYSLIST_NEXT
50 #define list_prev SYSLIST_PREV
51 #define list_head SYSLIST_HEAD
52 #define list_tail SYSLIST_TAIL
53 #define list_move_tail SYSLIST_MOVE_TAIL
54 #define list_remove SYSLIST_REMOVE
64 #ifdef HAVE_SYS_PARAM_H
65 #include <sys/param.h>
67 #ifdef HAVE_SYS_MOUNT_H
68 #include <sys/mount.h>
70 #ifdef HAVE_SYS_STATFS_H
71 #include <sys/statfs.h>
73 #ifdef HAVE_SYS_SYSCTL_H
74 #include <sys/sysctl.h>
76 #ifdef HAVE_SYS_EVENT_H
77 #include <sys/event.h>
87 #include <sys/mkdev.h>
88 #elif defined(MAJOR_IN_SYSMACROS)
89 #include <sys/sysmacros.h>
91 #include <sys/types.h>
93 #ifdef HAVE_SYS_SYSCALL_H
94 #include <sys/syscall.h>
98 #define WIN32_NO_STATUS
105 #include "winternl.h"
106 #include "winioctl.h"
109 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
110 # include <sys/epoll.h>
112 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
114 # define EPOLLIN POLLIN
115 # define EPOLLOUT POLLOUT
116 # define EPOLLERR POLLERR
117 # define EPOLLHUP POLLHUP
118 # define EPOLL_CTL_ADD 1
119 # define EPOLL_CTL_DEL 2
120 # define EPOLL_CTL_MOD 3
122 typedef union epoll_data
136 static inline int epoll_create( int size
)
138 return syscall( 254 /*NR_epoll_create*/, size
);
141 static inline int epoll_ctl( int epfd
, int op
, int fd
, const struct epoll_event
*event
)
143 return syscall( 255 /*NR_epoll_ctl*/, epfd
, op
, fd
, event
);
146 static inline int epoll_wait( int epfd
, struct epoll_event
*events
, int maxevents
, int timeout
)
148 return syscall( 256 /*NR_epoll_wait*/, epfd
, events
, maxevents
, timeout
);
151 #endif /* linux && __i386__ && HAVE_STDINT_H */
153 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
155 # define USE_EVENT_PORTS
156 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
158 /* Because of the stupid Posix locking semantics, we need to keep
159 * track of all file descriptors referencing a given file, and not
160 * close a single one until all the locks are gone (sigh).
163 /* file descriptor object */
165 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
168 struct list entry
; /* entry in inode closed list */
169 int unix_fd
; /* the unix file descriptor */
170 int unlink
; /* whether to unlink on close: -1 - implicit FILE_DELETE_ON_CLOSE, 1 - explicit disposition */
171 char *unix_name
; /* name to unlink on close, points to parent fd unix_name */
176 struct object obj
; /* object header */
177 const struct fd_ops
*fd_ops
; /* file descriptor operations */
178 struct inode
*inode
; /* inode that this fd belongs to */
179 struct list inode_entry
; /* entry in inode fd list */
180 struct closed_fd
*closed
; /* structure to store the unix fd at destroy time */
181 struct object
*user
; /* object using this file descriptor */
182 struct list locks
; /* list of locks on this fd */
183 unsigned int access
; /* file access (FILE_READ_DATA etc.) */
184 unsigned int options
; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
185 unsigned int sharing
; /* file sharing mode */
186 char *unix_name
; /* unix file name */
187 int unix_fd
; /* unix file descriptor */
188 unsigned int no_fd_status
;/* status to return when unix_fd is -1 */
189 unsigned int cacheable
:1;/* can the fd be cached on the client side? */
190 unsigned int signaled
:1; /* is the fd signaled? */
191 unsigned int fs_locks
:1; /* can we use filesystem locks for this fd? */
192 int poll_index
; /* index of fd in poll array */
193 struct async_queue read_q
; /* async readers of this fd */
194 struct async_queue write_q
; /* async writers of this fd */
195 struct async_queue wait_q
; /* other async waiters of this fd */
196 struct completion
*completion
; /* completion object attached to this fd */
197 apc_param_t comp_key
; /* completion key to set in completion events */
198 unsigned int comp_flags
; /* completion flags */
201 static void fd_dump( struct object
*obj
, int verbose
);
202 static void fd_destroy( struct object
*obj
);
204 static const struct object_ops fd_ops
=
206 sizeof(struct fd
), /* size */
208 no_get_type
, /* get_type */
209 no_add_queue
, /* add_queue */
210 NULL
, /* remove_queue */
212 NULL
, /* satisfied */
213 no_signal
, /* signal */
214 no_get_fd
, /* get_fd */
215 no_map_access
, /* map_access */
216 default_get_sd
, /* get_sd */
217 default_set_sd
, /* set_sd */
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 fd_destroy
/* destroy */
229 #define DEVICE_HASH_SIZE 7
230 #define INODE_HASH_SIZE 17
234 struct object obj
; /* object header */
235 struct list entry
; /* entry in device hash list */
236 dev_t dev
; /* device number */
237 int removable
; /* removable device? (or -1 if unknown) */
238 struct list inode_hash
[INODE_HASH_SIZE
]; /* inodes hash table */
241 static void device_dump( struct object
*obj
, int verbose
);
242 static void device_destroy( struct object
*obj
);
244 static const struct object_ops device_ops
=
246 sizeof(struct device
), /* size */
247 device_dump
, /* dump */
248 no_get_type
, /* get_type */
249 no_add_queue
, /* add_queue */
250 NULL
, /* remove_queue */
252 NULL
, /* satisfied */
253 no_signal
, /* signal */
254 no_get_fd
, /* get_fd */
255 no_map_access
, /* map_access */
256 default_get_sd
, /* get_sd */
257 default_set_sd
, /* set_sd */
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 device_destroy
/* destroy */
271 struct object obj
; /* object header */
272 struct list entry
; /* inode hash list entry */
273 struct device
*device
; /* device containing this inode */
274 ino_t ino
; /* inode number */
275 struct list open
; /* list of open file descriptors */
276 struct list locks
; /* list of file locks */
277 struct list closed
; /* list of file descriptors to close at destroy time */
280 static void inode_dump( struct object
*obj
, int verbose
);
281 static void inode_destroy( struct object
*obj
);
283 static const struct object_ops inode_ops
=
285 sizeof(struct inode
), /* size */
286 inode_dump
, /* dump */
287 no_get_type
, /* get_type */
288 no_add_queue
, /* add_queue */
289 NULL
, /* remove_queue */
291 NULL
, /* satisfied */
292 no_signal
, /* signal */
293 no_get_fd
, /* get_fd */
294 no_map_access
, /* map_access */
295 default_get_sd
, /* get_sd */
296 default_set_sd
, /* set_sd */
297 no_lookup_name
, /* lookup_name */
298 no_link_name
, /* link_name */
299 NULL
, /* unlink_name */
300 no_open_file
, /* open_file */
301 no_kernel_obj_list
, /* get_kernel_obj_list */
302 no_close_handle
, /* close_handle */
303 inode_destroy
/* destroy */
306 /* file lock object */
310 struct object obj
; /* object header */
311 struct fd
*fd
; /* fd owning this lock */
312 struct list fd_entry
; /* entry in list of locks on a given fd */
313 struct list inode_entry
; /* entry in inode list of locks */
314 int shared
; /* shared lock? */
315 file_pos_t start
; /* locked region is interval [start;end) */
317 struct process
*process
; /* process owning this lock */
318 struct list proc_entry
; /* entry in list of locks owned by the process */
321 static void file_lock_dump( struct object
*obj
, int verbose
);
322 static int file_lock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
324 static const struct object_ops file_lock_ops
=
326 sizeof(struct file_lock
), /* size */
327 file_lock_dump
, /* dump */
328 no_get_type
, /* get_type */
329 add_queue
, /* add_queue */
330 remove_queue
, /* remove_queue */
331 file_lock_signaled
, /* signaled */
332 no_satisfied
, /* satisfied */
333 no_signal
, /* signal */
334 no_get_fd
, /* get_fd */
335 no_map_access
, /* map_access */
336 default_get_sd
, /* get_sd */
337 default_set_sd
, /* set_sd */
338 no_lookup_name
, /* lookup_name */
339 no_link_name
, /* link_name */
340 NULL
, /* unlink_name */
341 no_open_file
, /* open_file */
342 no_kernel_obj_list
, /* get_kernel_obj_list */
343 no_close_handle
, /* close_handle */
344 no_destroy
/* destroy */
348 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
349 #define FILE_POS_T_MAX (~(file_pos_t)0)
351 static file_pos_t max_unix_offset
= OFF_T_MAX
;
353 #define DUMP_LONG_LONG(val) do { \
354 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
355 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
357 fprintf( stderr, "%lx", (unsigned long)(val) ); \
362 /****************************************************************/
363 /* timeouts support */
367 struct list entry
; /* entry in sorted timeout list */
368 abstime_t when
; /* timeout expiry */
369 timeout_callback callback
; /* callback function */
370 void *private; /* callback private data */
373 static struct list abs_timeout_list
= LIST_INIT(abs_timeout_list
); /* sorted absolute timeouts list */
374 static struct list rel_timeout_list
= LIST_INIT(rel_timeout_list
); /* sorted relative timeouts list */
375 timeout_t current_time
;
376 timeout_t monotonic_time
;
378 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
379 static const int user_shared_data_timeout
= 16;
381 static void set_user_shared_data_time(void)
383 timeout_t tick_count
= monotonic_time
/ 10000;
385 /* on X86 there should be total store order guarantees, so volatile is enough
386 * to ensure the stores aren't reordered by the compiler, and then they will
387 * always be seen in-order from other CPUs. On other archs, we need atomic
388 * intrinsics to guarantee that. */
389 #if defined(__i386__) || defined(__x86_64__)
390 user_shared_data
->SystemTime
.High2Time
= current_time
>> 32;
391 user_shared_data
->SystemTime
.LowPart
= current_time
;
392 user_shared_data
->SystemTime
.High1Time
= current_time
>> 32;
394 user_shared_data
->InterruptTime
.High2Time
= monotonic_time
>> 32;
395 user_shared_data
->InterruptTime
.LowPart
= monotonic_time
;
396 user_shared_data
->InterruptTime
.High1Time
= monotonic_time
>> 32;
398 user_shared_data
->TickCount
.High2Time
= tick_count
>> 32;
399 user_shared_data
->TickCount
.LowPart
= tick_count
;
400 user_shared_data
->TickCount
.High1Time
= tick_count
>> 32;
401 *(volatile ULONG
*)&user_shared_data
->TickCountLowDeprecated
= tick_count
;
403 __atomic_store_n(&user_shared_data
->SystemTime
.High2Time
, current_time
>> 32, __ATOMIC_SEQ_CST
);
404 __atomic_store_n(&user_shared_data
->SystemTime
.LowPart
, current_time
, __ATOMIC_SEQ_CST
);
405 __atomic_store_n(&user_shared_data
->SystemTime
.High1Time
, current_time
>> 32, __ATOMIC_SEQ_CST
);
407 __atomic_store_n(&user_shared_data
->InterruptTime
.High2Time
, monotonic_time
>> 32, __ATOMIC_SEQ_CST
);
408 __atomic_store_n(&user_shared_data
->InterruptTime
.LowPart
, monotonic_time
, __ATOMIC_SEQ_CST
);
409 __atomic_store_n(&user_shared_data
->InterruptTime
.High1Time
, monotonic_time
>> 32, __ATOMIC_SEQ_CST
);
411 __atomic_store_n(&user_shared_data
->TickCount
.High2Time
, tick_count
>> 32, __ATOMIC_SEQ_CST
);
412 __atomic_store_n(&user_shared_data
->TickCount
.LowPart
, tick_count
, __ATOMIC_SEQ_CST
);
413 __atomic_store_n(&user_shared_data
->TickCount
.High1Time
, tick_count
>> 32, __ATOMIC_SEQ_CST
);
414 __atomic_store_n(&user_shared_data
->TickCountLowDeprecated
, tick_count
, __ATOMIC_SEQ_CST
);
418 void set_current_time(void)
420 static const timeout_t ticks_1601_to_1970
= (timeout_t
)86400 * (369 * 365 + 89) * TICKS_PER_SEC
;
422 gettimeofday( &now
, NULL
);
423 current_time
= (timeout_t
)now
.tv_sec
* TICKS_PER_SEC
+ now
.tv_usec
* 10 + ticks_1601_to_1970
;
424 monotonic_time
= monotonic_counter();
425 if (user_shared_data
) set_user_shared_data_time();
428 /* add a timeout user */
429 struct timeout_user
*add_timeout_user( timeout_t when
, timeout_callback func
, void *private )
431 struct timeout_user
*user
;
434 if (!(user
= mem_alloc( sizeof(*user
) ))) return NULL
;
435 user
->when
= timeout_to_abstime( when
);
436 user
->callback
= func
;
437 user
->private = private;
439 /* Now insert it in the linked list */
443 LIST_FOR_EACH( ptr
, &abs_timeout_list
)
445 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
446 if (timeout
->when
>= user
->when
) break;
451 LIST_FOR_EACH( ptr
, &rel_timeout_list
)
453 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
454 if (timeout
->when
<= user
->when
) break;
457 list_add_before( ptr
, &user
->entry
);
461 /* remove a timeout user */
462 void remove_timeout_user( struct timeout_user
*user
)
464 list_remove( &user
->entry
);
468 /* return a text description of a timeout for debugging purposes */
469 const char *get_timeout_str( timeout_t timeout
)
471 static char buffer
[64];
474 if (!timeout
) return "0";
475 if (timeout
== TIMEOUT_INFINITE
) return "infinite";
477 if (timeout
< 0) /* relative */
479 secs
= -timeout
/ TICKS_PER_SEC
;
480 nsecs
= -timeout
% TICKS_PER_SEC
;
481 sprintf( buffer
, "+%ld.%07ld", secs
, nsecs
);
485 secs
= (timeout
- current_time
) / TICKS_PER_SEC
;
486 nsecs
= (timeout
- current_time
) % TICKS_PER_SEC
;
489 nsecs
+= TICKS_PER_SEC
;
493 sprintf( buffer
, "%x%08x (+%ld.%07ld)",
494 (unsigned int)(timeout
>> 32), (unsigned int)timeout
, secs
, nsecs
);
496 sprintf( buffer
, "%x%08x (-%ld.%07ld)",
497 (unsigned int)(timeout
>> 32), (unsigned int)timeout
,
498 -(secs
+ 1), TICKS_PER_SEC
- nsecs
);
504 /****************************************************************/
507 static struct fd
**poll_users
; /* users array */
508 static struct pollfd
*pollfd
; /* poll fd array */
509 static int nb_users
; /* count of array entries actually in use */
510 static int active_users
; /* current number of active users */
511 static int allocated_users
; /* count of allocated entries in the array */
512 static struct fd
**freelist
; /* list of free entries in the array */
514 static int get_next_timeout(void);
516 static inline void fd_poll_event( struct fd
*fd
, int event
)
518 fd
->fd_ops
->poll_event( fd
, event
);
523 static int epoll_fd
= -1;
525 static inline void init_epoll(void)
527 epoll_fd
= epoll_create( 128 );
530 /* set the events that epoll waits for on this fd; helper for set_fd_events */
531 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
533 struct epoll_event ev
;
536 if (epoll_fd
== -1) return;
538 if (events
== -1) /* stop waiting on this fd completely */
540 if (pollfd
[user
].fd
== -1) return; /* already removed */
543 else if (pollfd
[user
].fd
== -1)
545 if (pollfd
[user
].events
) return; /* stopped waiting on it, don't restart */
550 if (pollfd
[user
].events
== events
) return; /* nothing to do */
555 memset(&ev
.data
, 0, sizeof(ev
.data
));
558 if (epoll_ctl( epoll_fd
, ctl
, fd
->unix_fd
, &ev
) == -1)
560 if (errno
== ENOMEM
) /* not enough memory, give up on epoll */
565 else perror( "epoll_ctl" ); /* should not happen */
569 static inline void remove_epoll_user( struct fd
*fd
, int user
)
571 if (epoll_fd
== -1) return;
573 if (pollfd
[user
].fd
!= -1)
575 struct epoll_event dummy
;
576 epoll_ctl( epoll_fd
, EPOLL_CTL_DEL
, fd
->unix_fd
, &dummy
);
580 static inline void main_loop_epoll(void)
583 struct epoll_event events
[128];
585 assert( POLLIN
== EPOLLIN
);
586 assert( POLLOUT
== EPOLLOUT
);
587 assert( POLLERR
== EPOLLERR
);
588 assert( POLLHUP
== EPOLLHUP
);
590 if (epoll_fd
== -1) return;
594 timeout
= get_next_timeout();
596 if (!active_users
) break; /* last user removed by a timeout */
597 if (epoll_fd
== -1) break; /* an error occurred with epoll */
599 ret
= epoll_wait( epoll_fd
, events
, ARRAY_SIZE( events
), timeout
);
602 /* put the events into the pollfd array first, like poll does */
603 for (i
= 0; i
< ret
; i
++)
605 int user
= events
[i
].data
.u32
;
606 pollfd
[user
].revents
= events
[i
].events
;
609 /* read events from the pollfd array, as set_fd_events may modify them */
610 for (i
= 0; i
< ret
; i
++)
612 int user
= events
[i
].data
.u32
;
613 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
618 #elif defined(HAVE_KQUEUE)
620 static int kqueue_fd
= -1;
622 static inline void init_epoll(void)
624 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
627 size_t len
= sizeof(release
);
630 mib
[1] = KERN_OSRELEASE
;
631 if (sysctl( mib
, 2, release
, &len
, NULL
, 0 ) == -1) return;
632 if (atoi(release
) < 9) return;
634 kqueue_fd
= kqueue();
637 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
641 if (kqueue_fd
== -1) return;
643 EV_SET( &ev
[0], fd
->unix_fd
, EVFILT_READ
, 0, NOTE_LOWAT
, 1, (void *)(long)user
);
644 EV_SET( &ev
[1], fd
->unix_fd
, EVFILT_WRITE
, 0, NOTE_LOWAT
, 1, (void *)(long)user
);
646 if (events
== -1) /* stop waiting on this fd completely */
648 if (pollfd
[user
].fd
== -1) return; /* already removed */
649 ev
[0].flags
|= EV_DELETE
;
650 ev
[1].flags
|= EV_DELETE
;
652 else if (pollfd
[user
].fd
== -1)
654 if (pollfd
[user
].events
) return; /* stopped waiting on it, don't restart */
655 ev
[0].flags
|= EV_ADD
| ((events
& POLLIN
) ? EV_ENABLE
: EV_DISABLE
);
656 ev
[1].flags
|= EV_ADD
| ((events
& POLLOUT
) ? EV_ENABLE
: EV_DISABLE
);
660 if (pollfd
[user
].events
== events
) return; /* nothing to do */
661 ev
[0].flags
|= (events
& POLLIN
) ? EV_ENABLE
: EV_DISABLE
;
662 ev
[1].flags
|= (events
& POLLOUT
) ? EV_ENABLE
: EV_DISABLE
;
665 if (kevent( kqueue_fd
, ev
, 2, NULL
, 0, NULL
) == -1)
667 if (errno
== ENOMEM
) /* not enough memory, give up on kqueue */
672 else perror( "kevent" ); /* should not happen */
676 static inline void remove_epoll_user( struct fd
*fd
, int user
)
678 if (kqueue_fd
== -1) return;
680 if (pollfd
[user
].fd
!= -1)
684 EV_SET( &ev
[0], fd
->unix_fd
, EVFILT_READ
, EV_DELETE
, 0, 0, 0 );
685 EV_SET( &ev
[1], fd
->unix_fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, 0 );
686 kevent( kqueue_fd
, ev
, 2, NULL
, 0, NULL
);
690 static inline void main_loop_epoll(void)
693 struct kevent events
[128];
695 if (kqueue_fd
== -1) return;
699 timeout
= get_next_timeout();
701 if (!active_users
) break; /* last user removed by a timeout */
702 if (kqueue_fd
== -1) break; /* an error occurred with kqueue */
708 ts
.tv_sec
= timeout
/ 1000;
709 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
710 ret
= kevent( kqueue_fd
, NULL
, 0, events
, ARRAY_SIZE( events
), &ts
);
712 else ret
= kevent( kqueue_fd
, NULL
, 0, events
, ARRAY_SIZE( events
), NULL
);
716 /* put the events into the pollfd array first, like poll does */
717 for (i
= 0; i
< ret
; i
++)
719 long user
= (long)events
[i
].udata
;
720 pollfd
[user
].revents
= 0;
722 for (i
= 0; i
< ret
; i
++)
724 long user
= (long)events
[i
].udata
;
725 if (events
[i
].filter
== EVFILT_READ
) pollfd
[user
].revents
|= POLLIN
;
726 else if (events
[i
].filter
== EVFILT_WRITE
) pollfd
[user
].revents
|= POLLOUT
;
727 if (events
[i
].flags
& EV_EOF
) pollfd
[user
].revents
|= POLLHUP
;
728 if (events
[i
].flags
& EV_ERROR
) pollfd
[user
].revents
|= POLLERR
;
731 /* read events from the pollfd array, as set_fd_events may modify them */
732 for (i
= 0; i
< ret
; i
++)
734 long user
= (long)events
[i
].udata
;
735 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
736 pollfd
[user
].revents
= 0;
741 #elif defined(USE_EVENT_PORTS)
743 static int port_fd
= -1;
745 static inline void init_epoll(void)
747 port_fd
= port_create();
750 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
754 if (port_fd
== -1) return;
756 if (events
== -1) /* stop waiting on this fd completely */
758 if (pollfd
[user
].fd
== -1) return; /* already removed */
759 port_dissociate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
);
761 else if (pollfd
[user
].fd
== -1)
763 if (pollfd
[user
].events
) return; /* stopped waiting on it, don't restart */
764 ret
= port_associate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
, events
, (void *)user
);
768 if (pollfd
[user
].events
== events
) return; /* nothing to do */
769 ret
= port_associate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
, events
, (void *)user
);
774 if (errno
== ENOMEM
) /* not enough memory, give up on port_associate */
779 else perror( "port_associate" ); /* should not happen */
783 static inline void remove_epoll_user( struct fd
*fd
, int user
)
785 if (port_fd
== -1) return;
787 if (pollfd
[user
].fd
!= -1)
789 port_dissociate( port_fd
, PORT_SOURCE_FD
, fd
->unix_fd
);
793 static inline void main_loop_epoll(void)
795 int i
, nget
, ret
, timeout
;
796 port_event_t events
[128];
798 if (port_fd
== -1) return;
802 timeout
= get_next_timeout();
805 if (!active_users
) break; /* last user removed by a timeout */
806 if (port_fd
== -1) break; /* an error occurred with event completion */
812 ts
.tv_sec
= timeout
/ 1000;
813 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
814 ret
= port_getn( port_fd
, events
, ARRAY_SIZE( events
), &nget
, &ts
);
816 else ret
= port_getn( port_fd
, events
, ARRAY_SIZE( events
), &nget
, NULL
);
818 if (ret
== -1) break; /* an error occurred with event completion */
822 /* put the events into the pollfd array first, like poll does */
823 for (i
= 0; i
< nget
; i
++)
825 long user
= (long)events
[i
].portev_user
;
826 pollfd
[user
].revents
= events
[i
].portev_events
;
829 /* read events from the pollfd array, as set_fd_events may modify them */
830 for (i
= 0; i
< nget
; i
++)
832 long user
= (long)events
[i
].portev_user
;
833 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
834 /* if we are still interested, reassociate the fd */
835 if (pollfd
[user
].fd
!= -1) {
836 port_associate( port_fd
, PORT_SOURCE_FD
, pollfd
[user
].fd
, pollfd
[user
].events
, (void *)user
);
842 #else /* HAVE_KQUEUE */
844 static inline void init_epoll(void) { }
845 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
) { }
846 static inline void remove_epoll_user( struct fd
*fd
, int user
) { }
847 static inline void main_loop_epoll(void) { }
849 #endif /* USE_EPOLL */
852 /* add a user in the poll array and return its index, or -1 on failure */
853 static int add_poll_user( struct fd
*fd
)
858 ret
= freelist
- poll_users
;
859 freelist
= (struct fd
**)poll_users
[ret
];
863 if (nb_users
== allocated_users
)
865 struct fd
**newusers
;
866 struct pollfd
*newpoll
;
867 int new_count
= allocated_users
? (allocated_users
+ allocated_users
/ 2) : 16;
868 if (!(newusers
= realloc( poll_users
, new_count
* sizeof(*poll_users
) ))) return -1;
869 if (!(newpoll
= realloc( pollfd
, new_count
* sizeof(*pollfd
) )))
872 poll_users
= newusers
;
877 poll_users
= newusers
;
879 if (!allocated_users
) init_epoll();
880 allocated_users
= new_count
;
885 pollfd
[ret
].events
= 0;
886 pollfd
[ret
].revents
= 0;
887 poll_users
[ret
] = fd
;
892 /* remove a user from the poll list */
893 static void remove_poll_user( struct fd
*fd
, int user
)
896 assert( poll_users
[user
] == fd
);
898 remove_epoll_user( fd
, user
);
899 pollfd
[user
].fd
= -1;
900 pollfd
[user
].events
= 0;
901 pollfd
[user
].revents
= 0;
902 poll_users
[user
] = (struct fd
*)freelist
;
903 freelist
= &poll_users
[user
];
907 /* process pending timeouts and return the time until the next timeout, in milliseconds */
908 static int get_next_timeout(void)
910 int ret
= user_shared_data
? user_shared_data_timeout
: -1;
912 if (!list_empty( &abs_timeout_list
) || !list_empty( &rel_timeout_list
))
914 struct list expired_list
, *ptr
;
916 /* first remove all expired timers from the list */
918 list_init( &expired_list
);
919 while ((ptr
= list_head( &abs_timeout_list
)) != NULL
)
921 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
923 if (timeout
->when
<= current_time
)
925 list_remove( &timeout
->entry
);
926 list_add_tail( &expired_list
, &timeout
->entry
);
930 while ((ptr
= list_head( &rel_timeout_list
)) != NULL
)
932 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
934 if (-timeout
->when
<= monotonic_time
)
936 list_remove( &timeout
->entry
);
937 list_add_tail( &expired_list
, &timeout
->entry
);
942 /* now call the callback for all the removed timers */
944 while ((ptr
= list_head( &expired_list
)) != NULL
)
946 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
947 list_remove( &timeout
->entry
);
948 timeout
->callback( timeout
->private );
952 if ((ptr
= list_head( &abs_timeout_list
)) != NULL
)
954 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
955 int diff
= (timeout
->when
- current_time
+ 9999) / 10000;
956 if (diff
< 0) diff
= 0;
957 if (ret
== -1 || diff
< ret
) ret
= diff
;
960 if ((ptr
= list_head( &rel_timeout_list
)) != NULL
)
962 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
963 int diff
= (-timeout
->when
- monotonic_time
+ 9999) / 10000;
964 if (diff
< 0) diff
= 0;
965 if (ret
== -1 || diff
< ret
) ret
= diff
;
971 /* server main poll() loop */
977 server_start_time
= current_time
;
980 /* fall through to normal poll loop */
984 timeout
= get_next_timeout();
986 if (!active_users
) break; /* last user removed by a timeout */
988 ret
= poll( pollfd
, nb_users
, timeout
);
993 for (i
= 0; i
< nb_users
; i
++)
995 if (pollfd
[i
].revents
)
997 fd_poll_event( poll_users
[i
], pollfd
[i
].revents
);
1006 /****************************************************************/
1007 /* device functions */
1009 static struct list device_hash
[DEVICE_HASH_SIZE
];
1011 static int is_device_removable( dev_t dev
, int unix_fd
)
1013 #if defined(linux) && defined(HAVE_FSTATFS)
1016 /* check for floppy disk */
1017 if (major(dev
) == FLOPPY_MAJOR
) return 1;
1019 if (fstatfs( unix_fd
, &stfs
) == -1) return 0;
1020 return (stfs
.f_type
== 0x9660 || /* iso9660 */
1021 stfs
.f_type
== 0x9fa1 || /* supermount */
1022 stfs
.f_type
== 0x15013346); /* udf */
1023 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
1026 if (fstatfs( unix_fd
, &stfs
) == -1) return 0;
1027 return (!strcmp("cd9660", stfs
.f_fstypename
) || !strcmp("udf", stfs
.f_fstypename
));
1028 #elif defined(__NetBSD__)
1029 struct statvfs stfs
;
1031 if (fstatvfs( unix_fd
, &stfs
) == -1) return 0;
1032 return (!strcmp("cd9660", stfs
.f_fstypename
) || !strcmp("udf", stfs
.f_fstypename
));
1034 # include <sys/dkio.h>
1035 # include <sys/vtoc.h>
1036 struct dk_cinfo dkinf
;
1037 if (ioctl( unix_fd
, DKIOCINFO
, &dkinf
) == -1) return 0;
1038 return (dkinf
.dki_ctype
== DKC_CDROM
||
1039 dkinf
.dki_ctype
== DKC_NCRFLOPPY
||
1040 dkinf
.dki_ctype
== DKC_SMSFLOPPY
||
1041 dkinf
.dki_ctype
== DKC_INTEL82072
||
1042 dkinf
.dki_ctype
== DKC_INTEL82077
);
1048 /* retrieve the device object for a given fd, creating it if needed */
1049 static struct device
*get_device( dev_t dev
, int unix_fd
)
1051 struct device
*device
;
1052 unsigned int i
, hash
= dev
% DEVICE_HASH_SIZE
;
1054 if (device_hash
[hash
].next
)
1056 LIST_FOR_EACH_ENTRY( device
, &device_hash
[hash
], struct device
, entry
)
1057 if (device
->dev
== dev
) return (struct device
*)grab_object( device
);
1059 else list_init( &device_hash
[hash
] );
1061 /* not found, create it */
1063 if (unix_fd
== -1) return NULL
;
1064 if ((device
= alloc_object( &device_ops
)))
1067 device
->removable
= is_device_removable( dev
, unix_fd
);
1068 for (i
= 0; i
< INODE_HASH_SIZE
; i
++) list_init( &device
->inode_hash
[i
] );
1069 list_add_head( &device_hash
[hash
], &device
->entry
);
1074 static void device_dump( struct object
*obj
, int verbose
)
1076 struct device
*device
= (struct device
*)obj
;
1077 fprintf( stderr
, "Device dev=" );
1078 DUMP_LONG_LONG( device
->dev
);
1079 fprintf( stderr
, "\n" );
1082 static void device_destroy( struct object
*obj
)
1084 struct device
*device
= (struct device
*)obj
;
1087 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
1088 assert( list_empty(&device
->inode_hash
[i
]) );
1090 list_remove( &device
->entry
); /* remove it from the hash table */
1094 /****************************************************************/
1095 /* inode functions */
1097 /* close all pending file descriptors in the closed list */
1098 static void inode_close_pending( struct inode
*inode
, int keep_unlinks
)
1100 struct list
*ptr
= list_head( &inode
->closed
);
1104 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
1105 struct list
*next
= list_next( &inode
->closed
, ptr
);
1107 if (fd
->unix_fd
!= -1)
1109 close( fd
->unix_fd
);
1112 if (!keep_unlinks
|| !fd
->unlink
) /* get rid of it unless there's an unlink pending on that file */
1115 free( fd
->unix_name
);
1122 static void inode_dump( struct object
*obj
, int verbose
)
1124 struct inode
*inode
= (struct inode
*)obj
;
1125 fprintf( stderr
, "Inode device=%p ino=", inode
->device
);
1126 DUMP_LONG_LONG( inode
->ino
);
1127 fprintf( stderr
, "\n" );
1130 static void inode_destroy( struct object
*obj
)
1132 struct inode
*inode
= (struct inode
*)obj
;
1135 assert( list_empty(&inode
->open
) );
1136 assert( list_empty(&inode
->locks
) );
1138 list_remove( &inode
->entry
);
1140 while ((ptr
= list_head( &inode
->closed
)))
1142 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
1144 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1147 /* make sure it is still the same file */
1149 if (!stat( fd
->unix_name
, &st
) && st
.st_dev
== inode
->device
->dev
&& st
.st_ino
== inode
->ino
)
1151 if (S_ISDIR(st
.st_mode
)) rmdir( fd
->unix_name
);
1152 else unlink( fd
->unix_name
);
1155 free( fd
->unix_name
);
1158 release_object( inode
->device
);
1161 /* retrieve the inode object for a given fd, creating it if needed */
1162 static struct inode
*get_inode( dev_t dev
, ino_t ino
, int unix_fd
)
1164 struct device
*device
;
1165 struct inode
*inode
;
1166 unsigned int hash
= ino
% INODE_HASH_SIZE
;
1168 if (!(device
= get_device( dev
, unix_fd
))) return NULL
;
1170 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[hash
], struct inode
, entry
)
1172 if (inode
->ino
== ino
)
1174 release_object( device
);
1175 return (struct inode
*)grab_object( inode
);
1179 /* not found, create it */
1180 if ((inode
= alloc_object( &inode_ops
)))
1182 inode
->device
= device
;
1184 list_init( &inode
->open
);
1185 list_init( &inode
->locks
);
1186 list_init( &inode
->closed
);
1187 list_add_head( &device
->inode_hash
[hash
], &inode
->entry
);
1189 else release_object( device
);
1194 /* add fd to the inode list of file descriptors to close */
1195 static void inode_add_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
1197 if (!list_empty( &inode
->locks
))
1199 list_add_head( &inode
->closed
, &fd
->entry
);
1201 else if (fd
->unlink
) /* close the fd but keep the structure around for unlink */
1203 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1205 list_add_head( &inode
->closed
, &fd
->entry
);
1207 else /* no locks on this inode and no unlink, get rid of the fd */
1209 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1210 free( fd
->unix_name
);
1216 /****************************************************************/
1217 /* file lock functions */
1219 static void file_lock_dump( struct object
*obj
, int verbose
)
1221 struct file_lock
*lock
= (struct file_lock
*)obj
;
1222 fprintf( stderr
, "Lock %s fd=%p proc=%p start=",
1223 lock
->shared
? "shared" : "excl", lock
->fd
, lock
->process
);
1224 DUMP_LONG_LONG( lock
->start
);
1225 fprintf( stderr
, " end=" );
1226 DUMP_LONG_LONG( lock
->end
);
1227 fprintf( stderr
, "\n" );
1230 static int file_lock_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
1232 struct file_lock
*lock
= (struct file_lock
*)obj
;
1233 /* lock is signaled if it has lost its owner */
1234 return !lock
->process
;
1237 /* set (or remove) a Unix lock if possible for the given range */
1238 static int set_unix_lock( struct fd
*fd
, file_pos_t start
, file_pos_t end
, int type
)
1242 if (!fd
->fs_locks
) return 1; /* no fs locks possible for this fd */
1245 if (start
== end
) return 1; /* can't set zero-byte lock */
1246 if (start
> max_unix_offset
) return 1; /* ignore it */
1248 fl
.l_whence
= SEEK_SET
;
1250 if (!end
|| end
> max_unix_offset
) fl
.l_len
= 0;
1251 else fl
.l_len
= end
- start
;
1252 if (fcntl( fd
->unix_fd
, F_SETLK
, &fl
) != -1) return 1;
1257 /* check whether locks work at all on this file system */
1258 if (fcntl( fd
->unix_fd
, F_GETLK
, &fl
) != -1)
1260 set_error( STATUS_FILE_LOCK_CONFLICT
);
1267 /* no locking on this fs, just ignore it */
1271 set_error( STATUS_FILE_LOCK_CONFLICT
);
1274 /* this can happen if we try to set a write lock on a read-only file */
1275 /* try to at least grab a read lock */
1276 if (fl
.l_type
== F_WRLCK
)
1281 set_error( STATUS_ACCESS_DENIED
);
1287 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1288 /* in that case we shrink the limit and retry */
1289 if (max_unix_offset
> INT_MAX
)
1291 max_unix_offset
= INT_MAX
;
1302 /* check if interval [start;end) overlaps the lock */
1303 static inline int lock_overlaps( struct file_lock
*lock
, file_pos_t start
, file_pos_t end
)
1305 if (lock
->end
&& start
>= lock
->end
) return 0;
1306 if (end
&& lock
->start
>= end
) return 0;
1310 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1311 static void remove_unix_locks( struct fd
*fd
, file_pos_t start
, file_pos_t end
)
1319 } *first
, *cur
, *next
, *buffer
;
1324 if (!fd
->inode
) return;
1325 if (!fd
->fs_locks
) return;
1326 if (start
== end
|| start
> max_unix_offset
) return;
1327 if (!end
|| end
> max_unix_offset
) end
= max_unix_offset
+ 1;
1329 /* count the number of locks overlapping the specified area */
1331 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1333 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1334 if (lock
->start
== lock
->end
) continue;
1335 if (lock_overlaps( lock
, start
, end
)) count
++;
1338 if (!count
) /* no locks at all, we can unlock everything */
1340 set_unix_lock( fd
, start
, end
, F_UNLCK
);
1344 /* allocate space for the list of holes */
1345 /* max. number of holes is number of locks + 1 */
1347 if (!(buffer
= malloc( sizeof(*buffer
) * (count
+1) ))) return;
1351 first
->start
= start
;
1355 /* build a sorted list of unlocked holes in the specified area */
1357 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1359 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1360 if (lock
->start
== lock
->end
) continue;
1361 if (!lock_overlaps( lock
, start
, end
)) continue;
1363 /* go through all the holes touched by this lock */
1364 for (cur
= first
; cur
; cur
= cur
->next
)
1366 if (cur
->end
<= lock
->start
) continue; /* hole is before start of lock */
1367 if (lock
->end
&& cur
->start
>= lock
->end
) break; /* hole is after end of lock */
1369 /* now we know that lock is overlapping hole */
1371 if (cur
->start
>= lock
->start
) /* lock starts before hole, shrink from start */
1373 cur
->start
= lock
->end
;
1374 if (cur
->start
&& cur
->start
< cur
->end
) break; /* done with this lock */
1375 /* now hole is empty, remove it */
1376 if (cur
->next
) cur
->next
->prev
= cur
->prev
;
1377 if (cur
->prev
) cur
->prev
->next
= cur
->next
;
1378 else if (!(first
= cur
->next
)) goto done
; /* no more holes at all */
1380 else if (!lock
->end
|| cur
->end
<= lock
->end
) /* lock larger than hole, shrink from end */
1382 cur
->end
= lock
->start
;
1383 assert( cur
->start
< cur
->end
);
1385 else /* lock is in the middle of hole, split hole in two */
1388 next
->next
= cur
->next
;
1390 next
->start
= lock
->end
;
1391 next
->end
= cur
->end
;
1392 cur
->end
= lock
->start
;
1393 assert( next
->start
< next
->end
);
1394 assert( cur
->end
< next
->start
);
1396 break; /* done with this lock */
1401 /* clear Unix locks for all the holes */
1403 for (cur
= first
; cur
; cur
= cur
->next
)
1404 set_unix_lock( fd
, cur
->start
, cur
->end
, F_UNLCK
);
1410 /* create a new lock on a fd */
1411 static struct file_lock
*add_lock( struct fd
*fd
, int shared
, file_pos_t start
, file_pos_t end
)
1413 struct file_lock
*lock
;
1415 if (!(lock
= alloc_object( &file_lock_ops
))) return NULL
;
1416 lock
->shared
= shared
;
1417 lock
->start
= start
;
1420 lock
->process
= current
->process
;
1422 /* now try to set a Unix lock */
1423 if (!set_unix_lock( lock
->fd
, lock
->start
, lock
->end
, lock
->shared
? F_RDLCK
: F_WRLCK
))
1425 release_object( lock
);
1428 list_add_tail( &fd
->locks
, &lock
->fd_entry
);
1429 list_add_tail( &fd
->inode
->locks
, &lock
->inode_entry
);
1430 list_add_tail( &lock
->process
->locks
, &lock
->proc_entry
);
1434 /* remove an existing lock */
1435 static void remove_lock( struct file_lock
*lock
, int remove_unix
)
1437 struct inode
*inode
= lock
->fd
->inode
;
1439 list_remove( &lock
->fd_entry
);
1440 list_remove( &lock
->inode_entry
);
1441 list_remove( &lock
->proc_entry
);
1442 if (remove_unix
) remove_unix_locks( lock
->fd
, lock
->start
, lock
->end
);
1443 if (list_empty( &inode
->locks
)) inode_close_pending( inode
, 1 );
1444 lock
->process
= NULL
;
1445 wake_up( &lock
->obj
, 0 );
1446 release_object( lock
);
1449 /* remove all locks owned by a given process */
1450 void remove_process_locks( struct process
*process
)
1454 while ((ptr
= list_head( &process
->locks
)))
1456 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, proc_entry
);
1457 remove_lock( lock
, 1 ); /* this removes it from the list */
1461 /* remove all locks on a given fd */
1462 static void remove_fd_locks( struct fd
*fd
)
1464 file_pos_t start
= FILE_POS_T_MAX
, end
= 0;
1467 while ((ptr
= list_head( &fd
->locks
)))
1469 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1470 if (lock
->start
< start
) start
= lock
->start
;
1471 if (!lock
->end
|| lock
->end
> end
) end
= lock
->end
- 1;
1472 remove_lock( lock
, 0 );
1474 if (start
< end
) remove_unix_locks( fd
, start
, end
+ 1 );
1477 /* add a lock on an fd */
1478 /* returns handle to wait on */
1479 obj_handle_t
lock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
, int shared
, int wait
)
1482 file_pos_t end
= start
+ count
;
1484 if (!fd
->inode
) /* not a regular file */
1486 set_error( STATUS_INVALID_DEVICE_REQUEST
);
1490 /* don't allow wrapping locks */
1491 if (end
&& end
< start
)
1493 set_error( STATUS_INVALID_PARAMETER
);
1497 /* check if another lock on that file overlaps the area */
1498 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1500 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1501 if (!lock_overlaps( lock
, start
, end
)) continue;
1502 if (shared
&& (lock
->shared
|| lock
->fd
== fd
)) continue;
1506 set_error( STATUS_FILE_LOCK_CONFLICT
);
1509 set_error( STATUS_PENDING
);
1510 return alloc_handle( current
->process
, lock
, SYNCHRONIZE
, 0 );
1513 /* not found, add it */
1514 if (add_lock( fd
, shared
, start
, end
)) return 0;
1515 if (get_error() == STATUS_FILE_LOCK_CONFLICT
)
1517 /* Unix lock conflict -> tell client to wait and retry */
1518 if (wait
) set_error( STATUS_PENDING
);
1523 /* remove a lock on an fd */
1524 void unlock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
)
1527 file_pos_t end
= start
+ count
;
1529 /* find an existing lock with the exact same parameters */
1530 LIST_FOR_EACH( ptr
, &fd
->locks
)
1532 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1533 if ((lock
->start
== start
) && (lock
->end
== end
))
1535 remove_lock( lock
, 1 );
1539 set_error( STATUS_FILE_LOCK_CONFLICT
);
1543 /****************************************************************/
1544 /* file descriptor functions */
1546 static void fd_dump( struct object
*obj
, int verbose
)
1548 struct fd
*fd
= (struct fd
*)obj
;
1549 fprintf( stderr
, "Fd unix_fd=%d user=%p options=%08x", fd
->unix_fd
, fd
->user
, fd
->options
);
1550 if (fd
->inode
) fprintf( stderr
, " inode=%p unlink=%d", fd
->inode
, fd
->closed
->unlink
);
1551 fprintf( stderr
, "\n" );
1554 static void fd_destroy( struct object
*obj
)
1556 struct fd
*fd
= (struct fd
*)obj
;
1558 free_async_queue( &fd
->read_q
);
1559 free_async_queue( &fd
->write_q
);
1560 free_async_queue( &fd
->wait_q
);
1562 if (fd
->completion
) release_object( fd
->completion
);
1563 remove_fd_locks( fd
);
1564 list_remove( &fd
->inode_entry
);
1565 if (fd
->poll_index
!= -1) remove_poll_user( fd
, fd
->poll_index
);
1568 inode_add_closed_fd( fd
->inode
, fd
->closed
);
1569 release_object( fd
->inode
);
1571 else /* no inode, close it right away */
1573 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1574 free( fd
->unix_name
);
1578 /* check if the desired access is possible without violating */
1579 /* the sharing mode of other opens of the same file */
1580 static unsigned int check_sharing( struct fd
*fd
, unsigned int access
, unsigned int sharing
,
1581 unsigned int open_flags
, unsigned int options
)
1583 /* only a few access bits are meaningful wrt sharing */
1584 const unsigned int read_access
= FILE_READ_DATA
| FILE_EXECUTE
;
1585 const unsigned int write_access
= FILE_WRITE_DATA
| FILE_APPEND_DATA
;
1586 const unsigned int all_access
= read_access
| write_access
| DELETE
;
1588 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
1589 unsigned int existing_access
= 0;
1592 fd
->access
= access
;
1593 fd
->sharing
= sharing
;
1595 LIST_FOR_EACH( ptr
, &fd
->inode
->open
)
1597 struct fd
*fd_ptr
= LIST_ENTRY( ptr
, struct fd
, inode_entry
);
1600 /* if access mode is 0, sharing mode is ignored */
1601 if (fd_ptr
->access
& all_access
) existing_sharing
&= fd_ptr
->sharing
;
1602 existing_access
|= fd_ptr
->access
;
1606 if (((access
& read_access
) && !(existing_sharing
& FILE_SHARE_READ
)) ||
1607 ((access
& write_access
) && !(existing_sharing
& FILE_SHARE_WRITE
)) ||
1608 ((access
& DELETE
) && !(existing_sharing
& FILE_SHARE_DELETE
)))
1609 return STATUS_SHARING_VIOLATION
;
1610 if (((existing_access
& FILE_MAPPING_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) ||
1611 ((existing_access
& FILE_MAPPING_IMAGE
) && (access
& FILE_WRITE_DATA
)))
1612 return STATUS_SHARING_VIOLATION
;
1613 if ((existing_access
& FILE_MAPPING_IMAGE
) && (options
& FILE_DELETE_ON_CLOSE
))
1614 return STATUS_CANNOT_DELETE
;
1615 if ((existing_access
& FILE_MAPPING_ACCESS
) && (open_flags
& O_TRUNC
))
1616 return STATUS_USER_MAPPED_FILE
;
1617 if (!(access
& all_access
))
1618 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1619 if (((existing_access
& read_access
) && !(sharing
& FILE_SHARE_READ
)) ||
1620 ((existing_access
& write_access
) && !(sharing
& FILE_SHARE_WRITE
)) ||
1621 ((existing_access
& DELETE
) && !(sharing
& FILE_SHARE_DELETE
)))
1622 return STATUS_SHARING_VIOLATION
;
1626 /* set the events that select waits for on this fd */
1627 void set_fd_events( struct fd
*fd
, int events
)
1629 int user
= fd
->poll_index
;
1630 assert( poll_users
[user
] == fd
);
1632 set_fd_epoll_events( fd
, user
, events
);
1634 if (events
== -1) /* stop waiting on this fd completely */
1636 pollfd
[user
].fd
= -1;
1637 pollfd
[user
].events
= POLLERR
;
1638 pollfd
[user
].revents
= 0;
1640 else if (pollfd
[user
].fd
!= -1 || !pollfd
[user
].events
)
1642 pollfd
[user
].fd
= fd
->unix_fd
;
1643 pollfd
[user
].events
= events
;
1647 /* prepare an fd for unmounting its corresponding device */
1648 static inline void unmount_fd( struct fd
*fd
)
1650 assert( fd
->inode
);
1652 async_wake_up( &fd
->read_q
, STATUS_VOLUME_DISMOUNTED
);
1653 async_wake_up( &fd
->write_q
, STATUS_VOLUME_DISMOUNTED
);
1655 if (fd
->poll_index
!= -1) set_fd_events( fd
, -1 );
1657 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1660 fd
->no_fd_status
= STATUS_VOLUME_DISMOUNTED
;
1661 fd
->closed
->unix_fd
= -1;
1662 fd
->closed
->unlink
= 0;
1664 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1668 /* allocate an fd object, without setting the unix fd yet */
1669 static struct fd
*alloc_fd_object(void)
1671 struct fd
*fd
= alloc_object( &fd_ops
);
1673 if (!fd
) return NULL
;
1683 fd
->unix_name
= NULL
;
1687 fd
->poll_index
= -1;
1688 fd
->completion
= NULL
;
1690 init_async_queue( &fd
->read_q
);
1691 init_async_queue( &fd
->write_q
);
1692 init_async_queue( &fd
->wait_q
);
1693 list_init( &fd
->inode_entry
);
1694 list_init( &fd
->locks
);
1696 if ((fd
->poll_index
= add_poll_user( fd
)) == -1)
1698 release_object( fd
);
1704 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1705 struct fd
*alloc_pseudo_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
, unsigned int options
)
1707 struct fd
*fd
= alloc_object( &fd_ops
);
1709 if (!fd
) return NULL
;
1711 fd
->fd_ops
= fd_user_ops
;
1716 fd
->options
= options
;
1718 fd
->unix_name
= NULL
;
1723 fd
->poll_index
= -1;
1724 fd
->completion
= NULL
;
1726 fd
->no_fd_status
= STATUS_BAD_DEVICE_TYPE
;
1727 init_async_queue( &fd
->read_q
);
1728 init_async_queue( &fd
->write_q
);
1729 init_async_queue( &fd
->wait_q
);
1730 list_init( &fd
->inode_entry
);
1731 list_init( &fd
->locks
);
1735 /* duplicate an fd object for a different user */
1736 struct fd
*dup_fd_object( struct fd
*orig
, unsigned int access
, unsigned int sharing
, unsigned int options
)
1739 struct fd
*fd
= alloc_fd_object();
1741 if (!fd
) return NULL
;
1743 fd
->options
= options
;
1744 fd
->cacheable
= orig
->cacheable
;
1746 if (orig
->unix_name
)
1748 if (!(fd
->unix_name
= mem_alloc( strlen(orig
->unix_name
) + 1 ))) goto failed
;
1749 strcpy( fd
->unix_name
, orig
->unix_name
);
1754 struct closed_fd
*closed
= mem_alloc( sizeof(*closed
) );
1755 if (!closed
) goto failed
;
1756 if ((fd
->unix_fd
= dup( orig
->unix_fd
)) == -1)
1762 closed
->unix_fd
= fd
->unix_fd
;
1764 closed
->unix_name
= fd
->unix_name
;
1765 fd
->closed
= closed
;
1766 fd
->inode
= (struct inode
*)grab_object( orig
->inode
);
1767 list_add_head( &fd
->inode
->open
, &fd
->inode_entry
);
1768 if ((err
= check_sharing( fd
, access
, sharing
, 0, options
)))
1774 else if ((fd
->unix_fd
= dup( orig
->unix_fd
)) == -1)
1782 release_object( fd
);
1786 /* find an existing fd object that can be reused for a mapping */
1787 struct fd
*get_fd_object_for_mapping( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
1791 if (!fd
->inode
) return NULL
;
1793 LIST_FOR_EACH_ENTRY( fd_ptr
, &fd
->inode
->open
, struct fd
, inode_entry
)
1794 if (fd_ptr
->access
== access
&& fd_ptr
->sharing
== sharing
)
1795 return (struct fd
*)grab_object( fd_ptr
);
1800 /* sets the user of an fd that previously had no user */
1801 void set_fd_user( struct fd
*fd
, const struct fd_ops
*user_ops
, struct object
*user
)
1803 assert( fd
->fd_ops
== NULL
);
1804 fd
->fd_ops
= user_ops
;
1808 char *dup_fd_name( struct fd
*root
, const char *name
)
1812 if (!root
) return strdup( name
);
1813 if (!root
->unix_name
) return NULL
;
1816 if (name
[0] == '.' && (!name
[1] || name
[1] == '/')) name
++;
1818 if ((ret
= malloc( strlen(root
->unix_name
) + strlen(name
) + 2 )))
1820 strcpy( ret
, root
->unix_name
);
1821 if (name
[0] && name
[0] != '/') strcat( ret
, "/" );
1822 strcat( ret
, name
);
1827 /* open() wrapper that returns a struct fd with no fd user set */
1828 struct fd
*open_fd( struct fd
*root
, const char *name
, int flags
, mode_t
*mode
, unsigned int access
,
1829 unsigned int sharing
, unsigned int options
)
1832 struct closed_fd
*closed_fd
;
1838 if (((options
& FILE_DELETE_ON_CLOSE
) && !(access
& DELETE
)) ||
1839 ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_TRUNC
)))
1841 set_error( STATUS_INVALID_PARAMETER
);
1845 if (!(fd
= alloc_fd_object())) return NULL
;
1847 fd
->options
= options
;
1848 if (!(closed_fd
= mem_alloc( sizeof(*closed_fd
) )))
1850 release_object( fd
);
1856 if ((root_fd
= get_unix_fd( root
)) == -1) goto error
;
1857 if (fchdir( root_fd
) == -1)
1865 /* create the directory if needed */
1866 if ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_CREAT
))
1868 if (mkdir( name
, *mode
) == -1)
1870 if (errno
!= EEXIST
|| (flags
& O_EXCL
))
1876 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
1879 if ((access
& FILE_UNIX_WRITE_ACCESS
) && !(options
& FILE_DIRECTORY_FILE
))
1881 if (access
& FILE_UNIX_READ_ACCESS
) rw_mode
= O_RDWR
;
1882 else rw_mode
= O_WRONLY
;
1884 else rw_mode
= O_RDONLY
;
1886 if ((fd
->unix_fd
= open( name
, rw_mode
| (flags
& ~O_TRUNC
), *mode
)) == -1)
1888 /* if we tried to open a directory for write access, retry read-only */
1889 if (errno
== EISDIR
)
1891 if ((access
& FILE_UNIX_WRITE_ACCESS
) || (flags
& O_CREAT
))
1892 fd
->unix_fd
= open( name
, O_RDONLY
| (flags
& ~(O_TRUNC
| O_CREAT
| O_EXCL
)), *mode
);
1895 if (fd
->unix_fd
== -1)
1902 fd
->unix_name
= NULL
;
1903 if ((path
= dup_fd_name( root
, name
)))
1905 fd
->unix_name
= realpath( path
, NULL
);
1909 closed_fd
->unix_fd
= fd
->unix_fd
;
1910 closed_fd
->unlink
= 0;
1911 closed_fd
->unix_name
= fd
->unix_name
;
1912 fstat( fd
->unix_fd
, &st
);
1915 /* only bother with an inode for normal files and directories */
1916 if (S_ISREG(st
.st_mode
) || S_ISDIR(st
.st_mode
))
1919 struct inode
*inode
= get_inode( st
.st_dev
, st
.st_ino
, fd
->unix_fd
);
1923 /* we can close the fd because there are no others open on the same file,
1924 * otherwise we wouldn't have failed to allocate a new inode
1929 fd
->closed
= closed_fd
;
1930 fd
->cacheable
= !inode
->device
->removable
;
1931 list_add_head( &inode
->open
, &fd
->inode_entry
);
1934 /* check directory options */
1935 if ((options
& FILE_DIRECTORY_FILE
) && !S_ISDIR(st
.st_mode
))
1937 set_error( STATUS_NOT_A_DIRECTORY
);
1940 if ((options
& FILE_NON_DIRECTORY_FILE
) && S_ISDIR(st
.st_mode
))
1942 set_error( STATUS_FILE_IS_A_DIRECTORY
);
1945 if ((err
= check_sharing( fd
, access
, sharing
, flags
, options
)))
1951 /* can't unlink files if we don't have permission to access */
1952 if ((options
& FILE_DELETE_ON_CLOSE
) && !(flags
& O_CREAT
) &&
1953 !(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
1955 set_error( STATUS_CANNOT_DELETE
);
1959 fd
->closed
->unlink
= (options
& FILE_DELETE_ON_CLOSE
) ? -1 : 0;
1960 if (flags
& O_TRUNC
)
1962 if (S_ISDIR(st
.st_mode
))
1964 set_error( STATUS_OBJECT_NAME_COLLISION
);
1967 ftruncate( fd
->unix_fd
, 0 );
1970 else /* special file */
1972 if (options
& FILE_DELETE_ON_CLOSE
) /* we can't unlink special files */
1974 set_error( STATUS_INVALID_PARAMETER
);
1980 if (root_fd
!= -1) fchdir( server_dir_fd
); /* go back to the server dir */
1984 release_object( fd
);
1986 if (root_fd
!= -1) fchdir( server_dir_fd
); /* go back to the server dir */
1990 /* create an fd for an anonymous file */
1991 /* if the function fails the unix fd is closed */
1992 struct fd
*create_anonymous_fd( const struct fd_ops
*fd_user_ops
, int unix_fd
, struct object
*user
,
1993 unsigned int options
)
1995 struct fd
*fd
= alloc_fd_object();
1999 set_fd_user( fd
, fd_user_ops
, user
);
2000 fd
->unix_fd
= unix_fd
;
2001 fd
->options
= options
;
2008 /* retrieve the object that is using an fd */
2009 void *get_fd_user( struct fd
*fd
)
2014 /* retrieve the opening options for the fd */
2015 unsigned int get_fd_options( struct fd
*fd
)
2020 /* check if fd is in overlapped mode */
2021 int is_fd_overlapped( struct fd
*fd
)
2023 return !(fd
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
2026 /* retrieve the unix fd for an object */
2027 int get_unix_fd( struct fd
*fd
)
2029 if (fd
->unix_fd
== -1) set_error( fd
->no_fd_status
);
2033 /* check if two file descriptors point to the same file */
2034 int is_same_file_fd( struct fd
*fd1
, struct fd
*fd2
)
2036 return fd1
->inode
== fd2
->inode
;
2039 /* allow the fd to be cached (can't be reset once set) */
2040 void allow_fd_caching( struct fd
*fd
)
2045 /* check if fd is on a removable device */
2046 int is_fd_removable( struct fd
*fd
)
2048 return (fd
->inode
&& fd
->inode
->device
->removable
);
2051 /* set or clear the fd signaled state */
2052 void set_fd_signaled( struct fd
*fd
, int signaled
)
2054 if (fd
->comp_flags
& FILE_SKIP_SET_EVENT_ON_HANDLE
) return;
2055 fd
->signaled
= signaled
;
2056 if (signaled
) wake_up( fd
->user
, 0 );
2059 /* check if fd is signaled */
2060 int is_fd_signaled( struct fd
*fd
)
2062 return fd
->signaled
;
2065 /* handler for close_handle that refuses to close fd-associated handles in other processes */
2066 int fd_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
2068 return (!current
|| current
->process
== process
);
2071 /* check if events are pending and if yes return which one(s) */
2072 int check_fd_events( struct fd
*fd
, int events
)
2076 if (fd
->unix_fd
== -1) return POLLERR
;
2077 if (fd
->inode
) return events
; /* regular files are always signaled */
2079 pfd
.fd
= fd
->unix_fd
;
2080 pfd
.events
= events
;
2081 if (poll( &pfd
, 1, 0 ) <= 0) return 0;
2085 /* default signaled() routine for objects that poll() on an fd */
2086 int default_fd_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
2088 struct fd
*fd
= get_obj_fd( obj
);
2089 int ret
= fd
->signaled
;
2090 release_object( fd
);
2094 /* default map_access() routine for objects that behave like an fd */
2095 unsigned int default_fd_map_access( struct object
*obj
, unsigned int access
)
2097 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
2098 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
2099 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
2100 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
2101 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
2104 int default_fd_get_poll_events( struct fd
*fd
)
2108 if (async_waiting( &fd
->read_q
)) events
|= POLLIN
;
2109 if (async_waiting( &fd
->write_q
)) events
|= POLLOUT
;
2113 /* default handler for poll() events */
2114 void default_poll_event( struct fd
*fd
, int event
)
2116 if (event
& (POLLIN
| POLLERR
| POLLHUP
)) async_wake_up( &fd
->read_q
, STATUS_ALERTED
);
2117 if (event
& (POLLOUT
| POLLERR
| POLLHUP
)) async_wake_up( &fd
->write_q
, STATUS_ALERTED
);
2119 /* if an error occurred, stop polling this fd to avoid busy-looping */
2120 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
2121 else if (!fd
->inode
) set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
2124 void fd_queue_async( struct fd
*fd
, struct async
*async
, int type
)
2126 struct async_queue
*queue
;
2130 case ASYNC_TYPE_READ
:
2131 queue
= &fd
->read_q
;
2133 case ASYNC_TYPE_WRITE
:
2134 queue
= &fd
->write_q
;
2136 case ASYNC_TYPE_WAIT
:
2137 queue
= &fd
->wait_q
;
2144 queue_async( queue
, async
);
2146 if (type
!= ASYNC_TYPE_WAIT
)
2149 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
2150 else /* regular files are always ready for read and write */
2151 async_wake_up( queue
, STATUS_ALERTED
);
2155 void fd_async_wake_up( struct fd
*fd
, int type
, unsigned int status
)
2159 case ASYNC_TYPE_READ
:
2160 async_wake_up( &fd
->read_q
, status
);
2162 case ASYNC_TYPE_WRITE
:
2163 async_wake_up( &fd
->write_q
, status
);
2165 case ASYNC_TYPE_WAIT
:
2166 async_wake_up( &fd
->wait_q
, status
);
2173 void fd_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
2175 fd
->fd_ops
->reselect_async( fd
, queue
);
2178 void no_fd_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
2180 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2183 void default_fd_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
2185 fd_queue_async( fd
, async
, type
);
2186 set_error( STATUS_PENDING
);
2189 /* default reselect_async() fd routine */
2190 void default_fd_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
2192 if (queue
== &fd
->read_q
|| queue
== &fd
->write_q
)
2194 int poll_events
= fd
->fd_ops
->get_poll_events( fd
);
2195 int events
= check_fd_events( fd
, poll_events
);
2196 if (events
) fd
->fd_ops
->poll_event( fd
, events
);
2197 else set_fd_events( fd
, poll_events
);
2201 static inline int is_valid_mounted_device( struct stat
*st
)
2203 #if defined(linux) || defined(__sun__)
2204 return S_ISBLK( st
->st_mode
);
2206 /* disks are char devices on *BSD */
2207 return S_ISCHR( st
->st_mode
);
2211 /* close all Unix file descriptors on a device to allow unmounting it */
2212 static void unmount_device( struct fd
*device_fd
)
2216 struct device
*device
;
2217 struct inode
*inode
;
2219 int unix_fd
= get_unix_fd( device_fd
);
2221 if (unix_fd
== -1) return;
2223 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
2225 set_error( STATUS_INVALID_PARAMETER
);
2229 if (!(device
= get_device( st
.st_rdev
, -1 ))) return;
2231 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
2233 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[i
], struct inode
, entry
)
2235 LIST_FOR_EACH_ENTRY( fd
, &inode
->open
, struct fd
, inode_entry
)
2239 inode_close_pending( inode
, 0 );
2242 /* remove it from the hash table */
2243 list_remove( &device
->entry
);
2244 list_init( &device
->entry
);
2245 release_object( device
);
2248 /* default read() routine */
2249 int no_fd_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
2251 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2255 /* default write() routine */
2256 int no_fd_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
2258 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2262 /* default flush() routine */
2263 int no_fd_flush( struct fd
*fd
, struct async
*async
)
2265 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2269 /* default get_file_info() routine */
2270 void no_fd_get_file_info( struct fd
*fd
, obj_handle_t handle
, unsigned int info_class
)
2272 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2275 /* default get_file_info() routine */
2276 void default_fd_get_file_info( struct fd
*fd
, obj_handle_t handle
, unsigned int info_class
)
2280 case FileAccessInformation
:
2282 FILE_ACCESS_INFORMATION info
;
2283 if (get_reply_max_size() < sizeof(info
))
2285 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2288 info
.AccessFlags
= get_handle_access( current
->process
, handle
);
2289 set_reply_data( &info
, sizeof(info
) );
2292 case FileModeInformation
:
2294 FILE_MODE_INFORMATION info
;
2295 if (get_reply_max_size() < sizeof(info
))
2297 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2300 info
.Mode
= fd
->options
& ( FILE_WRITE_THROUGH
2301 | FILE_SEQUENTIAL_ONLY
2302 | FILE_NO_INTERMEDIATE_BUFFERING
2303 | FILE_SYNCHRONOUS_IO_ALERT
2304 | FILE_SYNCHRONOUS_IO_NONALERT
);
2305 set_reply_data( &info
, sizeof(info
) );
2308 case FileIoCompletionNotificationInformation
:
2310 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info
;
2311 if (get_reply_max_size() < sizeof(info
))
2313 set_error( STATUS_INFO_LENGTH_MISMATCH
);
2316 info
.Flags
= fd
->comp_flags
;
2317 set_reply_data( &info
, sizeof(info
) );
2321 set_error( STATUS_NOT_IMPLEMENTED
);
2325 /* default get_volume_info() routine */
2326 void no_fd_get_volume_info( struct fd
*fd
, unsigned int info_class
)
2328 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2331 /* default ioctl() routine */
2332 int no_fd_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2334 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2338 /* default ioctl() routine */
2339 int default_fd_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
2343 case FSCTL_DISMOUNT_VOLUME
:
2344 unmount_device( fd
);
2347 set_error( STATUS_NOT_SUPPORTED
);
2352 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2353 static struct fd
*get_handle_fd_obj( struct process
*process
, obj_handle_t handle
,
2354 unsigned int access
)
2356 struct fd
*fd
= NULL
;
2359 if ((obj
= get_handle_obj( process
, handle
, access
, NULL
)))
2361 fd
= get_obj_fd( obj
);
2362 release_object( obj
);
2367 /* set disposition for the fd */
2368 static void set_fd_disposition( struct fd
*fd
, int unlink
)
2374 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2378 if (fd
->unix_fd
== -1)
2380 set_error( fd
->no_fd_status
);
2384 if (fstat( fd
->unix_fd
, &st
) == -1)
2390 /* can't unlink special files */
2391 if (unlink
&& !S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
))
2393 set_error( STATUS_INVALID_PARAMETER
);
2397 /* can't unlink files we don't have permission to write */
2398 if (unlink
&& !(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) && !S_ISDIR(st
.st_mode
))
2400 set_error( STATUS_CANNOT_DELETE
);
2404 fd
->closed
->unlink
= unlink
? 1 : 0;
2405 if (fd
->options
& FILE_DELETE_ON_CLOSE
)
2406 fd
->closed
->unlink
= -1;
2409 /* set new name for the fd */
2410 static void set_fd_name( struct fd
*fd
, struct fd
*root
, const char *nameptr
,
2411 data_size_t len
, int create_link
, int replace
)
2413 struct inode
*inode
;
2414 struct stat st
, st2
;
2417 if (!fd
->inode
|| !fd
->unix_name
)
2419 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2422 if (fd
->unix_fd
== -1)
2424 set_error( fd
->no_fd_status
);
2428 if (!len
|| ((nameptr
[0] == '/') ^ !root
))
2430 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
2433 if (!(name
= mem_alloc( len
+ 1 ))) return;
2434 memcpy( name
, nameptr
, len
);
2439 char *combined_name
= dup_fd_name( root
, name
);
2442 set_error( STATUS_NO_MEMORY
);
2446 name
= combined_name
;
2449 /* when creating a hard link, source cannot be a dir */
2450 if (create_link
&& !fstat( fd
->unix_fd
, &st
) && S_ISDIR( st
.st_mode
))
2452 set_error( STATUS_FILE_IS_A_DIRECTORY
);
2456 if (!stat( name
, &st
))
2458 if (!fstat( fd
->unix_fd
, &st2
) && st
.st_ino
== st2
.st_ino
&& st
.st_dev
== st2
.st_dev
)
2460 if (create_link
&& !replace
) set_error( STATUS_OBJECT_NAME_COLLISION
);
2467 set_error( STATUS_OBJECT_NAME_COLLISION
);
2471 /* can't replace directories or special files */
2472 if (!S_ISREG( st
.st_mode
))
2474 set_error( STATUS_ACCESS_DENIED
);
2478 /* can't replace an opened file */
2479 if ((inode
= get_inode( st
.st_dev
, st
.st_ino
, -1 )))
2481 int is_empty
= list_empty( &inode
->open
);
2482 release_object( inode
);
2485 set_error( STATUS_ACCESS_DENIED
);
2490 /* link() expects that the target doesn't exist */
2491 /* rename() cannot replace files with directories */
2492 if (create_link
|| S_ISDIR( st2
.st_mode
))
2504 if (link( fd
->unix_name
, name
))
2510 if (rename( fd
->unix_name
, name
))
2516 if (is_file_executable( fd
->unix_name
) != is_file_executable( name
) && !fstat( fd
->unix_fd
, &st
))
2518 if (is_file_executable( name
))
2519 /* set executable bit where read bit is set */
2520 st
.st_mode
|= (st
.st_mode
& 0444) >> 2;
2522 st
.st_mode
&= ~0111;
2523 fchmod( fd
->unix_fd
, st
.st_mode
);
2526 free( fd
->unix_name
);
2527 fd
->closed
->unix_name
= fd
->unix_name
= realpath( name
, NULL
);
2530 set_error( STATUS_NO_MEMORY
);
2537 struct completion
*fd_get_completion( struct fd
*fd
, apc_param_t
*p_key
)
2539 *p_key
= fd
->comp_key
;
2540 return fd
->completion
? (struct completion
*)grab_object( fd
->completion
) : NULL
;
2543 void fd_copy_completion( struct fd
*src
, struct fd
*dst
)
2545 assert( !dst
->completion
);
2546 dst
->completion
= fd_get_completion( src
, &dst
->comp_key
);
2547 dst
->comp_flags
= src
->comp_flags
;
2550 /* flush a file buffers */
2553 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, 0 );
2554 struct async
*async
;
2558 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2560 reply
->event
= async_handoff( async
, fd
->fd_ops
->flush( fd
, async
), NULL
, 1 );
2561 release_object( async
);
2563 release_object( fd
);
2566 /* query file info */
2567 DECL_HANDLER(get_file_info
)
2569 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2573 fd
->fd_ops
->get_file_info( fd
, req
->handle
, req
->info_class
);
2574 release_object( fd
);
2578 /* query volume info */
2579 DECL_HANDLER(get_volume_info
)
2581 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2585 fd
->fd_ops
->get_volume_info( fd
, req
->info_class
);
2586 release_object( fd
);
2590 /* open a file object */
2591 DECL_HANDLER(open_file_object
)
2593 struct unicode_str name
= get_req_unicode_str();
2594 struct object
*obj
, *result
, *root
= NULL
;
2596 if (req
->rootdir
&& !(root
= get_handle_obj( current
->process
, req
->rootdir
, 0, NULL
))) return;
2598 obj
= open_named_object( root
, NULL
, &name
, req
->attributes
);
2599 if (root
) release_object( root
);
2602 if ((result
= obj
->ops
->open_file( obj
, req
->access
, req
->sharing
, req
->options
)))
2604 reply
->handle
= alloc_handle( current
->process
, result
, req
->access
, req
->attributes
);
2605 release_object( result
);
2607 release_object( obj
);
2610 /* get the Unix name from a file handle */
2611 DECL_HANDLER(get_handle_unix_name
)
2615 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2619 data_size_t name_len
= strlen( fd
->unix_name
);
2620 reply
->name_len
= name_len
;
2621 if (name_len
<= get_reply_max_size()) set_reply_data( fd
->unix_name
, name_len
);
2622 else set_error( STATUS_BUFFER_OVERFLOW
);
2624 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
2625 release_object( fd
);
2629 /* get a Unix fd to access a file */
2630 DECL_HANDLER(get_handle_fd
)
2634 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2636 int unix_fd
= get_unix_fd( fd
);
2637 reply
->cacheable
= fd
->cacheable
;
2640 reply
->type
= fd
->fd_ops
->get_fd_type( fd
);
2641 reply
->options
= fd
->options
;
2642 reply
->access
= get_handle_access( current
->process
, req
->handle
);
2643 send_client_fd( current
->process
, unix_fd
, req
->handle
);
2645 release_object( fd
);
2649 /* perform a read on a file object */
2652 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, FILE_READ_DATA
);
2653 struct async
*async
;
2657 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2659 reply
->wait
= async_handoff( async
, fd
->fd_ops
->read( fd
, async
, req
->pos
), NULL
, 0 );
2660 reply
->options
= fd
->options
;
2661 release_object( async
);
2663 release_object( fd
);
2666 /* perform a write on a file object */
2669 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, FILE_WRITE_DATA
);
2670 struct async
*async
;
2674 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2676 reply
->wait
= async_handoff( async
, fd
->fd_ops
->write( fd
, async
, req
->pos
), &reply
->size
, 0 );
2677 reply
->options
= fd
->options
;
2678 release_object( async
);
2680 release_object( fd
);
2683 /* perform an ioctl on a file */
2686 unsigned int access
= (req
->code
>> 14) & (FILE_READ_DATA
|FILE_WRITE_DATA
);
2687 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, access
);
2688 struct async
*async
;
2692 if ((async
= create_request_async( fd
, fd
->comp_flags
, &req
->async
)))
2694 reply
->wait
= async_handoff( async
, fd
->fd_ops
->ioctl( fd
, req
->code
, async
), NULL
, 0 );
2695 reply
->options
= fd
->options
;
2696 release_object( async
);
2698 release_object( fd
);
2701 /* create / reschedule an async I/O */
2702 DECL_HANDLER(register_async
)
2704 unsigned int access
;
2705 struct async
*async
;
2710 case ASYNC_TYPE_READ
:
2711 access
= FILE_READ_DATA
;
2713 case ASYNC_TYPE_WRITE
:
2714 access
= FILE_WRITE_DATA
;
2717 set_error( STATUS_INVALID_PARAMETER
);
2721 if ((fd
= get_handle_fd_obj( current
->process
, req
->async
.handle
, access
)))
2723 if (get_unix_fd( fd
) != -1 && (async
= create_async( fd
, current
, &req
->async
, NULL
)))
2725 fd
->fd_ops
->queue_async( fd
, async
, req
->type
, req
->count
);
2726 release_object( async
);
2728 release_object( fd
);
2732 /* attach completion object to a fd */
2733 DECL_HANDLER(set_completion_info
)
2735 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2739 if (is_fd_overlapped( fd
) && !fd
->completion
)
2741 fd
->completion
= get_completion_obj( current
->process
, req
->chandle
, IO_COMPLETION_MODIFY_STATE
);
2742 fd
->comp_key
= req
->ckey
;
2744 else set_error( STATUS_INVALID_PARAMETER
);
2745 release_object( fd
);
2749 /* push new completion msg into a completion queue attached to the fd */
2750 DECL_HANDLER(add_fd_completion
)
2752 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2755 if (fd
->completion
&& (req
->async
|| !(fd
->comp_flags
& FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
)))
2756 add_completion( fd
->completion
, fd
->comp_key
, req
->cvalue
, req
->status
, req
->information
);
2757 release_object( fd
);
2761 /* set fd completion information */
2762 DECL_HANDLER(set_fd_completion_mode
)
2764 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
2767 if (is_fd_overlapped( fd
))
2769 if (req
->flags
& FILE_SKIP_SET_EVENT_ON_HANDLE
)
2770 set_fd_signaled( fd
, 0 );
2771 /* removing flags is not allowed */
2772 fd
->comp_flags
|= req
->flags
& ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
2773 | FILE_SKIP_SET_EVENT_ON_HANDLE
2774 | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO
);
2777 set_error( STATUS_INVALID_PARAMETER
);
2778 release_object( fd
);
2782 /* set fd disposition information */
2783 DECL_HANDLER(set_fd_disp_info
)
2785 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, DELETE
);
2788 set_fd_disposition( fd
, req
->unlink
);
2789 release_object( fd
);
2793 /* set fd name information */
2794 DECL_HANDLER(set_fd_name_info
)
2796 struct fd
*fd
, *root_fd
= NULL
;
2802 if (!(root
= get_dir_obj( current
->process
, req
->rootdir
, 0 ))) return;
2803 root_fd
= get_obj_fd( (struct object
*)root
);
2804 release_object( root
);
2805 if (!root_fd
) return;
2808 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
2810 set_fd_name( fd
, root_fd
, get_req_data(), get_req_data_size(), req
->link
, req
->replace
);
2811 release_object( fd
);
2813 if (root_fd
) release_object( root_fd
);