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
56 #define DN_ACCESS 0x00000001 /* File accessed */
57 #define DN_MODIFY 0x00000002 /* File modified */
58 #define DN_CREATE 0x00000004 /* File created */
59 #define DN_DELETE 0x00000008 /* File removed */
60 #define DN_RENAME 0x00000010 /* File renamed */
61 #define DN_ATTRIB 0x00000020 /* File changed attributes */
62 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
68 #ifdef HAVE_SYS_INOTIFY_H
69 #include <sys/inotify.h>
71 #elif defined(__linux__) && defined(__i386__)
73 #define SYS_inotify_init 291
74 #define SYS_inotify_add_watch 292
75 #define SYS_inotify_rm_watch 293
77 struct inotify_event
{
85 #define IN_ACCESS 0x00000001
86 #define IN_MODIFY 0x00000002
87 #define IN_ATTRIB 0x00000004
88 #define IN_CLOSE_WRITE 0x00000008
89 #define IN_CLOSE_NOWRITE 0x00000010
90 #define IN_OPEN 0x00000020
91 #define IN_MOVED_FROM 0x00000040
92 #define IN_MOVED_TO 0x00000080
93 #define IN_CREATE 0x00000100
94 #define IN_DELETE 0x00000200
95 #define IN_DELETE_SELF 0x00000400
97 #define IN_ISDIR 0x40000000
99 static inline int inotify_init( void )
101 return syscall( SYS_inotify_init
);
104 static inline int inotify_add_watch( int fd
, const char *name
, unsigned int mask
)
106 return syscall( SYS_inotify_add_watch
, fd
, name
, mask
);
109 static inline int inotify_rm_watch( int fd
, int wd
)
111 return syscall( SYS_inotify_rm_watch
, fd
, wd
);
120 static void free_inode( struct inode
*inode
);
122 static struct fd
*inotify_fd
;
124 struct change_record
{
133 struct object obj
; /* object header */
134 struct fd
*fd
; /* file descriptor to the directory */
135 mode_t mode
; /* file stat.st_mode */
136 uid_t uid
; /* file stat.st_uid */
137 struct list entry
; /* entry in global change notifications list */
138 unsigned int filter
; /* notification filter */
139 int notified
; /* SIGIO counter */
140 int want_data
; /* return change data */
141 int subtree
; /* do we want to watch subdirectories? */
142 struct list change_records
; /* data for the change */
143 struct list in_entry
; /* entry in the inode dirs list */
144 struct inode
*inode
; /* inode of the associated directory */
147 static struct fd
*dir_get_fd( struct object
*obj
);
148 static struct security_descriptor
*dir_get_sd( struct object
*obj
);
149 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
150 unsigned int set_info
);
151 static void dir_dump( struct object
*obj
, int verbose
);
152 static void dir_destroy( struct object
*obj
);
154 static const struct object_ops dir_ops
=
156 sizeof(struct dir
), /* size */
158 no_get_type
, /* get_type */
159 add_queue
, /* add_queue */
160 remove_queue
, /* remove_queue */
161 default_fd_signaled
, /* signaled */
162 no_satisfied
, /* satisfied */
163 no_signal
, /* signal */
164 dir_get_fd
, /* get_fd */
165 default_fd_map_access
, /* map_access */
166 dir_get_sd
, /* get_sd */
167 dir_set_sd
, /* set_sd */
168 no_lookup_name
, /* lookup_name */
169 no_open_file
, /* open_file */
170 fd_close_handle
, /* close_handle */
171 dir_destroy
/* destroy */
174 static int dir_get_poll_events( struct fd
*fd
);
175 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
);
177 static const struct fd_ops dir_fd_ops
=
179 dir_get_poll_events
, /* get_poll_events */
180 default_poll_event
, /* poll_event */
181 no_flush
, /* flush */
182 dir_get_fd_type
, /* get_fd_type */
183 default_fd_ioctl
, /* ioctl */
184 default_fd_queue_async
, /* queue_async */
185 default_fd_reselect_async
, /* reselect_async */
186 default_fd_cancel_async
/* cancel_async */
189 static struct list change_list
= LIST_INIT(change_list
);
191 static void dnotify_adjust_changes( struct dir
*dir
)
193 #if defined(F_SETSIG) && defined(F_NOTIFY)
194 int fd
= get_unix_fd( dir
->fd
);
195 unsigned int filter
= dir
->filter
;
197 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
201 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
202 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
203 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
204 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
205 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
207 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
209 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
211 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
213 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
215 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
217 fcntl( fd
, F_NOTIFY
, val
);
221 /* insert change in the global list */
222 static inline void insert_change( struct dir
*dir
)
226 sigemptyset( &sigset
);
227 sigaddset( &sigset
, SIGIO
);
228 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
229 list_add_head( &change_list
, &dir
->entry
);
230 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
233 /* remove change from the global list */
234 static inline void remove_change( struct dir
*dir
)
238 sigemptyset( &sigset
);
239 sigaddset( &sigset
, SIGIO
);
240 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
241 list_remove( &dir
->entry
);
242 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
245 static void dir_dump( struct object
*obj
, int verbose
)
247 struct dir
*dir
= (struct dir
*)obj
;
248 assert( obj
->ops
== &dir_ops
);
249 fprintf( stderr
, "Dirfile fd=%p filter=%08x\n", dir
->fd
, dir
->filter
);
252 /* enter here directly from SIGIO signal handler */
253 void do_change_notify( int unix_fd
)
257 /* FIXME: this is O(n) ... probably can be improved */
258 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
260 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
261 interlocked_xchg_add( &dir
->notified
, 1 );
266 /* SIGIO callback, called synchronously with the poll loop */
267 void sigio_callback(void)
271 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
273 if (interlocked_xchg( &dir
->notified
, 0 ))
274 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_NOTIFY_ENUM_DIR
);
278 static struct fd
*dir_get_fd( struct object
*obj
)
280 struct dir
*dir
= (struct dir
*)obj
;
281 assert( obj
->ops
== &dir_ops
);
282 return (struct fd
*)grab_object( dir
->fd
);
285 static int get_dir_unix_fd( struct dir
*dir
)
287 return get_unix_fd( dir
->fd
);
290 static struct security_descriptor
*dir_get_sd( struct object
*obj
)
292 struct dir
*dir
= (struct dir
*)obj
;
295 struct security_descriptor
*sd
;
296 assert( obj
->ops
== &dir_ops
);
298 unix_fd
= get_dir_unix_fd( dir
);
300 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
303 /* mode and uid the same? if so, no need to re-generate security descriptor */
305 (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (dir
->mode
& (S_IRWXU
|S_IRWXO
)) &&
306 (st
.st_uid
== dir
->uid
))
309 sd
= mode_to_sd( st
.st_mode
,
310 security_unix_uid_to_sid( st
.st_uid
),
311 token_get_primary_group( current
->process
->token
));
312 if (!sd
) return obj
->sd
;
314 dir
->mode
= st
.st_mode
;
315 dir
->uid
= st
.st_uid
;
321 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
322 unsigned int set_info
)
324 struct dir
*dir
= (struct dir
*)obj
;
330 assert( obj
->ops
== &dir_ops
);
332 unix_fd
= get_dir_unix_fd( dir
);
334 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
336 if (set_info
& OWNER_SECURITY_INFORMATION
)
338 owner
= sd_get_owner( sd
);
341 set_error( STATUS_INVALID_SECURITY_DESCR
);
344 if (!obj
->sd
|| !security_equal_sid( owner
, sd_get_owner( obj
->sd
) ))
346 /* FIXME: get Unix uid and call fchown */
350 owner
= sd_get_owner( obj
->sd
);
352 owner
= token_get_user( current
->process
->token
);
354 if (set_info
& DACL_SECURITY_INFORMATION
)
356 /* keep the bits that we don't map to access rights in the ACL */
357 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
358 mode
|= sd_to_mode( sd
, owner
);
360 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
369 static struct change_record
*get_first_change_record( struct dir
*dir
)
371 struct list
*ptr
= list_head( &dir
->change_records
);
372 if (!ptr
) return NULL
;
374 return LIST_ENTRY( ptr
, struct change_record
, entry
);
377 static void dir_destroy( struct object
*obj
)
379 struct change_record
*record
;
380 struct dir
*dir
= (struct dir
*)obj
;
381 assert (obj
->ops
== &dir_ops
);
384 remove_change( dir
);
388 list_remove( &dir
->in_entry
);
389 free_inode( dir
->inode
);
392 while ((record
= get_first_change_record( dir
))) free( record
);
394 release_object( dir
->fd
);
396 if (inotify_fd
&& list_empty( &change_list
))
398 release_object( inotify_fd
);
403 struct dir
*get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
405 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
408 static int dir_get_poll_events( struct fd
*fd
)
413 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
)
423 struct list ch_entry
; /* entry in the children list */
424 struct list children
; /* children of this inode */
425 struct inode
*parent
; /* parent of this inode */
426 struct list dirs
; /* directory handles watching this inode */
427 struct list ino_entry
; /* entry in the inode hash */
428 struct list wd_entry
; /* entry in the watch descriptor hash */
429 dev_t dev
; /* device number */
430 ino_t ino
; /* device's inode number */
431 int wd
; /* inotify's watch descriptor */
432 char *name
; /* basename name of the inode */
435 static struct list inode_hash
[ HASH_SIZE
];
436 static struct list wd_hash
[ HASH_SIZE
];
438 static int inotify_add_dir( char *path
, unsigned int filter
);
440 static struct inode
*inode_from_wd( int wd
)
442 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
445 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
452 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
454 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
457 static struct inode
*find_inode( dev_t dev
, ino_t ino
)
459 struct list
*bucket
= get_hash_list( dev
, ino
);
462 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
463 if (inode
->ino
== ino
&& inode
->dev
== dev
)
469 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
473 inode
= malloc( sizeof *inode
);
476 list_init( &inode
->children
);
477 list_init( &inode
->dirs
);
481 inode
->parent
= NULL
;
483 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
488 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
492 inode
= find_inode( dev
, ino
);
495 return create_inode( dev
, ino
);
498 static void inode_set_wd( struct inode
*inode
, int wd
)
501 list_remove( &inode
->wd_entry
);
503 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
506 static void inode_set_name( struct inode
*inode
, const char *name
)
509 inode
->name
= name
? strdup( name
) : NULL
;
512 static void free_inode( struct inode
*inode
)
514 int subtree
= 0, watches
= 0;
515 struct inode
*tmp
, *next
;
518 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
520 subtree
|= dir
->subtree
;
524 if (!subtree
&& !inode
->parent
)
526 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
,
527 struct inode
, ch_entry
)
529 assert( tmp
!= inode
);
530 assert( tmp
->parent
== inode
);
539 list_remove( &inode
->ch_entry
);
541 /* disconnect remaining children from the parent */
542 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
, struct inode
, ch_entry
)
544 list_remove( &tmp
->ch_entry
);
550 inotify_rm_watch( get_unix_fd( inotify_fd
), inode
->wd
);
551 list_remove( &inode
->wd_entry
);
553 list_remove( &inode
->ino_entry
);
559 static struct inode
*inode_add( struct inode
*parent
,
560 dev_t dev
, ino_t ino
, const char *name
)
564 inode
= get_inode( dev
, ino
);
570 list_add_tail( &parent
->children
, &inode
->ch_entry
);
571 inode
->parent
= parent
;
572 assert( inode
!= parent
);
574 inode_set_name( inode
, name
);
579 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
583 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
584 if (i
->name
&& !strcmp( i
->name
, name
))
589 static int inotify_get_poll_events( struct fd
*fd
);
590 static void inotify_poll_event( struct fd
*fd
, int event
);
592 static const struct fd_ops inotify_fd_ops
=
594 inotify_get_poll_events
, /* get_poll_events */
595 inotify_poll_event
, /* poll_event */
597 NULL
, /* get_fd_type */
599 NULL
, /* queue_async */
600 NULL
, /* reselect_async */
601 NULL
, /* cancel_async */
604 static int inotify_get_poll_events( struct fd
*fd
)
609 static void inotify_do_change_notify( struct dir
*dir
, unsigned int action
,
610 const char *relpath
)
612 struct change_record
*record
;
614 assert( dir
->obj
.ops
== &dir_ops
);
618 size_t len
= strlen(relpath
);
619 record
= malloc( offsetof(struct change_record
, name
[len
]) );
623 record
->action
= action
;
624 memcpy( record
->name
, relpath
, len
);
627 list_add_tail( &dir
->change_records
, &record
->entry
);
630 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
633 static unsigned int filter_from_event( struct inotify_event
*ie
)
635 unsigned int filter
= 0;
637 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
638 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
639 if (ie
->mask
& IN_MODIFY
)
640 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
;
641 if (ie
->mask
& IN_ATTRIB
)
642 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
643 if (ie
->mask
& IN_ACCESS
)
644 filter
|= FILE_NOTIFY_CHANGE_LAST_ACCESS
;
645 if (ie
->mask
& IN_CREATE
)
646 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
648 if (ie
->mask
& IN_ISDIR
)
649 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
651 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
656 /* scan up the parent directories for watches */
657 static unsigned int filter_from_inode( struct inode
*inode
, int is_parent
)
659 unsigned int filter
= 0;
662 /* combine filters from parents watching subtrees */
665 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
666 if (dir
->subtree
|| !is_parent
)
667 filter
|= dir
->filter
;
669 inode
= inode
->parent
;
675 static char *inode_get_path( struct inode
*inode
, int sz
)
684 head
= list_head( &inode
->dirs
);
687 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
688 path
= malloc ( 32 + sz
);
690 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
697 len
= strlen( inode
->name
);
698 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
702 strcat( path
, inode
->name
);
708 static void inode_check_dir( struct inode
*parent
, const char *name
)
716 path
= inode_get_path( parent
, strlen(name
) );
720 strcat( path
, name
);
722 if (stat( path
, &st
) < 0)
725 filter
= filter_from_inode( parent
, 1 );
729 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
730 if (!inode
|| inode
->wd
!= -1)
733 wd
= inotify_add_dir( path
, filter
);
735 inode_set_wd( inode
, wd
);
743 static int prepend( char **path
, const char *segment
)
748 extra
= strlen( segment
) + 1;
751 int len
= strlen( *path
) + 1;
752 p
= realloc( *path
, len
+ extra
);
754 memmove( &p
[ extra
], p
, len
);
755 p
[ extra
- 1 ] = '/';
756 memcpy( p
, segment
, extra
- 1 );
762 memcpy( p
, segment
, extra
);
770 static void inotify_notify_all( struct inotify_event
*ie
)
772 unsigned int filter
, action
;
773 struct inode
*inode
, *i
;
777 inode
= inode_from_wd( ie
->wd
);
780 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
784 filter
= filter_from_event( ie
);
786 if (ie
->mask
& IN_CREATE
)
788 if (ie
->mask
& IN_ISDIR
)
789 inode_check_dir( inode
, ie
->name
);
791 action
= FILE_ACTION_ADDED
;
793 else if (ie
->mask
& IN_DELETE
)
794 action
= FILE_ACTION_REMOVED
;
795 else if (ie
->mask
& IN_MOVED_FROM
)
796 action
= FILE_ACTION_RENAMED_OLD_NAME
;
797 else if (ie
->mask
& IN_MOVED_TO
)
798 action
= FILE_ACTION_RENAMED_NEW_NAME
;
800 action
= FILE_ACTION_MODIFIED
;
803 * Work our way up the inode hierarchy
804 * extending the relative path as we go
805 * and notifying all recursive watches.
807 if (!prepend( &path
, ie
->name
))
810 for (i
= inode
; i
; i
= i
->parent
)
812 LIST_FOR_EACH_ENTRY( dir
, &i
->dirs
, struct dir
, in_entry
)
813 if ((filter
& dir
->filter
) && (i
==inode
|| dir
->subtree
))
814 inotify_do_change_notify( dir
, action
, path
);
816 if (!i
->name
|| !prepend( &path
, i
->name
))
822 if (ie
->mask
& IN_DELETE
)
824 i
= inode_from_name( inode
, ie
->name
);
830 static void inotify_poll_event( struct fd
*fd
, int event
)
834 struct inotify_event
*ie
;
836 unix_fd
= get_unix_fd( fd
);
837 r
= read( unix_fd
, buffer
, sizeof buffer
);
840 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
844 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
846 ie
= (struct inotify_event
*) &buffer
[ofs
];
849 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
851 inotify_notify_all( ie
);
855 static inline struct fd
*create_inotify_fd( void )
859 unix_fd
= inotify_init();
862 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
, 0 );
865 static int map_flags( unsigned int filter
)
869 /* always watch these so we can track subdirectories in recursive watches */
870 mask
= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
872 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
874 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
876 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
878 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
880 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
886 static int inotify_add_dir( char *path
, unsigned int filter
)
888 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
889 path
, map_flags( filter
) );
891 set_fd_events( inotify_fd
, POLLIN
);
895 static int init_inotify( void )
902 inotify_fd
= create_inotify_fd();
906 for (i
=0; i
<HASH_SIZE
; i
++)
908 list_init( &inode_hash
[i
] );
909 list_init( &wd_hash
[i
] );
915 static int inotify_adjust_changes( struct dir
*dir
)
926 unix_fd
= get_unix_fd( dir
->fd
);
931 /* check if this fd is already being watched */
932 if (-1 == fstat( unix_fd
, &st
))
935 inode
= get_inode( st
.st_dev
, st
.st_ino
);
937 inode
= create_inode( st
.st_dev
, st
.st_ino
);
940 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
944 filter
= filter_from_inode( inode
, 0 );
946 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
947 wd
= inotify_add_dir( path
, filter
);
948 if (wd
== -1) return 0;
950 inode_set_wd( inode
, wd
);
955 static char *get_basename( const char *link
)
957 char *buffer
, *name
= NULL
;
962 buffer
= malloc( n
);
963 if (!buffer
) return NULL
;
965 r
= readlink( link
, buffer
, n
);
980 while (r
> 0 && name
[ r
- 1 ] == '/' )
984 name
= strrchr( name
, '/' );
986 name
= strdup( &name
[1] );
993 static int dir_add_to_existing_notify( struct dir
*dir
)
995 struct inode
*inode
, *parent
;
996 unsigned int filter
= 0;
997 struct stat st
, st_new
;
998 char link
[35], *name
;
1004 unix_fd
= get_unix_fd( dir
->fd
);
1006 /* check if it's in the list of inodes we want to watch */
1007 if (-1 == fstat( unix_fd
, &st_new
))
1009 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
1013 /* lookup the parent */
1014 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
1015 if (-1 == stat( link
, &st
))
1019 * If there's no parent, stop. We could keep going adding
1020 * ../ to the path until we hit the root of the tree or
1021 * find a recursively watched ancestor.
1022 * Assume it's too expensive to search up the tree for now.
1024 parent
= find_inode( st
.st_dev
, st
.st_ino
);
1028 if (parent
->wd
== -1)
1031 filter
= filter_from_inode( parent
, 1 );
1035 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
1036 name
= get_basename( link
);
1039 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
1044 /* Couldn't find this inode at the start of the function, must be new */
1045 assert( inode
->wd
== -1 );
1047 wd
= inotify_add_dir( link
, filter
);
1049 inode_set_wd( inode
, wd
);
1056 static int init_inotify( void )
1061 static int inotify_adjust_changes( struct dir
*dir
)
1066 static void free_inode( struct inode
*inode
)
1071 static int dir_add_to_existing_notify( struct dir
*dir
)
1076 #endif /* USE_INOTIFY */
1078 struct object
*create_dir_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
1082 dir
= alloc_object( &dir_ops
);
1086 list_init( &dir
->change_records
);
1094 dir
->uid
= ~(uid_t
)0;
1095 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1097 dir_add_to_existing_notify( dir
);
1102 /* enable change notifications for a directory */
1103 DECL_HANDLER(read_directory_changes
)
1106 struct async
*async
;
1110 set_error(STATUS_INVALID_PARAMETER
);
1114 dir
= get_dir_obj( current
->process
, req
->async
.handle
, 0 );
1118 /* requests don't timeout */
1119 if (!(async
= fd_queue_async( dir
->fd
, &req
->async
, ASYNC_TYPE_WAIT
))) goto end
;
1121 /* assign it once */
1125 insert_change( dir
);
1126 dir
->filter
= req
->filter
;
1127 dir
->subtree
= req
->subtree
;
1128 dir
->want_data
= req
->want_data
;
1131 /* if there's already a change in the queue, send it */
1132 if (!list_empty( &dir
->change_records
))
1133 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
1135 /* setup the real notification */
1136 if (!inotify_adjust_changes( dir
))
1137 dnotify_adjust_changes( dir
);
1139 release_object( async
);
1140 set_error(STATUS_PENDING
);
1143 release_object( dir
);
1146 DECL_HANDLER(read_change
)
1148 struct change_record
*record
;
1151 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1155 if ((record
= get_first_change_record( dir
)) != NULL
)
1157 reply
->action
= record
->action
;
1158 set_reply_data( record
->name
, record
->len
);
1162 set_error( STATUS_NO_DATA_DETECTED
);
1164 release_object( dir
);