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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
31 #include <sys/types.h>
35 #ifdef HAVE_SYS_ERRNO_H
36 #include <sys/errno.h>
40 #define WIN32_NO_STATUS
54 #define DN_ACCESS 0x00000001 /* File accessed */
55 #define DN_MODIFY 0x00000002 /* File modified */
56 #define DN_CREATE 0x00000004 /* File created */
57 #define DN_DELETE 0x00000008 /* File removed */
58 #define DN_RENAME 0x00000010 /* File renamed */
59 #define DN_ATTRIB 0x00000020 /* File changed attibutes */
60 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
66 #if defined(__linux__) && defined(__i386__)
68 #define SYS_inotify_init 291
69 #define SYS_inotify_add_watch 292
70 #define SYS_inotify_rm_watch 293
72 struct inotify_event {
80 #define IN_ACCESS 0x00000001
81 #define IN_MODIFY 0x00000002
82 #define IN_ATTRIB 0x00000004
83 #define IN_CLOSE_WRITE 0x00000008
84 #define IN_CLOSE_NOWRITE 0x00000010
85 #define IN_OPEN 0x00000020
86 #define IN_MOVED_FROM 0x00000040
87 #define IN_MOVED_TO 0x00000080
88 #define IN_CREATE 0x00000100
89 #define IN_DELETE 0x00000200
90 #define IN_DELETE_SELF 0x00000400
92 static inline int inotify_init( void )
95 __asm__ __volatile__( "int $0x80"
97 : "0" (SYS_inotify_init));
98 if (ret<0) { errno = -ret; ret = -1; }
102 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
105 __asm__ __volatile__( "pushl %%ebx;\n\t"
109 : "=a" (ret) : "0" (SYS_inotify_add_watch),
110 "r" (fd), "c" (name), "d" (mask) );
111 if (ret<0) { errno = -ret; ret = -1; }
115 static inline int inotify_remove_watch( int fd, int wd )
118 __asm__ __volatile__( "pushl %%ebx;\n\t"
122 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
123 "r" (fd), "c" (wd) );
124 if (ret<0) { errno = -ret; ret = -1; }
134 static void free_inode( struct inode *inode );
136 static struct fd *inotify_fd;
138 struct change_record {
147 struct object obj; /* object header */
148 struct fd *fd; /* file descriptor to the directory */
149 struct list entry; /* entry in global change notifications list */
150 unsigned int filter; /* notification filter */
151 int notified; /* SIGIO counter */
152 int want_data; /* return change data */
153 int subtree; /* do we want to watch subdirectories? */
154 struct list change_records; /* data for the change */
155 struct list in_entry; /* entry in the inode dirs list */
156 struct inode *inode; /* inode of the associated directory */
159 static struct fd *dir_get_fd( struct object *obj );
160 static void dir_dump( struct object *obj, int verbose );
161 static void dir_destroy( struct object *obj );
163 static const struct object_ops dir_ops =
165 sizeof(struct dir), /* size */
167 add_queue, /* add_queue */
168 remove_queue, /* remove_queue */
169 default_fd_signaled, /* signaled */
170 no_satisfied, /* satisfied */
171 no_signal, /* signal */
172 dir_get_fd, /* get_fd */
173 default_fd_map_access, /* map_access */
174 default_get_sd, /* get_sd */
175 default_set_sd, /* set_sd */
176 no_lookup_name, /* lookup_name */
177 no_open_file, /* open_file */
178 fd_close_handle, /* close_handle */
179 dir_destroy /* destroy */
182 static int dir_get_poll_events( struct fd *fd );
183 static enum server_fd_type dir_get_fd_type( 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_fd_type, /* get_fd_type */
191 default_fd_ioctl, /* ioctl */
192 default_fd_queue_async, /* queue_async */
193 default_fd_reselect_async, /* reselect_async */
194 default_fd_cancel_async /* cancel_async */
197 static struct list change_list = LIST_INIT(change_list);
199 static void dnotify_adjust_changes( struct dir *dir )
201 #if defined(F_SETSIG) && defined(F_NOTIFY)
202 int fd = get_unix_fd( dir->fd );
203 unsigned int filter = dir->filter;
205 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
209 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
210 val |= DN_RENAME | DN_DELETE | DN_CREATE;
211 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
212 val |= DN_RENAME | DN_DELETE | DN_CREATE;
213 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
215 if (filter & FILE_NOTIFY_CHANGE_SIZE)
217 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
219 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
221 if (filter & FILE_NOTIFY_CHANGE_CREATION)
223 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
225 fcntl( fd, F_NOTIFY, val );
229 /* insert change in the global list */
230 static inline void insert_change( struct dir *dir )
234 sigemptyset( &sigset );
235 sigaddset( &sigset, SIGIO );
236 sigprocmask( SIG_BLOCK, &sigset, NULL );
237 list_add_head( &change_list, &dir->entry );
238 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
241 /* remove change from the global list */
242 static inline void remove_change( struct dir *dir )
246 sigemptyset( &sigset );
247 sigaddset( &sigset, SIGIO );
248 sigprocmask( SIG_BLOCK, &sigset, NULL );
249 list_remove( &dir->entry );
250 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
253 static void dir_dump( struct object *obj, int verbose )
255 struct dir *dir = (struct dir *)obj;
256 assert( obj->ops == &dir_ops );
257 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
260 /* enter here directly from SIGIO signal handler */
261 void do_change_notify( int unix_fd )
265 /* FIXME: this is O(n) ... probably can be improved */
266 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
268 if (get_unix_fd( dir->fd ) != unix_fd) continue;
269 interlocked_xchg_add( &dir->notified, 1 );
274 /* SIGIO callback, called synchronously with the poll loop */
275 void sigio_callback(void)
279 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
281 if (interlocked_xchg( &dir->notified, 0 ))
282 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
286 static struct fd *dir_get_fd( struct object *obj )
288 struct dir *dir = (struct dir *)obj;
289 assert( obj->ops == &dir_ops );
290 return (struct fd *)grab_object( dir->fd );
293 static struct change_record *get_first_change_record( struct dir *dir )
295 struct list *ptr = list_head( &dir->change_records );
296 if (!ptr) return NULL;
298 return LIST_ENTRY( ptr, struct change_record, entry );
301 static void dir_destroy( struct object *obj )
303 struct change_record *record;
304 struct dir *dir = (struct dir *)obj;
305 assert (obj->ops == &dir_ops);
308 remove_change( dir );
312 list_remove( &dir->in_entry );
313 free_inode( dir->inode );
316 while ((record = get_first_change_record( dir ))) free( record );
318 release_object( dir->fd );
320 if (inotify_fd && list_empty( &change_list ))
322 release_object( inotify_fd );
328 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
330 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
333 static int dir_get_poll_events( struct fd *fd )
338 static enum server_fd_type dir_get_fd_type( struct fd *fd )
348 struct list ch_entry; /* entry in the children list */
349 struct list children; /* children of this inode */
350 struct inode *parent; /* parent of this inode */
351 struct list dirs; /* directory handles watching this inode */
352 struct list ino_entry; /* entry in the inode hash */
353 struct list wd_entry; /* entry in the watch descriptor hash */
354 dev_t dev; /* device number */
355 ino_t ino; /* device's inode number */
356 int wd; /* inotify's watch descriptor */
357 char *name; /* basename name of the inode */
360 struct list inode_hash[ HASH_SIZE ];
361 struct list wd_hash[ HASH_SIZE ];
363 static int inotify_add_dir( char *path, unsigned int filter );
365 static struct inode *inode_from_wd( int wd )
367 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
370 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
377 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
379 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
382 static struct inode *find_inode( dev_t dev, ino_t ino )
384 struct list *bucket = get_hash_list( dev, ino );
387 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
388 if (inode->ino == ino && inode->dev == dev)
394 static struct inode *create_inode( dev_t dev, ino_t ino )
398 inode = malloc( sizeof *inode );
401 list_init( &inode->children );
402 list_init( &inode->dirs );
406 inode->parent = NULL;
408 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
413 static struct inode *get_inode( dev_t dev, ino_t ino )
417 inode = find_inode( dev, ino );
420 return create_inode( dev, ino );
423 static void inode_set_wd( struct inode *inode, int wd )
426 list_remove( &inode->wd_entry );
428 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
431 static void inode_set_name( struct inode *inode, const char *name )
434 inode->name = name ? strdup( name ) : NULL;
437 static void free_inode( struct inode *inode )
439 int subtree = 0, watches = 0;
442 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
444 subtree |= dir->subtree;
448 if (!subtree && !inode->parent)
450 struct inode *tmp, *next;
451 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
452 struct inode, ch_entry )
454 assert( tmp != inode );
455 assert( tmp->parent == inode );
464 list_remove( &inode->ch_entry );
468 inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
469 list_remove( &inode->wd_entry );
471 list_remove( &inode->ino_entry );
477 static struct inode *inode_add( struct inode *parent,
478 dev_t dev, ino_t ino, const char *name )
482 inode = get_inode( dev, ino );
488 list_add_tail( &parent->children, &inode->ch_entry );
489 inode->parent = parent;
490 assert( inode != parent );
492 inode_set_name( inode, name );
497 static struct inode *inode_from_name( struct inode *inode, const char *name )
501 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
502 if (i->name && !strcmp( i->name, name ))
507 static int inotify_get_poll_events( struct fd *fd );
508 static void inotify_poll_event( struct fd *fd, int event );
510 static const struct fd_ops inotify_fd_ops =
512 inotify_get_poll_events, /* get_poll_events */
513 inotify_poll_event, /* poll_event */
515 NULL, /* get_fd_type */
517 NULL, /* queue_async */
518 NULL, /* reselect_async */
519 NULL, /* cancel_async */
522 static int inotify_get_poll_events( struct fd *fd )
527 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
528 const char *relpath )
530 struct change_record *record;
532 assert( dir->obj.ops == &dir_ops );
536 size_t len = strlen(relpath);
537 record = malloc( offsetof(struct change_record, name[len]) );
541 record->action = action;
542 memcpy( record->name, relpath, len );
545 list_add_tail( &dir->change_records, &record->entry );
548 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
551 static unsigned int filter_from_event( struct inotify_event *ie )
553 unsigned int filter = 0;
555 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
556 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
557 if (ie->mask & IN_MODIFY)
558 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
559 if (ie->mask & IN_ATTRIB)
560 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
561 if (ie->mask & IN_ACCESS)
562 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
563 if (ie->mask & IN_CREATE)
564 filter |= FILE_NOTIFY_CHANGE_CREATION;
569 /* scan up the parent directories for watches */
570 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
572 unsigned int filter = 0;
575 /* combine filters from parents watching subtrees */
578 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
579 if (dir->subtree || !is_parent)
580 filter |= dir->filter;
582 inode = inode->parent;
588 static char *inode_get_path( struct inode *inode, int sz )
597 head = list_head( &inode->dirs );
600 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
601 path = malloc ( 32 + sz );
603 sprintf( path, "/proc/self/fd/%u/", unix_fd );
610 len = strlen( inode->name );
611 path = inode_get_path( inode->parent, sz + len + 1 );
615 strcat( path, inode->name );
621 static int inode_check_dir( struct inode *parent, const char *name )
629 path = inode_get_path( parent, strlen(name) );
633 strcat( path, name );
635 r = stat( path, &st );
638 if (!S_ISDIR(st.st_mode))
646 filter = filter_from_inode( parent, 1 );
650 inode = inode_add( parent, st.st_dev, st.st_ino, name );
651 if (!inode || inode->wd != -1)
654 wd = inotify_add_dir( path, filter );
656 inode_set_wd( inode, wd );
665 static int prepend( char **path, const char *segment )
670 extra = strlen( segment ) + 1;
673 int len = strlen( *path ) + 1;
674 p = realloc( *path, len + extra );
676 memmove( &p[ extra ], p, len );
677 p[ extra - 1 ] = '/';
678 memcpy( p, segment, extra - 1 );
684 memcpy( p, segment, extra );
692 static void inotify_notify_all( struct inotify_event *ie )
694 unsigned int filter, action;
695 struct inode *inode, *i;
699 inode = inode_from_wd( ie->wd );
702 fprintf( stderr, "no inode matches %d\n", ie->wd);
706 filter = filter_from_event( ie );
708 if (ie->mask & IN_CREATE)
710 switch (inode_check_dir( inode, ie->name ))
713 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
716 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
720 /* Maybe the file disappeared before we could check it? */
722 action = FILE_ACTION_ADDED;
724 else if (ie->mask & IN_DELETE)
725 action = FILE_ACTION_REMOVED;
727 action = FILE_ACTION_MODIFIED;
730 * Work our way up the inode hierarchy
731 * extending the relative path as we go
732 * and notifying all recursive watches.
734 if (!prepend( &path, ie->name ))
737 for (i = inode; i; i = i->parent)
739 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
740 if ((filter & dir->filter) && (i==inode || dir->subtree))
741 inotify_do_change_notify( dir, action, path );
743 if (!i->name || !prepend( &path, i->name ))
749 if (ie->mask & IN_DELETE)
751 i = inode_from_name( inode, ie->name );
757 static void inotify_poll_event( struct fd *fd, int event )
761 struct inotify_event *ie;
763 unix_fd = get_unix_fd( fd );
764 r = read( unix_fd, buffer, sizeof buffer );
767 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
771 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
773 ie = (struct inotify_event*) &buffer[ofs];
776 ofs += offsetof( struct inotify_event, name[ie->len] );
778 inotify_notify_all( ie );
782 static inline struct fd *create_inotify_fd( void )
786 unix_fd = inotify_init();
789 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
792 static int map_flags( unsigned int filter )
796 /* always watch these so we can track subdirectories in recursive watches */
797 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
799 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
801 if (filter & FILE_NOTIFY_CHANGE_SIZE)
803 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
805 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
807 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
813 static int inotify_add_dir( char *path, unsigned int filter )
815 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
816 path, map_flags( filter ) );
818 set_fd_events( inotify_fd, POLLIN );
822 static int init_inotify( void )
829 inotify_fd = create_inotify_fd();
833 for (i=0; i<HASH_SIZE; i++)
835 list_init( &inode_hash[i] );
836 list_init( &wd_hash[i] );
842 static int inotify_adjust_changes( struct dir *dir )
853 unix_fd = get_unix_fd( dir->fd );
858 /* check if this fd is already being watched */
859 if (-1 == fstat( unix_fd, &st ))
862 inode = get_inode( st.st_dev, st.st_ino );
864 inode = create_inode( st.st_dev, st.st_ino );
867 list_add_tail( &inode->dirs, &dir->in_entry );
871 filter = filter_from_inode( inode, 0 );
873 sprintf( path, "/proc/self/fd/%u", unix_fd );
874 wd = inotify_add_dir( path, filter );
875 if (wd == -1) return 0;
877 inode_set_wd( inode, wd );
882 static char *get_basename( const char *link )
884 char *buffer, *name = NULL;
889 buffer = malloc( n );
890 if (!buffer) return NULL;
892 r = readlink( link, buffer, n );
907 while (r > 0 && name[ r - 1 ] == '/' )
911 name = strrchr( name, '/' );
913 name = strdup( &name[1] );
920 static int dir_add_to_existing_notify( struct dir *dir )
922 struct inode *inode, *parent;
923 unsigned int filter = 0;
924 struct stat st, st_new;
925 char link[35], *name;
931 unix_fd = get_unix_fd( dir->fd );
933 /* check if it's in the list of inodes we want to watch */
934 if (-1 == fstat( unix_fd, &st_new ))
936 inode = find_inode( st_new.st_dev, st_new.st_ino );
940 /* lookup the parent */
941 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
942 if (-1 == stat( link, &st ))
946 * If there's no parent, stop. We could keep going adding
947 * ../ to the path until we hit the root of the tree or
948 * find a recursively watched ancestor.
949 * Assume it's too expensive to search up the tree for now.
951 parent = find_inode( st.st_dev, st.st_ino );
955 if (parent->wd == -1)
958 filter = filter_from_inode( parent, 1 );
962 sprintf( link, "/proc/self/fd/%u", unix_fd );
963 name = get_basename( link );
966 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
971 /* Couldn't find this inode at the start of the function, must be new */
972 assert( inode->wd == -1 );
974 wd = inotify_add_dir( link, filter );
976 inode_set_wd( inode, wd );
983 static int init_inotify( void )
988 static int inotify_adjust_changes( struct dir *dir )
993 static void free_inode( struct inode *inode )
998 static int dir_add_to_existing_notify( struct dir *dir )
1003 #endif /* USE_INOTIFY */
1005 struct object *create_dir_obj( struct fd *fd )
1009 dir = alloc_object( &dir_ops );
1013 list_init( &dir->change_records );
1020 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1022 dir_add_to_existing_notify( dir );
1027 /* enable change notifications for a directory */
1028 DECL_HANDLER(read_directory_changes)
1031 struct async *async;
1035 set_error(STATUS_INVALID_PARAMETER);
1039 dir = get_dir_obj( current->process, req->handle, 0 );
1043 /* requests don't timeout */
1044 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT, 0 ))) goto end;
1046 /* assign it once */
1050 insert_change( dir );
1051 dir->filter = req->filter;
1052 dir->subtree = req->subtree;
1053 dir->want_data = req->want_data;
1056 /* if there's already a change in the queue, send it */
1057 if (!list_empty( &dir->change_records ))
1058 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1060 /* setup the real notification */
1061 if (!inotify_adjust_changes( dir ))
1062 dnotify_adjust_changes( dir );
1064 release_object( async );
1065 set_error(STATUS_PENDING);
1068 release_object( dir );
1071 DECL_HANDLER(read_change)
1073 struct change_record *record;
1076 dir = get_dir_obj( current->process, req->handle, 0 );
1080 if ((record = get_first_change_record( dir )) != NULL)
1082 reply->action = record->action;
1083 set_reply_data( record->name, record->len );
1087 set_error( STATUS_NO_DATA_DETECTED );
1089 release_object( dir );