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>
37 #define WIN32_NO_STATUS
53 #define DN_ACCESS 0x00000001 /* File accessed */
54 #define DN_MODIFY 0x00000002 /* File modified */
55 #define DN_CREATE 0x00000004 /* File created */
56 #define DN_DELETE 0x00000008 /* File removed */
57 #define DN_RENAME 0x00000010 /* File renamed */
58 #define DN_ATTRIB 0x00000020 /* File changed attributes */
59 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
65 #ifdef HAVE_SYS_INOTIFY_H
66 #include <sys/inotify.h>
68 #elif defined(__linux__) && defined(__i386__)
70 #define SYS_inotify_init 291
71 #define SYS_inotify_add_watch 292
72 #define SYS_inotify_rm_watch 293
74 struct inotify_event
{
82 #define IN_ACCESS 0x00000001
83 #define IN_MODIFY 0x00000002
84 #define IN_ATTRIB 0x00000004
85 #define IN_CLOSE_WRITE 0x00000008
86 #define IN_CLOSE_NOWRITE 0x00000010
87 #define IN_OPEN 0x00000020
88 #define IN_MOVED_FROM 0x00000040
89 #define IN_MOVED_TO 0x00000080
90 #define IN_CREATE 0x00000100
91 #define IN_DELETE 0x00000200
92 #define IN_DELETE_SELF 0x00000400
94 #define IN_ISDIR 0x40000000
96 static inline int inotify_init( void )
98 return syscall( SYS_inotify_init
);
101 static inline int inotify_add_watch( int fd
, const char *name
, unsigned int mask
)
103 return syscall( SYS_inotify_add_watch
, fd
, name
, mask
);
106 static inline int inotify_rm_watch( int fd
, int wd
)
108 return syscall( SYS_inotify_rm_watch
, fd
, wd
);
117 static void free_inode( struct inode
*inode
);
119 static struct fd
*inotify_fd
;
121 struct change_record
{
124 struct filesystem_event event
;
129 struct object obj
; /* object header */
130 struct fd
*fd
; /* file descriptor to the directory */
131 mode_t mode
; /* file stat.st_mode */
132 uid_t uid
; /* file stat.st_uid */
133 struct list entry
; /* entry in global change notifications list */
134 unsigned int filter
; /* notification filter */
135 int notified
; /* SIGIO counter */
136 int want_data
; /* return change data */
137 int subtree
; /* do we want to watch subdirectories? */
138 struct list change_records
; /* data for the change */
139 struct list in_entry
; /* entry in the inode dirs list */
140 struct inode
*inode
; /* inode of the associated directory */
143 static struct fd
*dir_get_fd( struct object
*obj
);
144 static struct security_descriptor
*dir_get_sd( struct object
*obj
);
145 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
146 unsigned int set_info
);
147 static void dir_dump( struct object
*obj
, int verbose
);
148 static void dir_destroy( struct object
*obj
);
150 static const struct object_ops dir_ops
=
152 sizeof(struct dir
), /* size */
154 no_get_type
, /* get_type */
155 add_queue
, /* add_queue */
156 remove_queue
, /* remove_queue */
157 default_fd_signaled
, /* signaled */
158 no_satisfied
, /* satisfied */
159 no_signal
, /* signal */
160 dir_get_fd
, /* get_fd */
161 default_fd_map_access
, /* map_access */
162 dir_get_sd
, /* get_sd */
163 dir_set_sd
, /* set_sd */
164 no_lookup_name
, /* lookup_name */
165 no_open_file
, /* open_file */
166 fd_close_handle
, /* close_handle */
167 dir_destroy
/* destroy */
170 static int dir_get_poll_events( struct fd
*fd
);
171 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
);
173 static const struct fd_ops dir_fd_ops
=
175 dir_get_poll_events
, /* get_poll_events */
176 default_poll_event
, /* poll_event */
177 no_flush
, /* flush */
178 dir_get_fd_type
, /* get_fd_type */
179 default_fd_ioctl
, /* ioctl */
180 default_fd_queue_async
, /* queue_async */
181 default_fd_reselect_async
, /* reselect_async */
182 default_fd_cancel_async
/* cancel_async */
185 static struct list change_list
= LIST_INIT(change_list
);
187 static void dnotify_adjust_changes( struct dir
*dir
)
189 #if defined(F_SETSIG) && defined(F_NOTIFY)
190 int fd
= get_unix_fd( dir
->fd
);
191 unsigned int filter
= dir
->filter
;
193 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
197 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
198 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
199 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
200 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
201 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
203 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
205 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
207 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
209 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
211 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
213 fcntl( fd
, F_NOTIFY
, val
);
217 /* insert change in the global list */
218 static inline void insert_change( struct dir
*dir
)
222 sigemptyset( &sigset
);
223 sigaddset( &sigset
, SIGIO
);
224 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
225 list_add_head( &change_list
, &dir
->entry
);
226 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
229 /* remove change from the global list */
230 static inline void remove_change( struct dir
*dir
)
234 sigemptyset( &sigset
);
235 sigaddset( &sigset
, SIGIO
);
236 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
237 list_remove( &dir
->entry
);
238 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
241 static void dir_dump( struct object
*obj
, int verbose
)
243 struct dir
*dir
= (struct dir
*)obj
;
244 assert( obj
->ops
== &dir_ops
);
245 fprintf( stderr
, "Dirfile fd=%p filter=%08x\n", dir
->fd
, dir
->filter
);
248 /* enter here directly from SIGIO signal handler */
249 void do_change_notify( int unix_fd
)
253 /* FIXME: this is O(n) ... probably can be improved */
254 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
256 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
257 interlocked_xchg_add( &dir
->notified
, 1 );
262 /* SIGIO callback, called synchronously with the poll loop */
263 void sigio_callback(void)
267 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
269 if (interlocked_xchg( &dir
->notified
, 0 ))
270 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_NOTIFY_ENUM_DIR
);
274 static struct fd
*dir_get_fd( struct object
*obj
)
276 struct dir
*dir
= (struct dir
*)obj
;
277 assert( obj
->ops
== &dir_ops
);
278 return (struct fd
*)grab_object( dir
->fd
);
281 static int get_dir_unix_fd( struct dir
*dir
)
283 return get_unix_fd( dir
->fd
);
286 static struct security_descriptor
*dir_get_sd( struct object
*obj
)
288 struct dir
*dir
= (struct dir
*)obj
;
291 struct security_descriptor
*sd
;
292 assert( obj
->ops
== &dir_ops
);
294 unix_fd
= get_dir_unix_fd( dir
);
296 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
299 /* mode and uid the same? if so, no need to re-generate security descriptor */
301 (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (dir
->mode
& (S_IRWXU
|S_IRWXO
)) &&
302 (st
.st_uid
== dir
->uid
))
305 sd
= mode_to_sd( st
.st_mode
,
306 security_unix_uid_to_sid( st
.st_uid
),
307 token_get_primary_group( current
->process
->token
));
308 if (!sd
) return obj
->sd
;
310 dir
->mode
= st
.st_mode
;
311 dir
->uid
= st
.st_uid
;
317 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
318 unsigned int set_info
)
320 struct dir
*dir
= (struct dir
*)obj
;
326 assert( obj
->ops
== &dir_ops
);
328 unix_fd
= get_dir_unix_fd( dir
);
330 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
332 if (set_info
& OWNER_SECURITY_INFORMATION
)
334 owner
= sd_get_owner( sd
);
337 set_error( STATUS_INVALID_SECURITY_DESCR
);
340 if (!obj
->sd
|| !security_equal_sid( owner
, sd_get_owner( obj
->sd
) ))
342 /* FIXME: get Unix uid and call fchown */
346 owner
= sd_get_owner( obj
->sd
);
348 owner
= token_get_user( current
->process
->token
);
350 if (set_info
& DACL_SECURITY_INFORMATION
)
352 /* keep the bits that we don't map to access rights in the ACL */
353 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
354 mode
|= sd_to_mode( sd
, owner
);
356 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
365 static struct change_record
*get_first_change_record( struct dir
*dir
)
367 struct list
*ptr
= list_head( &dir
->change_records
);
368 if (!ptr
) return NULL
;
370 return LIST_ENTRY( ptr
, struct change_record
, entry
);
373 static void dir_destroy( struct object
*obj
)
375 struct change_record
*record
;
376 struct dir
*dir
= (struct dir
*)obj
;
377 assert (obj
->ops
== &dir_ops
);
380 remove_change( dir
);
384 list_remove( &dir
->in_entry
);
385 free_inode( dir
->inode
);
388 while ((record
= get_first_change_record( dir
))) free( record
);
390 release_object( dir
->fd
);
392 if (inotify_fd
&& list_empty( &change_list
))
394 release_object( inotify_fd
);
399 struct dir
*get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
401 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
404 static int dir_get_poll_events( struct fd
*fd
)
409 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
)
419 struct list ch_entry
; /* entry in the children list */
420 struct list children
; /* children of this inode */
421 struct inode
*parent
; /* parent of this inode */
422 struct list dirs
; /* directory handles watching this inode */
423 struct list ino_entry
; /* entry in the inode hash */
424 struct list wd_entry
; /* entry in the watch descriptor hash */
425 dev_t dev
; /* device number */
426 ino_t ino
; /* device's inode number */
427 int wd
; /* inotify's watch descriptor */
428 char *name
; /* basename name of the inode */
431 static struct list inode_hash
[ HASH_SIZE
];
432 static struct list wd_hash
[ HASH_SIZE
];
434 static int inotify_add_dir( char *path
, unsigned int filter
);
436 static struct inode
*inode_from_wd( int wd
)
438 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
441 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
448 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
450 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
453 static struct inode
*find_inode( dev_t dev
, ino_t ino
)
455 struct list
*bucket
= get_hash_list( dev
, ino
);
458 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
459 if (inode
->ino
== ino
&& inode
->dev
== dev
)
465 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
469 inode
= malloc( sizeof *inode
);
472 list_init( &inode
->children
);
473 list_init( &inode
->dirs
);
477 inode
->parent
= NULL
;
479 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
484 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
488 inode
= find_inode( dev
, ino
);
491 return create_inode( dev
, ino
);
494 static void inode_set_wd( struct inode
*inode
, int wd
)
497 list_remove( &inode
->wd_entry
);
499 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
502 static void inode_set_name( struct inode
*inode
, const char *name
)
505 inode
->name
= name
? strdup( name
) : NULL
;
508 static void free_inode( struct inode
*inode
)
510 int subtree
= 0, watches
= 0;
511 struct inode
*tmp
, *next
;
514 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
516 subtree
|= dir
->subtree
;
520 if (!subtree
&& !inode
->parent
)
522 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
,
523 struct inode
, ch_entry
)
525 assert( tmp
!= inode
);
526 assert( tmp
->parent
== inode
);
535 list_remove( &inode
->ch_entry
);
537 /* disconnect remaining children from the parent */
538 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
, struct inode
, ch_entry
)
540 list_remove( &tmp
->ch_entry
);
546 inotify_rm_watch( get_unix_fd( inotify_fd
), inode
->wd
);
547 list_remove( &inode
->wd_entry
);
549 list_remove( &inode
->ino_entry
);
555 static struct inode
*inode_add( struct inode
*parent
,
556 dev_t dev
, ino_t ino
, const char *name
)
560 inode
= get_inode( dev
, ino
);
566 list_add_tail( &parent
->children
, &inode
->ch_entry
);
567 inode
->parent
= parent
;
568 assert( inode
!= parent
);
570 inode_set_name( inode
, name
);
575 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
579 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
580 if (i
->name
&& !strcmp( i
->name
, name
))
585 static int inotify_get_poll_events( struct fd
*fd
);
586 static void inotify_poll_event( struct fd
*fd
, int event
);
588 static const struct fd_ops inotify_fd_ops
=
590 inotify_get_poll_events
, /* get_poll_events */
591 inotify_poll_event
, /* poll_event */
593 NULL
, /* get_fd_type */
595 NULL
, /* queue_async */
596 NULL
, /* reselect_async */
597 NULL
, /* cancel_async */
600 static int inotify_get_poll_events( struct fd
*fd
)
605 static void inotify_do_change_notify( struct dir
*dir
, unsigned int action
,
606 unsigned int cookie
, const char *relpath
)
608 struct change_record
*record
;
610 assert( dir
->obj
.ops
== &dir_ops
);
614 size_t len
= strlen(relpath
);
615 record
= malloc( offsetof(struct change_record
, event
.name
[len
]) );
619 record
->cookie
= cookie
;
620 record
->event
.action
= action
;
621 memcpy( record
->event
.name
, relpath
, len
);
622 record
->event
.len
= len
;
624 list_add_tail( &dir
->change_records
, &record
->entry
);
627 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
630 static unsigned int filter_from_event( struct inotify_event
*ie
)
632 unsigned int filter
= 0;
634 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
635 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
636 if (ie
->mask
& IN_MODIFY
)
637 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
;
638 if (ie
->mask
& IN_ATTRIB
)
639 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
640 if (ie
->mask
& IN_ACCESS
)
641 filter
|= FILE_NOTIFY_CHANGE_LAST_ACCESS
;
642 if (ie
->mask
& IN_CREATE
)
643 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
645 if (ie
->mask
& IN_ISDIR
)
646 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
648 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
653 /* scan up the parent directories for watches */
654 static unsigned int filter_from_inode( struct inode
*inode
, int is_parent
)
656 unsigned int filter
= 0;
659 /* combine filters from parents watching subtrees */
662 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
663 if (dir
->subtree
|| !is_parent
)
664 filter
|= dir
->filter
;
666 inode
= inode
->parent
;
672 static char *inode_get_path( struct inode
*inode
, int sz
)
681 head
= list_head( &inode
->dirs
);
684 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
685 path
= malloc ( 32 + sz
);
687 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
694 len
= strlen( inode
->name
);
695 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
699 strcat( path
, inode
->name
);
705 static void inode_check_dir( struct inode
*parent
, const char *name
)
713 path
= inode_get_path( parent
, strlen(name
) );
717 strcat( path
, name
);
719 if (stat( path
, &st
) < 0)
722 filter
= filter_from_inode( parent
, 1 );
726 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
727 if (!inode
|| inode
->wd
!= -1)
730 wd
= inotify_add_dir( path
, filter
);
732 inode_set_wd( inode
, wd
);
740 static int prepend( char **path
, const char *segment
)
745 extra
= strlen( segment
) + 1;
748 int len
= strlen( *path
) + 1;
749 p
= realloc( *path
, len
+ extra
);
751 memmove( &p
[ extra
], p
, len
);
752 p
[ extra
- 1 ] = '/';
753 memcpy( p
, segment
, extra
- 1 );
759 memcpy( p
, segment
, extra
);
767 static void inotify_notify_all( struct inotify_event
*ie
)
769 unsigned int filter
, action
;
770 struct inode
*inode
, *i
;
774 inode
= inode_from_wd( ie
->wd
);
777 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
781 filter
= filter_from_event( ie
);
783 if (ie
->mask
& IN_CREATE
)
785 if (ie
->mask
& IN_ISDIR
)
786 inode_check_dir( inode
, ie
->name
);
788 action
= FILE_ACTION_ADDED
;
790 else if (ie
->mask
& IN_DELETE
)
791 action
= FILE_ACTION_REMOVED
;
792 else if (ie
->mask
& IN_MOVED_FROM
)
793 action
= FILE_ACTION_RENAMED_OLD_NAME
;
794 else if (ie
->mask
& IN_MOVED_TO
)
795 action
= FILE_ACTION_RENAMED_NEW_NAME
;
797 action
= FILE_ACTION_MODIFIED
;
800 * Work our way up the inode hierarchy
801 * extending the relative path as we go
802 * and notifying all recursive watches.
804 if (!prepend( &path
, ie
->name
))
807 for (i
= inode
; i
; i
= i
->parent
)
809 LIST_FOR_EACH_ENTRY( dir
, &i
->dirs
, struct dir
, in_entry
)
810 if ((filter
& dir
->filter
) && (i
==inode
|| dir
->subtree
))
811 inotify_do_change_notify( dir
, action
, ie
->cookie
, path
);
813 if (!i
->name
|| !prepend( &path
, i
->name
))
819 if (ie
->mask
& IN_DELETE
)
821 i
= inode_from_name( inode
, ie
->name
);
827 static void inotify_poll_event( struct fd
*fd
, int event
)
831 struct inotify_event
*ie
;
833 unix_fd
= get_unix_fd( fd
);
834 r
= read( unix_fd
, buffer
, sizeof buffer
);
837 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
841 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
843 ie
= (struct inotify_event
*) &buffer
[ofs
];
846 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
848 inotify_notify_all( ie
);
852 static inline struct fd
*create_inotify_fd( void )
856 unix_fd
= inotify_init();
859 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
, 0 );
862 static int map_flags( unsigned int filter
)
866 /* always watch these so we can track subdirectories in recursive watches */
867 mask
= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
869 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
871 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
873 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
875 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
877 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
883 static int inotify_add_dir( char *path
, unsigned int filter
)
885 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
886 path
, map_flags( filter
) );
888 set_fd_events( inotify_fd
, POLLIN
);
892 static int init_inotify( void )
899 inotify_fd
= create_inotify_fd();
903 for (i
=0; i
<HASH_SIZE
; i
++)
905 list_init( &inode_hash
[i
] );
906 list_init( &wd_hash
[i
] );
912 static int inotify_adjust_changes( struct dir
*dir
)
923 unix_fd
= get_unix_fd( dir
->fd
);
928 /* check if this fd is already being watched */
929 if (-1 == fstat( unix_fd
, &st
))
932 inode
= get_inode( st
.st_dev
, st
.st_ino
);
934 inode
= create_inode( st
.st_dev
, st
.st_ino
);
937 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
941 filter
= filter_from_inode( inode
, 0 );
943 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
944 wd
= inotify_add_dir( path
, filter
);
945 if (wd
== -1) return 0;
947 inode_set_wd( inode
, wd
);
952 static char *get_basename( const char *link
)
954 char *buffer
, *name
= NULL
;
959 buffer
= malloc( n
);
960 if (!buffer
) return NULL
;
962 r
= readlink( link
, buffer
, n
);
977 while (r
> 0 && name
[ r
- 1 ] == '/' )
981 name
= strrchr( name
, '/' );
983 name
= strdup( &name
[1] );
990 static int dir_add_to_existing_notify( struct dir
*dir
)
992 struct inode
*inode
, *parent
;
993 unsigned int filter
= 0;
994 struct stat st
, st_new
;
995 char link
[35], *name
;
1001 unix_fd
= get_unix_fd( dir
->fd
);
1003 /* check if it's in the list of inodes we want to watch */
1004 if (-1 == fstat( unix_fd
, &st_new
))
1006 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
1010 /* lookup the parent */
1011 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
1012 if (-1 == stat( link
, &st
))
1016 * If there's no parent, stop. We could keep going adding
1017 * ../ to the path until we hit the root of the tree or
1018 * find a recursively watched ancestor.
1019 * Assume it's too expensive to search up the tree for now.
1021 parent
= find_inode( st
.st_dev
, st
.st_ino
);
1025 if (parent
->wd
== -1)
1028 filter
= filter_from_inode( parent
, 1 );
1032 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
1033 name
= get_basename( link
);
1036 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
1041 /* Couldn't find this inode at the start of the function, must be new */
1042 assert( inode
->wd
== -1 );
1044 wd
= inotify_add_dir( link
, filter
);
1046 inode_set_wd( inode
, wd
);
1053 static int init_inotify( void )
1058 static int inotify_adjust_changes( struct dir
*dir
)
1063 static void free_inode( struct inode
*inode
)
1068 static int dir_add_to_existing_notify( struct dir
*dir
)
1073 #endif /* USE_INOTIFY */
1075 struct object
*create_dir_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
1079 dir
= alloc_object( &dir_ops
);
1083 list_init( &dir
->change_records
);
1091 dir
->uid
= ~(uid_t
)0;
1092 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1094 dir_add_to_existing_notify( dir
);
1099 /* enable change notifications for a directory */
1100 DECL_HANDLER(read_directory_changes
)
1103 struct async
*async
;
1107 set_error(STATUS_INVALID_PARAMETER
);
1111 dir
= get_dir_obj( current
->process
, req
->async
.handle
, 0 );
1115 /* requests don't timeout */
1116 if (!(async
= fd_queue_async( dir
->fd
, &req
->async
, ASYNC_TYPE_WAIT
))) goto end
;
1118 /* assign it once */
1122 insert_change( dir
);
1123 dir
->filter
= req
->filter
;
1124 dir
->subtree
= req
->subtree
;
1125 dir
->want_data
= req
->want_data
;
1128 /* if there's already a change in the queue, send it */
1129 if (!list_empty( &dir
->change_records
))
1130 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
1132 /* setup the real notification */
1133 if (!inotify_adjust_changes( dir
))
1134 dnotify_adjust_changes( dir
);
1136 release_object( async
);
1137 set_error(STATUS_PENDING
);
1140 release_object( dir
);
1143 DECL_HANDLER(read_change
)
1145 struct change_record
*record
, *next
;
1151 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1155 list_init( &events
);
1156 list_move_tail( &events
, &dir
->change_records
);
1157 release_object( dir
);
1159 if (list_empty( &events
))
1161 set_error( STATUS_NO_DATA_DETECTED
);
1165 LIST_FOR_EACH_ENTRY( record
, &events
, struct change_record
, entry
)
1167 size
+= (offsetof(struct filesystem_event
, name
[record
->event
.len
])
1168 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1171 if (size
> get_reply_max_size())
1172 set_error( STATUS_BUFFER_TOO_SMALL
);
1173 else if ((data
= mem_alloc( size
)) != NULL
)
1176 LIST_FOR_EACH_ENTRY( record
, &events
, struct change_record
, entry
)
1178 data_size_t len
= offsetof( struct filesystem_event
, name
[record
->event
.len
] );
1180 /* FIXME: rename events are sometimes reported as delete/create */
1181 if (record
->event
.action
== FILE_ACTION_RENAMED_OLD_NAME
)
1183 struct list
*elem
= list_next( &events
, &record
->entry
);
1185 next
= LIST_ENTRY(elem
, struct change_record
, entry
);
1187 if (elem
&& next
->cookie
== record
->cookie
)
1190 record
->event
.action
= FILE_ACTION_REMOVED
;
1192 else if (record
->event
.action
== FILE_ACTION_RENAMED_NEW_NAME
&& record
->cookie
)
1193 record
->event
.action
= FILE_ACTION_ADDED
;
1195 memcpy( event
, &record
->event
, len
);
1197 if (len
% sizeof(int))
1199 memset( event
, 0, sizeof(int) - len
% sizeof(int) );
1200 event
+= sizeof(int) - len
% sizeof(int);
1203 set_reply_data_ptr( data
, size
);
1206 LIST_FOR_EACH_ENTRY_SAFE( record
, next
, &events
, struct change_record
, entry
)
1208 list_remove( &record
->entry
);