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
30 #include <sys/types.h>
36 #ifdef HAVE_SYS_INOTIFY_H
37 #include <sys/inotify.h>
41 #define WIN32_NO_STATUS
57 #define DN_ACCESS 0x00000001 /* File accessed */
58 #define DN_MODIFY 0x00000002 /* File modified */
59 #define DN_CREATE 0x00000004 /* File created */
60 #define DN_DELETE 0x00000008 /* File removed */
61 #define DN_RENAME 0x00000010 /* File renamed */
62 #define DN_ATTRIB 0x00000020 /* File changed attributes */
63 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
71 static void free_inode( struct inode
*inode
);
73 static struct fd
*inotify_fd
;
75 struct change_record
{
78 struct filesystem_event event
;
83 struct object obj
; /* object header */
84 struct fd
*fd
; /* file descriptor to the directory */
85 mode_t mode
; /* file stat.st_mode */
86 uid_t uid
; /* file stat.st_uid */
87 struct list entry
; /* entry in global change notifications list */
88 unsigned int filter
; /* notification filter */
89 volatile int notified
; /* SIGIO counter */
90 int want_data
; /* return change data */
91 int subtree
; /* do we want to watch subdirectories? */
92 struct list change_records
; /* data for the change */
93 struct list in_entry
; /* entry in the inode dirs list */
94 struct inode
*inode
; /* inode of the associated directory */
95 struct process
*client_process
; /* client process that has a cache for this directory */
96 int client_entry
; /* entry in client process cache */
99 static struct fd
*dir_get_fd( struct object
*obj
);
100 static struct security_descriptor
*dir_get_sd( struct object
*obj
);
101 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
102 unsigned int set_info
);
103 static void dir_dump( struct object
*obj
, int verbose
);
104 static int dir_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
105 static void dir_destroy( struct object
*obj
);
107 static const struct object_ops dir_ops
=
109 sizeof(struct dir
), /* size */
110 &file_type
, /* type */
112 add_queue
, /* add_queue */
113 remove_queue
, /* remove_queue */
114 default_fd_signaled
, /* signaled */
115 no_satisfied
, /* satisfied */
116 no_signal
, /* signal */
117 dir_get_fd
, /* get_fd */
118 default_map_access
, /* map_access */
119 dir_get_sd
, /* get_sd */
120 dir_set_sd
, /* set_sd */
121 no_get_full_name
, /* get_full_name */
122 no_lookup_name
, /* lookup_name */
123 no_link_name
, /* link_name */
124 NULL
, /* unlink_name */
125 no_open_file
, /* open_file */
126 no_kernel_obj_list
, /* get_kernel_obj_list */
127 dir_close_handle
, /* close_handle */
128 dir_destroy
/* destroy */
131 static int dir_get_poll_events( struct fd
*fd
);
132 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
);
134 static const struct fd_ops dir_fd_ops
=
136 dir_get_poll_events
, /* get_poll_events */
137 default_poll_event
, /* poll_event */
138 dir_get_fd_type
, /* get_fd_type */
139 no_fd_read
, /* read */
140 no_fd_write
, /* write */
141 no_fd_flush
, /* flush */
142 default_fd_get_file_info
, /* get_file_info */
143 no_fd_get_volume_info
, /* get_volume_info */
144 default_fd_ioctl
, /* ioctl */
145 default_fd_cancel_async
, /* cancel_async */
146 default_fd_queue_async
, /* queue_async */
147 default_fd_reselect_async
/* reselect_async */
150 static struct list change_list
= LIST_INIT(change_list
);
152 /* per-process structure to keep track of cache entries on the client size */
157 unsigned char state
[1];
162 DIR_CACHE_STATE_FREE
,
163 DIR_CACHE_STATE_INUSE
,
164 DIR_CACHE_STATE_RELEASED
167 /* return an array of cache entries that can be freed on the client side */
168 static int *get_free_dir_cache_entries( struct process
*process
, data_size_t
*size
)
171 struct dir_cache
*cache
= process
->dir_cache
;
172 unsigned int i
, j
, count
;
174 if (!cache
) return NULL
;
175 for (i
= count
= 0; i
< cache
->count
&& count
< *size
/ sizeof(*ret
); i
++)
176 if (cache
->state
[i
] == DIR_CACHE_STATE_RELEASED
) count
++;
177 if (!count
) return NULL
;
179 if ((ret
= malloc( count
* sizeof(*ret
) )))
181 for (i
= j
= 0; j
< count
; i
++)
183 if (cache
->state
[i
] != DIR_CACHE_STATE_RELEASED
) continue;
184 cache
->state
[i
] = DIR_CACHE_STATE_FREE
;
187 *size
= count
* sizeof(*ret
);
192 /* allocate a new client-side directory cache entry */
193 static int alloc_dir_cache_entry( struct dir
*dir
, struct process
*process
)
196 struct dir_cache
*cache
= process
->dir_cache
;
199 for (i
= 0; i
< cache
->count
; i
++)
200 if (cache
->state
[i
] == DIR_CACHE_STATE_FREE
) goto found
;
202 if (!cache
|| cache
->count
== cache
->size
)
204 unsigned int size
= cache
? cache
->size
* 2 : 256;
205 if (!(cache
= realloc( cache
, offsetof( struct dir_cache
, state
[size
] ))))
207 set_error( STATUS_NO_MEMORY
);
210 process
->dir_cache
= cache
;
213 cache
->count
= i
+ 1;
216 cache
->state
[i
] = DIR_CACHE_STATE_INUSE
;
220 /* release a directory cache entry; it will be freed on the client side on the next cache request */
221 static void release_dir_cache_entry( struct dir
*dir
)
223 struct dir_cache
*cache
;
225 if (!dir
->client_process
) return;
226 cache
= dir
->client_process
->dir_cache
;
227 cache
->state
[dir
->client_entry
] = DIR_CACHE_STATE_RELEASED
;
228 release_object( dir
->client_process
);
229 dir
->client_process
= NULL
;
232 static void dnotify_adjust_changes( struct dir
*dir
)
234 #if defined(F_SETSIG) && defined(F_NOTIFY)
235 int fd
= get_unix_fd( dir
->fd
);
236 unsigned int filter
= dir
->filter
;
238 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
242 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
243 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
244 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
245 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
246 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
248 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
250 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
252 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
254 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
256 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
258 fcntl( fd
, F_NOTIFY
, val
);
262 /* insert change in the global list */
263 static inline void insert_change( struct dir
*dir
)
267 sigemptyset( &sigset
);
268 sigaddset( &sigset
, SIGIO
);
269 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
270 list_add_head( &change_list
, &dir
->entry
);
271 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
274 /* remove change from the global list */
275 static inline void remove_change( struct dir
*dir
)
279 sigemptyset( &sigset
);
280 sigaddset( &sigset
, SIGIO
);
281 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
282 list_remove( &dir
->entry
);
283 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
286 static void dir_dump( struct object
*obj
, int verbose
)
288 struct dir
*dir
= (struct dir
*)obj
;
289 assert( obj
->ops
== &dir_ops
);
290 fprintf( stderr
, "Dirfile fd=%p filter=%08x\n", dir
->fd
, dir
->filter
);
293 /* enter here directly from SIGIO signal handler */
294 void do_change_notify( int unix_fd
)
298 /* FIXME: this is O(n) ... probably can be improved */
299 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
301 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
307 /* SIGIO callback, called synchronously with the poll loop */
308 void sigio_callback(void)
312 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
314 if (!dir
->notified
) continue;
316 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
320 static struct fd
*dir_get_fd( struct object
*obj
)
322 struct dir
*dir
= (struct dir
*)obj
;
323 assert( obj
->ops
== &dir_ops
);
324 return (struct fd
*)grab_object( dir
->fd
);
327 static int get_dir_unix_fd( struct dir
*dir
)
329 return get_unix_fd( dir
->fd
);
332 static struct security_descriptor
*dir_get_sd( struct object
*obj
)
334 struct dir
*dir
= (struct dir
*)obj
;
337 struct security_descriptor
*sd
;
338 assert( obj
->ops
== &dir_ops
);
340 unix_fd
= get_dir_unix_fd( dir
);
342 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
345 /* mode and uid the same? if so, no need to re-generate security descriptor */
347 (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (dir
->mode
& (S_IRWXU
|S_IRWXO
)) &&
348 (st
.st_uid
== dir
->uid
))
351 sd
= mode_to_sd( st
.st_mode
,
352 security_unix_uid_to_sid( st
.st_uid
),
353 token_get_primary_group( current
->process
->token
));
354 if (!sd
) return obj
->sd
;
356 dir
->mode
= st
.st_mode
;
357 dir
->uid
= st
.st_uid
;
363 static int dir_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
364 unsigned int set_info
)
366 struct dir
*dir
= (struct dir
*)obj
;
367 const struct sid
*owner
;
372 assert( obj
->ops
== &dir_ops
);
374 unix_fd
= get_dir_unix_fd( dir
);
376 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
378 if (set_info
& OWNER_SECURITY_INFORMATION
)
380 owner
= sd_get_owner( sd
);
383 set_error( STATUS_INVALID_SECURITY_DESCR
);
386 if (!obj
->sd
|| !equal_sid( owner
, sd_get_owner( obj
->sd
) ))
388 /* FIXME: get Unix uid and call fchown */
392 owner
= sd_get_owner( obj
->sd
);
394 owner
= token_get_owner( current
->process
->token
);
396 if (set_info
& DACL_SECURITY_INFORMATION
)
398 /* keep the bits that we don't map to access rights in the ACL */
399 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
400 mode
|= sd_to_mode( sd
, owner
);
402 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
411 static struct change_record
*get_first_change_record( struct dir
*dir
)
413 struct list
*ptr
= list_head( &dir
->change_records
);
414 if (!ptr
) return NULL
;
416 return LIST_ENTRY( ptr
, struct change_record
, entry
);
419 static int dir_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
421 struct dir
*dir
= (struct dir
*)obj
;
423 if (obj
->handle_count
== 1) release_dir_cache_entry( dir
); /* closing last handle, release cache */
424 return 1; /* ok to close */
427 static void dir_destroy( struct object
*obj
)
429 struct change_record
*record
;
430 struct dir
*dir
= (struct dir
*)obj
;
431 assert (obj
->ops
== &dir_ops
);
434 remove_change( dir
);
438 list_remove( &dir
->in_entry
);
439 free_inode( dir
->inode
);
442 while ((record
= get_first_change_record( dir
))) free( record
);
444 release_dir_cache_entry( dir
);
445 release_object( dir
->fd
);
447 if (inotify_fd
&& list_empty( &change_list
))
449 release_object( inotify_fd
);
454 struct dir
*get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
456 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
459 static int dir_get_poll_events( struct fd
*fd
)
464 static enum server_fd_type
dir_get_fd_type( struct fd
*fd
)
469 #ifdef HAVE_SYS_INOTIFY_H
474 struct list ch_entry
; /* entry in the children list */
475 struct list children
; /* children of this inode */
476 struct inode
*parent
; /* parent of this inode */
477 struct list dirs
; /* directory handles watching this inode */
478 struct list ino_entry
; /* entry in the inode hash */
479 struct list wd_entry
; /* entry in the watch descriptor hash */
480 dev_t dev
; /* device number */
481 ino_t ino
; /* device's inode number */
482 int wd
; /* inotify's watch descriptor */
483 char *name
; /* basename name of the inode */
486 static struct list inode_hash
[ HASH_SIZE
];
487 static struct list wd_hash
[ HASH_SIZE
];
489 static int inotify_add_dir( char *path
, unsigned int filter
);
491 static struct inode
*inode_from_wd( int wd
)
493 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
496 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
503 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
505 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
508 static struct inode
*find_inode( dev_t dev
, ino_t ino
)
510 struct list
*bucket
= get_hash_list( dev
, ino
);
513 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
514 if (inode
->ino
== ino
&& inode
->dev
== dev
)
520 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
524 inode
= malloc( sizeof *inode
);
527 list_init( &inode
->children
);
528 list_init( &inode
->dirs
);
532 inode
->parent
= NULL
;
534 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
539 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
543 inode
= find_inode( dev
, ino
);
546 return create_inode( dev
, ino
);
549 static void inode_set_wd( struct inode
*inode
, int wd
)
552 list_remove( &inode
->wd_entry
);
554 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
557 static void inode_set_name( struct inode
*inode
, const char *name
)
560 inode
->name
= name
? strdup( name
) : NULL
;
563 static void free_inode( struct inode
*inode
)
565 int subtree
= 0, watches
= 0;
566 struct inode
*tmp
, *next
;
569 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
571 subtree
|= dir
->subtree
;
575 if (!subtree
&& !inode
->parent
)
577 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
,
578 struct inode
, ch_entry
)
580 assert( tmp
!= inode
);
581 assert( tmp
->parent
== inode
);
590 list_remove( &inode
->ch_entry
);
592 /* disconnect remaining children from the parent */
593 LIST_FOR_EACH_ENTRY_SAFE( tmp
, next
, &inode
->children
, struct inode
, ch_entry
)
595 list_remove( &tmp
->ch_entry
);
601 inotify_rm_watch( get_unix_fd( inotify_fd
), inode
->wd
);
602 list_remove( &inode
->wd_entry
);
604 list_remove( &inode
->ino_entry
);
610 static struct inode
*inode_add( struct inode
*parent
,
611 dev_t dev
, ino_t ino
, const char *name
)
615 inode
= get_inode( dev
, ino
);
621 list_add_tail( &parent
->children
, &inode
->ch_entry
);
622 inode
->parent
= parent
;
623 assert( inode
!= parent
);
625 inode_set_name( inode
, name
);
630 static struct inode
*inode_from_name( struct inode
*inode
, const char *name
)
634 LIST_FOR_EACH_ENTRY( i
, &inode
->children
, struct inode
, ch_entry
)
635 if (i
->name
&& !strcmp( i
->name
, name
))
640 static int inotify_get_poll_events( struct fd
*fd
);
641 static void inotify_poll_event( struct fd
*fd
, int event
);
643 static const struct fd_ops inotify_fd_ops
=
645 inotify_get_poll_events
, /* get_poll_events */
646 inotify_poll_event
, /* poll_event */
648 NULL
, /* get_fd_type */
650 NULL
, /* queue_async */
651 NULL
/* reselect_async */
654 static int inotify_get_poll_events( struct fd
*fd
)
659 static void inotify_do_change_notify( struct dir
*dir
, unsigned int action
,
660 unsigned int cookie
, const char *relpath
)
662 struct change_record
*record
;
664 assert( dir
->obj
.ops
== &dir_ops
);
668 size_t len
= strlen(relpath
);
669 record
= malloc( offsetof(struct change_record
, event
.name
[len
]) );
673 record
->cookie
= cookie
;
674 record
->event
.action
= action
;
675 memcpy( record
->event
.name
, relpath
, len
);
676 record
->event
.len
= len
;
678 list_add_tail( &dir
->change_records
, &record
->entry
);
681 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
684 static unsigned int filter_from_event( struct inotify_event
*ie
)
686 unsigned int filter
= 0;
688 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
689 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
690 if (ie
->mask
& IN_MODIFY
)
691 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_LAST_ACCESS
;
692 if (ie
->mask
& IN_ATTRIB
)
693 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
694 if (ie
->mask
& IN_CREATE
)
695 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
697 if (ie
->mask
& IN_ISDIR
)
698 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
700 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
705 /* scan up the parent directories for watches */
706 static unsigned int filter_from_inode( struct inode
*inode
, int is_parent
)
708 unsigned int filter
= 0;
711 /* combine filters from parents watching subtrees */
714 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
715 if (dir
->subtree
|| !is_parent
)
716 filter
|= dir
->filter
;
718 inode
= inode
->parent
;
724 static char *inode_get_path( struct inode
*inode
, int sz
)
733 head
= list_head( &inode
->dirs
);
736 int unix_fd
= get_unix_fd( LIST_ENTRY( head
, struct dir
, in_entry
)->fd
);
737 path
= malloc ( 32 + sz
);
739 sprintf( path
, "/proc/self/fd/%u/", unix_fd
);
746 len
= strlen( inode
->name
);
747 path
= inode_get_path( inode
->parent
, sz
+ len
+ 1 );
751 strcat( path
, inode
->name
);
757 static void inode_check_dir( struct inode
*parent
, const char *name
)
765 path
= inode_get_path( parent
, strlen(name
) );
769 strcat( path
, name
);
771 if (stat( path
, &st
) < 0)
774 filter
= filter_from_inode( parent
, 1 );
778 inode
= inode_add( parent
, st
.st_dev
, st
.st_ino
, name
);
779 if (!inode
|| inode
->wd
!= -1)
782 wd
= inotify_add_dir( path
, filter
);
784 inode_set_wd( inode
, wd
);
792 static int prepend( char **path
, const char *segment
)
797 extra
= strlen( segment
) + 1;
800 int len
= strlen( *path
) + 1;
801 p
= realloc( *path
, len
+ extra
);
803 memmove( &p
[ extra
], p
, len
);
804 p
[ extra
- 1 ] = '/';
805 memcpy( p
, segment
, extra
- 1 );
811 memcpy( p
, segment
, extra
);
819 static void inotify_notify_all( struct inotify_event
*ie
)
821 unsigned int filter
, action
;
822 struct inode
*inode
, *i
;
826 inode
= inode_from_wd( ie
->wd
);
829 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
833 filter
= filter_from_event( ie
);
835 if (ie
->mask
& IN_CREATE
)
837 if (ie
->mask
& IN_ISDIR
)
838 inode_check_dir( inode
, ie
->name
);
840 action
= FILE_ACTION_ADDED
;
842 else if (ie
->mask
& IN_DELETE
)
843 action
= FILE_ACTION_REMOVED
;
844 else if (ie
->mask
& IN_MOVED_FROM
)
845 action
= FILE_ACTION_RENAMED_OLD_NAME
;
846 else if (ie
->mask
& IN_MOVED_TO
)
847 action
= FILE_ACTION_RENAMED_NEW_NAME
;
849 action
= FILE_ACTION_MODIFIED
;
852 * Work our way up the inode hierarchy
853 * extending the relative path as we go
854 * and notifying all recursive watches.
856 if (!prepend( &path
, ie
->name
))
859 for (i
= inode
; i
; i
= i
->parent
)
861 LIST_FOR_EACH_ENTRY( dir
, &i
->dirs
, struct dir
, in_entry
)
862 if ((filter
& dir
->filter
) && (i
==inode
|| dir
->subtree
))
863 inotify_do_change_notify( dir
, action
, ie
->cookie
, path
);
865 if (!i
->name
|| !prepend( &path
, i
->name
))
871 if (ie
->mask
& IN_DELETE
)
873 i
= inode_from_name( inode
, ie
->name
);
879 static void inotify_poll_event( struct fd
*fd
, int event
)
883 struct inotify_event
*ie
;
885 unix_fd
= get_unix_fd( fd
);
886 r
= read( unix_fd
, buffer
, sizeof buffer
);
889 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
893 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
895 ie
= (struct inotify_event
*) &buffer
[ofs
];
896 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
898 if (ie
->len
) inotify_notify_all( ie
);
902 static inline struct fd
*create_inotify_fd( void )
906 unix_fd
= inotify_init();
909 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
, 0 );
912 static int map_flags( unsigned int filter
)
916 /* always watch these so we can track subdirectories in recursive watches */
917 mask
= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
919 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
921 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
923 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
925 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
927 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
933 static int inotify_add_dir( char *path
, unsigned int filter
)
935 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
936 path
, map_flags( filter
) );
938 set_fd_events( inotify_fd
, POLLIN
);
942 static int init_inotify( void )
949 inotify_fd
= create_inotify_fd();
953 for (i
=0; i
<HASH_SIZE
; i
++)
955 list_init( &inode_hash
[i
] );
956 list_init( &wd_hash
[i
] );
962 static int inotify_adjust_changes( struct dir
*dir
)
973 unix_fd
= get_unix_fd( dir
->fd
);
978 /* check if this fd is already being watched */
979 if (-1 == fstat( unix_fd
, &st
))
982 inode
= get_inode( st
.st_dev
, st
.st_ino
);
984 inode
= create_inode( st
.st_dev
, st
.st_ino
);
987 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
991 filter
= filter_from_inode( inode
, 0 );
993 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
994 wd
= inotify_add_dir( path
, filter
);
995 if (wd
== -1) return 0;
997 inode_set_wd( inode
, wd
);
1002 static char *get_basename( const char *link
)
1004 char *buffer
, *name
= NULL
;
1009 buffer
= malloc( n
);
1010 if (!buffer
) return NULL
;
1012 r
= readlink( link
, buffer
, n
);
1027 while (r
> 0 && name
[ r
- 1 ] == '/' )
1031 name
= strrchr( name
, '/' );
1033 name
= strdup( &name
[1] );
1040 static int dir_add_to_existing_notify( struct dir
*dir
)
1042 struct inode
*inode
, *parent
;
1043 unsigned int filter
= 0;
1044 struct stat st
, st_new
;
1045 char link
[35], *name
;
1051 unix_fd
= get_unix_fd( dir
->fd
);
1053 /* check if it's in the list of inodes we want to watch */
1054 if (-1 == fstat( unix_fd
, &st_new
))
1056 inode
= find_inode( st_new
.st_dev
, st_new
.st_ino
);
1060 /* lookup the parent */
1061 sprintf( link
, "/proc/self/fd/%u/..", unix_fd
);
1062 if (-1 == stat( link
, &st
))
1066 * If there's no parent, stop. We could keep going adding
1067 * ../ to the path until we hit the root of the tree or
1068 * find a recursively watched ancestor.
1069 * Assume it's too expensive to search up the tree for now.
1071 parent
= find_inode( st
.st_dev
, st
.st_ino
);
1075 if (parent
->wd
== -1)
1078 filter
= filter_from_inode( parent
, 1 );
1082 sprintf( link
, "/proc/self/fd/%u", unix_fd
);
1083 name
= get_basename( link
);
1086 inode
= inode_add( parent
, st_new
.st_dev
, st_new
.st_ino
, name
);
1091 /* Couldn't find this inode at the start of the function, must be new */
1092 assert( inode
->wd
== -1 );
1094 wd
= inotify_add_dir( link
, filter
);
1096 inode_set_wd( inode
, wd
);
1103 static int init_inotify( void )
1108 static int inotify_adjust_changes( struct dir
*dir
)
1113 static void free_inode( struct inode
*inode
)
1118 static int dir_add_to_existing_notify( struct dir
*dir
)
1123 #endif /* HAVE_SYS_INOTIFY_H */
1125 struct object
*create_dir_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
1129 dir
= alloc_object( &dir_ops
);
1133 list_init( &dir
->change_records
);
1141 dir
->uid
= ~(uid_t
)0;
1142 dir
->client_process
= NULL
;
1143 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
1145 dir_add_to_existing_notify( dir
);
1150 /* retrieve (or allocate) the client-side directory cache entry */
1151 DECL_HANDLER(get_directory_cache_entry
)
1155 data_size_t free_size
;
1157 if (!(dir
= get_dir_obj( current
->process
, req
->handle
, 0 ))) return;
1159 if (!dir
->client_process
)
1161 if ((dir
->client_entry
= alloc_dir_cache_entry( dir
, current
->process
)) == -1) goto done
;
1162 dir
->client_process
= (struct process
*)grab_object( current
->process
);
1165 if (dir
->client_process
== current
->process
) reply
->entry
= dir
->client_entry
;
1166 else set_error( STATUS_SHARING_VIOLATION
);
1168 done
: /* allow freeing entries even on failure */
1169 free_size
= get_reply_max_size();
1170 free_entries
= get_free_dir_cache_entries( current
->process
, &free_size
);
1171 if (free_entries
) set_reply_data_ptr( free_entries
, free_size
);
1173 release_object( dir
);
1176 /* enable change notifications for a directory */
1177 DECL_HANDLER(read_directory_changes
)
1180 struct async
*async
;
1184 set_error(STATUS_INVALID_PARAMETER
);
1188 dir
= get_dir_obj( current
->process
, req
->async
.handle
, 0 );
1192 /* requests don't timeout */
1193 if (!(async
= create_async( dir
->fd
, current
, &req
->async
, NULL
))) goto end
;
1194 fd_queue_async( dir
->fd
, async
, ASYNC_TYPE_WAIT
);
1196 /* assign it once */
1200 insert_change( dir
);
1201 dir
->filter
= req
->filter
;
1202 dir
->subtree
= req
->subtree
;
1203 dir
->want_data
= req
->want_data
;
1206 /* if there's already a change in the queue, send it */
1207 if (!list_empty( &dir
->change_records
))
1208 fd_async_wake_up( dir
->fd
, ASYNC_TYPE_WAIT
, STATUS_ALERTED
);
1210 /* setup the real notification */
1211 if (!inotify_adjust_changes( dir
))
1212 dnotify_adjust_changes( dir
);
1214 set_error(STATUS_PENDING
);
1216 release_object( async
);
1218 release_object( dir
);
1221 DECL_HANDLER(read_change
)
1223 struct change_record
*record
, *next
;
1229 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
1233 list_init( &events
);
1234 list_move_tail( &events
, &dir
->change_records
);
1235 release_object( dir
);
1237 if (list_empty( &events
))
1239 set_error( STATUS_NO_DATA_DETECTED
);
1243 LIST_FOR_EACH_ENTRY( record
, &events
, struct change_record
, entry
)
1245 size
+= (offsetof(struct filesystem_event
, name
[record
->event
.len
])
1246 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1249 if (size
> get_reply_max_size())
1250 set_error( STATUS_BUFFER_TOO_SMALL
);
1251 else if ((data
= mem_alloc( size
)) != NULL
)
1254 LIST_FOR_EACH_ENTRY( record
, &events
, struct change_record
, entry
)
1256 data_size_t len
= offsetof( struct filesystem_event
, name
[record
->event
.len
] );
1258 /* FIXME: rename events are sometimes reported as delete/create */
1259 if (record
->event
.action
== FILE_ACTION_RENAMED_OLD_NAME
)
1261 struct list
*elem
= list_next( &events
, &record
->entry
);
1263 next
= LIST_ENTRY(elem
, struct change_record
, entry
);
1265 if (elem
&& next
->cookie
== record
->cookie
)
1268 record
->event
.action
= FILE_ACTION_REMOVED
;
1270 else if (record
->event
.action
== FILE_ACTION_RENAMED_NEW_NAME
&& record
->cookie
)
1271 record
->event
.action
= FILE_ACTION_ADDED
;
1273 memcpy( event
, &record
->event
, len
);
1275 if (len
% sizeof(int))
1277 memset( event
, 0, sizeof(int) - len
% sizeof(int) );
1278 event
+= sizeof(int) - len
% sizeof(int);
1281 set_reply_data_ptr( data
, size
);
1284 LIST_FOR_EACH_ENTRY_SAFE( record
, next
, &events
, struct change_record
, entry
)
1286 list_remove( &record
->entry
);