Transform the Packaging Guide into a nice ASCII file.
[wine/hacks.git] / server / fd.c
blob7417dee8aeac60f83b99c7b79df7d58449de3107
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"
45 #include "console.h"
47 /* Because of the stupid Posix locking semantics, we need to keep
48 * track of all file descriptors referencing a given file, and not
49 * close a single one until all the locks are gone (sigh).
52 /* file descriptor object */
54 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
55 struct closed_fd
57 struct closed_fd *next; /* next fd in close list */
58 int fd; /* the unix file descriptor */
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 int unix_fd; /* unix file descriptor */
71 int poll_index; /* index of fd in poll array */
74 static void fd_dump( struct object *obj, int verbose );
75 static void fd_destroy( struct object *obj );
77 static const struct object_ops fd_ops =
79 sizeof(struct fd), /* size */
80 fd_dump, /* dump */
81 no_add_queue, /* add_queue */
82 NULL, /* remove_queue */
83 NULL, /* signaled */
84 NULL, /* satisfied */
85 no_get_fd, /* get_fd */
86 fd_destroy /* destroy */
89 /* inode object */
91 struct inode
93 struct object obj; /* object header */
94 struct list entry; /* inode hash list entry */
95 unsigned int hash; /* hashing code */
96 dev_t dev; /* device number */
97 ino_t ino; /* inode number */
98 struct list open; /* list of open file descriptors */
99 struct list locks; /* list of file locks */
100 struct closed_fd *closed; /* list of file descriptors to close at destroy time */
103 static void inode_dump( struct object *obj, int verbose );
104 static void inode_destroy( struct object *obj );
106 static const struct object_ops inode_ops =
108 sizeof(struct inode), /* size */
109 inode_dump, /* dump */
110 no_add_queue, /* add_queue */
111 NULL, /* remove_queue */
112 NULL, /* signaled */
113 NULL, /* satisfied */
114 no_get_fd, /* get_fd */
115 inode_destroy /* destroy */
118 /* file lock object */
120 struct file_lock
122 struct object obj; /* object header */
123 struct fd *fd; /* fd owning this lock */
124 struct list fd_entry; /* entry in list of locks on a given fd */
125 struct list inode_entry; /* entry in inode list of locks */
126 int shared; /* shared lock? */
127 file_pos_t start; /* locked region is interval [start;end) */
128 file_pos_t end;
129 struct process *process; /* process owning this lock */
130 struct list proc_entry; /* entry in list of locks owned by the process */
133 static void file_lock_dump( struct object *obj, int verbose );
134 static int file_lock_signaled( struct object *obj, struct thread *thread );
136 static const struct object_ops file_lock_ops =
138 sizeof(struct file_lock), /* size */
139 file_lock_dump, /* dump */
140 add_queue, /* add_queue */
141 remove_queue, /* remove_queue */
142 file_lock_signaled, /* signaled */
143 no_satisfied, /* satisfied */
144 no_get_fd, /* get_fd */
145 no_destroy /* destroy */
149 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
150 #define FILE_POS_T_MAX (~(file_pos_t)0)
152 static file_pos_t max_unix_offset = OFF_T_MAX;
154 #define DUMP_LONG_LONG(val) do { \
155 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
156 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
157 else \
158 fprintf( stderr, "%lx", (unsigned long)(val) ); \
159 } while (0)
163 /****************************************************************/
164 /* timeouts support */
166 struct timeout_user
168 struct timeout_user *next; /* next in sorted timeout list */
169 struct timeout_user *prev; /* prev in sorted timeout list */
170 struct timeval when; /* timeout expiry (absolute time) */
171 timeout_callback callback; /* callback function */
172 void *private; /* callback private data */
175 static struct timeout_user *timeout_head; /* sorted timeouts list head */
176 static struct timeout_user *timeout_tail; /* sorted timeouts list tail */
178 /* add a timeout user */
179 struct timeout_user *add_timeout_user( struct timeval *when, timeout_callback func, void *private )
181 struct timeout_user *user;
182 struct timeout_user *pos;
184 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
185 user->when = *when;
186 user->callback = func;
187 user->private = private;
189 /* Now insert it in the linked list */
191 for (pos = timeout_head; pos; pos = pos->next)
192 if (!time_before( &pos->when, when )) break;
194 if (pos) /* insert it before 'pos' */
196 if ((user->prev = pos->prev)) user->prev->next = user;
197 else timeout_head = user;
198 user->next = pos;
199 pos->prev = user;
201 else /* insert it at the tail */
203 user->next = NULL;
204 if (timeout_tail) timeout_tail->next = user;
205 else timeout_head = user;
206 user->prev = timeout_tail;
207 timeout_tail = user;
209 return user;
212 /* remove a timeout user */
213 void remove_timeout_user( struct timeout_user *user )
215 if (user->next) user->next->prev = user->prev;
216 else timeout_tail = user->prev;
217 if (user->prev) user->prev->next = user->next;
218 else timeout_head = user->next;
219 free( user );
222 /* add a timeout in milliseconds to an absolute time */
223 void add_timeout( struct timeval *when, int timeout )
225 if (timeout)
227 long sec = timeout / 1000;
228 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
230 when->tv_usec -= 1000000;
231 when->tv_sec++;
233 when->tv_sec += sec;
237 /* handle the next expired timeout */
238 inline static void handle_timeout(void)
240 struct timeout_user *user = timeout_head;
241 timeout_head = user->next;
242 if (user->next) user->next->prev = user->prev;
243 else timeout_tail = user->prev;
244 user->callback( user->private );
245 free( user );
249 /****************************************************************/
250 /* poll support */
252 static struct fd **poll_users; /* users array */
253 static struct pollfd *pollfd; /* poll fd array */
254 static int nb_users; /* count of array entries actually in use */
255 static int active_users; /* current number of active users */
256 static int allocated_users; /* count of allocated entries in the array */
257 static struct fd **freelist; /* list of free entries in the array */
259 /* add a user in the poll array and return its index, or -1 on failure */
260 static int add_poll_user( struct fd *fd )
262 int ret;
263 if (freelist)
265 ret = freelist - poll_users;
266 freelist = (struct fd **)poll_users[ret];
268 else
270 if (nb_users == allocated_users)
272 struct fd **newusers;
273 struct pollfd *newpoll;
274 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
275 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
276 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
278 if (allocated_users)
279 poll_users = newusers;
280 else
281 free( newusers );
282 return -1;
284 poll_users = newusers;
285 pollfd = newpoll;
286 allocated_users = new_count;
288 ret = nb_users++;
290 pollfd[ret].fd = -1;
291 pollfd[ret].events = 0;
292 pollfd[ret].revents = 0;
293 poll_users[ret] = fd;
294 active_users++;
295 return ret;
298 /* remove a user from the poll list */
299 static void remove_poll_user( struct fd *fd, int user )
301 assert( user >= 0 );
302 assert( poll_users[user] == fd );
303 pollfd[user].fd = -1;
304 pollfd[user].events = 0;
305 pollfd[user].revents = 0;
306 poll_users[user] = (struct fd *)freelist;
307 freelist = &poll_users[user];
308 active_users--;
312 /* server main poll() loop */
313 void main_loop(void)
315 int ret;
317 while (active_users)
319 long diff = -1;
320 if (timeout_head)
322 struct timeval now;
323 gettimeofday( &now, NULL );
324 while (timeout_head)
326 if (!time_before( &now, &timeout_head->when )) handle_timeout();
327 else
329 diff = (timeout_head->when.tv_sec - now.tv_sec) * 1000
330 + (timeout_head->when.tv_usec - now.tv_usec) / 1000;
331 break;
334 if (!active_users) break; /* last user removed by a timeout */
336 ret = poll( pollfd, nb_users, diff );
337 if (ret > 0)
339 int i;
340 for (i = 0; i < nb_users; i++)
342 if (pollfd[i].revents)
344 fd_poll_event( poll_users[i], pollfd[i].revents );
345 if (!--ret) break;
353 /****************************************************************/
354 /* inode functions */
356 #define HASH_SIZE 37
358 static struct list inode_hash[HASH_SIZE];
360 /* close all pending file descriptors in the closed list */
361 static void inode_close_pending( struct inode *inode )
363 while (inode->closed)
365 struct closed_fd *fd = inode->closed;
366 inode->closed = fd->next;
367 close( fd->fd );
368 free( fd );
373 static void inode_dump( struct object *obj, int verbose )
375 struct inode *inode = (struct inode *)obj;
376 fprintf( stderr, "Inode dev=" );
377 DUMP_LONG_LONG( inode->dev );
378 fprintf( stderr, " ino=" );
379 DUMP_LONG_LONG( inode->ino );
380 fprintf( stderr, "\n" );
383 static void inode_destroy( struct object *obj )
385 struct inode *inode = (struct inode *)obj;
387 assert( list_empty(&inode->open) );
388 assert( list_empty(&inode->locks) );
390 list_remove( &inode->entry );
391 inode_close_pending( inode );
394 /* retrieve the inode object for a given fd, creating it if needed */
395 static struct inode *get_inode( dev_t dev, ino_t ino )
397 struct list *ptr;
398 struct inode *inode;
399 unsigned int hash = (dev ^ ino) % HASH_SIZE;
401 if (inode_hash[hash].next)
403 LIST_FOR_EACH( ptr, &inode_hash[hash] )
405 inode = LIST_ENTRY( ptr, struct inode, entry );
406 if (inode->dev == dev && inode->ino == ino)
407 return (struct inode *)grab_object( inode );
410 else list_init( &inode_hash[hash] );
412 /* not found, create it */
413 if ((inode = alloc_object( &inode_ops )))
415 inode->hash = hash;
416 inode->dev = dev;
417 inode->ino = ino;
418 inode->closed = NULL;
419 list_init( &inode->open );
420 list_init( &inode->locks );
421 list_add_head( &inode_hash[hash], &inode->entry );
423 return inode;
426 /* add fd to the indoe list of file descriptors to close */
427 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
429 if (!list_empty( &inode->locks ))
431 fd->next = inode->closed;
432 inode->closed = fd;
434 else /* no locks on this inode, we can close the fd right away */
436 close( fd->fd );
437 free( fd );
442 /****************************************************************/
443 /* file lock functions */
445 static void file_lock_dump( struct object *obj, int verbose )
447 struct file_lock *lock = (struct file_lock *)obj;
448 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
449 lock->shared ? "shared" : "excl", lock->fd, lock->process );
450 DUMP_LONG_LONG( lock->start );
451 fprintf( stderr, " end=" );
452 DUMP_LONG_LONG( lock->end );
453 fprintf( stderr, "\n" );
456 static int file_lock_signaled( struct object *obj, struct thread *thread )
458 struct file_lock *lock = (struct file_lock *)obj;
459 /* lock is signaled if it has lost its owner */
460 return !lock->process;
463 /* set (or remove) a Unix lock if possible for the given range */
464 static int set_unix_lock( const struct fd *fd, file_pos_t start, file_pos_t end, int type )
466 struct flock fl;
468 for (;;)
470 if (start == end) return 1; /* can't set zero-byte lock */
471 if (start > max_unix_offset) return 1; /* ignore it */
472 fl.l_type = type;
473 fl.l_whence = SEEK_SET;
474 fl.l_start = start;
475 if (!end || end > max_unix_offset) fl.l_len = 0;
476 else fl.l_len = end - start;
477 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
479 switch(errno)
481 case EACCES:
482 case EAGAIN:
483 set_error( STATUS_FILE_LOCK_CONFLICT );
484 return 0;
485 case EBADF:
486 /* this can happen if we try to set a write lock on a read-only file */
487 /* we just ignore that error */
488 if (fl.l_type == F_WRLCK) return 1;
489 set_error( STATUS_ACCESS_DENIED );
490 return 0;
491 case EOVERFLOW:
492 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
493 /* in that case we shrink the limit and retry */
494 if (max_unix_offset > INT_MAX)
496 max_unix_offset = INT_MAX;
497 break; /* retry */
499 /* fall through */
500 default:
501 file_set_error();
502 return 0;
507 /* check if interval [start;end) overlaps the lock */
508 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
510 if (lock->end && start >= lock->end) return 0;
511 if (end && lock->start >= end) return 0;
512 return 1;
515 /* remove Unix locks for all bytes in the specified area that are no longer locked */
516 static void remove_unix_locks( const struct fd *fd, file_pos_t start, file_pos_t end )
518 struct hole
520 struct hole *next;
521 struct hole *prev;
522 file_pos_t start;
523 file_pos_t end;
524 } *first, *cur, *next, *buffer;
526 struct list *ptr;
527 int count = 0;
529 if (!fd->inode) return;
530 if (start == end || start > max_unix_offset) return;
531 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
533 /* count the number of locks overlapping the specified area */
535 LIST_FOR_EACH( ptr, &fd->inode->locks )
537 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
538 if (lock->start == lock->end) continue;
539 if (lock_overlaps( lock, start, end )) count++;
542 if (!count) /* no locks at all, we can unlock everything */
544 set_unix_lock( fd, start, end, F_UNLCK );
545 return;
548 /* allocate space for the list of holes */
549 /* max. number of holes is number of locks + 1 */
551 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
552 first = buffer;
553 first->next = NULL;
554 first->prev = NULL;
555 first->start = start;
556 first->end = end;
557 next = first + 1;
559 /* build a sorted list of unlocked holes in the specified area */
561 LIST_FOR_EACH( ptr, &fd->inode->locks )
563 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
564 if (lock->start == lock->end) continue;
565 if (!lock_overlaps( lock, start, end )) continue;
567 /* go through all the holes touched by this lock */
568 for (cur = first; cur; cur = cur->next)
570 if (cur->end <= lock->start) continue; /* hole is before start of lock */
571 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
573 /* now we know that lock is overlapping hole */
575 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
577 cur->start = lock->end;
578 if (cur->start && cur->start < cur->end) break; /* done with this lock */
579 /* now hole is empty, remove it */
580 if (cur->next) cur->next->prev = cur->prev;
581 if (cur->prev) cur->prev->next = cur->next;
582 else if (!(first = cur->next)) goto done; /* no more holes at all */
584 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
586 cur->end = lock->start;
587 assert( cur->start < cur->end );
589 else /* lock is in the middle of hole, split hole in two */
591 next->prev = cur;
592 next->next = cur->next;
593 cur->next = next;
594 next->start = lock->end;
595 next->end = cur->end;
596 cur->end = lock->start;
597 assert( next->start < next->end );
598 assert( cur->end < next->start );
599 next++;
600 break; /* done with this lock */
605 /* clear Unix locks for all the holes */
607 for (cur = first; cur; cur = cur->next)
608 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
610 done:
611 free( buffer );
614 /* create a new lock on a fd */
615 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
617 struct file_lock *lock;
619 if (!fd->inode) /* not a regular file */
621 set_error( STATUS_INVALID_HANDLE );
622 return NULL;
625 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
626 lock->shared = shared;
627 lock->start = start;
628 lock->end = end;
629 lock->fd = fd;
630 lock->process = current->process;
632 /* now try to set a Unix lock */
633 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
635 release_object( lock );
636 return NULL;
638 list_add_head( &fd->locks, &lock->fd_entry );
639 list_add_head( &fd->inode->locks, &lock->inode_entry );
640 list_add_head( &lock->process->locks, &lock->proc_entry );
641 return lock;
644 /* remove an existing lock */
645 static void remove_lock( struct file_lock *lock, int remove_unix )
647 struct inode *inode = lock->fd->inode;
649 list_remove( &lock->fd_entry );
650 list_remove( &lock->inode_entry );
651 list_remove( &lock->proc_entry );
652 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
653 if (list_empty( &inode->locks )) inode_close_pending( inode );
654 lock->process = NULL;
655 wake_up( &lock->obj, 0 );
656 release_object( lock );
659 /* remove all locks owned by a given process */
660 void remove_process_locks( struct process *process )
662 struct list *ptr;
664 while ((ptr = list_head( &process->locks )))
666 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
667 remove_lock( lock, 1 ); /* this removes it from the list */
671 /* remove all locks on a given fd */
672 static void remove_fd_locks( struct fd *fd )
674 file_pos_t start = FILE_POS_T_MAX, end = 0;
675 struct list *ptr;
677 while ((ptr = list_head( &fd->locks )))
679 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
680 if (lock->start < start) start = lock->start;
681 if (!lock->end || lock->end > end) end = lock->end - 1;
682 remove_lock( lock, 0 );
684 if (start < end) remove_unix_locks( fd, start, end + 1 );
687 /* add a lock on an fd */
688 /* returns handle to wait on */
689 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
691 struct list *ptr;
692 file_pos_t end = start + count;
694 /* don't allow wrapping locks */
695 if (end && end < start)
697 set_error( STATUS_INVALID_PARAMETER );
698 return 0;
701 /* check if another lock on that file overlaps the area */
702 LIST_FOR_EACH( ptr, &fd->inode->locks )
704 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
705 if (!lock_overlaps( lock, start, end )) continue;
706 if (lock->shared && shared) continue;
707 /* found one */
708 if (!wait)
710 set_error( STATUS_FILE_LOCK_CONFLICT );
711 return 0;
713 set_error( STATUS_PENDING );
714 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
717 /* not found, add it */
718 if (add_lock( fd, shared, start, end )) return 0;
719 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
721 /* Unix lock conflict -> tell client to wait and retry */
722 if (wait) set_error( STATUS_PENDING );
724 return 0;
727 /* remove a lock on an fd */
728 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
730 struct list *ptr;
731 file_pos_t end = start + count;
733 /* find an existing lock with the exact same parameters */
734 LIST_FOR_EACH( ptr, &fd->locks )
736 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
737 if ((lock->start == start) && (lock->end == end))
739 remove_lock( lock, 1 );
740 return;
743 set_error( STATUS_FILE_LOCK_CONFLICT );
747 /****************************************************************/
748 /* file descriptor functions */
750 static void fd_dump( struct object *obj, int verbose )
752 struct fd *fd = (struct fd *)obj;
753 fprintf( stderr, "Fd unix_fd=%d user=%p\n", fd->unix_fd, fd->user );
756 static void fd_destroy( struct object *obj )
758 struct fd *fd = (struct fd *)obj;
760 remove_fd_locks( fd );
761 list_remove( &fd->inode_entry );
762 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
763 if (fd->inode)
765 inode_add_closed_fd( fd->inode, fd->closed );
766 release_object( fd->inode );
768 else /* no inode, close it right away */
770 if (fd->unix_fd != -1) close( fd->unix_fd );
774 /* set the events that select waits for on this fd */
775 void set_fd_events( struct fd *fd, int events )
777 int user = fd->poll_index;
778 assert( poll_users[user] == fd );
779 if (events == -1) /* stop waiting on this fd completely */
781 pollfd[user].fd = -1;
782 pollfd[user].events = POLLERR;
783 pollfd[user].revents = 0;
785 else if (pollfd[user].fd != -1 || !pollfd[user].events)
787 pollfd[user].fd = fd->unix_fd;
788 pollfd[user].events = events;
792 /* allocate an fd object, without setting the unix fd yet */
793 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
795 struct fd *fd = alloc_object( &fd_ops );
797 if (!fd) return NULL;
799 fd->fd_ops = fd_user_ops;
800 fd->user = user;
801 fd->inode = NULL;
802 fd->closed = NULL;
803 fd->unix_fd = -1;
804 fd->poll_index = -1;
805 list_init( &fd->inode_entry );
806 list_init( &fd->locks );
808 if ((fd->poll_index = add_poll_user( fd )) == -1)
810 release_object( fd );
811 return NULL;
813 return fd;
816 /* open() wrapper using a struct fd */
817 /* the fd must have been created with alloc_fd */
818 /* on error the fd object is released */
819 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode )
821 struct stat st;
822 struct closed_fd *closed_fd;
824 assert( fd->unix_fd == -1 );
826 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
828 release_object( fd );
829 return NULL;
831 if ((fd->unix_fd = open( name, flags, *mode )) == -1)
833 file_set_error();
834 release_object( fd );
835 free( closed_fd );
836 return NULL;
838 closed_fd->fd = fd->unix_fd;
839 fstat( fd->unix_fd, &st );
840 *mode = st.st_mode;
842 if (S_ISREG(st.st_mode)) /* only bother with an inode for normal files */
844 struct inode *inode = get_inode( st.st_dev, st.st_ino );
846 if (!inode)
848 /* we can close the fd because there are no others open on the same file,
849 * otherwise we wouldn't have failed to allocate a new inode
851 release_object( fd );
852 free( closed_fd );
853 return NULL;
855 fd->inode = inode;
856 fd->closed = closed_fd;
857 list_add_head( &inode->open, &fd->inode_entry );
859 else
861 free( closed_fd );
863 return fd;
866 /* create an fd for an anonymous file */
867 /* if the function fails the unix fd is closed */
868 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
870 struct fd *fd = alloc_fd( fd_user_ops, user );
872 if (fd)
874 fd->unix_fd = unix_fd;
875 return fd;
877 close( unix_fd );
878 return NULL;
881 /* retrieve the object that is using an fd */
882 void *get_fd_user( struct fd *fd )
884 return fd->user;
887 /* retrieve the unix fd for an object */
888 int get_unix_fd( struct fd *fd )
890 return fd->unix_fd;
893 /* callback for event happening in the main poll() loop */
894 void fd_poll_event( struct fd *fd, int event )
896 return fd->fd_ops->poll_event( fd, event );
899 /* check if events are pending and if yes return which one(s) */
900 int check_fd_events( struct fd *fd, int events )
902 struct pollfd pfd;
904 pfd.fd = fd->unix_fd;
905 pfd.events = events;
906 if (poll( &pfd, 1, 0 ) <= 0) return 0;
907 return pfd.revents;
910 /* default add_queue() routine for objects that poll() on an fd */
911 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
913 struct fd *fd = get_obj_fd( obj );
915 if (!fd) return 0;
916 if (!obj->head) /* first on the queue */
917 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
918 add_queue( obj, entry );
919 release_object( fd );
920 return 1;
923 /* default remove_queue() routine for objects that poll() on an fd */
924 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
926 struct fd *fd = get_obj_fd( obj );
928 grab_object( obj );
929 remove_queue( obj, entry );
930 if (!obj->head) /* last on the queue is gone */
931 set_fd_events( fd, 0 );
932 release_object( obj );
933 release_object( fd );
936 /* default signaled() routine for objects that poll() on an fd */
937 int default_fd_signaled( struct object *obj, struct thread *thread )
939 struct fd *fd = get_obj_fd( obj );
940 int events = fd->fd_ops->get_poll_events( fd );
941 int ret = check_fd_events( fd, events ) != 0;
943 if (ret)
944 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
945 else if (obj->head)
946 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
948 release_object( fd );
949 return ret;
952 /* default handler for poll() events */
953 void default_poll_event( struct fd *fd, int event )
955 /* an error occurred, stop polling this fd to avoid busy-looping */
956 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
957 wake_up( fd->user, 0 );
960 /* default flush() routine */
961 int no_flush( struct fd *fd )
963 set_error( STATUS_OBJECT_TYPE_MISMATCH );
964 return 0;
967 /* default get_file_info() routine */
968 int no_get_file_info( struct fd *fd, struct get_file_info_reply *info, int *flags )
970 set_error( STATUS_OBJECT_TYPE_MISMATCH );
971 *flags = 0;
972 return FD_TYPE_INVALID;
975 /* default queue_async() routine */
976 void no_queue_async( struct fd *fd, void* ptr, unsigned int status, int type, int count )
978 set_error( STATUS_OBJECT_TYPE_MISMATCH );
981 /* same as get_handle_obj but retrieve the struct fd associated to the object */
982 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
983 unsigned int access )
985 struct fd *fd = NULL;
986 struct object *obj;
988 if ((obj = get_handle_obj( process, handle, access, NULL )))
990 if (!(fd = get_obj_fd( obj ))) set_error( STATUS_OBJECT_TYPE_MISMATCH );
991 release_object( obj );
993 return fd;
996 /* flush a file buffers */
997 DECL_HANDLER(flush_file)
999 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1001 if (fd)
1003 fd->fd_ops->flush( fd );
1004 release_object( fd );
1008 /* get a Unix fd to access a file */
1009 DECL_HANDLER(get_handle_fd)
1011 struct fd *fd;
1013 reply->fd = -1;
1014 reply->type = FD_TYPE_INVALID;
1016 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1018 int unix_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1019 if (unix_fd != -1) reply->fd = unix_fd;
1020 else if (!get_error())
1022 unix_fd = fd->unix_fd;
1023 if (unix_fd != -1) send_client_fd( current->process, unix_fd, req->handle );
1025 reply->type = fd->fd_ops->get_file_info( fd, NULL, &reply->flags );
1026 release_object( fd );
1028 else /* check for console handle (FIXME: should be done in the client) */
1030 struct object *obj;
1032 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
1034 if (is_console_object( obj )) reply->type = FD_TYPE_CONSOLE;
1035 release_object( obj );
1040 /* get a file information */
1041 DECL_HANDLER(get_file_info)
1043 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1045 if (fd)
1047 int flags;
1048 fd->fd_ops->get_file_info( fd, reply, &flags );
1049 release_object( fd );
1053 /* create / reschedule an async I/O */
1054 DECL_HANDLER(register_async)
1056 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1059 * The queue_async method must do the following:
1061 * 1. Get the async_queue for the request of given type.
1062 * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
1063 * 3. If status is STATUS_PENDING:
1064 * a) If no async request found in step 2 (new request): call create_async() to initialize one.
1065 * b) Set request's status to STATUS_PENDING.
1066 * c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
1067 * Otherwise:
1068 * If the async request was found in step 2, destroy it by calling destroy_async().
1069 * 4. Carry out any operations necessary to adjust the object's poll events
1070 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1072 * See also the implementations in file.c, serial.c, and sock.c.
1075 if (fd)
1077 fd->fd_ops->queue_async( fd, req->overlapped, req->status, req->type, req->count );
1078 release_object( fd );