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
23 #include "wine/port.h"
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
37 #define WIN32_NO_STATUS
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 */
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 {
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 )
92 __asm__ __volatile__( "int $0x80"
94 : "0" (SYS_inotify_init));
95 if (ret<0) { errno = -ret; ret = -1; }
99 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
102 __asm__ __volatile__( "pushl %%ebx;\n\t"
106 : "=a" (ret) : "0" (SYS_inotify_add_watch),
107 "r" (fd), "c" (name), "d" (mask) );
108 if (ret<0) { errno = -ret; ret = -1; }
112 static inline int inotify_remove_watch( int fd, int wd )
115 __asm__ __volatile__( "pushl %%ebx;\n\t"
119 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
120 "r" (fd), "c" (wd) );
121 if (ret<0) { errno = -ret; ret = -1; }
129 struct change_record {
138 struct object obj; /* object header */
139 struct fd *fd; /* file descriptor to the directory */
140 struct list entry; /* entry in global change notifications list */
142 unsigned int filter; /* notification filter */
143 int notified; /* SIGIO counter */
144 long signaled; /* the file changed */
145 struct fd *inotify_fd; /* inotify file descriptor */
146 int wd; /* inotify watch descriptor */
147 struct list change_q; /* change readers */
148 struct list change_records; /* data for the change */
151 static struct fd *dir_get_fd( struct object *obj );
152 static unsigned int dir_map_access( struct object *obj, unsigned int access );
153 static void dir_dump( struct object *obj, int verbose );
154 static void dir_destroy( struct object *obj );
155 static int dir_signaled( struct object *obj, struct thread *thread );
157 static const struct object_ops dir_ops =
159 sizeof(struct dir), /* size */
161 add_queue, /* add_queue */
162 remove_queue, /* remove_queue */
163 dir_signaled, /* signaled */
164 no_satisfied, /* satisfied */
165 no_signal, /* signal */
166 dir_get_fd, /* get_fd */
167 dir_map_access, /* map_access */
168 no_lookup_name, /* lookup_name */
169 no_close_handle, /* close_handle */
170 dir_destroy /* destroy */
173 static int dir_get_poll_events( struct fd *fd );
174 static int dir_get_info( struct fd *fd );
175 static void dir_cancel_async( struct fd *fd );
177 static const struct fd_ops dir_fd_ops =
179 dir_get_poll_events, /* get_poll_events */
180 default_poll_event, /* poll_event */
181 no_flush, /* flush */
182 dir_get_info, /* get_file_info */
183 default_fd_queue_async, /* queue_async */
184 dir_cancel_async /* cancel_async */
187 static struct list change_list = LIST_INIT(change_list);
189 static void dnotify_adjust_changes( struct dir *dir )
191 #if defined(F_SETSIG) && defined(F_NOTIFY)
192 int fd = get_unix_fd( dir->fd );
193 unsigned int filter = dir->filter;
195 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
199 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
200 val |= DN_RENAME | DN_DELETE | DN_CREATE;
201 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
202 val |= DN_RENAME | DN_DELETE | DN_CREATE;
203 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
205 if (filter & FILE_NOTIFY_CHANGE_SIZE)
207 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
209 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
211 if (filter & FILE_NOTIFY_CHANGE_CREATION)
213 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
215 fcntl( fd, F_NOTIFY, val );
219 /* insert change in the global list */
220 static inline void insert_change( struct dir *dir )
224 sigemptyset( &sigset );
225 sigaddset( &sigset, SIGIO );
226 sigprocmask( SIG_BLOCK, &sigset, NULL );
227 list_add_head( &change_list, &dir->entry );
228 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
231 /* remove change from the global list */
232 static inline void remove_change( struct dir *dir )
236 sigemptyset( &sigset );
237 sigaddset( &sigset, SIGIO );
238 sigprocmask( SIG_BLOCK, &sigset, NULL );
239 list_remove( &dir->entry );
240 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
243 struct object *create_dir_obj( struct fd *fd )
247 dir = alloc_object( &dir_ops );
251 list_init( &dir->change_q );
252 list_init( &dir->change_records );
257 dir->inotify_fd = NULL;
261 set_fd_user( fd, &dir_fd_ops, &dir->obj );
266 static void dir_dump( struct object *obj, int verbose )
268 struct dir *dir = (struct dir *)obj;
269 assert( obj->ops == &dir_ops );
270 fprintf( stderr, "Dirfile fd=%p event=%p filter=%08x\n",
271 dir->fd, dir->event, dir->filter );
274 static int dir_signaled( struct object *obj, struct thread *thread )
276 struct dir *dir = (struct dir *)obj;
277 assert (obj->ops == &dir_ops);
278 return (dir->event == NULL) && dir->signaled;
281 /* enter here directly from SIGIO signal handler */
282 void do_change_notify( int unix_fd )
286 /* FIXME: this is O(n) ... probably can be improved */
287 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
289 if (get_unix_fd( dir->fd ) != unix_fd) continue;
290 interlocked_xchg_add( &dir->notified, 1 );
295 static void dir_signal_changed( struct dir *dir )
298 set_event( dir->event );
300 wake_up( &dir->obj, 0 );
303 /* SIGIO callback, called synchronously with the poll loop */
304 void sigio_callback(void)
308 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
310 long count = interlocked_xchg( &dir->notified, 0 );
313 dir->signaled += count;
314 if (dir->signaled == count) /* was it 0? */
315 dir_signal_changed( dir );
320 static struct fd *dir_get_fd( struct object *obj )
322 struct dir *dir = (struct dir *)obj;
323 assert( obj->ops == &dir_ops );
324 return (struct fd *)grab_object( dir->fd );
327 static unsigned int dir_map_access( struct object *obj, unsigned int access )
329 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
330 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
331 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
332 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
333 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
336 static struct change_record *get_first_change_record( struct dir *dir )
338 struct list *ptr = list_head( &dir->change_records );
339 if (!ptr) return NULL;
341 return LIST_ENTRY( ptr, struct change_record, entry );
344 static void dir_destroy( struct object *obj )
346 struct change_record *record;
347 struct dir *dir = (struct dir *)obj;
348 assert (obj->ops == &dir_ops);
351 remove_change( dir );
354 release_object( dir->inotify_fd );
356 async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
357 while ((record = get_first_change_record( dir ))) free( record );
361 set_event( dir->event );
362 release_object( dir->event );
364 release_object( dir->fd );
368 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
370 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
373 static int dir_get_poll_events( struct fd *fd )
378 static int dir_get_info( struct fd *fd )
383 static void dir_cancel_async( struct fd *fd )
385 struct dir *dir = (struct dir *) get_fd_user( fd );
386 async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
392 static int inotify_get_poll_events( struct fd *fd );
393 static void inotify_poll_event( struct fd *fd, int event );
394 static int inotify_get_info( struct fd *fd );
396 static const struct fd_ops inotify_fd_ops =
398 inotify_get_poll_events, /* get_poll_events */
399 inotify_poll_event, /* poll_event */
400 no_flush, /* flush */
401 inotify_get_info, /* get_file_info */
402 default_fd_queue_async, /* queue_async */
403 default_fd_cancel_async, /* cancel_async */
406 static int inotify_get_poll_events( struct fd *fd )
411 static void inotify_do_change_notify( struct dir *dir, struct inotify_event *ie )
413 struct change_record *record;
415 record = malloc( sizeof (*record) + ie->len - 1 ) ;
419 if( ie->mask & IN_CREATE )
420 record->action = FILE_ACTION_ADDED;
421 else if( ie->mask & IN_DELETE )
422 record->action = FILE_ACTION_REMOVED;
424 record->action = FILE_ACTION_MODIFIED;
425 memcpy( record->name, ie->name, ie->len );
426 record->len = strlen( ie->name );
428 list_add_tail( &dir->change_records, &record->entry );
430 if (!list_empty( &dir->change_q ))
431 async_terminate_head( &dir->change_q, STATUS_ALERTED );
435 dir_signal_changed( dir );
439 static void inotify_poll_event( struct fd *fd, int event )
443 struct inotify_event *ie;
444 struct dir *dir = get_fd_user( fd );
446 unix_fd = get_unix_fd( fd );
447 r = read( unix_fd, buffer, sizeof buffer );
450 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
454 for( ofs = 0; ofs < r; )
456 ie = (struct inotify_event*) &buffer[ofs];
459 inotify_do_change_notify( dir, ie );
460 ofs += (sizeof (*ie) + ie->len - 1);
464 static int inotify_get_info( struct fd *fd )
469 static inline struct fd *create_inotify_fd( struct dir *dir )
473 unix_fd = inotify_init();
476 return create_anonymous_fd( &inotify_fd_ops, unix_fd, &dir->obj );
479 static int inotify_adjust_changes( struct dir *dir )
481 unsigned int filter = dir->filter;
482 unsigned int mask = 0;
485 if (!dir->inotify_fd)
487 if (!(dir->inotify_fd = create_inotify_fd( dir ))) return 0;
490 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
491 mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE);
492 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
493 mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
494 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
496 if (filter & FILE_NOTIFY_CHANGE_SIZE)
498 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
500 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
502 if (filter & FILE_NOTIFY_CHANGE_CREATION)
504 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
507 sprintf( link, "/proc/self/fd/%u", get_unix_fd( dir->fd ) );
508 dir->wd = inotify_add_watch( get_unix_fd( dir->inotify_fd ), link, mask );
510 set_fd_events( dir->inotify_fd, POLLIN );
514 #endif /* USE_INOTIFY */
516 /* enable change notifications for a directory */
517 DECL_HANDLER(read_directory_changes)
519 struct event *event = NULL;
524 set_error(STATUS_INVALID_PARAMETER);
528 dir = get_dir_obj( current->process, req->handle, 0 );
532 /* possibly send changes through an event flag */
535 event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
540 /* discard the current data, and move onto the next event */
541 if (dir->event) release_object( dir->event );
544 /* requests don't timeout */
545 if ( req->io_apc && !create_async( current, NULL, &dir->change_q,
546 req->io_apc, req->io_user, req->io_sb ))
552 insert_change( dir );
553 dir->filter = req->filter;
556 /* remove any notifications */
560 /* setup the real notification */
562 if (!inotify_adjust_changes( dir ))
564 dnotify_adjust_changes( dir );
566 set_error(STATUS_PENDING);
569 release_object( dir );
572 DECL_HANDLER(read_change)
574 struct change_record *record;
577 dir = get_dir_obj( current->process, req->handle, 0 );
581 if ((record = get_first_change_record( dir )) != NULL)
583 reply->action = record->action;
584 set_reply_data( record->name, record->len );
588 set_error( STATUS_NO_DATA_DETECTED );
592 dir_signal_changed( dir );
594 release_object( dir );