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; }
131 struct object obj
; /* object header */
132 struct fd
*fd
; /* file descriptor to the directory */
133 struct list entry
; /* entry in global change notifications list */
135 unsigned int filter
; /* notification filter */
136 int notified
; /* SIGIO counter */
137 long signaled
; /* the file changed */
138 struct fd
*inotify_fd
; /* inotify file descriptor */
139 int wd
; /* inotify watch descriptor */
142 static struct fd
*dir_get_fd( struct object
*obj
);
143 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
);
144 static void dir_dump( struct object
*obj
, int verbose
);
145 static void dir_destroy( struct object
*obj
);
146 static int dir_signaled( struct object
*obj
, struct thread
*thread
);
148 static const struct object_ops dir_ops
=
150 sizeof(struct dir
), /* size */
152 add_queue
, /* add_queue */
153 remove_queue
, /* remove_queue */
154 dir_signaled
, /* signaled */
155 no_satisfied
, /* satisfied */
156 no_signal
, /* signal */
157 dir_get_fd
, /* get_fd */
158 dir_map_access
, /* map_access */
159 no_lookup_name
, /* lookup_name */
160 no_close_handle
, /* close_handle */
161 dir_destroy
/* destroy */
164 static int dir_get_poll_events( struct fd
*fd
);
165 static int dir_get_info( struct fd
*fd
);
167 static const struct fd_ops dir_fd_ops
=
169 dir_get_poll_events
, /* get_poll_events */
170 default_poll_event
, /* poll_event */
171 no_flush
, /* flush */
172 dir_get_info
, /* get_file_info */
173 default_fd_queue_async
, /* queue_async */
174 default_fd_cancel_async
/* cancel_async */
177 static struct list change_list
= LIST_INIT(change_list
);
179 static void dnotify_adjust_changes( struct dir
*dir
)
181 #if defined(F_SETSIG) && defined(F_NOTIFY)
182 int fd
= get_unix_fd( dir
->fd
);
183 unsigned int filter
= dir
->filter
;
185 if ( 0 > fcntl( fd
, F_SETSIG
, SIGIO
) )
189 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
190 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
191 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
192 val
|= DN_RENAME
| DN_DELETE
| DN_CREATE
;
193 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
195 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
197 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
199 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
201 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
203 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
205 fcntl( fd
, F_NOTIFY
, val
);
209 /* insert change in the global list */
210 static inline void insert_change( struct dir
*dir
)
214 sigemptyset( &sigset
);
215 sigaddset( &sigset
, SIGIO
);
216 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
217 list_add_head( &change_list
, &dir
->entry
);
218 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
221 /* remove change from the global list */
222 static inline void remove_change( struct dir
*dir
)
226 sigemptyset( &sigset
);
227 sigaddset( &sigset
, SIGIO
);
228 sigprocmask( SIG_BLOCK
, &sigset
, NULL
);
229 list_remove( &dir
->entry
);
230 sigprocmask( SIG_UNBLOCK
, &sigset
, NULL
);
233 struct object
*create_dir_obj( struct fd
*fd
)
237 dir
= alloc_object( &dir_ops
);
245 dir
->inotify_fd
= NULL
;
249 set_fd_user( fd
, &dir_fd_ops
, &dir
->obj
);
254 static void dir_dump( struct object
*obj
, int verbose
)
256 struct dir
*dir
= (struct dir
*)obj
;
257 assert( obj
->ops
== &dir_ops
);
258 fprintf( stderr
, "Dirfile fd=%p event=%p filter=%08x\n",
259 dir
->fd
, dir
->event
, dir
->filter
);
262 static int dir_signaled( struct object
*obj
, struct thread
*thread
)
264 struct dir
*dir
= (struct dir
*)obj
;
265 assert (obj
->ops
== &dir_ops
);
266 return (dir
->event
== NULL
) && dir
->signaled
;
269 /* enter here directly from SIGIO signal handler */
270 void do_change_notify( int unix_fd
)
274 /* FIXME: this is O(n) ... probably can be improved */
275 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
277 if (get_unix_fd( dir
->fd
) != unix_fd
) continue;
278 interlocked_xchg_add( &dir
->notified
, 1 );
283 static void dir_signal_changed( struct dir
*dir
)
286 set_event( dir
->event
);
288 wake_up( &dir
->obj
, 0 );
291 /* SIGIO callback, called synchronously with the poll loop */
292 void sigio_callback(void)
296 LIST_FOR_EACH_ENTRY( dir
, &change_list
, struct dir
, entry
)
298 long count
= interlocked_xchg( &dir
->notified
, 0 );
301 dir
->signaled
+= count
;
302 if (dir
->signaled
== count
) /* was it 0? */
303 dir_signal_changed( dir
);
308 static struct fd
*dir_get_fd( struct object
*obj
)
310 struct dir
*dir
= (struct dir
*)obj
;
311 assert( obj
->ops
== &dir_ops
);
312 return (struct fd
*)grab_object( dir
->fd
);
315 static unsigned int dir_map_access( struct object
*obj
, unsigned int access
)
317 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
318 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
319 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
320 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
321 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
324 static void dir_destroy( struct object
*obj
)
326 struct dir
*dir
= (struct dir
*)obj
;
327 assert (obj
->ops
== &dir_ops
);
330 remove_change( dir
);
333 release_object( dir
->inotify_fd
);
337 set_event( dir
->event
);
338 release_object( dir
->event
);
340 release_object( dir
->fd
);
344 get_dir_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
346 return (struct dir
*)get_handle_obj( process
, handle
, access
, &dir_ops
);
349 static int dir_get_poll_events( struct fd
*fd
)
354 static int dir_get_info( struct fd
*fd
)
362 static int inotify_get_poll_events( struct fd
*fd
);
363 static void inotify_poll_event( struct fd
*fd
, int event
);
364 static int inotify_get_info( struct fd
*fd
);
366 static const struct fd_ops inotify_fd_ops
=
368 inotify_get_poll_events
, /* get_poll_events */
369 inotify_poll_event
, /* poll_event */
370 no_flush
, /* flush */
371 inotify_get_info
, /* get_file_info */
372 default_fd_queue_async
, /* queue_async */
373 default_fd_cancel_async
, /* cancel_async */
376 static int inotify_get_poll_events( struct fd
*fd
)
381 static void inotify_do_change_notify( struct dir
*dir
)
384 dir_signal_changed( dir
);
387 static void inotify_poll_event( struct fd
*fd
, int event
)
391 struct inotify_event
*ie
;
392 struct dir
*dir
= get_fd_user( fd
);
394 unix_fd
= get_unix_fd( fd
);
395 r
= read( unix_fd
, buffer
, sizeof buffer
);
398 fprintf(stderr
,"inotify_poll_event(): inotify read failed!\n");
402 for( ofs
= 0; ofs
< r
; )
404 ie
= (struct inotify_event
*) &buffer
[ofs
];
407 inotify_do_change_notify( dir
);
408 ofs
+= (sizeof (*ie
) + ie
->len
- 1);
412 static int inotify_get_info( struct fd
*fd
)
417 static inline struct fd
*create_inotify_fd( struct dir
*dir
)
421 unix_fd
= inotify_init();
424 return create_anonymous_fd( &inotify_fd_ops
, unix_fd
, &dir
->obj
);
427 static int inotify_adjust_changes( struct dir
*dir
)
429 unsigned int filter
= dir
->filter
;
430 unsigned int mask
= 0;
433 if (!dir
->inotify_fd
)
435 if (!(dir
->inotify_fd
= create_inotify_fd( dir
))) return 0;
438 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
439 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
);
440 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
441 mask
|= (IN_MOVED_FROM
| IN_MOVED_TO
| IN_DELETE
| IN_CREATE
| IN_DELETE_SELF
);
442 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
444 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
446 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
448 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
450 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
452 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
455 sprintf( link
, "/proc/self/fd/%u", get_unix_fd( dir
->fd
) );
456 dir
->wd
= inotify_add_watch( get_unix_fd( dir
->inotify_fd
), link
, mask
);
458 set_fd_events( dir
->inotify_fd
, POLLIN
);
462 #endif /* USE_INOTIFY */
464 /* enable change notifications for a directory */
465 DECL_HANDLER(read_directory_changes
)
467 struct event
*event
= NULL
;
472 set_error(STATUS_INVALID_PARAMETER
);
476 dir
= get_dir_obj( current
->process
, req
->handle
, 0 );
480 /* possibly send changes through an event flag */
483 event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
488 /* discard the current data, and move onto the next event */
489 if (dir
->event
) release_object( dir
->event
);
495 insert_change( dir
);
496 dir
->filter
= req
->filter
;
499 /* remove any notifications */
503 /* setup the real notification */
505 if (!inotify_adjust_changes( dir
))
507 dnotify_adjust_changes( dir
);
509 set_error(STATUS_PENDING
);
512 release_object( dir
);