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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
31 #include <sys/types.h>
35 #ifdef HAVE_SYS_ERRNO_H
36 #include <sys/errno.h>
40 #define WIN32_NO_STATUS
54 #define DN_ACCESS 0x00000001 /* File accessed */
55 #define DN_MODIFY 0x00000002 /* File modified */
56 #define DN_CREATE 0x00000004 /* File created */
57 #define DN_DELETE 0x00000008 /* File removed */
58 #define DN_RENAME 0x00000010 /* File renamed */
59 #define DN_ATTRIB 0x00000020 /* File changed attibutes */
60 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
66 #if defined(__linux__) && defined(__i386__)
68 #define SYS_inotify_init 291
69 #define SYS_inotify_add_watch 292
70 #define SYS_inotify_rm_watch 293
72 struct inotify_event
{
80 #define IN_ACCESS 0x00000001
81 #define IN_MODIFY 0x00000002
82 #define IN_ATTRIB 0x00000004
83 #define IN_CLOSE_WRITE 0x00000008
84 #define IN_CLOSE_NOWRITE 0x00000010
85 #define IN_OPEN 0x00000020
86 #define IN_MOVED_FROM 0x00000040
87 #define IN_MOVED_TO 0x00000080
88 #define IN_CREATE 0x00000100
89 #define IN_DELETE 0x00000200
90 #define IN_DELETE_SELF 0x00000400
92 static inline int inotify_init( void )
95 __asm__
__volatile__( "int $0x80"
97 : "0" (SYS_inotify_init
));
98 if (ret
<0) { errno
= -ret
; ret
= -1; }
102 static inline int inotify_add_watch( int fd
, const char *name
, unsigned int mask
)
105 __asm__
__volatile__( "pushl %%ebx;\n\t"
109 : "=a" (ret
) : "0" (SYS_inotify_add_watch
),
110 "r" (fd
), "c" (name
), "d" (mask
) );
111 if (ret
<0) { errno
= -ret
; ret
= -1; }
115 static inline int inotify_remove_watch( int fd
, int wd
)
118 __asm__
__volatile__( "pushl %%ebx;\n\t"
122 : "=a" (ret
) : "0" (SYS_inotify_rm_watch
),
123 "r" (fd
), "c" (wd
) );
124 if (ret
<0) { errno
= -ret
; ret
= -1; }
134 static void free_inode( struct inode
*inode
);
136 static struct fd
*inotify_fd
;
138 struct change_record
{
147 struct object obj
; /* object header */
148 struct fd
*fd
; /* file descriptor to the directory */
149 struct list entry
; /* entry in global change notifications list */
151 unsigned int filter
; /* notification filter */
152 int notified
; /* SIGIO counter */
153 int want_data
; /* return change data */
154 long signaled
; /* the file changed */
155 struct list change_q
; /* change readers */
156 struct list change_records
; /* data for the change */
157 struct list in_entry
; /* entry in the inode dirs list */
158 struct inode
*inode
; /* inode of the associated directory */
161 static struct fd
*dir_get_fd( struct object
*obj
);
162 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
);
163 static void dir_dump( struct object
*obj
, int verbose
);
164 static void dir_destroy( struct object
*obj
);
165 static int dir_signaled( struct object
*obj
, struct thread
*thread
);
167 static const struct object_ops dir_ops
=
169 sizeof(struct dir
), /* size */
171 add_queue
, /* add_queue */
172 remove_queue
, /* remove_queue */
173 dir_signaled
, /* signaled */
174 no_satisfied
, /* satisfied */
175 no_signal
, /* signal */
176 dir_get_fd
, /* get_fd */
177 dir_map_access
, /* map_access */
178 no_lookup_name
, /* lookup_name */
179 no_close_handle
, /* close_handle */
180 dir_destroy
/* destroy */
183 static int dir_get_poll_events( struct fd
*fd
);
184 static int dir_get_info( struct fd
*fd
);
185 static void dir_cancel_async( struct fd
*fd
);
187 static const struct fd_ops dir_fd_ops
=
189 dir_get_poll_events
, /* get_poll_events */
190 default_poll_event
, /* poll_event */
191 no_flush
, /* flush */
192 dir_get_info
, /* get_file_info */
193 default_fd_queue_async
, /* queue_async */
194 dir_cancel_async
/* cancel_async */
197 static struct list change_list
= LIST_INIT(change_list
);
199 static void dnotify_adjust_changes( struct dir
*dir
)
201 #if defined(F_SETSIG) && defined(F_NOTIFY)
202 int fd
= get_unix_fd( dir
->fd
);
203 unsigned int filter
= dir
->filter
;
205 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
209 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
210 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
211 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
212 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
213 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
215 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
217 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
219 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
221 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
223 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
225 fcntl( fd
, F_NOTIFY
, val
);
229 /* insert change in the global list */
230 static inline void insert_change( struct dir
*dir
)
234 sigemptyset( &sigset
);
235 sigaddset( &sigset
, SIGIO
);
236 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
237 list_add_head( &change_list
, &dir
->entry
);
238 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
241 /* remove change from the global list */
242 static inline void remove_change( struct dir
*dir
)
246 sigemptyset( &sigset
);
247 sigaddset( &sigset
, SIGIO
);
248 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
249 list_remove( &dir
->entry
);
250 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
253 static void dir_dump( struct object
*obj
, int verbose
)
255 struct dir
*dir
= (struct dir
*)obj
;
256 assert( obj
->ops
== &dir_ops
);
257 fprintf( stderr
, "Dirfile fd=%p event=%p filter=%08x\n",
258 dir
->fd
, dir
->event
, dir
->filter
);
261 static int dir_signaled( struct object
*obj
, struct thread
*thread
)
263 struct dir
*dir
= (struct dir
*)obj
;
264 assert (obj
->ops
== &dir_ops
);
265 return (dir
->event
== NULL
) && dir
->signaled
;
268 /* enter here directly from SIGIO signal handler */
269 void do_change_notify( int unix_fd
)
273 /* FIXME: this is O(n) ... probably can be improved */
274 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
276 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
277 interlocked_xchg_add( &dir
->notified
, 1 );
282 static void dir_signal_changed( struct dir
*dir
)
285 set_event( dir
->event
);
287 wake_up( &dir
->obj
, 0 );
290 /* SIGIO callback, called synchronously with the poll loop */
291 void sigio_callback(void)
295 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
297 long count
= interlocked_xchg( &dir
->notified
, 0 );
300 dir
->signaled
+= count
;
301 if (dir
->signaled
== count
) /* was it 0? */
302 dir_signal_changed( dir
);
307 static struct fd
*dir_get_fd( struct object
*obj
)
309 struct dir
*dir
= (struct dir
*)obj
;
310 assert( obj
->ops
== &dir_ops
);
311 return (struct fd
*)grab_object( dir
->fd
);
314 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
)
316 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
317 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
318 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
319 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
320 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
323 static struct change_record
*get_first_change_record( struct dir
*dir
)
325 struct list
*ptr
= list_head( &dir
->change_records
);
326 if (!ptr
) return NULL
;
328 return LIST_ENTRY( ptr
, struct change_record
, entry
);
331 static void dir_destroy( struct object
*obj
)
333 struct change_record
*record
;
334 struct dir
*dir
= (struct dir
*)obj
;
335 assert (obj
->ops
== &dir_ops
);
338 remove_change( dir
);
342 list_remove( &dir
->in_entry
);
343 free_inode( dir
->inode
);
346 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
347 while ((record
= get_first_change_record( dir
))) free( record
);
351 set_event( dir
->event
);
352 release_object( dir
->event
);
354 release_object( dir
->fd
);
356 if (inotify_fd
&& list_empty( &change_list
))
358 release_object( inotify_fd
);
364 get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
366 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
369 static int dir_get_poll_events( struct fd
*fd
)
374 static int dir_get_info( struct fd
*fd
)
379 static void dir_cancel_async( struct fd
*fd
)
381 struct dir
*dir
= (struct dir
*) get_fd_user( fd
);
382 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
391 struct list dirs
; /* directory handles watching this inode */
392 struct list ino_entry
; /* entry in the inode hash */
393 struct list wd_entry
; /* entry in the watch descriptor hash */
394 dev_t dev
; /* device number */
395 ino_t ino
; /* device's inode number */
396 int wd
; /* inotify's watch descriptor */
399 struct list inode_hash
[ HASH_SIZE
];
400 struct list wd_hash
[ HASH_SIZE
];
402 static struct inode
*inode_from_wd( int wd
)
404 struct list
*bucket
= &wd_hash
[ wd
% HASH_SIZE
];
407 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, wd_entry
)
414 static inline struct list
*get_hash_list( dev_t dev
, ino_t ino
)
416 return &inode_hash
[ (ino
^ dev
) % HASH_SIZE
];
419 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
421 struct list
*bucket
= get_hash_list( dev
, ino
);
424 LIST_FOR_EACH_ENTRY( inode
, bucket
, struct inode
, ino_entry
)
425 if (inode
->ino
== ino
&& inode
->dev
== dev
)
431 static struct inode
*create_inode( dev_t dev
, ino_t ino
)
435 inode
= malloc( sizeof *inode
);
438 list_init( &inode
->dirs
);
442 list_add_tail( get_hash_list( dev
, ino
), &inode
->ino_entry
);
447 static void inode_set_wd( struct inode
*inode
, int wd
)
450 list_remove( &inode
->wd_entry
);
452 list_add_tail( &wd_hash
[ wd
% HASH_SIZE
], &inode
->wd_entry
);
455 static void free_inode( struct inode
*inode
)
457 if (!list_empty( &inode
->dirs
))
462 inotify_remove_watch( get_unix_fd( inotify_fd
), inode
->wd
);
463 list_remove( &inode
->wd_entry
);
465 list_remove( &inode
->ino_entry
);
469 static int inotify_get_poll_events( struct fd
*fd
);
470 static void inotify_poll_event( struct fd
*fd
, int event
);
471 static int inotify_get_info( struct fd
*fd
);
473 static const struct fd_ops inotify_fd_ops
=
475 inotify_get_poll_events
, /* get_poll_events */
476 inotify_poll_event
, /* poll_event */
477 no_flush
, /* flush */
478 inotify_get_info
, /* get_file_info */
479 default_fd_queue_async
, /* queue_async */
480 default_fd_cancel_async
, /* cancel_async */
483 static int inotify_get_poll_events( struct fd
*fd
)
488 static void inotify_do_change_notify( struct dir
*dir
, struct inotify_event
*ie
)
490 struct change_record
*record
;
494 size_t len
= strlen(ie
->name
);
495 record
= malloc( offsetof(struct change_record
, name
[len
]) );
499 if( ie
->mask
& IN_CREATE
)
500 record
->action
= FILE_ACTION_ADDED
;
501 else if( ie
->mask
& IN_DELETE
)
502 record
->action
= FILE_ACTION_REMOVED
;
504 record
->action
= FILE_ACTION_MODIFIED
;
505 memcpy( record
->name
, ie
->name
, len
);
508 list_add_tail( &dir
->change_records
, &record
->entry
);
511 if (!list_empty( &dir
->change_q
))
512 async_terminate_head( &dir
->change_q
, STATUS_ALERTED
);
516 dir_signal_changed( dir
);
520 static unsigned int filter_from_event( struct inotify_event
*ie
)
522 unsigned int filter
= 0;
524 if (ie
->mask
& (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
))
525 filter
|= FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
;
526 if (ie
->mask
& IN_MODIFY
)
527 filter
|= FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
;
528 if (ie
->mask
& IN_ATTRIB
)
529 filter
|= FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SECURITY
;
530 if (ie
->mask
& IN_ACCESS
)
531 filter
|= FILE_NOTIFY_CHANGE_LAST_ACCESS
;
532 if (ie
->mask
& IN_CREATE
)
533 filter
|= FILE_NOTIFY_CHANGE_CREATION
;
538 static void inotify_notify_all( struct inotify_event
*ie
)
544 inode
= inode_from_wd( ie
->wd
);
547 fprintf( stderr
, "no inode matches %d\n", ie
->wd
);
551 filter
= filter_from_event( ie
);
553 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
554 if (filter
& dir
->filter
)
555 inotify_do_change_notify( dir
, ie
);
558 static void inotify_poll_event( struct fd
*fd
, int event
)
562 struct inotify_event
*ie
;
564 unix_fd
= get_unix_fd( fd
);
565 r
= read( unix_fd
, buffer
, sizeof buffer
);
568 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
572 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
574 ie
= (struct inotify_event
*) &buffer
[ofs
];
577 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
579 inotify_notify_all( ie
);
583 static int inotify_get_info( struct fd
*fd
)
588 static inline struct fd
*create_inotify_fd( void )
592 unix_fd
= inotify_init();
595 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, NULL
);
598 static int map_flags( unsigned int filter
)
600 unsigned int mask
= 0;
602 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
603 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
);
604 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
605 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
606 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
608 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
610 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
612 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
614 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
616 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
622 static int inotify_add_dir( char *path
, unsigned int filter
)
624 int wd
= inotify_add_watch( get_unix_fd( inotify_fd
),
625 path
, map_flags( filter
) );
627 set_fd_events( inotify_fd
, POLLIN
);
631 static unsigned int filter_from_inode( struct inode
*inode
)
633 unsigned int filter
= 0;
636 LIST_FOR_EACH_ENTRY( dir
, &inode
->dirs
, struct dir
, in_entry
)
637 filter
|= dir
->filter
;
642 static int init_inotify( void )
649 inotify_fd
= create_inotify_fd();
653 for (i
=0; i
<HASH_SIZE
; i
++)
655 list_init( &inode_hash
[i
] );
656 list_init( &wd_hash
[i
] );
662 static int inotify_adjust_changes( struct dir
*dir
)
673 unix_fd
= get_unix_fd( dir
->fd
);
678 /* check if this fd is already being watched */
679 if (-1 == fstat( unix_fd
, &st
))
682 inode
= get_inode( st
.st_dev
, st
.st_ino
);
684 inode
= create_inode( st
.st_dev
, st
.st_ino
);
687 list_add_tail( &inode
->dirs
, &dir
->in_entry
);
691 filter
= filter_from_inode( inode
);
693 sprintf( path
, "/proc/self/fd/%u", unix_fd
);
694 wd
= inotify_add_dir( path
, filter
);
695 if (wd
== -1) return 0;
697 inode_set_wd( inode
, wd
);
704 static int init_inotify( void )
709 static int inotify_adjust_changes( struct dir
*dir
)
714 static void free_inode( struct inode
*inode
)
719 #endif /* USE_INOTIFY */
721 struct object
*create_dir_obj( struct fd
*fd
)
725 dir
= alloc_object( &dir_ops
);
729 list_init( &dir
->change_q
);
730 list_init( &dir
->change_records
);
739 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
744 /* enable change notifications for a directory */
745 DECL_HANDLER(read_directory_changes
)
747 struct event
*event
= NULL
;
752 set_error(STATUS_INVALID_PARAMETER
);
756 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
760 /* possibly send changes through an event flag */
763 event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
768 /* discard the current data, and move onto the next event */
769 if (dir
->event
) release_object( dir
->event
);
772 /* requests don't timeout */
773 if ( req
->io_apc
&& !create_async( current
, NULL
, &dir
->change_q
,
774 req
->io_apc
, req
->io_user
, req
->io_sb
))
781 insert_change( dir
);
782 dir
->filter
= req
->filter
;
783 dir
->want_data
= req
->want_data
;
786 /* remove any notifications */
790 /* clear the event */
792 reset_event( event
);
794 /* setup the real notification */
795 if (!inotify_adjust_changes( dir
))
796 dnotify_adjust_changes( dir
);
798 set_error(STATUS_PENDING
);
801 release_object( dir
);
804 DECL_HANDLER(read_change
)
806 struct change_record
*record
;
809 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
813 if ((record
= get_first_change_record( dir
)) != NULL
)
815 reply
->action
= record
->action
;
816 set_reply_data( record
->name
, record
->len
);
820 set_error( STATUS_NO_DATA_DETECTED
);
824 dir_signal_changed( dir
);
826 release_object( dir
);