Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side file descriptor management |
| 3 | * |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000, 2003 Alexandre Julliard |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | |
| 22 | #include "config.h" |
Alexandre Julliard | 49b7fdc | 2005-08-03 21:25:10 +0000 | [diff] [blame] | 23 | #include "wine/port.h" |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 28 | #include <limits.h> |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
Steven Edwards | 5727918 | 2005-03-04 12:38:36 +0000 | [diff] [blame] | 34 | #ifdef HAVE_POLL_H |
| 35 | #include <poll.h> |
| 36 | #endif |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 37 | #ifdef HAVE_SYS_POLL_H |
| 38 | #include <sys/poll.h> |
| 39 | #endif |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 40 | #ifdef HAVE_LINUX_MAJOR_H |
| 41 | #include <linux/major.h> |
| 42 | #endif |
| 43 | #ifdef HAVE_SYS_STATVFS_H |
| 44 | #include <sys/statvfs.h> |
| 45 | #endif |
| 46 | #ifdef HAVE_SYS_VFS_H |
Petr Sumbera | f4575aa | 2008-04-10 17:03:18 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Solaris defines its system list in sys/list.h. |
| 49 | * This need to be workaround it here. |
| 50 | */ |
| 51 | #define list SYSLIST |
| 52 | #define list_next SYSLIST_NEXT |
| 53 | #define list_prev SYSLIST_PREV |
| 54 | #define list_head SYSLIST_HEAD |
| 55 | #define list_tail SYSLIST_TAIL |
| 56 | #define list_move_tail SYSLIST_MOVE_TAIL |
| 57 | #define list_remove SYSLIST_REMOVE |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 58 | #include <sys/vfs.h> |
Petr Sumbera | f4575aa | 2008-04-10 17:03:18 +0200 | [diff] [blame] | 59 | #undef list |
| 60 | #undef list_next |
| 61 | #undef list_prev |
| 62 | #undef list_head |
| 63 | #undef list_tail |
| 64 | #undef list_move_tail |
| 65 | #undef list_remove |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 66 | #endif |
Gerald Pfeifer | 35f0a41 | 2006-11-05 06:50:45 +0100 | [diff] [blame] | 67 | #ifdef HAVE_SYS_PARAM_H |
| 68 | #include <sys/param.h> |
| 69 | #endif |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 70 | #ifdef HAVE_SYS_MOUNT_H |
| 71 | #include <sys/mount.h> |
| 72 | #endif |
| 73 | #ifdef HAVE_SYS_STATFS_H |
| 74 | #include <sys/statfs.h> |
| 75 | #endif |
Alexandre Julliard | 8143d42 | 2007-06-15 12:41:43 +0200 | [diff] [blame] | 76 | #ifdef HAVE_SYS_SYSCTL_H |
| 77 | #include <sys/sysctl.h> |
| 78 | #endif |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 79 | #ifdef HAVE_SYS_EVENT_H |
| 80 | #include <sys/event.h> |
| 81 | #undef LIST_INIT |
| 82 | #undef LIST_ENTRY |
| 83 | #endif |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 84 | #ifdef HAVE_STDINT_H |
| 85 | #include <stdint.h> |
| 86 | #endif |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 87 | #include <sys/stat.h> |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 88 | #include <sys/time.h> |
| 89 | #include <sys/types.h> |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 90 | #include <unistd.h> |
| 91 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 92 | #include "ntstatus.h" |
| 93 | #define WIN32_NO_STATUS |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 94 | #include "object.h" |
| 95 | #include "file.h" |
| 96 | #include "handle.h" |
| 97 | #include "process.h" |
| 98 | #include "request.h" |
| 99 | |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 100 | #include "winternl.h" |
Alexandre Julliard | 2669af7 | 2007-04-16 14:54:52 +0200 | [diff] [blame] | 101 | #include "winioctl.h" |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 102 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 103 | #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE) |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 104 | # include <sys/epoll.h> |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 105 | # define USE_EPOLL |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 106 | #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H) |
| 107 | # define USE_EPOLL |
| 108 | # define EPOLLIN POLLIN |
| 109 | # define EPOLLOUT POLLOUT |
| 110 | # define EPOLLERR POLLERR |
| 111 | # define EPOLLHUP POLLHUP |
| 112 | # define EPOLL_CTL_ADD 1 |
| 113 | # define EPOLL_CTL_DEL 2 |
| 114 | # define EPOLL_CTL_MOD 3 |
| 115 | |
| 116 | typedef union epoll_data |
| 117 | { |
| 118 | void *ptr; |
| 119 | int fd; |
| 120 | uint32_t u32; |
| 121 | uint64_t u64; |
| 122 | } epoll_data_t; |
| 123 | |
| 124 | struct epoll_event |
| 125 | { |
| 126 | uint32_t events; |
| 127 | epoll_data_t data; |
| 128 | }; |
| 129 | |
| 130 | #define SYSCALL_RET(ret) do { \ |
| 131 | if (ret < 0) { errno = -ret; ret = -1; } \ |
| 132 | return ret; \ |
| 133 | } while(0) |
| 134 | |
| 135 | static inline int epoll_create( int size ) |
| 136 | { |
| 137 | int ret; |
| 138 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 139 | : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) ); |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 140 | SYSCALL_RET(ret); |
| 141 | } |
| 142 | |
| 143 | static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event ) |
| 144 | { |
| 145 | int ret; |
| 146 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 147 | : "=a" (ret) |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 148 | : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) ); |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 149 | SYSCALL_RET(ret); |
| 150 | } |
| 151 | |
| 152 | static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout ) |
| 153 | { |
| 154 | int ret; |
| 155 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 156 | : "=a" (ret) |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 157 | : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout) |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 158 | : "memory" ); |
| 159 | SYSCALL_RET(ret); |
| 160 | } |
| 161 | #undef SYSCALL_RET |
| 162 | |
| 163 | #endif /* linux && __i386__ && HAVE_STDINT_H */ |
| 164 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 165 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 166 | /* Because of the stupid Posix locking semantics, we need to keep |
| 167 | * track of all file descriptors referencing a given file, and not |
| 168 | * close a single one until all the locks are gone (sigh). |
| 169 | */ |
| 170 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 171 | /* file descriptor object */ |
| 172 | |
| 173 | /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */ |
| 174 | struct closed_fd |
| 175 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 176 | struct list entry; /* entry in inode closed list */ |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 177 | int unix_fd; /* the unix file descriptor */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 178 | char unlink[1]; /* name to unlink on close (if any) */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 181 | struct fd |
| 182 | { |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 183 | struct object obj; /* object header */ |
| 184 | const struct fd_ops *fd_ops; /* file descriptor operations */ |
| 185 | struct inode *inode; /* inode that this fd belongs to */ |
| 186 | struct list inode_entry; /* entry in inode fd list */ |
| 187 | struct closed_fd *closed; /* structure to store the unix fd at destroy time */ |
| 188 | struct object *user; /* object using this file descriptor */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 189 | struct list locks; /* list of locks on this fd */ |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 190 | unsigned int access; /* file access (FILE_READ_DATA etc.) */ |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 191 | unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */ |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 192 | unsigned int sharing; /* file sharing mode */ |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 193 | char *unix_name; /* unix file name */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 194 | int unix_fd; /* unix file descriptor */ |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 195 | unsigned int no_fd_status;/* status to return when unix_fd is -1 */ |
Michael Stefaniuc | 0eed440 | 2009-10-01 00:22:55 +0200 | [diff] [blame] | 196 | unsigned int signaled :1; /* is the fd signaled? */ |
| 197 | unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 198 | int poll_index; /* index of fd in poll array */ |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 199 | struct async_queue *read_q; /* async readers of this fd */ |
| 200 | struct async_queue *write_q; /* async writers of this fd */ |
| 201 | struct async_queue *wait_q; /* other async waiters of this fd */ |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 202 | struct completion *completion; /* completion object attached to this fd */ |
Alexandre Julliard | dc7f170 | 2008-12-15 13:29:38 +0100 | [diff] [blame] | 203 | apc_param_t comp_key; /* completion key to set in completion events */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static void fd_dump( struct object *obj, int verbose ); |
| 207 | static void fd_destroy( struct object *obj ); |
| 208 | |
| 209 | static const struct object_ops fd_ops = |
| 210 | { |
| 211 | sizeof(struct fd), /* size */ |
| 212 | fd_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 213 | no_get_type, /* get_type */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 214 | no_add_queue, /* add_queue */ |
| 215 | NULL, /* remove_queue */ |
| 216 | NULL, /* signaled */ |
| 217 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 218 | no_signal, /* signal */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 219 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 220 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 221 | default_get_sd, /* get_sd */ |
| 222 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 223 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 224 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 225 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 226 | fd_destroy /* destroy */ |
| 227 | }; |
| 228 | |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 229 | /* device object */ |
| 230 | |
| 231 | #define DEVICE_HASH_SIZE 7 |
| 232 | #define INODE_HASH_SIZE 17 |
| 233 | |
| 234 | struct device |
| 235 | { |
| 236 | struct object obj; /* object header */ |
| 237 | struct list entry; /* entry in device hash list */ |
| 238 | dev_t dev; /* device number */ |
Alexandre Julliard | f62f6e8 | 2005-08-24 18:33:50 +0000 | [diff] [blame] | 239 | int removable; /* removable device? (or -1 if unknown) */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 240 | struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */ |
| 241 | }; |
| 242 | |
| 243 | static void device_dump( struct object *obj, int verbose ); |
| 244 | static void device_destroy( struct object *obj ); |
| 245 | |
| 246 | static const struct object_ops device_ops = |
| 247 | { |
| 248 | sizeof(struct device), /* size */ |
| 249 | device_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 250 | no_get_type, /* get_type */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 251 | no_add_queue, /* add_queue */ |
| 252 | NULL, /* remove_queue */ |
| 253 | NULL, /* signaled */ |
| 254 | NULL, /* satisfied */ |
| 255 | no_signal, /* signal */ |
| 256 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 257 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 258 | default_get_sd, /* get_sd */ |
| 259 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 260 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 261 | no_open_file, /* open_file */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 262 | no_close_handle, /* close_handle */ |
| 263 | device_destroy /* destroy */ |
| 264 | }; |
| 265 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 266 | /* inode object */ |
| 267 | |
| 268 | struct inode |
| 269 | { |
| 270 | struct object obj; /* object header */ |
| 271 | struct list entry; /* inode hash list entry */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 272 | struct device *device; /* device containing this inode */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 273 | ino_t ino; /* inode number */ |
| 274 | struct list open; /* list of open file descriptors */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 275 | struct list locks; /* list of file locks */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 276 | struct list closed; /* list of file descriptors to close at destroy time */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static void inode_dump( struct object *obj, int verbose ); |
| 280 | static void inode_destroy( struct object *obj ); |
| 281 | |
| 282 | static const struct object_ops inode_ops = |
| 283 | { |
| 284 | sizeof(struct inode), /* size */ |
| 285 | inode_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 286 | no_get_type, /* get_type */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 287 | no_add_queue, /* add_queue */ |
| 288 | NULL, /* remove_queue */ |
| 289 | NULL, /* signaled */ |
| 290 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 291 | no_signal, /* signal */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 292 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 293 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 294 | default_get_sd, /* get_sd */ |
| 295 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 296 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 297 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 298 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 299 | inode_destroy /* destroy */ |
| 300 | }; |
| 301 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 302 | /* file lock object */ |
| 303 | |
| 304 | struct file_lock |
| 305 | { |
| 306 | struct object obj; /* object header */ |
| 307 | struct fd *fd; /* fd owning this lock */ |
| 308 | struct list fd_entry; /* entry in list of locks on a given fd */ |
| 309 | struct list inode_entry; /* entry in inode list of locks */ |
| 310 | int shared; /* shared lock? */ |
| 311 | file_pos_t start; /* locked region is interval [start;end) */ |
| 312 | file_pos_t end; |
| 313 | struct process *process; /* process owning this lock */ |
| 314 | struct list proc_entry; /* entry in list of locks owned by the process */ |
| 315 | }; |
| 316 | |
| 317 | static void file_lock_dump( struct object *obj, int verbose ); |
| 318 | static int file_lock_signaled( struct object *obj, struct thread *thread ); |
| 319 | |
| 320 | static const struct object_ops file_lock_ops = |
| 321 | { |
| 322 | sizeof(struct file_lock), /* size */ |
| 323 | file_lock_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 324 | no_get_type, /* get_type */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 325 | add_queue, /* add_queue */ |
| 326 | remove_queue, /* remove_queue */ |
| 327 | file_lock_signaled, /* signaled */ |
| 328 | no_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 329 | no_signal, /* signal */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 330 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 331 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 332 | default_get_sd, /* get_sd */ |
| 333 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 334 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 335 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 336 | no_close_handle, /* close_handle */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 337 | no_destroy /* destroy */ |
| 338 | }; |
| 339 | |
| 340 | |
| 341 | #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1))) |
| 342 | #define FILE_POS_T_MAX (~(file_pos_t)0) |
| 343 | |
| 344 | static file_pos_t max_unix_offset = OFF_T_MAX; |
| 345 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 346 | #define DUMP_LONG_LONG(val) do { \ |
| 347 | if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \ |
Alexandre Julliard | 0fa7170 | 2006-06-15 14:14:31 +0200 | [diff] [blame] | 348 | fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 349 | else \ |
| 350 | fprintf( stderr, "%lx", (unsigned long)(val) ); \ |
| 351 | } while (0) |
| 352 | |
| 353 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 354 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 355 | /****************************************************************/ |
| 356 | /* timeouts support */ |
| 357 | |
| 358 | struct timeout_user |
| 359 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 360 | struct list entry; /* entry in sorted timeout list */ |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 361 | timeout_t when; /* timeout expiry (absolute time) */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 362 | timeout_callback callback; /* callback function */ |
| 363 | void *private; /* callback private data */ |
| 364 | }; |
| 365 | |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 366 | static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */ |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 367 | timeout_t current_time; |
| 368 | |
| 369 | static inline void set_current_time(void) |
| 370 | { |
| 371 | static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC; |
| 372 | struct timeval now; |
| 373 | gettimeofday( &now, NULL ); |
| 374 | current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970; |
| 375 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 376 | |
| 377 | /* add a timeout user */ |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 378 | struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private ) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 379 | { |
| 380 | struct timeout_user *user; |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 381 | struct list *ptr; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 382 | |
| 383 | if (!(user = mem_alloc( sizeof(*user) ))) return NULL; |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 384 | user->when = (when > 0) ? when : current_time - when; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 385 | user->callback = func; |
| 386 | user->private = private; |
| 387 | |
| 388 | /* Now insert it in the linked list */ |
| 389 | |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 390 | LIST_FOR_EACH( ptr, &timeout_list ) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 391 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 392 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 393 | if (timeout->when >= user->when) break; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 394 | } |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 395 | list_add_before( ptr, &user->entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 396 | return user; |
| 397 | } |
| 398 | |
| 399 | /* remove a timeout user */ |
| 400 | void remove_timeout_user( struct timeout_user *user ) |
| 401 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 402 | list_remove( &user->entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 403 | free( user ); |
| 404 | } |
| 405 | |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 406 | /* return a text description of a timeout for debugging purposes */ |
| 407 | const char *get_timeout_str( timeout_t timeout ) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 408 | { |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 409 | static char buffer[64]; |
| 410 | long secs, nsecs; |
| 411 | |
| 412 | if (!timeout) return "0"; |
| 413 | if (timeout == TIMEOUT_INFINITE) return "infinite"; |
| 414 | |
| 415 | if (timeout < 0) /* relative */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 416 | { |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 417 | secs = -timeout / TICKS_PER_SEC; |
| 418 | nsecs = -timeout % TICKS_PER_SEC; |
| 419 | sprintf( buffer, "+%ld.%07ld", secs, nsecs ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 420 | } |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 421 | else /* absolute */ |
| 422 | { |
| 423 | secs = (timeout - current_time) / TICKS_PER_SEC; |
| 424 | nsecs = (timeout - current_time) % TICKS_PER_SEC; |
| 425 | if (nsecs < 0) |
| 426 | { |
| 427 | nsecs += TICKS_PER_SEC; |
| 428 | secs--; |
| 429 | } |
| 430 | if (secs >= 0) |
| 431 | sprintf( buffer, "%x%08x (+%ld.%07ld)", |
| 432 | (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs ); |
| 433 | else |
| 434 | sprintf( buffer, "%x%08x (-%ld.%07ld)", |
| 435 | (unsigned int)(timeout >> 32), (unsigned int)timeout, |
| 436 | -(secs + 1), TICKS_PER_SEC - nsecs ); |
| 437 | } |
| 438 | return buffer; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 441 | |
| 442 | /****************************************************************/ |
| 443 | /* poll support */ |
| 444 | |
| 445 | static struct fd **poll_users; /* users array */ |
| 446 | static struct pollfd *pollfd; /* poll fd array */ |
| 447 | static int nb_users; /* count of array entries actually in use */ |
| 448 | static int active_users; /* current number of active users */ |
| 449 | static int allocated_users; /* count of allocated entries in the array */ |
| 450 | static struct fd **freelist; /* list of free entries in the array */ |
| 451 | |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 452 | static int get_next_timeout(void); |
| 453 | |
Dmitry Timoshkov | f2213dd | 2007-12-03 17:47:31 +0800 | [diff] [blame] | 454 | static inline void fd_poll_event( struct fd *fd, int event ) |
| 455 | { |
| 456 | fd->fd_ops->poll_event( fd, event ); |
| 457 | } |
| 458 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 459 | #ifdef USE_EPOLL |
| 460 | |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 461 | static int epoll_fd = -1; |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 462 | |
| 463 | static inline void init_epoll(void) |
| 464 | { |
| 465 | epoll_fd = epoll_create( 128 ); |
| 466 | } |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 467 | |
| 468 | /* set the events that epoll waits for on this fd; helper for set_fd_events */ |
| 469 | static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) |
| 470 | { |
| 471 | struct epoll_event ev; |
| 472 | int ctl; |
| 473 | |
| 474 | if (epoll_fd == -1) return; |
| 475 | |
| 476 | if (events == -1) /* stop waiting on this fd completely */ |
| 477 | { |
| 478 | if (pollfd[user].fd == -1) return; /* already removed */ |
| 479 | ctl = EPOLL_CTL_DEL; |
| 480 | } |
| 481 | else if (pollfd[user].fd == -1) |
| 482 | { |
| 483 | if (pollfd[user].events) return; /* stopped waiting on it, don't restart */ |
| 484 | ctl = EPOLL_CTL_ADD; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | if (pollfd[user].events == events) return; /* nothing to do */ |
| 489 | ctl = EPOLL_CTL_MOD; |
| 490 | } |
| 491 | |
| 492 | ev.events = events; |
Eric Pouech | 2e0b533 | 2006-01-27 16:17:51 +0100 | [diff] [blame] | 493 | memset(&ev.data, 0, sizeof(ev.data)); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 494 | ev.data.u32 = user; |
| 495 | |
| 496 | if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1) |
| 497 | { |
| 498 | if (errno == ENOMEM) /* not enough memory, give up on epoll */ |
| 499 | { |
| 500 | close( epoll_fd ); |
| 501 | epoll_fd = -1; |
| 502 | } |
| 503 | else perror( "epoll_ctl" ); /* should not happen */ |
| 504 | } |
| 505 | } |
| 506 | |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 507 | static inline void remove_epoll_user( struct fd *fd, int user ) |
| 508 | { |
| 509 | if (epoll_fd == -1) return; |
| 510 | |
| 511 | if (pollfd[user].fd != -1) |
| 512 | { |
| 513 | struct epoll_event dummy; |
| 514 | epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy ); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static inline void main_loop_epoll(void) |
| 519 | { |
| 520 | int i, ret, timeout; |
| 521 | struct epoll_event events[128]; |
| 522 | |
| 523 | assert( POLLIN == EPOLLIN ); |
| 524 | assert( POLLOUT == EPOLLOUT ); |
| 525 | assert( POLLERR == EPOLLERR ); |
| 526 | assert( POLLHUP == EPOLLHUP ); |
| 527 | |
| 528 | if (epoll_fd == -1) return; |
| 529 | |
| 530 | while (active_users) |
| 531 | { |
| 532 | timeout = get_next_timeout(); |
| 533 | |
| 534 | if (!active_users) break; /* last user removed by a timeout */ |
| 535 | if (epoll_fd == -1) break; /* an error occurred with epoll */ |
| 536 | |
| 537 | ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 538 | set_current_time(); |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 539 | |
| 540 | /* put the events into the pollfd array first, like poll does */ |
| 541 | for (i = 0; i < ret; i++) |
| 542 | { |
| 543 | int user = events[i].data.u32; |
| 544 | pollfd[user].revents = events[i].events; |
| 545 | } |
| 546 | |
| 547 | /* read events from the pollfd array, as set_fd_events may modify them */ |
| 548 | for (i = 0; i < ret; i++) |
| 549 | { |
| 550 | int user = events[i].data.u32; |
| 551 | if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents ); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 556 | #elif defined(HAVE_KQUEUE) |
| 557 | |
| 558 | static int kqueue_fd = -1; |
| 559 | |
| 560 | static inline void init_epoll(void) |
| 561 | { |
Alexandre Julliard | 8143d42 | 2007-06-15 12:41:43 +0200 | [diff] [blame] | 562 | #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */ |
| 563 | int mib[2]; |
| 564 | char release[32]; |
| 565 | size_t len = sizeof(release); |
| 566 | |
| 567 | mib[0] = CTL_KERN; |
| 568 | mib[1] = KERN_OSRELEASE; |
| 569 | if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return; |
| 570 | if (atoi(release) < 9) return; |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 571 | #endif |
Alexandre Julliard | 8143d42 | 2007-06-15 12:41:43 +0200 | [diff] [blame] | 572 | kqueue_fd = kqueue(); |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) |
| 576 | { |
| 577 | struct kevent ev[2]; |
| 578 | |
| 579 | if (kqueue_fd == -1) return; |
| 580 | |
| 581 | EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user ); |
| 582 | EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user ); |
| 583 | |
| 584 | if (events == -1) /* stop waiting on this fd completely */ |
| 585 | { |
| 586 | if (pollfd[user].fd == -1) return; /* already removed */ |
| 587 | ev[0].flags |= EV_DELETE; |
| 588 | ev[1].flags |= EV_DELETE; |
| 589 | } |
| 590 | else if (pollfd[user].fd == -1) |
| 591 | { |
| 592 | if (pollfd[user].events) return; /* stopped waiting on it, don't restart */ |
| 593 | ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE); |
| 594 | ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE); |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | if (pollfd[user].events == events) return; /* nothing to do */ |
| 599 | ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE; |
| 600 | ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE; |
| 601 | } |
| 602 | |
| 603 | if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1) |
| 604 | { |
| 605 | if (errno == ENOMEM) /* not enough memory, give up on kqueue */ |
| 606 | { |
| 607 | close( kqueue_fd ); |
| 608 | kqueue_fd = -1; |
| 609 | } |
| 610 | else perror( "kevent" ); /* should not happen */ |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | static inline void remove_epoll_user( struct fd *fd, int user ) |
| 615 | { |
| 616 | if (kqueue_fd == -1) return; |
| 617 | |
| 618 | if (pollfd[user].fd != -1) |
| 619 | { |
| 620 | struct kevent ev[2]; |
| 621 | |
| 622 | EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 ); |
| 623 | EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 ); |
| 624 | kevent( kqueue_fd, ev, 2, NULL, 0, NULL ); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static inline void main_loop_epoll(void) |
| 629 | { |
| 630 | int i, ret, timeout; |
| 631 | struct kevent events[128]; |
| 632 | |
| 633 | if (kqueue_fd == -1) return; |
| 634 | |
| 635 | while (active_users) |
| 636 | { |
| 637 | timeout = get_next_timeout(); |
| 638 | |
| 639 | if (!active_users) break; /* last user removed by a timeout */ |
| 640 | if (kqueue_fd == -1) break; /* an error occurred with kqueue */ |
| 641 | |
| 642 | if (timeout != -1) |
| 643 | { |
| 644 | struct timespec ts; |
| 645 | |
| 646 | ts.tv_sec = timeout / 1000; |
| 647 | ts.tv_nsec = (timeout % 1000) * 1000000; |
| 648 | ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts ); |
| 649 | } |
| 650 | else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL ); |
| 651 | |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 652 | set_current_time(); |
Alexandre Julliard | 753c870 | 2006-08-10 16:42:09 +0200 | [diff] [blame] | 653 | |
Alexandre Julliard | c827892 | 2006-08-04 22:11:00 +0200 | [diff] [blame] | 654 | /* put the events into the pollfd array first, like poll does */ |
| 655 | for (i = 0; i < ret; i++) |
| 656 | { |
| 657 | long user = (long)events[i].udata; |
| 658 | pollfd[user].revents = 0; |
| 659 | } |
| 660 | for (i = 0; i < ret; i++) |
| 661 | { |
| 662 | long user = (long)events[i].udata; |
| 663 | if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN; |
| 664 | else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT; |
| 665 | if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP; |
| 666 | if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR; |
| 667 | } |
| 668 | |
| 669 | /* read events from the pollfd array, as set_fd_events may modify them */ |
| 670 | for (i = 0; i < ret; i++) |
| 671 | { |
| 672 | long user = (long)events[i].udata; |
| 673 | if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents ); |
| 674 | pollfd[user].revents = 0; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | #else /* HAVE_KQUEUE */ |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 680 | |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 681 | static inline void init_epoll(void) { } |
| 682 | static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { } |
| 683 | static inline void remove_epoll_user( struct fd *fd, int user ) { } |
| 684 | static inline void main_loop_epoll(void) { } |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 685 | |
| 686 | #endif /* USE_EPOLL */ |
| 687 | |
| 688 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 689 | /* add a user in the poll array and return its index, or -1 on failure */ |
| 690 | static int add_poll_user( struct fd *fd ) |
| 691 | { |
| 692 | int ret; |
| 693 | if (freelist) |
| 694 | { |
| 695 | ret = freelist - poll_users; |
| 696 | freelist = (struct fd **)poll_users[ret]; |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | if (nb_users == allocated_users) |
| 701 | { |
| 702 | struct fd **newusers; |
| 703 | struct pollfd *newpoll; |
| 704 | int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16; |
| 705 | if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1; |
| 706 | if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) ))) |
| 707 | { |
| 708 | if (allocated_users) |
| 709 | poll_users = newusers; |
| 710 | else |
| 711 | free( newusers ); |
| 712 | return -1; |
| 713 | } |
| 714 | poll_users = newusers; |
| 715 | pollfd = newpoll; |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 716 | if (!allocated_users) init_epoll(); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 717 | allocated_users = new_count; |
| 718 | } |
| 719 | ret = nb_users++; |
| 720 | } |
| 721 | pollfd[ret].fd = -1; |
| 722 | pollfd[ret].events = 0; |
| 723 | pollfd[ret].revents = 0; |
| 724 | poll_users[ret] = fd; |
| 725 | active_users++; |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /* remove a user from the poll list */ |
| 730 | static void remove_poll_user( struct fd *fd, int user ) |
| 731 | { |
| 732 | assert( user >= 0 ); |
| 733 | assert( poll_users[user] == fd ); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 734 | |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 735 | remove_epoll_user( fd, user ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 736 | pollfd[user].fd = -1; |
| 737 | pollfd[user].events = 0; |
| 738 | pollfd[user].revents = 0; |
| 739 | poll_users[user] = (struct fd *)freelist; |
| 740 | freelist = &poll_users[user]; |
| 741 | active_users--; |
| 742 | } |
| 743 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 744 | /* process pending timeouts and return the time until the next timeout, in milliseconds */ |
| 745 | static int get_next_timeout(void) |
| 746 | { |
| 747 | if (!list_empty( &timeout_list )) |
| 748 | { |
| 749 | struct list expired_list, *ptr; |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 750 | |
| 751 | /* first remove all expired timers from the list */ |
| 752 | |
| 753 | list_init( &expired_list ); |
| 754 | while ((ptr = list_head( &timeout_list )) != NULL) |
| 755 | { |
| 756 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 757 | |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 758 | if (timeout->when <= current_time) |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 759 | { |
| 760 | list_remove( &timeout->entry ); |
| 761 | list_add_tail( &expired_list, &timeout->entry ); |
| 762 | } |
| 763 | else break; |
| 764 | } |
| 765 | |
| 766 | /* now call the callback for all the removed timers */ |
| 767 | |
| 768 | while ((ptr = list_head( &expired_list )) != NULL) |
| 769 | { |
| 770 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 771 | list_remove( &timeout->entry ); |
| 772 | timeout->callback( timeout->private ); |
| 773 | free( timeout ); |
| 774 | } |
| 775 | |
| 776 | if ((ptr = list_head( &timeout_list )) != NULL) |
| 777 | { |
| 778 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 779 | int diff = (timeout->when - current_time + 9999) / 10000; |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 780 | if (diff < 0) diff = 0; |
| 781 | return diff; |
| 782 | } |
| 783 | } |
| 784 | return -1; /* no pending timeouts */ |
| 785 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 786 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 787 | /* server main poll() loop */ |
| 788 | void main_loop(void) |
| 789 | { |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 790 | int i, ret, timeout; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 791 | |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 792 | set_current_time(); |
| 793 | server_start_time = current_time; |
Alexandre Julliard | 753c870 | 2006-08-10 16:42:09 +0200 | [diff] [blame] | 794 | |
Alexandre Julliard | 2f40727 | 2006-08-03 19:56:55 +0200 | [diff] [blame] | 795 | main_loop_epoll(); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 796 | /* fall through to normal poll loop */ |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 797 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 798 | while (active_users) |
| 799 | { |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 800 | timeout = get_next_timeout(); |
Alexandre Julliard | 0a6af13 | 2004-09-07 23:28:14 +0000 | [diff] [blame] | 801 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 802 | if (!active_users) break; /* last user removed by a timeout */ |
Alexandre Julliard | 0a6af13 | 2004-09-07 23:28:14 +0000 | [diff] [blame] | 803 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 804 | ret = poll( pollfd, nb_users, timeout ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 805 | set_current_time(); |
Alexandre Julliard | 753c870 | 2006-08-10 16:42:09 +0200 | [diff] [blame] | 806 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 807 | if (ret > 0) |
| 808 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 809 | for (i = 0; i < nb_users; i++) |
| 810 | { |
| 811 | if (pollfd[i].revents) |
| 812 | { |
| 813 | fd_poll_event( poll_users[i], pollfd[i].revents ); |
| 814 | if (!--ret) break; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 821 | |
| 822 | /****************************************************************/ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 823 | /* device functions */ |
| 824 | |
| 825 | static struct list device_hash[DEVICE_HASH_SIZE]; |
| 826 | |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 827 | static int is_device_removable( dev_t dev, int unix_fd ) |
| 828 | { |
| 829 | #if defined(linux) && defined(HAVE_FSTATFS) |
| 830 | struct statfs stfs; |
| 831 | |
| 832 | /* check for floppy disk */ |
| 833 | if (major(dev) == FLOPPY_MAJOR) return 1; |
| 834 | |
| 835 | if (fstatfs( unix_fd, &stfs ) == -1) return 0; |
| 836 | return (stfs.f_type == 0x9660 || /* iso9660 */ |
| 837 | stfs.f_type == 0x9fa1 || /* supermount */ |
| 838 | stfs.f_type == 0x15013346); /* udf */ |
| 839 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) |
| 840 | struct statfs stfs; |
| 841 | |
| 842 | if (fstatfs( unix_fd, &stfs ) == -1) return 0; |
Alexandre Julliard | 8765a0d | 2007-08-29 12:02:13 +0200 | [diff] [blame] | 843 | return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename)); |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 844 | #elif defined(__NetBSD__) |
| 845 | struct statvfs stfs; |
| 846 | |
| 847 | if (fstatvfs( unix_fd, &stfs ) == -1) return 0; |
Alexandre Julliard | 8765a0d | 2007-08-29 12:02:13 +0200 | [diff] [blame] | 848 | return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename)); |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 849 | #elif defined(sun) |
| 850 | # include <sys/dkio.h> |
| 851 | # include <sys/vtoc.h> |
| 852 | struct dk_cinfo dkinf; |
| 853 | if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0; |
| 854 | return (dkinf.dki_ctype == DKC_CDROM || |
| 855 | dkinf.dki_ctype == DKC_NCRFLOPPY || |
| 856 | dkinf.dki_ctype == DKC_SMSFLOPPY || |
| 857 | dkinf.dki_ctype == DKC_INTEL82072 || |
| 858 | dkinf.dki_ctype == DKC_INTEL82077); |
| 859 | #else |
| 860 | return 0; |
| 861 | #endif |
| 862 | } |
| 863 | |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 864 | /* retrieve the device object for a given fd, creating it if needed */ |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 865 | static struct device *get_device( dev_t dev, int unix_fd ) |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 866 | { |
| 867 | struct device *device; |
| 868 | unsigned int i, hash = dev % DEVICE_HASH_SIZE; |
| 869 | |
| 870 | if (device_hash[hash].next) |
| 871 | { |
| 872 | LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry ) |
| 873 | if (device->dev == dev) return (struct device *)grab_object( device ); |
| 874 | } |
| 875 | else list_init( &device_hash[hash] ); |
| 876 | |
| 877 | /* not found, create it */ |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 878 | |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 879 | if (unix_fd == -1) return NULL; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 880 | if ((device = alloc_object( &device_ops ))) |
| 881 | { |
| 882 | device->dev = dev; |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 883 | device->removable = is_device_removable( dev, unix_fd ); |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 884 | for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] ); |
| 885 | list_add_head( &device_hash[hash], &device->entry ); |
| 886 | } |
| 887 | return device; |
| 888 | } |
| 889 | |
| 890 | static void device_dump( struct object *obj, int verbose ) |
| 891 | { |
| 892 | struct device *device = (struct device *)obj; |
| 893 | fprintf( stderr, "Device dev=" ); |
| 894 | DUMP_LONG_LONG( device->dev ); |
| 895 | fprintf( stderr, "\n" ); |
| 896 | } |
| 897 | |
| 898 | static void device_destroy( struct object *obj ) |
| 899 | { |
| 900 | struct device *device = (struct device *)obj; |
| 901 | unsigned int i; |
| 902 | |
| 903 | for (i = 0; i < INODE_HASH_SIZE; i++) |
| 904 | assert( list_empty(&device->inode_hash[i]) ); |
| 905 | |
| 906 | list_remove( &device->entry ); /* remove it from the hash table */ |
| 907 | } |
| 908 | |
| 909 | |
| 910 | /****************************************************************/ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 911 | /* inode functions */ |
| 912 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 913 | /* close all pending file descriptors in the closed list */ |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 914 | static void inode_close_pending( struct inode *inode, int keep_unlinks ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 915 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 916 | struct list *ptr = list_head( &inode->closed ); |
| 917 | |
| 918 | while (ptr) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 919 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 920 | struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry ); |
| 921 | struct list *next = list_next( &inode->closed, ptr ); |
| 922 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 923 | if (fd->unix_fd != -1) |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 924 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 925 | close( fd->unix_fd ); |
| 926 | fd->unix_fd = -1; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 927 | } |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 928 | if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 929 | { |
| 930 | list_remove( ptr ); |
| 931 | free( fd ); |
| 932 | } |
| 933 | ptr = next; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 937 | static void inode_dump( struct object *obj, int verbose ) |
| 938 | { |
| 939 | struct inode *inode = (struct inode *)obj; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 940 | fprintf( stderr, "Inode device=%p ino=", inode->device ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 941 | DUMP_LONG_LONG( inode->ino ); |
| 942 | fprintf( stderr, "\n" ); |
| 943 | } |
| 944 | |
| 945 | static void inode_destroy( struct object *obj ) |
| 946 | { |
| 947 | struct inode *inode = (struct inode *)obj; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 948 | struct list *ptr; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 949 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 950 | assert( list_empty(&inode->open) ); |
| 951 | assert( list_empty(&inode->locks) ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 952 | |
| 953 | list_remove( &inode->entry ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 954 | |
| 955 | while ((ptr = list_head( &inode->closed ))) |
| 956 | { |
| 957 | struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry ); |
| 958 | list_remove( ptr ); |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 959 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 960 | if (fd->unlink[0]) |
| 961 | { |
| 962 | /* make sure it is still the same file */ |
| 963 | struct stat st; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 964 | if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino) |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 965 | { |
| 966 | if (S_ISDIR(st.st_mode)) rmdir( fd->unlink ); |
| 967 | else unlink( fd->unlink ); |
| 968 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 969 | } |
| 970 | free( fd ); |
| 971 | } |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 972 | release_object( inode->device ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* retrieve the inode object for a given fd, creating it if needed */ |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 976 | static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 977 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 978 | struct device *device; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 979 | struct inode *inode; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 980 | unsigned int hash = ino % INODE_HASH_SIZE; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 981 | |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 982 | if (!(device = get_device( dev, unix_fd ))) return NULL; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 983 | |
| 984 | LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 985 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 986 | if (inode->ino == ino) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 987 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 988 | release_object( device ); |
| 989 | return (struct inode *)grab_object( inode ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 992 | |
| 993 | /* not found, create it */ |
| 994 | if ((inode = alloc_object( &inode_ops ))) |
| 995 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 996 | inode->device = device; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 997 | inode->ino = ino; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 998 | list_init( &inode->open ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 999 | list_init( &inode->locks ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1000 | list_init( &inode->closed ); |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 1001 | list_add_head( &device->inode_hash[hash], &inode->entry ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1002 | } |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 1003 | else release_object( device ); |
| 1004 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1005 | return inode; |
| 1006 | } |
| 1007 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 1008 | /* add fd to the inode list of file descriptors to close */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1009 | static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd ) |
| 1010 | { |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1011 | if (!list_empty( &inode->locks )) |
| 1012 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1013 | list_add_head( &inode->closed, &fd->entry ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1014 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1015 | else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */ |
| 1016 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 1017 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 1018 | fd->unix_fd = -1; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1019 | list_add_head( &inode->closed, &fd->entry ); |
| 1020 | } |
| 1021 | else /* no locks on this inode and no unlink, get rid of the fd */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1022 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 1023 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1024 | free( fd ); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | /****************************************************************/ |
| 1030 | /* file lock functions */ |
| 1031 | |
| 1032 | static void file_lock_dump( struct object *obj, int verbose ) |
| 1033 | { |
| 1034 | struct file_lock *lock = (struct file_lock *)obj; |
| 1035 | fprintf( stderr, "Lock %s fd=%p proc=%p start=", |
| 1036 | lock->shared ? "shared" : "excl", lock->fd, lock->process ); |
| 1037 | DUMP_LONG_LONG( lock->start ); |
| 1038 | fprintf( stderr, " end=" ); |
| 1039 | DUMP_LONG_LONG( lock->end ); |
| 1040 | fprintf( stderr, "\n" ); |
| 1041 | } |
| 1042 | |
| 1043 | static int file_lock_signaled( struct object *obj, struct thread *thread ) |
| 1044 | { |
| 1045 | struct file_lock *lock = (struct file_lock *)obj; |
| 1046 | /* lock is signaled if it has lost its owner */ |
| 1047 | return !lock->process; |
| 1048 | } |
| 1049 | |
| 1050 | /* set (or remove) a Unix lock if possible for the given range */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1051 | static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1052 | { |
| 1053 | struct flock fl; |
| 1054 | |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1055 | if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1056 | for (;;) |
| 1057 | { |
| 1058 | if (start == end) return 1; /* can't set zero-byte lock */ |
| 1059 | if (start > max_unix_offset) return 1; /* ignore it */ |
| 1060 | fl.l_type = type; |
| 1061 | fl.l_whence = SEEK_SET; |
| 1062 | fl.l_start = start; |
| 1063 | if (!end || end > max_unix_offset) fl.l_len = 0; |
| 1064 | else fl.l_len = end - start; |
| 1065 | if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1; |
| 1066 | |
| 1067 | switch(errno) |
| 1068 | { |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1069 | case EACCES: |
| 1070 | /* check whether locks work at all on this file system */ |
| 1071 | if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1) |
| 1072 | { |
| 1073 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | /* fall through */ |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 1077 | case EIO: |
| 1078 | case ENOLCK: |
| 1079 | /* no locking on this fs, just ignore it */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1080 | fd->fs_locks = 0; |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 1081 | return 1; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1082 | case EAGAIN: |
| 1083 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 1084 | return 0; |
Alexandre Julliard | d71ba93 | 2003-03-21 21:31:35 +0000 | [diff] [blame] | 1085 | case EBADF: |
| 1086 | /* this can happen if we try to set a write lock on a read-only file */ |
| 1087 | /* we just ignore that error */ |
| 1088 | if (fl.l_type == F_WRLCK) return 1; |
| 1089 | set_error( STATUS_ACCESS_DENIED ); |
| 1090 | return 0; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 1091 | #ifdef EOVERFLOW |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1092 | case EOVERFLOW: |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 1093 | #endif |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 1094 | case EINVAL: |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1095 | /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */ |
| 1096 | /* in that case we shrink the limit and retry */ |
| 1097 | if (max_unix_offset > INT_MAX) |
| 1098 | { |
| 1099 | max_unix_offset = INT_MAX; |
| 1100 | break; /* retry */ |
| 1101 | } |
| 1102 | /* fall through */ |
| 1103 | default: |
| 1104 | file_set_error(); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | /* check if interval [start;end) overlaps the lock */ |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 1111 | static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1112 | { |
| 1113 | if (lock->end && start >= lock->end) return 0; |
| 1114 | if (end && lock->start >= end) return 0; |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | /* remove Unix locks for all bytes in the specified area that are no longer locked */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1119 | static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1120 | { |
| 1121 | struct hole |
| 1122 | { |
| 1123 | struct hole *next; |
| 1124 | struct hole *prev; |
| 1125 | file_pos_t start; |
| 1126 | file_pos_t end; |
| 1127 | } *first, *cur, *next, *buffer; |
| 1128 | |
| 1129 | struct list *ptr; |
| 1130 | int count = 0; |
| 1131 | |
| 1132 | if (!fd->inode) return; |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1133 | if (!fd->fs_locks) return; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1134 | if (start == end || start > max_unix_offset) return; |
| 1135 | if (!end || end > max_unix_offset) end = max_unix_offset + 1; |
| 1136 | |
| 1137 | /* count the number of locks overlapping the specified area */ |
| 1138 | |
| 1139 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 1140 | { |
| 1141 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 1142 | if (lock->start == lock->end) continue; |
| 1143 | if (lock_overlaps( lock, start, end )) count++; |
| 1144 | } |
| 1145 | |
| 1146 | if (!count) /* no locks at all, we can unlock everything */ |
| 1147 | { |
| 1148 | set_unix_lock( fd, start, end, F_UNLCK ); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | /* allocate space for the list of holes */ |
| 1153 | /* max. number of holes is number of locks + 1 */ |
| 1154 | |
| 1155 | if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return; |
| 1156 | first = buffer; |
| 1157 | first->next = NULL; |
| 1158 | first->prev = NULL; |
| 1159 | first->start = start; |
| 1160 | first->end = end; |
| 1161 | next = first + 1; |
| 1162 | |
| 1163 | /* build a sorted list of unlocked holes in the specified area */ |
| 1164 | |
| 1165 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 1166 | { |
| 1167 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 1168 | if (lock->start == lock->end) continue; |
| 1169 | if (!lock_overlaps( lock, start, end )) continue; |
| 1170 | |
| 1171 | /* go through all the holes touched by this lock */ |
| 1172 | for (cur = first; cur; cur = cur->next) |
| 1173 | { |
| 1174 | if (cur->end <= lock->start) continue; /* hole is before start of lock */ |
| 1175 | if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */ |
| 1176 | |
| 1177 | /* now we know that lock is overlapping hole */ |
| 1178 | |
| 1179 | if (cur->start >= lock->start) /* lock starts before hole, shrink from start */ |
| 1180 | { |
| 1181 | cur->start = lock->end; |
| 1182 | if (cur->start && cur->start < cur->end) break; /* done with this lock */ |
| 1183 | /* now hole is empty, remove it */ |
| 1184 | if (cur->next) cur->next->prev = cur->prev; |
| 1185 | if (cur->prev) cur->prev->next = cur->next; |
| 1186 | else if (!(first = cur->next)) goto done; /* no more holes at all */ |
| 1187 | } |
| 1188 | else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */ |
| 1189 | { |
| 1190 | cur->end = lock->start; |
| 1191 | assert( cur->start < cur->end ); |
| 1192 | } |
| 1193 | else /* lock is in the middle of hole, split hole in two */ |
| 1194 | { |
| 1195 | next->prev = cur; |
| 1196 | next->next = cur->next; |
| 1197 | cur->next = next; |
| 1198 | next->start = lock->end; |
| 1199 | next->end = cur->end; |
| 1200 | cur->end = lock->start; |
| 1201 | assert( next->start < next->end ); |
| 1202 | assert( cur->end < next->start ); |
| 1203 | next++; |
| 1204 | break; /* done with this lock */ |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /* clear Unix locks for all the holes */ |
| 1210 | |
| 1211 | for (cur = first; cur; cur = cur->next) |
| 1212 | set_unix_lock( fd, cur->start, cur->end, F_UNLCK ); |
| 1213 | |
| 1214 | done: |
| 1215 | free( buffer ); |
| 1216 | } |
| 1217 | |
| 1218 | /* create a new lock on a fd */ |
| 1219 | static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end ) |
| 1220 | { |
| 1221 | struct file_lock *lock; |
| 1222 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1223 | if (!(lock = alloc_object( &file_lock_ops ))) return NULL; |
| 1224 | lock->shared = shared; |
| 1225 | lock->start = start; |
| 1226 | lock->end = end; |
| 1227 | lock->fd = fd; |
| 1228 | lock->process = current->process; |
| 1229 | |
| 1230 | /* now try to set a Unix lock */ |
| 1231 | if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK )) |
| 1232 | { |
| 1233 | release_object( lock ); |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | list_add_head( &fd->locks, &lock->fd_entry ); |
| 1237 | list_add_head( &fd->inode->locks, &lock->inode_entry ); |
| 1238 | list_add_head( &lock->process->locks, &lock->proc_entry ); |
| 1239 | return lock; |
| 1240 | } |
| 1241 | |
| 1242 | /* remove an existing lock */ |
| 1243 | static void remove_lock( struct file_lock *lock, int remove_unix ) |
| 1244 | { |
| 1245 | struct inode *inode = lock->fd->inode; |
| 1246 | |
| 1247 | list_remove( &lock->fd_entry ); |
| 1248 | list_remove( &lock->inode_entry ); |
| 1249 | list_remove( &lock->proc_entry ); |
| 1250 | if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1251 | if (list_empty( &inode->locks )) inode_close_pending( inode, 1 ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1252 | lock->process = NULL; |
| 1253 | wake_up( &lock->obj, 0 ); |
| 1254 | release_object( lock ); |
| 1255 | } |
| 1256 | |
| 1257 | /* remove all locks owned by a given process */ |
| 1258 | void remove_process_locks( struct process *process ) |
| 1259 | { |
| 1260 | struct list *ptr; |
| 1261 | |
| 1262 | while ((ptr = list_head( &process->locks ))) |
| 1263 | { |
| 1264 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry ); |
| 1265 | remove_lock( lock, 1 ); /* this removes it from the list */ |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* remove all locks on a given fd */ |
| 1270 | static void remove_fd_locks( struct fd *fd ) |
| 1271 | { |
| 1272 | file_pos_t start = FILE_POS_T_MAX, end = 0; |
| 1273 | struct list *ptr; |
| 1274 | |
| 1275 | while ((ptr = list_head( &fd->locks ))) |
| 1276 | { |
| 1277 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry ); |
| 1278 | if (lock->start < start) start = lock->start; |
| 1279 | if (!lock->end || lock->end > end) end = lock->end - 1; |
| 1280 | remove_lock( lock, 0 ); |
| 1281 | } |
| 1282 | if (start < end) remove_unix_locks( fd, start, end + 1 ); |
| 1283 | } |
| 1284 | |
| 1285 | /* add a lock on an fd */ |
| 1286 | /* returns handle to wait on */ |
| 1287 | obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait ) |
| 1288 | { |
| 1289 | struct list *ptr; |
| 1290 | file_pos_t end = start + count; |
| 1291 | |
Alexandre Julliard | 60901b7 | 2007-06-20 13:14:55 +0200 | [diff] [blame] | 1292 | if (!fd->inode) /* not a regular file */ |
| 1293 | { |
| 1294 | set_error( STATUS_INVALID_DEVICE_REQUEST ); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1298 | /* don't allow wrapping locks */ |
| 1299 | if (end && end < start) |
| 1300 | { |
| 1301 | set_error( STATUS_INVALID_PARAMETER ); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /* check if another lock on that file overlaps the area */ |
| 1306 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 1307 | { |
| 1308 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 1309 | if (!lock_overlaps( lock, start, end )) continue; |
| 1310 | if (lock->shared && shared) continue; |
| 1311 | /* found one */ |
| 1312 | if (!wait) |
| 1313 | { |
| 1314 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | set_error( STATUS_PENDING ); |
| 1318 | return alloc_handle( current->process, lock, SYNCHRONIZE, 0 ); |
| 1319 | } |
| 1320 | |
| 1321 | /* not found, add it */ |
| 1322 | if (add_lock( fd, shared, start, end )) return 0; |
| 1323 | if (get_error() == STATUS_FILE_LOCK_CONFLICT) |
| 1324 | { |
| 1325 | /* Unix lock conflict -> tell client to wait and retry */ |
| 1326 | if (wait) set_error( STATUS_PENDING ); |
| 1327 | } |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* remove a lock on an fd */ |
| 1332 | void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count ) |
| 1333 | { |
| 1334 | struct list *ptr; |
| 1335 | file_pos_t end = start + count; |
| 1336 | |
| 1337 | /* find an existing lock with the exact same parameters */ |
| 1338 | LIST_FOR_EACH( ptr, &fd->locks ) |
| 1339 | { |
| 1340 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry ); |
| 1341 | if ((lock->start == start) && (lock->end == end)) |
| 1342 | { |
| 1343 | remove_lock( lock, 1 ); |
| 1344 | return; |
| 1345 | } |
| 1346 | } |
| 1347 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1351 | /****************************************************************/ |
| 1352 | /* file descriptor functions */ |
| 1353 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1354 | static void fd_dump( struct object *obj, int verbose ) |
| 1355 | { |
| 1356 | struct fd *fd = (struct fd *)obj; |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1357 | fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options ); |
Alexandre Julliard | e432d54 | 2005-02-03 10:45:18 +0000 | [diff] [blame] | 1358 | if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink ); |
| 1359 | fprintf( stderr, "\n" ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static void fd_destroy( struct object *obj ) |
| 1363 | { |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1364 | struct fd *fd = (struct fd *)obj; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1365 | |
Alexandre Julliard | b2cba95 | 2007-04-03 19:36:07 +0200 | [diff] [blame] | 1366 | free_async_queue( fd->read_q ); |
| 1367 | free_async_queue( fd->write_q ); |
| 1368 | free_async_queue( fd->wait_q ); |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1369 | |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 1370 | if (fd->completion) release_object( fd->completion ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1371 | remove_fd_locks( fd ); |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 1372 | free( fd->unix_name ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1373 | list_remove( &fd->inode_entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1374 | if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1375 | if (fd->inode) |
| 1376 | { |
| 1377 | inode_add_closed_fd( fd->inode, fd->closed ); |
| 1378 | release_object( fd->inode ); |
| 1379 | } |
| 1380 | else /* no inode, close it right away */ |
| 1381 | { |
| 1382 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 1383 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1386 | /* set the events that select waits for on this fd */ |
| 1387 | void set_fd_events( struct fd *fd, int events ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1388 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1389 | int user = fd->poll_index; |
| 1390 | assert( poll_users[user] == fd ); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 1391 | |
| 1392 | set_fd_epoll_events( fd, user, events ); |
| 1393 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1394 | if (events == -1) /* stop waiting on this fd completely */ |
| 1395 | { |
| 1396 | pollfd[user].fd = -1; |
| 1397 | pollfd[user].events = POLLERR; |
| 1398 | pollfd[user].revents = 0; |
| 1399 | } |
| 1400 | else if (pollfd[user].fd != -1 || !pollfd[user].events) |
| 1401 | { |
| 1402 | pollfd[user].fd = fd->unix_fd; |
| 1403 | pollfd[user].events = events; |
| 1404 | } |
| 1405 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1406 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1407 | /* prepare an fd for unmounting its corresponding device */ |
| 1408 | static inline void unmount_fd( struct fd *fd ) |
| 1409 | { |
| 1410 | assert( fd->inode ); |
| 1411 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1412 | async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED ); |
| 1413 | async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (fd->poll_index != -1) set_fd_events( fd, -1 ); |
| 1416 | |
| 1417 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 1418 | |
| 1419 | fd->unix_fd = -1; |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 1420 | fd->no_fd_status = STATUS_VOLUME_DISMOUNTED; |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1421 | fd->closed->unix_fd = -1; |
| 1422 | fd->closed->unlink[0] = 0; |
| 1423 | |
| 1424 | /* stop using Unix locks on this fd (existing locks have been removed by close) */ |
| 1425 | fd->fs_locks = 0; |
| 1426 | } |
| 1427 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1428 | /* allocate an fd object, without setting the unix fd yet */ |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1429 | static struct fd *alloc_fd_object(void) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1430 | { |
| 1431 | struct fd *fd = alloc_object( &fd_ops ); |
| 1432 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1433 | if (!fd) return NULL; |
| 1434 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1435 | fd->fd_ops = NULL; |
| 1436 | fd->user = NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1437 | fd->inode = NULL; |
| 1438 | fd->closed = NULL; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1439 | fd->access = 0; |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1440 | fd->options = 0; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1441 | fd->sharing = 0; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1442 | fd->unix_fd = -1; |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 1443 | fd->unix_name = NULL; |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 1444 | fd->signaled = 1; |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1445 | fd->fs_locks = 1; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1446 | fd->poll_index = -1; |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1447 | fd->read_q = NULL; |
| 1448 | fd->write_q = NULL; |
| 1449 | fd->wait_q = NULL; |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 1450 | fd->completion = NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1451 | list_init( &fd->inode_entry ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1452 | list_init( &fd->locks ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1453 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1454 | if ((fd->poll_index = add_poll_user( fd )) == -1) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1455 | { |
| 1456 | release_object( fd ); |
| 1457 | return NULL; |
| 1458 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1459 | return fd; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1462 | /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */ |
Alexandre Julliard | 017480d | 2007-05-03 16:07:30 +0200 | [diff] [blame] | 1463 | struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options ) |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1464 | { |
| 1465 | struct fd *fd = alloc_object( &fd_ops ); |
| 1466 | |
| 1467 | if (!fd) return NULL; |
| 1468 | |
| 1469 | fd->fd_ops = fd_user_ops; |
| 1470 | fd->user = user; |
| 1471 | fd->inode = NULL; |
| 1472 | fd->closed = NULL; |
| 1473 | fd->access = 0; |
Alexandre Julliard | 017480d | 2007-05-03 16:07:30 +0200 | [diff] [blame] | 1474 | fd->options = options; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1475 | fd->sharing = 0; |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 1476 | fd->unix_name = NULL; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1477 | fd->unix_fd = -1; |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 1478 | fd->signaled = 0; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1479 | fd->fs_locks = 0; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1480 | fd->poll_index = -1; |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1481 | fd->read_q = NULL; |
| 1482 | fd->write_q = NULL; |
| 1483 | fd->wait_q = NULL; |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 1484 | fd->completion = NULL; |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 1485 | fd->no_fd_status = STATUS_BAD_DEVICE_TYPE; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1486 | list_init( &fd->inode_entry ); |
| 1487 | list_init( &fd->locks ); |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1488 | return fd; |
| 1489 | } |
| 1490 | |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 1491 | /* set the status to return when the fd has no associated unix fd */ |
| 1492 | void set_no_fd_status( struct fd *fd, unsigned int status ) |
| 1493 | { |
| 1494 | fd->no_fd_status = status; |
| 1495 | } |
| 1496 | |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1497 | /* check if the desired access is possible without violating */ |
| 1498 | /* the sharing mode of other opens of the same file */ |
| 1499 | static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing ) |
| 1500 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1501 | unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1502 | unsigned int existing_access = 0; |
| 1503 | struct list *ptr; |
| 1504 | |
| 1505 | /* if access mode is 0, sharing mode is ignored */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1506 | if (!access) sharing = existing_sharing; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1507 | fd->access = access; |
| 1508 | fd->sharing = sharing; |
| 1509 | |
| 1510 | LIST_FOR_EACH( ptr, &fd->inode->open ) |
| 1511 | { |
| 1512 | struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry ); |
| 1513 | if (fd_ptr != fd) |
| 1514 | { |
| 1515 | existing_sharing &= fd_ptr->sharing; |
| 1516 | existing_access |= fd_ptr->access; |
| 1517 | } |
| 1518 | } |
| 1519 | |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1520 | if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0; |
| 1521 | if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0; |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1522 | if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0; |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1523 | if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0; |
| 1524 | if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0; |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1525 | if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1526 | return 1; |
| 1527 | } |
| 1528 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1529 | /* sets the user of an fd that previously had no user */ |
| 1530 | void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user ) |
| 1531 | { |
| 1532 | assert( fd->fd_ops == NULL ); |
| 1533 | fd->fd_ops = user_ops; |
| 1534 | fd->user = user; |
| 1535 | } |
| 1536 | |
| 1537 | /* open() wrapper that returns a struct fd with no fd user set */ |
| 1538 | struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access, |
| 1539 | unsigned int sharing, unsigned int options ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1540 | { |
| 1541 | struct stat st; |
| 1542 | struct closed_fd *closed_fd; |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1543 | struct fd *fd; |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1544 | const char *unlink_name = ""; |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1545 | int rw_mode; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1546 | |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1547 | if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) |
| 1548 | { |
| 1549 | set_error( STATUS_INVALID_PARAMETER ); |
| 1550 | return NULL; |
| 1551 | } |
| 1552 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1553 | if (!(fd = alloc_fd_object())) return NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1554 | |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1555 | fd->options = options; |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1556 | if (options & FILE_DELETE_ON_CLOSE) unlink_name = name; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1557 | if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) ))) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1558 | { |
| 1559 | release_object( fd ); |
| 1560 | return NULL; |
| 1561 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1562 | |
Alexandre Julliard | ad9b799 | 2004-04-27 02:27:09 +0000 | [diff] [blame] | 1563 | /* create the directory if needed */ |
| 1564 | if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT)) |
| 1565 | { |
| 1566 | if (mkdir( name, 0777 ) == -1) |
| 1567 | { |
| 1568 | if (errno != EEXIST || (flags & O_EXCL)) |
| 1569 | { |
| 1570 | file_set_error(); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1571 | goto error; |
Alexandre Julliard | ad9b799 | 2004-04-27 02:27:09 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); |
| 1575 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1576 | |
| 1577 | if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE)) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1578 | { |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1579 | if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR; |
| 1580 | else rw_mode = O_WRONLY; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1581 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1582 | else rw_mode = O_RDONLY; |
| 1583 | |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 1584 | if (!(fd->unix_name = mem_alloc( strlen(name) + 1 ))) goto error; |
| 1585 | strcpy( fd->unix_name, name ); |
| 1586 | |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1587 | if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1) |
| 1588 | { |
| 1589 | /* if we tried to open a directory for write access, retry read-only */ |
| 1590 | if (errno != EISDIR || |
| 1591 | !(access & FILE_UNIX_WRITE_ACCESS) || |
Jeff Zaroyko | afce615 | 2008-12-03 17:30:54 +1100 | [diff] [blame] | 1592 | (fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode )) == -1) |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1593 | { |
| 1594 | file_set_error(); |
| 1595 | goto error; |
| 1596 | } |
| 1597 | } |
| 1598 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 1599 | closed_fd->unix_fd = fd->unix_fd; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1600 | closed_fd->unlink[0] = 0; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1601 | fstat( fd->unix_fd, &st ); |
| 1602 | *mode = st.st_mode; |
| 1603 | |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1604 | /* only bother with an inode for normal files and directories */ |
| 1605 | if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1606 | { |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 1607 | struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (!inode) |
| 1610 | { |
| 1611 | /* we can close the fd because there are no others open on the same file, |
| 1612 | * otherwise we wouldn't have failed to allocate a new inode |
| 1613 | */ |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1614 | goto error; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1615 | } |
| 1616 | fd->inode = inode; |
| 1617 | fd->closed = closed_fd; |
| 1618 | list_add_head( &inode->open, &fd->inode_entry ); |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* check directory options */ |
| 1621 | if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode)) |
| 1622 | { |
| 1623 | release_object( fd ); |
| 1624 | set_error( STATUS_NOT_A_DIRECTORY ); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode)) |
| 1628 | { |
| 1629 | release_object( fd ); |
| 1630 | set_error( STATUS_FILE_IS_A_DIRECTORY ); |
| 1631 | return NULL; |
| 1632 | } |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1633 | if (!check_sharing( fd, access, sharing )) |
| 1634 | { |
| 1635 | release_object( fd ); |
| 1636 | set_error( STATUS_SHARING_VIOLATION ); |
| 1637 | return NULL; |
| 1638 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1639 | strcpy( closed_fd->unlink, unlink_name ); |
Alexandre Julliard | 60287d0 | 2004-05-22 03:15:04 +0000 | [diff] [blame] | 1640 | if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1641 | } |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1642 | else /* special file */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1643 | { |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1644 | if (options & FILE_DIRECTORY_FILE) |
| 1645 | { |
| 1646 | set_error( STATUS_NOT_A_DIRECTORY ); |
| 1647 | goto error; |
| 1648 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1649 | if (unlink_name[0]) /* we can't unlink special files */ |
| 1650 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1651 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1652 | goto error; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1653 | } |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1654 | free( closed_fd ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1655 | } |
| 1656 | return fd; |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1657 | |
| 1658 | error: |
| 1659 | release_object( fd ); |
| 1660 | free( closed_fd ); |
| 1661 | return NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* create an fd for an anonymous file */ |
| 1665 | /* if the function fails the unix fd is closed */ |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1666 | struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user, |
| 1667 | unsigned int options ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1668 | { |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1669 | struct fd *fd = alloc_fd_object(); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1670 | |
| 1671 | if (fd) |
| 1672 | { |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1673 | set_fd_user( fd, fd_user_ops, user ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1674 | fd->unix_fd = unix_fd; |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1675 | fd->options = options; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1676 | return fd; |
| 1677 | } |
| 1678 | close( unix_fd ); |
| 1679 | return NULL; |
| 1680 | } |
| 1681 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1682 | /* retrieve the object that is using an fd */ |
| 1683 | void *get_fd_user( struct fd *fd ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1684 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1685 | return fd->user; |
| 1686 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1687 | |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 1688 | /* retrieve the opening options for the fd */ |
| 1689 | unsigned int get_fd_options( struct fd *fd ) |
| 1690 | { |
| 1691 | return fd->options; |
| 1692 | } |
| 1693 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1694 | /* retrieve the unix fd for an object */ |
| 1695 | int get_unix_fd( struct fd *fd ) |
| 1696 | { |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 1697 | if (fd->unix_fd == -1) set_error( fd->no_fd_status ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1698 | return fd->unix_fd; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1701 | /* check if two file descriptors point to the same file */ |
| 1702 | int is_same_file_fd( struct fd *fd1, struct fd *fd2 ) |
| 1703 | { |
| 1704 | return fd1->inode == fd2->inode; |
| 1705 | } |
| 1706 | |
Alexandre Julliard | 5bd5136 | 2007-01-12 14:42:43 +0100 | [diff] [blame] | 1707 | /* check if fd is on a removable device */ |
| 1708 | int is_fd_removable( struct fd *fd ) |
| 1709 | { |
| 1710 | return (fd->inode && fd->inode->device->removable); |
| 1711 | } |
| 1712 | |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 1713 | /* set or clear the fd signaled state */ |
| 1714 | void set_fd_signaled( struct fd *fd, int signaled ) |
| 1715 | { |
| 1716 | fd->signaled = signaled; |
| 1717 | if (signaled) wake_up( fd->user, 0 ); |
| 1718 | } |
| 1719 | |
Alexandre Julliard | 7a344c1 | 2009-06-09 12:11:05 +0200 | [diff] [blame] | 1720 | /* set or clear the fd signaled state */ |
| 1721 | int is_fd_signaled( struct fd *fd ) |
| 1722 | { |
| 1723 | return fd->signaled; |
| 1724 | } |
| 1725 | |
Alexandre Julliard | 715d78e | 2006-11-02 20:52:22 +0100 | [diff] [blame] | 1726 | /* handler for close_handle that refuses to close fd-associated handles in other processes */ |
| 1727 | int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle ) |
| 1728 | { |
| 1729 | return (!current || current->process == process); |
| 1730 | } |
| 1731 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1732 | /* check if events are pending and if yes return which one(s) */ |
| 1733 | int check_fd_events( struct fd *fd, int events ) |
| 1734 | { |
| 1735 | struct pollfd pfd; |
| 1736 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1737 | if (fd->unix_fd == -1) return POLLERR; |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 1738 | if (fd->inode) return events; /* regular files are always signaled */ |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1739 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1740 | pfd.fd = fd->unix_fd; |
| 1741 | pfd.events = events; |
| 1742 | if (poll( &pfd, 1, 0 ) <= 0) return 0; |
| 1743 | return pfd.revents; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1746 | /* default signaled() routine for objects that poll() on an fd */ |
| 1747 | int default_fd_signaled( struct object *obj, struct thread *thread ) |
| 1748 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1749 | struct fd *fd = get_obj_fd( obj ); |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 1750 | int ret = fd->signaled; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1751 | release_object( fd ); |
| 1752 | return ret; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Alexandre Julliard | 24001e8 | 2007-10-02 14:20:15 +0200 | [diff] [blame] | 1755 | /* default map_access() routine for objects that behave like an fd */ |
| 1756 | unsigned int default_fd_map_access( struct object *obj, unsigned int access ) |
| 1757 | { |
| 1758 | if (access & GENERIC_READ) access |= FILE_GENERIC_READ; |
| 1759 | if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE; |
| 1760 | if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE; |
| 1761 | if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS; |
| 1762 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 1763 | } |
| 1764 | |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1765 | int default_fd_get_poll_events( struct fd *fd ) |
| 1766 | { |
| 1767 | int events = 0; |
| 1768 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1769 | if (async_waiting( fd->read_q )) events |= POLLIN; |
| 1770 | if (async_waiting( fd->write_q )) events |= POLLOUT; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1771 | return events; |
| 1772 | } |
| 1773 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1774 | /* default handler for poll() events */ |
| 1775 | void default_poll_event( struct fd *fd, int event ) |
| 1776 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 1777 | if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED ); |
| 1778 | if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED ); |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1779 | |
| 1780 | /* if an error occurred, stop polling this fd to avoid busy-looping */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1781 | if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 ); |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 1782 | else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Alexandre Julliard | 9ed42d2 | 2008-12-26 12:27:48 +0100 | [diff] [blame] | 1785 | struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type ) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1786 | { |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1787 | struct async_queue *queue; |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1788 | struct async *async; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1789 | |
| 1790 | switch (type) |
| 1791 | { |
| 1792 | case ASYNC_TYPE_READ: |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1793 | if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL; |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1794 | queue = fd->read_q; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | case ASYNC_TYPE_WRITE: |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1797 | if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL; |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1798 | queue = fd->write_q; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1799 | break; |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1800 | case ASYNC_TYPE_WAIT: |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1801 | if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL; |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1802 | queue = fd->wait_q; |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1803 | break; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1804 | default: |
Gerald Pfeifer | d529c63 | 2007-04-20 13:11:54 +0200 | [diff] [blame] | 1805 | queue = NULL; |
Alexandre Julliard | 02ed704 | 2007-04-02 20:24:55 +0200 | [diff] [blame] | 1806 | assert(0); |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Alexandre Julliard | c18e8d6 | 2007-04-18 16:28:01 +0200 | [diff] [blame] | 1809 | if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT) |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1810 | { |
| 1811 | if (!fd->inode) |
| 1812 | set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) ); |
| 1813 | else /* regular files are always ready for read and write */ |
Alexandre Julliard | c18e8d6 | 2007-04-18 16:28:01 +0200 | [diff] [blame] | 1814 | async_wake_up( queue, STATUS_ALERTED ); |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1815 | } |
| 1816 | return async; |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1819 | void fd_async_wake_up( struct fd *fd, int type, unsigned int status ) |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1820 | { |
| 1821 | switch (type) |
| 1822 | { |
| 1823 | case ASYNC_TYPE_READ: |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1824 | async_wake_up( fd->read_q, status ); |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1825 | break; |
| 1826 | case ASYNC_TYPE_WRITE: |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1827 | async_wake_up( fd->write_q, status ); |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1828 | break; |
| 1829 | case ASYNC_TYPE_WAIT: |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1830 | async_wake_up( fd->wait_q, status ); |
Alexandre Julliard | 95ba4b5 | 2007-04-02 12:47:52 +0200 | [diff] [blame] | 1831 | break; |
| 1832 | default: |
| 1833 | assert(0); |
| 1834 | } |
| 1835 | } |
| 1836 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 1837 | void fd_reselect_async( struct fd *fd, struct async_queue *queue ) |
| 1838 | { |
| 1839 | fd->fd_ops->reselect_async( fd, queue ); |
| 1840 | } |
| 1841 | |
Alexandre Julliard | 111610c | 2007-03-20 20:21:12 +0100 | [diff] [blame] | 1842 | void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count ) |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1843 | { |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1844 | struct async *async; |
Alexandre Julliard | e92f854 | 2007-04-02 12:48:13 +0200 | [diff] [blame] | 1845 | |
Alexandre Julliard | 9ed42d2 | 2008-12-26 12:27:48 +0100 | [diff] [blame] | 1846 | if ((async = fd_queue_async( fd, data, type ))) |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1847 | { |
| 1848 | release_object( async ); |
| 1849 | set_error( STATUS_PENDING ); |
| 1850 | } |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 1853 | /* default reselect_async() fd routine */ |
| 1854 | void default_fd_reselect_async( struct fd *fd, struct async_queue *queue ) |
| 1855 | { |
| 1856 | if (queue != fd->wait_q) |
| 1857 | { |
| 1858 | int poll_events = fd->fd_ops->get_poll_events( fd ); |
| 1859 | int events = check_fd_events( fd, poll_events ); |
| 1860 | if (events) fd->fd_ops->poll_event( fd, events ); |
| 1861 | else set_fd_events( fd, poll_events ); |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | /* default cancel_async() fd routine */ |
Mike Kaplinskiy | b05774e | 2009-08-09 00:08:27 -0400 | [diff] [blame] | 1866 | void default_fd_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb ) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1867 | { |
Mike Kaplinskiy | b05774e | 2009-08-09 00:08:27 -0400 | [diff] [blame] | 1868 | int n = 0; |
| 1869 | |
| 1870 | n += async_wake_up_by( fd->read_q, process, thread, iosb, STATUS_CANCELLED ); |
| 1871 | n += async_wake_up_by( fd->write_q, process, thread, iosb, STATUS_CANCELLED ); |
| 1872 | n += async_wake_up_by( fd->wait_q, process, thread, iosb, STATUS_CANCELLED ); |
| 1873 | if (!n && iosb) |
| 1874 | set_error( STATUS_NOT_FOUND ); |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1877 | /* default flush() routine */ |
Alexandre Julliard | df65187 | 2007-03-27 16:51:44 +0200 | [diff] [blame] | 1878 | void no_flush( struct fd *fd, struct event **event ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1879 | { |
| 1880 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Alexandre Julliard | 01dd1ff | 2006-10-03 14:54:21 +0200 | [diff] [blame] | 1883 | static inline int is_valid_mounted_device( struct stat *st ) |
| 1884 | { |
| 1885 | #if defined(linux) || defined(__sun__) |
| 1886 | return S_ISBLK( st->st_mode ); |
| 1887 | #else |
| 1888 | /* disks are char devices on *BSD */ |
| 1889 | return S_ISCHR( st->st_mode ); |
| 1890 | #endif |
| 1891 | } |
| 1892 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1893 | /* close all Unix file descriptors on a device to allow unmounting it */ |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1894 | static void unmount_device( struct fd *device_fd ) |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1895 | { |
| 1896 | unsigned int i; |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1897 | struct stat st; |
| 1898 | struct device *device; |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1899 | struct inode *inode; |
| 1900 | struct fd *fd; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1901 | int unix_fd = get_unix_fd( device_fd ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1902 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1903 | if (unix_fd == -1) return; |
| 1904 | |
Alexandre Julliard | 01dd1ff | 2006-10-03 14:54:21 +0200 | [diff] [blame] | 1905 | if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st )) |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1906 | { |
| 1907 | set_error( STATUS_INVALID_PARAMETER ); |
| 1908 | return; |
| 1909 | } |
| 1910 | |
Alexandre Julliard | 30b878b | 2006-11-01 13:28:05 +0100 | [diff] [blame] | 1911 | if (!(device = get_device( st.st_rdev, -1 ))) return; |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1912 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1913 | for (i = 0; i < INODE_HASH_SIZE; i++) |
| 1914 | { |
| 1915 | LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry ) |
| 1916 | { |
| 1917 | LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry ) |
| 1918 | { |
| 1919 | unmount_fd( fd ); |
| 1920 | } |
| 1921 | inode_close_pending( inode, 0 ); |
| 1922 | } |
| 1923 | } |
| 1924 | /* remove it from the hash table */ |
| 1925 | list_remove( &device->entry ); |
| 1926 | list_init( &device->entry ); |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1927 | release_object( device ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Alexandre Julliard | 2669af7 | 2007-04-16 14:54:52 +0200 | [diff] [blame] | 1930 | /* default ioctl() routine */ |
Alexandre Julliard | fd59e15 | 2007-05-03 17:43:18 +0200 | [diff] [blame] | 1931 | obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async, |
Alexandre Julliard | 7ec95c5 | 2008-12-30 20:37:20 +0100 | [diff] [blame] | 1932 | int blocking, const void *data, data_size_t size ) |
Alexandre Julliard | 2669af7 | 2007-04-16 14:54:52 +0200 | [diff] [blame] | 1933 | { |
| 1934 | switch(code) |
| 1935 | { |
| 1936 | case FSCTL_DISMOUNT_VOLUME: |
| 1937 | unmount_device( fd ); |
Alexandre Julliard | fd59e15 | 2007-05-03 17:43:18 +0200 | [diff] [blame] | 1938 | return 0; |
Alexandre Julliard | 2669af7 | 2007-04-16 14:54:52 +0200 | [diff] [blame] | 1939 | default: |
| 1940 | set_error( STATUS_NOT_SUPPORTED ); |
Alexandre Julliard | fd59e15 | 2007-05-03 17:43:18 +0200 | [diff] [blame] | 1941 | return 0; |
Alexandre Julliard | 2669af7 | 2007-04-16 14:54:52 +0200 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1945 | /* same as get_handle_obj but retrieve the struct fd associated to the object */ |
| 1946 | static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle, |
| 1947 | unsigned int access ) |
| 1948 | { |
| 1949 | struct fd *fd = NULL; |
| 1950 | struct object *obj; |
| 1951 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1952 | if ((obj = get_handle_obj( process, handle, access, NULL ))) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1953 | { |
Bill Medland | 3f7c3ff | 2003-04-17 02:14:04 +0000 | [diff] [blame] | 1954 | fd = get_obj_fd( obj ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1955 | release_object( obj ); |
| 1956 | } |
| 1957 | return fd; |
| 1958 | } |
| 1959 | |
Alexandre Julliard | 50171c5 | 2009-03-02 20:34:39 +0100 | [diff] [blame] | 1960 | struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key ) |
Andrey Turkin | c702a91 | 2007-11-10 01:11:48 +0300 | [diff] [blame] | 1961 | { |
Andrey Turkin | 3afbee5 | 2007-12-17 22:06:17 +0300 | [diff] [blame] | 1962 | *p_key = fd->comp_key; |
Alexandre Julliard | 50171c5 | 2009-03-02 20:34:39 +0100 | [diff] [blame] | 1963 | return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL; |
| 1964 | } |
| 1965 | |
| 1966 | void fd_copy_completion( struct fd *src, struct fd *dst ) |
| 1967 | { |
| 1968 | assert( !dst->completion ); |
| 1969 | dst->completion = fd_get_completion( src, &dst->comp_key ); |
Andrey Turkin | c702a91 | 2007-11-10 01:11:48 +0300 | [diff] [blame] | 1970 | } |
| 1971 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1972 | /* flush a file buffers */ |
| 1973 | DECL_HANDLER(flush_file) |
| 1974 | { |
| 1975 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1976 | struct event * event = NULL; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1977 | |
| 1978 | if (fd) |
| 1979 | { |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1980 | fd->fd_ops->flush( fd, &event ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1981 | if ( event ) |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1982 | { |
| 1983 | reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 ); |
| 1984 | } |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1985 | release_object( fd ); |
| 1986 | } |
| 1987 | } |
| 1988 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1989 | /* open a file object */ |
| 1990 | DECL_HANDLER(open_file_object) |
| 1991 | { |
| 1992 | struct unicode_str name; |
| 1993 | struct directory *root = NULL; |
Alexandre Julliard | 94655c8 | 2007-03-22 11:52:40 +0100 | [diff] [blame] | 1994 | struct object *obj, *result; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1995 | |
| 1996 | get_req_unicode_str( &name ); |
| 1997 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 1998 | return; |
| 1999 | |
| 2000 | if ((obj = open_object_dir( root, &name, req->attributes, NULL ))) |
| 2001 | { |
Alexandre Julliard | 94655c8 | 2007-03-22 11:52:40 +0100 | [diff] [blame] | 2002 | if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options ))) |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 2003 | { |
Alexandre Julliard | 94655c8 | 2007-03-22 11:52:40 +0100 | [diff] [blame] | 2004 | reply->handle = alloc_handle( current->process, result, req->access, req->attributes ); |
| 2005 | release_object( result ); |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 2006 | } |
| 2007 | release_object( obj ); |
| 2008 | } |
| 2009 | |
| 2010 | if (root) release_object( root ); |
| 2011 | } |
| 2012 | |
Henri Verbeet | 27705d5 | 2009-11-12 15:10:12 +0100 | [diff] [blame] | 2013 | /* get the Unix name from a file handle */ |
| 2014 | DECL_HANDLER(get_handle_unix_name) |
| 2015 | { |
| 2016 | struct fd *fd; |
| 2017 | |
| 2018 | if ((fd = get_handle_fd_obj( current->process, req->handle, 0 ))) |
| 2019 | { |
| 2020 | if (fd->unix_name) |
| 2021 | { |
| 2022 | data_size_t name_len = strlen( fd->unix_name ); |
| 2023 | reply->name_len = name_len; |
| 2024 | if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len ); |
| 2025 | else set_error( STATUS_BUFFER_OVERFLOW ); |
| 2026 | } |
| 2027 | release_object( fd ); |
| 2028 | } |
| 2029 | } |
| 2030 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 2031 | /* get a Unix fd to access a file */ |
| 2032 | DECL_HANDLER(get_handle_fd) |
| 2033 | { |
| 2034 | struct fd *fd; |
| 2035 | |
Alexandre Julliard | d85121f | 2007-04-10 22:32:46 +0200 | [diff] [blame] | 2036 | if ((fd = get_handle_fd_obj( current->process, req->handle, 0 ))) |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 2037 | { |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 2038 | int unix_fd = get_unix_fd( fd ); |
| 2039 | if (unix_fd != -1) |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 2040 | { |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 2041 | reply->type = fd->fd_ops->get_fd_type( fd ); |
| 2042 | reply->removable = is_fd_removable(fd); |
| 2043 | reply->options = fd->options; |
| 2044 | reply->access = get_handle_access( current->process, req->handle ); |
Alexandre Julliard | a319263 | 2008-02-19 16:51:16 +0100 | [diff] [blame] | 2045 | send_client_fd( current->process, unix_fd, req->handle ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 2046 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2047 | release_object( fd ); |
| 2048 | } |
| 2049 | } |
| 2050 | |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 2051 | /* perform an ioctl on a file */ |
| 2052 | DECL_HANDLER(ioctl) |
| 2053 | { |
| 2054 | unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA); |
Alexandre Julliard | a7b3efd | 2008-12-26 12:17:20 +0100 | [diff] [blame] | 2055 | struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access ); |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 2056 | |
| 2057 | if (fd) |
| 2058 | { |
Alexandre Julliard | 7ec95c5 | 2008-12-30 20:37:20 +0100 | [diff] [blame] | 2059 | reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async, req->blocking, |
Alexandre Julliard | fd59e15 | 2007-05-03 17:43:18 +0200 | [diff] [blame] | 2060 | get_req_data(), get_req_data_size() ); |
| 2061 | reply->options = fd->options; |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 2062 | release_object( fd ); |
| 2063 | } |
| 2064 | } |
| 2065 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2066 | /* create / reschedule an async I/O */ |
| 2067 | DECL_HANDLER(register_async) |
| 2068 | { |
Alexandre Julliard | 02ed704 | 2007-04-02 20:24:55 +0200 | [diff] [blame] | 2069 | unsigned int access; |
| 2070 | struct fd *fd; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2071 | |
Alexandre Julliard | 02ed704 | 2007-04-02 20:24:55 +0200 | [diff] [blame] | 2072 | switch(req->type) |
| 2073 | { |
| 2074 | case ASYNC_TYPE_READ: |
| 2075 | access = FILE_READ_DATA; |
| 2076 | break; |
| 2077 | case ASYNC_TYPE_WRITE: |
| 2078 | access = FILE_WRITE_DATA; |
| 2079 | break; |
| 2080 | default: |
| 2081 | set_error( STATUS_INVALID_PARAMETER ); |
| 2082 | return; |
| 2083 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2084 | |
Alexandre Julliard | a7b3efd | 2008-12-26 12:17:20 +0100 | [diff] [blame] | 2085 | if ((fd = get_handle_fd_obj( current->process, req->async.handle, access ))) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2086 | { |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 2087 | if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 2088 | release_object( fd ); |
| 2089 | } |
| 2090 | } |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* cancels all async I/O */ |
| 2093 | DECL_HANDLER(cancel_async) |
| 2094 | { |
| 2095 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
Mike Kaplinskiy | b05774e | 2009-08-09 00:08:27 -0400 | [diff] [blame] | 2096 | struct thread *thread = req->only_thread ? current : NULL; |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 2097 | |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 2098 | if (fd) |
| 2099 | { |
Mike Kaplinskiy | b05774e | 2009-08-09 00:08:27 -0400 | [diff] [blame] | 2100 | if (get_unix_fd( fd ) != -1) fd->fd_ops->cancel_async( fd, current->process, thread, req->iosb ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 2101 | release_object( fd ); |
Alexandre Julliard | f3fbae4 | 2007-04-18 16:05:59 +0200 | [diff] [blame] | 2102 | } |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 2103 | } |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 2104 | |
| 2105 | /* attach completion object to a fd */ |
| 2106 | DECL_HANDLER(set_completion_info) |
| 2107 | { |
| 2108 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
| 2109 | |
| 2110 | if (fd) |
| 2111 | { |
Andrey Turkin | f1dcf4b | 2007-12-17 02:02:12 +0300 | [diff] [blame] | 2112 | if (!(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) && !fd->completion) |
Andrey Turkin | d1a8155 | 2007-09-28 00:03:39 +0400 | [diff] [blame] | 2113 | { |
| 2114 | fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE ); |
| 2115 | fd->comp_key = req->ckey; |
| 2116 | } |
| 2117 | else set_error( STATUS_INVALID_PARAMETER ); |
| 2118 | release_object( fd ); |
| 2119 | } |
| 2120 | } |
Andrey Turkin | 27cb7c7 | 2007-11-10 01:11:58 +0300 | [diff] [blame] | 2121 | |
| 2122 | /* push new completion msg into a completion queue attached to the fd */ |
| 2123 | DECL_HANDLER(add_fd_completion) |
| 2124 | { |
| 2125 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
| 2126 | if (fd) |
| 2127 | { |
Andrey Turkin | 3afbee5 | 2007-12-17 22:06:17 +0300 | [diff] [blame] | 2128 | if (fd->completion) |
| 2129 | add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information ); |
Andrey Turkin | 27cb7c7 | 2007-11-10 01:11:58 +0300 | [diff] [blame] | 2130 | release_object( fd ); |
| 2131 | } |
| 2132 | } |