ole32: Don't send DragOver if we're about to drop or cancel.
[wine.git] / server / fd.c
blobe9953807716043b1766ad92c8195e9c7eeae17aa
1 /*
2 * Server-side file descriptor management
4 * Copyright (C) 2000, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_LINUX_MAJOR_H
41 #include <linux/major.h>
42 #endif
43 #ifdef HAVE_SYS_STATVFS_H
44 #include <sys/statvfs.h>
45 #endif
46 #ifdef HAVE_SYS_VFS_H
47 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
48 #define list SYSLIST
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
55 #include <sys/vfs.h>
56 #undef list
57 #undef list_next
58 #undef list_prev
59 #undef list_head
60 #undef list_tail
61 #undef list_move_tail
62 #undef list_remove
63 #endif
64 #ifdef HAVE_SYS_PARAM_H
65 #include <sys/param.h>
66 #endif
67 #ifdef HAVE_SYS_MOUNT_H
68 #include <sys/mount.h>
69 #endif
70 #ifdef HAVE_SYS_STATFS_H
71 #include <sys/statfs.h>
72 #endif
73 #ifdef HAVE_SYS_SYSCTL_H
74 #include <sys/sysctl.h>
75 #endif
76 #ifdef HAVE_SYS_EVENT_H
77 #include <sys/event.h>
78 #undef LIST_INIT
79 #undef LIST_ENTRY
80 #endif
81 #ifdef HAVE_STDINT_H
82 #include <stdint.h>
83 #endif
84 #include <sys/stat.h>
85 #include <sys/time.h>
86 #include <sys/types.h>
87 #include <unistd.h>
88 #ifdef HAVE_SYS_SYSCALL_H
89 #include <sys/syscall.h>
90 #endif
92 #include "ntstatus.h"
93 #define WIN32_NO_STATUS
94 #include "object.h"
95 #include "file.h"
96 #include "handle.h"
97 #include "process.h"
98 #include "request.h"
100 #include "winternl.h"
101 #include "winioctl.h"
103 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
104 # include <sys/epoll.h>
105 # define USE_EPOLL
106 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
107 # define USE_EPOLL
108 # define EPOLLIN POLLIN
109 # define EPOLLOUT POLLOUT
110 # define EPOLLERR POLLERR
111 # define EPOLLHUP POLLHUP
112 # define EPOLL_CTL_ADD 1
113 # define EPOLL_CTL_DEL 2
114 # define EPOLL_CTL_MOD 3
116 typedef union epoll_data
118 void *ptr;
119 int fd;
120 uint32_t u32;
121 uint64_t u64;
122 } epoll_data_t;
124 struct epoll_event
126 uint32_t events;
127 epoll_data_t data;
130 static inline int epoll_create( int size )
132 return syscall( 254 /*NR_epoll_create*/, size );
135 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
137 return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
140 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
142 return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
145 #endif /* linux && __i386__ && HAVE_STDINT_H */
147 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
148 # include <port.h>
149 # define USE_EVENT_PORTS
150 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
152 /* Because of the stupid Posix locking semantics, we need to keep
153 * track of all file descriptors referencing a given file, and not
154 * close a single one until all the locks are gone (sigh).
157 /* file descriptor object */
159 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
160 struct closed_fd
162 struct list entry; /* entry in inode closed list */
163 int unix_fd; /* the unix file descriptor */
164 int unlink; /* whether to unlink on close */
165 char *unix_name; /* name to unlink on close, points to parent fd unix_name */
168 struct fd
170 struct object obj; /* object header */
171 const struct fd_ops *fd_ops; /* file descriptor operations */
172 struct inode *inode; /* inode that this fd belongs to */
173 struct list inode_entry; /* entry in inode fd list */
174 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
175 struct object *user; /* object using this file descriptor */
176 struct list locks; /* list of locks on this fd */
177 unsigned int access; /* file access (FILE_READ_DATA etc.) */
178 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
179 unsigned int sharing; /* file sharing mode */
180 char *unix_name; /* unix file name */
181 int unix_fd; /* unix file descriptor */
182 unsigned int no_fd_status;/* status to return when unix_fd is -1 */
183 unsigned int cacheable :1;/* can the fd be cached on the client side? */
184 unsigned int signaled :1; /* is the fd signaled? */
185 unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
186 int poll_index; /* index of fd in poll array */
187 struct async_queue *read_q; /* async readers of this fd */
188 struct async_queue *write_q; /* async writers of this fd */
189 struct async_queue *wait_q; /* other async waiters of this fd */
190 struct completion *completion; /* completion object attached to this fd */
191 apc_param_t comp_key; /* completion key to set in completion events */
194 static void fd_dump( struct object *obj, int verbose );
195 static void fd_destroy( struct object *obj );
197 static const struct object_ops fd_ops =
199 sizeof(struct fd), /* size */
200 fd_dump, /* dump */
201 no_get_type, /* get_type */
202 no_add_queue, /* add_queue */
203 NULL, /* remove_queue */
204 NULL, /* signaled */
205 NULL, /* satisfied */
206 no_signal, /* signal */
207 no_get_fd, /* get_fd */
208 no_map_access, /* map_access */
209 default_get_sd, /* get_sd */
210 default_set_sd, /* set_sd */
211 no_lookup_name, /* lookup_name */
212 no_link_name, /* link_name */
213 NULL, /* unlink_name */
214 no_open_file, /* open_file */
215 no_close_handle, /* close_handle */
216 fd_destroy /* destroy */
219 /* device object */
221 #define DEVICE_HASH_SIZE 7
222 #define INODE_HASH_SIZE 17
224 struct device
226 struct object obj; /* object header */
227 struct list entry; /* entry in device hash list */
228 dev_t dev; /* device number */
229 int removable; /* removable device? (or -1 if unknown) */
230 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
233 static void device_dump( struct object *obj, int verbose );
234 static void device_destroy( struct object *obj );
236 static const struct object_ops device_ops =
238 sizeof(struct device), /* size */
239 device_dump, /* dump */
240 no_get_type, /* get_type */
241 no_add_queue, /* add_queue */
242 NULL, /* remove_queue */
243 NULL, /* signaled */
244 NULL, /* satisfied */
245 no_signal, /* signal */
246 no_get_fd, /* get_fd */
247 no_map_access, /* map_access */
248 default_get_sd, /* get_sd */
249 default_set_sd, /* set_sd */
250 no_lookup_name, /* lookup_name */
251 no_link_name, /* link_name */
252 NULL, /* unlink_name */
253 no_open_file, /* open_file */
254 no_close_handle, /* close_handle */
255 device_destroy /* destroy */
258 /* inode object */
260 struct inode
262 struct object obj; /* object header */
263 struct list entry; /* inode hash list entry */
264 struct device *device; /* device containing this inode */
265 ino_t ino; /* inode number */
266 struct list open; /* list of open file descriptors */
267 struct list locks; /* list of file locks */
268 struct list closed; /* list of file descriptors to close at destroy time */
271 static void inode_dump( struct object *obj, int verbose );
272 static void inode_destroy( struct object *obj );
274 static const struct object_ops inode_ops =
276 sizeof(struct inode), /* size */
277 inode_dump, /* dump */
278 no_get_type, /* get_type */
279 no_add_queue, /* add_queue */
280 NULL, /* remove_queue */
281 NULL, /* signaled */
282 NULL, /* satisfied */
283 no_signal, /* signal */
284 no_get_fd, /* get_fd */
285 no_map_access, /* map_access */
286 default_get_sd, /* get_sd */
287 default_set_sd, /* set_sd */
288 no_lookup_name, /* lookup_name */
289 no_link_name, /* link_name */
290 NULL, /* unlink_name */
291 no_open_file, /* open_file */
292 no_close_handle, /* close_handle */
293 inode_destroy /* destroy */
296 /* file lock object */
298 struct file_lock
300 struct object obj; /* object header */
301 struct fd *fd; /* fd owning this lock */
302 struct list fd_entry; /* entry in list of locks on a given fd */
303 struct list inode_entry; /* entry in inode list of locks */
304 int shared; /* shared lock? */
305 file_pos_t start; /* locked region is interval [start;end) */
306 file_pos_t end;
307 struct process *process; /* process owning this lock */
308 struct list proc_entry; /* entry in list of locks owned by the process */
311 static void file_lock_dump( struct object *obj, int verbose );
312 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
314 static const struct object_ops file_lock_ops =
316 sizeof(struct file_lock), /* size */
317 file_lock_dump, /* dump */
318 no_get_type, /* get_type */
319 add_queue, /* add_queue */
320 remove_queue, /* remove_queue */
321 file_lock_signaled, /* signaled */
322 no_satisfied, /* satisfied */
323 no_signal, /* signal */
324 no_get_fd, /* get_fd */
325 no_map_access, /* map_access */
326 default_get_sd, /* get_sd */
327 default_set_sd, /* set_sd */
328 no_lookup_name, /* lookup_name */
329 no_link_name, /* link_name */
330 NULL, /* unlink_name */
331 no_open_file, /* open_file */
332 no_close_handle, /* close_handle */
333 no_destroy /* destroy */
337 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
338 #define FILE_POS_T_MAX (~(file_pos_t)0)
340 static file_pos_t max_unix_offset = OFF_T_MAX;
342 #define DUMP_LONG_LONG(val) do { \
343 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
344 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
345 else \
346 fprintf( stderr, "%lx", (unsigned long)(val) ); \
347 } while (0)
351 /****************************************************************/
352 /* timeouts support */
354 struct timeout_user
356 struct list entry; /* entry in sorted timeout list */
357 timeout_t when; /* timeout expiry (absolute time) */
358 timeout_callback callback; /* callback function */
359 void *private; /* callback private data */
362 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
363 timeout_t current_time;
365 static inline void set_current_time(void)
367 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
368 struct timeval now;
369 gettimeofday( &now, NULL );
370 current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
373 /* add a timeout user */
374 struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
376 struct timeout_user *user;
377 struct list *ptr;
379 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
380 user->when = (when > 0) ? when : current_time - when;
381 user->callback = func;
382 user->private = private;
384 /* Now insert it in the linked list */
386 LIST_FOR_EACH( ptr, &timeout_list )
388 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
389 if (timeout->when >= user->when) break;
391 list_add_before( ptr, &user->entry );
392 return user;
395 /* remove a timeout user */
396 void remove_timeout_user( struct timeout_user *user )
398 list_remove( &user->entry );
399 free( user );
402 /* return a text description of a timeout for debugging purposes */
403 const char *get_timeout_str( timeout_t timeout )
405 static char buffer[64];
406 long secs, nsecs;
408 if (!timeout) return "0";
409 if (timeout == TIMEOUT_INFINITE) return "infinite";
411 if (timeout < 0) /* relative */
413 secs = -timeout / TICKS_PER_SEC;
414 nsecs = -timeout % TICKS_PER_SEC;
415 sprintf( buffer, "+%ld.%07ld", secs, nsecs );
417 else /* absolute */
419 secs = (timeout - current_time) / TICKS_PER_SEC;
420 nsecs = (timeout - current_time) % TICKS_PER_SEC;
421 if (nsecs < 0)
423 nsecs += TICKS_PER_SEC;
424 secs--;
426 if (secs >= 0)
427 sprintf( buffer, "%x%08x (+%ld.%07ld)",
428 (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
429 else
430 sprintf( buffer, "%x%08x (-%ld.%07ld)",
431 (unsigned int)(timeout >> 32), (unsigned int)timeout,
432 -(secs + 1), TICKS_PER_SEC - nsecs );
434 return buffer;
438 /****************************************************************/
439 /* poll support */
441 static struct fd **poll_users; /* users array */
442 static struct pollfd *pollfd; /* poll fd array */
443 static int nb_users; /* count of array entries actually in use */
444 static int active_users; /* current number of active users */
445 static int allocated_users; /* count of allocated entries in the array */
446 static struct fd **freelist; /* list of free entries in the array */
448 static int get_next_timeout(void);
450 static inline void fd_poll_event( struct fd *fd, int event )
452 fd->fd_ops->poll_event( fd, event );
455 #ifdef USE_EPOLL
457 static int epoll_fd = -1;
459 static inline void init_epoll(void)
461 epoll_fd = epoll_create( 128 );
464 /* set the events that epoll waits for on this fd; helper for set_fd_events */
465 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
467 struct epoll_event ev;
468 int ctl;
470 if (epoll_fd == -1) return;
472 if (events == -1) /* stop waiting on this fd completely */
474 if (pollfd[user].fd == -1) return; /* already removed */
475 ctl = EPOLL_CTL_DEL;
477 else if (pollfd[user].fd == -1)
479 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
480 ctl = EPOLL_CTL_ADD;
482 else
484 if (pollfd[user].events == events) return; /* nothing to do */
485 ctl = EPOLL_CTL_MOD;
488 ev.events = events;
489 memset(&ev.data, 0, sizeof(ev.data));
490 ev.data.u32 = user;
492 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
494 if (errno == ENOMEM) /* not enough memory, give up on epoll */
496 close( epoll_fd );
497 epoll_fd = -1;
499 else perror( "epoll_ctl" ); /* should not happen */
503 static inline void remove_epoll_user( struct fd *fd, int user )
505 if (epoll_fd == -1) return;
507 if (pollfd[user].fd != -1)
509 struct epoll_event dummy;
510 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
514 static inline void main_loop_epoll(void)
516 int i, ret, timeout;
517 struct epoll_event events[128];
519 assert( POLLIN == EPOLLIN );
520 assert( POLLOUT == EPOLLOUT );
521 assert( POLLERR == EPOLLERR );
522 assert( POLLHUP == EPOLLHUP );
524 if (epoll_fd == -1) return;
526 while (active_users)
528 timeout = get_next_timeout();
530 if (!active_users) break; /* last user removed by a timeout */
531 if (epoll_fd == -1) break; /* an error occurred with epoll */
533 ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
534 set_current_time();
536 /* put the events into the pollfd array first, like poll does */
537 for (i = 0; i < ret; i++)
539 int user = events[i].data.u32;
540 pollfd[user].revents = events[i].events;
543 /* read events from the pollfd array, as set_fd_events may modify them */
544 for (i = 0; i < ret; i++)
546 int user = events[i].data.u32;
547 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
552 #elif defined(HAVE_KQUEUE)
554 static int kqueue_fd = -1;
556 static inline void init_epoll(void)
558 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
559 int mib[2];
560 char release[32];
561 size_t len = sizeof(release);
563 mib[0] = CTL_KERN;
564 mib[1] = KERN_OSRELEASE;
565 if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
566 if (atoi(release) < 9) return;
567 #endif
568 kqueue_fd = kqueue();
571 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
573 struct kevent ev[2];
575 if (kqueue_fd == -1) return;
577 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
578 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
580 if (events == -1) /* stop waiting on this fd completely */
582 if (pollfd[user].fd == -1) return; /* already removed */
583 ev[0].flags |= EV_DELETE;
584 ev[1].flags |= EV_DELETE;
586 else if (pollfd[user].fd == -1)
588 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
589 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
590 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
592 else
594 if (pollfd[user].events == events) return; /* nothing to do */
595 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
596 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
599 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
601 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
603 close( kqueue_fd );
604 kqueue_fd = -1;
606 else perror( "kevent" ); /* should not happen */
610 static inline void remove_epoll_user( struct fd *fd, int user )
612 if (kqueue_fd == -1) return;
614 if (pollfd[user].fd != -1)
616 struct kevent ev[2];
618 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
619 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
620 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
624 static inline void main_loop_epoll(void)
626 int i, ret, timeout;
627 struct kevent events[128];
629 if (kqueue_fd == -1) return;
631 while (active_users)
633 timeout = get_next_timeout();
635 if (!active_users) break; /* last user removed by a timeout */
636 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
638 if (timeout != -1)
640 struct timespec ts;
642 ts.tv_sec = timeout / 1000;
643 ts.tv_nsec = (timeout % 1000) * 1000000;
644 ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
646 else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
648 set_current_time();
650 /* put the events into the pollfd array first, like poll does */
651 for (i = 0; i < ret; i++)
653 long user = (long)events[i].udata;
654 pollfd[user].revents = 0;
656 for (i = 0; i < ret; i++)
658 long user = (long)events[i].udata;
659 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
660 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
661 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
662 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
665 /* read events from the pollfd array, as set_fd_events may modify them */
666 for (i = 0; i < ret; i++)
668 long user = (long)events[i].udata;
669 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
670 pollfd[user].revents = 0;
675 #elif defined(USE_EVENT_PORTS)
677 static int port_fd = -1;
679 static inline void init_epoll(void)
681 port_fd = port_create();
684 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
686 int ret;
688 if (port_fd == -1) return;
690 if (events == -1) /* stop waiting on this fd completely */
692 if (pollfd[user].fd == -1) return; /* already removed */
693 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
695 else if (pollfd[user].fd == -1)
697 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
698 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
700 else
702 if (pollfd[user].events == events) return; /* nothing to do */
703 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
706 if (ret == -1)
708 if (errno == ENOMEM) /* not enough memory, give up on port_associate */
710 close( port_fd );
711 port_fd = -1;
713 else perror( "port_associate" ); /* should not happen */
717 static inline void remove_epoll_user( struct fd *fd, int user )
719 if (port_fd == -1) return;
721 if (pollfd[user].fd != -1)
723 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
727 static inline void main_loop_epoll(void)
729 int i, nget, ret, timeout;
730 port_event_t events[128];
732 if (port_fd == -1) return;
734 while (active_users)
736 timeout = get_next_timeout();
737 nget = 1;
739 if (!active_users) break; /* last user removed by a timeout */
740 if (port_fd == -1) break; /* an error occurred with event completion */
742 if (timeout != -1)
744 struct timespec ts;
746 ts.tv_sec = timeout / 1000;
747 ts.tv_nsec = (timeout % 1000) * 1000000;
748 ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, &ts );
750 else ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, NULL );
752 if (ret == -1) break; /* an error occurred with event completion */
754 set_current_time();
756 /* put the events into the pollfd array first, like poll does */
757 for (i = 0; i < nget; i++)
759 long user = (long)events[i].portev_user;
760 pollfd[user].revents = events[i].portev_events;
763 /* read events from the pollfd array, as set_fd_events may modify them */
764 for (i = 0; i < nget; i++)
766 long user = (long)events[i].portev_user;
767 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
768 /* if we are still interested, reassociate the fd */
769 if (pollfd[user].fd != -1) {
770 port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
776 #else /* HAVE_KQUEUE */
778 static inline void init_epoll(void) { }
779 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
780 static inline void remove_epoll_user( struct fd *fd, int user ) { }
781 static inline void main_loop_epoll(void) { }
783 #endif /* USE_EPOLL */
786 /* add a user in the poll array and return its index, or -1 on failure */
787 static int add_poll_user( struct fd *fd )
789 int ret;
790 if (freelist)
792 ret = freelist - poll_users;
793 freelist = (struct fd **)poll_users[ret];
795 else
797 if (nb_users == allocated_users)
799 struct fd **newusers;
800 struct pollfd *newpoll;
801 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
802 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
803 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
805 if (allocated_users)
806 poll_users = newusers;
807 else
808 free( newusers );
809 return -1;
811 poll_users = newusers;
812 pollfd = newpoll;
813 if (!allocated_users) init_epoll();
814 allocated_users = new_count;
816 ret = nb_users++;
818 pollfd[ret].fd = -1;
819 pollfd[ret].events = 0;
820 pollfd[ret].revents = 0;
821 poll_users[ret] = fd;
822 active_users++;
823 return ret;
826 /* remove a user from the poll list */
827 static void remove_poll_user( struct fd *fd, int user )
829 assert( user >= 0 );
830 assert( poll_users[user] == fd );
832 remove_epoll_user( fd, user );
833 pollfd[user].fd = -1;
834 pollfd[user].events = 0;
835 pollfd[user].revents = 0;
836 poll_users[user] = (struct fd *)freelist;
837 freelist = &poll_users[user];
838 active_users--;
841 /* process pending timeouts and return the time until the next timeout, in milliseconds */
842 static int get_next_timeout(void)
844 if (!list_empty( &timeout_list ))
846 struct list expired_list, *ptr;
848 /* first remove all expired timers from the list */
850 list_init( &expired_list );
851 while ((ptr = list_head( &timeout_list )) != NULL)
853 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
855 if (timeout->when <= current_time)
857 list_remove( &timeout->entry );
858 list_add_tail( &expired_list, &timeout->entry );
860 else break;
863 /* now call the callback for all the removed timers */
865 while ((ptr = list_head( &expired_list )) != NULL)
867 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
868 list_remove( &timeout->entry );
869 timeout->callback( timeout->private );
870 free( timeout );
873 if ((ptr = list_head( &timeout_list )) != NULL)
875 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
876 int diff = (timeout->when - current_time + 9999) / 10000;
877 if (diff < 0) diff = 0;
878 return diff;
881 return -1; /* no pending timeouts */
884 /* server main poll() loop */
885 void main_loop(void)
887 int i, ret, timeout;
889 set_current_time();
890 server_start_time = current_time;
892 main_loop_epoll();
893 /* fall through to normal poll loop */
895 while (active_users)
897 timeout = get_next_timeout();
899 if (!active_users) break; /* last user removed by a timeout */
901 ret = poll( pollfd, nb_users, timeout );
902 set_current_time();
904 if (ret > 0)
906 for (i = 0; i < nb_users; i++)
908 if (pollfd[i].revents)
910 fd_poll_event( poll_users[i], pollfd[i].revents );
911 if (!--ret) break;
919 /****************************************************************/
920 /* device functions */
922 static struct list device_hash[DEVICE_HASH_SIZE];
924 static int is_device_removable( dev_t dev, int unix_fd )
926 #if defined(linux) && defined(HAVE_FSTATFS)
927 struct statfs stfs;
929 /* check for floppy disk */
930 if (major(dev) == FLOPPY_MAJOR) return 1;
932 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
933 return (stfs.f_type == 0x9660 || /* iso9660 */
934 stfs.f_type == 0x9fa1 || /* supermount */
935 stfs.f_type == 0x15013346); /* udf */
936 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
937 struct statfs stfs;
939 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
940 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
941 #elif defined(__NetBSD__)
942 struct statvfs stfs;
944 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
945 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
946 #elif defined(sun)
947 # include <sys/dkio.h>
948 # include <sys/vtoc.h>
949 struct dk_cinfo dkinf;
950 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
951 return (dkinf.dki_ctype == DKC_CDROM ||
952 dkinf.dki_ctype == DKC_NCRFLOPPY ||
953 dkinf.dki_ctype == DKC_SMSFLOPPY ||
954 dkinf.dki_ctype == DKC_INTEL82072 ||
955 dkinf.dki_ctype == DKC_INTEL82077);
956 #else
957 return 0;
958 #endif
961 /* retrieve the device object for a given fd, creating it if needed */
962 static struct device *get_device( dev_t dev, int unix_fd )
964 struct device *device;
965 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
967 if (device_hash[hash].next)
969 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
970 if (device->dev == dev) return (struct device *)grab_object( device );
972 else list_init( &device_hash[hash] );
974 /* not found, create it */
976 if (unix_fd == -1) return NULL;
977 if ((device = alloc_object( &device_ops )))
979 device->dev = dev;
980 device->removable = is_device_removable( dev, unix_fd );
981 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
982 list_add_head( &device_hash[hash], &device->entry );
984 return device;
987 static void device_dump( struct object *obj, int verbose )
989 struct device *device = (struct device *)obj;
990 fprintf( stderr, "Device dev=" );
991 DUMP_LONG_LONG( device->dev );
992 fprintf( stderr, "\n" );
995 static void device_destroy( struct object *obj )
997 struct device *device = (struct device *)obj;
998 unsigned int i;
1000 for (i = 0; i < INODE_HASH_SIZE; i++)
1001 assert( list_empty(&device->inode_hash[i]) );
1003 list_remove( &device->entry ); /* remove it from the hash table */
1007 /****************************************************************/
1008 /* inode functions */
1010 /* close all pending file descriptors in the closed list */
1011 static void inode_close_pending( struct inode *inode, int keep_unlinks )
1013 struct list *ptr = list_head( &inode->closed );
1015 while (ptr)
1017 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1018 struct list *next = list_next( &inode->closed, ptr );
1020 if (fd->unix_fd != -1)
1022 close( fd->unix_fd );
1023 fd->unix_fd = -1;
1025 if (!keep_unlinks || !fd->unlink) /* get rid of it unless there's an unlink pending on that file */
1027 list_remove( ptr );
1028 free( fd->unix_name );
1029 free( fd );
1031 ptr = next;
1035 static void inode_dump( struct object *obj, int verbose )
1037 struct inode *inode = (struct inode *)obj;
1038 fprintf( stderr, "Inode device=%p ino=", inode->device );
1039 DUMP_LONG_LONG( inode->ino );
1040 fprintf( stderr, "\n" );
1043 static void inode_destroy( struct object *obj )
1045 struct inode *inode = (struct inode *)obj;
1046 struct list *ptr;
1048 assert( list_empty(&inode->open) );
1049 assert( list_empty(&inode->locks) );
1051 list_remove( &inode->entry );
1053 while ((ptr = list_head( &inode->closed )))
1055 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1056 list_remove( ptr );
1057 if (fd->unix_fd != -1) close( fd->unix_fd );
1058 if (fd->unlink)
1060 /* make sure it is still the same file */
1061 struct stat st;
1062 if (!stat( fd->unix_name, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
1064 if (S_ISDIR(st.st_mode)) rmdir( fd->unix_name );
1065 else unlink( fd->unix_name );
1068 free( fd->unix_name );
1069 free( fd );
1071 release_object( inode->device );
1074 /* retrieve the inode object for a given fd, creating it if needed */
1075 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
1077 struct device *device;
1078 struct inode *inode;
1079 unsigned int hash = ino % INODE_HASH_SIZE;
1081 if (!(device = get_device( dev, unix_fd ))) return NULL;
1083 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
1085 if (inode->ino == ino)
1087 release_object( device );
1088 return (struct inode *)grab_object( inode );
1092 /* not found, create it */
1093 if ((inode = alloc_object( &inode_ops )))
1095 inode->device = device;
1096 inode->ino = ino;
1097 list_init( &inode->open );
1098 list_init( &inode->locks );
1099 list_init( &inode->closed );
1100 list_add_head( &device->inode_hash[hash], &inode->entry );
1102 else release_object( device );
1104 return inode;
1107 /* add fd to the inode list of file descriptors to close */
1108 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
1110 if (!list_empty( &inode->locks ))
1112 list_add_head( &inode->closed, &fd->entry );
1114 else if (fd->unlink) /* close the fd but keep the structure around for unlink */
1116 if (fd->unix_fd != -1) close( fd->unix_fd );
1117 fd->unix_fd = -1;
1118 list_add_head( &inode->closed, &fd->entry );
1120 else /* no locks on this inode and no unlink, get rid of the fd */
1122 if (fd->unix_fd != -1) close( fd->unix_fd );
1123 free( fd->unix_name );
1124 free( fd );
1129 /****************************************************************/
1130 /* file lock functions */
1132 static void file_lock_dump( struct object *obj, int verbose )
1134 struct file_lock *lock = (struct file_lock *)obj;
1135 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
1136 lock->shared ? "shared" : "excl", lock->fd, lock->process );
1137 DUMP_LONG_LONG( lock->start );
1138 fprintf( stderr, " end=" );
1139 DUMP_LONG_LONG( lock->end );
1140 fprintf( stderr, "\n" );
1143 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
1145 struct file_lock *lock = (struct file_lock *)obj;
1146 /* lock is signaled if it has lost its owner */
1147 return !lock->process;
1150 /* set (or remove) a Unix lock if possible for the given range */
1151 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
1153 struct flock fl;
1155 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
1156 for (;;)
1158 if (start == end) return 1; /* can't set zero-byte lock */
1159 if (start > max_unix_offset) return 1; /* ignore it */
1160 fl.l_type = type;
1161 fl.l_whence = SEEK_SET;
1162 fl.l_start = start;
1163 if (!end || end > max_unix_offset) fl.l_len = 0;
1164 else fl.l_len = end - start;
1165 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
1167 switch(errno)
1169 case EACCES:
1170 /* check whether locks work at all on this file system */
1171 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
1173 set_error( STATUS_FILE_LOCK_CONFLICT );
1174 return 0;
1176 /* fall through */
1177 case EIO:
1178 case ENOLCK:
1179 case ENOTSUP:
1180 /* no locking on this fs, just ignore it */
1181 fd->fs_locks = 0;
1182 return 1;
1183 case EAGAIN:
1184 set_error( STATUS_FILE_LOCK_CONFLICT );
1185 return 0;
1186 case EBADF:
1187 /* this can happen if we try to set a write lock on a read-only file */
1188 /* try to at least grab a read lock */
1189 if (fl.l_type == F_WRLCK)
1191 type = F_RDLCK;
1192 break; /* retry */
1194 set_error( STATUS_ACCESS_DENIED );
1195 return 0;
1196 #ifdef EOVERFLOW
1197 case EOVERFLOW:
1198 #endif
1199 case EINVAL:
1200 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1201 /* in that case we shrink the limit and retry */
1202 if (max_unix_offset > INT_MAX)
1204 max_unix_offset = INT_MAX;
1205 break; /* retry */
1207 /* fall through */
1208 default:
1209 file_set_error();
1210 return 0;
1215 /* check if interval [start;end) overlaps the lock */
1216 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1218 if (lock->end && start >= lock->end) return 0;
1219 if (end && lock->start >= end) return 0;
1220 return 1;
1223 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1224 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1226 struct hole
1228 struct hole *next;
1229 struct hole *prev;
1230 file_pos_t start;
1231 file_pos_t end;
1232 } *first, *cur, *next, *buffer;
1234 struct list *ptr;
1235 int count = 0;
1237 if (!fd->inode) return;
1238 if (!fd->fs_locks) return;
1239 if (start == end || start > max_unix_offset) return;
1240 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1242 /* count the number of locks overlapping the specified area */
1244 LIST_FOR_EACH( ptr, &fd->inode->locks )
1246 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1247 if (lock->start == lock->end) continue;
1248 if (lock_overlaps( lock, start, end )) count++;
1251 if (!count) /* no locks at all, we can unlock everything */
1253 set_unix_lock( fd, start, end, F_UNLCK );
1254 return;
1257 /* allocate space for the list of holes */
1258 /* max. number of holes is number of locks + 1 */
1260 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1261 first = buffer;
1262 first->next = NULL;
1263 first->prev = NULL;
1264 first->start = start;
1265 first->end = end;
1266 next = first + 1;
1268 /* build a sorted list of unlocked holes in the specified area */
1270 LIST_FOR_EACH( ptr, &fd->inode->locks )
1272 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1273 if (lock->start == lock->end) continue;
1274 if (!lock_overlaps( lock, start, end )) continue;
1276 /* go through all the holes touched by this lock */
1277 for (cur = first; cur; cur = cur->next)
1279 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1280 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1282 /* now we know that lock is overlapping hole */
1284 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1286 cur->start = lock->end;
1287 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1288 /* now hole is empty, remove it */
1289 if (cur->next) cur->next->prev = cur->prev;
1290 if (cur->prev) cur->prev->next = cur->next;
1291 else if (!(first = cur->next)) goto done; /* no more holes at all */
1293 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1295 cur->end = lock->start;
1296 assert( cur->start < cur->end );
1298 else /* lock is in the middle of hole, split hole in two */
1300 next->prev = cur;
1301 next->next = cur->next;
1302 cur->next = next;
1303 next->start = lock->end;
1304 next->end = cur->end;
1305 cur->end = lock->start;
1306 assert( next->start < next->end );
1307 assert( cur->end < next->start );
1308 next++;
1309 break; /* done with this lock */
1314 /* clear Unix locks for all the holes */
1316 for (cur = first; cur; cur = cur->next)
1317 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1319 done:
1320 free( buffer );
1323 /* create a new lock on a fd */
1324 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1326 struct file_lock *lock;
1328 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1329 lock->shared = shared;
1330 lock->start = start;
1331 lock->end = end;
1332 lock->fd = fd;
1333 lock->process = current->process;
1335 /* now try to set a Unix lock */
1336 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1338 release_object( lock );
1339 return NULL;
1341 list_add_tail( &fd->locks, &lock->fd_entry );
1342 list_add_tail( &fd->inode->locks, &lock->inode_entry );
1343 list_add_tail( &lock->process->locks, &lock->proc_entry );
1344 return lock;
1347 /* remove an existing lock */
1348 static void remove_lock( struct file_lock *lock, int remove_unix )
1350 struct inode *inode = lock->fd->inode;
1352 list_remove( &lock->fd_entry );
1353 list_remove( &lock->inode_entry );
1354 list_remove( &lock->proc_entry );
1355 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1356 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1357 lock->process = NULL;
1358 wake_up( &lock->obj, 0 );
1359 release_object( lock );
1362 /* remove all locks owned by a given process */
1363 void remove_process_locks( struct process *process )
1365 struct list *ptr;
1367 while ((ptr = list_head( &process->locks )))
1369 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1370 remove_lock( lock, 1 ); /* this removes it from the list */
1374 /* remove all locks on a given fd */
1375 static void remove_fd_locks( struct fd *fd )
1377 file_pos_t start = FILE_POS_T_MAX, end = 0;
1378 struct list *ptr;
1380 while ((ptr = list_head( &fd->locks )))
1382 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1383 if (lock->start < start) start = lock->start;
1384 if (!lock->end || lock->end > end) end = lock->end - 1;
1385 remove_lock( lock, 0 );
1387 if (start < end) remove_unix_locks( fd, start, end + 1 );
1390 /* add a lock on an fd */
1391 /* returns handle to wait on */
1392 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1394 struct list *ptr;
1395 file_pos_t end = start + count;
1397 if (!fd->inode) /* not a regular file */
1399 set_error( STATUS_INVALID_DEVICE_REQUEST );
1400 return 0;
1403 /* don't allow wrapping locks */
1404 if (end && end < start)
1406 set_error( STATUS_INVALID_PARAMETER );
1407 return 0;
1410 /* check if another lock on that file overlaps the area */
1411 LIST_FOR_EACH( ptr, &fd->inode->locks )
1413 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1414 if (!lock_overlaps( lock, start, end )) continue;
1415 if (shared && (lock->shared || lock->fd == fd)) continue;
1416 /* found one */
1417 if (!wait)
1419 set_error( STATUS_FILE_LOCK_CONFLICT );
1420 return 0;
1422 set_error( STATUS_PENDING );
1423 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1426 /* not found, add it */
1427 if (add_lock( fd, shared, start, end )) return 0;
1428 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1430 /* Unix lock conflict -> tell client to wait and retry */
1431 if (wait) set_error( STATUS_PENDING );
1433 return 0;
1436 /* remove a lock on an fd */
1437 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1439 struct list *ptr;
1440 file_pos_t end = start + count;
1442 /* find an existing lock with the exact same parameters */
1443 LIST_FOR_EACH( ptr, &fd->locks )
1445 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1446 if ((lock->start == start) && (lock->end == end))
1448 remove_lock( lock, 1 );
1449 return;
1452 set_error( STATUS_FILE_LOCK_CONFLICT );
1456 /****************************************************************/
1457 /* file descriptor functions */
1459 static void fd_dump( struct object *obj, int verbose )
1461 struct fd *fd = (struct fd *)obj;
1462 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1463 if (fd->inode) fprintf( stderr, " inode=%p unlink=%d", fd->inode, fd->closed->unlink );
1464 fprintf( stderr, "\n" );
1467 static void fd_destroy( struct object *obj )
1469 struct fd *fd = (struct fd *)obj;
1471 free_async_queue( fd->read_q );
1472 free_async_queue( fd->write_q );
1473 free_async_queue( fd->wait_q );
1475 if (fd->completion) release_object( fd->completion );
1476 remove_fd_locks( fd );
1477 list_remove( &fd->inode_entry );
1478 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1479 if (fd->inode)
1481 inode_add_closed_fd( fd->inode, fd->closed );
1482 release_object( fd->inode );
1484 else /* no inode, close it right away */
1486 if (fd->unix_fd != -1) close( fd->unix_fd );
1487 free( fd->unix_name );
1491 /* check if the desired access is possible without violating */
1492 /* the sharing mode of other opens of the same file */
1493 static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
1494 unsigned int open_flags, unsigned int options )
1496 /* only a few access bits are meaningful wrt sharing */
1497 const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
1498 const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
1499 const unsigned int all_access = read_access | write_access | DELETE;
1501 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1502 unsigned int existing_access = 0;
1503 struct list *ptr;
1505 fd->access = access;
1506 fd->sharing = sharing;
1508 LIST_FOR_EACH( ptr, &fd->inode->open )
1510 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1511 if (fd_ptr != fd)
1513 /* if access mode is 0, sharing mode is ignored */
1514 if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
1515 existing_access |= fd_ptr->access;
1519 if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
1520 ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
1521 ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
1522 return STATUS_SHARING_VIOLATION;
1523 if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
1524 ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
1525 return STATUS_SHARING_VIOLATION;
1526 if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
1527 return STATUS_CANNOT_DELETE;
1528 if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
1529 return STATUS_USER_MAPPED_FILE;
1530 if (!(access & all_access))
1531 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1532 if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
1533 ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
1534 ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
1535 return STATUS_SHARING_VIOLATION;
1536 return 0;
1539 /* set the events that select waits for on this fd */
1540 void set_fd_events( struct fd *fd, int events )
1542 int user = fd->poll_index;
1543 assert( poll_users[user] == fd );
1545 set_fd_epoll_events( fd, user, events );
1547 if (events == -1) /* stop waiting on this fd completely */
1549 pollfd[user].fd = -1;
1550 pollfd[user].events = POLLERR;
1551 pollfd[user].revents = 0;
1553 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1555 pollfd[user].fd = fd->unix_fd;
1556 pollfd[user].events = events;
1560 /* prepare an fd for unmounting its corresponding device */
1561 static inline void unmount_fd( struct fd *fd )
1563 assert( fd->inode );
1565 async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
1566 async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
1568 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1570 if (fd->unix_fd != -1) close( fd->unix_fd );
1572 fd->unix_fd = -1;
1573 fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
1574 fd->closed->unix_fd = -1;
1575 fd->closed->unlink = 0;
1577 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1578 fd->fs_locks = 0;
1581 /* allocate an fd object, without setting the unix fd yet */
1582 static struct fd *alloc_fd_object(void)
1584 struct fd *fd = alloc_object( &fd_ops );
1586 if (!fd) return NULL;
1588 fd->fd_ops = NULL;
1589 fd->user = NULL;
1590 fd->inode = NULL;
1591 fd->closed = NULL;
1592 fd->access = 0;
1593 fd->options = 0;
1594 fd->sharing = 0;
1595 fd->unix_fd = -1;
1596 fd->unix_name = NULL;
1597 fd->cacheable = 0;
1598 fd->signaled = 1;
1599 fd->fs_locks = 1;
1600 fd->poll_index = -1;
1601 fd->read_q = NULL;
1602 fd->write_q = NULL;
1603 fd->wait_q = NULL;
1604 fd->completion = NULL;
1605 list_init( &fd->inode_entry );
1606 list_init( &fd->locks );
1608 if ((fd->poll_index = add_poll_user( fd )) == -1)
1610 release_object( fd );
1611 return NULL;
1613 return fd;
1616 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1617 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
1619 struct fd *fd = alloc_object( &fd_ops );
1621 if (!fd) return NULL;
1623 fd->fd_ops = fd_user_ops;
1624 fd->user = user;
1625 fd->inode = NULL;
1626 fd->closed = NULL;
1627 fd->access = 0;
1628 fd->options = options;
1629 fd->sharing = 0;
1630 fd->unix_name = NULL;
1631 fd->unix_fd = -1;
1632 fd->cacheable = 0;
1633 fd->signaled = 0;
1634 fd->fs_locks = 0;
1635 fd->poll_index = -1;
1636 fd->read_q = NULL;
1637 fd->write_q = NULL;
1638 fd->wait_q = NULL;
1639 fd->completion = NULL;
1640 fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
1641 list_init( &fd->inode_entry );
1642 list_init( &fd->locks );
1643 return fd;
1646 /* duplicate an fd object for a different user */
1647 struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
1649 unsigned int err;
1650 struct fd *fd = alloc_fd_object();
1652 if (!fd) return NULL;
1654 fd->options = options;
1655 fd->cacheable = orig->cacheable;
1657 if (orig->unix_name)
1659 if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
1660 strcpy( fd->unix_name, orig->unix_name );
1663 if (orig->inode)
1665 struct closed_fd *closed = mem_alloc( sizeof(*closed) );
1666 if (!closed) goto failed;
1667 if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1669 file_set_error();
1670 free( closed );
1671 goto failed;
1673 closed->unix_fd = fd->unix_fd;
1674 closed->unlink = 0;
1675 closed->unix_name = fd->unix_name;
1676 fd->closed = closed;
1677 fd->inode = (struct inode *)grab_object( orig->inode );
1678 list_add_head( &fd->inode->open, &fd->inode_entry );
1679 if ((err = check_sharing( fd, access, sharing, 0, options )))
1681 set_error( err );
1682 goto failed;
1685 else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1687 file_set_error();
1688 goto failed;
1690 return fd;
1692 failed:
1693 release_object( fd );
1694 return NULL;
1697 /* find an existing fd object that can be reused for a mapping */
1698 struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
1700 struct fd *fd_ptr;
1702 if (!fd->inode) return NULL;
1704 LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
1705 if (fd_ptr->access == access && fd_ptr->sharing == sharing)
1706 return (struct fd *)grab_object( fd_ptr );
1708 return NULL;
1711 /* set the status to return when the fd has no associated unix fd */
1712 void set_no_fd_status( struct fd *fd, unsigned int status )
1714 fd->no_fd_status = status;
1717 /* sets the user of an fd that previously had no user */
1718 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1720 assert( fd->fd_ops == NULL );
1721 fd->fd_ops = user_ops;
1722 fd->user = user;
1725 char *dup_fd_name( struct fd *root, const char *name )
1727 char *ret;
1729 if (!root) return strdup( name );
1730 if (!root->unix_name) return NULL;
1732 /* skip . prefix */
1733 if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
1735 if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
1737 strcpy( ret, root->unix_name );
1738 if (name[0] && name[0] != '/') strcat( ret, "/" );
1739 strcat( ret, name );
1741 return ret;
1744 /* open() wrapper that returns a struct fd with no fd user set */
1745 struct fd *open_fd( struct fd *root, const char *name, int flags, mode_t *mode, unsigned int access,
1746 unsigned int sharing, unsigned int options )
1748 struct stat st;
1749 struct closed_fd *closed_fd;
1750 struct fd *fd;
1751 int root_fd = -1;
1752 int rw_mode;
1754 if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
1755 ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
1757 set_error( STATUS_INVALID_PARAMETER );
1758 return NULL;
1761 if (!(fd = alloc_fd_object())) return NULL;
1763 fd->options = options;
1764 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
1766 release_object( fd );
1767 return NULL;
1770 if (root)
1772 if ((root_fd = get_unix_fd( root )) == -1) goto error;
1773 if (fchdir( root_fd ) == -1)
1775 file_set_error();
1776 root_fd = -1;
1777 goto error;
1781 /* create the directory if needed */
1782 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1784 if (mkdir( name, *mode ) == -1)
1786 if (errno != EEXIST || (flags & O_EXCL))
1788 file_set_error();
1789 goto error;
1792 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1795 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1797 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1798 else rw_mode = O_WRONLY;
1800 else rw_mode = O_RDONLY;
1802 fd->unix_name = dup_fd_name( root, name );
1804 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1806 /* if we tried to open a directory for write access, retry read-only */
1807 if (errno == EISDIR)
1809 if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
1810 fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
1813 if (fd->unix_fd == -1)
1815 file_set_error();
1816 goto error;
1820 closed_fd->unix_fd = fd->unix_fd;
1821 closed_fd->unlink = 0;
1822 closed_fd->unix_name = fd->unix_name;
1823 fstat( fd->unix_fd, &st );
1824 *mode = st.st_mode;
1826 /* only bother with an inode for normal files and directories */
1827 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1829 unsigned int err;
1830 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1832 if (!inode)
1834 /* we can close the fd because there are no others open on the same file,
1835 * otherwise we wouldn't have failed to allocate a new inode
1837 goto error;
1839 fd->inode = inode;
1840 fd->closed = closed_fd;
1841 fd->cacheable = !inode->device->removable;
1842 list_add_head( &inode->open, &fd->inode_entry );
1843 closed_fd = NULL;
1845 /* check directory options */
1846 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1848 set_error( STATUS_NOT_A_DIRECTORY );
1849 goto error;
1851 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1853 set_error( STATUS_FILE_IS_A_DIRECTORY );
1854 goto error;
1856 if ((err = check_sharing( fd, access, sharing, flags, options )))
1858 set_error( err );
1859 goto error;
1862 /* can't unlink files if we don't have permission to access */
1863 if ((options & FILE_DELETE_ON_CLOSE) && !(flags & O_CREAT) &&
1864 !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1866 set_error( STATUS_CANNOT_DELETE );
1867 goto error;
1870 fd->closed->unlink = (options & FILE_DELETE_ON_CLOSE) != 0;
1871 if (flags & O_TRUNC)
1873 if (S_ISDIR(st.st_mode))
1875 set_error( STATUS_OBJECT_NAME_COLLISION );
1876 goto error;
1878 ftruncate( fd->unix_fd, 0 );
1881 else /* special file */
1883 if (options & FILE_DELETE_ON_CLOSE) /* we can't unlink special files */
1885 set_error( STATUS_INVALID_PARAMETER );
1886 goto error;
1888 free( closed_fd );
1889 fd->cacheable = 1;
1891 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1892 return fd;
1894 error:
1895 release_object( fd );
1896 free( closed_fd );
1897 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1898 return NULL;
1901 /* create an fd for an anonymous file */
1902 /* if the function fails the unix fd is closed */
1903 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
1904 unsigned int options )
1906 struct fd *fd = alloc_fd_object();
1908 if (fd)
1910 set_fd_user( fd, fd_user_ops, user );
1911 fd->unix_fd = unix_fd;
1912 fd->options = options;
1913 return fd;
1915 close( unix_fd );
1916 return NULL;
1919 /* retrieve the object that is using an fd */
1920 void *get_fd_user( struct fd *fd )
1922 return fd->user;
1925 /* retrieve the opening options for the fd */
1926 unsigned int get_fd_options( struct fd *fd )
1928 return fd->options;
1931 /* retrieve the unix fd for an object */
1932 int get_unix_fd( struct fd *fd )
1934 if (fd->unix_fd == -1) set_error( fd->no_fd_status );
1935 return fd->unix_fd;
1938 /* check if two file descriptors point to the same file */
1939 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1941 return fd1->inode == fd2->inode;
1944 /* allow the fd to be cached (can't be reset once set) */
1945 void allow_fd_caching( struct fd *fd )
1947 fd->cacheable = 1;
1950 /* check if fd is on a removable device */
1951 int is_fd_removable( struct fd *fd )
1953 return (fd->inode && fd->inode->device->removable);
1956 /* set or clear the fd signaled state */
1957 void set_fd_signaled( struct fd *fd, int signaled )
1959 fd->signaled = signaled;
1960 if (signaled) wake_up( fd->user, 0 );
1963 /* check if fd is signaled */
1964 int is_fd_signaled( struct fd *fd )
1966 return fd->signaled;
1969 /* handler for close_handle that refuses to close fd-associated handles in other processes */
1970 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1972 return (!current || current->process == process);
1975 /* check if events are pending and if yes return which one(s) */
1976 int check_fd_events( struct fd *fd, int events )
1978 struct pollfd pfd;
1980 if (fd->unix_fd == -1) return POLLERR;
1981 if (fd->inode) return events; /* regular files are always signaled */
1983 pfd.fd = fd->unix_fd;
1984 pfd.events = events;
1985 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1986 return pfd.revents;
1989 /* default signaled() routine for objects that poll() on an fd */
1990 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
1992 struct fd *fd = get_obj_fd( obj );
1993 int ret = fd->signaled;
1994 release_object( fd );
1995 return ret;
1998 /* default map_access() routine for objects that behave like an fd */
1999 unsigned int default_fd_map_access( struct object *obj, unsigned int access )
2001 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2002 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2003 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2004 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2005 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2008 int default_fd_get_poll_events( struct fd *fd )
2010 int events = 0;
2012 if (async_waiting( fd->read_q )) events |= POLLIN;
2013 if (async_waiting( fd->write_q )) events |= POLLOUT;
2014 return events;
2017 /* default handler for poll() events */
2018 void default_poll_event( struct fd *fd, int event )
2020 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
2021 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
2023 /* if an error occurred, stop polling this fd to avoid busy-looping */
2024 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2025 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2028 struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type )
2030 struct async_queue *queue;
2031 struct async *async;
2033 switch (type)
2035 case ASYNC_TYPE_READ:
2036 if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
2037 queue = fd->read_q;
2038 break;
2039 case ASYNC_TYPE_WRITE:
2040 if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
2041 queue = fd->write_q;
2042 break;
2043 case ASYNC_TYPE_WAIT:
2044 if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
2045 queue = fd->wait_q;
2046 break;
2047 default:
2048 queue = NULL;
2049 assert(0);
2052 if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT)
2054 if (!fd->inode)
2055 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2056 else /* regular files are always ready for read and write */
2057 async_wake_up( queue, STATUS_ALERTED );
2059 return async;
2062 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2064 switch (type)
2066 case ASYNC_TYPE_READ:
2067 async_wake_up( fd->read_q, status );
2068 break;
2069 case ASYNC_TYPE_WRITE:
2070 async_wake_up( fd->write_q, status );
2071 break;
2072 case ASYNC_TYPE_WAIT:
2073 async_wake_up( fd->wait_q, status );
2074 break;
2075 default:
2076 assert(0);
2080 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2082 fd->fd_ops->reselect_async( fd, queue );
2085 void no_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
2087 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2090 void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
2092 struct async *async;
2094 if ((async = fd_queue_async( fd, data, type )))
2096 release_object( async );
2097 set_error( STATUS_PENDING );
2101 /* default reselect_async() fd routine */
2102 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2104 if (queue != fd->wait_q)
2106 int poll_events = fd->fd_ops->get_poll_events( fd );
2107 int events = check_fd_events( fd, poll_events );
2108 if (events) fd->fd_ops->poll_event( fd, events );
2109 else set_fd_events( fd, poll_events );
2113 /* default cancel_async() fd routine */
2114 int default_fd_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
2116 int n = 0;
2118 n += async_wake_up_by( fd->read_q, process, thread, iosb, STATUS_CANCELLED );
2119 n += async_wake_up_by( fd->write_q, process, thread, iosb, STATUS_CANCELLED );
2120 n += async_wake_up_by( fd->wait_q, process, thread, iosb, STATUS_CANCELLED );
2121 return n;
2124 static inline int is_valid_mounted_device( struct stat *st )
2126 #if defined(linux) || defined(__sun__)
2127 return S_ISBLK( st->st_mode );
2128 #else
2129 /* disks are char devices on *BSD */
2130 return S_ISCHR( st->st_mode );
2131 #endif
2134 /* close all Unix file descriptors on a device to allow unmounting it */
2135 static void unmount_device( struct fd *device_fd )
2137 unsigned int i;
2138 struct stat st;
2139 struct device *device;
2140 struct inode *inode;
2141 struct fd *fd;
2142 int unix_fd = get_unix_fd( device_fd );
2144 if (unix_fd == -1) return;
2146 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2148 set_error( STATUS_INVALID_PARAMETER );
2149 return;
2152 if (!(device = get_device( st.st_rdev, -1 ))) return;
2154 for (i = 0; i < INODE_HASH_SIZE; i++)
2156 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2158 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2160 unmount_fd( fd );
2162 inode_close_pending( inode, 0 );
2165 /* remove it from the hash table */
2166 list_remove( &device->entry );
2167 list_init( &device->entry );
2168 release_object( device );
2171 /* default read() routine */
2172 obj_handle_t no_fd_read( struct fd *fd, const async_data_t *async, int blocking, file_pos_t pos )
2174 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2175 return 0;
2178 /* default write() routine */
2179 obj_handle_t no_fd_write( struct fd *fd, const async_data_t *async, int blocking,
2180 file_pos_t pos, data_size_t *written )
2182 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2183 return 0;
2186 /* default flush() routine */
2187 obj_handle_t no_fd_flush( struct fd *fd, const async_data_t *async, int blocking )
2189 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2190 return 0;
2193 /* default ioctl() routine */
2194 obj_handle_t no_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async, int blocking )
2196 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2197 return 0;
2200 /* default ioctl() routine */
2201 obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async, int blocking )
2203 switch(code)
2205 case FSCTL_DISMOUNT_VOLUME:
2206 unmount_device( fd );
2207 return 0;
2208 default:
2209 set_error( STATUS_NOT_SUPPORTED );
2210 return 0;
2214 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2215 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2216 unsigned int access )
2218 struct fd *fd = NULL;
2219 struct object *obj;
2221 if ((obj = get_handle_obj( process, handle, access, NULL )))
2223 fd = get_obj_fd( obj );
2224 release_object( obj );
2226 return fd;
2229 /* set disposition for the fd */
2230 static void set_fd_disposition( struct fd *fd, int unlink )
2232 struct stat st;
2234 if (!fd->inode)
2236 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2237 return;
2240 if (fd->unix_fd == -1)
2242 set_error( fd->no_fd_status );
2243 return;
2246 if (fstat( fd->unix_fd, &st ) == -1)
2248 file_set_error();
2249 return;
2252 /* can't unlink special files */
2253 if (unlink && !S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
2255 set_error( STATUS_INVALID_PARAMETER );
2256 return;
2259 /* can't unlink files we don't have permission to access */
2260 if (unlink && !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2262 set_error( STATUS_CANNOT_DELETE );
2263 return;
2266 fd->closed->unlink = unlink || (fd->options & FILE_DELETE_ON_CLOSE);
2269 /* set new name for the fd */
2270 static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr,
2271 data_size_t len, int create_link )
2273 struct inode *inode;
2274 struct stat st;
2275 char *name;
2277 if (!fd->inode || !fd->unix_name)
2279 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2280 return;
2282 if (!len || ((nameptr[0] == '/') ^ !root))
2284 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
2285 return;
2287 if (!(name = mem_alloc( len + 1 ))) return;
2288 memcpy( name, nameptr, len );
2289 name[len] = 0;
2291 if (root)
2293 char *combined_name = dup_fd_name( root, name );
2294 if (!combined_name)
2296 set_error( STATUS_NO_MEMORY );
2297 goto failed;
2299 free( name );
2300 name = combined_name;
2303 /* when creating a hard link, source cannot be a dir */
2304 if (create_link && fd->unix_fd != -1 &&
2305 !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
2307 set_error( STATUS_FILE_IS_A_DIRECTORY );
2308 goto failed;
2311 if (!stat( name, &st ))
2313 /* can't replace directories or special files */
2314 if (!S_ISREG( st.st_mode ))
2316 set_error( STATUS_ACCESS_DENIED );
2317 goto failed;
2320 /* can't replace an opened file */
2321 if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
2323 int is_empty = list_empty( &inode->open );
2324 release_object( inode );
2325 if (!is_empty)
2327 set_error( STATUS_ACCESS_DENIED );
2328 goto failed;
2332 /* link() expects that the target doesn't exist */
2333 /* rename() cannot replace files with directories */
2334 if (create_link || (fd->unix_fd != -1 &&
2335 !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode )))
2337 if (unlink( name ))
2339 file_set_error();
2340 goto failed;
2345 if (create_link)
2347 if (link( fd->unix_name, name ))
2348 file_set_error();
2349 free( name );
2350 return;
2353 if (rename( fd->unix_name, name ))
2355 file_set_error();
2356 goto failed;
2359 free( fd->unix_name );
2360 fd->unix_name = name;
2361 fd->closed->unix_name = name;
2362 return;
2364 failed:
2365 free( name );
2368 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2370 *p_key = fd->comp_key;
2371 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2374 void fd_copy_completion( struct fd *src, struct fd *dst )
2376 assert( !dst->completion );
2377 dst->completion = fd_get_completion( src, &dst->comp_key );
2380 /* flush a file buffers */
2381 DECL_HANDLER(flush)
2383 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
2385 if (fd)
2387 reply->event = fd->fd_ops->flush( fd, &req->async, req->blocking );
2388 release_object( fd );
2392 /* open a file object */
2393 DECL_HANDLER(open_file_object)
2395 struct unicode_str name = get_req_unicode_str();
2396 struct object *obj, *result, *root = NULL;
2398 if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
2400 obj = open_named_object( root, NULL, &name, req->attributes );
2401 if (root) release_object( root );
2402 if (!obj) return;
2404 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2406 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2407 release_object( result );
2409 release_object( obj );
2412 /* get the Unix name from a file handle */
2413 DECL_HANDLER(get_handle_unix_name)
2415 struct fd *fd;
2417 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2419 if (fd->unix_name)
2421 data_size_t name_len = strlen( fd->unix_name );
2422 reply->name_len = name_len;
2423 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2424 else set_error( STATUS_BUFFER_OVERFLOW );
2426 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2427 release_object( fd );
2431 /* get a Unix fd to access a file */
2432 DECL_HANDLER(get_handle_fd)
2434 struct fd *fd;
2436 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2438 int unix_fd = get_unix_fd( fd );
2439 if (unix_fd != -1)
2441 reply->type = fd->fd_ops->get_fd_type( fd );
2442 reply->cacheable = fd->cacheable;
2443 reply->options = fd->options;
2444 reply->access = get_handle_access( current->process, req->handle );
2445 send_client_fd( current->process, unix_fd, req->handle );
2447 release_object( fd );
2451 /* perform a read on a file object */
2452 DECL_HANDLER(read)
2454 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
2456 if (fd)
2458 reply->wait = fd->fd_ops->read( fd, &req->async, req->blocking, req->pos );
2459 reply->options = fd->options;
2460 release_object( fd );
2464 /* perform a write on a file object */
2465 DECL_HANDLER(write)
2467 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
2469 if (fd)
2471 reply->wait = fd->fd_ops->write( fd, &req->async, req->blocking, req->pos, &reply->size );
2472 reply->options = fd->options;
2473 release_object( fd );
2477 /* perform an ioctl on a file */
2478 DECL_HANDLER(ioctl)
2480 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2481 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2483 if (fd)
2485 reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async, req->blocking );
2486 reply->options = fd->options;
2487 release_object( fd );
2491 /* create / reschedule an async I/O */
2492 DECL_HANDLER(register_async)
2494 unsigned int access;
2495 struct fd *fd;
2497 switch(req->type)
2499 case ASYNC_TYPE_READ:
2500 access = FILE_READ_DATA;
2501 break;
2502 case ASYNC_TYPE_WRITE:
2503 access = FILE_WRITE_DATA;
2504 break;
2505 default:
2506 set_error( STATUS_INVALID_PARAMETER );
2507 return;
2510 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2512 if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
2513 release_object( fd );
2517 /* cancels all async I/O */
2518 DECL_HANDLER(cancel_async)
2520 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2521 struct thread *thread = req->only_thread ? current : NULL;
2523 if (fd)
2525 int count = fd->fd_ops->cancel_async( fd, current->process, thread, req->iosb );
2526 if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
2527 release_object( fd );
2531 /* attach completion object to a fd */
2532 DECL_HANDLER(set_completion_info)
2534 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2536 if (fd)
2538 if (!(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) && !fd->completion)
2540 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2541 fd->comp_key = req->ckey;
2543 else set_error( STATUS_INVALID_PARAMETER );
2544 release_object( fd );
2548 /* push new completion msg into a completion queue attached to the fd */
2549 DECL_HANDLER(add_fd_completion)
2551 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2552 if (fd)
2554 if (fd->completion)
2555 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2556 release_object( fd );
2560 /* set fd disposition information */
2561 DECL_HANDLER(set_fd_disp_info)
2563 struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
2564 if (fd)
2566 set_fd_disposition( fd, req->unlink );
2567 release_object( fd );
2571 /* set fd name information */
2572 DECL_HANDLER(set_fd_name_info)
2574 struct fd *fd, *root_fd = NULL;
2576 if (req->rootdir)
2578 struct dir *root;
2580 if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
2581 root_fd = get_obj_fd( (struct object *)root );
2582 release_object( root );
2583 if (!root_fd) return;
2586 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2588 set_fd_name( fd, root_fd, get_req_data(), get_req_data_size(), req->link );
2589 release_object( fd );
2591 if (root_fd) release_object( root_fd );