urlmon/tests: Don't proceed with POST operation (that will fail) if we run out of...
[wine.git] / server / change.c
blobe996d4999e42c20dd27a200f95bdc2c2b7163e90
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_fd_type( struct fd *fd );
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_fd_type, /* get_fd_type */
190 default_fd_ioctl, /* ioctl */
191 default_fd_queue_async, /* queue_async */
192 default_fd_reselect_async, /* reselect_async */
193 default_fd_cancel_async /* cancel_async */
196 static struct list change_list = LIST_INIT(change_list);
198 static void dnotify_adjust_changes( struct dir *dir )
200 #if defined(F_SETSIG) && defined(F_NOTIFY)
201 int fd = get_unix_fd( dir->fd );
202 unsigned int filter = dir->filter;
203 unsigned int val;
204 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
205 return;
207 val = DN_MULTISHOT;
208 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
209 val |= DN_RENAME | DN_DELETE | DN_CREATE;
210 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
211 val |= DN_RENAME | DN_DELETE | DN_CREATE;
212 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
213 val |= DN_ATTRIB;
214 if (filter & FILE_NOTIFY_CHANGE_SIZE)
215 val |= DN_MODIFY;
216 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
217 val |= DN_MODIFY;
218 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
219 val |= DN_ACCESS;
220 if (filter & FILE_NOTIFY_CHANGE_CREATION)
221 val |= DN_CREATE;
222 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
223 val |= DN_ATTRIB;
224 fcntl( fd, F_NOTIFY, val );
225 #endif
228 /* insert change in the global list */
229 static inline void insert_change( struct dir *dir )
231 sigset_t sigset;
233 sigemptyset( &sigset );
234 sigaddset( &sigset, SIGIO );
235 sigprocmask( SIG_BLOCK, &sigset, NULL );
236 list_add_head( &change_list, &dir->entry );
237 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
240 /* remove change from the global list */
241 static inline void remove_change( struct dir *dir )
243 sigset_t sigset;
245 sigemptyset( &sigset );
246 sigaddset( &sigset, SIGIO );
247 sigprocmask( SIG_BLOCK, &sigset, NULL );
248 list_remove( &dir->entry );
249 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
252 static void dir_dump( struct object *obj, int verbose )
254 struct dir *dir = (struct dir *)obj;
255 assert( obj->ops == &dir_ops );
256 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
259 /* enter here directly from SIGIO signal handler */
260 void do_change_notify( int unix_fd )
262 struct dir *dir;
264 /* FIXME: this is O(n) ... probably can be improved */
265 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
267 if (get_unix_fd( dir->fd ) != unix_fd) continue;
268 interlocked_xchg_add( &dir->notified, 1 );
269 break;
273 /* SIGIO callback, called synchronously with the poll loop */
274 void sigio_callback(void)
276 struct dir *dir;
278 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
280 if (interlocked_xchg( &dir->notified, 0 ))
281 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
285 static struct fd *dir_get_fd( struct object *obj )
287 struct dir *dir = (struct dir *)obj;
288 assert( obj->ops == &dir_ops );
289 return (struct fd *)grab_object( dir->fd );
292 static unsigned int dir_map_access( struct object *obj, unsigned int access )
294 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
295 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
296 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
297 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
298 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
301 static struct change_record *get_first_change_record( struct dir *dir )
303 struct list *ptr = list_head( &dir->change_records );
304 if (!ptr) return NULL;
305 list_remove( ptr );
306 return LIST_ENTRY( ptr, struct change_record, entry );
309 static void dir_destroy( struct object *obj )
311 struct change_record *record;
312 struct dir *dir = (struct dir *)obj;
313 assert (obj->ops == &dir_ops);
315 if (dir->filter)
316 remove_change( dir );
318 if (dir->inode)
320 list_remove( &dir->in_entry );
321 free_inode( dir->inode );
324 while ((record = get_first_change_record( dir ))) free( record );
326 release_object( dir->fd );
328 if (inotify_fd && list_empty( &change_list ))
330 release_object( inotify_fd );
331 inotify_fd = NULL;
335 static struct dir *
336 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
338 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
341 static int dir_get_poll_events( struct fd *fd )
343 return 0;
346 static enum server_fd_type dir_get_fd_type( struct fd *fd )
348 return FD_TYPE_DIR;
351 #ifdef USE_INOTIFY
353 #define HASH_SIZE 31
355 struct inode {
356 struct list ch_entry; /* entry in the children list */
357 struct list children; /* children of this inode */
358 struct inode *parent; /* parent of this inode */
359 struct list dirs; /* directory handles watching this inode */
360 struct list ino_entry; /* entry in the inode hash */
361 struct list wd_entry; /* entry in the watch descriptor hash */
362 dev_t dev; /* device number */
363 ino_t ino; /* device's inode number */
364 int wd; /* inotify's watch descriptor */
365 char *name; /* basename name of the inode */
368 struct list inode_hash[ HASH_SIZE ];
369 struct list wd_hash[ HASH_SIZE ];
371 static int inotify_add_dir( char *path, unsigned int filter );
373 static struct inode *inode_from_wd( int wd )
375 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
376 struct inode *inode;
378 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
379 if (inode->wd == wd)
380 return inode;
382 return NULL;
385 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
387 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
390 static struct inode *find_inode( dev_t dev, ino_t ino )
392 struct list *bucket = get_hash_list( dev, ino );
393 struct inode *inode;
395 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
396 if (inode->ino == ino && inode->dev == dev)
397 return inode;
399 return NULL;
402 static struct inode *create_inode( dev_t dev, ino_t ino )
404 struct inode *inode;
406 inode = malloc( sizeof *inode );
407 if (inode)
409 list_init( &inode->children );
410 list_init( &inode->dirs );
411 inode->ino = ino;
412 inode->dev = dev;
413 inode->wd = -1;
414 inode->parent = NULL;
415 inode->name = NULL;
416 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
418 return inode;
421 static struct inode *get_inode( dev_t dev, ino_t ino )
423 struct inode *inode;
425 inode = find_inode( dev, ino );
426 if (inode)
427 return inode;
428 return create_inode( dev, ino );
431 static void inode_set_wd( struct inode *inode, int wd )
433 if (inode->wd != -1)
434 list_remove( &inode->wd_entry );
435 inode->wd = wd;
436 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
439 static void inode_set_name( struct inode *inode, const char *name )
441 free (inode->name);
442 inode->name = name ? strdup( name ) : NULL;
445 static void free_inode( struct inode *inode )
447 int subtree = 0, watches = 0;
448 struct dir *dir;
450 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
452 subtree |= dir->subtree;
453 watches++;
456 if (!subtree && !inode->parent)
458 struct inode *tmp, *next;
459 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
460 struct inode, ch_entry )
462 assert( tmp != inode );
463 assert( tmp->parent == inode );
464 free_inode( tmp );
468 if (watches)
469 return;
471 if (inode->parent)
472 list_remove( &inode->ch_entry );
474 if (inode->wd != -1)
476 inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
477 list_remove( &inode->wd_entry );
479 list_remove( &inode->ino_entry );
481 free( inode->name );
482 free( inode );
485 static struct inode *inode_add( struct inode *parent,
486 dev_t dev, ino_t ino, const char *name )
488 struct inode *inode;
490 inode = get_inode( dev, ino );
491 if (!inode)
492 return NULL;
494 if (!inode->parent)
496 list_add_tail( &parent->children, &inode->ch_entry );
497 inode->parent = parent;
498 assert( inode != parent );
500 inode_set_name( inode, name );
502 return inode;
505 static struct inode *inode_from_name( struct inode *inode, const char *name )
507 struct inode *i;
509 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
510 if (i->name && !strcmp( i->name, name ))
511 return i;
512 return NULL;
515 static int inotify_get_poll_events( struct fd *fd );
516 static void inotify_poll_event( struct fd *fd, int event );
518 static const struct fd_ops inotify_fd_ops =
520 inotify_get_poll_events, /* get_poll_events */
521 inotify_poll_event, /* poll_event */
522 NULL, /* flush */
523 NULL, /* get_fd_type */
524 NULL, /* ioctl */
525 NULL, /* queue_async */
526 NULL, /* reselect_async */
527 NULL, /* cancel_async */
530 static int inotify_get_poll_events( struct fd *fd )
532 return POLLIN;
535 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
536 const char *relpath )
538 struct change_record *record;
540 assert( dir->obj.ops == &dir_ops );
542 if (dir->want_data)
544 size_t len = strlen(relpath);
545 record = malloc( offsetof(struct change_record, name[len]) );
546 if (!record)
547 return;
549 record->action = action;
550 memcpy( record->name, relpath, len );
551 record->len = len;
553 list_add_tail( &dir->change_records, &record->entry );
556 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
559 static unsigned int filter_from_event( struct inotify_event *ie )
561 unsigned int filter = 0;
563 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
564 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
565 if (ie->mask & IN_MODIFY)
566 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
567 if (ie->mask & IN_ATTRIB)
568 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
569 if (ie->mask & IN_ACCESS)
570 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
571 if (ie->mask & IN_CREATE)
572 filter |= FILE_NOTIFY_CHANGE_CREATION;
574 return filter;
577 /* scan up the parent directories for watches */
578 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
580 unsigned int filter = 0;
581 struct dir *dir;
583 /* combine filters from parents watching subtrees */
584 while (inode)
586 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
587 if (dir->subtree || !is_parent)
588 filter |= dir->filter;
589 is_parent = 1;
590 inode = inode->parent;
593 return filter;
596 static char *inode_get_path( struct inode *inode, int sz )
598 struct list *head;
599 char *path;
600 int len;
602 if (!inode)
603 return NULL;
605 head = list_head( &inode->dirs );
606 if (head)
608 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
609 path = malloc ( 32 + sz );
610 if (path)
611 sprintf( path, "/proc/self/fd/%u/", unix_fd );
612 return path;
615 if (!inode->name)
616 return NULL;
618 len = strlen( inode->name );
619 path = inode_get_path( inode->parent, sz + len + 1 );
620 if (!path)
621 return NULL;
623 strcat( path, inode->name );
624 strcat( path, "/" );
626 return path;
629 static int inode_check_dir( struct inode *parent, const char *name )
631 char *path;
632 unsigned int filter;
633 struct inode *inode;
634 struct stat st;
635 int wd = -1, r = -1;
637 path = inode_get_path( parent, strlen(name) );
638 if (!path)
639 return r;
641 strcat( path, name );
643 r = stat( path, &st );
644 if (r < 0) goto end;
646 if (!S_ISDIR(st.st_mode))
648 r = 0;
649 goto end;
652 r = 1;
654 filter = filter_from_inode( parent, 1 );
655 if (!filter)
656 goto end;
658 inode = inode_add( parent, st.st_dev, st.st_ino, name );
659 if (!inode || inode->wd != -1)
660 goto end;
662 wd = inotify_add_dir( path, filter );
663 if (wd != -1)
664 inode_set_wd( inode, wd );
665 else
666 free_inode( inode );
668 end:
669 free( path );
670 return r;
673 static int prepend( char **path, const char *segment )
675 int extra;
676 char *p;
678 extra = strlen( segment ) + 1;
679 if (*path)
681 int len = strlen( *path ) + 1;
682 p = realloc( *path, len + extra );
683 if (!p) return 0;
684 memmove( &p[ extra ], p, len );
685 p[ extra - 1 ] = '/';
686 memcpy( p, segment, extra - 1 );
688 else
690 p = malloc( extra );
691 if (!p) return 0;
692 memcpy( p, segment, extra );
695 *path = p;
697 return 1;
700 static void inotify_notify_all( struct inotify_event *ie )
702 unsigned int filter, action;
703 struct inode *inode, *i;
704 char *path = NULL;
705 struct dir *dir;
707 inode = inode_from_wd( ie->wd );
708 if (!inode)
710 fprintf( stderr, "no inode matches %d\n", ie->wd);
711 return;
714 filter = filter_from_event( ie );
716 if (ie->mask & IN_CREATE)
718 switch (inode_check_dir( inode, ie->name ))
720 case 1:
721 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
722 break;
723 case 0:
724 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
725 break;
726 default:
727 break;
728 /* Maybe the file disappeared before we could check it? */
730 action = FILE_ACTION_ADDED;
732 else if (ie->mask & IN_DELETE)
733 action = FILE_ACTION_REMOVED;
734 else
735 action = FILE_ACTION_MODIFIED;
738 * Work our way up the inode hierarchy
739 * extending the relative path as we go
740 * and notifying all recursive watches.
742 if (!prepend( &path, ie->name ))
743 return;
745 for (i = inode; i; i = i->parent)
747 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
748 if ((filter & dir->filter) && (i==inode || dir->subtree))
749 inotify_do_change_notify( dir, action, path );
751 if (!i->name || !prepend( &path, i->name ))
752 break;
755 free( path );
757 if (ie->mask & IN_DELETE)
759 i = inode_from_name( inode, ie->name );
760 if (i)
761 free_inode( i );
765 static void inotify_poll_event( struct fd *fd, int event )
767 int r, ofs, unix_fd;
768 char buffer[0x1000];
769 struct inotify_event *ie;
771 unix_fd = get_unix_fd( fd );
772 r = read( unix_fd, buffer, sizeof buffer );
773 if (r < 0)
775 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
776 return;
779 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
781 ie = (struct inotify_event*) &buffer[ofs];
782 if (!ie->len)
783 break;
784 ofs += offsetof( struct inotify_event, name[ie->len] );
785 if (ofs > r) break;
786 inotify_notify_all( ie );
790 static inline struct fd *create_inotify_fd( void )
792 int unix_fd;
794 unix_fd = inotify_init();
795 if (unix_fd<0)
796 return NULL;
797 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
800 static int map_flags( unsigned int filter )
802 unsigned int mask;
804 /* always watch these so we can track subdirectories in recursive watches */
805 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
807 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
808 mask |= IN_ATTRIB;
809 if (filter & FILE_NOTIFY_CHANGE_SIZE)
810 mask |= IN_MODIFY;
811 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
812 mask |= IN_MODIFY;
813 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
814 mask |= IN_ACCESS;
815 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
816 mask |= IN_ATTRIB;
818 return mask;
821 static int inotify_add_dir( char *path, unsigned int filter )
823 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
824 path, map_flags( filter ) );
825 if (wd != -1)
826 set_fd_events( inotify_fd, POLLIN );
827 return wd;
830 static int init_inotify( void )
832 int i;
834 if (inotify_fd)
835 return 1;
837 inotify_fd = create_inotify_fd();
838 if (!inotify_fd)
839 return 0;
841 for (i=0; i<HASH_SIZE; i++)
843 list_init( &inode_hash[i] );
844 list_init( &wd_hash[i] );
847 return 1;
850 static int inotify_adjust_changes( struct dir *dir )
852 unsigned int filter;
853 struct inode *inode;
854 struct stat st;
855 char path[32];
856 int wd, unix_fd;
858 if (!inotify_fd)
859 return 0;
861 unix_fd = get_unix_fd( dir->fd );
863 inode = dir->inode;
864 if (!inode)
866 /* check if this fd is already being watched */
867 if (-1 == fstat( unix_fd, &st ))
868 return 0;
870 inode = get_inode( st.st_dev, st.st_ino );
871 if (!inode)
872 inode = create_inode( st.st_dev, st.st_ino );
873 if (!inode)
874 return 0;
875 list_add_tail( &inode->dirs, &dir->in_entry );
876 dir->inode = inode;
879 filter = filter_from_inode( inode, 0 );
881 sprintf( path, "/proc/self/fd/%u", unix_fd );
882 wd = inotify_add_dir( path, filter );
883 if (wd == -1) return 0;
885 inode_set_wd( inode, wd );
887 return 1;
890 static char *get_basename( const char *link )
892 char *buffer, *name = NULL;
893 int r, n = 0x100;
895 while (1)
897 buffer = malloc( n );
898 if (!buffer) return NULL;
900 r = readlink( link, buffer, n );
901 if (r < 0)
902 break;
904 if (r < n)
906 name = buffer;
907 break;
909 free( buffer );
910 n *= 2;
913 if (name)
915 while (r > 0 && name[ r - 1 ] == '/' )
916 r--;
917 name[ r ] = 0;
919 name = strrchr( name, '/' );
920 if (name)
921 name = strdup( &name[1] );
924 free( buffer );
925 return name;
928 static int dir_add_to_existing_notify( struct dir *dir )
930 struct inode *inode, *parent;
931 unsigned int filter = 0;
932 struct stat st, st_new;
933 char link[35], *name;
934 int wd, unix_fd;
936 if (!inotify_fd)
937 return 0;
939 unix_fd = get_unix_fd( dir->fd );
941 /* check if it's in the list of inodes we want to watch */
942 if (-1 == fstat( unix_fd, &st_new ))
943 return 0;
944 inode = find_inode( st_new.st_dev, st_new.st_ino );
945 if (inode)
946 return 0;
948 /* lookup the parent */
949 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
950 if (-1 == stat( link, &st ))
951 return 0;
954 * If there's no parent, stop. We could keep going adding
955 * ../ to the path until we hit the root of the tree or
956 * find a recursively watched ancestor.
957 * Assume it's too expensive to search up the tree for now.
959 parent = find_inode( st.st_dev, st.st_ino );
960 if (!parent)
961 return 0;
963 if (parent->wd == -1)
964 return 0;
966 filter = filter_from_inode( parent, 1 );
967 if (!filter)
968 return 0;
970 sprintf( link, "/proc/self/fd/%u", unix_fd );
971 name = get_basename( link );
972 if (!name)
973 return 0;
974 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
975 free( name );
976 if (!inode)
977 return 0;
979 /* Couldn't find this inode at the start of the function, must be new */
980 assert( inode->wd == -1 );
982 wd = inotify_add_dir( link, filter );
983 if (wd != -1)
984 inode_set_wd( inode, wd );
986 return 1;
989 #else
991 static int init_inotify( void )
993 return 0;
996 static int inotify_adjust_changes( struct dir *dir )
998 return 0;
1001 static void free_inode( struct inode *inode )
1003 assert( 0 );
1006 static int dir_add_to_existing_notify( struct dir *dir )
1008 return 0;
1011 #endif /* USE_INOTIFY */
1013 struct object *create_dir_obj( struct fd *fd )
1015 struct dir *dir;
1017 dir = alloc_object( &dir_ops );
1018 if (!dir)
1019 return NULL;
1021 list_init( &dir->change_records );
1022 dir->filter = 0;
1023 dir->notified = 0;
1024 dir->want_data = 0;
1025 dir->inode = NULL;
1026 grab_object( fd );
1027 dir->fd = fd;
1028 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1030 dir_add_to_existing_notify( dir );
1032 return &dir->obj;
1035 /* enable change notifications for a directory */
1036 DECL_HANDLER(read_directory_changes)
1038 struct dir *dir;
1039 struct async *async;
1041 if (!req->filter)
1043 set_error(STATUS_INVALID_PARAMETER);
1044 return;
1047 dir = get_dir_obj( current->process, req->handle, 0 );
1048 if (!dir)
1049 return;
1051 /* requests don't timeout */
1052 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT, 0 ))) goto end;
1054 /* assign it once */
1055 if (!dir->filter)
1057 init_inotify();
1058 insert_change( dir );
1059 dir->filter = req->filter;
1060 dir->subtree = req->subtree;
1061 dir->want_data = req->want_data;
1064 /* if there's already a change in the queue, send it */
1065 if (!list_empty( &dir->change_records ))
1066 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1068 /* setup the real notification */
1069 if (!inotify_adjust_changes( dir ))
1070 dnotify_adjust_changes( dir );
1072 release_object( async );
1073 set_error(STATUS_PENDING);
1075 end:
1076 release_object( dir );
1079 DECL_HANDLER(read_change)
1081 struct change_record *record;
1082 struct dir *dir;
1084 dir = get_dir_obj( current->process, req->handle, 0 );
1085 if (!dir)
1086 return;
1088 if ((record = get_first_change_record( dir )) != NULL)
1090 reply->action = record->action;
1091 set_reply_data( record->name, record->len );
1092 free( record );
1094 else
1095 set_error( STATUS_NO_DATA_DETECTED );
1097 release_object( dir );