oleaut: Dump the pointer for VT_ARRAY/VT_VECTOR typed variants.
[wine/wine64.git] / server / change.c
blobef437aaef924f7f446b0febcf22d9d327a466e10
1 /*
2 * Server-side change notification management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2006 Mike McCormack
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.
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.
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
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
34 #endif
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "windef.h"
40 #include "file.h"
41 #include "handle.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "winternl.h"
46 /* dnotify support */
48 #ifdef linux
49 #ifndef F_NOTIFY
50 #define F_NOTIFY 1026
51 #define DN_ACCESS 0x00000001 /* File accessed */
52 #define DN_MODIFY 0x00000002 /* File modified */
53 #define DN_CREATE 0x00000004 /* File created */
54 #define DN_DELETE 0x00000008 /* File removed */
55 #define DN_RENAME 0x00000010 /* File renamed */
56 #define DN_ATTRIB 0x00000020 /* File changed attibutes */
57 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
58 #endif
59 #endif
61 /* inotify support */
63 #if defined(__linux__) && defined(__i386__)
65 #define SYS_inotify_init 291
66 #define SYS_inotify_add_watch 292
67 #define SYS_inotify_rm_watch 293
69 struct inotify_event {
70 int wd;
71 unsigned int mask;
72 unsigned int cookie;
73 unsigned int len;
74 char name[1];
77 #define IN_ACCESS 0x00000001
78 #define IN_MODIFY 0x00000002
79 #define IN_ATTRIB 0x00000004
80 #define IN_CLOSE_WRITE 0x00000008
81 #define IN_CLOSE_NOWRITE 0x00000010
82 #define IN_OPEN 0x00000020
83 #define IN_MOVED_FROM 0x00000040
84 #define IN_MOVED_TO 0x00000080
85 #define IN_CREATE 0x00000100
86 #define IN_DELETE 0x00000200
87 #define IN_DELETE_SELF 0x00000400
89 static inline int inotify_init( void )
91 int ret;
92 __asm__ __volatile__( "int $0x80"
93 : "=a" (ret)
94 : "0" (SYS_inotify_init));
95 if (ret<0) { errno = -ret; ret = -1; }
96 return ret;
99 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
101 int ret;
102 __asm__ __volatile__( "pushl %%ebx;\n\t"
103 "movl %2,%%ebx;\n\t"
104 "int $0x80;\n\t"
105 "popl %%ebx"
106 : "=a" (ret) : "0" (SYS_inotify_add_watch),
107 "r" (fd), "c" (name), "d" (mask) );
108 if (ret<0) { errno = -ret; ret = -1; }
109 return ret;
112 static inline int inotify_remove_watch( int fd, int wd )
114 int ret;
115 __asm__ __volatile__( "pushl %%ebx;\n\t"
116 "movl %2,%%ebx;\n\t"
117 "int $0x80;\n\t"
118 "popl %%ebx"
119 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
120 "r" (fd), "c" (wd) );
121 if (ret<0) { errno = -ret; ret = -1; }
122 return ret;
125 #else
127 static inline int inotify_init( void )
129 errno = ENOSYS;
130 return -1;
133 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
135 errno = ENOSYS;
136 return -1;
139 static inline int inotify_remove_watch( int fd, int wd )
141 errno = ENOSYS;
142 return -1;
145 #endif
147 struct dir
149 struct object obj; /* object header */
150 struct fd *fd; /* file descriptor to the directory */
151 struct list entry; /* entry in global change notifications list */
152 struct event *event;
153 unsigned int filter; /* notification filter */
154 int notified; /* SIGIO counter */
155 long signaled; /* the file changed */
156 struct fd *inotify_fd; /* inotify file descriptor */
157 int wd; /* inotify watch descriptor */
160 static struct fd *dir_get_fd( struct object *obj );
161 static unsigned int dir_map_access( struct object *obj, unsigned int access );
162 static void dir_dump( struct object *obj, int verbose );
163 static void dir_destroy( struct object *obj );
164 static int dir_signaled( struct object *obj, struct thread *thread );
166 static const struct object_ops dir_ops =
168 sizeof(struct dir), /* size */
169 dir_dump, /* dump */
170 add_queue, /* add_queue */
171 remove_queue, /* remove_queue */
172 dir_signaled, /* signaled */
173 no_satisfied, /* satisfied */
174 no_signal, /* signal */
175 dir_get_fd, /* get_fd */
176 dir_map_access, /* map_access */
177 no_lookup_name, /* lookup_name */
178 no_close_handle, /* close_handle */
179 dir_destroy /* destroy */
182 static int dir_get_poll_events( struct fd *fd );
183 static int dir_get_info( struct fd *fd );
185 static const struct fd_ops dir_fd_ops =
187 dir_get_poll_events, /* get_poll_events */
188 default_poll_event, /* poll_event */
189 no_flush, /* flush */
190 dir_get_info, /* get_file_info */
191 default_fd_queue_async, /* queue_async */
192 default_fd_cancel_async /* cancel_async */
195 static struct list change_list = LIST_INIT(change_list);
197 static void dnotify_adjust_changes( struct dir *dir )
199 #if defined(F_SETSIG) && defined(F_NOTIFY)
200 int fd = get_unix_fd( dir->fd );
201 unsigned int filter = dir->filter;
202 unsigned int val;
203 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
204 return;
206 val = DN_MULTISHOT;
207 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
208 val |= DN_RENAME | DN_DELETE | DN_CREATE;
209 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
210 val |= DN_RENAME | DN_DELETE | DN_CREATE;
211 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
212 val |= DN_ATTRIB;
213 if (filter & FILE_NOTIFY_CHANGE_SIZE)
214 val |= DN_MODIFY;
215 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
216 val |= DN_MODIFY;
217 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
218 val |= DN_ACCESS;
219 if (filter & FILE_NOTIFY_CHANGE_CREATION)
220 val |= DN_CREATE;
221 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
222 val |= DN_ATTRIB;
223 fcntl( fd, F_NOTIFY, val );
224 #endif
227 /* insert change in the global list */
228 static inline void insert_change( struct dir *dir )
230 sigset_t sigset;
232 sigemptyset( &sigset );
233 sigaddset( &sigset, SIGIO );
234 sigprocmask( SIG_BLOCK, &sigset, NULL );
235 list_add_head( &change_list, &dir->entry );
236 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
239 /* remove change from the global list */
240 static inline void remove_change( struct dir *dir )
242 sigset_t sigset;
244 sigemptyset( &sigset );
245 sigaddset( &sigset, SIGIO );
246 sigprocmask( SIG_BLOCK, &sigset, NULL );
247 list_remove( &dir->entry );
248 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
251 struct object *create_dir_obj( struct fd *fd )
253 struct dir *dir;
255 dir = alloc_object( &dir_ops );
256 if (!dir)
257 return NULL;
259 dir->event = NULL;
260 dir->filter = 0;
261 dir->notified = 0;
262 dir->signaled = 0;
263 dir->inotify_fd = NULL;
264 dir->wd = -1;
265 grab_object( fd );
266 dir->fd = fd;
267 set_fd_user( fd, &dir_fd_ops, &dir->obj );
269 return &dir->obj;
272 static void dir_dump( struct object *obj, int verbose )
274 struct dir *dir = (struct dir *)obj;
275 assert( obj->ops == &dir_ops );
276 fprintf( stderr, "Dirfile fd=%p event=%p filter=%08x\n",
277 dir->fd, dir->event, dir->filter );
280 static int dir_signaled( struct object *obj, struct thread *thread )
282 struct dir *dir = (struct dir *)obj;
283 assert (obj->ops == &dir_ops);
284 return (dir->event == NULL) && dir->signaled;
287 /* enter here directly from SIGIO signal handler */
288 void do_change_notify( int unix_fd )
290 struct dir *dir;
292 /* FIXME: this is O(n) ... probably can be improved */
293 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
295 if (get_unix_fd( dir->fd ) != unix_fd) continue;
296 interlocked_xchg_add( &dir->notified, 1 );
297 break;
301 static void dir_signal_changed( struct dir *dir )
303 if (dir->event)
304 set_event( dir->event );
305 else
306 wake_up( &dir->obj, 0 );
309 /* SIGIO callback, called synchronously with the poll loop */
310 void sigio_callback(void)
312 struct dir *dir;
314 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
316 long count = interlocked_xchg( &dir->notified, 0 );
317 if (count)
319 dir->signaled += count;
320 if (dir->signaled == count) /* was it 0? */
321 dir_signal_changed( dir );
326 static struct fd *dir_get_fd( struct object *obj )
328 struct dir *dir = (struct dir *)obj;
329 assert( obj->ops == &dir_ops );
330 return (struct fd *)grab_object( dir->fd );
333 static unsigned int dir_map_access( struct object *obj, unsigned int access )
335 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
336 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
337 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
338 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
339 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
342 static void dir_destroy( struct object *obj )
344 struct dir *dir = (struct dir *)obj;
345 assert (obj->ops == &dir_ops);
347 if (dir->filter)
348 remove_change( dir );
350 if (dir->inotify_fd)
352 if (dir->wd != -1)
353 inotify_remove_watch( get_unix_fd( dir->inotify_fd ), dir->wd );
354 release_object( dir->inotify_fd );
357 if (dir->event)
359 set_event( dir->event );
360 release_object( dir->event );
362 release_object( dir->fd );
365 static struct dir *
366 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
368 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
371 static int dir_get_poll_events( struct fd *fd )
373 return 0;
376 static int dir_get_info( struct fd *fd )
378 return 0;
382 static int inotify_get_poll_events( struct fd *fd );
383 static void inotify_poll_event( struct fd *fd, int event );
384 static int inotify_get_info( struct fd *fd );
386 static const struct fd_ops inotify_fd_ops =
388 inotify_get_poll_events, /* get_poll_events */
389 inotify_poll_event, /* poll_event */
390 no_flush, /* flush */
391 inotify_get_info, /* get_file_info */
392 default_fd_queue_async, /* queue_async */
393 default_fd_cancel_async, /* cancel_async */
396 static int inotify_get_poll_events( struct fd *fd )
398 return POLLIN;
401 static void inotify_do_change_notify( struct dir *dir )
403 dir->signaled++;
404 dir_signal_changed( dir );
407 static void inotify_poll_event( struct fd *fd, int event )
409 int r, ofs, unix_fd;
410 char buffer[0x1000];
411 struct inotify_event *ie;
412 struct dir *dir = get_fd_user( fd );
414 unix_fd = get_unix_fd( fd );
415 r = read( unix_fd, buffer, sizeof buffer );
416 if (r < 0)
418 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
419 return;
422 for( ofs = 0; ofs < r; )
424 ie = (struct inotify_event*) &buffer[ofs];
425 if (!ie->len)
426 break;
427 inotify_do_change_notify( dir );
428 ofs += (sizeof (*ie) + ie->len - 1);
432 static int inotify_get_info( struct fd *fd )
434 return 0;
437 static void inotify_adjust_changes( struct dir *dir )
439 int filter = dir->filter;
440 unsigned int mask = 0;
441 char link[32];
443 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
444 mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE);
445 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
446 mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
447 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
448 mask |= IN_ATTRIB;
449 if (filter & FILE_NOTIFY_CHANGE_SIZE)
450 mask |= IN_MODIFY;
451 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
452 mask |= IN_MODIFY;
453 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
454 mask |= IN_ACCESS;
455 if (filter & FILE_NOTIFY_CHANGE_CREATION)
456 mask |= IN_CREATE;
457 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
458 mask |= IN_ATTRIB;
460 sprintf( link, "/proc/self/fd/%u", get_unix_fd( dir->fd ) );
461 dir->wd = inotify_add_watch( get_unix_fd( dir->inotify_fd ), link, mask );
462 if (dir->wd != -1)
463 set_fd_events( dir->inotify_fd, POLLIN );
466 static struct fd *create_inotify_fd( struct dir *dir )
468 int unix_fd;
470 unix_fd = inotify_init();
471 if (unix_fd<0)
472 return NULL;
473 return create_anonymous_fd( &inotify_fd_ops, unix_fd, &dir->obj );
476 /* enable change notifications for a directory */
477 DECL_HANDLER(read_directory_changes)
479 struct event *event = NULL;
480 struct dir *dir;
482 if (!req->filter)
484 set_error(STATUS_INVALID_PARAMETER);
485 return;
488 dir = get_dir_obj( current->process, req->handle, 0 );
489 if (!dir)
490 return;
492 /* possibly send changes through an event flag */
493 if (req->event)
495 event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
496 if (!event)
497 goto end;
500 /* discard the current data, and move onto the next event */
501 if (dir->event) release_object( dir->event );
502 dir->event = event;
504 /* assign it once */
505 if (!dir->filter)
507 insert_change( dir );
508 dir->filter = req->filter;
511 /* remove any notifications */
512 if (dir->signaled>0)
513 dir->signaled--;
515 if (!dir->inotify_fd)
516 dir->inotify_fd = create_inotify_fd( dir );
518 /* setup the real notification */
519 if (dir->inotify_fd)
520 inotify_adjust_changes( dir );
521 else
522 dnotify_adjust_changes( dir );
524 set_error(STATUS_PENDING);
526 end:
527 release_object( dir );