Automatically detect whether the entry point is main or WinMain
[wine/multimedia.git] / server / fd.c
blob6848a2ed7635980e226bf147cfe2296e6a725da2
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 <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #ifdef HAVE_SYS_POLL_H
33 #include <sys/poll.h>
34 #endif
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
40 #include "object.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "process.h"
44 #include "request.h"
46 /* Because of the stupid Posix locking semantics, we need to keep
47 * track of all file descriptors referencing a given file, and not
48 * close a single one until all the locks are gone (sigh).
51 /* file descriptor object */
53 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
54 struct closed_fd
56 struct list entry; /* entry in inode closed list */
57 int fd; /* the unix file descriptor */
58 char unlink[1]; /* name to unlink on close (if any) */
61 struct fd
63 struct object obj; /* object header */
64 const struct fd_ops *fd_ops; /* file descriptor operations */
65 struct inode *inode; /* inode that this fd belongs to */
66 struct list inode_entry; /* entry in inode fd list */
67 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
68 struct object *user; /* object using this file descriptor */
69 struct list locks; /* list of locks on this fd */
70 unsigned int access; /* file access (GENERIC_READ/WRITE) */
71 unsigned int sharing; /* file sharing mode */
72 int unix_fd; /* unix file descriptor */
73 int fs_locks; /* can we use filesystem locks for this fd? */
74 int poll_index; /* index of fd in poll array */
77 static void fd_dump( struct object *obj, int verbose );
78 static void fd_destroy( struct object *obj );
80 static const struct object_ops fd_ops =
82 sizeof(struct fd), /* size */
83 fd_dump, /* dump */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 NULL, /* satisfied */
88 no_get_fd, /* get_fd */
89 fd_destroy /* destroy */
92 /* inode object */
94 struct inode
96 struct object obj; /* object header */
97 struct list entry; /* inode hash list entry */
98 unsigned int hash; /* hashing code */
99 dev_t dev; /* device number */
100 ino_t ino; /* inode number */
101 struct list open; /* list of open file descriptors */
102 struct list locks; /* list of file locks */
103 struct list closed; /* list of file descriptors to close at destroy time */
106 static void inode_dump( struct object *obj, int verbose );
107 static void inode_destroy( struct object *obj );
109 static const struct object_ops inode_ops =
111 sizeof(struct inode), /* size */
112 inode_dump, /* dump */
113 no_add_queue, /* add_queue */
114 NULL, /* remove_queue */
115 NULL, /* signaled */
116 NULL, /* satisfied */
117 no_get_fd, /* get_fd */
118 inode_destroy /* destroy */
121 /* file lock object */
123 struct file_lock
125 struct object obj; /* object header */
126 struct fd *fd; /* fd owning this lock */
127 struct list fd_entry; /* entry in list of locks on a given fd */
128 struct list inode_entry; /* entry in inode list of locks */
129 int shared; /* shared lock? */
130 file_pos_t start; /* locked region is interval [start;end) */
131 file_pos_t end;
132 struct process *process; /* process owning this lock */
133 struct list proc_entry; /* entry in list of locks owned by the process */
136 static void file_lock_dump( struct object *obj, int verbose );
137 static int file_lock_signaled( struct object *obj, struct thread *thread );
139 static const struct object_ops file_lock_ops =
141 sizeof(struct file_lock), /* size */
142 file_lock_dump, /* dump */
143 add_queue, /* add_queue */
144 remove_queue, /* remove_queue */
145 file_lock_signaled, /* signaled */
146 no_satisfied, /* satisfied */
147 no_get_fd, /* get_fd */
148 no_destroy /* destroy */
152 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
153 #define FILE_POS_T_MAX (~(file_pos_t)0)
155 static file_pos_t max_unix_offset = OFF_T_MAX;
157 #define DUMP_LONG_LONG(val) do { \
158 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
159 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
160 else \
161 fprintf( stderr, "%lx", (unsigned long)(val) ); \
162 } while (0)
166 /****************************************************************/
167 /* timeouts support */
169 struct timeout_user
171 struct timeout_user *next; /* next in sorted timeout list */
172 struct timeout_user *prev; /* prev in sorted timeout list */
173 struct timeval when; /* timeout expiry (absolute time) */
174 timeout_callback callback; /* callback function */
175 void *private; /* callback private data */
178 static struct timeout_user *timeout_head; /* sorted timeouts list head */
179 static struct timeout_user *timeout_tail; /* sorted timeouts list tail */
181 /* add a timeout user */
182 struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
184 struct timeout_user *user;
185 struct timeout_user *pos;
187 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
188 user->when = *when;
189 user->callback = func;
190 user->private = private;
192 /* Now insert it in the linked list */
194 for (pos = timeout_head; pos; pos = pos->next)
195 if (!time_before( &pos->when, when )) break;
197 if (pos) /* insert it before 'pos' */
199 if ((user->prev = pos->prev)) user->prev->next = user;
200 else timeout_head = user;
201 user->next = pos;
202 pos->prev = user;
204 else /* insert it at the tail */
206 user->next = NULL;
207 if (timeout_tail) timeout_tail->next = user;
208 else timeout_head = user;
209 user->prev = timeout_tail;
210 timeout_tail = user;
212 return user;
215 /* remove a timeout user */
216 void remove_timeout_user( struct timeout_user *user )
218 if (user->next) user->next->prev = user->prev;
219 else timeout_tail = user->prev;
220 if (user->prev) user->prev->next = user->next;
221 else timeout_head = user->next;
222 free( user );
225 /* add a timeout in milliseconds to an absolute time */
226 void add_timeout( struct timeval *when, int timeout )
228 if (timeout)
230 long sec = timeout / 1000;
231 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
233 when->tv_usec -= 1000000;
234 when->tv_sec++;
236 when->tv_sec += sec;
240 /* handle the next expired timeout */
241 inline static void handle_timeout(void)
243 struct timeout_user *user = timeout_head;
244 timeout_head = user->next;
245 if (user->next) user->next->prev = user->prev;
246 else timeout_tail = user->prev;
247 user->callback( user->private );
248 free( user );
252 /****************************************************************/
253 /* poll support */
255 static struct fd **poll_users; /* users array */
256 static struct pollfd *pollfd; /* poll fd array */
257 static int nb_users; /* count of array entries actually in use */
258 static int active_users; /* current number of active users */
259 static int allocated_users; /* count of allocated entries in the array */
260 static struct fd **freelist; /* list of free entries in the array */
262 /* add a user in the poll array and return its index, or -1 on failure */
263 static int add_poll_user( struct fd *fd )
265 int ret;
266 if (freelist)
268 ret = freelist - poll_users;
269 freelist = (struct fd **)poll_users[ret];
271 else
273 if (nb_users == allocated_users)
275 struct fd **newusers;
276 struct pollfd *newpoll;
277 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
278 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
279 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
281 if (allocated_users)
282 poll_users = newusers;
283 else
284 free( newusers );
285 return -1;
287 poll_users = newusers;
288 pollfd = newpoll;
289 allocated_users = new_count;
291 ret = nb_users++;
293 pollfd[ret].fd = -1;
294 pollfd[ret].events = 0;
295 pollfd[ret].revents = 0;
296 poll_users[ret] = fd;
297 active_users++;
298 return ret;
301 /* remove a user from the poll list */
302 static void remove_poll_user( struct fd *fd, int user )
304 assert( user >= 0 );
305 assert( poll_users[user] == fd );
306 pollfd[user].fd = -1;
307 pollfd[user].events = 0;
308 pollfd[user].revents = 0;
309 poll_users[user] = (struct fd *)freelist;
310 freelist = &poll_users[user];
311 active_users--;
315 /* server main poll() loop */
316 void main_loop(void)
318 int ret;
320 while (active_users)
322 long diff = -1;
323 if (timeout_head)
325 struct timeval now;
326 gettimeofday( &now, NULL );
327 while (timeout_head)
329 if (!time_before( &now, &timeout_head->when )) handle_timeout();
330 else
332 diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
333 + (timeout_head->when.tv_usec - now.tv_usec) / 1000;
334 break;
337 if (!active_users) break; /* last user removed by a timeout */
339 ret = poll( pollfd, nb_users, diff );
340 if (ret > 0)
342 int i;
343 for (i = 0; i < nb_users; i++)
345 if (pollfd[i].revents)
347 fd_poll_event( poll_users[i], pollfd[i].revents );
348 if (!--ret) break;
356 /****************************************************************/
357 /* inode functions */
359 #define HASH_SIZE 37
361 static struct list inode_hash[HASH_SIZE];
363 /* close all pending file descriptors in the closed list */
364 static void inode_close_pending( struct inode *inode )
366 struct list *ptr = list_head( &inode->closed );
368 while (ptr)
370 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
371 struct list *next = list_next( &inode->closed, ptr );
373 if (fd->fd != -1)
375 close( fd->fd );
376 fd->fd = -1;
378 if (!fd->unlink) /* get rid of it unless there's an unlink pending on that file */
380 list_remove( ptr );
381 free( fd );
383 ptr = next;
388 static void inode_dump( struct object *obj, int verbose )
390 struct inode *inode = (struct inode *)obj;
391 fprintf( stderr, "Inode dev=" );
392 DUMP_LONG_LONG( inode->dev );
393 fprintf( stderr, " ino=" );
394 DUMP_LONG_LONG( inode->ino );
395 fprintf( stderr, "\n" );
398 static void inode_destroy( struct object *obj )
400 struct inode *inode = (struct inode *)obj;
401 struct list *ptr;
403 assert( list_empty(&inode->open) );
404 assert( list_empty(&inode->locks) );
406 list_remove( &inode->entry );
408 while ((ptr = list_head( &inode->closed )))
410 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
411 list_remove( ptr );
412 if (fd->fd != -1) close( fd->fd );
413 if (fd->unlink[0])
415 /* make sure it is still the same file */
416 struct stat st;
417 if (!stat( fd->unlink, &st ) && st.st_dev == inode->dev && st.st_ino == inode->ino)
418 unlink( fd->unlink );
420 free( fd );
424 /* retrieve the inode object for a given fd, creating it if needed */
425 static struct inode *get_inode( dev_t dev, ino_t ino )
427 struct list *ptr;
428 struct inode *inode;
429 unsigned int hash = (dev ^ ino) % HASH_SIZE;
431 if (inode_hash[hash].next)
433 LIST_FOR_EACH( ptr, &inode_hash[hash] )
435 inode = LIST_ENTRY( ptr, struct inode, entry );
436 if (inode->dev == dev && inode->ino == ino)
437 return (struct inode *)grab_object( inode );
440 else list_init( &inode_hash[hash] );
442 /* not found, create it */
443 if ((inode = alloc_object( &inode_ops )))
445 inode->hash = hash;
446 inode->dev = dev;
447 inode->ino = ino;
448 list_init( &inode->open );
449 list_init( &inode->locks );
450 list_init( &inode->closed );
451 list_add_head( &inode_hash[hash], &inode->entry );
453 return inode;
456 /* add fd to the indoe list of file descriptors to close */
457 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
459 if (!list_empty( &inode->locks ))
461 list_add_head( &inode->closed, &fd->entry );
463 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
465 close( fd->fd );
466 fd->fd = -1;
467 list_add_head( &inode->closed, &fd->entry );
469 else /* no locks on this inode and no unlink, get rid of the fd */
471 close( fd->fd );
472 free( fd );
477 /****************************************************************/
478 /* file lock functions */
480 static void file_lock_dump( struct object *obj, int verbose )
482 struct file_lock *lock = (struct file_lock *)obj;
483 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
484 lock->shared ? "shared" : "excl", lock->fd, lock->process );
485 DUMP_LONG_LONG( lock->start );
486 fprintf( stderr, " end=" );
487 DUMP_LONG_LONG( lock->end );
488 fprintf( stderr, "\n" );
491 static int file_lock_signaled( struct object *obj, struct thread *thread )
493 struct file_lock *lock = (struct file_lock *)obj;
494 /* lock is signaled if it has lost its owner */
495 return !lock->process;
498 /* set (or remove) a Unix lock if possible for the given range */
499 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
501 struct flock fl;
503 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
504 for (;;)
506 if (start == end) return 1; /* can't set zero-byte lock */
507 if (start > max_unix_offset) return 1; /* ignore it */
508 fl.l_type = type;
509 fl.l_whence = SEEK_SET;
510 fl.l_start = start;
511 if (!end || end > max_unix_offset) fl.l_len = 0;
512 else fl.l_len = end - start;
513 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
515 switch(errno)
517 case EACCES:
518 /* check whether locks work at all on this file system */
519 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
521 set_error( STATUS_FILE_LOCK_CONFLICT );
522 return 0;
524 /* fall through */
525 case EIO:
526 case ENOLCK:
527 /* no locking on this fs, just ignore it */
528 fd->fs_locks = 0;
529 return 1;
530 case EAGAIN:
531 set_error( STATUS_FILE_LOCK_CONFLICT );
532 return 0;
533 case EBADF:
534 /* this can happen if we try to set a write lock on a read-only file */
535 /* we just ignore that error */
536 if (fl.l_type == F_WRLCK) return 1;
537 set_error( STATUS_ACCESS_DENIED );
538 return 0;
539 #ifdef EOVERFLOW
540 case EOVERFLOW:
541 #endif
542 case EINVAL:
543 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
544 /* in that case we shrink the limit and retry */
545 if (max_unix_offset > INT_MAX)
547 max_unix_offset = INT_MAX;
548 break; /* retry */
550 /* fall through */
551 default:
552 file_set_error();
553 return 0;
558 /* check if interval [start;end) overlaps the lock */
559 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
561 if (lock->end && start >= lock->end) return 0;
562 if (end && lock->start >= end) return 0;
563 return 1;
566 /* remove Unix locks for all bytes in the specified area that are no longer locked */
567 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
569 struct hole
571 struct hole *next;
572 struct hole *prev;
573 file_pos_t start;
574 file_pos_t end;
575 } *first, *cur, *next, *buffer;
577 struct list *ptr;
578 int count = 0;
580 if (!fd->inode) return;
581 if (!fd->fs_locks) return;
582 if (start == end || start > max_unix_offset) return;
583 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
585 /* count the number of locks overlapping the specified area */
587 LIST_FOR_EACH( ptr, &fd->inode->locks )
589 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
590 if (lock->start == lock->end) continue;
591 if (lock_overlaps( lock, start, end )) count++;
594 if (!count) /* no locks at all, we can unlock everything */
596 set_unix_lock( fd, start, end, F_UNLCK );
597 return;
600 /* allocate space for the list of holes */
601 /* max. number of holes is number of locks + 1 */
603 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
604 first = buffer;
605 first->next = NULL;
606 first->prev = NULL;
607 first->start = start;
608 first->end = end;
609 next = first + 1;
611 /* build a sorted list of unlocked holes in the specified area */
613 LIST_FOR_EACH( ptr, &fd->inode->locks )
615 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
616 if (lock->start == lock->end) continue;
617 if (!lock_overlaps( lock, start, end )) continue;
619 /* go through all the holes touched by this lock */
620 for (cur = first; cur; cur = cur->next)
622 if (cur->end <= lock->start) continue; /* hole is before start of lock */
623 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
625 /* now we know that lock is overlapping hole */
627 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
629 cur->start = lock->end;
630 if (cur->start && cur->start < cur->end) break; /* done with this lock */
631 /* now hole is empty, remove it */
632 if (cur->next) cur->next->prev = cur->prev;
633 if (cur->prev) cur->prev->next = cur->next;
634 else if (!(first = cur->next)) goto done; /* no more holes at all */
636 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
638 cur->end = lock->start;
639 assert( cur->start < cur->end );
641 else /* lock is in the middle of hole, split hole in two */
643 next->prev = cur;
644 next->next = cur->next;
645 cur->next = next;
646 next->start = lock->end;
647 next->end = cur->end;
648 cur->end = lock->start;
649 assert( next->start < next->end );
650 assert( cur->end < next->start );
651 next++;
652 break; /* done with this lock */
657 /* clear Unix locks for all the holes */
659 for (cur = first; cur; cur = cur->next)
660 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
662 done:
663 free( buffer );
666 /* create a new lock on a fd */
667 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
669 struct file_lock *lock;
671 if (!fd->inode) /* not a regular file */
673 set_error( STATUS_INVALID_HANDLE );
674 return NULL;
677 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
678 lock->shared = shared;
679 lock->start = start;
680 lock->end = end;
681 lock->fd = fd;
682 lock->process = current->process;
684 /* now try to set a Unix lock */
685 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
687 release_object( lock );
688 return NULL;
690 list_add_head( &fd->locks, &lock->fd_entry );
691 list_add_head( &fd->inode->locks, &lock->inode_entry );
692 list_add_head( &lock->process->locks, &lock->proc_entry );
693 return lock;
696 /* remove an existing lock */
697 static void remove_lock( struct file_lock *lock, int remove_unix )
699 struct inode *inode = lock->fd->inode;
701 list_remove( &lock->fd_entry );
702 list_remove( &lock->inode_entry );
703 list_remove( &lock->proc_entry );
704 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
705 if (list_empty( &inode->locks )) inode_close_pending( inode );
706 lock->process = NULL;
707 wake_up( &lock->obj, 0 );
708 release_object( lock );
711 /* remove all locks owned by a given process */
712 void remove_process_locks( struct process *process )
714 struct list *ptr;
716 while ((ptr = list_head( &process->locks )))
718 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
719 remove_lock( lock, 1 ); /* this removes it from the list */
723 /* remove all locks on a given fd */
724 static void remove_fd_locks( struct fd *fd )
726 file_pos_t start = FILE_POS_T_MAX, end = 0;
727 struct list *ptr;
729 while ((ptr = list_head( &fd->locks )))
731 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
732 if (lock->start < start) start = lock->start;
733 if (!lock->end || lock->end > end) end = lock->end - 1;
734 remove_lock( lock, 0 );
736 if (start < end) remove_unix_locks( fd, start, end + 1 );
739 /* add a lock on an fd */
740 /* returns handle to wait on */
741 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
743 struct list *ptr;
744 file_pos_t end = start + count;
746 /* don't allow wrapping locks */
747 if (end && end < start)
749 set_error( STATUS_INVALID_PARAMETER );
750 return 0;
753 /* check if another lock on that file overlaps the area */
754 LIST_FOR_EACH( ptr, &fd->inode->locks )
756 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
757 if (!lock_overlaps( lock, start, end )) continue;
758 if (lock->shared && shared) continue;
759 /* found one */
760 if (!wait)
762 set_error( STATUS_FILE_LOCK_CONFLICT );
763 return 0;
765 set_error( STATUS_PENDING );
766 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
769 /* not found, add it */
770 if (add_lock( fd, shared, start, end )) return 0;
771 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
773 /* Unix lock conflict -> tell client to wait and retry */
774 if (wait) set_error( STATUS_PENDING );
776 return 0;
779 /* remove a lock on an fd */
780 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
782 struct list *ptr;
783 file_pos_t end = start + count;
785 /* find an existing lock with the exact same parameters */
786 LIST_FOR_EACH( ptr, &fd->locks )
788 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
789 if ((lock->start == start) && (lock->end == end))
791 remove_lock( lock, 1 );
792 return;
795 set_error( STATUS_FILE_LOCK_CONFLICT );
799 /****************************************************************/
800 /* file descriptor functions */
802 static void fd_dump( struct object *obj, int verbose )
804 struct fd *fd = (struct fd *)obj;
805 fprintf( stderr, "Fd unix_fd=%d user=%p unlink='%s'\n",
806 fd->unix_fd, fd->user, fd->closed->unlink );
809 static void fd_destroy( struct object *obj )
811 struct fd *fd = (struct fd *)obj;
813 remove_fd_locks( fd );
814 list_remove( &fd->inode_entry );
815 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
816 if (fd->inode)
818 inode_add_closed_fd( fd->inode, fd->closed );
819 release_object( fd->inode );
821 else /* no inode, close it right away */
823 if (fd->unix_fd != -1) close( fd->unix_fd );
827 /* set the events that select waits for on this fd */
828 void set_fd_events( struct fd *fd, int events )
830 int user = fd->poll_index;
831 assert( poll_users[user] == fd );
832 if (events == -1) /* stop waiting on this fd completely */
834 pollfd[user].fd = -1;
835 pollfd[user].events = POLLERR;
836 pollfd[user].revents = 0;
838 else if (pollfd[user].fd != -1 || !pollfd[user].events)
840 pollfd[user].fd = fd->unix_fd;
841 pollfd[user].events = events;
845 /* allocate an fd object, without setting the unix fd yet */
846 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
848 struct fd *fd = alloc_object( &fd_ops );
850 if (!fd) return NULL;
852 fd->fd_ops = fd_user_ops;
853 fd->user = user;
854 fd->inode = NULL;
855 fd->closed = NULL;
856 fd->access = 0;
857 fd->sharing = 0;
858 fd->unix_fd = -1;
859 fd->fs_locks = 1;
860 fd->poll_index = -1;
861 list_init( &fd->inode_entry );
862 list_init( &fd->locks );
864 if ((fd->poll_index = add_poll_user( fd )) == -1)
866 release_object( fd );
867 return NULL;
869 return fd;
872 /* check if the desired access is possible without violating */
873 /* the sharing mode of other opens of the same file */
874 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
876 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
877 unsigned int existing_access = 0;
878 int unlink = 0;
879 struct list *ptr;
881 /* if access mode is 0, sharing mode is ignored */
882 if (!access) sharing = existing_sharing;
883 fd->access = access;
884 fd->sharing = sharing;
886 LIST_FOR_EACH( ptr, &fd->inode->open )
888 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
889 if (fd_ptr != fd)
891 existing_sharing &= fd_ptr->sharing;
892 existing_access |= fd_ptr->access;
893 if (fd_ptr->closed->unlink[0]) unlink = 1;
897 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
898 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
899 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
900 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
901 if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
902 if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
903 return 1;
906 /* open() wrapper using a struct fd */
907 /* the fd must have been created with alloc_fd */
908 /* on error the fd object is released */
909 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
910 unsigned int access, unsigned int sharing, const char *unlink_name )
912 struct stat st;
913 struct closed_fd *closed_fd;
915 assert( fd->unix_fd == -1 );
917 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
919 release_object( fd );
920 return NULL;
922 if ((fd->unix_fd = open( name, flags, *mode )) == -1)
924 file_set_error();
925 release_object( fd );
926 free( closed_fd );
927 return NULL;
929 closed_fd->fd = fd->unix_fd;
930 closed_fd->unlink[0] = 0;
931 fstat( fd->unix_fd, &st );
932 *mode = st.st_mode;
934 if (S_ISREG(st.st_mode)) /* only bother with an inode for normal files */
936 struct inode *inode = get_inode( st.st_dev, st.st_ino );
938 if (!inode)
940 /* we can close the fd because there are no others open on the same file,
941 * otherwise we wouldn't have failed to allocate a new inode
943 release_object( fd );
944 free( closed_fd );
945 return NULL;
947 fd->inode = inode;
948 fd->closed = closed_fd;
949 list_add_head( &inode->open, &fd->inode_entry );
950 if (!check_sharing( fd, access, sharing ))
952 release_object( fd );
953 set_error( STATUS_SHARING_VIOLATION );
954 return NULL;
956 strcpy( closed_fd->unlink, unlink_name );
958 else
960 free( closed_fd );
961 if (unlink_name[0]) /* we can't unlink special files */
963 release_object( fd );
964 set_error( STATUS_INVALID_PARAMETER );
965 return NULL;
968 return fd;
971 /* create an fd for an anonymous file */
972 /* if the function fails the unix fd is closed */
973 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
975 struct fd *fd = alloc_fd( fd_user_ops, user );
977 if (fd)
979 fd->unix_fd = unix_fd;
980 return fd;
982 close( unix_fd );
983 return NULL;
986 /* retrieve the object that is using an fd */
987 void *get_fd_user( struct fd *fd )
989 return fd->user;
992 /* retrieve the unix fd for an object */
993 int get_unix_fd( struct fd *fd )
995 return fd->unix_fd;
998 /* check if two file descriptors point to the same file */
999 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1001 return fd1->inode == fd2->inode;
1004 /* callback for event happening in the main poll() loop */
1005 void fd_poll_event( struct fd *fd, int event )
1007 return fd->fd_ops->poll_event( fd, event );
1010 /* check if events are pending and if yes return which one(s) */
1011 int check_fd_events( struct fd *fd, int events )
1013 struct pollfd pfd;
1015 pfd.fd = fd->unix_fd;
1016 pfd.events = events;
1017 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1018 return pfd.revents;
1021 /* default add_queue() routine for objects that poll() on an fd */
1022 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1024 struct fd *fd = get_obj_fd( obj );
1026 if (!fd) return 0;
1027 if (!obj->head) /* first on the queue */
1028 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1029 add_queue( obj, entry );
1030 release_object( fd );
1031 return 1;
1034 /* default remove_queue() routine for objects that poll() on an fd */
1035 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1037 struct fd *fd = get_obj_fd( obj );
1039 grab_object( obj );
1040 remove_queue( obj, entry );
1041 if (!obj->head) /* last on the queue is gone */
1042 set_fd_events( fd, 0 );
1043 release_object( obj );
1044 release_object( fd );
1047 /* default signaled() routine for objects that poll() on an fd */
1048 int default_fd_signaled( struct object *obj, struct thread *thread )
1050 struct fd *fd = get_obj_fd( obj );
1051 int events = fd->fd_ops->get_poll_events( fd );
1052 int ret = check_fd_events( fd, events ) != 0;
1054 if (ret)
1055 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
1056 else if (obj->head)
1057 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1059 release_object( fd );
1060 return ret;
1063 /* default handler for poll() events */
1064 void default_poll_event( struct fd *fd, int event )
1066 /* an error occurred, stop polling this fd to avoid busy-looping */
1067 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1068 wake_up( fd->user, 0 );
1071 /* default flush() routine */
1072 int no_flush( struct fd *fd, struct event **event )
1074 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1075 return 0;
1078 /* default get_file_info() routine */
1079 int no_get_file_info( struct fd *fd, struct get_file_info_reply *info, int *flags )
1081 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1082 *flags = 0;
1083 return FD_TYPE_INVALID;
1086 /* default queue_async() routine */
1087 void no_queue_async( struct fd *fd, void* ptr, unsigned int status, int type, int count )
1089 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1092 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1093 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1094 unsigned int access )
1096 struct fd *fd = NULL;
1097 struct object *obj;
1099 if ((obj = get_handle_obj( process, handle, access, NULL )))
1101 fd = get_obj_fd( obj );
1102 release_object( obj );
1104 return fd;
1107 /* flush a file buffers */
1108 DECL_HANDLER(flush_file)
1110 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1111 struct event * event = NULL;
1113 if (fd)
1115 fd->fd_ops->flush( fd, &event );
1116 if( event )
1118 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1120 release_object( fd );
1124 /* get a Unix fd to access a file */
1125 DECL_HANDLER(get_handle_fd)
1127 struct fd *fd;
1129 reply->fd = -1;
1130 reply->type = FD_TYPE_INVALID;
1132 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1134 int unix_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1135 if (unix_fd != -1) reply->fd = unix_fd;
1136 else if (!get_error())
1138 assert( fd->unix_fd != -1 );
1139 send_client_fd( current->process, fd->unix_fd, req->handle );
1141 reply->type = fd->fd_ops->get_file_info( fd, NULL, &reply->flags );
1142 release_object( fd );
1146 /* get a file information */
1147 DECL_HANDLER(get_file_info)
1149 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1151 if (fd)
1153 int flags;
1154 fd->fd_ops->get_file_info( fd, reply, &flags );
1155 release_object( fd );
1159 /* create / reschedule an async I/O */
1160 DECL_HANDLER(register_async)
1162 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1165 * The queue_async method must do the following:
1167 * 1. Get the async_queue for the request of given type.
1168 * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
1169 * 3. If status is STATUS_PENDING:
1170 * a) If no async request found in step 2 (new request): call create_async() to initialize one.
1171 * b) Set request's status to STATUS_PENDING.
1172 * c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
1173 * Otherwise:
1174 * If the async request was found in step 2, destroy it by calling destroy_async().
1175 * 4. Carry out any operations necessary to adjust the object's poll events
1176 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1178 * See also the implementations in file.c, serial.c, and sock.c.
1181 if (fd)
1183 fd->fd_ops->queue_async( fd, req->overlapped, req->status, req->type, req->count );
1184 release_object( fd );