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
32 #ifdef HAVE_SYS_POLL_H
37 #include <sys/types.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 */
56 struct closed_fd
*next
; /* next fd in close list */
57 int fd
; /* the unix file descriptor */
62 struct object obj
; /* object header */
63 const struct fd_ops
*fd_ops
; /* file descriptor operations */
64 struct inode
*inode
; /* inode that this fd belongs to */
65 struct list inode_entry
; /* entry in inode fd list */
66 struct closed_fd
*closed
; /* structure to store the unix fd at destroy time */
67 struct object
*user
; /* object using this file descriptor */
68 struct list locks
; /* list of locks on this fd */
69 int unix_fd
; /* unix file descriptor */
70 int poll_index
; /* index of fd in poll array */
73 static void fd_dump( struct object
*obj
, int verbose
);
74 static void fd_destroy( struct object
*obj
);
76 static const struct object_ops fd_ops
=
78 sizeof(struct fd
), /* size */
80 no_add_queue
, /* add_queue */
81 NULL
, /* remove_queue */
84 no_get_fd
, /* get_fd */
85 fd_destroy
/* destroy */
92 struct object obj
; /* object header */
93 struct list entry
; /* inode hash list entry */
94 unsigned int hash
; /* hashing code */
95 dev_t dev
; /* device number */
96 ino_t ino
; /* inode number */
97 struct list open
; /* list of open file descriptors */
98 struct list locks
; /* list of file locks */
99 struct closed_fd
*closed
; /* list of file descriptors to close at destroy time */
102 static void inode_dump( struct object
*obj
, int verbose
);
103 static void inode_destroy( struct object
*obj
);
105 static const struct object_ops inode_ops
=
107 sizeof(struct inode
), /* size */
108 inode_dump
, /* dump */
109 no_add_queue
, /* add_queue */
110 NULL
, /* remove_queue */
112 NULL
, /* satisfied */
113 no_get_fd
, /* get_fd */
114 inode_destroy
/* destroy */
117 /* file lock object */
121 struct object obj
; /* object header */
122 struct fd
*fd
; /* fd owning this lock */
123 struct list fd_entry
; /* entry in list of locks on a given fd */
124 struct list inode_entry
; /* entry in inode list of locks */
125 int shared
; /* shared lock? */
126 file_pos_t start
; /* locked region is interval [start;end) */
128 struct process
*process
; /* process owning this lock */
129 struct list proc_entry
; /* entry in list of locks owned by the process */
132 static void file_lock_dump( struct object
*obj
, int verbose
);
133 static int file_lock_signaled( struct object
*obj
, struct thread
*thread
);
135 static const struct object_ops file_lock_ops
=
137 sizeof(struct file_lock
), /* size */
138 file_lock_dump
, /* dump */
139 add_queue
, /* add_queue */
140 remove_queue
, /* remove_queue */
141 file_lock_signaled
, /* signaled */
142 no_satisfied
, /* satisfied */
143 no_get_fd
, /* get_fd */
144 no_destroy
/* destroy */
148 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
149 #define FILE_POS_T_MAX (~(file_pos_t)0)
151 static file_pos_t max_unix_offset
= OFF_T_MAX
;
153 #define DUMP_LONG_LONG(val) do { \
154 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
155 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
157 fprintf( stderr, "%lx", (unsigned long)(val) ); \
162 /****************************************************************/
163 /* timeouts support */
167 struct timeout_user
*next
; /* next in sorted timeout list */
168 struct timeout_user
*prev
; /* prev in sorted timeout list */
169 struct timeval when
; /* timeout expiry (absolute time) */
170 timeout_callback callback
; /* callback function */
171 void *private; /* callback private data */
174 static struct timeout_user
*timeout_head
; /* sorted timeouts list head */
175 static struct timeout_user
*timeout_tail
; /* sorted timeouts list tail */
177 /* add a timeout user */
178 struct timeout_user
*add_timeout_user( struct timeval
*when
, timeout_callback func
, void *private )
180 struct timeout_user
*user
;
181 struct timeout_user
*pos
;
183 if (!(user
= mem_alloc( sizeof(*user
) ))) return NULL
;
185 user
->callback
= func
;
186 user
->private = private;
188 /* Now insert it in the linked list */
190 for (pos
= timeout_head
; pos
; pos
= pos
->next
)
191 if (!time_before( &pos
->when
, when
)) break;
193 if (pos
) /* insert it before 'pos' */
195 if ((user
->prev
= pos
->prev
)) user
->prev
->next
= user
;
196 else timeout_head
= user
;
200 else /* insert it at the tail */
203 if (timeout_tail
) timeout_tail
->next
= user
;
204 else timeout_head
= user
;
205 user
->prev
= timeout_tail
;
211 /* remove a timeout user */
212 void remove_timeout_user( struct timeout_user
*user
)
214 if (user
->next
) user
->next
->prev
= user
->prev
;
215 else timeout_tail
= user
->prev
;
216 if (user
->prev
) user
->prev
->next
= user
->next
;
217 else timeout_head
= user
->next
;
221 /* add a timeout in milliseconds to an absolute time */
222 void add_timeout( struct timeval
*when
, int timeout
)
226 long sec
= timeout
/ 1000;
227 if ((when
->tv_usec
+= (timeout
- 1000*sec
) * 1000) >= 1000000)
229 when
->tv_usec
-= 1000000;
236 /* handle the next expired timeout */
237 inline static void handle_timeout(void)
239 struct timeout_user
*user
= timeout_head
;
240 timeout_head
= user
->next
;
241 if (user
->next
) user
->next
->prev
= user
->prev
;
242 else timeout_tail
= user
->prev
;
243 user
->callback( user
->private );
248 /****************************************************************/
251 static struct fd
**poll_users
; /* users array */
252 static struct pollfd
*pollfd
; /* poll fd array */
253 static int nb_users
; /* count of array entries actually in use */
254 static int active_users
; /* current number of active users */
255 static int allocated_users
; /* count of allocated entries in the array */
256 static struct fd
**freelist
; /* list of free entries in the array */
258 /* add a user in the poll array and return its index, or -1 on failure */
259 static int add_poll_user( struct fd
*fd
)
264 ret
= freelist
- poll_users
;
265 freelist
= (struct fd
**)poll_users
[ret
];
269 if (nb_users
== allocated_users
)
271 struct fd
**newusers
;
272 struct pollfd
*newpoll
;
273 int new_count
= allocated_users
? (allocated_users
+ allocated_users
/ 2) : 16;
274 if (!(newusers
= realloc( poll_users
, new_count
* sizeof(*poll_users
) ))) return -1;
275 if (!(newpoll
= realloc( pollfd
, new_count
* sizeof(*pollfd
) )))
278 poll_users
= newusers
;
283 poll_users
= newusers
;
285 allocated_users
= new_count
;
290 pollfd
[ret
].events
= 0;
291 pollfd
[ret
].revents
= 0;
292 poll_users
[ret
] = fd
;
297 /* remove a user from the poll list */
298 static void remove_poll_user( struct fd
*fd
, int user
)
301 assert( poll_users
[user
] == fd
);
302 pollfd
[user
].fd
= -1;
303 pollfd
[user
].events
= 0;
304 pollfd
[user
].revents
= 0;
305 poll_users
[user
] = (struct fd
*)freelist
;
306 freelist
= &poll_users
[user
];
311 /* server main poll() loop */
322 gettimeofday( &now
, NULL
);
325 if (!time_before( &now
, &timeout_head
->when
)) handle_timeout();
328 diff
= (timeout_head
->when
.tv_sec
- now
.tv_sec
) * 1000
329 + (timeout_head
->when
.tv_usec
- now
.tv_usec
) / 1000;
333 if (!active_users
) break; /* last user removed by a timeout */
335 ret
= poll( pollfd
, nb_users
, diff
);
339 for (i
= 0; i
< nb_users
; i
++)
341 if (pollfd
[i
].revents
)
343 fd_poll_event( poll_users
[i
], pollfd
[i
].revents
);
352 /****************************************************************/
353 /* inode functions */
357 static struct list inode_hash
[HASH_SIZE
];
359 /* close all pending file descriptors in the closed list */
360 static void inode_close_pending( struct inode
*inode
)
362 while (inode
->closed
)
364 struct closed_fd
*fd
= inode
->closed
;
365 inode
->closed
= fd
->next
;
372 static void inode_dump( struct object
*obj
, int verbose
)
374 struct inode
*inode
= (struct inode
*)obj
;
375 fprintf( stderr
, "Inode dev=" );
376 DUMP_LONG_LONG( inode
->dev
);
377 fprintf( stderr
, " ino=" );
378 DUMP_LONG_LONG( inode
->ino
);
379 fprintf( stderr
, "\n" );
382 static void inode_destroy( struct object
*obj
)
384 struct inode
*inode
= (struct inode
*)obj
;
386 assert( list_empty(&inode
->open
) );
387 assert( list_empty(&inode
->locks
) );
389 list_remove( &inode
->entry
);
390 inode_close_pending( inode
);
393 /* retrieve the inode object for a given fd, creating it if needed */
394 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
398 unsigned int hash
= (dev
^ ino
) % HASH_SIZE
;
400 if (inode_hash
[hash
].next
)
402 LIST_FOR_EACH( ptr
, &inode_hash
[hash
] )
404 inode
= LIST_ENTRY( ptr
, struct inode
, entry
);
405 if (inode
->dev
== dev
&& inode
->ino
== ino
)
406 return (struct inode
*)grab_object( inode
);
409 else list_init( &inode_hash
[hash
] );
411 /* not found, create it */
412 if ((inode
= alloc_object( &inode_ops
)))
417 inode
->closed
= NULL
;
418 list_init( &inode
->open
);
419 list_init( &inode
->locks
);
420 list_add_head( &inode_hash
[hash
], &inode
->entry
);
425 /* add fd to the indoe list of file descriptors to close */
426 static void inode_add_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
428 if (!list_empty( &inode
->locks
))
430 fd
->next
= inode
->closed
;
433 else /* no locks on this inode, we can close the fd right away */
441 /****************************************************************/
442 /* file lock functions */
444 static void file_lock_dump( struct object
*obj
, int verbose
)
446 struct file_lock
*lock
= (struct file_lock
*)obj
;
447 fprintf( stderr
, "Lock %s fd=%p proc=%p start=",
448 lock
->shared
? "shared" : "excl", lock
->fd
, lock
->process
);
449 DUMP_LONG_LONG( lock
->start
);
450 fprintf( stderr
, " end=" );
451 DUMP_LONG_LONG( lock
->end
);
452 fprintf( stderr
, "\n" );
455 static int file_lock_signaled( struct object
*obj
, struct thread
*thread
)
457 struct file_lock
*lock
= (struct file_lock
*)obj
;
458 /* lock is signaled if it has lost its owner */
459 return !lock
->process
;
462 /* set (or remove) a Unix lock if possible for the given range */
463 static int set_unix_lock( const struct fd
*fd
, file_pos_t start
, file_pos_t end
, int type
)
469 if (start
== end
) return 1; /* can't set zero-byte lock */
470 if (start
> max_unix_offset
) return 1; /* ignore it */
472 fl
.l_whence
= SEEK_SET
;
474 if (!end
|| end
> max_unix_offset
) fl
.l_len
= 0;
475 else fl
.l_len
= end
- start
;
476 if (fcntl( fd
->unix_fd
, F_SETLK
, &fl
) != -1) return 1;
482 /* no locking on this fs, just ignore it */
486 set_error( STATUS_FILE_LOCK_CONFLICT
);
489 /* this can happen if we try to set a write lock on a read-only file */
490 /* we just ignore that error */
491 if (fl
.l_type
== F_WRLCK
) return 1;
492 set_error( STATUS_ACCESS_DENIED
);
496 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
497 /* in that case we shrink the limit and retry */
498 if (max_unix_offset
> INT_MAX
)
500 max_unix_offset
= INT_MAX
;
511 /* check if interval [start;end) overlaps the lock */
512 inline static int lock_overlaps( struct file_lock
*lock
, file_pos_t start
, file_pos_t end
)
514 if (lock
->end
&& start
>= lock
->end
) return 0;
515 if (end
&& lock
->start
>= end
) return 0;
519 /* remove Unix locks for all bytes in the specified area that are no longer locked */
520 static void remove_unix_locks( const struct fd
*fd
, file_pos_t start
, file_pos_t end
)
528 } *first
, *cur
, *next
, *buffer
;
533 if (!fd
->inode
) return;
534 if (start
== end
|| start
> max_unix_offset
) return;
535 if (!end
|| end
> max_unix_offset
) end
= max_unix_offset
+ 1;
537 /* count the number of locks overlapping the specified area */
539 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
541 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
542 if (lock
->start
== lock
->end
) continue;
543 if (lock_overlaps( lock
, start
, end
)) count
++;
546 if (!count
) /* no locks at all, we can unlock everything */
548 set_unix_lock( fd
, start
, end
, F_UNLCK
);
552 /* allocate space for the list of holes */
553 /* max. number of holes is number of locks + 1 */
555 if (!(buffer
= malloc( sizeof(*buffer
) * (count
+1) ))) return;
559 first
->start
= start
;
563 /* build a sorted list of unlocked holes in the specified area */
565 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
567 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
568 if (lock
->start
== lock
->end
) continue;
569 if (!lock_overlaps( lock
, start
, end
)) continue;
571 /* go through all the holes touched by this lock */
572 for (cur
= first
; cur
; cur
= cur
->next
)
574 if (cur
->end
<= lock
->start
) continue; /* hole is before start of lock */
575 if (lock
->end
&& cur
->start
>= lock
->end
) break; /* hole is after end of lock */
577 /* now we know that lock is overlapping hole */
579 if (cur
->start
>= lock
->start
) /* lock starts before hole, shrink from start */
581 cur
->start
= lock
->end
;
582 if (cur
->start
&& cur
->start
< cur
->end
) break; /* done with this lock */
583 /* now hole is empty, remove it */
584 if (cur
->next
) cur
->next
->prev
= cur
->prev
;
585 if (cur
->prev
) cur
->prev
->next
= cur
->next
;
586 else if (!(first
= cur
->next
)) goto done
; /* no more holes at all */
588 else if (!lock
->end
|| cur
->end
<= lock
->end
) /* lock larger than hole, shrink from end */
590 cur
->end
= lock
->start
;
591 assert( cur
->start
< cur
->end
);
593 else /* lock is in the middle of hole, split hole in two */
596 next
->next
= cur
->next
;
598 next
->start
= lock
->end
;
599 next
->end
= cur
->end
;
600 cur
->end
= lock
->start
;
601 assert( next
->start
< next
->end
);
602 assert( cur
->end
< next
->start
);
604 break; /* done with this lock */
609 /* clear Unix locks for all the holes */
611 for (cur
= first
; cur
; cur
= cur
->next
)
612 set_unix_lock( fd
, cur
->start
, cur
->end
, F_UNLCK
);
618 /* create a new lock on a fd */
619 static struct file_lock
*add_lock( struct fd
*fd
, int shared
, file_pos_t start
, file_pos_t end
)
621 struct file_lock
*lock
;
623 if (!fd
->inode
) /* not a regular file */
625 set_error( STATUS_INVALID_HANDLE
);
629 if (!(lock
= alloc_object( &file_lock_ops
))) return NULL
;
630 lock
->shared
= shared
;
634 lock
->process
= current
->process
;
636 /* now try to set a Unix lock */
637 if (!set_unix_lock( lock
->fd
, lock
->start
, lock
->end
, lock
->shared
? F_RDLCK
: F_WRLCK
))
639 release_object( lock
);
642 list_add_head( &fd
->locks
, &lock
->fd_entry
);
643 list_add_head( &fd
->inode
->locks
, &lock
->inode_entry
);
644 list_add_head( &lock
->process
->locks
, &lock
->proc_entry
);
648 /* remove an existing lock */
649 static void remove_lock( struct file_lock
*lock
, int remove_unix
)
651 struct inode
*inode
= lock
->fd
->inode
;
653 list_remove( &lock
->fd_entry
);
654 list_remove( &lock
->inode_entry
);
655 list_remove( &lock
->proc_entry
);
656 if (remove_unix
) remove_unix_locks( lock
->fd
, lock
->start
, lock
->end
);
657 if (list_empty( &inode
->locks
)) inode_close_pending( inode
);
658 lock
->process
= NULL
;
659 wake_up( &lock
->obj
, 0 );
660 release_object( lock
);
663 /* remove all locks owned by a given process */
664 void remove_process_locks( struct process
*process
)
668 while ((ptr
= list_head( &process
->locks
)))
670 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, proc_entry
);
671 remove_lock( lock
, 1 ); /* this removes it from the list */
675 /* remove all locks on a given fd */
676 static void remove_fd_locks( struct fd
*fd
)
678 file_pos_t start
= FILE_POS_T_MAX
, end
= 0;
681 while ((ptr
= list_head( &fd
->locks
)))
683 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
684 if (lock
->start
< start
) start
= lock
->start
;
685 if (!lock
->end
|| lock
->end
> end
) end
= lock
->end
- 1;
686 remove_lock( lock
, 0 );
688 if (start
< end
) remove_unix_locks( fd
, start
, end
+ 1 );
691 /* add a lock on an fd */
692 /* returns handle to wait on */
693 obj_handle_t
lock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
, int shared
, int wait
)
696 file_pos_t end
= start
+ count
;
698 /* don't allow wrapping locks */
699 if (end
&& end
< start
)
701 set_error( STATUS_INVALID_PARAMETER
);
705 /* check if another lock on that file overlaps the area */
706 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
708 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
709 if (!lock_overlaps( lock
, start
, end
)) continue;
710 if (lock
->shared
&& shared
) continue;
714 set_error( STATUS_FILE_LOCK_CONFLICT
);
717 set_error( STATUS_PENDING
);
718 return alloc_handle( current
->process
, lock
, SYNCHRONIZE
, 0 );
721 /* not found, add it */
722 if (add_lock( fd
, shared
, start
, end
)) return 0;
723 if (get_error() == STATUS_FILE_LOCK_CONFLICT
)
725 /* Unix lock conflict -> tell client to wait and retry */
726 if (wait
) set_error( STATUS_PENDING
);
731 /* remove a lock on an fd */
732 void unlock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
)
735 file_pos_t end
= start
+ count
;
737 /* find an existing lock with the exact same parameters */
738 LIST_FOR_EACH( ptr
, &fd
->locks
)
740 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
741 if ((lock
->start
== start
) && (lock
->end
== end
))
743 remove_lock( lock
, 1 );
747 set_error( STATUS_FILE_LOCK_CONFLICT
);
751 /****************************************************************/
752 /* file descriptor functions */
754 static void fd_dump( struct object
*obj
, int verbose
)
756 struct fd
*fd
= (struct fd
*)obj
;
757 fprintf( stderr
, "Fd unix_fd=%d user=%p\n", fd
->unix_fd
, fd
->user
);
760 static void fd_destroy( struct object
*obj
)
762 struct fd
*fd
= (struct fd
*)obj
;
764 remove_fd_locks( fd
);
765 list_remove( &fd
->inode_entry
);
766 if (fd
->poll_index
!= -1) remove_poll_user( fd
, fd
->poll_index
);
769 inode_add_closed_fd( fd
->inode
, fd
->closed
);
770 release_object( fd
->inode
);
772 else /* no inode, close it right away */
774 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
778 /* set the events that select waits for on this fd */
779 void set_fd_events( struct fd
*fd
, int events
)
781 int user
= fd
->poll_index
;
782 assert( poll_users
[user
] == fd
);
783 if (events
== -1) /* stop waiting on this fd completely */
785 pollfd
[user
].fd
= -1;
786 pollfd
[user
].events
= POLLERR
;
787 pollfd
[user
].revents
= 0;
789 else if (pollfd
[user
].fd
!= -1 || !pollfd
[user
].events
)
791 pollfd
[user
].fd
= fd
->unix_fd
;
792 pollfd
[user
].events
= events
;
796 /* allocate an fd object, without setting the unix fd yet */
797 struct fd
*alloc_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
)
799 struct fd
*fd
= alloc_object( &fd_ops
);
801 if (!fd
) return NULL
;
803 fd
->fd_ops
= fd_user_ops
;
809 list_init( &fd
->inode_entry
);
810 list_init( &fd
->locks
);
812 if ((fd
->poll_index
= add_poll_user( fd
)) == -1)
814 release_object( fd
);
820 /* open() wrapper using a struct fd */
821 /* the fd must have been created with alloc_fd */
822 /* on error the fd object is released */
823 struct fd
*open_fd( struct fd
*fd
, const char *name
, int flags
, mode_t
*mode
)
826 struct closed_fd
*closed_fd
;
828 assert( fd
->unix_fd
== -1 );
830 if (!(closed_fd
= mem_alloc( sizeof(*closed_fd
) )))
832 release_object( fd
);
835 if ((fd
->unix_fd
= open( name
, flags
, *mode
)) == -1)
838 release_object( fd
);
842 closed_fd
->fd
= fd
->unix_fd
;
843 fstat( fd
->unix_fd
, &st
);
846 if (S_ISREG(st
.st_mode
)) /* only bother with an inode for normal files */
848 struct inode
*inode
= get_inode( st
.st_dev
, st
.st_ino
);
852 /* we can close the fd because there are no others open on the same file,
853 * otherwise we wouldn't have failed to allocate a new inode
855 release_object( fd
);
860 fd
->closed
= closed_fd
;
861 list_add_head( &inode
->open
, &fd
->inode_entry
);
870 /* create an fd for an anonymous file */
871 /* if the function fails the unix fd is closed */
872 struct fd
*create_anonymous_fd( const struct fd_ops
*fd_user_ops
, int unix_fd
, struct object
*user
)
874 struct fd
*fd
= alloc_fd( fd_user_ops
, user
);
878 fd
->unix_fd
= unix_fd
;
885 /* retrieve the object that is using an fd */
886 void *get_fd_user( struct fd
*fd
)
891 /* retrieve the unix fd for an object */
892 int get_unix_fd( struct fd
*fd
)
897 /* callback for event happening in the main poll() loop */
898 void fd_poll_event( struct fd
*fd
, int event
)
900 return fd
->fd_ops
->poll_event( fd
, event
);
903 /* check if events are pending and if yes return which one(s) */
904 int check_fd_events( struct fd
*fd
, int events
)
908 pfd
.fd
= fd
->unix_fd
;
910 if (poll( &pfd
, 1, 0 ) <= 0) return 0;
914 /* default add_queue() routine for objects that poll() on an fd */
915 int default_fd_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
917 struct fd
*fd
= get_obj_fd( obj
);
920 if (!obj
->head
) /* first on the queue */
921 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
922 add_queue( obj
, entry
);
923 release_object( fd
);
927 /* default remove_queue() routine for objects that poll() on an fd */
928 void default_fd_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
930 struct fd
*fd
= get_obj_fd( obj
);
933 remove_queue( obj
, entry
);
934 if (!obj
->head
) /* last on the queue is gone */
935 set_fd_events( fd
, 0 );
936 release_object( obj
);
937 release_object( fd
);
940 /* default signaled() routine for objects that poll() on an fd */
941 int default_fd_signaled( struct object
*obj
, struct thread
*thread
)
943 struct fd
*fd
= get_obj_fd( obj
);
944 int events
= fd
->fd_ops
->get_poll_events( fd
);
945 int ret
= check_fd_events( fd
, events
) != 0;
948 set_fd_events( fd
, 0 ); /* stop waiting on select() if we are signaled */
950 set_fd_events( fd
, events
); /* restart waiting on poll() if we are no longer signaled */
952 release_object( fd
);
956 /* default handler for poll() events */
957 void default_poll_event( struct fd
*fd
, int event
)
959 /* an error occurred, stop polling this fd to avoid busy-looping */
960 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
961 wake_up( fd
->user
, 0 );
964 /* default flush() routine */
965 int no_flush( struct fd
*fd
, struct event
**event
)
967 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
971 /* default get_file_info() routine */
972 int no_get_file_info( struct fd
*fd
, struct get_file_info_reply
*info
, int *flags
)
974 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
976 return FD_TYPE_INVALID
;
979 /* default queue_async() routine */
980 void no_queue_async( struct fd
*fd
, void* ptr
, unsigned int status
, int type
, int count
)
982 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
985 /* same as get_handle_obj but retrieve the struct fd associated to the object */
986 static struct fd
*get_handle_fd_obj( struct process
*process
, obj_handle_t handle
,
987 unsigned int access
)
989 struct fd
*fd
= NULL
;
992 if ((obj
= get_handle_obj( process
, handle
, access
, NULL
)))
994 fd
= get_obj_fd( obj
);
995 release_object( obj
);
1000 /* flush a file buffers */
1001 DECL_HANDLER(flush_file
)
1003 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1004 struct event
* event
= NULL
;
1008 fd
->fd_ops
->flush( fd
, &event
);
1011 reply
->event
= alloc_handle( current
->process
, event
, SYNCHRONIZE
, 0 );
1013 release_object( fd
);
1017 /* get a Unix fd to access a file */
1018 DECL_HANDLER(get_handle_fd
)
1023 reply
->type
= FD_TYPE_INVALID
;
1025 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, req
->access
)))
1027 int unix_fd
= get_handle_unix_fd( current
->process
, req
->handle
, req
->access
);
1028 if (unix_fd
!= -1) reply
->fd
= unix_fd
;
1029 else if (!get_error())
1031 unix_fd
= fd
->unix_fd
;
1032 if (unix_fd
!= -1) send_client_fd( current
->process
, unix_fd
, req
->handle
);
1034 reply
->type
= fd
->fd_ops
->get_file_info( fd
, NULL
, &reply
->flags
);
1035 release_object( fd
);
1039 /* get a file information */
1040 DECL_HANDLER(get_file_info
)
1042 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1047 fd
->fd_ops
->get_file_info( fd
, reply
, &flags
);
1048 release_object( fd
);
1052 /* create / reschedule an async I/O */
1053 DECL_HANDLER(register_async
)
1055 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1058 * The queue_async method must do the following:
1060 * 1. Get the async_queue for the request of given type.
1061 * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
1062 * 3. If status is STATUS_PENDING:
1063 * a) If no async request found in step 2 (new request): call create_async() to initialize one.
1064 * b) Set request's status to STATUS_PENDING.
1065 * c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
1067 * If the async request was found in step 2, destroy it by calling destroy_async().
1068 * 4. Carry out any operations necessary to adjust the object's poll events
1069 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1071 * See also the implementations in file.c, serial.c, and sock.c.
1076 fd
->fd_ops
->queue_async( fd
, req
->overlapped
, req
->status
, req
->type
, req
->count
);
1077 release_object( fd
);