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 */
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_q
; /* change readers */
157 struct list change_records
; /* data for the change */
158 struct list in_entry
; /* entry in the inode dirs list */
159 struct inode
*inode
; /* inode of the associated directory */
162 static struct fd
*dir_get_fd( struct object
*obj
);
163 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
);
164 static void dir_dump( struct object
*obj
, int verbose
);
165 static void dir_destroy( struct object
*obj
);
166 static int dir_signaled( struct object
*obj
, struct thread
*thread
);
168 static const struct object_ops dir_ops
=
170 sizeof(struct dir
), /* size */
172 add_queue
, /* add_queue */
173 remove_queue
, /* remove_queue */
174 dir_signaled
, /* signaled */
175 no_satisfied
, /* satisfied */
176 no_signal
, /* signal */
177 dir_get_fd
, /* get_fd */
178 dir_map_access
, /* map_access */
179 no_lookup_name
, /* lookup_name */
180 fd_close_handle
, /* close_handle */
181 dir_destroy
/* destroy */
184 static int dir_get_poll_events( struct fd
*fd
);
185 static int dir_get_info( struct fd
*fd
);
186 static void dir_cancel_async( struct fd
*fd
);
188 static const struct fd_ops dir_fd_ops
=
190 dir_get_poll_events
, /* get_poll_events */
191 default_poll_event
, /* poll_event */
192 no_flush
, /* flush */
193 dir_get_info
, /* get_file_info */
194 default_fd_queue_async
, /* queue_async */
195 dir_cancel_async
/* cancel_async */
198 static struct list change_list
= LIST_INIT(change_list
);
200 static void dnotify_adjust_changes( struct dir
*dir
)
202 #if defined(F_SETSIG) && defined(F_NOTIFY)
203 int fd
= get_unix_fd( dir
->fd
);
204 unsigned int filter
= dir
->filter
;
206 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
210 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
211 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
212 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
213 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
214 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
216 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
218 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
220 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
222 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
224 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
226 fcntl( fd
, F_NOTIFY
, val
);
230 /* insert change in the global list */
231 static inline void insert_change( struct dir
*dir
)
235 sigemptyset( &sigset
);
236 sigaddset( &sigset
, SIGIO
);
237 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
238 list_add_head( &change_list
, &dir
->entry
);
239 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
242 /* remove change from the global list */
243 static inline void remove_change( struct dir
*dir
)
247 sigemptyset( &sigset
);
248 sigaddset( &sigset
, SIGIO
);
249 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
250 list_remove( &dir
->entry
);
251 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
254 static void dir_dump( struct object
*obj
, int verbose
)
256 struct dir
*dir
= (struct dir
*)obj
;
257 assert( obj
->ops
== &dir_ops
);
258 fprintf( stderr
, "Dirfile fd=%p event=%p filter=%08x\n",
259 dir
->fd
, dir
->event
, dir
->filter
);
262 static int dir_signaled( struct object
*obj
, struct thread
*thread
)
264 struct dir
*dir
= (struct dir
*)obj
;
265 assert (obj
->ops
== &dir_ops
);
266 return (dir
->event
== NULL
) && dir
->signaled
;
269 /* enter here directly from SIGIO signal handler */
270 void do_change_notify( int unix_fd
)
274 /* FIXME: this is O(n) ... probably can be improved */
275 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
277 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
278 interlocked_xchg_add( &dir
->notified
, 1 );
283 static void dir_signal_changed( struct dir
*dir
)
286 set_event( dir
->event
);
288 wake_up( &dir
->obj
, 0 );
291 /* SIGIO callback, called synchronously with the poll loop */
292 void sigio_callback(void)
296 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
298 long count
= interlocked_xchg( &dir
->notified
, 0 );
301 dir
->signaled
+= count
;
302 if (dir
->signaled
== count
) /* was it 0? */
303 dir_signal_changed( dir
);
308 static struct fd
*dir_get_fd( struct object
*obj
)
310 struct dir
*dir
= (struct dir
*)obj
;
311 assert( obj
->ops
== &dir_ops
);
312 return (struct fd
*)grab_object( dir
->fd
);
315 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
)
317 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
318 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
319 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
320 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
321 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
324 static struct change_record
*get_first_change_record( struct dir
*dir
)
326 struct list
*ptr
= list_head( &dir
->change_records
);
327 if (!ptr
) return NULL
;
329 return LIST_ENTRY( ptr
, struct change_record
, entry
);
332 static void dir_destroy( struct object
*obj
)
334 struct change_record
*record
;
335 struct dir
*dir
= (struct dir
*)obj
;
336 assert (obj
->ops
== &dir_ops
);
339 remove_change( dir
);
343 list_remove( &dir
->in_entry
);
344 free_inode( dir
->inode
);
347 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
348 while ((record
= get_first_change_record( dir
))) free( record
);
352 set_event( dir
->event
);
353 release_object( dir
->event
);
355 release_object( dir
->fd
);
357 if (inotify_fd
&& list_empty( &change_list
))
359 release_object( inotify_fd
);
365 get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
367 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
370 static int dir_get_poll_events( struct fd
*fd
)
375 static int dir_get_info( struct fd
*fd
)
380 static void dir_cancel_async( struct fd
*fd
)
382 struct dir
*dir
= (struct dir
*) get_fd_user( fd
);
383 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
392 struct list ch_entry
; /* entry in the children list */
393 struct list children
; /* children of this inode */
394 struct inode
*parent
; /* parent of this inode */
395 struct list dirs
; /* directory handles watching this inode */
396 struct list ino_entry
; /* entry in the inode hash */
397 struct list wd_entry
; /* entry in the watch descriptor hash */
398 dev_t dev
; /* device number */
399 ino_t ino
; /* device's inode number */
400 int wd
; /* inotify's watch descriptor */
401 char *name
; /* basename name of the inode */
404 struct list inode_hash
[ HASH_SIZE
];
405 struct list wd_hash
[ HASH_SIZE
];
407 static int inotify_add_dir( char *path
, unsigned int filter
);
409 static struct inode
*inode_from_wd( int wd
)
411 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
414 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
421 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
423 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
426 static struct inode
*find_inode( dev_t dev
, ino_t ino
)
428 struct list
*bucket
= get_hash_list( dev
, ino
);
431 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
432 if (inode
->ino
== ino
&& inode
->dev
== dev
)
438 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
442 inode
= malloc( sizeof *inode
);
445 list_init( &inode
->children
);
446 list_init( &inode
->dirs
);
450 inode
->parent
= NULL
;
452 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
457 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
461 inode
= find_inode( dev
, ino
);
464 return create_inode( dev
, ino
);
467 static void inode_set_wd( struct inode
*inode
, int wd
)
470 list_remove( &inode
->wd_entry
);
472 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
475 static void inode_set_name( struct inode
*inode
, const char *name
)
478 inode
->name
= name
? strdup( name
) : NULL
;
481 static void free_inode( struct inode
*inode
)
483 int subtree
= 0, watches
= 0;
486 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
488 subtree
|= dir
->subtree
;
492 if (!subtree
&& !inode
->parent
)
494 struct inode
*tmp
, *next
;
495 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
,
496 struct inode
, ch_entry
)
498 assert( tmp
!= inode
);
499 assert( tmp
->parent
== inode
);
508 list_remove( &inode
->ch_entry
);
512 inotify_remove_watch( get_unix_fd( inotify_fd
), inode
->wd
);
513 list_remove( &inode
->wd_entry
);
515 list_remove( &inode
->ino_entry
);
521 static struct inode
*inode_add( struct inode
*parent
,
522 dev_t dev
, ino_t ino
, const char *name
)
526 inode
= get_inode( dev
, ino
);
532 list_add_tail( &parent
->children
, &inode
->ch_entry
);
533 inode
->parent
= parent
;
534 assert( inode
!= parent
);
536 inode_set_name( inode
, name
);
541 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
545 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
546 if (i
->name
&& !strcmp( i
->name
, name
))
551 static int inotify_get_poll_events( struct fd
*fd
);
552 static void inotify_poll_event( struct fd
*fd
, int event
);
553 static int inotify_get_info( struct fd
*fd
);
555 static const struct fd_ops inotify_fd_ops
=
557 inotify_get_poll_events
, /* get_poll_events */
558 inotify_poll_event
, /* poll_event */
559 no_flush
, /* flush */
560 inotify_get_info
, /* get_file_info */
561 default_fd_queue_async
, /* queue_async */
562 default_fd_cancel_async
, /* cancel_async */
565 static int inotify_get_poll_events( struct fd
*fd
)
570 static void inotify_do_change_notify( struct dir
*dir
, unsigned int action
,
571 const char *relpath
)
573 struct change_record
*record
;
575 assert( dir
->obj
.ops
== &dir_ops
);
579 size_t len
= strlen(relpath
);
580 record
= malloc( offsetof(struct change_record
, name
[len
]) );
584 record
->action
= action
;
585 memcpy( record
->name
, relpath
, len
);
588 list_add_tail( &dir
->change_records
, &record
->entry
);
591 if (!list_empty( &dir
->change_q
))
592 async_terminate_head( &dir
->change_q
, STATUS_ALERTED
);
595 static unsigned int filter_from_event( struct inotify_event
*ie
)
597 unsigned int filter
= 0;
599 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
600 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
601 if (ie
->mask
& IN_MODIFY
)
602 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
;
603 if (ie
->mask
& IN_ATTRIB
)
604 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
605 if (ie
->mask
& IN_ACCESS
)
606 filter
|= FILE_NOTIFY_CHANGE_LAST_ACCESS
;
607 if (ie
->mask
& IN_CREATE
)
608 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
613 /* scan up the parent directories for watches */
614 static unsigned int filter_from_inode( struct inode
*inode
, int is_parent
)
616 unsigned int filter
= 0;
619 /* combine filters from parents watching subtrees */
622 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
623 if (dir
->subtree
|| !is_parent
)
624 filter
|= dir
->filter
;
626 inode
= inode
->parent
;
632 static char *inode_get_path( struct inode
*inode
, int sz
)
641 head
= list_head( &inode
->dirs
);
644 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
645 path
= malloc ( 32 + sz
);
647 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
654 len
= strlen( inode
->name
);
655 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
659 strcat( path
, inode
->name
);
665 static int inode_check_dir( struct inode
*parent
, const char *name
)
673 path
= inode_get_path( parent
, strlen(name
) );
677 strcat( path
, name
);
679 r
= stat( path
, &st
);
682 if (!S_ISDIR(st
.st_mode
))
690 filter
= filter_from_inode( parent
, 1 );
694 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
695 if (!inode
|| inode
->wd
!= -1)
698 wd
= inotify_add_dir( path
, filter
);
700 inode_set_wd( inode
, wd
);
709 static int prepend( char **path
, const char *segment
)
714 extra
= strlen( segment
) + 1;
717 int len
= strlen( *path
) + 1;
718 p
= realloc( *path
, len
+ extra
);
720 memmove( &p
[ extra
], p
, len
);
721 p
[ extra
- 1 ] = '/';
722 memcpy( p
, segment
, extra
- 1 );
728 memcpy( p
, segment
, extra
);
736 static void inotify_notify_all( struct inotify_event
*ie
)
738 unsigned int filter
, action
;
739 struct inode
*inode
, *i
;
743 inode
= inode_from_wd( ie
->wd
);
746 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
750 filter
= filter_from_event( ie
);
752 if (ie
->mask
& IN_CREATE
)
754 switch (inode_check_dir( inode
, ie
->name
))
757 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
760 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
764 /* Maybe the file disappeared before we could check it? */
766 action
= FILE_ACTION_ADDED
;
768 else if (ie
->mask
& IN_DELETE
)
769 action
= FILE_ACTION_REMOVED
;
771 action
= FILE_ACTION_MODIFIED
;
774 * Work our way up the inode hierarchy
775 * extending the relative path as we go
776 * and notifying all recursive watches.
778 if (!prepend( &path
, ie
->name
))
781 for (i
= inode
; i
; i
= i
->parent
)
783 LIST_FOR_EACH_ENTRY( dir
, &i
->dirs
, struct dir
, in_entry
)
784 if ((filter
& dir
->filter
) && (i
==inode
|| dir
->subtree
))
785 inotify_do_change_notify( dir
, action
, path
);
787 if (!i
->name
|| !prepend( &path
, i
->name
))
793 if (ie
->mask
& IN_DELETE
)
795 i
= inode_from_name( inode
, ie
->name
);
801 static void inotify_poll_event( struct fd
*fd
, int event
)
805 struct inotify_event
*ie
;
807 unix_fd
= get_unix_fd( fd
);
808 r
= read( unix_fd
, buffer
, sizeof buffer
);
811 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
815 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
817 ie
= (struct inotify_event
*) &buffer
[ofs
];
820 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
822 inotify_notify_all( ie
);
826 static int inotify_get_info( struct fd
*fd
)
831 static inline struct fd
*create_inotify_fd( void )
835 unix_fd
= inotify_init();
838 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
);
841 static int map_flags( unsigned int filter
)
845 /* always watch these so we can track subdirectories in recursive watches */
846 mask
= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
848 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
850 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
852 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
854 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
856 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
862 static int inotify_add_dir( char *path
, unsigned int filter
)
864 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
865 path
, map_flags( filter
) );
867 set_fd_events( inotify_fd
, POLLIN
);
871 static int init_inotify( void )
878 inotify_fd
= create_inotify_fd();
882 for (i
=0; i
<HASH_SIZE
; i
++)
884 list_init( &inode_hash
[i
] );
885 list_init( &wd_hash
[i
] );
891 static int inotify_adjust_changes( struct dir
*dir
)
902 unix_fd
= get_unix_fd( dir
->fd
);
907 /* check if this fd is already being watched */
908 if (-1 == fstat( unix_fd
, &st
))
911 inode
= get_inode( st
.st_dev
, st
.st_ino
);
913 inode
= create_inode( st
.st_dev
, st
.st_ino
);
916 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
920 filter
= filter_from_inode( inode
, 0 );
922 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
923 wd
= inotify_add_dir( path
, filter
);
924 if (wd
== -1) return 0;
926 inode_set_wd( inode
, wd
);
931 static char *get_basename( const char *link
)
933 char *buffer
, *name
= NULL
;
938 buffer
= malloc( n
);
939 if (!buffer
) return NULL
;
941 r
= readlink( link
, buffer
, n
);
956 while (r
> 0 && name
[ r
- 1 ] == '/' )
960 name
= strrchr( name
, '/' );
962 name
= strdup( &name
[1] );
969 static int dir_add_to_existing_notify( struct dir
*dir
)
971 struct inode
*inode
, *parent
;
972 unsigned int filter
= 0;
973 struct stat st
, st_new
;
974 char link
[35], *name
;
980 unix_fd
= get_unix_fd( dir
->fd
);
982 /* check if it's in the list of inodes we want to watch */
983 if (-1 == fstat( unix_fd
, &st_new
))
985 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
989 /* lookup the parent */
990 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
991 if (-1 == stat( link
, &st
))
995 * If there's no parent, stop. We could keep going adding
996 * ../ to the path until we hit the root of the tree or
997 * find a recursively watched ancestor.
998 * Assume it's too expensive to search up the tree for now.
1000 parent
= find_inode( st
.st_dev
, st
.st_ino
);
1004 if (parent
->wd
== -1)
1007 filter
= filter_from_inode( parent
, 1 );
1011 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
1012 name
= get_basename( link
);
1015 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
1020 /* Couldn't find this inode at the start of the function, must be new */
1021 assert( inode
->wd
== -1 );
1023 wd
= inotify_add_dir( link
, filter
);
1025 inode_set_wd( inode
, wd
);
1032 static int init_inotify( void )
1037 static int inotify_adjust_changes( struct dir
*dir
)
1042 static void free_inode( struct inode
*inode
)
1047 static int dir_add_to_existing_notify( struct dir
*dir
)
1052 #endif /* USE_INOTIFY */
1054 struct object
*create_dir_obj( struct fd
*fd
)
1058 dir
= alloc_object( &dir_ops
);
1062 list_init( &dir
->change_q
);
1063 list_init( &dir
->change_records
);
1072 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1074 dir_add_to_existing_notify( dir
);
1079 /* enable change notifications for a directory */
1080 DECL_HANDLER(read_directory_changes
)
1082 struct event
*event
= NULL
;
1087 set_error(STATUS_INVALID_PARAMETER
);
1091 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1095 /* possibly send changes through an event flag */
1098 event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
1103 /* discard the current data, and move onto the next event */
1104 if (dir
->event
) release_object( dir
->event
);
1107 /* requests don't timeout */
1108 if ( req
->io_apc
&& !create_async( current
, NULL
, &dir
->change_q
,
1109 req
->io_apc
, req
->io_user
, req
->io_sb
))
1112 /* assign it once */
1116 insert_change( dir
);
1117 dir
->filter
= req
->filter
;
1118 dir
->subtree
= req
->subtree
;
1119 dir
->want_data
= req
->want_data
;
1122 /* remove any notifications */
1123 if (dir
->signaled
>0)
1126 /* clear the event */
1128 reset_event( event
);
1130 /* if there's already a change in the queue, send it */
1131 if (!list_empty( &dir
->change_q
) &&
1132 !list_empty( &dir
->change_records
))
1133 async_terminate_head( &dir
->change_q
, STATUS_ALERTED
);
1135 /* setup the real notification */
1136 if (!inotify_adjust_changes( dir
))
1137 dnotify_adjust_changes( dir
);
1139 set_error(STATUS_PENDING
);
1142 release_object( dir
);
1145 DECL_HANDLER(read_change
)
1147 struct change_record
*record
;
1150 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1154 if ((record
= get_first_change_record( dir
)) != NULL
)
1156 reply
->action
= record
->action
;
1157 set_reply_data( record
->name
, record
->len
);
1161 set_error( STATUS_NO_DATA_DETECTED
);
1165 dir_signal_changed( dir
);
1167 release_object( dir
);