serialui: Turkish translation.
[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 change_record {
130     struct list entry;
131     int action;
132     int len;
133     char name[1];
134 };
135
136 struct dir
137 {
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 */
141     struct event  *event;
142     unsigned int   filter;   /* notification filter */
143     int            notified; /* SIGIO counter */
144     long           signaled; /* the file changed */
145     struct fd     *inotify_fd; /* inotify file descriptor */
146     int            wd;       /* inotify watch descriptor */
147     struct list    change_q; /* change readers */
148     struct list    change_records;   /* data for the change */
149 };
150
151 static struct fd *dir_get_fd( struct object *obj );
152 static unsigned int dir_map_access( struct object *obj, unsigned int access );
153 static void dir_dump( struct object *obj, int verbose );
154 static void dir_destroy( struct object *obj );
155 static int dir_signaled( struct object *obj, struct thread *thread );
156
157 static const struct object_ops dir_ops =
158 {
159     sizeof(struct dir),       /* size */
160     dir_dump,                 /* dump */
161     add_queue,                /* add_queue */
162     remove_queue,             /* remove_queue */
163     dir_signaled,             /* signaled */
164     no_satisfied,             /* satisfied */
165     no_signal,                /* signal */
166     dir_get_fd,               /* get_fd */
167     dir_map_access,           /* map_access */
168     no_lookup_name,           /* lookup_name */
169     no_close_handle,          /* close_handle */
170     dir_destroy               /* destroy */
171 };
172
173 static int dir_get_poll_events( struct fd *fd );
174 static int dir_get_info( struct fd *fd );
175 static void dir_cancel_async( struct fd *fd );
176
177 static const struct fd_ops dir_fd_ops =
178 {
179     dir_get_poll_events,      /* get_poll_events */
180     default_poll_event,       /* poll_event */
181     no_flush,                 /* flush */
182     dir_get_info,             /* get_file_info */
183     default_fd_queue_async,   /* queue_async */
184     dir_cancel_async          /* cancel_async */
185 };
186
187 static struct list change_list = LIST_INIT(change_list);
188
189 static void dnotify_adjust_changes( struct dir *dir )
190 {
191 #if defined(F_SETSIG) && defined(F_NOTIFY)
192     int fd = get_unix_fd( dir->fd );
193     unsigned int filter = dir->filter;
194     unsigned int val;
195     if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
196         return;
197
198     val = DN_MULTISHOT;
199     if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
200         val |= DN_RENAME | DN_DELETE | DN_CREATE;
201     if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
202         val |= DN_RENAME | DN_DELETE | DN_CREATE;
203     if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
204         val |= DN_ATTRIB;
205     if (filter & FILE_NOTIFY_CHANGE_SIZE)
206         val |= DN_MODIFY;
207     if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
208         val |= DN_MODIFY;
209     if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
210         val |= DN_ACCESS;
211     if (filter & FILE_NOTIFY_CHANGE_CREATION)
212         val |= DN_CREATE;
213     if (filter & FILE_NOTIFY_CHANGE_SECURITY)
214         val |= DN_ATTRIB;
215     fcntl( fd, F_NOTIFY, val );
216 #endif
217 }
218
219 /* insert change in the global list */
220 static inline void insert_change( struct dir *dir )
221 {
222     sigset_t sigset;
223
224     sigemptyset( &sigset );
225     sigaddset( &sigset, SIGIO );
226     sigprocmask( SIG_BLOCK, &sigset, NULL );
227     list_add_head( &change_list, &dir->entry );
228     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
229 }
230
231 /* remove change from the global list */
232 static inline void remove_change( struct dir *dir )
233 {
234     sigset_t sigset;
235
236     sigemptyset( &sigset );
237     sigaddset( &sigset, SIGIO );
238     sigprocmask( SIG_BLOCK, &sigset, NULL );
239     list_remove( &dir->entry );
240     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
241 }
242
243 struct object *create_dir_obj( struct fd *fd )
244 {
245     struct dir *dir;
246
247     dir = alloc_object( &dir_ops );
248     if (!dir)
249         return NULL;
250
251     list_init( &dir->change_q );
252     list_init( &dir->change_records );
253     dir->event = NULL;
254     dir->filter = 0;
255     dir->notified = 0;
256     dir->signaled = 0;
257     dir->inotify_fd = NULL;
258     dir->wd = -1;
259     grab_object( fd );
260     dir->fd = fd;
261     set_fd_user( fd, &dir_fd_ops, &dir->obj );
262
263     return &dir->obj;
264 }
265
266 static void dir_dump( struct object *obj, int verbose )
267 {
268     struct dir *dir = (struct dir *)obj;
269     assert( obj->ops == &dir_ops );
270     fprintf( stderr, "Dirfile fd=%p event=%p filter=%08x\n",
271              dir->fd, dir->event, dir->filter );
272 }
273
274 static int dir_signaled( struct object *obj, struct thread *thread )
275 {
276     struct dir *dir = (struct dir *)obj;
277     assert (obj->ops == &dir_ops);
278     return (dir->event == NULL) && dir->signaled;
279 }
280
281 /* enter here directly from SIGIO signal handler */
282 void do_change_notify( int unix_fd )
283 {
284     struct dir *dir;
285
286     /* FIXME: this is O(n) ... probably can be improved */
287     LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
288     {
289         if (get_unix_fd( dir->fd ) != unix_fd) continue;
290         interlocked_xchg_add( &dir->notified, 1 );
291         break;
292     }
293 }
294
295 static void dir_signal_changed( struct dir *dir )
296 {
297     if (dir->event)
298         set_event( dir->event );
299     else
300         wake_up( &dir->obj, 0 );
301 }
302
303 /* SIGIO callback, called synchronously with the poll loop */
304 void sigio_callback(void)
305 {
306     struct dir *dir;
307
308     LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
309     {
310         long count = interlocked_xchg( &dir->notified, 0 );
311         if (count)
312         {
313             dir->signaled += count;
314             if (dir->signaled == count)  /* was it 0? */
315                 dir_signal_changed( dir );
316         }
317     }
318 }
319
320 static struct fd *dir_get_fd( struct object *obj )
321 {
322     struct dir *dir = (struct dir *)obj;
323     assert( obj->ops == &dir_ops );
324     return (struct fd *)grab_object( dir->fd );
325 }
326
327 static unsigned int dir_map_access( struct object *obj, unsigned int access )
328 {
329     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
330     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
331     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
332     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
333     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
334 }
335
336 static struct change_record *get_first_change_record( struct dir *dir )
337 {
338     struct list *ptr = list_head( &dir->change_records );
339     if (!ptr) return NULL;
340     list_remove( ptr );
341     return LIST_ENTRY( ptr, struct change_record, entry );
342 }
343
344 static void dir_destroy( struct object *obj )
345 {
346     struct change_record *record;
347     struct dir *dir = (struct dir *)obj;
348     assert (obj->ops == &dir_ops);
349
350     if (dir->filter)
351         remove_change( dir );
352
353     if (dir->inotify_fd)
354         release_object( dir->inotify_fd );
355
356     async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
357     while ((record = get_first_change_record( dir ))) free( record );
358
359     if (dir->event)
360     {
361         set_event( dir->event );
362         release_object( dir->event );
363     }
364     release_object( dir->fd );
365 }
366
367 static struct dir *
368 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
369 {
370     return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
371 }
372
373 static int dir_get_poll_events( struct fd *fd )
374 {
375     return 0;
376 }
377
378 static int dir_get_info( struct fd *fd )
379 {
380     return 0;
381 }
382
383 static void dir_cancel_async( struct fd *fd )
384 {
385     struct dir *dir = (struct dir *) get_fd_user( fd );
386     async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
387 }
388
389
390 #ifdef USE_INOTIFY
391
392 static int inotify_get_poll_events( struct fd *fd );
393 static void inotify_poll_event( struct fd *fd, int event );
394 static int inotify_get_info( struct fd *fd );
395
396 static const struct fd_ops inotify_fd_ops =
397 {
398     inotify_get_poll_events,  /* get_poll_events */
399     inotify_poll_event,       /* poll_event */
400     no_flush,                 /* flush */
401     inotify_get_info,         /* get_file_info */
402     default_fd_queue_async,   /* queue_async */
403     default_fd_cancel_async,  /* cancel_async */
404 };
405
406 static int inotify_get_poll_events( struct fd *fd )
407 {
408     return POLLIN;
409 }
410
411 static void inotify_do_change_notify( struct dir *dir, struct inotify_event *ie )
412 {
413     struct change_record *record;
414
415     record = malloc( sizeof (*record) + ie->len - 1 ) ;
416     if (!record)
417         return;
418
419     if( ie->mask & IN_CREATE )
420         record->action = FILE_ACTION_ADDED;
421     else if( ie->mask & IN_DELETE )
422         record->action = FILE_ACTION_REMOVED;
423     else
424         record->action = FILE_ACTION_MODIFIED;
425     memcpy( record->name, ie->name, ie->len );
426     record->len = strlen( ie->name );
427
428     list_add_tail( &dir->change_records, &record->entry );
429
430     if (!list_empty( &dir->change_q ))
431         async_terminate_head( &dir->change_q, STATUS_ALERTED );
432     else
433     {
434         dir->signaled++;
435         dir_signal_changed( dir );
436     }
437 }
438
439 static void inotify_poll_event( struct fd *fd, int event )
440 {
441     int r, ofs, unix_fd;
442     char buffer[0x1000];
443     struct inotify_event *ie;
444     struct dir *dir = get_fd_user( fd );
445
446     unix_fd = get_unix_fd( fd );
447     r = read( unix_fd, buffer, sizeof buffer );
448     if (r < 0)
449     {
450         fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
451         return;
452     }
453
454     for( ofs = 0; ofs < r; )
455     {
456         ie = (struct inotify_event*) &buffer[ofs];
457         if (!ie->len)
458             break;
459         inotify_do_change_notify( dir, ie );
460         ofs += (sizeof (*ie) + ie->len - 1);
461     }
462 }
463
464 static int inotify_get_info( struct fd *fd )
465 {
466     return 0;
467 }
468
469 static inline struct fd *create_inotify_fd( struct dir *dir )
470 {
471     int unix_fd;
472
473     unix_fd = inotify_init();
474     if (unix_fd<0)
475         return NULL;
476     return create_anonymous_fd( &inotify_fd_ops, unix_fd, &dir->obj );
477 }
478
479 static int inotify_adjust_changes( struct dir *dir )
480 {
481     unsigned int filter = dir->filter;
482     unsigned int mask = 0;
483     char link[32];
484
485     if (!dir->inotify_fd)
486     {
487         if (!(dir->inotify_fd = create_inotify_fd( dir ))) return 0;
488     }
489
490     if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
491         mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE);
492     if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
493         mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
494     if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
495         mask |= IN_ATTRIB;
496     if (filter & FILE_NOTIFY_CHANGE_SIZE)
497         mask |= IN_MODIFY;
498     if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
499         mask |= IN_MODIFY;
500     if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
501         mask |= IN_ACCESS;
502     if (filter & FILE_NOTIFY_CHANGE_CREATION)
503         mask |= IN_CREATE;
504     if (filter & FILE_NOTIFY_CHANGE_SECURITY)
505         mask |= IN_ATTRIB;
506
507     sprintf( link, "/proc/self/fd/%u", get_unix_fd( dir->fd ) );
508     dir->wd = inotify_add_watch( get_unix_fd( dir->inotify_fd ), link, mask );
509     if (dir->wd != -1)
510         set_fd_events( dir->inotify_fd, POLLIN );
511     return 1;
512 }
513
514 #endif  /* USE_INOTIFY */
515
516 /* enable change notifications for a directory */
517 DECL_HANDLER(read_directory_changes)
518 {
519     struct event *event = NULL;
520     struct dir *dir;
521
522     if (!req->filter)
523     {
524         set_error(STATUS_INVALID_PARAMETER);
525         return;
526     }
527
528     dir = get_dir_obj( current->process, req->handle, 0 );
529     if (!dir)
530         return;
531
532     /* possibly send changes through an event flag */
533     if (req->event)
534     {
535         event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
536         if (!event)
537             goto end;
538     }
539
540     /* discard the current data, and move onto the next event */
541     if (dir->event) release_object( dir->event );
542     dir->event = event;
543
544     /* requests don't timeout */
545     if ( req->io_apc && !create_async( current, NULL, &dir->change_q,
546                         req->io_apc, req->io_user, req->io_sb ))
547         return;
548
549     /* assign it once */
550     if (!dir->filter)
551     {
552         insert_change( dir );
553         dir->filter = req->filter;
554     }
555
556     /* remove any notifications */
557     if (dir->signaled>0)
558         dir->signaled--;
559
560     /* setup the real notification */
561 #ifdef USE_INOTIFY
562     if (!inotify_adjust_changes( dir ))
563 #endif
564         dnotify_adjust_changes( dir );
565
566     set_error(STATUS_PENDING);
567
568 end:
569     release_object( dir );
570 }
571
572 DECL_HANDLER(read_change)
573 {
574     struct change_record *record;
575     struct dir *dir;
576
577     dir = get_dir_obj( current->process, req->handle, 0 );
578     if (!dir)
579         return;
580
581     if ((record = get_first_change_record( dir )) != NULL)
582     {
583         reply->action = record->action;
584         set_reply_data( record->name, record->len );
585         free( record );
586     }
587     else
588         set_error( STATUS_NO_DATA_DETECTED );
589
590     /* now signal it */
591     dir->signaled++;
592     dir_signal_changed( dir );
593
594     release_object( dir );
595 }