server: Fixed compile without inotify.
[wine] / server / change.c
1 /*
2  * Server-side change notification management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2006 Mike McCormack
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
34 #endif
35
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "windef.h"
39
40 #include "file.h"
41 #include "handle.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "winternl.h"
45
46 /* dnotify support */
47
48 #ifdef linux
49 #ifndef F_NOTIFY
50 #define F_NOTIFY 1026
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 */
58 #endif
59 #endif
60
61 /* inotify support */
62
63 #if defined(__linux__) && defined(__i386__)
64
65 #define SYS_inotify_init        291
66 #define SYS_inotify_add_watch   292
67 #define SYS_inotify_rm_watch    293
68
69 struct inotify_event {
70     int           wd;
71     unsigned int  mask;
72     unsigned int  cookie;
73     unsigned int  len;
74     char          name[1];
75 };
76
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
88
89 static inline int inotify_init( void )
90 {
91     int ret;
92     __asm__ __volatile__( "int $0x80"
93                           : "=a" (ret)
94                           : "0" (SYS_inotify_init));
95     if (ret<0) { errno = -ret; ret = -1; }
96     return ret;
97 }
98
99 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
100 {
101     int ret;
102     __asm__ __volatile__( "pushl %%ebx;\n\t"
103                           "movl %2,%%ebx;\n\t"
104                           "int $0x80;\n\t"
105                           "popl %%ebx"
106                           : "=a" (ret) : "0" (SYS_inotify_add_watch),
107                             "r" (fd), "c" (name), "d" (mask) );
108     if (ret<0) { errno = -ret; ret = -1; }
109     return ret;
110 }
111
112 static inline int inotify_remove_watch( int fd, int wd )
113 {
114     int ret;
115     __asm__ __volatile__( "pushl %%ebx;\n\t"
116                           "movl %2,%%ebx;\n\t"
117                           "int $0x80;\n\t"
118                           "popl %%ebx"
119                           : "=a" (ret) : "0" (SYS_inotify_rm_watch),
120                             "r" (fd), "c" (wd) );
121     if (ret<0) { errno = -ret; ret = -1; }
122     return ret;
123 }
124
125 #define USE_INOTIFY
126
127 #endif
128
129 struct dir
130 {
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 */
134     struct event  *event;
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 */
140 };
141
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 );
147
148 static const struct object_ops dir_ops =
149 {
150     sizeof(struct dir),       /* size */
151     dir_dump,                 /* dump */
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 */
162 };
163
164 static int dir_get_poll_events( struct fd *fd );
165 static int dir_get_info( struct fd *fd );
166
167 static const struct fd_ops dir_fd_ops =
168 {
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 */
175 };
176
177 static struct list change_list = LIST_INIT(change_list);
178
179 static void dnotify_adjust_changes( struct dir *dir )
180 {
181 #if defined(F_SETSIG) && defined(F_NOTIFY)
182     int fd = get_unix_fd( dir->fd );
183     unsigned int filter = dir->filter;
184     unsigned int val;
185     if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
186         return;
187
188     val = DN_MULTISHOT;
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)
194         val |= DN_ATTRIB;
195     if (filter & FILE_NOTIFY_CHANGE_SIZE)
196         val |= DN_MODIFY;
197     if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
198         val |= DN_MODIFY;
199     if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
200         val |= DN_ACCESS;
201     if (filter & FILE_NOTIFY_CHANGE_CREATION)
202         val |= DN_CREATE;
203     if (filter & FILE_NOTIFY_CHANGE_SECURITY)
204         val |= DN_ATTRIB;
205     fcntl( fd, F_NOTIFY, val );
206 #endif
207 }
208
209 /* insert change in the global list */
210 static inline void insert_change( struct dir *dir )
211 {
212     sigset_t sigset;
213
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 );
219 }
220
221 /* remove change from the global list */
222 static inline void remove_change( struct dir *dir )
223 {
224     sigset_t sigset;
225
226     sigemptyset( &sigset );
227     sigaddset( &sigset, SIGIO );
228     sigprocmask( SIG_BLOCK, &sigset, NULL );
229     list_remove( &dir->entry );
230     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
231 }
232
233 struct object *create_dir_obj( struct fd *fd )
234 {
235     struct dir *dir;
236
237     dir = alloc_object( &dir_ops );
238     if (!dir)
239         return NULL;
240
241     dir->event = NULL;
242     dir->filter = 0;
243     dir->notified = 0;
244     dir->signaled = 0;
245     dir->inotify_fd = NULL;
246     dir->wd = -1;
247     grab_object( fd );
248     dir->fd = fd;
249     set_fd_user( fd, &dir_fd_ops, &dir->obj );
250
251     return &dir->obj;
252 }
253
254 static void dir_dump( struct object *obj, int verbose )
255 {
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 );
260 }
261
262 static int dir_signaled( struct object *obj, struct thread *thread )
263 {
264     struct dir *dir = (struct dir *)obj;
265     assert (obj->ops == &dir_ops);
266     return (dir->event == NULL) && dir->signaled;
267 }
268
269 /* enter here directly from SIGIO signal handler */
270 void do_change_notify( int unix_fd )
271 {
272     struct dir *dir;
273
274     /* FIXME: this is O(n) ... probably can be improved */
275     LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
276     {
277         if (get_unix_fd( dir->fd ) != unix_fd) continue;
278         interlocked_xchg_add( &dir->notified, 1 );
279         break;
280     }
281 }
282
283 static void dir_signal_changed( struct dir *dir )
284 {
285     if (dir->event)
286         set_event( dir->event );
287     else
288         wake_up( &dir->obj, 0 );
289 }
290
291 /* SIGIO callback, called synchronously with the poll loop */
292 void sigio_callback(void)
293 {
294     struct dir *dir;
295
296     LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
297     {
298         long count = interlocked_xchg( &dir->notified, 0 );
299         if (count)
300         {
301             dir->signaled += count;
302             if (dir->signaled == count)  /* was it 0? */
303                 dir_signal_changed( dir );
304         }
305     }
306 }
307
308 static struct fd *dir_get_fd( struct object *obj )
309 {
310     struct dir *dir = (struct dir *)obj;
311     assert( obj->ops == &dir_ops );
312     return (struct fd *)grab_object( dir->fd );
313 }
314
315 static unsigned int dir_map_access( struct object *obj, unsigned int access )
316 {
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);
322 }
323
324 static void dir_destroy( struct object *obj )
325 {
326     struct dir *dir = (struct dir *)obj;
327     assert (obj->ops == &dir_ops);
328
329     if (dir->filter)
330         remove_change( dir );
331
332     if (dir->inotify_fd)
333         release_object( dir->inotify_fd );
334
335     if (dir->event)
336     {
337         set_event( dir->event );
338         release_object( dir->event );
339     }
340     release_object( dir->fd );
341 }
342
343 static struct dir *
344 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
345 {
346     return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
347 }
348
349 static int dir_get_poll_events( struct fd *fd )
350 {
351     return 0;
352 }
353
354 static int dir_get_info( struct fd *fd )
355 {
356     return 0;
357 }
358
359
360 #ifdef USE_INOTIFY
361
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 );
365
366 static const struct fd_ops inotify_fd_ops =
367 {
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 */
374 };
375
376 static int inotify_get_poll_events( struct fd *fd )
377 {
378     return POLLIN;
379 }
380
381 static void inotify_do_change_notify( struct dir *dir )
382 {
383     dir->signaled++;
384     dir_signal_changed( dir );
385 }
386
387 static void inotify_poll_event( struct fd *fd, int event )
388 {
389     int r, ofs, unix_fd;
390     char buffer[0x1000];
391     struct inotify_event *ie;
392     struct dir *dir = get_fd_user( fd );
393
394     unix_fd = get_unix_fd( fd );
395     r = read( unix_fd, buffer, sizeof buffer );
396     if (r < 0)
397     {
398         fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
399         return;
400     }
401
402     for( ofs = 0; ofs < r; )
403     {
404         ie = (struct inotify_event*) &buffer[ofs];
405         if (!ie->len)
406             break;
407         inotify_do_change_notify( dir );
408         ofs += (sizeof (*ie) + ie->len - 1);
409     }
410 }
411
412 static int inotify_get_info( struct fd *fd )
413 {
414     return 0;
415 }
416
417 static inline struct fd *create_inotify_fd( struct dir *dir )
418 {
419     int unix_fd;
420
421     unix_fd = inotify_init();
422     if (unix_fd<0)
423         return NULL;
424     return create_anonymous_fd( &inotify_fd_ops, unix_fd, &dir->obj );
425 }
426
427 static int inotify_adjust_changes( struct dir *dir )
428 {
429     unsigned int filter = dir->filter;
430     unsigned int mask = 0;
431     char link[32];
432
433     if (!dir->inotify_fd)
434     {
435         if (!(dir->inotify_fd = create_inotify_fd( dir ))) return 0;
436     }
437
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)
443         mask |= IN_ATTRIB;
444     if (filter & FILE_NOTIFY_CHANGE_SIZE)
445         mask |= IN_MODIFY;
446     if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
447         mask |= IN_MODIFY;
448     if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
449         mask |= IN_ACCESS;
450     if (filter & FILE_NOTIFY_CHANGE_CREATION)
451         mask |= IN_CREATE;
452     if (filter & FILE_NOTIFY_CHANGE_SECURITY)
453         mask |= IN_ATTRIB;
454
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 );
457     if (dir->wd != -1)
458         set_fd_events( dir->inotify_fd, POLLIN );
459     return 1;
460 }
461
462 #endif  /* USE_INOTIFY */
463
464 /* enable change notifications for a directory */
465 DECL_HANDLER(read_directory_changes)
466 {
467     struct event *event = NULL;
468     struct dir *dir;
469
470     if (!req->filter)
471     {
472         set_error(STATUS_INVALID_PARAMETER);
473         return;
474     }
475
476     dir = get_dir_obj( current->process, req->handle, 0 );
477     if (!dir)
478         return;
479
480     /* possibly send changes through an event flag */
481     if (req->event)
482     {
483         event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
484         if (!event)
485             goto end;
486     }
487
488     /* discard the current data, and move onto the next event */
489     if (dir->event) release_object( dir->event );
490     dir->event = event;
491
492     /* assign it once */
493     if (!dir->filter)
494     {
495         insert_change( dir );
496         dir->filter = req->filter;
497     }
498
499     /* remove any notifications */
500     if (dir->signaled>0)
501         dir->signaled--;
502
503     /* setup the real notification */
504 #ifdef USE_INOTIFY
505     if (!inotify_adjust_changes( dir ))
506 #endif
507         dnotify_adjust_changes( dir );
508
509     set_error(STATUS_PENDING);
510
511 end:
512     release_object( dir );
513 }