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 static inline int inotify_init( void )
100 __asm__
__volatile__( "int $0x80"
102 : "0" (SYS_inotify_init
));
103 if (ret
<0) { errno
= -ret
; ret
= -1; }
107 static inline int inotify_add_watch( int fd
, const char *name
, unsigned int mask
)
110 __asm__
__volatile__( "pushl %%ebx;\n\t"
114 : "=a" (ret
) : "0" (SYS_inotify_add_watch
),
115 "r" (fd
), "c" (name
), "d" (mask
) );
116 if (ret
<0) { errno
= -ret
; ret
= -1; }
120 static inline int inotify_rm_watch( int fd
, int wd
)
123 __asm__
__volatile__( "pushl %%ebx;\n\t"
127 : "=a" (ret
) : "0" (SYS_inotify_rm_watch
),
128 "r" (fd
), "c" (wd
) );
129 if (ret
<0) { errno
= -ret
; ret
= -1; }
139 static void free_inode( struct inode
*inode
);
141 static struct fd
*inotify_fd
;
143 struct change_record
{
152 struct object obj
; /* object header */
153 struct fd
*fd
; /* file descriptor to the directory */
154 mode_t mode
; /* file stat.st_mode */
155 uid_t uid
; /* file stat.st_uid */
156 struct list entry
; /* entry in global change notifications list */
157 unsigned int filter
; /* notification filter */
158 int notified
; /* SIGIO counter */
159 int want_data
; /* return change data */
160 int subtree
; /* do we want to watch subdirectories? */
161 struct list change_records
; /* data for the change */
162 struct list in_entry
; /* entry in the inode dirs list */
163 struct inode
*inode
; /* inode of the associated directory */
166 static struct fd
*dir_get_fd( struct object
*obj
);
167 static struct security_descriptor
*dir_get_sd( struct object
*obj
);
168 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
169 unsigned int set_info
);
170 static void dir_dump( struct object
*obj
, int verbose
);
171 static void dir_destroy( struct object
*obj
);
173 static const struct object_ops dir_ops
=
175 sizeof(struct dir
), /* size */
177 no_get_type
, /* get_type */
178 add_queue
, /* add_queue */
179 remove_queue
, /* remove_queue */
180 default_fd_signaled
, /* signaled */
181 no_satisfied
, /* satisfied */
182 no_signal
, /* signal */
183 dir_get_fd
, /* get_fd */
184 default_fd_map_access
, /* map_access */
185 dir_get_sd
, /* get_sd */
186 dir_set_sd
, /* set_sd */
187 no_lookup_name
, /* lookup_name */
188 no_open_file
, /* open_file */
189 fd_close_handle
, /* close_handle */
190 dir_destroy
/* destroy */
193 static int dir_get_poll_events( struct fd
*fd
);
194 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
);
196 static const struct fd_ops dir_fd_ops
=
198 dir_get_poll_events
, /* get_poll_events */
199 default_poll_event
, /* poll_event */
200 no_flush
, /* flush */
201 dir_get_fd_type
, /* get_fd_type */
202 default_fd_ioctl
, /* ioctl */
203 default_fd_queue_async
, /* queue_async */
204 default_fd_reselect_async
, /* reselect_async */
205 default_fd_cancel_async
/* cancel_async */
208 static struct list change_list
= LIST_INIT(change_list
);
210 static void dnotify_adjust_changes( struct dir
*dir
)
212 #if defined(F_SETSIG) && defined(F_NOTIFY)
213 int fd
= get_unix_fd( dir
->fd
);
214 unsigned int filter
= dir
->filter
;
216 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
220 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
221 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
222 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
223 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
224 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
226 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
228 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
230 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
232 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
234 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
236 fcntl( fd
, F_NOTIFY
, val
);
240 /* insert change in the global list */
241 static inline void insert_change( struct dir
*dir
)
245 sigemptyset( &sigset
);
246 sigaddset( &sigset
, SIGIO
);
247 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
248 list_add_head( &change_list
, &dir
->entry
);
249 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
252 /* remove change from the global list */
253 static inline void remove_change( struct dir
*dir
)
257 sigemptyset( &sigset
);
258 sigaddset( &sigset
, SIGIO
);
259 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
260 list_remove( &dir
->entry
);
261 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
264 static void dir_dump( struct object
*obj
, int verbose
)
266 struct dir
*dir
= (struct dir
*)obj
;
267 assert( obj
->ops
== &dir_ops
);
268 fprintf( stderr
, "Dirfile fd=%p filter=%08x\n", dir
->fd
, dir
->filter
);
271 /* enter here directly from SIGIO signal handler */
272 void do_change_notify( int unix_fd
)
276 /* FIXME: this is O(n) ... probably can be improved */
277 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
279 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
280 interlocked_xchg_add( &dir
->notified
, 1 );
285 /* SIGIO callback, called synchronously with the poll loop */
286 void sigio_callback(void)
290 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
292 if (interlocked_xchg( &dir
->notified
, 0 ))
293 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_NOTIFY_ENUM_DIR
);
297 static struct fd
*dir_get_fd( struct object
*obj
)
299 struct dir
*dir
= (struct dir
*)obj
;
300 assert( obj
->ops
== &dir_ops
);
301 return (struct fd
*)grab_object( dir
->fd
);
304 static int get_dir_unix_fd( struct dir
*dir
)
306 return get_unix_fd( dir
->fd
);
309 static struct security_descriptor
*dir_get_sd( struct object
*obj
)
311 struct dir
*dir
= (struct dir
*)obj
;
314 struct security_descriptor
*sd
;
315 assert( obj
->ops
== &dir_ops
);
317 unix_fd
= get_dir_unix_fd( dir
);
319 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
322 /* mode and uid the same? if so, no need to re-generate security descriptor */
324 (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (dir
->mode
& (S_IRWXU
|S_IRWXO
)) &&
325 (st
.st_uid
== dir
->uid
))
328 sd
= mode_to_sd( st
.st_mode
,
329 security_unix_uid_to_sid( st
.st_uid
),
330 token_get_primary_group( current
->process
->token
));
331 if (!sd
) return obj
->sd
;
333 dir
->mode
= st
.st_mode
;
334 dir
->uid
= st
.st_uid
;
340 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
341 unsigned int set_info
)
343 struct dir
*dir
= (struct dir
*)obj
;
349 assert( obj
->ops
== &dir_ops
);
351 unix_fd
= get_dir_unix_fd( dir
);
353 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
355 if (set_info
& OWNER_SECURITY_INFORMATION
)
357 owner
= sd_get_owner( sd
);
360 set_error( STATUS_INVALID_SECURITY_DESCR
);
363 if (!obj
->sd
|| !security_equal_sid( owner
, sd_get_owner( obj
->sd
) ))
365 /* FIXME: get Unix uid and call fchown */
369 owner
= sd_get_owner( obj
->sd
);
371 owner
= token_get_user( current
->process
->token
);
373 if (set_info
& DACL_SECURITY_INFORMATION
)
375 /* keep the bits that we don't map to access rights in the ACL */
376 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXG
);
377 mode
|= sd_to_mode( sd
, owner
);
379 if (st
.st_mode
!= mode
&& fchmod( unix_fd
, mode
) == -1)
388 static struct change_record
*get_first_change_record( struct dir
*dir
)
390 struct list
*ptr
= list_head( &dir
->change_records
);
391 if (!ptr
) return NULL
;
393 return LIST_ENTRY( ptr
, struct change_record
, entry
);
396 static void dir_destroy( struct object
*obj
)
398 struct change_record
*record
;
399 struct dir
*dir
= (struct dir
*)obj
;
400 assert (obj
->ops
== &dir_ops
);
403 remove_change( dir
);
407 list_remove( &dir
->in_entry
);
408 free_inode( dir
->inode
);
411 while ((record
= get_first_change_record( dir
))) free( record
);
413 release_object( dir
->fd
);
415 if (inotify_fd
&& list_empty( &change_list
))
417 release_object( inotify_fd
);
422 struct dir
*get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
424 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
427 static int dir_get_poll_events( struct fd
*fd
)
432 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
)
442 struct list ch_entry
; /* entry in the children list */
443 struct list children
; /* children of this inode */
444 struct inode
*parent
; /* parent of this inode */
445 struct list dirs
; /* directory handles watching this inode */
446 struct list ino_entry
; /* entry in the inode hash */
447 struct list wd_entry
; /* entry in the watch descriptor hash */
448 dev_t dev
; /* device number */
449 ino_t ino
; /* device's inode number */
450 int wd
; /* inotify's watch descriptor */
451 char *name
; /* basename name of the inode */
454 struct list inode_hash
[ HASH_SIZE
];
455 struct list wd_hash
[ HASH_SIZE
];
457 static int inotify_add_dir( char *path
, unsigned int filter
);
459 static struct inode
*inode_from_wd( int wd
)
461 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
464 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
471 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
473 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
476 static struct inode
*find_inode( dev_t dev
, ino_t ino
)
478 struct list
*bucket
= get_hash_list( dev
, ino
);
481 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
482 if (inode
->ino
== ino
&& inode
->dev
== dev
)
488 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
492 inode
= malloc( sizeof *inode
);
495 list_init( &inode
->children
);
496 list_init( &inode
->dirs
);
500 inode
->parent
= NULL
;
502 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
507 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
511 inode
= find_inode( dev
, ino
);
514 return create_inode( dev
, ino
);
517 static void inode_set_wd( struct inode
*inode
, int wd
)
520 list_remove( &inode
->wd_entry
);
522 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
525 static void inode_set_name( struct inode
*inode
, const char *name
)
528 inode
->name
= name
? strdup( name
) : NULL
;
531 static void free_inode( struct inode
*inode
)
533 int subtree
= 0, watches
= 0;
534 struct inode
*tmp
, *next
;
537 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
539 subtree
|= dir
->subtree
;
543 if (!subtree
&& !inode
->parent
)
545 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
,
546 struct inode
, ch_entry
)
548 assert( tmp
!= inode
);
549 assert( tmp
->parent
== inode
);
558 list_remove( &inode
->ch_entry
);
560 /* disconnect remaining children from the parent */
561 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
, struct inode
, ch_entry
)
563 list_remove( &tmp
->ch_entry
);
569 inotify_rm_watch( get_unix_fd( inotify_fd
), inode
->wd
);
570 list_remove( &inode
->wd_entry
);
572 list_remove( &inode
->ino_entry
);
578 static struct inode
*inode_add( struct inode
*parent
,
579 dev_t dev
, ino_t ino
, const char *name
)
583 inode
= get_inode( dev
, ino
);
589 list_add_tail( &parent
->children
, &inode
->ch_entry
);
590 inode
->parent
= parent
;
591 assert( inode
!= parent
);
593 inode_set_name( inode
, name
);
598 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
602 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
603 if (i
->name
&& !strcmp( i
->name
, name
))
608 static int inotify_get_poll_events( struct fd
*fd
);
609 static void inotify_poll_event( struct fd
*fd
, int event
);
611 static const struct fd_ops inotify_fd_ops
=
613 inotify_get_poll_events
, /* get_poll_events */
614 inotify_poll_event
, /* poll_event */
616 NULL
, /* get_fd_type */
618 NULL
, /* queue_async */
619 NULL
, /* reselect_async */
620 NULL
, /* cancel_async */
623 static int inotify_get_poll_events( struct fd
*fd
)
628 static void inotify_do_change_notify( struct dir
*dir
, unsigned int action
,
629 const char *relpath
)
631 struct change_record
*record
;
633 assert( dir
->obj
.ops
== &dir_ops
);
637 size_t len
= strlen(relpath
);
638 record
= malloc( offsetof(struct change_record
, name
[len
]) );
642 record
->action
= action
;
643 memcpy( record
->name
, relpath
, len
);
646 list_add_tail( &dir
->change_records
, &record
->entry
);
649 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
652 static unsigned int filter_from_event( struct inotify_event
*ie
)
654 unsigned int filter
= 0;
656 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
657 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
658 if (ie
->mask
& IN_MODIFY
)
659 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
;
660 if (ie
->mask
& IN_ATTRIB
)
661 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
662 if (ie
->mask
& IN_ACCESS
)
663 filter
|= FILE_NOTIFY_CHANGE_LAST_ACCESS
;
664 if (ie
->mask
& IN_CREATE
)
665 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
670 /* scan up the parent directories for watches */
671 static unsigned int filter_from_inode( struct inode
*inode
, int is_parent
)
673 unsigned int filter
= 0;
676 /* combine filters from parents watching subtrees */
679 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
680 if (dir
->subtree
|| !is_parent
)
681 filter
|= dir
->filter
;
683 inode
= inode
->parent
;
689 static char *inode_get_path( struct inode
*inode
, int sz
)
698 head
= list_head( &inode
->dirs
);
701 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
702 path
= malloc ( 32 + sz
);
704 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
711 len
= strlen( inode
->name
);
712 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
716 strcat( path
, inode
->name
);
722 static int inode_check_dir( struct inode
*parent
, const char *name
)
730 path
= inode_get_path( parent
, strlen(name
) );
734 strcat( path
, name
);
736 r
= stat( path
, &st
);
739 if (!S_ISDIR(st
.st_mode
))
747 filter
= filter_from_inode( parent
, 1 );
751 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
752 if (!inode
|| inode
->wd
!= -1)
755 wd
= inotify_add_dir( path
, filter
);
757 inode_set_wd( inode
, wd
);
766 static int prepend( char **path
, const char *segment
)
771 extra
= strlen( segment
) + 1;
774 int len
= strlen( *path
) + 1;
775 p
= realloc( *path
, len
+ extra
);
777 memmove( &p
[ extra
], p
, len
);
778 p
[ extra
- 1 ] = '/';
779 memcpy( p
, segment
, extra
- 1 );
785 memcpy( p
, segment
, extra
);
793 static void inotify_notify_all( struct inotify_event
*ie
)
795 unsigned int filter
, action
;
796 struct inode
*inode
, *i
;
800 inode
= inode_from_wd( ie
->wd
);
803 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
807 filter
= filter_from_event( ie
);
809 if (ie
->mask
& IN_CREATE
)
811 switch (inode_check_dir( inode
, ie
->name
))
814 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
817 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
821 /* Maybe the file disappeared before we could check it? */
823 action
= FILE_ACTION_ADDED
;
825 else if (ie
->mask
& IN_DELETE
)
826 action
= FILE_ACTION_REMOVED
;
828 action
= FILE_ACTION_MODIFIED
;
831 * Work our way up the inode hierarchy
832 * extending the relative path as we go
833 * and notifying all recursive watches.
835 if (!prepend( &path
, ie
->name
))
838 for (i
= inode
; i
; i
= i
->parent
)
840 LIST_FOR_EACH_ENTRY( dir
, &i
->dirs
, struct dir
, in_entry
)
841 if ((filter
& dir
->filter
) && (i
==inode
|| dir
->subtree
))
842 inotify_do_change_notify( dir
, action
, path
);
844 if (!i
->name
|| !prepend( &path
, i
->name
))
850 if (ie
->mask
& IN_DELETE
)
852 i
= inode_from_name( inode
, ie
->name
);
858 static void inotify_poll_event( struct fd
*fd
, int event
)
862 struct inotify_event
*ie
;
864 unix_fd
= get_unix_fd( fd
);
865 r
= read( unix_fd
, buffer
, sizeof buffer
);
868 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
872 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
874 ie
= (struct inotify_event
*) &buffer
[ofs
];
877 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
879 inotify_notify_all( ie
);
883 static inline struct fd
*create_inotify_fd( void )
887 unix_fd
= inotify_init();
890 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
, 0 );
893 static int map_flags( unsigned int filter
)
897 /* always watch these so we can track subdirectories in recursive watches */
898 mask
= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
900 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
902 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
904 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
906 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
908 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
914 static int inotify_add_dir( char *path
, unsigned int filter
)
916 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
917 path
, map_flags( filter
) );
919 set_fd_events( inotify_fd
, POLLIN
);
923 static int init_inotify( void )
930 inotify_fd
= create_inotify_fd();
934 for (i
=0; i
<HASH_SIZE
; i
++)
936 list_init( &inode_hash
[i
] );
937 list_init( &wd_hash
[i
] );
943 static int inotify_adjust_changes( struct dir
*dir
)
954 unix_fd
= get_unix_fd( dir
->fd
);
959 /* check if this fd is already being watched */
960 if (-1 == fstat( unix_fd
, &st
))
963 inode
= get_inode( st
.st_dev
, st
.st_ino
);
965 inode
= create_inode( st
.st_dev
, st
.st_ino
);
968 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
972 filter
= filter_from_inode( inode
, 0 );
974 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
975 wd
= inotify_add_dir( path
, filter
);
976 if (wd
== -1) return 0;
978 inode_set_wd( inode
, wd
);
983 static char *get_basename( const char *link
)
985 char *buffer
, *name
= NULL
;
990 buffer
= malloc( n
);
991 if (!buffer
) return NULL
;
993 r
= readlink( link
, buffer
, n
);
1008 while (r
> 0 && name
[ r
- 1 ] == '/' )
1012 name
= strrchr( name
, '/' );
1014 name
= strdup( &name
[1] );
1021 static int dir_add_to_existing_notify( struct dir
*dir
)
1023 struct inode
*inode
, *parent
;
1024 unsigned int filter
= 0;
1025 struct stat st
, st_new
;
1026 char link
[35], *name
;
1032 unix_fd
= get_unix_fd( dir
->fd
);
1034 /* check if it's in the list of inodes we want to watch */
1035 if (-1 == fstat( unix_fd
, &st_new
))
1037 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
1041 /* lookup the parent */
1042 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
1043 if (-1 == stat( link
, &st
))
1047 * If there's no parent, stop. We could keep going adding
1048 * ../ to the path until we hit the root of the tree or
1049 * find a recursively watched ancestor.
1050 * Assume it's too expensive to search up the tree for now.
1052 parent
= find_inode( st
.st_dev
, st
.st_ino
);
1056 if (parent
->wd
== -1)
1059 filter
= filter_from_inode( parent
, 1 );
1063 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
1064 name
= get_basename( link
);
1067 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
1072 /* Couldn't find this inode at the start of the function, must be new */
1073 assert( inode
->wd
== -1 );
1075 wd
= inotify_add_dir( link
, filter
);
1077 inode_set_wd( inode
, wd
);
1084 static int init_inotify( void )
1089 static int inotify_adjust_changes( struct dir
*dir
)
1094 static void free_inode( struct inode
*inode
)
1099 static int dir_add_to_existing_notify( struct dir
*dir
)
1104 #endif /* USE_INOTIFY */
1106 struct object
*create_dir_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
1110 dir
= alloc_object( &dir_ops
);
1114 list_init( &dir
->change_records
);
1122 dir
->uid
= ~(uid_t
)0;
1123 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1125 dir_add_to_existing_notify( dir
);
1130 /* enable change notifications for a directory */
1131 DECL_HANDLER(read_directory_changes
)
1134 struct async
*async
;
1138 set_error(STATUS_INVALID_PARAMETER
);
1142 dir
= get_dir_obj( current
->process
, req
->async
.handle
, 0 );
1146 /* requests don't timeout */
1147 if (!(async
= fd_queue_async( dir
->fd
, &req
->async
, ASYNC_TYPE_WAIT
))) goto end
;
1149 /* assign it once */
1153 insert_change( dir
);
1154 dir
->filter
= req
->filter
;
1155 dir
->subtree
= req
->subtree
;
1156 dir
->want_data
= req
->want_data
;
1159 /* if there's already a change in the queue, send it */
1160 if (!list_empty( &dir
->change_records
))
1161 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
1163 /* setup the real notification */
1164 if (!inotify_adjust_changes( dir
))
1165 dnotify_adjust_changes( dir
);
1167 release_object( async
);
1168 set_error(STATUS_PENDING
);
1171 release_object( dir
);
1174 DECL_HANDLER(read_change
)
1176 struct change_record
*record
;
1179 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1183 if ((record
= get_first_change_record( dir
)) != NULL
)
1185 reply
->action
= record
->action
;
1186 set_reply_data( record
->name
, record
->len
);
1190 set_error( STATUS_NO_DATA_DETECTED
);
1192 release_object( dir
);