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_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 */
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
;
205 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
209 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
210 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
211 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
212 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
213 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
215 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
217 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
219 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
221 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
223 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
225 fcntl( fd
, F_NOTIFY
, val
);
229 /* insert change in the global list */
230 static inline void insert_change( struct dir
*dir
)
234 sigemptyset( &sigset
);
235 sigaddset( &sigset
, SIGIO
);
236 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
237 list_add_head( &change_list
, &dir
->entry
);
238 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
241 /* remove change from the global list */
242 static inline void remove_change( struct dir
*dir
)
246 sigemptyset( &sigset
);
247 sigaddset( &sigset
, SIGIO
);
248 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
249 list_remove( &dir
->entry
);
250 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
253 static void dir_dump( struct object
*obj
, int verbose
)
255 struct dir
*dir
= (struct dir
*)obj
;
256 assert( obj
->ops
== &dir_ops
);
257 fprintf( stderr
, "Dirfile fd=%p 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
)
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 );
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)
292 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
294 long count
= interlocked_xchg( &dir
->notified
, 0 );
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
;
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
);
335 remove_change( dir
);
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
);
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
)
366 static enum server_fd_type
dir_get_info( struct fd
*fd
, int *flags
)
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
];
399 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
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
);
416 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
417 if (inode
->ino
== ino
&& inode
->dev
== dev
)
423 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
427 inode
= malloc( sizeof *inode
);
430 list_init( &inode
->children
);
431 list_init( &inode
->dirs
);
435 inode
->parent
= NULL
;
437 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
442 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
446 inode
= find_inode( dev
, ino
);
449 return create_inode( dev
, ino
);
452 static void inode_set_wd( struct inode
*inode
, int wd
)
455 list_remove( &inode
->wd_entry
);
457 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
460 static void inode_set_name( struct inode
*inode
, const char *name
)
463 inode
->name
= name
? strdup( name
) : NULL
;
466 static void free_inode( struct inode
*inode
)
468 int subtree
= 0, watches
= 0;
471 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
473 subtree
|= dir
->subtree
;
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
);
493 list_remove( &inode
->ch_entry
);
497 inotify_remove_watch( get_unix_fd( inotify_fd
), inode
->wd
);
498 list_remove( &inode
->wd_entry
);
500 list_remove( &inode
->ino_entry
);
506 static struct inode
*inode_add( struct inode
*parent
,
507 dev_t dev
, ino_t ino
, const char *name
)
511 inode
= get_inode( dev
, ino
);
517 list_add_tail( &parent
->children
, &inode
->ch_entry
);
518 inode
->parent
= parent
;
519 assert( inode
!= parent
);
521 inode_set_name( inode
, name
);
526 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
530 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
531 if (i
->name
&& !strcmp( i
->name
, name
))
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
)
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
);
563 size_t len
= strlen(relpath
);
564 record
= malloc( offsetof(struct change_record
, name
[len
]) );
568 record
->action
= action
;
569 memcpy( record
->name
, relpath
, len
);
572 list_add_tail( &dir
->change_records
, &record
->entry
);
575 fd_async_terminate_head( 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
;
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;
602 /* combine filters from parents watching subtrees */
605 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
606 if (dir
->subtree
|| !is_parent
)
607 filter
|= dir
->filter
;
609 inode
= inode
->parent
;
615 static char *inode_get_path( struct inode
*inode
, int sz
)
624 head
= list_head( &inode
->dirs
);
627 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
628 path
= malloc ( 32 + sz
);
630 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
637 len
= strlen( inode
->name
);
638 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
642 strcat( path
, inode
->name
);
648 static int inode_check_dir( struct inode
*parent
, const char *name
)
656 path
= inode_get_path( parent
, strlen(name
) );
660 strcat( path
, name
);
662 r
= stat( path
, &st
);
665 if (!S_ISDIR(st
.st_mode
))
673 filter
= filter_from_inode( parent
, 1 );
677 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
678 if (!inode
|| inode
->wd
!= -1)
681 wd
= inotify_add_dir( path
, filter
);
683 inode_set_wd( inode
, wd
);
692 static int prepend( char **path
, const char *segment
)
697 extra
= strlen( segment
) + 1;
700 int len
= strlen( *path
) + 1;
701 p
= realloc( *path
, len
+ extra
);
703 memmove( &p
[ extra
], p
, len
);
704 p
[ extra
- 1 ] = '/';
705 memcpy( p
, segment
, extra
- 1 );
711 memcpy( p
, segment
, extra
);
719 static void inotify_notify_all( struct inotify_event
*ie
)
721 unsigned int filter
, action
;
722 struct inode
*inode
, *i
;
726 inode
= inode_from_wd( ie
->wd
);
729 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
733 filter
= filter_from_event( ie
);
735 if (ie
->mask
& IN_CREATE
)
737 switch (inode_check_dir( inode
, ie
->name
))
740 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
743 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
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
;
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
))
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
))
776 if (ie
->mask
& IN_DELETE
)
778 i
= inode_from_name( inode
, ie
->name
);
784 static void inotify_poll_event( struct fd
*fd
, int event
)
788 struct inotify_event
*ie
;
790 unix_fd
= get_unix_fd( fd
);
791 r
= read( unix_fd
, buffer
, sizeof buffer
);
794 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
798 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
800 ie
= (struct inotify_event
*) &buffer
[ofs
];
803 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
805 inotify_notify_all( ie
);
809 static inline struct fd
*create_inotify_fd( void )
813 unix_fd
= inotify_init();
816 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
);
819 static int map_flags( unsigned int filter
)
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
)
828 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
830 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
832 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
834 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
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
) );
845 set_fd_events( inotify_fd
, POLLIN
);
849 static int init_inotify( void )
856 inotify_fd
= create_inotify_fd();
860 for (i
=0; i
<HASH_SIZE
; i
++)
862 list_init( &inode_hash
[i
] );
863 list_init( &wd_hash
[i
] );
869 static int inotify_adjust_changes( struct dir
*dir
)
880 unix_fd
= get_unix_fd( dir
->fd
);
885 /* check if this fd is already being watched */
886 if (-1 == fstat( unix_fd
, &st
))
889 inode
= get_inode( st
.st_dev
, st
.st_ino
);
891 inode
= create_inode( st
.st_dev
, st
.st_ino
);
894 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
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
);
909 static char *get_basename( const char *link
)
911 char *buffer
, *name
= NULL
;
916 buffer
= malloc( n
);
917 if (!buffer
) return NULL
;
919 r
= readlink( link
, buffer
, n
);
934 while (r
> 0 && name
[ r
- 1 ] == '/' )
938 name
= strrchr( name
, '/' );
940 name
= strdup( &name
[1] );
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
;
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
))
963 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
967 /* lookup the parent */
968 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
969 if (-1 == stat( link
, &st
))
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
);
982 if (parent
->wd
== -1)
985 filter
= filter_from_inode( parent
, 1 );
989 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
990 name
= get_basename( link
);
993 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
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
);
1003 inode_set_wd( inode
, wd
);
1010 static int init_inotify( void )
1015 static int inotify_adjust_changes( struct dir
*dir
)
1020 static void free_inode( struct inode
*inode
)
1025 static int dir_add_to_existing_notify( struct dir
*dir
)
1030 #endif /* USE_INOTIFY */
1032 struct object
*create_dir_obj( struct fd
*fd
)
1036 dir
= alloc_object( &dir_ops
);
1040 list_init( &dir
->change_records
);
1049 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1051 dir_add_to_existing_notify( dir
);
1056 /* enable change notifications for a directory */
1057 DECL_HANDLER(read_directory_changes
)
1059 struct event
*event
= NULL
;
1064 set_error(STATUS_INVALID_PARAMETER
);
1068 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
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
)))
1077 /* discard the current data, and move onto the next event */
1078 if (dir
->event
) release_object( dir
->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 */
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)
1098 /* if there's already a change in the queue, send it */
1099 if (!list_empty( &dir
->change_records
))
1100 fd_async_terminate_head( 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
);
1109 release_object( dir
);
1112 DECL_HANDLER(read_change
)
1114 struct change_record
*record
;
1117 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1121 if ((record
= get_first_change_record( dir
)) != NULL
)
1123 reply
->action
= record
->action
;
1124 set_reply_data( record
->name
, record
->len
);
1128 set_error( STATUS_NO_DATA_DETECTED
);
1132 dir_signal_changed( dir
);
1134 release_object( dir
);