push 17e95bb5b89ca06d3b4ab9c4a2ab44cb05c88b95
[wine/hacks.git] / server / change.c
blob33034da7b5dbdd037630fe585d7ca618c821baa1
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <sys/types.h>
32 #include <limits.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #ifdef HAVE_SYS_ERRNO_H
36 #include <sys/errno.h>
37 #endif
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "thread.h"
46 #include "request.h"
47 #include "winternl.h"
49 /* dnotify support */
51 #ifdef linux
52 #ifndef F_NOTIFY
53 #define F_NOTIFY 1026
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 */
61 #endif
62 #endif
64 /* inotify support */
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 {
73 int wd;
74 unsigned int mask;
75 unsigned int cookie;
76 unsigned int len;
77 char name[1];
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 )
94 int ret;
95 __asm__ __volatile__( "int $0x80"
96 : "=a" (ret)
97 : "0" (SYS_inotify_init));
98 if (ret<0) { errno = -ret; ret = -1; }
99 return ret;
102 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
104 int ret;
105 __asm__ __volatile__( "pushl %%ebx;\n\t"
106 "movl %2,%%ebx;\n\t"
107 "int $0x80;\n\t"
108 "popl %%ebx"
109 : "=a" (ret) : "0" (SYS_inotify_add_watch),
110 "r" (fd), "c" (name), "d" (mask) );
111 if (ret<0) { errno = -ret; ret = -1; }
112 return ret;
115 static inline int inotify_remove_watch( int fd, int wd )
117 int ret;
118 __asm__ __volatile__( "pushl %%ebx;\n\t"
119 "movl %2,%%ebx;\n\t"
120 "int $0x80;\n\t"
121 "popl %%ebx"
122 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
123 "r" (fd), "c" (wd) );
124 if (ret<0) { errno = -ret; ret = -1; }
125 return ret;
128 #define USE_INOTIFY
130 #endif
132 struct inode;
134 static void free_inode( struct inode *inode );
136 static struct fd *inotify_fd;
138 struct change_record {
139 struct list entry;
140 int action;
141 int len;
142 char name[1];
145 struct dir
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 unsigned int dir_map_access( struct object *obj, unsigned int access );
161 static void dir_dump( struct object *obj, int verbose );
162 static void dir_destroy( struct object *obj );
164 static const struct object_ops dir_ops =
166 sizeof(struct dir), /* size */
167 dir_dump, /* dump */
168 add_queue, /* add_queue */
169 remove_queue, /* remove_queue */
170 default_fd_signaled, /* signaled */
171 no_satisfied, /* satisfied */
172 no_signal, /* signal */
173 dir_get_fd, /* get_fd */
174 dir_map_access, /* map_access */
175 no_lookup_name, /* lookup_name */
176 no_open_file, /* open_file */
177 fd_close_handle, /* close_handle */
178 dir_destroy /* destroy */
181 static int dir_get_poll_events( struct fd *fd );
182 static enum server_fd_type dir_get_info( struct fd *fd, int *flags );
184 static const struct fd_ops dir_fd_ops =
186 dir_get_poll_events, /* get_poll_events */
187 default_poll_event, /* poll_event */
188 no_flush, /* flush */
189 dir_get_info, /* get_file_info */
190 default_fd_queue_async, /* queue_async */
191 default_fd_cancel_async /* cancel_async */
194 static struct list change_list = LIST_INIT(change_list);
196 static void dnotify_adjust_changes( struct dir *dir )
198 #if defined(F_SETSIG) && defined(F_NOTIFY)
199 int fd = get_unix_fd( dir->fd );
200 unsigned int filter = dir->filter;
201 unsigned int val;
202 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
203 return;
205 val = DN_MULTISHOT;
206 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
207 val |= DN_RENAME | DN_DELETE | DN_CREATE;
208 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
209 val |= DN_RENAME | DN_DELETE | DN_CREATE;
210 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
211 val |= DN_ATTRIB;
212 if (filter & FILE_NOTIFY_CHANGE_SIZE)
213 val |= DN_MODIFY;
214 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
215 val |= DN_MODIFY;
216 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
217 val |= DN_ACCESS;
218 if (filter & FILE_NOTIFY_CHANGE_CREATION)
219 val |= DN_CREATE;
220 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
221 val |= DN_ATTRIB;
222 fcntl( fd, F_NOTIFY, val );
223 #endif
226 /* insert change in the global list */
227 static inline void insert_change( struct dir *dir )
229 sigset_t sigset;
231 sigemptyset( &sigset );
232 sigaddset( &sigset, SIGIO );
233 sigprocmask( SIG_BLOCK, &sigset, NULL );
234 list_add_head( &change_list, &dir->entry );
235 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
238 /* remove change from the global list */
239 static inline void remove_change( struct dir *dir )
241 sigset_t sigset;
243 sigemptyset( &sigset );
244 sigaddset( &sigset, SIGIO );
245 sigprocmask( SIG_BLOCK, &sigset, NULL );
246 list_remove( &dir->entry );
247 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
250 static void dir_dump( struct object *obj, int verbose )
252 struct dir *dir = (struct dir *)obj;
253 assert( obj->ops == &dir_ops );
254 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
257 /* enter here directly from SIGIO signal handler */
258 void do_change_notify( int unix_fd )
260 struct dir *dir;
262 /* FIXME: this is O(n) ... probably can be improved */
263 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
265 if (get_unix_fd( dir->fd ) != unix_fd) continue;
266 interlocked_xchg_add( &dir->notified, 1 );
267 break;
271 /* SIGIO callback, called synchronously with the poll loop */
272 void sigio_callback(void)
274 struct dir *dir;
276 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
278 if (interlocked_xchg( &dir->notified, 0 ))
279 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
283 static struct fd *dir_get_fd( struct object *obj )
285 struct dir *dir = (struct dir *)obj;
286 assert( obj->ops == &dir_ops );
287 return (struct fd *)grab_object( dir->fd );
290 static unsigned int dir_map_access( struct object *obj, unsigned int access )
292 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
293 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
294 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
295 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
296 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
299 static struct change_record *get_first_change_record( struct dir *dir )
301 struct list *ptr = list_head( &dir->change_records );
302 if (!ptr) return NULL;
303 list_remove( ptr );
304 return LIST_ENTRY( ptr, struct change_record, entry );
307 static void dir_destroy( struct object *obj )
309 struct change_record *record;
310 struct dir *dir = (struct dir *)obj;
311 assert (obj->ops == &dir_ops);
313 if (dir->filter)
314 remove_change( dir );
316 if (dir->inode)
318 list_remove( &dir->in_entry );
319 free_inode( dir->inode );
322 while ((record = get_first_change_record( dir ))) free( record );
324 release_object( dir->fd );
326 if (inotify_fd && list_empty( &change_list ))
328 release_object( inotify_fd );
329 inotify_fd = NULL;
333 static struct dir *
334 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
336 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
339 static int dir_get_poll_events( struct fd *fd )
341 return 0;
344 static enum server_fd_type dir_get_info( struct fd *fd, int *flags )
346 *flags = 0;
347 return FD_TYPE_DIR;
350 #ifdef USE_INOTIFY
352 #define HASH_SIZE 31
354 struct inode {
355 struct list ch_entry; /* entry in the children list */
356 struct list children; /* children of this inode */
357 struct inode *parent; /* parent of this inode */
358 struct list dirs; /* directory handles watching this inode */
359 struct list ino_entry; /* entry in the inode hash */
360 struct list wd_entry; /* entry in the watch descriptor hash */
361 dev_t dev; /* device number */
362 ino_t ino; /* device's inode number */
363 int wd; /* inotify's watch descriptor */
364 char *name; /* basename name of the inode */
367 struct list inode_hash[ HASH_SIZE ];
368 struct list wd_hash[ HASH_SIZE ];
370 static int inotify_add_dir( char *path, unsigned int filter );
372 static struct inode *inode_from_wd( int wd )
374 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
375 struct inode *inode;
377 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
378 if (inode->wd == wd)
379 return inode;
381 return NULL;
384 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
386 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
389 static struct inode *find_inode( dev_t dev, ino_t ino )
391 struct list *bucket = get_hash_list( dev, ino );
392 struct inode *inode;
394 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
395 if (inode->ino == ino && inode->dev == dev)
396 return inode;
398 return NULL;
401 static struct inode *create_inode( dev_t dev, ino_t ino )
403 struct inode *inode;
405 inode = malloc( sizeof *inode );
406 if (inode)
408 list_init( &inode->children );
409 list_init( &inode->dirs );
410 inode->ino = ino;
411 inode->dev = dev;
412 inode->wd = -1;
413 inode->parent = NULL;
414 inode->name = NULL;
415 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
417 return inode;
420 static struct inode *get_inode( dev_t dev, ino_t ino )
422 struct inode *inode;
424 inode = find_inode( dev, ino );
425 if (inode)
426 return inode;
427 return create_inode( dev, ino );
430 static void inode_set_wd( struct inode *inode, int wd )
432 if (inode->wd != -1)
433 list_remove( &inode->wd_entry );
434 inode->wd = wd;
435 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
438 static void inode_set_name( struct inode *inode, const char *name )
440 free (inode->name);
441 inode->name = name ? strdup( name ) : NULL;
444 static void free_inode( struct inode *inode )
446 int subtree = 0, watches = 0;
447 struct dir *dir;
449 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
451 subtree |= dir->subtree;
452 watches++;
455 if (!subtree && !inode->parent)
457 struct inode *tmp, *next;
458 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
459 struct inode, ch_entry )
461 assert( tmp != inode );
462 assert( tmp->parent == inode );
463 free_inode( tmp );
467 if (watches)
468 return;
470 if (inode->parent)
471 list_remove( &inode->ch_entry );
473 if (inode->wd != -1)
475 inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
476 list_remove( &inode->wd_entry );
478 list_remove( &inode->ino_entry );
480 free( inode->name );
481 free( inode );
484 static struct inode *inode_add( struct inode *parent,
485 dev_t dev, ino_t ino, const char *name )
487 struct inode *inode;
489 inode = get_inode( dev, ino );
490 if (!inode)
491 return NULL;
493 if (!inode->parent)
495 list_add_tail( &parent->children, &inode->ch_entry );
496 inode->parent = parent;
497 assert( inode != parent );
499 inode_set_name( inode, name );
501 return inode;
504 static struct inode *inode_from_name( struct inode *inode, const char *name )
506 struct inode *i;
508 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
509 if (i->name && !strcmp( i->name, name ))
510 return i;
511 return NULL;
514 static int inotify_get_poll_events( struct fd *fd );
515 static void inotify_poll_event( struct fd *fd, int event );
517 static const struct fd_ops inotify_fd_ops =
519 inotify_get_poll_events, /* get_poll_events */
520 inotify_poll_event, /* poll_event */
521 no_flush, /* flush */
522 no_get_file_info, /* get_file_info */
523 default_fd_queue_async, /* queue_async */
524 default_fd_cancel_async, /* cancel_async */
527 static int inotify_get_poll_events( struct fd *fd )
529 return POLLIN;
532 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
533 const char *relpath )
535 struct change_record *record;
537 assert( dir->obj.ops == &dir_ops );
539 if (dir->want_data)
541 size_t len = strlen(relpath);
542 record = malloc( offsetof(struct change_record, name[len]) );
543 if (!record)
544 return;
546 record->action = action;
547 memcpy( record->name, relpath, len );
548 record->len = len;
550 list_add_tail( &dir->change_records, &record->entry );
553 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
556 static unsigned int filter_from_event( struct inotify_event *ie )
558 unsigned int filter = 0;
560 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
561 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
562 if (ie->mask & IN_MODIFY)
563 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
564 if (ie->mask & IN_ATTRIB)
565 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
566 if (ie->mask & IN_ACCESS)
567 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
568 if (ie->mask & IN_CREATE)
569 filter |= FILE_NOTIFY_CHANGE_CREATION;
571 return filter;
574 /* scan up the parent directories for watches */
575 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
577 unsigned int filter = 0;
578 struct dir *dir;
580 /* combine filters from parents watching subtrees */
581 while (inode)
583 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
584 if (dir->subtree || !is_parent)
585 filter |= dir->filter;
586 is_parent = 1;
587 inode = inode->parent;
590 return filter;
593 static char *inode_get_path( struct inode *inode, int sz )
595 struct list *head;
596 char *path;
597 int len;
599 if (!inode)
600 return NULL;
602 head = list_head( &inode->dirs );
603 if (head)
605 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
606 path = malloc ( 32 + sz );
607 if (path)
608 sprintf( path, "/proc/self/fd/%u/", unix_fd );
609 return path;
612 if (!inode->name)
613 return NULL;
615 len = strlen( inode->name );
616 path = inode_get_path( inode->parent, sz + len + 1 );
617 if (!path)
618 return NULL;
620 strcat( path, inode->name );
621 strcat( path, "/" );
623 return path;
626 static int inode_check_dir( struct inode *parent, const char *name )
628 char *path;
629 unsigned int filter;
630 struct inode *inode;
631 struct stat st;
632 int wd = -1, r = -1;
634 path = inode_get_path( parent, strlen(name) );
635 if (!path)
636 return r;
638 strcat( path, name );
640 r = stat( path, &st );
641 if (r < 0) goto end;
643 if (!S_ISDIR(st.st_mode))
645 r = 0;
646 goto end;
649 r = 1;
651 filter = filter_from_inode( parent, 1 );
652 if (!filter)
653 goto end;
655 inode = inode_add( parent, st.st_dev, st.st_ino, name );
656 if (!inode || inode->wd != -1)
657 goto end;
659 wd = inotify_add_dir( path, filter );
660 if (wd != -1)
661 inode_set_wd( inode, wd );
662 else
663 free_inode( inode );
665 end:
666 free( path );
667 return r;
670 static int prepend( char **path, const char *segment )
672 int extra;
673 char *p;
675 extra = strlen( segment ) + 1;
676 if (*path)
678 int len = strlen( *path ) + 1;
679 p = realloc( *path, len + extra );
680 if (!p) return 0;
681 memmove( &p[ extra ], p, len );
682 p[ extra - 1 ] = '/';
683 memcpy( p, segment, extra - 1 );
685 else
687 p = malloc( extra );
688 if (!p) return 0;
689 memcpy( p, segment, extra );
692 *path = p;
694 return 1;
697 static void inotify_notify_all( struct inotify_event *ie )
699 unsigned int filter, action;
700 struct inode *inode, *i;
701 char *path = NULL;
702 struct dir *dir;
704 inode = inode_from_wd( ie->wd );
705 if (!inode)
707 fprintf( stderr, "no inode matches %d\n", ie->wd);
708 return;
711 filter = filter_from_event( ie );
713 if (ie->mask & IN_CREATE)
715 switch (inode_check_dir( inode, ie->name ))
717 case 1:
718 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
719 break;
720 case 0:
721 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
722 break;
723 default:
724 break;
725 /* Maybe the file disappeared before we could check it? */
727 action = FILE_ACTION_ADDED;
729 else if (ie->mask & IN_DELETE)
730 action = FILE_ACTION_REMOVED;
731 else
732 action = FILE_ACTION_MODIFIED;
735 * Work our way up the inode hierarchy
736 * extending the relative path as we go
737 * and notifying all recursive watches.
739 if (!prepend( &path, ie->name ))
740 return;
742 for (i = inode; i; i = i->parent)
744 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
745 if ((filter & dir->filter) && (i==inode || dir->subtree))
746 inotify_do_change_notify( dir, action, path );
748 if (!i->name || !prepend( &path, i->name ))
749 break;
752 free( path );
754 if (ie->mask & IN_DELETE)
756 i = inode_from_name( inode, ie->name );
757 if (i)
758 free_inode( i );
762 static void inotify_poll_event( struct fd *fd, int event )
764 int r, ofs, unix_fd;
765 char buffer[0x1000];
766 struct inotify_event *ie;
768 unix_fd = get_unix_fd( fd );
769 r = read( unix_fd, buffer, sizeof buffer );
770 if (r < 0)
772 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
773 return;
776 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
778 ie = (struct inotify_event*) &buffer[ofs];
779 if (!ie->len)
780 break;
781 ofs += offsetof( struct inotify_event, name[ie->len] );
782 if (ofs > r) break;
783 inotify_notify_all( ie );
787 static inline struct fd *create_inotify_fd( void )
789 int unix_fd;
791 unix_fd = inotify_init();
792 if (unix_fd<0)
793 return NULL;
794 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL );
797 static int map_flags( unsigned int filter )
799 unsigned int mask;
801 /* always watch these so we can track subdirectories in recursive watches */
802 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
804 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
805 mask |= IN_ATTRIB;
806 if (filter & FILE_NOTIFY_CHANGE_SIZE)
807 mask |= IN_MODIFY;
808 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
809 mask |= IN_MODIFY;
810 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
811 mask |= IN_ACCESS;
812 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
813 mask |= IN_ATTRIB;
815 return mask;
818 static int inotify_add_dir( char *path, unsigned int filter )
820 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
821 path, map_flags( filter ) );
822 if (wd != -1)
823 set_fd_events( inotify_fd, POLLIN );
824 return wd;
827 static int init_inotify( void )
829 int i;
831 if (inotify_fd)
832 return 1;
834 inotify_fd = create_inotify_fd();
835 if (!inotify_fd)
836 return 0;
838 for (i=0; i<HASH_SIZE; i++)
840 list_init( &inode_hash[i] );
841 list_init( &wd_hash[i] );
844 return 1;
847 static int inotify_adjust_changes( struct dir *dir )
849 unsigned int filter;
850 struct inode *inode;
851 struct stat st;
852 char path[32];
853 int wd, unix_fd;
855 if (!inotify_fd)
856 return 0;
858 unix_fd = get_unix_fd( dir->fd );
860 inode = dir->inode;
861 if (!inode)
863 /* check if this fd is already being watched */
864 if (-1 == fstat( unix_fd, &st ))
865 return 0;
867 inode = get_inode( st.st_dev, st.st_ino );
868 if (!inode)
869 inode = create_inode( st.st_dev, st.st_ino );
870 if (!inode)
871 return 0;
872 list_add_tail( &inode->dirs, &dir->in_entry );
873 dir->inode = inode;
876 filter = filter_from_inode( inode, 0 );
878 sprintf( path, "/proc/self/fd/%u", unix_fd );
879 wd = inotify_add_dir( path, filter );
880 if (wd == -1) return 0;
882 inode_set_wd( inode, wd );
884 return 1;
887 static char *get_basename( const char *link )
889 char *buffer, *name = NULL;
890 int r, n = 0x100;
892 while (1)
894 buffer = malloc( n );
895 if (!buffer) return NULL;
897 r = readlink( link, buffer, n );
898 if (r < 0)
899 break;
901 if (r < n)
903 name = buffer;
904 break;
906 free( buffer );
907 n *= 2;
910 if (name)
912 while (r > 0 && name[ r - 1 ] == '/' )
913 r--;
914 name[ r ] = 0;
916 name = strrchr( name, '/' );
917 if (name)
918 name = strdup( &name[1] );
921 free( buffer );
922 return name;
925 static int dir_add_to_existing_notify( struct dir *dir )
927 struct inode *inode, *parent;
928 unsigned int filter = 0;
929 struct stat st, st_new;
930 char link[35], *name;
931 int wd, unix_fd;
933 if (!inotify_fd)
934 return 0;
936 unix_fd = get_unix_fd( dir->fd );
938 /* check if it's in the list of inodes we want to watch */
939 if (-1 == fstat( unix_fd, &st_new ))
940 return 0;
941 inode = find_inode( st_new.st_dev, st_new.st_ino );
942 if (inode)
943 return 0;
945 /* lookup the parent */
946 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
947 if (-1 == stat( link, &st ))
948 return 0;
951 * If there's no parent, stop. We could keep going adding
952 * ../ to the path until we hit the root of the tree or
953 * find a recursively watched ancestor.
954 * Assume it's too expensive to search up the tree for now.
956 parent = find_inode( st.st_dev, st.st_ino );
957 if (!parent)
958 return 0;
960 if (parent->wd == -1)
961 return 0;
963 filter = filter_from_inode( parent, 1 );
964 if (!filter)
965 return 0;
967 sprintf( link, "/proc/self/fd/%u", unix_fd );
968 name = get_basename( link );
969 if (!name)
970 return 0;
971 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
972 free( name );
973 if (!inode)
974 return 0;
976 /* Couldn't find this inode at the start of the function, must be new */
977 assert( inode->wd == -1 );
979 wd = inotify_add_dir( link, filter );
980 if (wd != -1)
981 inode_set_wd( inode, wd );
983 return 1;
986 #else
988 static int init_inotify( void )
990 return 0;
993 static int inotify_adjust_changes( struct dir *dir )
995 return 0;
998 static void free_inode( struct inode *inode )
1000 assert( 0 );
1003 static int dir_add_to_existing_notify( struct dir *dir )
1005 return 0;
1008 #endif /* USE_INOTIFY */
1010 struct object *create_dir_obj( struct fd *fd )
1012 struct dir *dir;
1014 dir = alloc_object( &dir_ops );
1015 if (!dir)
1016 return NULL;
1018 list_init( &dir->change_records );
1019 dir->filter = 0;
1020 dir->notified = 0;
1021 dir->want_data = 0;
1022 dir->inode = NULL;
1023 grab_object( fd );
1024 dir->fd = fd;
1025 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1027 dir_add_to_existing_notify( dir );
1029 return &dir->obj;
1032 /* enable change notifications for a directory */
1033 DECL_HANDLER(read_directory_changes)
1035 struct dir *dir;
1036 struct async *async;
1038 if (!req->filter)
1040 set_error(STATUS_INVALID_PARAMETER);
1041 return;
1044 dir = get_dir_obj( current->process, req->handle, 0 );
1045 if (!dir)
1046 return;
1048 /* requests don't timeout */
1049 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT, 0 ))) goto end;
1051 /* assign it once */
1052 if (!dir->filter)
1054 init_inotify();
1055 insert_change( dir );
1056 dir->filter = req->filter;
1057 dir->subtree = req->subtree;
1058 dir->want_data = req->want_data;
1061 /* if there's already a change in the queue, send it */
1062 if (!list_empty( &dir->change_records ))
1063 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1065 /* setup the real notification */
1066 if (!inotify_adjust_changes( dir ))
1067 dnotify_adjust_changes( dir );
1069 release_object( async );
1070 set_error(STATUS_PENDING);
1072 end:
1073 release_object( dir );
1076 DECL_HANDLER(read_change)
1078 struct change_record *record;
1079 struct dir *dir;
1081 dir = get_dir_obj( current->process, req->handle, 0 );
1082 if (!dir)
1083 return;
1085 if ((record = get_first_change_record( dir )) != NULL)
1087 reply->action = record->action;
1088 set_reply_data( record->name, record->len );
1089 free( record );
1091 else
1092 set_error( STATUS_NO_DATA_DETECTED );
1094 release_object( dir );