server: Make async I/O queues into real objects.
[wine/wine-kai.git] / server / change.c
blobc4696a0a80f5e46e88d5f36fbe729b7241545fe0
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 struct event *event;
151 unsigned int filter; /* notification filter */
152 int notified; /* SIGIO counter */
153 int want_data; /* return change data */
154 long signaled; /* the file changed */
155 int subtree; /* do we want to watch subdirectories? */
156 struct list change_records; /* data for the change */
157 struct list in_entry; /* entry in the inode dirs list */
158 struct inode *inode; /* inode of the associated directory */
161 static struct fd *dir_get_fd( struct object *obj );
162 static unsigned int dir_map_access( struct object *obj, unsigned int access );
163 static void dir_dump( struct object *obj, int verbose );
164 static void dir_destroy( struct object *obj );
165 static int dir_signaled( struct object *obj, struct thread *thread );
167 static const struct object_ops dir_ops =
169 sizeof(struct dir), /* size */
170 dir_dump, /* dump */
171 add_queue, /* add_queue */
172 remove_queue, /* remove_queue */
173 dir_signaled, /* signaled */
174 no_satisfied, /* satisfied */
175 no_signal, /* signal */
176 dir_get_fd, /* get_fd */
177 dir_map_access, /* map_access */
178 no_lookup_name, /* lookup_name */
179 no_open_file, /* open_file */
180 fd_close_handle, /* close_handle */
181 dir_destroy /* destroy */
184 static int dir_get_poll_events( struct fd *fd );
185 static enum server_fd_type dir_get_info( struct fd *fd, int *flags );
187 static const struct fd_ops dir_fd_ops =
189 dir_get_poll_events, /* get_poll_events */
190 default_poll_event, /* poll_event */
191 no_flush, /* flush */
192 dir_get_info, /* get_file_info */
193 default_fd_queue_async, /* queue_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;
204 unsigned int val;
205 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
206 return;
208 val = DN_MULTISHOT;
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)
214 val |= DN_ATTRIB;
215 if (filter & FILE_NOTIFY_CHANGE_SIZE)
216 val |= DN_MODIFY;
217 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
218 val |= DN_MODIFY;
219 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
220 val |= DN_ACCESS;
221 if (filter & FILE_NOTIFY_CHANGE_CREATION)
222 val |= DN_CREATE;
223 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
224 val |= DN_ATTRIB;
225 fcntl( fd, F_NOTIFY, val );
226 #endif
229 /* insert change in the global list */
230 static inline void insert_change( struct dir *dir )
232 sigset_t sigset;
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 )
244 sigset_t sigset;
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 event=%p filter=%08x\n",
258 dir->fd, dir->event, dir->filter );
261 static int dir_signaled( struct object *obj, struct thread *thread )
263 struct dir *dir = (struct dir *)obj;
264 assert (obj->ops == &dir_ops);
265 return (dir->event == NULL) && dir->signaled;
268 /* enter here directly from SIGIO signal handler */
269 void do_change_notify( int unix_fd )
271 struct dir *dir;
273 /* FIXME: this is O(n) ... probably can be improved */
274 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
276 if (get_unix_fd( dir->fd ) != unix_fd) continue;
277 interlocked_xchg_add( &dir->notified, 1 );
278 break;
282 static void dir_signal_changed( struct dir *dir )
284 if (!dir->event) wake_up( &dir->obj, 0 );
287 /* SIGIO callback, called synchronously with the poll loop */
288 void sigio_callback(void)
290 struct dir *dir;
292 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
294 long count = interlocked_xchg( &dir->notified, 0 );
295 if (count)
297 dir->signaled += count;
298 if (dir->signaled == count) /* was it 0? */
299 dir_signal_changed( dir );
304 static struct fd *dir_get_fd( struct object *obj )
306 struct dir *dir = (struct dir *)obj;
307 assert( obj->ops == &dir_ops );
308 return (struct fd *)grab_object( dir->fd );
311 static unsigned int dir_map_access( struct object *obj, unsigned int access )
313 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
314 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
315 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
316 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
317 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
320 static struct change_record *get_first_change_record( struct dir *dir )
322 struct list *ptr = list_head( &dir->change_records );
323 if (!ptr) return NULL;
324 list_remove( ptr );
325 return LIST_ENTRY( ptr, struct change_record, entry );
328 static void dir_destroy( struct object *obj )
330 struct change_record *record;
331 struct dir *dir = (struct dir *)obj;
332 assert (obj->ops == &dir_ops);
334 if (dir->filter)
335 remove_change( dir );
337 if (dir->inode)
339 list_remove( &dir->in_entry );
340 free_inode( dir->inode );
343 while ((record = get_first_change_record( dir ))) free( record );
345 if (dir->event) release_object( dir->event );
346 release_object( dir->fd );
348 if (inotify_fd && list_empty( &change_list ))
350 release_object( inotify_fd );
351 inotify_fd = NULL;
355 static struct dir *
356 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
358 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
361 static int dir_get_poll_events( struct fd *fd )
363 return 0;
366 static enum server_fd_type dir_get_info( struct fd *fd, int *flags )
368 *flags = 0;
369 return FD_TYPE_DIR;
372 #ifdef USE_INOTIFY
374 #define HASH_SIZE 31
376 struct inode {
377 struct list ch_entry; /* entry in the children list */
378 struct list children; /* children of this inode */
379 struct inode *parent; /* parent of this inode */
380 struct list dirs; /* directory handles watching this inode */
381 struct list ino_entry; /* entry in the inode hash */
382 struct list wd_entry; /* entry in the watch descriptor hash */
383 dev_t dev; /* device number */
384 ino_t ino; /* device's inode number */
385 int wd; /* inotify's watch descriptor */
386 char *name; /* basename name of the inode */
389 struct list inode_hash[ HASH_SIZE ];
390 struct list wd_hash[ HASH_SIZE ];
392 static int inotify_add_dir( char *path, unsigned int filter );
394 static struct inode *inode_from_wd( int wd )
396 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
397 struct inode *inode;
399 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
400 if (inode->wd == wd)
401 return inode;
403 return NULL;
406 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
408 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
411 static struct inode *find_inode( dev_t dev, ino_t ino )
413 struct list *bucket = get_hash_list( dev, ino );
414 struct inode *inode;
416 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
417 if (inode->ino == ino && inode->dev == dev)
418 return inode;
420 return NULL;
423 static struct inode *create_inode( dev_t dev, ino_t ino )
425 struct inode *inode;
427 inode = malloc( sizeof *inode );
428 if (inode)
430 list_init( &inode->children );
431 list_init( &inode->dirs );
432 inode->ino = ino;
433 inode->dev = dev;
434 inode->wd = -1;
435 inode->parent = NULL;
436 inode->name = NULL;
437 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
439 return inode;
442 static struct inode *get_inode( dev_t dev, ino_t ino )
444 struct inode *inode;
446 inode = find_inode( dev, ino );
447 if (inode)
448 return inode;
449 return create_inode( dev, ino );
452 static void inode_set_wd( struct inode *inode, int wd )
454 if (inode->wd != -1)
455 list_remove( &inode->wd_entry );
456 inode->wd = wd;
457 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
460 static void inode_set_name( struct inode *inode, const char *name )
462 free (inode->name);
463 inode->name = name ? strdup( name ) : NULL;
466 static void free_inode( struct inode *inode )
468 int subtree = 0, watches = 0;
469 struct dir *dir;
471 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
473 subtree |= dir->subtree;
474 watches++;
477 if (!subtree && !inode->parent)
479 struct inode *tmp, *next;
480 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
481 struct inode, ch_entry )
483 assert( tmp != inode );
484 assert( tmp->parent == inode );
485 free_inode( tmp );
489 if (watches)
490 return;
492 if (inode->parent)
493 list_remove( &inode->ch_entry );
495 if (inode->wd != -1)
497 inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
498 list_remove( &inode->wd_entry );
500 list_remove( &inode->ino_entry );
502 free( inode->name );
503 free( inode );
506 static struct inode *inode_add( struct inode *parent,
507 dev_t dev, ino_t ino, const char *name )
509 struct inode *inode;
511 inode = get_inode( dev, ino );
512 if (!inode)
513 return NULL;
515 if (!inode->parent)
517 list_add_tail( &parent->children, &inode->ch_entry );
518 inode->parent = parent;
519 assert( inode != parent );
521 inode_set_name( inode, name );
523 return inode;
526 static struct inode *inode_from_name( struct inode *inode, const char *name )
528 struct inode *i;
530 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
531 if (i->name && !strcmp( i->name, name ))
532 return i;
533 return NULL;
536 static int inotify_get_poll_events( struct fd *fd );
537 static void inotify_poll_event( struct fd *fd, int event );
539 static const struct fd_ops inotify_fd_ops =
541 inotify_get_poll_events, /* get_poll_events */
542 inotify_poll_event, /* poll_event */
543 no_flush, /* flush */
544 no_get_file_info, /* get_file_info */
545 default_fd_queue_async, /* queue_async */
546 default_fd_cancel_async, /* cancel_async */
549 static int inotify_get_poll_events( struct fd *fd )
551 return POLLIN;
554 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
555 const char *relpath )
557 struct change_record *record;
559 assert( dir->obj.ops == &dir_ops );
561 if (dir->want_data)
563 size_t len = strlen(relpath);
564 record = malloc( offsetof(struct change_record, name[len]) );
565 if (!record)
566 return;
568 record->action = action;
569 memcpy( record->name, relpath, len );
570 record->len = len;
572 list_add_tail( &dir->change_records, &record->entry );
575 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
578 static unsigned int filter_from_event( struct inotify_event *ie )
580 unsigned int filter = 0;
582 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
583 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
584 if (ie->mask & IN_MODIFY)
585 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
586 if (ie->mask & IN_ATTRIB)
587 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
588 if (ie->mask & IN_ACCESS)
589 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
590 if (ie->mask & IN_CREATE)
591 filter |= FILE_NOTIFY_CHANGE_CREATION;
593 return filter;
596 /* scan up the parent directories for watches */
597 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
599 unsigned int filter = 0;
600 struct dir *dir;
602 /* combine filters from parents watching subtrees */
603 while (inode)
605 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
606 if (dir->subtree || !is_parent)
607 filter |= dir->filter;
608 is_parent = 1;
609 inode = inode->parent;
612 return filter;
615 static char *inode_get_path( struct inode *inode, int sz )
617 struct list *head;
618 char *path;
619 int len;
621 if (!inode)
622 return NULL;
624 head = list_head( &inode->dirs );
625 if (head)
627 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
628 path = malloc ( 32 + sz );
629 if (path)
630 sprintf( path, "/proc/self/fd/%u/", unix_fd );
631 return path;
634 if (!inode->name)
635 return NULL;
637 len = strlen( inode->name );
638 path = inode_get_path( inode->parent, sz + len + 1 );
639 if (!path)
640 return NULL;
642 strcat( path, inode->name );
643 strcat( path, "/" );
645 return path;
648 static int inode_check_dir( struct inode *parent, const char *name )
650 char *path;
651 unsigned int filter;
652 struct inode *inode;
653 struct stat st;
654 int wd = -1, r = -1;
656 path = inode_get_path( parent, strlen(name) );
657 if (!path)
658 return r;
660 strcat( path, name );
662 r = stat( path, &st );
663 if (r < 0) goto end;
665 if (!S_ISDIR(st.st_mode))
667 r = 0;
668 goto end;
671 r = 1;
673 filter = filter_from_inode( parent, 1 );
674 if (!filter)
675 goto end;
677 inode = inode_add( parent, st.st_dev, st.st_ino, name );
678 if (!inode || inode->wd != -1)
679 goto end;
681 wd = inotify_add_dir( path, filter );
682 if (wd != -1)
683 inode_set_wd( inode, wd );
684 else
685 free_inode( inode );
687 end:
688 free( path );
689 return r;
692 static int prepend( char **path, const char *segment )
694 int extra;
695 char *p;
697 extra = strlen( segment ) + 1;
698 if (*path)
700 int len = strlen( *path ) + 1;
701 p = realloc( *path, len + extra );
702 if (!p) return 0;
703 memmove( &p[ extra ], p, len );
704 p[ extra - 1 ] = '/';
705 memcpy( p, segment, extra - 1 );
707 else
709 p = malloc( extra );
710 if (!p) return 0;
711 memcpy( p, segment, extra );
714 *path = p;
716 return 1;
719 static void inotify_notify_all( struct inotify_event *ie )
721 unsigned int filter, action;
722 struct inode *inode, *i;
723 char *path = NULL;
724 struct dir *dir;
726 inode = inode_from_wd( ie->wd );
727 if (!inode)
729 fprintf( stderr, "no inode matches %d\n", ie->wd);
730 return;
733 filter = filter_from_event( ie );
735 if (ie->mask & IN_CREATE)
737 switch (inode_check_dir( inode, ie->name ))
739 case 1:
740 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
741 break;
742 case 0:
743 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
744 break;
745 default:
746 break;
747 /* Maybe the file disappeared before we could check it? */
749 action = FILE_ACTION_ADDED;
751 else if (ie->mask & IN_DELETE)
752 action = FILE_ACTION_REMOVED;
753 else
754 action = FILE_ACTION_MODIFIED;
757 * Work our way up the inode hierarchy
758 * extending the relative path as we go
759 * and notifying all recursive watches.
761 if (!prepend( &path, ie->name ))
762 return;
764 for (i = inode; i; i = i->parent)
766 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
767 if ((filter & dir->filter) && (i==inode || dir->subtree))
768 inotify_do_change_notify( dir, action, path );
770 if (!i->name || !prepend( &path, i->name ))
771 break;
774 free( path );
776 if (ie->mask & IN_DELETE)
778 i = inode_from_name( inode, ie->name );
779 if (i)
780 free_inode( i );
784 static void inotify_poll_event( struct fd *fd, int event )
786 int r, ofs, unix_fd;
787 char buffer[0x1000];
788 struct inotify_event *ie;
790 unix_fd = get_unix_fd( fd );
791 r = read( unix_fd, buffer, sizeof buffer );
792 if (r < 0)
794 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
795 return;
798 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
800 ie = (struct inotify_event*) &buffer[ofs];
801 if (!ie->len)
802 break;
803 ofs += offsetof( struct inotify_event, name[ie->len] );
804 if (ofs > r) break;
805 inotify_notify_all( ie );
809 static inline struct fd *create_inotify_fd( void )
811 int unix_fd;
813 unix_fd = inotify_init();
814 if (unix_fd<0)
815 return NULL;
816 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL );
819 static int map_flags( unsigned int filter )
821 unsigned int mask;
823 /* always watch these so we can track subdirectories in recursive watches */
824 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
826 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
827 mask |= IN_ATTRIB;
828 if (filter & FILE_NOTIFY_CHANGE_SIZE)
829 mask |= IN_MODIFY;
830 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
831 mask |= IN_MODIFY;
832 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
833 mask |= IN_ACCESS;
834 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
835 mask |= IN_ATTRIB;
837 return mask;
840 static int inotify_add_dir( char *path, unsigned int filter )
842 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
843 path, map_flags( filter ) );
844 if (wd != -1)
845 set_fd_events( inotify_fd, POLLIN );
846 return wd;
849 static int init_inotify( void )
851 int i;
853 if (inotify_fd)
854 return 1;
856 inotify_fd = create_inotify_fd();
857 if (!inotify_fd)
858 return 0;
860 for (i=0; i<HASH_SIZE; i++)
862 list_init( &inode_hash[i] );
863 list_init( &wd_hash[i] );
866 return 1;
869 static int inotify_adjust_changes( struct dir *dir )
871 unsigned int filter;
872 struct inode *inode;
873 struct stat st;
874 char path[32];
875 int wd, unix_fd;
877 if (!inotify_fd)
878 return 0;
880 unix_fd = get_unix_fd( dir->fd );
882 inode = dir->inode;
883 if (!inode)
885 /* check if this fd is already being watched */
886 if (-1 == fstat( unix_fd, &st ))
887 return 0;
889 inode = get_inode( st.st_dev, st.st_ino );
890 if (!inode)
891 inode = create_inode( st.st_dev, st.st_ino );
892 if (!inode)
893 return 0;
894 list_add_tail( &inode->dirs, &dir->in_entry );
895 dir->inode = inode;
898 filter = filter_from_inode( inode, 0 );
900 sprintf( path, "/proc/self/fd/%u", unix_fd );
901 wd = inotify_add_dir( path, filter );
902 if (wd == -1) return 0;
904 inode_set_wd( inode, wd );
906 return 1;
909 static char *get_basename( const char *link )
911 char *buffer, *name = NULL;
912 int r, n = 0x100;
914 while (1)
916 buffer = malloc( n );
917 if (!buffer) return NULL;
919 r = readlink( link, buffer, n );
920 if (r < 0)
921 break;
923 if (r < n)
925 name = buffer;
926 break;
928 free( buffer );
929 n *= 2;
932 if (name)
934 while (r > 0 && name[ r - 1 ] == '/' )
935 r--;
936 name[ r ] = 0;
938 name = strrchr( name, '/' );
939 if (name)
940 name = strdup( &name[1] );
943 free( buffer );
944 return name;
947 static int dir_add_to_existing_notify( struct dir *dir )
949 struct inode *inode, *parent;
950 unsigned int filter = 0;
951 struct stat st, st_new;
952 char link[35], *name;
953 int wd, unix_fd;
955 if (!inotify_fd)
956 return 0;
958 unix_fd = get_unix_fd( dir->fd );
960 /* check if it's in the list of inodes we want to watch */
961 if (-1 == fstat( unix_fd, &st_new ))
962 return 0;
963 inode = find_inode( st_new.st_dev, st_new.st_ino );
964 if (inode)
965 return 0;
967 /* lookup the parent */
968 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
969 if (-1 == stat( link, &st ))
970 return 0;
973 * If there's no parent, stop. We could keep going adding
974 * ../ to the path until we hit the root of the tree or
975 * find a recursively watched ancestor.
976 * Assume it's too expensive to search up the tree for now.
978 parent = find_inode( st.st_dev, st.st_ino );
979 if (!parent)
980 return 0;
982 if (parent->wd == -1)
983 return 0;
985 filter = filter_from_inode( parent, 1 );
986 if (!filter)
987 return 0;
989 sprintf( link, "/proc/self/fd/%u", unix_fd );
990 name = get_basename( link );
991 if (!name)
992 return 0;
993 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
994 free( name );
995 if (!inode)
996 return 0;
998 /* Couldn't find this inode at the start of the function, must be new */
999 assert( inode->wd == -1 );
1001 wd = inotify_add_dir( link, filter );
1002 if (wd != -1)
1003 inode_set_wd( inode, wd );
1005 return 1;
1008 #else
1010 static int init_inotify( void )
1012 return 0;
1015 static int inotify_adjust_changes( struct dir *dir )
1017 return 0;
1020 static void free_inode( struct inode *inode )
1022 assert( 0 );
1025 static int dir_add_to_existing_notify( struct dir *dir )
1027 return 0;
1030 #endif /* USE_INOTIFY */
1032 struct object *create_dir_obj( struct fd *fd )
1034 struct dir *dir;
1036 dir = alloc_object( &dir_ops );
1037 if (!dir)
1038 return NULL;
1040 list_init( &dir->change_records );
1041 dir->event = NULL;
1042 dir->filter = 0;
1043 dir->notified = 0;
1044 dir->signaled = 0;
1045 dir->want_data = 0;
1046 dir->inode = NULL;
1047 grab_object( fd );
1048 dir->fd = fd;
1049 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1051 dir_add_to_existing_notify( dir );
1053 return &dir->obj;
1056 /* enable change notifications for a directory */
1057 DECL_HANDLER(read_directory_changes)
1059 struct event *event = NULL;
1060 struct dir *dir;
1062 if (!req->filter)
1064 set_error(STATUS_INVALID_PARAMETER);
1065 return;
1068 dir = get_dir_obj( current->process, req->handle, 0 );
1069 if (!dir)
1070 return;
1072 /* possibly send changes through an event flag */
1073 if (req->async.event &&
1074 !(event = get_event_obj( current->process, req->async.event, EVENT_MODIFY_STATE )))
1075 goto end;
1077 /* discard the current data, and move onto the next event */
1078 if (dir->event) release_object( dir->event );
1079 dir->event = event;
1081 /* requests don't timeout */
1082 if (!fd_queue_async_timeout( dir->fd, &req->async, ASYNC_TYPE_WAIT, 0, NULL )) goto end;
1084 /* assign it once */
1085 if (!dir->filter)
1087 init_inotify();
1088 insert_change( dir );
1089 dir->filter = req->filter;
1090 dir->subtree = req->subtree;
1091 dir->want_data = req->want_data;
1094 /* remove any notifications */
1095 if (dir->signaled>0)
1096 dir->signaled--;
1098 /* if there's already a change in the queue, send it */
1099 if (!list_empty( &dir->change_records ))
1100 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1102 /* setup the real notification */
1103 if (!inotify_adjust_changes( dir ))
1104 dnotify_adjust_changes( dir );
1106 set_error(STATUS_PENDING);
1108 end:
1109 release_object( dir );
1112 DECL_HANDLER(read_change)
1114 struct change_record *record;
1115 struct dir *dir;
1117 dir = get_dir_obj( current->process, req->handle, 0 );
1118 if (!dir)
1119 return;
1121 if ((record = get_first_change_record( dir )) != NULL)
1123 reply->action = record->action;
1124 set_reply_data( record->name, record->len );
1125 free( record );
1127 else
1128 set_error( STATUS_NO_DATA_DETECTED );
1130 /* now signal it */
1131 dir->signaled++;
1132 dir_signal_changed( dir );
1134 release_object( dir );