Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side change notification management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 5 | * Copyright (C) 2006 Mike McCormack |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include "wine/port.h" |
| 24 | |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 25 | #include <assert.h> |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Hans Leidekker | ff49652 | 2004-02-05 01:45:58 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <limits.h> |
| 33 | #include <dirent.h> |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #ifdef HAVE_SYS_ERRNO_H |
| 36 | #include <sys/errno.h> |
| 37 | #endif |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 38 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 39 | #include "ntstatus.h" |
| 40 | #define WIN32_NO_STATUS |
Alexandre Julliard | 435e2e6 | 2002-12-10 22:56:43 +0000 | [diff] [blame] | 41 | #include "windef.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 42 | |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 43 | #include "file.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 44 | #include "handle.h" |
| 45 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 46 | #include "request.h" |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 47 | #include "process.h" |
| 48 | #include "security.h" |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 49 | #include "winternl.h" |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 50 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 51 | /* dnotify support */ |
| 52 | |
Huw Davies | bdb63ec | 2004-02-10 20:08:24 +0000 | [diff] [blame] | 53 | #ifdef linux |
| 54 | #ifndef F_NOTIFY |
| 55 | #define F_NOTIFY 1026 |
| 56 | #define DN_ACCESS 0x00000001 /* File accessed */ |
| 57 | #define DN_MODIFY 0x00000002 /* File modified */ |
| 58 | #define DN_CREATE 0x00000004 /* File created */ |
| 59 | #define DN_DELETE 0x00000008 /* File removed */ |
| 60 | #define DN_RENAME 0x00000010 /* File renamed */ |
Francois Gouget | 8a18e0e | 2008-04-07 13:01:02 +0200 | [diff] [blame] | 61 | #define DN_ATTRIB 0x00000020 /* File changed attributes */ |
Huw Davies | bdb63ec | 2004-02-10 20:08:24 +0000 | [diff] [blame] | 62 | #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */ |
| 63 | #endif |
| 64 | #endif |
| 65 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 66 | /* inotify support */ |
| 67 | |
Alexandre Julliard | 61a7036 | 2009-06-26 15:45:03 +0200 | [diff] [blame] | 68 | #ifdef HAVE_SYS_INOTIFY_H |
| 69 | #include <sys/inotify.h> |
| 70 | #define USE_INOTIFY |
| 71 | #elif defined(__linux__) && defined(__i386__) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 72 | |
| 73 | #define SYS_inotify_init 291 |
| 74 | #define SYS_inotify_add_watch 292 |
| 75 | #define SYS_inotify_rm_watch 293 |
| 76 | |
| 77 | struct inotify_event { |
| 78 | int wd; |
| 79 | unsigned int mask; |
| 80 | unsigned int cookie; |
| 81 | unsigned int len; |
| 82 | char name[1]; |
| 83 | }; |
| 84 | |
| 85 | #define IN_ACCESS 0x00000001 |
| 86 | #define IN_MODIFY 0x00000002 |
| 87 | #define IN_ATTRIB 0x00000004 |
| 88 | #define IN_CLOSE_WRITE 0x00000008 |
| 89 | #define IN_CLOSE_NOWRITE 0x00000010 |
| 90 | #define IN_OPEN 0x00000020 |
| 91 | #define IN_MOVED_FROM 0x00000040 |
| 92 | #define IN_MOVED_TO 0x00000080 |
| 93 | #define IN_CREATE 0x00000100 |
| 94 | #define IN_DELETE 0x00000200 |
| 95 | #define IN_DELETE_SELF 0x00000400 |
| 96 | |
| 97 | static inline int inotify_init( void ) |
| 98 | { |
| 99 | int ret; |
| 100 | __asm__ __volatile__( "int $0x80" |
| 101 | : "=a" (ret) |
| 102 | : "0" (SYS_inotify_init)); |
| 103 | if (ret<0) { errno = -ret; ret = -1; } |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static inline int inotify_add_watch( int fd, const char *name, unsigned int mask ) |
| 108 | { |
| 109 | int ret; |
| 110 | __asm__ __volatile__( "pushl %%ebx;\n\t" |
| 111 | "movl %2,%%ebx;\n\t" |
| 112 | "int $0x80;\n\t" |
| 113 | "popl %%ebx" |
| 114 | : "=a" (ret) : "0" (SYS_inotify_add_watch), |
| 115 | "r" (fd), "c" (name), "d" (mask) ); |
| 116 | if (ret<0) { errno = -ret; ret = -1; } |
| 117 | return ret; |
| 118 | } |
| 119 | |
Alexandre Julliard | 61a7036 | 2009-06-26 15:45:03 +0200 | [diff] [blame] | 120 | static inline int inotify_rm_watch( int fd, int wd ) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | __asm__ __volatile__( "pushl %%ebx;\n\t" |
| 124 | "movl %2,%%ebx;\n\t" |
| 125 | "int $0x80;\n\t" |
| 126 | "popl %%ebx" |
| 127 | : "=a" (ret) : "0" (SYS_inotify_rm_watch), |
| 128 | "r" (fd), "c" (wd) ); |
| 129 | if (ret<0) { errno = -ret; ret = -1; } |
| 130 | return ret; |
| 131 | } |
| 132 | |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 133 | #define USE_INOTIFY |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 134 | |
| 135 | #endif |
| 136 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 137 | struct inode; |
| 138 | |
| 139 | static void free_inode( struct inode *inode ); |
| 140 | |
| 141 | static struct fd *inotify_fd; |
| 142 | |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 143 | struct change_record { |
| 144 | struct list entry; |
| 145 | int action; |
| 146 | int len; |
| 147 | char name[1]; |
| 148 | }; |
| 149 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 150 | struct dir |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 151 | { |
| 152 | struct object obj; /* object header */ |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 153 | struct fd *fd; /* file descriptor to the directory */ |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 154 | mode_t mode; /* file stat.st_mode */ |
| 155 | uid_t uid; /* file stat.st_uid */ |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 156 | struct list entry; /* entry in global change notifications list */ |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 157 | unsigned int filter; /* notification filter */ |
Alexandre Julliard | 43c6396 | 2005-09-26 13:51:58 +0000 | [diff] [blame] | 158 | int notified; /* SIGIO counter */ |
Mike McCormack | 0790f95 | 2006-02-07 16:50:36 +0100 | [diff] [blame] | 159 | int want_data; /* return change data */ |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 160 | int subtree; /* do we want to watch subdirectories? */ |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 161 | struct list change_records; /* data for the change */ |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 162 | struct list in_entry; /* entry in the inode dirs list */ |
| 163 | struct inode *inode; /* inode of the associated directory */ |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 166 | static struct fd *dir_get_fd( struct object *obj ); |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 167 | static struct security_descriptor *dir_get_sd( struct object *obj ); |
| 168 | static int dir_set_sd( struct object *obj, const struct security_descriptor *sd, |
| 169 | unsigned int set_info ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 170 | static void dir_dump( struct object *obj, int verbose ); |
| 171 | static void dir_destroy( struct object *obj ); |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 172 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 173 | static const struct object_ops dir_ops = |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 174 | { |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 175 | sizeof(struct dir), /* size */ |
| 176 | dir_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 177 | no_get_type, /* get_type */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 178 | add_queue, /* add_queue */ |
| 179 | remove_queue, /* remove_queue */ |
Alexandre Julliard | a867553 | 2007-04-04 19:54:33 +0200 | [diff] [blame] | 180 | default_fd_signaled, /* signaled */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 181 | no_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 182 | no_signal, /* signal */ |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 183 | dir_get_fd, /* get_fd */ |
Alexandre Julliard | 24001e8 | 2007-10-02 14:20:15 +0200 | [diff] [blame] | 184 | default_fd_map_access, /* map_access */ |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 185 | dir_get_sd, /* get_sd */ |
| 186 | dir_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 187 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 188 | no_open_file, /* open_file */ |
Alexandre Julliard | 715d78e | 2006-11-02 20:52:22 +0100 | [diff] [blame] | 189 | fd_close_handle, /* close_handle */ |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 190 | dir_destroy /* destroy */ |
| 191 | }; |
| 192 | |
| 193 | static int dir_get_poll_events( struct fd *fd ); |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 194 | static enum server_fd_type dir_get_fd_type( struct fd *fd ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 195 | |
| 196 | static const struct fd_ops dir_fd_ops = |
| 197 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 198 | dir_get_poll_events, /* get_poll_events */ |
| 199 | default_poll_event, /* poll_event */ |
| 200 | no_flush, /* flush */ |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 201 | dir_get_fd_type, /* get_fd_type */ |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 202 | default_fd_ioctl, /* ioctl */ |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 203 | default_fd_queue_async, /* queue_async */ |
| 204 | default_fd_reselect_async, /* reselect_async */ |
| 205 | default_fd_cancel_async /* cancel_async */ |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 208 | static struct list change_list = LIST_INIT(change_list); |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 209 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 210 | static void dnotify_adjust_changes( struct dir *dir ) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 211 | { |
Alexandre Julliard | e8a339c | 2004-03-01 21:32:02 +0000 | [diff] [blame] | 212 | #if defined(F_SETSIG) && defined(F_NOTIFY) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 213 | int fd = get_unix_fd( dir->fd ); |
| 214 | unsigned int filter = dir->filter; |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 215 | unsigned int val; |
| 216 | if ( 0 > fcntl( fd, F_SETSIG, SIGIO) ) |
| 217 | return; |
| 218 | |
| 219 | val = DN_MULTISHOT; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 220 | if (filter & FILE_NOTIFY_CHANGE_FILE_NAME) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 221 | val |= DN_RENAME | DN_DELETE | DN_CREATE; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 222 | if (filter & FILE_NOTIFY_CHANGE_DIR_NAME) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 223 | val |= DN_RENAME | DN_DELETE | DN_CREATE; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 224 | if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 225 | val |= DN_ATTRIB; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 226 | if (filter & FILE_NOTIFY_CHANGE_SIZE) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 227 | val |= DN_MODIFY; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 228 | if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 229 | val |= DN_MODIFY; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 230 | if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS) |
Hans Leidekker | ff49652 | 2004-02-05 01:45:58 +0000 | [diff] [blame] | 231 | val |= DN_ACCESS; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 232 | if (filter & FILE_NOTIFY_CHANGE_CREATION) |
Hans Leidekker | ff49652 | 2004-02-05 01:45:58 +0000 | [diff] [blame] | 233 | val |= DN_CREATE; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 234 | if (filter & FILE_NOTIFY_CHANGE_SECURITY) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 235 | val |= DN_ATTRIB; |
| 236 | fcntl( fd, F_NOTIFY, val ); |
| 237 | #endif |
| 238 | } |
| 239 | |
| 240 | /* insert change in the global list */ |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 241 | static inline void insert_change( struct dir *dir ) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 242 | { |
| 243 | sigset_t sigset; |
| 244 | |
| 245 | sigemptyset( &sigset ); |
| 246 | sigaddset( &sigset, SIGIO ); |
| 247 | sigprocmask( SIG_BLOCK, &sigset, NULL ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 248 | list_add_head( &change_list, &dir->entry ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 249 | sigprocmask( SIG_UNBLOCK, &sigset, NULL ); |
| 250 | } |
| 251 | |
| 252 | /* remove change from the global list */ |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 253 | static inline void remove_change( struct dir *dir ) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 254 | { |
| 255 | sigset_t sigset; |
| 256 | |
| 257 | sigemptyset( &sigset ); |
| 258 | sigaddset( &sigset, SIGIO ); |
| 259 | sigprocmask( SIG_BLOCK, &sigset, NULL ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 260 | list_remove( &dir->entry ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 261 | sigprocmask( SIG_UNBLOCK, &sigset, NULL ); |
| 262 | } |
| 263 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 264 | static void dir_dump( struct object *obj, int verbose ) |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 265 | { |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 266 | struct dir *dir = (struct dir *)obj; |
| 267 | assert( obj->ops == &dir_ops ); |
Alexandre Julliard | a867553 | 2007-04-04 19:54:33 +0200 | [diff] [blame] | 268 | fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* enter here directly from SIGIO signal handler */ |
| 272 | void do_change_notify( int unix_fd ) |
| 273 | { |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 274 | struct dir *dir; |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 275 | |
| 276 | /* FIXME: this is O(n) ... probably can be improved */ |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 277 | LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry ) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 278 | { |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 279 | if (get_unix_fd( dir->fd ) != unix_fd) continue; |
| 280 | interlocked_xchg_add( &dir->notified, 1 ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* SIGIO callback, called synchronously with the poll loop */ |
| 286 | void sigio_callback(void) |
| 287 | { |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 288 | struct dir *dir; |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 289 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 290 | LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry ) |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 291 | { |
Alexandre Julliard | a867553 | 2007-04-04 19:54:33 +0200 | [diff] [blame] | 292 | if (interlocked_xchg( &dir->notified, 0 )) |
| 293 | fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 294 | } |
Alexandre Julliard | 63cb0f8 | 1998-12-31 15:43:48 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 297 | static struct fd *dir_get_fd( struct object *obj ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 298 | { |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 299 | struct dir *dir = (struct dir *)obj; |
| 300 | assert( obj->ops == &dir_ops ); |
| 301 | return (struct fd *)grab_object( dir->fd ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 304 | static int get_dir_unix_fd( struct dir *dir ) |
| 305 | { |
| 306 | return get_unix_fd( dir->fd ); |
| 307 | } |
| 308 | |
| 309 | static struct security_descriptor *dir_get_sd( struct object *obj ) |
| 310 | { |
| 311 | struct dir *dir = (struct dir *)obj; |
| 312 | int unix_fd; |
| 313 | struct stat st; |
| 314 | struct security_descriptor *sd; |
| 315 | assert( obj->ops == &dir_ops ); |
| 316 | |
| 317 | unix_fd = get_dir_unix_fd( dir ); |
| 318 | |
| 319 | if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) |
| 320 | return obj->sd; |
| 321 | |
| 322 | /* mode and uid the same? if so, no need to re-generate security descriptor */ |
| 323 | if (obj->sd && |
| 324 | (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) && |
| 325 | (st.st_uid == dir->uid)) |
| 326 | return obj->sd; |
| 327 | |
| 328 | sd = mode_to_sd( st.st_mode, |
| 329 | security_unix_uid_to_sid( st.st_uid ), |
| 330 | token_get_primary_group( current->process->token )); |
| 331 | if (!sd) return obj->sd; |
| 332 | |
| 333 | dir->mode = st.st_mode; |
| 334 | dir->uid = st.st_uid; |
| 335 | free( obj->sd ); |
| 336 | obj->sd = sd; |
| 337 | return sd; |
| 338 | } |
| 339 | |
| 340 | static int dir_set_sd( struct object *obj, const struct security_descriptor *sd, |
| 341 | unsigned int set_info ) |
| 342 | { |
| 343 | struct dir *dir = (struct dir *)obj; |
| 344 | const SID *owner; |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 345 | struct stat st; |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 346 | mode_t mode; |
| 347 | int unix_fd; |
| 348 | |
| 349 | assert( obj->ops == &dir_ops ); |
| 350 | |
| 351 | unix_fd = get_dir_unix_fd( dir ); |
| 352 | |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 353 | if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1; |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 354 | |
| 355 | if (set_info & OWNER_SECURITY_INFORMATION) |
| 356 | { |
| 357 | owner = sd_get_owner( sd ); |
| 358 | if (!owner) |
| 359 | { |
| 360 | set_error( STATUS_INVALID_SECURITY_DESCR ); |
| 361 | return 0; |
| 362 | } |
| 363 | if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) )) |
| 364 | { |
| 365 | /* FIXME: get Unix uid and call fchown */ |
| 366 | } |
| 367 | } |
| 368 | else if (obj->sd) |
| 369 | owner = sd_get_owner( obj->sd ); |
| 370 | else |
| 371 | owner = token_get_user( current->process->token ); |
| 372 | |
| 373 | if (set_info & DACL_SECURITY_INFORMATION) |
| 374 | { |
| 375 | /* keep the bits that we don't map to access rights in the ACL */ |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 376 | mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX); |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 377 | mode |= sd_to_mode( sd, owner ); |
| 378 | |
Paul Chitescu | 9235249 | 2009-12-08 14:10:01 +0200 | [diff] [blame] | 379 | if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1) |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 380 | { |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 381 | file_set_error(); |
| 382 | return 0; |
Paul Bryan Roberts | 6435a5d | 2008-11-03 22:37:17 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | return 1; |
| 386 | } |
| 387 | |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 388 | static struct change_record *get_first_change_record( struct dir *dir ) |
| 389 | { |
| 390 | struct list *ptr = list_head( &dir->change_records ); |
| 391 | if (!ptr) return NULL; |
| 392 | list_remove( ptr ); |
| 393 | return LIST_ENTRY( ptr, struct change_record, entry ); |
| 394 | } |
| 395 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 396 | static void dir_destroy( struct object *obj ) |
| 397 | { |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 398 | struct change_record *record; |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 399 | struct dir *dir = (struct dir *)obj; |
| 400 | assert (obj->ops == &dir_ops); |
| 401 | |
| 402 | if (dir->filter) |
| 403 | remove_change( dir ); |
| 404 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 405 | if (dir->inode) |
| 406 | { |
| 407 | list_remove( &dir->in_entry ); |
| 408 | free_inode( dir->inode ); |
| 409 | } |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 410 | |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 411 | while ((record = get_first_change_record( dir ))) free( record ); |
| 412 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 413 | release_object( dir->fd ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 414 | |
| 415 | if (inotify_fd && list_empty( &change_list )) |
| 416 | { |
| 417 | release_object( inotify_fd ); |
| 418 | inotify_fd = NULL; |
| 419 | } |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 422 | struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access ) |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 423 | { |
| 424 | return (struct dir *)get_handle_obj( process, handle, access, &dir_ops ); |
| 425 | } |
| 426 | |
| 427 | static int dir_get_poll_events( struct fd *fd ) |
| 428 | { |
| 429 | return 0; |
| 430 | } |
| 431 | |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 432 | static enum server_fd_type dir_get_fd_type( struct fd *fd ) |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 433 | { |
Alexandre Julliard | 8930427 | 2006-11-20 14:14:04 +0100 | [diff] [blame] | 434 | return FD_TYPE_DIR; |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 437 | #ifdef USE_INOTIFY |
| 438 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 439 | #define HASH_SIZE 31 |
| 440 | |
| 441 | struct inode { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 442 | struct list ch_entry; /* entry in the children list */ |
| 443 | struct list children; /* children of this inode */ |
| 444 | struct inode *parent; /* parent of this inode */ |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 445 | struct list dirs; /* directory handles watching this inode */ |
| 446 | struct list ino_entry; /* entry in the inode hash */ |
| 447 | struct list wd_entry; /* entry in the watch descriptor hash */ |
| 448 | dev_t dev; /* device number */ |
| 449 | ino_t ino; /* device's inode number */ |
| 450 | int wd; /* inotify's watch descriptor */ |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 451 | char *name; /* basename name of the inode */ |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 452 | }; |
| 453 | |
Rob Shearman | f12f850 | 2010-01-21 11:57:45 +0000 | [diff] [blame] | 454 | static struct list inode_hash[ HASH_SIZE ]; |
| 455 | static struct list wd_hash[ HASH_SIZE ]; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 456 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 457 | static int inotify_add_dir( char *path, unsigned int filter ); |
| 458 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 459 | static struct inode *inode_from_wd( int wd ) |
| 460 | { |
| 461 | struct list *bucket = &wd_hash[ wd % HASH_SIZE ]; |
| 462 | struct inode *inode; |
| 463 | |
| 464 | LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry ) |
| 465 | if (inode->wd == wd) |
| 466 | return inode; |
| 467 | |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | static inline struct list *get_hash_list( dev_t dev, ino_t ino ) |
| 472 | { |
| 473 | return &inode_hash[ (ino ^ dev) % HASH_SIZE ]; |
| 474 | } |
| 475 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 476 | static struct inode *find_inode( dev_t dev, ino_t ino ) |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 477 | { |
| 478 | struct list *bucket = get_hash_list( dev, ino ); |
| 479 | struct inode *inode; |
| 480 | |
| 481 | LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry ) |
| 482 | if (inode->ino == ino && inode->dev == dev) |
| 483 | return inode; |
| 484 | |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | static struct inode *create_inode( dev_t dev, ino_t ino ) |
| 489 | { |
| 490 | struct inode *inode; |
| 491 | |
| 492 | inode = malloc( sizeof *inode ); |
| 493 | if (inode) |
| 494 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 495 | list_init( &inode->children ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 496 | list_init( &inode->dirs ); |
| 497 | inode->ino = ino; |
| 498 | inode->dev = dev; |
| 499 | inode->wd = -1; |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 500 | inode->parent = NULL; |
| 501 | inode->name = NULL; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 502 | list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry ); |
| 503 | } |
| 504 | return inode; |
| 505 | } |
| 506 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 507 | static struct inode *get_inode( dev_t dev, ino_t ino ) |
| 508 | { |
| 509 | struct inode *inode; |
| 510 | |
| 511 | inode = find_inode( dev, ino ); |
| 512 | if (inode) |
| 513 | return inode; |
| 514 | return create_inode( dev, ino ); |
| 515 | } |
| 516 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 517 | static void inode_set_wd( struct inode *inode, int wd ) |
| 518 | { |
| 519 | if (inode->wd != -1) |
| 520 | list_remove( &inode->wd_entry ); |
| 521 | inode->wd = wd; |
| 522 | list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry ); |
| 523 | } |
| 524 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 525 | static void inode_set_name( struct inode *inode, const char *name ) |
| 526 | { |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 527 | free (inode->name); |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 528 | inode->name = name ? strdup( name ) : NULL; |
| 529 | } |
| 530 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 531 | static void free_inode( struct inode *inode ) |
| 532 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 533 | int subtree = 0, watches = 0; |
Alexandre Julliard | 037afca | 2008-02-28 10:47:14 +0100 | [diff] [blame] | 534 | struct inode *tmp, *next; |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 535 | struct dir *dir; |
| 536 | |
| 537 | LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry ) |
| 538 | { |
| 539 | subtree |= dir->subtree; |
| 540 | watches++; |
| 541 | } |
| 542 | |
| 543 | if (!subtree && !inode->parent) |
| 544 | { |
Mike McCormack | 309a9bf | 2006-02-22 23:11:17 +0900 | [diff] [blame] | 545 | LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, |
| 546 | struct inode, ch_entry ) |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 547 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 548 | assert( tmp != inode ); |
| 549 | assert( tmp->parent == inode ); |
| 550 | free_inode( tmp ); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (watches) |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 555 | return; |
| 556 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 557 | if (inode->parent) |
| 558 | list_remove( &inode->ch_entry ); |
| 559 | |
Alexandre Julliard | 037afca | 2008-02-28 10:47:14 +0100 | [diff] [blame] | 560 | /* disconnect remaining children from the parent */ |
| 561 | LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry ) |
| 562 | { |
| 563 | list_remove( &tmp->ch_entry ); |
| 564 | tmp->parent = NULL; |
| 565 | } |
| 566 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 567 | if (inode->wd != -1) |
| 568 | { |
Alexandre Julliard | 61a7036 | 2009-06-26 15:45:03 +0200 | [diff] [blame] | 569 | inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 570 | list_remove( &inode->wd_entry ); |
| 571 | } |
| 572 | list_remove( &inode->ino_entry ); |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 573 | |
| 574 | free( inode->name ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 575 | free( inode ); |
| 576 | } |
| 577 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 578 | static struct inode *inode_add( struct inode *parent, |
| 579 | dev_t dev, ino_t ino, const char *name ) |
| 580 | { |
| 581 | struct inode *inode; |
| 582 | |
| 583 | inode = get_inode( dev, ino ); |
| 584 | if (!inode) |
| 585 | return NULL; |
| 586 | |
| 587 | if (!inode->parent) |
| 588 | { |
| 589 | list_add_tail( &parent->children, &inode->ch_entry ); |
| 590 | inode->parent = parent; |
| 591 | assert( inode != parent ); |
| 592 | } |
| 593 | inode_set_name( inode, name ); |
| 594 | |
| 595 | return inode; |
| 596 | } |
| 597 | |
| 598 | static struct inode *inode_from_name( struct inode *inode, const char *name ) |
| 599 | { |
| 600 | struct inode *i; |
| 601 | |
| 602 | LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry ) |
| 603 | if (i->name && !strcmp( i->name, name )) |
| 604 | return i; |
| 605 | return NULL; |
| 606 | } |
| 607 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 608 | static int inotify_get_poll_events( struct fd *fd ); |
| 609 | static void inotify_poll_event( struct fd *fd, int event ); |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 610 | |
| 611 | static const struct fd_ops inotify_fd_ops = |
| 612 | { |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 613 | inotify_get_poll_events, /* get_poll_events */ |
| 614 | inotify_poll_event, /* poll_event */ |
Alexandre Julliard | 3f05759 | 2007-04-12 20:21:53 +0200 | [diff] [blame] | 615 | NULL, /* flush */ |
| 616 | NULL, /* get_fd_type */ |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 617 | NULL, /* ioctl */ |
Alexandre Julliard | 3f05759 | 2007-04-12 20:21:53 +0200 | [diff] [blame] | 618 | NULL, /* queue_async */ |
| 619 | NULL, /* reselect_async */ |
| 620 | NULL, /* cancel_async */ |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | static int inotify_get_poll_events( struct fd *fd ) |
| 624 | { |
| 625 | return POLLIN; |
| 626 | } |
| 627 | |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 628 | static void inotify_do_change_notify( struct dir *dir, unsigned int action, |
| 629 | const char *relpath ) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 630 | { |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 631 | struct change_record *record; |
| 632 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 633 | assert( dir->obj.ops == &dir_ops ); |
| 634 | |
Mike McCormack | 0790f95 | 2006-02-07 16:50:36 +0100 | [diff] [blame] | 635 | if (dir->want_data) |
| 636 | { |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 637 | size_t len = strlen(relpath); |
Alexandre Julliard | e979832 | 2006-02-08 15:06:42 +0100 | [diff] [blame] | 638 | record = malloc( offsetof(struct change_record, name[len]) ); |
Mike McCormack | 0790f95 | 2006-02-07 16:50:36 +0100 | [diff] [blame] | 639 | if (!record) |
| 640 | return; |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 641 | |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 642 | record->action = action; |
| 643 | memcpy( record->name, relpath, len ); |
Alexandre Julliard | e979832 | 2006-02-08 15:06:42 +0100 | [diff] [blame] | 644 | record->len = len; |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 645 | |
Mike McCormack | 0790f95 | 2006-02-07 16:50:36 +0100 | [diff] [blame] | 646 | list_add_tail( &dir->change_records, &record->entry ); |
| 647 | } |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 648 | |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 649 | fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED ); |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 652 | static unsigned int filter_from_event( struct inotify_event *ie ) |
| 653 | { |
| 654 | unsigned int filter = 0; |
| 655 | |
| 656 | if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE)) |
| 657 | filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME; |
| 658 | if (ie->mask & IN_MODIFY) |
| 659 | filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE; |
| 660 | if (ie->mask & IN_ATTRIB) |
| 661 | filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY; |
| 662 | if (ie->mask & IN_ACCESS) |
| 663 | filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS; |
| 664 | if (ie->mask & IN_CREATE) |
| 665 | filter |= FILE_NOTIFY_CHANGE_CREATION; |
| 666 | |
| 667 | return filter; |
| 668 | } |
| 669 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 670 | /* scan up the parent directories for watches */ |
| 671 | static unsigned int filter_from_inode( struct inode *inode, int is_parent ) |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 672 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 673 | unsigned int filter = 0; |
| 674 | struct dir *dir; |
| 675 | |
| 676 | /* combine filters from parents watching subtrees */ |
| 677 | while (inode) |
| 678 | { |
| 679 | LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry ) |
| 680 | if (dir->subtree || !is_parent) |
| 681 | filter |= dir->filter; |
| 682 | is_parent = 1; |
| 683 | inode = inode->parent; |
| 684 | } |
| 685 | |
| 686 | return filter; |
| 687 | } |
| 688 | |
| 689 | static char *inode_get_path( struct inode *inode, int sz ) |
| 690 | { |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 691 | struct list *head; |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 692 | char *path; |
| 693 | int len; |
| 694 | |
| 695 | if (!inode) |
| 696 | return NULL; |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 697 | |
| 698 | head = list_head( &inode->dirs ); |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 699 | if (head) |
| 700 | { |
| 701 | int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd ); |
| 702 | path = malloc ( 32 + sz ); |
| 703 | if (path) |
| 704 | sprintf( path, "/proc/self/fd/%u/", unix_fd ); |
| 705 | return path; |
| 706 | } |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 707 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 708 | if (!inode->name) |
| 709 | return NULL; |
| 710 | |
| 711 | len = strlen( inode->name ); |
| 712 | path = inode_get_path( inode->parent, sz + len + 1 ); |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 713 | if (!path) |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 714 | return NULL; |
| 715 | |
| 716 | strcat( path, inode->name ); |
| 717 | strcat( path, "/" ); |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 718 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 719 | return path; |
| 720 | } |
| 721 | |
| 722 | static int inode_check_dir( struct inode *parent, const char *name ) |
| 723 | { |
| 724 | char *path; |
| 725 | unsigned int filter; |
| 726 | struct inode *inode; |
| 727 | struct stat st; |
| 728 | int wd = -1, r = -1; |
| 729 | |
| 730 | path = inode_get_path( parent, strlen(name) ); |
| 731 | if (!path) |
| 732 | return r; |
| 733 | |
| 734 | strcat( path, name ); |
| 735 | |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 736 | r = stat( path, &st ); |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 737 | if (r < 0) goto end; |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 738 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 739 | if (!S_ISDIR(st.st_mode)) |
| 740 | { |
| 741 | r = 0; |
| 742 | goto end; |
| 743 | } |
| 744 | |
| 745 | r = 1; |
| 746 | |
| 747 | filter = filter_from_inode( parent, 1 ); |
| 748 | if (!filter) |
| 749 | goto end; |
| 750 | |
| 751 | inode = inode_add( parent, st.st_dev, st.st_ino, name ); |
| 752 | if (!inode || inode->wd != -1) |
| 753 | goto end; |
| 754 | |
| 755 | wd = inotify_add_dir( path, filter ); |
| 756 | if (wd != -1) |
| 757 | inode_set_wd( inode, wd ); |
| 758 | else |
| 759 | free_inode( inode ); |
| 760 | |
| 761 | end: |
| 762 | free( path ); |
| 763 | return r; |
| 764 | } |
| 765 | |
| 766 | static int prepend( char **path, const char *segment ) |
| 767 | { |
| 768 | int extra; |
| 769 | char *p; |
| 770 | |
| 771 | extra = strlen( segment ) + 1; |
| 772 | if (*path) |
| 773 | { |
| 774 | int len = strlen( *path ) + 1; |
| 775 | p = realloc( *path, len + extra ); |
| 776 | if (!p) return 0; |
| 777 | memmove( &p[ extra ], p, len ); |
| 778 | p[ extra - 1 ] = '/'; |
| 779 | memcpy( p, segment, extra - 1 ); |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | p = malloc( extra ); |
| 784 | if (!p) return 0; |
| 785 | memcpy( p, segment, extra ); |
| 786 | } |
| 787 | |
| 788 | *path = p; |
| 789 | |
| 790 | return 1; |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 793 | static void inotify_notify_all( struct inotify_event *ie ) |
| 794 | { |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 795 | unsigned int filter, action; |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 796 | struct inode *inode, *i; |
| 797 | char *path = NULL; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 798 | struct dir *dir; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 799 | |
| 800 | inode = inode_from_wd( ie->wd ); |
| 801 | if (!inode) |
| 802 | { |
| 803 | fprintf( stderr, "no inode matches %d\n", ie->wd); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | filter = filter_from_event( ie ); |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 808 | |
| 809 | if (ie->mask & IN_CREATE) |
| 810 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 811 | switch (inode_check_dir( inode, ie->name )) |
Mike McCormack | a2813f7 | 2006-02-20 12:28:46 +0100 | [diff] [blame] | 812 | { |
| 813 | case 1: |
| 814 | filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME; |
| 815 | break; |
| 816 | case 0: |
| 817 | filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME; |
| 818 | break; |
| 819 | default: |
| 820 | break; |
| 821 | /* Maybe the file disappeared before we could check it? */ |
| 822 | } |
| 823 | action = FILE_ACTION_ADDED; |
| 824 | } |
| 825 | else if (ie->mask & IN_DELETE) |
| 826 | action = FILE_ACTION_REMOVED; |
| 827 | else |
| 828 | action = FILE_ACTION_MODIFIED; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 829 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 830 | /* |
Francois Gouget | 4aa6418 | 2006-02-24 16:11:36 +0100 | [diff] [blame] | 831 | * Work our way up the inode hierarchy |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 832 | * extending the relative path as we go |
Francois Gouget | 4aa6418 | 2006-02-24 16:11:36 +0100 | [diff] [blame] | 833 | * and notifying all recursive watches. |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 834 | */ |
| 835 | if (!prepend( &path, ie->name )) |
| 836 | return; |
| 837 | |
| 838 | for (i = inode; i; i = i->parent) |
| 839 | { |
| 840 | LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry ) |
| 841 | if ((filter & dir->filter) && (i==inode || dir->subtree)) |
| 842 | inotify_do_change_notify( dir, action, path ); |
| 843 | |
| 844 | if (!i->name || !prepend( &path, i->name )) |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | free( path ); |
| 849 | |
| 850 | if (ie->mask & IN_DELETE) |
| 851 | { |
| 852 | i = inode_from_name( inode, ie->name ); |
| 853 | if (i) |
| 854 | free_inode( i ); |
| 855 | } |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 858 | static void inotify_poll_event( struct fd *fd, int event ) |
| 859 | { |
| 860 | int r, ofs, unix_fd; |
| 861 | char buffer[0x1000]; |
| 862 | struct inotify_event *ie; |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 863 | |
| 864 | unix_fd = get_unix_fd( fd ); |
| 865 | r = read( unix_fd, buffer, sizeof buffer ); |
| 866 | if (r < 0) |
| 867 | { |
| 868 | fprintf(stderr,"inotify_poll_event(): inotify read failed!\n"); |
| 869 | return; |
| 870 | } |
| 871 | |
Alexandre Julliard | e979832 | 2006-02-08 15:06:42 +0100 | [diff] [blame] | 872 | for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); ) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 873 | { |
| 874 | ie = (struct inotify_event*) &buffer[ofs]; |
| 875 | if (!ie->len) |
| 876 | break; |
Alexandre Julliard | e979832 | 2006-02-08 15:06:42 +0100 | [diff] [blame] | 877 | ofs += offsetof( struct inotify_event, name[ie->len] ); |
| 878 | if (ofs > r) break; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 879 | inotify_notify_all( ie ); |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 883 | static inline struct fd *create_inotify_fd( void ) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 884 | { |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 885 | int unix_fd; |
| 886 | |
| 887 | unix_fd = inotify_init(); |
| 888 | if (unix_fd<0) |
| 889 | return NULL; |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 890 | return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 ); |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 893 | static int map_flags( unsigned int filter ) |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 894 | { |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 895 | unsigned int mask; |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 896 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 897 | /* always watch these so we can track subdirectories in recursive watches */ |
| 898 | mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF); |
| 899 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 900 | if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) |
| 901 | mask |= IN_ATTRIB; |
| 902 | if (filter & FILE_NOTIFY_CHANGE_SIZE) |
| 903 | mask |= IN_MODIFY; |
| 904 | if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE) |
| 905 | mask |= IN_MODIFY; |
| 906 | if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS) |
| 907 | mask |= IN_ACCESS; |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 908 | if (filter & FILE_NOTIFY_CHANGE_SECURITY) |
| 909 | mask |= IN_ATTRIB; |
| 910 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 911 | return mask; |
| 912 | } |
| 913 | |
| 914 | static int inotify_add_dir( char *path, unsigned int filter ) |
| 915 | { |
| 916 | int wd = inotify_add_watch( get_unix_fd( inotify_fd ), |
| 917 | path, map_flags( filter ) ); |
| 918 | if (wd != -1) |
| 919 | set_fd_events( inotify_fd, POLLIN ); |
| 920 | return wd; |
| 921 | } |
| 922 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 923 | static int init_inotify( void ) |
| 924 | { |
| 925 | int i; |
| 926 | |
| 927 | if (inotify_fd) |
| 928 | return 1; |
| 929 | |
| 930 | inotify_fd = create_inotify_fd(); |
| 931 | if (!inotify_fd) |
| 932 | return 0; |
| 933 | |
| 934 | for (i=0; i<HASH_SIZE; i++) |
| 935 | { |
| 936 | list_init( &inode_hash[i] ); |
| 937 | list_init( &wd_hash[i] ); |
| 938 | } |
| 939 | |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 940 | return 1; |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 943 | static int inotify_adjust_changes( struct dir *dir ) |
| 944 | { |
| 945 | unsigned int filter; |
| 946 | struct inode *inode; |
| 947 | struct stat st; |
| 948 | char path[32]; |
| 949 | int wd, unix_fd; |
| 950 | |
| 951 | if (!inotify_fd) |
| 952 | return 0; |
| 953 | |
| 954 | unix_fd = get_unix_fd( dir->fd ); |
| 955 | |
| 956 | inode = dir->inode; |
| 957 | if (!inode) |
| 958 | { |
| 959 | /* check if this fd is already being watched */ |
| 960 | if (-1 == fstat( unix_fd, &st )) |
| 961 | return 0; |
| 962 | |
| 963 | inode = get_inode( st.st_dev, st.st_ino ); |
| 964 | if (!inode) |
| 965 | inode = create_inode( st.st_dev, st.st_ino ); |
| 966 | if (!inode) |
| 967 | return 0; |
| 968 | list_add_tail( &inode->dirs, &dir->in_entry ); |
| 969 | dir->inode = inode; |
| 970 | } |
| 971 | |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 972 | filter = filter_from_inode( inode, 0 ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 973 | |
| 974 | sprintf( path, "/proc/self/fd/%u", unix_fd ); |
| 975 | wd = inotify_add_dir( path, filter ); |
| 976 | if (wd == -1) return 0; |
| 977 | |
| 978 | inode_set_wd( inode, wd ); |
| 979 | |
| 980 | return 1; |
| 981 | } |
| 982 | |
Mike McCormack | 4212108 | 2006-02-23 00:45:34 +0900 | [diff] [blame] | 983 | static char *get_basename( const char *link ) |
| 984 | { |
| 985 | char *buffer, *name = NULL; |
| 986 | int r, n = 0x100; |
| 987 | |
| 988 | while (1) |
| 989 | { |
| 990 | buffer = malloc( n ); |
Alexandre Julliard | c9cc7e3 | 2006-03-02 18:03:32 +0100 | [diff] [blame] | 991 | if (!buffer) return NULL; |
Mike McCormack | 4212108 | 2006-02-23 00:45:34 +0900 | [diff] [blame] | 992 | |
| 993 | r = readlink( link, buffer, n ); |
| 994 | if (r < 0) |
| 995 | break; |
| 996 | |
| 997 | if (r < n) |
| 998 | { |
| 999 | name = buffer; |
| 1000 | break; |
| 1001 | } |
| 1002 | free( buffer ); |
| 1003 | n *= 2; |
| 1004 | } |
| 1005 | |
| 1006 | if (name) |
| 1007 | { |
| 1008 | while (r > 0 && name[ r - 1 ] == '/' ) |
| 1009 | r--; |
| 1010 | name[ r ] = 0; |
| 1011 | |
| 1012 | name = strrchr( name, '/' ); |
| 1013 | if (name) |
| 1014 | name = strdup( &name[1] ); |
| 1015 | } |
| 1016 | |
| 1017 | free( buffer ); |
| 1018 | return name; |
| 1019 | } |
| 1020 | |
| 1021 | static int dir_add_to_existing_notify( struct dir *dir ) |
| 1022 | { |
| 1023 | struct inode *inode, *parent; |
| 1024 | unsigned int filter = 0; |
| 1025 | struct stat st, st_new; |
| 1026 | char link[35], *name; |
| 1027 | int wd, unix_fd; |
| 1028 | |
| 1029 | if (!inotify_fd) |
| 1030 | return 0; |
| 1031 | |
| 1032 | unix_fd = get_unix_fd( dir->fd ); |
| 1033 | |
| 1034 | /* check if it's in the list of inodes we want to watch */ |
| 1035 | if (-1 == fstat( unix_fd, &st_new )) |
| 1036 | return 0; |
| 1037 | inode = find_inode( st_new.st_dev, st_new.st_ino ); |
| 1038 | if (inode) |
| 1039 | return 0; |
| 1040 | |
| 1041 | /* lookup the parent */ |
| 1042 | sprintf( link, "/proc/self/fd/%u/..", unix_fd ); |
| 1043 | if (-1 == stat( link, &st )) |
| 1044 | return 0; |
| 1045 | |
| 1046 | /* |
| 1047 | * If there's no parent, stop. We could keep going adding |
| 1048 | * ../ to the path until we hit the root of the tree or |
| 1049 | * find a recursively watched ancestor. |
| 1050 | * Assume it's too expensive to search up the tree for now. |
| 1051 | */ |
| 1052 | parent = find_inode( st.st_dev, st.st_ino ); |
| 1053 | if (!parent) |
| 1054 | return 0; |
| 1055 | |
| 1056 | if (parent->wd == -1) |
| 1057 | return 0; |
| 1058 | |
| 1059 | filter = filter_from_inode( parent, 1 ); |
| 1060 | if (!filter) |
| 1061 | return 0; |
| 1062 | |
| 1063 | sprintf( link, "/proc/self/fd/%u", unix_fd ); |
| 1064 | name = get_basename( link ); |
| 1065 | if (!name) |
| 1066 | return 0; |
| 1067 | inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name ); |
| 1068 | free( name ); |
| 1069 | if (!inode) |
| 1070 | return 0; |
| 1071 | |
| 1072 | /* Couldn't find this inode at the start of the function, must be new */ |
| 1073 | assert( inode->wd == -1 ); |
| 1074 | |
| 1075 | wd = inotify_add_dir( link, filter ); |
| 1076 | if (wd != -1) |
| 1077 | inode_set_wd( inode, wd ); |
| 1078 | |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1082 | #else |
| 1083 | |
| 1084 | static int init_inotify( void ) |
| 1085 | { |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int inotify_adjust_changes( struct dir *dir ) |
| 1090 | { |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static void free_inode( struct inode *inode ) |
| 1095 | { |
| 1096 | assert( 0 ); |
| 1097 | } |
| 1098 | |
Mike McCormack | 4212108 | 2006-02-23 00:45:34 +0900 | [diff] [blame] | 1099 | static int dir_add_to_existing_notify( struct dir *dir ) |
| 1100 | { |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 1104 | #endif /* USE_INOTIFY */ |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 1105 | |
Alexandre Julliard | 74a6302 | 2009-08-17 17:18:54 +0200 | [diff] [blame] | 1106 | struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode ) |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1107 | { |
| 1108 | struct dir *dir; |
| 1109 | |
| 1110 | dir = alloc_object( &dir_ops ); |
| 1111 | if (!dir) |
| 1112 | return NULL; |
| 1113 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1114 | list_init( &dir->change_records ); |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1115 | dir->filter = 0; |
| 1116 | dir->notified = 0; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1117 | dir->want_data = 0; |
| 1118 | dir->inode = NULL; |
| 1119 | grab_object( fd ); |
| 1120 | dir->fd = fd; |
Alexandre Julliard | 74a6302 | 2009-08-17 17:18:54 +0200 | [diff] [blame] | 1121 | dir->mode = mode; |
| 1122 | dir->uid = ~(uid_t)0; |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1123 | set_fd_user( fd, &dir_fd_ops, &dir->obj ); |
| 1124 | |
Mike McCormack | 4212108 | 2006-02-23 00:45:34 +0900 | [diff] [blame] | 1125 | dir_add_to_existing_notify( dir ); |
| 1126 | |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1127 | return &dir->obj; |
| 1128 | } |
| 1129 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1130 | /* enable change notifications for a directory */ |
| 1131 | DECL_HANDLER(read_directory_changes) |
| 1132 | { |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1133 | struct dir *dir; |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1134 | struct async *async; |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1135 | |
| 1136 | if (!req->filter) |
| 1137 | { |
| 1138 | set_error(STATUS_INVALID_PARAMETER); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Alexandre Julliard | a7b3efd | 2008-12-26 12:17:20 +0100 | [diff] [blame] | 1142 | dir = get_dir_obj( current->process, req->async.handle, 0 ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1143 | if (!dir) |
| 1144 | return; |
| 1145 | |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 1146 | /* requests don't timeout */ |
Alexandre Julliard | 9ed42d2 | 2008-12-26 12:27:48 +0100 | [diff] [blame] | 1147 | if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end; |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 1148 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1149 | /* assign it once */ |
| 1150 | if (!dir->filter) |
| 1151 | { |
Mike McCormack | fbc00db | 2006-02-17 11:40:38 +0100 | [diff] [blame] | 1152 | init_inotify(); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1153 | insert_change( dir ); |
| 1154 | dir->filter = req->filter; |
Mike McCormack | e4faabf | 2006-02-21 16:58:19 +0900 | [diff] [blame] | 1155 | dir->subtree = req->subtree; |
Mike McCormack | 0790f95 | 2006-02-07 16:50:36 +0100 | [diff] [blame] | 1156 | dir->want_data = req->want_data; |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
Mike McCormack | f5c1381 | 2006-03-28 17:58:07 +0900 | [diff] [blame] | 1159 | /* if there's already a change in the queue, send it */ |
Alexandre Julliard | d99ee34 | 2007-04-02 12:49:45 +0200 | [diff] [blame] | 1160 | if (!list_empty( &dir->change_records )) |
Alexandre Julliard | df09ac5 | 2007-04-02 20:09:29 +0200 | [diff] [blame] | 1161 | fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED ); |
Mike McCormack | f5c1381 | 2006-03-28 17:58:07 +0900 | [diff] [blame] | 1162 | |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 1163 | /* setup the real notification */ |
Alexandre Julliard | cf9ced5 | 2006-02-05 12:19:56 +0100 | [diff] [blame] | 1164 | if (!inotify_adjust_changes( dir )) |
Mike McCormack | fdf0c68 | 2006-01-30 18:15:31 +0100 | [diff] [blame] | 1165 | dnotify_adjust_changes( dir ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1166 | |
Alexandre Julliard | 0aae1ca | 2007-04-02 20:41:59 +0200 | [diff] [blame] | 1167 | release_object( async ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 1168 | set_error(STATUS_PENDING); |
| 1169 | |
| 1170 | end: |
| 1171 | release_object( dir ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1172 | } |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 1173 | |
| 1174 | DECL_HANDLER(read_change) |
| 1175 | { |
| 1176 | struct change_record *record; |
| 1177 | struct dir *dir; |
| 1178 | |
| 1179 | dir = get_dir_obj( current->process, req->handle, 0 ); |
| 1180 | if (!dir) |
| 1181 | return; |
| 1182 | |
| 1183 | if ((record = get_first_change_record( dir )) != NULL) |
| 1184 | { |
| 1185 | reply->action = record->action; |
| 1186 | set_reply_data( record->name, record->len ); |
| 1187 | free( record ); |
| 1188 | } |
| 1189 | else |
| 1190 | set_error( STATUS_NO_DATA_DETECTED ); |
| 1191 | |
Mike McCormack | 0193211 | 2006-02-06 11:58:55 +0100 | [diff] [blame] | 1192 | release_object( dir ); |
| 1193 | } |