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
33 #ifdef HAVE_SYS_POLL_H
38 #include <sys/types.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 */
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) */
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 */
89 no_add_queue
, /* add_queue */
90 NULL
, /* remove_queue */
93 no_get_fd
, /* get_fd */
94 fd_destroy
/* destroy */
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 */
121 NULL
, /* satisfied */
122 no_get_fd
, /* get_fd */
123 inode_destroy
/* destroy */
126 /* file lock object */
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) */
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) ); \
166 fprintf( stderr, "%lx", (unsigned long)(val) ); \
171 /****************************************************************/
172 /* timeouts support */
176 struct list entry
; /* entry in sorted timeout list */
177 struct timeval when
; /* timeout expiry (absolute time) */
178 timeout_callback callback
; /* callback function */
179 void *private; /* callback private data */
182 static struct list timeout_list
= LIST_INIT(timeout_list
); /* sorted timeouts list */
184 /* add a timeout user */
185 struct timeout_user
*add_timeout_user( struct timeval
*when
, timeout_callback func
, void *private )
187 struct timeout_user
*user
;
190 if (!(user
= mem_alloc( sizeof(*user
) ))) return NULL
;
192 user
->callback
= func
;
193 user
->private = private;
195 /* Now insert it in the linked list */
197 LIST_FOR_EACH( ptr
, &timeout_list
)
199 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
200 if (!time_before( &timeout
->when
, when
)) break;
202 list_add_before( ptr
, &user
->entry
);
206 /* remove a timeout user */
207 void remove_timeout_user( struct timeout_user
*user
)
209 list_remove( &user
->entry
);
213 /* add a timeout in milliseconds to an absolute time */
214 void add_timeout( struct timeval
*when
, int timeout
)
218 long sec
= timeout
/ 1000;
219 if ((when
->tv_usec
+= (timeout
- 1000*sec
) * 1000) >= 1000000)
221 when
->tv_usec
-= 1000000;
229 /****************************************************************/
232 static struct fd
**poll_users
; /* users array */
233 static struct pollfd
*pollfd
; /* poll fd array */
234 static int nb_users
; /* count of array entries actually in use */
235 static int active_users
; /* current number of active users */
236 static int allocated_users
; /* count of allocated entries in the array */
237 static struct fd
**freelist
; /* list of free entries in the array */
239 /* add a user in the poll array and return its index, or -1 on failure */
240 static int add_poll_user( struct fd
*fd
)
245 ret
= freelist
- poll_users
;
246 freelist
= (struct fd
**)poll_users
[ret
];
250 if (nb_users
== allocated_users
)
252 struct fd
**newusers
;
253 struct pollfd
*newpoll
;
254 int new_count
= allocated_users
? (allocated_users
+ allocated_users
/ 2) : 16;
255 if (!(newusers
= realloc( poll_users
, new_count
* sizeof(*poll_users
) ))) return -1;
256 if (!(newpoll
= realloc( pollfd
, new_count
* sizeof(*pollfd
) )))
259 poll_users
= newusers
;
264 poll_users
= newusers
;
266 allocated_users
= new_count
;
271 pollfd
[ret
].events
= 0;
272 pollfd
[ret
].revents
= 0;
273 poll_users
[ret
] = fd
;
278 /* remove a user from the poll list */
279 static void remove_poll_user( struct fd
*fd
, int user
)
282 assert( poll_users
[user
] == fd
);
283 pollfd
[user
].fd
= -1;
284 pollfd
[user
].events
= 0;
285 pollfd
[user
].revents
= 0;
286 poll_users
[user
] = (struct fd
*)freelist
;
287 freelist
= &poll_users
[user
];
291 /* process pending timeouts and return the time until the next timeout, in milliseconds */
292 static int get_next_timeout(void)
294 if (!list_empty( &timeout_list
))
296 struct list expired_list
, *ptr
;
299 gettimeofday( &now
, NULL
);
301 /* first remove all expired timers from the list */
303 list_init( &expired_list
);
304 while ((ptr
= list_head( &timeout_list
)) != NULL
)
306 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
308 if (!time_before( &now
, &timeout
->when
))
310 list_remove( &timeout
->entry
);
311 list_add_tail( &expired_list
, &timeout
->entry
);
316 /* now call the callback for all the removed timers */
318 while ((ptr
= list_head( &expired_list
)) != NULL
)
320 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
321 list_remove( &timeout
->entry
);
322 timeout
->callback( timeout
->private );
326 if ((ptr
= list_head( &timeout_list
)) != NULL
)
328 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
329 int diff
= (timeout
->when
.tv_sec
- now
.tv_sec
) * 1000
330 + (timeout
->when
.tv_usec
- now
.tv_usec
) / 1000;
331 if (diff
< 0) diff
= 0;
335 return -1; /* no pending timeouts */
338 /* server main poll() loop */
345 timeout
= get_next_timeout();
347 if (!active_users
) break; /* last user removed by a timeout */
349 ret
= poll( pollfd
, nb_users
, timeout
);
352 for (i
= 0; i
< nb_users
; i
++)
354 if (pollfd
[i
].revents
)
356 fd_poll_event( poll_users
[i
], pollfd
[i
].revents
);
365 /****************************************************************/
366 /* inode functions */
370 static struct list inode_hash
[HASH_SIZE
];
372 /* close all pending file descriptors in the closed list */
373 static void inode_close_pending( struct inode
*inode
)
375 struct list
*ptr
= list_head( &inode
->closed
);
379 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
380 struct list
*next
= list_next( &inode
->closed
, ptr
);
387 if (!fd
->unlink
) /* get rid of it unless there's an unlink pending on that file */
397 static void inode_dump( struct object
*obj
, int verbose
)
399 struct inode
*inode
= (struct inode
*)obj
;
400 fprintf( stderr
, "Inode dev=" );
401 DUMP_LONG_LONG( inode
->dev
);
402 fprintf( stderr
, " ino=" );
403 DUMP_LONG_LONG( inode
->ino
);
404 fprintf( stderr
, "\n" );
407 static void inode_destroy( struct object
*obj
)
409 struct inode
*inode
= (struct inode
*)obj
;
412 assert( list_empty(&inode
->open
) );
413 assert( list_empty(&inode
->locks
) );
415 list_remove( &inode
->entry
);
417 while ((ptr
= list_head( &inode
->closed
)))
419 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
421 if (fd
->fd
!= -1) close( fd
->fd
);
424 /* make sure it is still the same file */
426 if (!stat( fd
->unlink
, &st
) && st
.st_dev
== inode
->dev
&& st
.st_ino
== inode
->ino
)
428 if (S_ISDIR(st
.st_mode
)) rmdir( fd
->unlink
);
429 else unlink( fd
->unlink
);
436 /* retrieve the inode object for a given fd, creating it if needed */
437 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
441 unsigned int hash
= (dev
^ ino
) % HASH_SIZE
;
443 if (inode_hash
[hash
].next
)
445 LIST_FOR_EACH( ptr
, &inode_hash
[hash
] )
447 inode
= LIST_ENTRY( ptr
, struct inode
, entry
);
448 if (inode
->dev
== dev
&& inode
->ino
== ino
)
449 return (struct inode
*)grab_object( inode
);
452 else list_init( &inode_hash
[hash
] );
454 /* not found, create it */
455 if ((inode
= alloc_object( &inode_ops
)))
460 list_init( &inode
->open
);
461 list_init( &inode
->locks
);
462 list_init( &inode
->closed
);
463 list_add_head( &inode_hash
[hash
], &inode
->entry
);
468 /* add fd to the indoe list of file descriptors to close */
469 static void inode_add_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
471 if (!list_empty( &inode
->locks
))
473 list_add_head( &inode
->closed
, &fd
->entry
);
475 else if (fd
->unlink
[0]) /* close the fd but keep the structure around for unlink */
479 list_add_head( &inode
->closed
, &fd
->entry
);
481 else /* no locks on this inode and no unlink, get rid of the fd */
489 /****************************************************************/
490 /* file lock functions */
492 static void file_lock_dump( struct object
*obj
, int verbose
)
494 struct file_lock
*lock
= (struct file_lock
*)obj
;
495 fprintf( stderr
, "Lock %s fd=%p proc=%p start=",
496 lock
->shared
? "shared" : "excl", lock
->fd
, lock
->process
);
497 DUMP_LONG_LONG( lock
->start
);
498 fprintf( stderr
, " end=" );
499 DUMP_LONG_LONG( lock
->end
);
500 fprintf( stderr
, "\n" );
503 static int file_lock_signaled( struct object
*obj
, struct thread
*thread
)
505 struct file_lock
*lock
= (struct file_lock
*)obj
;
506 /* lock is signaled if it has lost its owner */
507 return !lock
->process
;
510 /* set (or remove) a Unix lock if possible for the given range */
511 static int set_unix_lock( struct fd
*fd
, file_pos_t start
, file_pos_t end
, int type
)
515 if (!fd
->fs_locks
) return 1; /* no fs locks possible for this fd */
518 if (start
== end
) return 1; /* can't set zero-byte lock */
519 if (start
> max_unix_offset
) return 1; /* ignore it */
521 fl
.l_whence
= SEEK_SET
;
523 if (!end
|| end
> max_unix_offset
) fl
.l_len
= 0;
524 else fl
.l_len
= end
- start
;
525 if (fcntl( fd
->unix_fd
, F_SETLK
, &fl
) != -1) return 1;
530 /* check whether locks work at all on this file system */
531 if (fcntl( fd
->unix_fd
, F_GETLK
, &fl
) != -1)
533 set_error( STATUS_FILE_LOCK_CONFLICT
);
539 /* no locking on this fs, just ignore it */
543 set_error( STATUS_FILE_LOCK_CONFLICT
);
546 /* this can happen if we try to set a write lock on a read-only file */
547 /* we just ignore that error */
548 if (fl
.l_type
== F_WRLCK
) return 1;
549 set_error( STATUS_ACCESS_DENIED
);
555 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
556 /* in that case we shrink the limit and retry */
557 if (max_unix_offset
> INT_MAX
)
559 max_unix_offset
= INT_MAX
;
570 /* check if interval [start;end) overlaps the lock */
571 inline static int lock_overlaps( struct file_lock
*lock
, file_pos_t start
, file_pos_t end
)
573 if (lock
->end
&& start
>= lock
->end
) return 0;
574 if (end
&& lock
->start
>= end
) return 0;
578 /* remove Unix locks for all bytes in the specified area that are no longer locked */
579 static void remove_unix_locks( struct fd
*fd
, file_pos_t start
, file_pos_t end
)
587 } *first
, *cur
, *next
, *buffer
;
592 if (!fd
->inode
) return;
593 if (!fd
->fs_locks
) return;
594 if (start
== end
|| start
> max_unix_offset
) return;
595 if (!end
|| end
> max_unix_offset
) end
= max_unix_offset
+ 1;
597 /* count the number of locks overlapping the specified area */
599 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
601 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
602 if (lock
->start
== lock
->end
) continue;
603 if (lock_overlaps( lock
, start
, end
)) count
++;
606 if (!count
) /* no locks at all, we can unlock everything */
608 set_unix_lock( fd
, start
, end
, F_UNLCK
);
612 /* allocate space for the list of holes */
613 /* max. number of holes is number of locks + 1 */
615 if (!(buffer
= malloc( sizeof(*buffer
) * (count
+1) ))) return;
619 first
->start
= start
;
623 /* build a sorted list of unlocked holes in the specified area */
625 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
627 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
628 if (lock
->start
== lock
->end
) continue;
629 if (!lock_overlaps( lock
, start
, end
)) continue;
631 /* go through all the holes touched by this lock */
632 for (cur
= first
; cur
; cur
= cur
->next
)
634 if (cur
->end
<= lock
->start
) continue; /* hole is before start of lock */
635 if (lock
->end
&& cur
->start
>= lock
->end
) break; /* hole is after end of lock */
637 /* now we know that lock is overlapping hole */
639 if (cur
->start
>= lock
->start
) /* lock starts before hole, shrink from start */
641 cur
->start
= lock
->end
;
642 if (cur
->start
&& cur
->start
< cur
->end
) break; /* done with this lock */
643 /* now hole is empty, remove it */
644 if (cur
->next
) cur
->next
->prev
= cur
->prev
;
645 if (cur
->prev
) cur
->prev
->next
= cur
->next
;
646 else if (!(first
= cur
->next
)) goto done
; /* no more holes at all */
648 else if (!lock
->end
|| cur
->end
<= lock
->end
) /* lock larger than hole, shrink from end */
650 cur
->end
= lock
->start
;
651 assert( cur
->start
< cur
->end
);
653 else /* lock is in the middle of hole, split hole in two */
656 next
->next
= cur
->next
;
658 next
->start
= lock
->end
;
659 next
->end
= cur
->end
;
660 cur
->end
= lock
->start
;
661 assert( next
->start
< next
->end
);
662 assert( cur
->end
< next
->start
);
664 break; /* done with this lock */
669 /* clear Unix locks for all the holes */
671 for (cur
= first
; cur
; cur
= cur
->next
)
672 set_unix_lock( fd
, cur
->start
, cur
->end
, F_UNLCK
);
678 /* create a new lock on a fd */
679 static struct file_lock
*add_lock( struct fd
*fd
, int shared
, file_pos_t start
, file_pos_t end
)
681 struct file_lock
*lock
;
683 if (!fd
->inode
) /* not a regular file */
685 set_error( STATUS_INVALID_HANDLE
);
689 if (!(lock
= alloc_object( &file_lock_ops
))) return NULL
;
690 lock
->shared
= shared
;
694 lock
->process
= current
->process
;
696 /* now try to set a Unix lock */
697 if (!set_unix_lock( lock
->fd
, lock
->start
, lock
->end
, lock
->shared
? F_RDLCK
: F_WRLCK
))
699 release_object( lock
);
702 list_add_head( &fd
->locks
, &lock
->fd_entry
);
703 list_add_head( &fd
->inode
->locks
, &lock
->inode_entry
);
704 list_add_head( &lock
->process
->locks
, &lock
->proc_entry
);
708 /* remove an existing lock */
709 static void remove_lock( struct file_lock
*lock
, int remove_unix
)
711 struct inode
*inode
= lock
->fd
->inode
;
713 list_remove( &lock
->fd_entry
);
714 list_remove( &lock
->inode_entry
);
715 list_remove( &lock
->proc_entry
);
716 if (remove_unix
) remove_unix_locks( lock
->fd
, lock
->start
, lock
->end
);
717 if (list_empty( &inode
->locks
)) inode_close_pending( inode
);
718 lock
->process
= NULL
;
719 wake_up( &lock
->obj
, 0 );
720 release_object( lock
);
723 /* remove all locks owned by a given process */
724 void remove_process_locks( struct process
*process
)
728 while ((ptr
= list_head( &process
->locks
)))
730 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, proc_entry
);
731 remove_lock( lock
, 1 ); /* this removes it from the list */
735 /* remove all locks on a given fd */
736 static void remove_fd_locks( struct fd
*fd
)
738 file_pos_t start
= FILE_POS_T_MAX
, end
= 0;
741 while ((ptr
= list_head( &fd
->locks
)))
743 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
744 if (lock
->start
< start
) start
= lock
->start
;
745 if (!lock
->end
|| lock
->end
> end
) end
= lock
->end
- 1;
746 remove_lock( lock
, 0 );
748 if (start
< end
) remove_unix_locks( fd
, start
, end
+ 1 );
751 /* add a lock on an fd */
752 /* returns handle to wait on */
753 obj_handle_t
lock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
, int shared
, int wait
)
756 file_pos_t end
= start
+ count
;
758 /* don't allow wrapping locks */
759 if (end
&& end
< start
)
761 set_error( STATUS_INVALID_PARAMETER
);
765 /* check if another lock on that file overlaps the area */
766 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
768 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
769 if (!lock_overlaps( lock
, start
, end
)) continue;
770 if (lock
->shared
&& shared
) continue;
774 set_error( STATUS_FILE_LOCK_CONFLICT
);
777 set_error( STATUS_PENDING
);
778 return alloc_handle( current
->process
, lock
, SYNCHRONIZE
, 0 );
781 /* not found, add it */
782 if (add_lock( fd
, shared
, start
, end
)) return 0;
783 if (get_error() == STATUS_FILE_LOCK_CONFLICT
)
785 /* Unix lock conflict -> tell client to wait and retry */
786 if (wait
) set_error( STATUS_PENDING
);
791 /* remove a lock on an fd */
792 void unlock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
)
795 file_pos_t end
= start
+ count
;
797 /* find an existing lock with the exact same parameters */
798 LIST_FOR_EACH( ptr
, &fd
->locks
)
800 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
801 if ((lock
->start
== start
) && (lock
->end
== end
))
803 remove_lock( lock
, 1 );
807 set_error( STATUS_FILE_LOCK_CONFLICT
);
811 /****************************************************************/
812 /* file descriptor functions */
814 static void fd_dump( struct object
*obj
, int verbose
)
816 struct fd
*fd
= (struct fd
*)obj
;
817 fprintf( stderr
, "Fd unix_fd=%d user=%p unlink='%s'\n",
818 fd
->unix_fd
, fd
->user
, fd
->closed
->unlink
);
821 static void fd_destroy( struct object
*obj
)
823 struct fd
*fd
= (struct fd
*)obj
;
825 remove_fd_locks( fd
);
826 list_remove( &fd
->inode_entry
);
827 if (fd
->poll_index
!= -1) remove_poll_user( fd
, fd
->poll_index
);
830 inode_add_closed_fd( fd
->inode
, fd
->closed
);
831 release_object( fd
->inode
);
833 else /* no inode, close it right away */
835 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
839 /* set the events that select waits for on this fd */
840 void set_fd_events( struct fd
*fd
, int events
)
842 int user
= fd
->poll_index
;
843 assert( poll_users
[user
] == fd
);
844 if (events
== -1) /* stop waiting on this fd completely */
846 pollfd
[user
].fd
= -1;
847 pollfd
[user
].events
= POLLERR
;
848 pollfd
[user
].revents
= 0;
850 else if (pollfd
[user
].fd
!= -1 || !pollfd
[user
].events
)
852 pollfd
[user
].fd
= fd
->unix_fd
;
853 pollfd
[user
].events
= events
;
857 /* allocate an fd object, without setting the unix fd yet */
858 struct fd
*alloc_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
)
860 struct fd
*fd
= alloc_object( &fd_ops
);
862 if (!fd
) return NULL
;
864 fd
->fd_ops
= fd_user_ops
;
873 list_init( &fd
->inode_entry
);
874 list_init( &fd
->locks
);
876 if ((fd
->poll_index
= add_poll_user( fd
)) == -1)
878 release_object( fd
);
884 /* check if the desired access is possible without violating */
885 /* the sharing mode of other opens of the same file */
886 static int check_sharing( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
888 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
889 unsigned int existing_access
= 0;
893 /* if access mode is 0, sharing mode is ignored */
894 if (!access
) sharing
= existing_sharing
;
896 fd
->sharing
= sharing
;
898 LIST_FOR_EACH( ptr
, &fd
->inode
->open
)
900 struct fd
*fd_ptr
= LIST_ENTRY( ptr
, struct fd
, inode_entry
);
903 existing_sharing
&= fd_ptr
->sharing
;
904 existing_access
|= fd_ptr
->access
;
905 if (fd_ptr
->closed
->unlink
[0]) unlink
= 1;
909 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) return 0;
910 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
911 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) return 0;
912 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
913 if (fd
->closed
->unlink
[0] && !(existing_sharing
& FILE_SHARE_DELETE
)) return 0;
914 if (unlink
&& !(sharing
& FILE_SHARE_DELETE
)) return 0;
918 /* open() wrapper using a struct fd */
919 /* the fd must have been created with alloc_fd */
920 /* on error the fd object is released */
921 struct fd
*open_fd( struct fd
*fd
, const char *name
, int flags
, mode_t
*mode
,
922 unsigned int access
, unsigned int sharing
, unsigned int options
)
925 struct closed_fd
*closed_fd
;
926 const char *unlink_name
= "";
928 assert( fd
->unix_fd
== -1 );
930 if (options
& FILE_DELETE_ON_CLOSE
) unlink_name
= name
;
931 if (!(closed_fd
= mem_alloc( sizeof(*closed_fd
) + strlen(unlink_name
) )))
933 release_object( fd
);
936 /* create the directory if needed */
937 if ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_CREAT
))
939 if (mkdir( name
, 0777 ) == -1)
941 if (errno
!= EEXIST
|| (flags
& O_EXCL
))
944 release_object( fd
);
949 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
951 if ((fd
->unix_fd
= open( name
, flags
& ~O_TRUNC
, *mode
)) == -1)
954 release_object( fd
);
958 closed_fd
->fd
= fd
->unix_fd
;
959 closed_fd
->unlink
[0] = 0;
960 fstat( fd
->unix_fd
, &st
);
963 /* only bother with an inode for normal files and directories */
964 if (S_ISREG(st
.st_mode
) || S_ISDIR(st
.st_mode
))
966 struct inode
*inode
= get_inode( st
.st_dev
, st
.st_ino
);
970 /* we can close the fd because there are no others open on the same file,
971 * otherwise we wouldn't have failed to allocate a new inode
976 fd
->closed
= closed_fd
;
977 list_add_head( &inode
->open
, &fd
->inode_entry
);
979 /* check directory options */
980 if ((options
& FILE_DIRECTORY_FILE
) && !S_ISDIR(st
.st_mode
))
982 release_object( fd
);
983 set_error( STATUS_NOT_A_DIRECTORY
);
986 if ((options
& FILE_NON_DIRECTORY_FILE
) && S_ISDIR(st
.st_mode
))
988 release_object( fd
);
989 set_error( STATUS_FILE_IS_A_DIRECTORY
);
992 if (!check_sharing( fd
, access
, sharing
))
994 release_object( fd
);
995 set_error( STATUS_SHARING_VIOLATION
);
998 strcpy( closed_fd
->unlink
, unlink_name
);
999 if (flags
& O_TRUNC
) ftruncate( fd
->unix_fd
, 0 );
1001 else /* special file */
1003 if (options
& FILE_DIRECTORY_FILE
)
1005 set_error( STATUS_NOT_A_DIRECTORY
);
1008 if (unlink_name
[0]) /* we can't unlink special files */
1010 set_error( STATUS_INVALID_PARAMETER
);
1018 release_object( fd
);
1023 /* create an fd for an anonymous file */
1024 /* if the function fails the unix fd is closed */
1025 struct fd
*create_anonymous_fd( const struct fd_ops
*fd_user_ops
, int unix_fd
, struct object
*user
)
1027 struct fd
*fd
= alloc_fd( fd_user_ops
, user
);
1031 fd
->unix_fd
= unix_fd
;
1038 /* retrieve the object that is using an fd */
1039 void *get_fd_user( struct fd
*fd
)
1044 /* retrieve the unix fd for an object */
1045 int get_unix_fd( struct fd
*fd
)
1050 /* check if two file descriptors point to the same file */
1051 int is_same_file_fd( struct fd
*fd1
, struct fd
*fd2
)
1053 return fd1
->inode
== fd2
->inode
;
1056 /* callback for event happening in the main poll() loop */
1057 void fd_poll_event( struct fd
*fd
, int event
)
1059 return fd
->fd_ops
->poll_event( fd
, event
);
1062 /* check if events are pending and if yes return which one(s) */
1063 int check_fd_events( struct fd
*fd
, int events
)
1067 pfd
.fd
= fd
->unix_fd
;
1068 pfd
.events
= events
;
1069 if (poll( &pfd
, 1, 0 ) <= 0) return 0;
1073 /* default add_queue() routine for objects that poll() on an fd */
1074 int default_fd_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1076 struct fd
*fd
= get_obj_fd( obj
);
1079 if (!obj
->head
) /* first on the queue */
1080 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
1081 add_queue( obj
, entry
);
1082 release_object( fd
);
1086 /* default remove_queue() routine for objects that poll() on an fd */
1087 void default_fd_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1089 struct fd
*fd
= get_obj_fd( obj
);
1092 remove_queue( obj
, entry
);
1093 if (!obj
->head
) /* last on the queue is gone */
1094 set_fd_events( fd
, 0 );
1095 release_object( obj
);
1096 release_object( fd
);
1099 /* default signaled() routine for objects that poll() on an fd */
1100 int default_fd_signaled( struct object
*obj
, struct thread
*thread
)
1102 struct fd
*fd
= get_obj_fd( obj
);
1103 int events
= fd
->fd_ops
->get_poll_events( fd
);
1104 int ret
= check_fd_events( fd
, events
) != 0;
1107 set_fd_events( fd
, 0 ); /* stop waiting on select() if we are signaled */
1109 set_fd_events( fd
, events
); /* restart waiting on poll() if we are no longer signaled */
1111 release_object( fd
);
1115 /* default handler for poll() events */
1116 void default_poll_event( struct fd
*fd
, int event
)
1118 /* an error occurred, stop polling this fd to avoid busy-looping */
1119 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
1120 wake_up( fd
->user
, 0 );
1123 /* default flush() routine */
1124 int no_flush( struct fd
*fd
, struct event
**event
)
1126 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1130 /* default get_file_info() routine */
1131 int no_get_file_info( struct fd
*fd
)
1133 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1137 /* default queue_async() routine */
1138 void no_queue_async( struct fd
*fd
, void* ptr
, unsigned int status
, int type
, int count
)
1140 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1143 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1144 static struct fd
*get_handle_fd_obj( struct process
*process
, obj_handle_t handle
,
1145 unsigned int access
)
1147 struct fd
*fd
= NULL
;
1150 if ((obj
= get_handle_obj( process
, handle
, access
, NULL
)))
1152 fd
= get_obj_fd( obj
);
1153 release_object( obj
);
1158 /* flush a file buffers */
1159 DECL_HANDLER(flush_file
)
1161 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1162 struct event
* event
= NULL
;
1166 fd
->fd_ops
->flush( fd
, &event
);
1169 reply
->event
= alloc_handle( current
->process
, event
, SYNCHRONIZE
, 0 );
1171 release_object( fd
);
1175 /* get a Unix fd to access a file */
1176 DECL_HANDLER(get_handle_fd
)
1182 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, req
->access
)))
1184 int unix_fd
= get_handle_unix_fd( current
->process
, req
->handle
, req
->access
);
1185 if (unix_fd
!= -1) reply
->fd
= unix_fd
;
1186 else if (!get_error())
1188 assert( fd
->unix_fd
!= -1 );
1189 send_client_fd( current
->process
, fd
->unix_fd
, req
->handle
);
1191 reply
->flags
= fd
->fd_ops
->get_file_info( fd
);
1192 release_object( fd
);
1196 /* create / reschedule an async I/O */
1197 DECL_HANDLER(register_async
)
1199 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1202 * The queue_async method must do the following:
1204 * 1. Get the async_queue for the request of given type.
1205 * 2. Call find_async() to look for the specific client request in the queue (=> NULL if not found).
1206 * 3. If status is STATUS_PENDING:
1207 * a) If no async request found in step 2 (new request): call create_async() to initialize one.
1208 * b) Set request's status to STATUS_PENDING.
1209 * c) If the "queue" field of the async request is NULL: call async_insert() to put it into the queue.
1211 * If the async request was found in step 2, destroy it by calling destroy_async().
1212 * 4. Carry out any operations necessary to adjust the object's poll events
1213 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1215 * See also the implementations in file.c, serial.c, and sock.c.
1220 fd
->fd_ops
->queue_async( fd
, req
->overlapped
, req
->status
, req
->type
, req
->count
);
1221 release_object( fd
);