Removed some more uses of the non-standard ICOM_THIS macro.
[wine.git] / server / fd.c
blob04e8df84c360170227de388e5b63c5854808581d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #ifdef HAVE_SYS_POLL_H
34 #include <sys/poll.h>
35 #endif
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <unistd.h>
41 #include "object.h"
42 #include "file.h"
43 #include "handle.h"
44 #include "process.h"
45 #include "request.h"
47 #include "winbase.h"
48 #include "winreg.h"
49 #include "winternl.h"
51 /* Because of the stupid Posix locking semantics, we need to keep
52 * track of all file descriptors referencing a given file, and not
53 * close a single one until all the locks are gone (sigh).
56 /* file descriptor object */
58 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
59 struct closed_fd
61 struct list entry; /* entry in inode closed list */
62 int fd; /* the unix file descriptor */
63 char unlink[1]; /* name to unlink on close (if any) */
66 struct fd
68 struct object obj; /* object header */
69 const struct fd_ops *fd_ops; /* file descriptor operations */
70 struct inode *inode; /* inode that this fd belongs to */
71 struct list inode_entry; /* entry in inode fd list */
72 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
73 struct object *user; /* object using this file descriptor */
74 struct list locks; /* list of locks on this fd */
75 unsigned int access; /* file access (GENERIC_READ/WRITE) */
76 unsigned int sharing; /* file sharing mode */
77 int unix_fd; /* unix file descriptor */
78 int fs_locks; /* can we use filesystem locks for this fd? */
79 int poll_index; /* index of fd in poll array */
82 static void fd_dump( struct object *obj, int verbose );
83 static void fd_destroy( struct object *obj );
85 static const struct object_ops fd_ops =
87 sizeof(struct fd), /* size */
88 fd_dump, /* dump */
89 no_add_queue, /* add_queue */
90 NULL, /* remove_queue */
91 NULL, /* signaled */
92 NULL, /* satisfied */
93 no_get_fd, /* get_fd */
94 fd_destroy /* destroy */
97 /* inode object */
99 struct inode
101 struct object obj; /* object header */
102 struct list entry; /* inode hash list entry */
103 unsigned int hash; /* hashing code */
104 dev_t dev; /* device number */
105 ino_t ino; /* inode number */
106 struct list open; /* list of open file descriptors */
107 struct list locks; /* list of file locks */
108 struct list closed; /* list of file descriptors to close at destroy time */
111 static void inode_dump( struct object *obj, int verbose );
112 static void inode_destroy( struct object *obj );
114 static const struct object_ops inode_ops =
116 sizeof(struct inode), /* size */
117 inode_dump, /* dump */
118 no_add_queue, /* add_queue */
119 NULL, /* remove_queue */
120 NULL, /* signaled */
121 NULL, /* satisfied */
122 no_get_fd, /* get_fd */
123 inode_destroy /* destroy */
126 /* file lock object */
128 struct file_lock
130 struct object obj; /* object header */
131 struct fd *fd; /* fd owning this lock */
132 struct list fd_entry; /* entry in list of locks on a given fd */
133 struct list inode_entry; /* entry in inode list of locks */
134 int shared; /* shared lock? */
135 file_pos_t start; /* locked region is interval [start;end) */
136 file_pos_t end;
137 struct process *process; /* process owning this lock */
138 struct list proc_entry; /* entry in list of locks owned by the process */
141 static void file_lock_dump( struct object *obj, int verbose );
142 static int file_lock_signaled( struct object *obj, struct thread *thread );
144 static const struct object_ops file_lock_ops =
146 sizeof(struct file_lock), /* size */
147 file_lock_dump, /* dump */
148 add_queue, /* add_queue */
149 remove_queue, /* remove_queue */
150 file_lock_signaled, /* signaled */
151 no_satisfied, /* satisfied */
152 no_get_fd, /* get_fd */
153 no_destroy /* destroy */
157 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
158 #define FILE_POS_T_MAX (~(file_pos_t)0)
160 static file_pos_t max_unix_offset = OFF_T_MAX;
162 #define DUMP_LONG_LONG(val) do { \
163 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
164 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
165 else \
166 fprintf( stderr, "%lx", (unsigned long)(val) ); \
167 } while (0)
171 /****************************************************************/
172 /* timeouts support */
174 struct timeout_user
176 struct timeout_user *next; /* next in sorted timeout list */
177 struct timeout_user *prev; /* prev in sorted timeout list */
178 struct timeval when; /* timeout expiry (absolute time) */
179 timeout_callback callback; /* callback function */
180 void *private; /* callback private data */
183 static struct timeout_user *timeout_head; /* sorted timeouts list head */
184 static struct timeout_user *timeout_tail; /* sorted timeouts list tail */
186 /* add a timeout user */
187 struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
189 struct timeout_user *user;
190 struct timeout_user *pos;
192 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
193 user->when = *when;
194 user->callback = func;
195 user->private = private;
197 /* Now insert it in the linked list */
199 for (pos = timeout_head; pos; pos = pos->next)
200 if (!time_before( &pos->when, when )) break;
202 if (pos) /* insert it before 'pos' */
204 if ((user->prev = pos->prev)) user->prev->next = user;
205 else timeout_head = user;
206 user->next = pos;
207 pos->prev = user;
209 else /* insert it at the tail */
211 user->next = NULL;
212 if (timeout_tail) timeout_tail->next = user;
213 else timeout_head = user;
214 user->prev = timeout_tail;
215 timeout_tail = user;
217 return user;
220 /* remove a timeout user */
221 void remove_timeout_user( struct timeout_user *user )
223 if (user->next) user->next->prev = user->prev;
224 else timeout_tail = user->prev;
225 if (user->prev) user->prev->next = user->next;
226 else timeout_head = user->next;
227 free( user );
230 /* add a timeout in milliseconds to an absolute time */
231 void add_timeout( struct timeval *when, int timeout )
233 if (timeout)
235 long sec = timeout / 1000;
236 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
238 when->tv_usec -= 1000000;
239 when->tv_sec++;
241 when->tv_sec += sec;
246 /****************************************************************/
247 /* poll support */
249 static struct fd **poll_users; /* users array */
250 static struct pollfd *pollfd; /* poll fd array */
251 static int nb_users; /* count of array entries actually in use */
252 static int active_users; /* current number of active users */
253 static int allocated_users; /* count of allocated entries in the array */
254 static struct fd **freelist; /* list of free entries in the array */
256 /* add a user in the poll array and return its index, or -1 on failure */
257 static int add_poll_user( struct fd *fd )
259 int ret;
260 if (freelist)
262 ret = freelist - poll_users;
263 freelist = (struct fd **)poll_users[ret];
265 else
267 if (nb_users == allocated_users)
269 struct fd **newusers;
270 struct pollfd *newpoll;
271 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
272 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
273 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
275 if (allocated_users)
276 poll_users = newusers;
277 else
278 free( newusers );
279 return -1;
281 poll_users = newusers;
282 pollfd = newpoll;
283 allocated_users = new_count;
285 ret = nb_users++;
287 pollfd[ret].fd = -1;
288 pollfd[ret].events = 0;
289 pollfd[ret].revents = 0;
290 poll_users[ret] = fd;
291 active_users++;
292 return ret;
295 /* remove a user from the poll list */
296 static void remove_poll_user( struct fd *fd, int user )
298 assert( user >= 0 );
299 assert( poll_users[user] == fd );
300 pollfd[user].fd = -1;
301 pollfd[user].events = 0;
302 pollfd[user].revents = 0;
303 poll_users[user] = (struct fd *)freelist;
304 freelist = &poll_users[user];
305 active_users--;
309 /* server main poll() loop */
310 void main_loop(void)
312 int ret;
314 while (active_users)
316 long diff = -1;
317 if (timeout_head)
319 struct timeout_user *first = timeout_head;
320 struct timeval now;
321 gettimeofday( &now, NULL );
323 /* first remove all expired timers from the list */
325 while (timeout_head && !time_before( &now, &timeout_head->when ))
326 timeout_head = timeout_head->next;
328 if (timeout_head)
330 if (timeout_head->prev)
332 timeout_head->prev->next = NULL;
333 timeout_head->prev = NULL;
335 else first = NULL; /* no timer removed */
337 else timeout_tail = NULL; /* all timers removed */
339 /* now call the callback for all the removed timers */
341 while (first)
343 struct timeout_user *next = first->next;
344 first->callback( first->private );
345 free( first );
346 first = next;
349 if (!active_users) break; /* last user removed by a timeout */
351 if (timeout_head)
353 diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
354 + (timeout_head->when.tv_usec - now.tv_usec) / 1000;
355 if (diff < 0) diff = 0;
359 ret = poll( pollfd, nb_users, diff );
360 if (ret > 0)
362 int i;
363 for (i = 0; i < nb_users; i++)
365 if (pollfd[i].revents)
367 fd_poll_event( poll_users[i], pollfd[i].revents );
368 if (!--ret) break;
376 /****************************************************************/
377 /* inode functions */
379 #define HASH_SIZE 37
381 static struct list inode_hash[HASH_SIZE];
383 /* close all pending file descriptors in the closed list */
384 static void inode_close_pending( struct inode *inode )
386 struct list *ptr = list_head( &inode->closed );
388 while (ptr)
390 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
391 struct list *next = list_next( &inode->closed, ptr );
393 if (fd->fd != -1)
395 close( fd->fd );
396 fd->fd = -1;
398 if (!fd->unlink) /* get rid of it unless there's an unlink pending on that file */
400 list_remove( ptr );
401 free( fd );
403 ptr = next;
408 static void inode_dump( struct object *obj, int verbose )
410 struct inode *inode = (struct inode *)obj;
411 fprintf( stderr, "Inode dev=" );
412 DUMP_LONG_LONG( inode->dev );
413 fprintf( stderr, " ino=" );
414 DUMP_LONG_LONG( inode->ino );
415 fprintf( stderr, "\n" );
418 static void inode_destroy( struct object *obj )
420 struct inode *inode = (struct inode *)obj;
421 struct list *ptr;
423 assert( list_empty(&inode->open) );
424 assert( list_empty(&inode->locks) );
426 list_remove( &inode->entry );
428 while ((ptr = list_head( &inode->closed )))
430 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
431 list_remove( ptr );
432 if (fd->fd != -1) close( fd->fd );
433 if (fd->unlink[0])
435 /* make sure it is still the same file */
436 struct stat st;
437 if (!stat( fd->unlink, &st ) && st.st_dev == inode->dev && st.st_ino == inode->ino)
439 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
440 else unlink( fd->unlink );
443 free( fd );
447 /* retrieve the inode object for a given fd, creating it if needed */
448 static struct inode *get_inode( dev_t dev, ino_t ino )
450 struct list *ptr;
451 struct inode *inode;
452 unsigned int hash = (dev ^ ino) % HASH_SIZE;
454 if (inode_hash[hash].next)
456 LIST_FOR_EACH( ptr, &inode_hash[hash] )
458 inode = LIST_ENTRY( ptr, struct inode, entry );
459 if (inode->dev == dev && inode->ino == ino)
460 return (struct inode *)grab_object( inode );
463 else list_init( &inode_hash[hash] );
465 /* not found, create it */
466 if ((inode = alloc_object( &inode_ops )))
468 inode->hash = hash;
469 inode->dev = dev;
470 inode->ino = ino;
471 list_init( &inode->open );
472 list_init( &inode->locks );
473 list_init( &inode->closed );
474 list_add_head( &inode_hash[hash], &inode->entry );
476 return inode;
479 /* add fd to the indoe list of file descriptors to close */
480 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
482 if (!list_empty( &inode->locks ))
484 list_add_head( &inode->closed, &fd->entry );
486 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
488 close( fd->fd );
489 fd->fd = -1;
490 list_add_head( &inode->closed, &fd->entry );
492 else /* no locks on this inode and no unlink, get rid of the fd */
494 close( fd->fd );
495 free( fd );
500 /****************************************************************/
501 /* file lock functions */
503 static void file_lock_dump( struct object *obj, int verbose )
505 struct file_lock *lock = (struct file_lock *)obj;
506 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
507 lock->shared ? "shared" : "excl", lock->fd, lock->process );
508 DUMP_LONG_LONG( lock->start );
509 fprintf( stderr, " end=" );
510 DUMP_LONG_LONG( lock->end );
511 fprintf( stderr, "\n" );
514 static int file_lock_signaled( struct object *obj, struct thread *thread )
516 struct file_lock *lock = (struct file_lock *)obj;
517 /* lock is signaled if it has lost its owner */
518 return !lock->process;
521 /* set (or remove) a Unix lock if possible for the given range */
522 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
524 struct flock fl;
526 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
527 for (;;)
529 if (start == end) return 1; /* can't set zero-byte lock */
530 if (start > max_unix_offset) return 1; /* ignore it */
531 fl.l_type = type;
532 fl.l_whence = SEEK_SET;
533 fl.l_start = start;
534 if (!end || end > max_unix_offset) fl.l_len = 0;
535 else fl.l_len = end - start;
536 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
538 switch(errno)
540 case EACCES:
541 /* check whether locks work at all on this file system */
542 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
544 set_error( STATUS_FILE_LOCK_CONFLICT );
545 return 0;
547 /* fall through */
548 case EIO:
549 case ENOLCK:
550 /* no locking on this fs, just ignore it */
551 fd->fs_locks = 0;
552 return 1;
553 case EAGAIN:
554 set_error( STATUS_FILE_LOCK_CONFLICT );
555 return 0;
556 case EBADF:
557 /* this can happen if we try to set a write lock on a read-only file */
558 /* we just ignore that error */
559 if (fl.l_type == F_WRLCK) return 1;
560 set_error( STATUS_ACCESS_DENIED );
561 return 0;
562 #ifdef EOVERFLOW
563 case EOVERFLOW:
564 #endif
565 case EINVAL:
566 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
567 /* in that case we shrink the limit and retry */
568 if (max_unix_offset > INT_MAX)
570 max_unix_offset = INT_MAX;
571 break; /* retry */
573 /* fall through */
574 default:
575 file_set_error();
576 return 0;
581 /* check if interval [start;end) overlaps the lock */
582 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
584 if (lock->end && start >= lock->end) return 0;
585 if (end && lock->start >= end) return 0;
586 return 1;
589 /* remove Unix locks for all bytes in the specified area that are no longer locked */
590 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
592 struct hole
594 struct hole *next;
595 struct hole *prev;
596 file_pos_t start;
597 file_pos_t end;
598 } *first, *cur, *next, *buffer;
600 struct list *ptr;
601 int count = 0;
603 if (!fd->inode) return;
604 if (!fd->fs_locks) return;
605 if (start == end || start > max_unix_offset) return;
606 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
608 /* count the number of locks overlapping the specified area */
610 LIST_FOR_EACH( ptr, &fd->inode->locks )
612 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
613 if (lock->start == lock->end) continue;
614 if (lock_overlaps( lock, start, end )) count++;
617 if (!count) /* no locks at all, we can unlock everything */
619 set_unix_lock( fd, start, end, F_UNLCK );
620 return;
623 /* allocate space for the list of holes */
624 /* max. number of holes is number of locks + 1 */
626 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
627 first = buffer;
628 first->next = NULL;
629 first->prev = NULL;
630 first->start = start;
631 first->end = end;
632 next = first + 1;
634 /* build a sorted list of unlocked holes in the specified area */
636 LIST_FOR_EACH( ptr, &fd->inode->locks )
638 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
639 if (lock->start == lock->end) continue;
640 if (!lock_overlaps( lock, start, end )) continue;
642 /* go through all the holes touched by this lock */
643 for (cur = first; cur; cur = cur->next)
645 if (cur->end <= lock->start) continue; /* hole is before start of lock */
646 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
648 /* now we know that lock is overlapping hole */
650 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
652 cur->start = lock->end;
653 if (cur->start && cur->start < cur->end) break; /* done with this lock */
654 /* now hole is empty, remove it */
655 if (cur->next) cur->next->prev = cur->prev;
656 if (cur->prev) cur->prev->next = cur->next;
657 else if (!(first = cur->next)) goto done; /* no more holes at all */
659 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
661 cur->end = lock->start;
662 assert( cur->start < cur->end );
664 else /* lock is in the middle of hole, split hole in two */
666 next->prev = cur;
667 next->next = cur->next;
668 cur->next = next;
669 next->start = lock->end;
670 next->end = cur->end;
671 cur->end = lock->start;
672 assert( next->start < next->end );
673 assert( cur->end < next->start );
674 next++;
675 break; /* done with this lock */
680 /* clear Unix locks for all the holes */
682 for (cur = first; cur; cur = cur->next)
683 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
685 done:
686 free( buffer );
689 /* create a new lock on a fd */
690 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
692 struct file_lock *lock;
694 if (!fd->inode) /* not a regular file */
696 set_error( STATUS_INVALID_HANDLE );
697 return NULL;
700 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
701 lock->shared = shared;
702 lock->start = start;
703 lock->end = end;
704 lock->fd = fd;
705 lock->process = current->process;
707 /* now try to set a Unix lock */
708 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
710 release_object( lock );
711 return NULL;
713 list_add_head( &fd->locks, &lock->fd_entry );
714 list_add_head( &fd->inode->locks, &lock->inode_entry );
715 list_add_head( &lock->process->locks, &lock->proc_entry );
716 return lock;
719 /* remove an existing lock */
720 static void remove_lock( struct file_lock *lock, int remove_unix )
722 struct inode *inode = lock->fd->inode;
724 list_remove( &lock->fd_entry );
725 list_remove( &lock->inode_entry );
726 list_remove( &lock->proc_entry );
727 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
728 if (list_empty( &inode->locks )) inode_close_pending( inode );
729 lock->process = NULL;
730 wake_up( &lock->obj, 0 );
731 release_object( lock );
734 /* remove all locks owned by a given process */
735 void remove_process_locks( struct process *process )
737 struct list *ptr;
739 while ((ptr = list_head( &process->locks )))
741 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
742 remove_lock( lock, 1 ); /* this removes it from the list */
746 /* remove all locks on a given fd */
747 static void remove_fd_locks( struct fd *fd )
749 file_pos_t start = FILE_POS_T_MAX, end = 0;
750 struct list *ptr;
752 while ((ptr = list_head( &fd->locks )))
754 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
755 if (lock->start < start) start = lock->start;
756 if (!lock->end || lock->end > end) end = lock->end - 1;
757 remove_lock( lock, 0 );
759 if (start < end) remove_unix_locks( fd, start, end + 1 );
762 /* add a lock on an fd */
763 /* returns handle to wait on */
764 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
766 struct list *ptr;
767 file_pos_t end = start + count;
769 /* don't allow wrapping locks */
770 if (end && end < start)
772 set_error( STATUS_INVALID_PARAMETER );
773 return 0;
776 /* check if another lock on that file overlaps the area */
777 LIST_FOR_EACH( ptr, &fd->inode->locks )
779 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
780 if (!lock_overlaps( lock, start, end )) continue;
781 if (lock->shared && shared) continue;
782 /* found one */
783 if (!wait)
785 set_error( STATUS_FILE_LOCK_CONFLICT );
786 return 0;
788 set_error( STATUS_PENDING );
789 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
792 /* not found, add it */
793 if (add_lock( fd, shared, start, end )) return 0;
794 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
796 /* Unix lock conflict -> tell client to wait and retry */
797 if (wait) set_error( STATUS_PENDING );
799 return 0;
802 /* remove a lock on an fd */
803 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
805 struct list *ptr;
806 file_pos_t end = start + count;
808 /* find an existing lock with the exact same parameters */
809 LIST_FOR_EACH( ptr, &fd->locks )
811 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
812 if ((lock->start == start) && (lock->end == end))
814 remove_lock( lock, 1 );
815 return;
818 set_error( STATUS_FILE_LOCK_CONFLICT );
822 /****************************************************************/
823 /* file descriptor functions */
825 static void fd_dump( struct object *obj, int verbose )
827 struct fd *fd = (struct fd *)obj;
828 fprintf( stderr, "Fd unix_fd=%d user=%p unlink='%s'\n",
829 fd->unix_fd, fd->user, fd->closed->unlink );
832 static void fd_destroy( struct object *obj )
834 struct fd *fd = (struct fd *)obj;
836 remove_fd_locks( fd );
837 list_remove( &fd->inode_entry );
838 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
839 if (fd->inode)
841 inode_add_closed_fd( fd->inode, fd->closed );
842 release_object( fd->inode );
844 else /* no inode, close it right away */
846 if (fd->unix_fd != -1) close( fd->unix_fd );
850 /* set the events that select waits for on this fd */
851 void set_fd_events( struct fd *fd, int events )
853 int user = fd->poll_index;
854 assert( poll_users[user] == fd );
855 if (events == -1) /* stop waiting on this fd completely */
857 pollfd[user].fd = -1;
858 pollfd[user].events = POLLERR;
859 pollfd[user].revents = 0;
861 else if (pollfd[user].fd != -1 || !pollfd[user].events)
863 pollfd[user].fd = fd->unix_fd;
864 pollfd[user].events = events;
868 /* allocate an fd object, without setting the unix fd yet */
869 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
871 struct fd *fd = alloc_object( &fd_ops );
873 if (!fd) return NULL;
875 fd->fd_ops = fd_user_ops;
876 fd->user = user;
877 fd->inode = NULL;
878 fd->closed = NULL;
879 fd->access = 0;
880 fd->sharing = 0;
881 fd->unix_fd = -1;
882 fd->fs_locks = 1;
883 fd->poll_index = -1;
884 list_init( &fd->inode_entry );
885 list_init( &fd->locks );
887 if ((fd->poll_index = add_poll_user( fd )) == -1)
889 release_object( fd );
890 return NULL;
892 return fd;
895 /* check if the desired access is possible without violating */
896 /* the sharing mode of other opens of the same file */
897 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
899 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
900 unsigned int existing_access = 0;
901 int unlink = 0;
902 struct list *ptr;
904 /* if access mode is 0, sharing mode is ignored */
905 if (!access) sharing = existing_sharing;
906 fd->access = access;
907 fd->sharing = sharing;
909 LIST_FOR_EACH( ptr, &fd->inode->open )
911 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
912 if (fd_ptr != fd)
914 existing_sharing &= fd_ptr->sharing;
915 existing_access |= fd_ptr->access;
916 if (fd_ptr->closed->unlink[0]) unlink = 1;
920 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
921 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
922 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
923 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
924 if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
925 if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
926 return 1;
929 /* open() wrapper using a struct fd */
930 /* the fd must have been created with alloc_fd */
931 /* on error the fd object is released */
932 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
933 unsigned int access, unsigned int sharing, unsigned int options )
935 struct stat st;
936 struct closed_fd *closed_fd;
937 const char *unlink_name = "";
939 assert( fd->unix_fd == -1 );
941 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
942 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
944 release_object( fd );
945 return NULL;
947 /* create the directory if needed */
948 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
950 if (mkdir( name, 0777 ) == -1)
952 if (errno != EEXIST || (flags & O_EXCL))
954 file_set_error();
955 release_object( fd );
956 free( closed_fd );
957 return NULL;
960 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
962 if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
964 file_set_error();
965 release_object( fd );
966 free( closed_fd );
967 return NULL;
969 closed_fd->fd = fd->unix_fd;
970 closed_fd->unlink[0] = 0;
971 fstat( fd->unix_fd, &st );
972 *mode = st.st_mode;
974 /* check directory options */
975 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
977 set_error( STATUS_NOT_A_DIRECTORY );
978 goto error;
980 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
982 set_error( STATUS_FILE_IS_A_DIRECTORY );
983 goto error;
986 /* only bother with an inode for normal files and directories */
987 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
989 struct inode *inode = get_inode( st.st_dev, st.st_ino );
991 if (!inode)
993 /* we can close the fd because there are no others open on the same file,
994 * otherwise we wouldn't have failed to allocate a new inode
996 goto error;
998 fd->inode = inode;
999 fd->closed = closed_fd;
1000 list_add_head( &inode->open, &fd->inode_entry );
1001 if (!check_sharing( fd, access, sharing ))
1003 release_object( fd );
1004 set_error( STATUS_SHARING_VIOLATION );
1005 return NULL;
1007 strcpy( closed_fd->unlink, unlink_name );
1008 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1010 else
1012 if (unlink_name[0]) /* we can't unlink special files */
1014 set_error( STATUS_INVALID_PARAMETER );
1015 goto error;
1017 free( closed_fd );
1019 return fd;
1021 error:
1022 release_object( fd );
1023 free( closed_fd );
1024 return NULL;
1027 /* create an fd for an anonymous file */
1028 /* if the function fails the unix fd is closed */
1029 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1031 struct fd *fd = alloc_fd( fd_user_ops, user );
1033 if (fd)
1035 fd->unix_fd = unix_fd;
1036 return fd;
1038 close( unix_fd );
1039 return NULL;
1042 /* retrieve the object that is using an fd */
1043 void *get_fd_user( struct fd *fd )
1045 return fd->user;
1048 /* retrieve the unix fd for an object */
1049 int get_unix_fd( struct fd *fd )
1051 return fd->unix_fd;
1054 /* check if two file descriptors point to the same file */
1055 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1057 return fd1->inode == fd2->inode;
1060 /* callback for event happening in the main poll() loop */
1061 void fd_poll_event( struct fd *fd, int event )
1063 return fd->fd_ops->poll_event( fd, event );
1066 /* check if events are pending and if yes return which one(s) */
1067 int check_fd_events( struct fd *fd, int events )
1069 struct pollfd pfd;
1071 pfd.fd = fd->unix_fd;
1072 pfd.events = events;
1073 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1074 return pfd.revents;
1077 /* default add_queue() routine for objects that poll() on an fd */
1078 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1080 struct fd *fd = get_obj_fd( obj );
1082 if (!fd) return 0;
1083 if (!obj->head) /* first on the queue */
1084 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1085 add_queue( obj, entry );
1086 release_object( fd );
1087 return 1;
1090 /* default remove_queue() routine for objects that poll() on an fd */
1091 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1093 struct fd *fd = get_obj_fd( obj );
1095 grab_object( obj );
1096 remove_queue( obj, entry );
1097 if (!obj->head) /* last on the queue is gone */
1098 set_fd_events( fd, 0 );
1099 release_object( obj );
1100 release_object( fd );
1103 /* default signaled() routine for objects that poll() on an fd */
1104 int default_fd_signaled( struct object *obj, struct thread *thread )
1106 struct fd *fd = get_obj_fd( obj );
1107 int events = fd->fd_ops->get_poll_events( fd );
1108 int ret = check_fd_events( fd, events ) != 0;
1110 if (ret)
1111 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
1112 else if (obj->head)
1113 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1115 release_object( fd );
1116 return ret;
1119 /* default handler for poll() events */
1120 void default_poll_event( struct fd *fd, int event )
1122 /* an error occurred, stop polling this fd to avoid busy-looping */
1123 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1124 wake_up( fd->user, 0 );
1127 /* default flush() routine */
1128 int no_flush( struct fd *fd, struct event **event )
1130 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1131 return 0;
1134 /* default get_file_info() routine */
1135 int no_get_file_info( struct fd *fd )
1137 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1138 return 0;
1141 /* default queue_async() routine */
1142 void no_queue_async( struct fd *fd, void* ptr, unsigned int status, int type, int count )
1144 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1147 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1148 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1149 unsigned int access )
1151 struct fd *fd = NULL;
1152 struct object *obj;
1154 if ((obj = get_handle_obj( process, handle, access, NULL )))
1156 fd = get_obj_fd( obj );
1157 release_object( obj );
1159 return fd;
1162 /* flush a file buffers */
1163 DECL_HANDLER(flush_file)
1165 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1166 struct event * event = NULL;
1168 if (fd)
1170 fd->fd_ops->flush( fd, &event );
1171 if( event )
1173 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1175 release_object( fd );
1179 /* get a Unix fd to access a file */
1180 DECL_HANDLER(get_handle_fd)
1182 struct fd *fd;
1184 reply->fd = -1;
1186 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1188 int unix_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1189 if (unix_fd != -1) reply->fd = unix_fd;
1190 else if (!get_error())
1192 assert( fd->unix_fd != -1 );
1193 send_client_fd( current->process, fd->unix_fd, req->handle );
1195 reply->flags = fd->fd_ops->get_file_info( fd );
1196 release_object( fd );
1200 /* create / reschedule an async I/O */
1201 DECL_HANDLER(register_async)
1203 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1206 * The queue_async method must do the following:
1208 * 1. Get the async_queue for the request of given type.
1209 * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
1210 * 3. If status is STATUS_PENDING:
1211 * a) If no async request found in step 2 (new request): call create_async() to initialize one.
1212 * b) Set request's status to STATUS_PENDING.
1213 * c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
1214 * Otherwise:
1215 * If the async request was found in step 2, destroy it by calling destroy_async().
1216 * 4. Carry out any operations necessary to adjust the object's poll events
1217 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1219 * See also the implementations in file.c, serial.c, and sock.c.
1222 if (fd)
1224 fd->fd_ops->queue_async( fd, req->overlapped, req->status, req->type, req->count );
1225 release_object( fd );