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"
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
37 #define WIN32_NO_STATUS
51 #define DN_ACCESS 0x00000001 /* File accessed */
52 #define DN_MODIFY 0x00000002 /* File modified */
53 #define DN_CREATE 0x00000004 /* File created */
54 #define DN_DELETE 0x00000008 /* File removed */
55 #define DN_RENAME 0x00000010 /* File renamed */
56 #define DN_ATTRIB 0x00000020 /* File changed attibutes */
57 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
63 #if defined(__linux__) && defined(__i386__)
65 #define SYS_inotify_init 291
66 #define SYS_inotify_add_watch 292
67 #define SYS_inotify_rm_watch 293
69 struct inotify_event
{
77 #define IN_ACCESS 0x00000001
78 #define IN_MODIFY 0x00000002
79 #define IN_ATTRIB 0x00000004
80 #define IN_CLOSE_WRITE 0x00000008
81 #define IN_CLOSE_NOWRITE 0x00000010
82 #define IN_OPEN 0x00000020
83 #define IN_MOVED_FROM 0x00000040
84 #define IN_MOVED_TO 0x00000080
85 #define IN_CREATE 0x00000100
86 #define IN_DELETE 0x00000200
87 #define IN_DELETE_SELF 0x00000400
89 static inline int inotify_init( void )
92 __asm__
__volatile__( "int $0x80"
94 : "0" (SYS_inotify_init
));
95 if (ret
<0) { errno
= -ret
; ret
= -1; }
99 static inline int inotify_add_watch( int fd
, const char *name
, unsigned int mask
)
102 __asm__
__volatile__( "pushl %%ebx;\n\t"
106 : "=a" (ret
) : "0" (SYS_inotify_add_watch
),
107 "r" (fd
), "c" (name
), "d" (mask
) );
108 if (ret
<0) { errno
= -ret
; ret
= -1; }
112 static inline int inotify_remove_watch( int fd
, int wd
)
115 __asm__
__volatile__( "pushl %%ebx;\n\t"
119 : "=a" (ret
) : "0" (SYS_inotify_rm_watch
),
120 "r" (fd
), "c" (wd
) );
121 if (ret
<0) { errno
= -ret
; ret
= -1; }
129 struct change_record
{
138 struct object obj
; /* object header */
139 struct fd
*fd
; /* file descriptor to the directory */
140 struct list entry
; /* entry in global change notifications list */
142 unsigned int filter
; /* notification filter */
143 int notified
; /* SIGIO counter */
144 int want_data
; /* return change data */
145 long signaled
; /* the file changed */
146 struct fd
*inotify_fd
; /* inotify file descriptor */
147 int wd
; /* inotify watch descriptor */
148 struct list change_q
; /* change readers */
149 struct list change_records
; /* data for the change */
152 static struct fd
*dir_get_fd( struct object
*obj
);
153 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
);
154 static void dir_dump( struct object
*obj
, int verbose
);
155 static void dir_destroy( struct object
*obj
);
156 static int dir_signaled( struct object
*obj
, struct thread
*thread
);
158 static const struct object_ops dir_ops
=
160 sizeof(struct dir
), /* size */
162 add_queue
, /* add_queue */
163 remove_queue
, /* remove_queue */
164 dir_signaled
, /* signaled */
165 no_satisfied
, /* satisfied */
166 no_signal
, /* signal */
167 dir_get_fd
, /* get_fd */
168 dir_map_access
, /* map_access */
169 no_lookup_name
, /* lookup_name */
170 no_close_handle
, /* close_handle */
171 dir_destroy
/* destroy */
174 static int dir_get_poll_events( struct fd
*fd
);
175 static int dir_get_info( struct fd
*fd
);
176 static void dir_cancel_async( struct fd
*fd
);
178 static const struct fd_ops dir_fd_ops
=
180 dir_get_poll_events
, /* get_poll_events */
181 default_poll_event
, /* poll_event */
182 no_flush
, /* flush */
183 dir_get_info
, /* get_file_info */
184 default_fd_queue_async
, /* queue_async */
185 dir_cancel_async
/* cancel_async */
188 static struct list change_list
= LIST_INIT(change_list
);
190 static void dnotify_adjust_changes( struct dir
*dir
)
192 #if defined(F_SETSIG) && defined(F_NOTIFY)
193 int fd
= get_unix_fd( dir
->fd
);
194 unsigned int filter
= dir
->filter
;
196 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
200 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
201 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
202 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
203 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
204 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
206 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
208 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
210 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
212 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
214 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
216 fcntl( fd
, F_NOTIFY
, val
);
220 /* insert change in the global list */
221 static inline void insert_change( struct dir
*dir
)
225 sigemptyset( &sigset
);
226 sigaddset( &sigset
, SIGIO
);
227 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
228 list_add_head( &change_list
, &dir
->entry
);
229 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
232 /* remove change from the global list */
233 static inline void remove_change( struct dir
*dir
)
237 sigemptyset( &sigset
);
238 sigaddset( &sigset
, SIGIO
);
239 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
240 list_remove( &dir
->entry
);
241 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
244 struct object
*create_dir_obj( struct fd
*fd
)
248 dir
= alloc_object( &dir_ops
);
252 list_init( &dir
->change_q
);
253 list_init( &dir
->change_records
);
259 dir
->inotify_fd
= NULL
;
263 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
268 static void dir_dump( struct object
*obj
, int verbose
)
270 struct dir
*dir
= (struct dir
*)obj
;
271 assert( obj
->ops
== &dir_ops
);
272 fprintf( stderr
, "Dirfile fd=%p event=%p filter=%08x\n",
273 dir
->fd
, dir
->event
, dir
->filter
);
276 static int dir_signaled( struct object
*obj
, struct thread
*thread
)
278 struct dir
*dir
= (struct dir
*)obj
;
279 assert (obj
->ops
== &dir_ops
);
280 return (dir
->event
== NULL
) && dir
->signaled
;
283 /* enter here directly from SIGIO signal handler */
284 void do_change_notify( int unix_fd
)
288 /* FIXME: this is O(n) ... probably can be improved */
289 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
291 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
292 interlocked_xchg_add( &dir
->notified
, 1 );
297 static void dir_signal_changed( struct dir
*dir
)
300 set_event( dir
->event
);
302 wake_up( &dir
->obj
, 0 );
305 /* SIGIO callback, called synchronously with the poll loop */
306 void sigio_callback(void)
310 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
312 long count
= interlocked_xchg( &dir
->notified
, 0 );
315 dir
->signaled
+= count
;
316 if (dir
->signaled
== count
) /* was it 0? */
317 dir_signal_changed( dir
);
322 static struct fd
*dir_get_fd( struct object
*obj
)
324 struct dir
*dir
= (struct dir
*)obj
;
325 assert( obj
->ops
== &dir_ops
);
326 return (struct fd
*)grab_object( dir
->fd
);
329 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
)
331 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
332 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
333 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
334 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
335 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
338 static struct change_record
*get_first_change_record( struct dir
*dir
)
340 struct list
*ptr
= list_head( &dir
->change_records
);
341 if (!ptr
) return NULL
;
343 return LIST_ENTRY( ptr
, struct change_record
, entry
);
346 static void dir_destroy( struct object
*obj
)
348 struct change_record
*record
;
349 struct dir
*dir
= (struct dir
*)obj
;
350 assert (obj
->ops
== &dir_ops
);
353 remove_change( dir
);
356 release_object( dir
->inotify_fd
);
358 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
359 while ((record
= get_first_change_record( dir
))) free( record
);
363 set_event( dir
->event
);
364 release_object( dir
->event
);
366 release_object( dir
->fd
);
370 get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
372 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
375 static int dir_get_poll_events( struct fd
*fd
)
380 static int dir_get_info( struct fd
*fd
)
385 static void dir_cancel_async( struct fd
*fd
)
387 struct dir
*dir
= (struct dir
*) get_fd_user( fd
);
388 async_terminate_queue( &dir
->change_q
, STATUS_CANCELLED
);
394 static int inotify_get_poll_events( struct fd
*fd
);
395 static void inotify_poll_event( struct fd
*fd
, int event
);
396 static int inotify_get_info( struct fd
*fd
);
398 static const struct fd_ops inotify_fd_ops
=
400 inotify_get_poll_events
, /* get_poll_events */
401 inotify_poll_event
, /* poll_event */
402 no_flush
, /* flush */
403 inotify_get_info
, /* get_file_info */
404 default_fd_queue_async
, /* queue_async */
405 default_fd_cancel_async
, /* cancel_async */
408 static int inotify_get_poll_events( struct fd
*fd
)
413 static void inotify_do_change_notify( struct dir
*dir
, struct inotify_event
*ie
)
415 struct change_record
*record
;
419 size_t len
= strlen(ie
->name
);
420 record
= malloc( offsetof(struct change_record
, name
[len
]) );
424 if( ie
->mask
& IN_CREATE
)
425 record
->action
= FILE_ACTION_ADDED
;
426 else if( ie
->mask
& IN_DELETE
)
427 record
->action
= FILE_ACTION_REMOVED
;
429 record
->action
= FILE_ACTION_MODIFIED
;
430 memcpy( record
->name
, ie
->name
, len
);
433 list_add_tail( &dir
->change_records
, &record
->entry
);
436 if (!list_empty( &dir
->change_q
))
437 async_terminate_head( &dir
->change_q
, STATUS_ALERTED
);
441 dir_signal_changed( dir
);
445 static void inotify_poll_event( struct fd
*fd
, int event
)
449 struct inotify_event
*ie
;
450 struct dir
*dir
= get_fd_user( fd
);
452 unix_fd
= get_unix_fd( fd
);
453 r
= read( unix_fd
, buffer
, sizeof buffer
);
456 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
460 for( ofs
= 0; ofs
< r
- offsetof(struct inotify_event
, name
); )
462 ie
= (struct inotify_event
*) &buffer
[ofs
];
465 ofs
+= offsetof( struct inotify_event
, name
[ie
->len
] );
467 inotify_do_change_notify( dir
, ie
);
471 static int inotify_get_info( struct fd
*fd
)
476 static inline struct fd
*create_inotify_fd( struct dir
*dir
)
480 unix_fd
= inotify_init();
483 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, &dir
->obj
);
486 static int inotify_adjust_changes( struct dir
*dir
)
488 unsigned int filter
= dir
->filter
;
489 unsigned int mask
= 0;
492 if (!dir
->inotify_fd
)
494 if (!(dir
->inotify_fd
= create_inotify_fd( dir
))) return 0;
497 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
498 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
);
499 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
500 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
501 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
503 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
505 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
507 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
509 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
511 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
514 sprintf( link
, "/proc/self/fd/%u", get_unix_fd( dir
->fd
) );
515 dir
->wd
= inotify_add_watch( get_unix_fd( dir
->inotify_fd
), link
, mask
);
517 set_fd_events( dir
->inotify_fd
, POLLIN
);
521 #endif /* USE_INOTIFY */
523 /* enable change notifications for a directory */
524 DECL_HANDLER(read_directory_changes
)
526 struct event
*event
= NULL
;
531 set_error(STATUS_INVALID_PARAMETER
);
535 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
539 /* possibly send changes through an event flag */
542 event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
547 /* discard the current data, and move onto the next event */
548 if (dir
->event
) release_object( dir
->event
);
551 /* requests don't timeout */
552 if ( req
->io_apc
&& !create_async( current
, NULL
, &dir
->change_q
,
553 req
->io_apc
, req
->io_user
, req
->io_sb
))
559 insert_change( dir
);
560 dir
->filter
= req
->filter
;
561 dir
->want_data
= req
->want_data
;
564 /* remove any notifications */
568 /* clear the event */
570 reset_event( event
);
572 /* setup the real notification */
574 if (!inotify_adjust_changes( dir
))
576 dnotify_adjust_changes( dir
);
578 set_error(STATUS_PENDING
);
581 release_object( dir
);
584 DECL_HANDLER(read_change
)
586 struct change_record
*record
;
589 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
593 if ((record
= get_first_change_record( dir
)) != NULL
)
595 reply
->action
= record
->action
;
596 set_reply_data( record
->name
, record
->len
);
600 set_error( STATUS_NO_DATA_DETECTED
);
604 dir_signal_changed( dir
);
606 release_object( dir
);