libwine: Set the default bindir and dlldir from argv0 if dladdr is not available.
[wine/multimedia.git] / server / change.c
blobaa8e0cb13fa0940e0b1d36427c312f9bca39c56b
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
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 <sys/types.h>
32 #include <limits.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #ifdef HAVE_SYS_ERRNO_H
36 #include <sys/errno.h>
37 #endif
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "thread.h"
46 #include "request.h"
47 #include "winternl.h"
49 /* dnotify support */
51 #ifdef linux
52 #ifndef F_NOTIFY
53 #define F_NOTIFY 1026
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 */
61 #endif
62 #endif
64 /* inotify support */
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 {
73 int wd;
74 unsigned int mask;
75 unsigned int cookie;
76 unsigned int len;
77 char name[1];
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 )
94 int ret;
95 __asm__ __volatile__( "int $0x80"
96 : "=a" (ret)
97 : "0" (SYS_inotify_init));
98 if (ret<0) { errno = -ret; ret = -1; }
99 return ret;
102 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
104 int ret;
105 __asm__ __volatile__( "pushl %%ebx;\n\t"
106 "movl %2,%%ebx;\n\t"
107 "int $0x80;\n\t"
108 "popl %%ebx"
109 : "=a" (ret) : "0" (SYS_inotify_add_watch),
110 "r" (fd), "c" (name), "d" (mask) );
111 if (ret<0) { errno = -ret; ret = -1; }
112 return ret;
115 static inline int inotify_remove_watch( int fd, int wd )
117 int ret;
118 __asm__ __volatile__( "pushl %%ebx;\n\t"
119 "movl %2,%%ebx;\n\t"
120 "int $0x80;\n\t"
121 "popl %%ebx"
122 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
123 "r" (fd), "c" (wd) );
124 if (ret<0) { errno = -ret; ret = -1; }
125 return ret;
128 #define USE_INOTIFY
130 #endif
132 struct inode;
134 static void free_inode( struct inode *inode );
136 static struct fd *inotify_fd;
138 struct change_record {
139 struct list entry;
140 int action;
141 int len;
142 char name[1];
145 struct dir
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 */
150 struct event *event;
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 */
170 dir_dump, /* dump */
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;
204 unsigned int val;
205 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
206 return;
208 val = DN_MULTISHOT;
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)
214 val |= DN_ATTRIB;
215 if (filter & FILE_NOTIFY_CHANGE_SIZE)
216 val |= DN_MODIFY;
217 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
218 val |= DN_MODIFY;
219 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
220 val |= DN_ACCESS;
221 if (filter & FILE_NOTIFY_CHANGE_CREATION)
222 val |= DN_CREATE;
223 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
224 val |= DN_ATTRIB;
225 fcntl( fd, F_NOTIFY, val );
226 #endif
229 /* insert change in the global list */
230 static inline void insert_change( struct dir *dir )
232 sigset_t sigset;
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 )
244 sigset_t sigset;
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 )
271 struct dir *dir;
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 );
278 break;
282 static void dir_signal_changed( struct dir *dir )
284 if (dir->event)
285 set_event( dir->event );
286 else
287 wake_up( &dir->obj, 0 );
290 /* SIGIO callback, called synchronously with the poll loop */
291 void sigio_callback(void)
293 struct dir *dir;
295 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
297 long count = interlocked_xchg( &dir->notified, 0 );
298 if (count)
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;
327 list_remove( ptr );
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);
337 if (dir->filter)
338 remove_change( dir );
340 if (dir->inode)
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 );
349 if (dir->event)
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 );
359 inotify_fd = NULL;
363 static struct dir *
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 )
371 return 0;
374 static int dir_get_info( struct fd *fd )
376 return 0;
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 );
386 #ifdef USE_INOTIFY
388 #define HASH_SIZE 31
390 struct inode {
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 ];
405 struct inode *inode;
407 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
408 if (inode->wd == wd)
409 return inode;
411 return NULL;
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 );
422 struct inode *inode;
424 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
425 if (inode->ino == ino && inode->dev == dev)
426 return inode;
428 return NULL;
431 static struct inode *create_inode( dev_t dev, ino_t ino )
433 struct inode *inode;
435 inode = malloc( sizeof *inode );
436 if (inode)
438 list_init( &inode->dirs );
439 inode->ino = ino;
440 inode->dev = dev;
441 inode->wd = -1;
442 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
444 return inode;
447 static void inode_set_wd( struct inode *inode, int wd )
449 if (inode->wd != -1)
450 list_remove( &inode->wd_entry );
451 inode->wd = wd;
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 ))
458 return;
460 if (inode->wd != -1)
462 inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
463 list_remove( &inode->wd_entry );
465 list_remove( &inode->ino_entry );
466 free( inode );
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 )
485 return POLLIN;
488 static void inotify_do_change_notify( struct dir *dir, struct inotify_event *ie )
490 struct change_record *record;
492 if (dir->want_data)
494 size_t len = strlen(ie->name);
495 record = malloc( offsetof(struct change_record, name[len]) );
496 if (!record)
497 return;
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;
503 else
504 record->action = FILE_ACTION_MODIFIED;
505 memcpy( record->name, ie->name, len );
506 record->len = 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 );
513 else
515 dir->signaled++;
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;
535 return filter;
538 static void inotify_notify_all( struct inotify_event *ie )
540 struct inode *inode;
541 struct dir *dir;
542 unsigned int filter;
544 inode = inode_from_wd( ie->wd );
545 if (!inode)
547 fprintf( stderr, "no inode matches %d\n", ie->wd);
548 return;
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 )
560 int r, ofs, unix_fd;
561 char buffer[0x1000];
562 struct inotify_event *ie;
564 unix_fd = get_unix_fd( fd );
565 r = read( unix_fd, buffer, sizeof buffer );
566 if (r < 0)
568 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
569 return;
572 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
574 ie = (struct inotify_event*) &buffer[ofs];
575 if (!ie->len)
576 break;
577 ofs += offsetof( struct inotify_event, name[ie->len] );
578 if (ofs > r) break;
579 inotify_notify_all( ie );
583 static int inotify_get_info( struct fd *fd )
585 return 0;
588 static inline struct fd *create_inotify_fd( void )
590 int unix_fd;
592 unix_fd = inotify_init();
593 if (unix_fd<0)
594 return NULL;
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)
607 mask |= IN_ATTRIB;
608 if (filter & FILE_NOTIFY_CHANGE_SIZE)
609 mask |= IN_MODIFY;
610 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
611 mask |= IN_MODIFY;
612 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
613 mask |= IN_ACCESS;
614 if (filter & FILE_NOTIFY_CHANGE_CREATION)
615 mask |= IN_CREATE;
616 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
617 mask |= IN_ATTRIB;
619 return mask;
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 ) );
626 if (wd != -1)
627 set_fd_events( inotify_fd, POLLIN );
628 return wd;
631 static unsigned int filter_from_inode( struct inode *inode )
633 unsigned int filter = 0;
634 struct dir *dir;
636 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
637 filter |= dir->filter;
639 return filter;
642 static int init_inotify( void )
644 int i;
646 if (inotify_fd)
647 return 1;
649 inotify_fd = create_inotify_fd();
650 if (!inotify_fd)
651 return 0;
653 for (i=0; i<HASH_SIZE; i++)
655 list_init( &inode_hash[i] );
656 list_init( &wd_hash[i] );
659 return 1;
662 static int inotify_adjust_changes( struct dir *dir )
664 unsigned int filter;
665 struct inode *inode;
666 struct stat st;
667 char path[32];
668 int wd, unix_fd;
670 if (!inotify_fd)
671 return 0;
673 unix_fd = get_unix_fd( dir->fd );
675 inode = dir->inode;
676 if (!inode)
678 /* check if this fd is already being watched */
679 if (-1 == fstat( unix_fd, &st ))
680 return 0;
682 inode = get_inode( st.st_dev, st.st_ino );
683 if (!inode)
684 inode = create_inode( st.st_dev, st.st_ino );
685 if (!inode)
686 return 0;
687 list_add_tail( &inode->dirs, &dir->in_entry );
688 dir->inode = inode;
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 );
699 return 1;
702 #else
704 static int init_inotify( void )
706 return 0;
709 static int inotify_adjust_changes( struct dir *dir )
711 return 0;
714 static void free_inode( struct inode *inode )
716 assert( 0 );
719 #endif /* USE_INOTIFY */
721 struct object *create_dir_obj( struct fd *fd )
723 struct dir *dir;
725 dir = alloc_object( &dir_ops );
726 if (!dir)
727 return NULL;
729 list_init( &dir->change_q );
730 list_init( &dir->change_records );
731 dir->event = NULL;
732 dir->filter = 0;
733 dir->notified = 0;
734 dir->signaled = 0;
735 dir->want_data = 0;
736 dir->inode = NULL;
737 grab_object( fd );
738 dir->fd = fd;
739 set_fd_user( fd, &dir_fd_ops, &dir->obj );
741 return &dir->obj;
744 /* enable change notifications for a directory */
745 DECL_HANDLER(read_directory_changes)
747 struct event *event = NULL;
748 struct dir *dir;
750 if (!req->filter)
752 set_error(STATUS_INVALID_PARAMETER);
753 return;
756 dir = get_dir_obj( current->process, req->handle, 0 );
757 if (!dir)
758 return;
760 /* possibly send changes through an event flag */
761 if (req->event)
763 event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
764 if (!event)
765 goto end;
768 /* discard the current data, and move onto the next event */
769 if (dir->event) release_object( dir->event );
770 dir->event = 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 ))
775 return;
777 /* assign it once */
778 if (!dir->filter)
780 init_inotify();
781 insert_change( dir );
782 dir->filter = req->filter;
783 dir->want_data = req->want_data;
786 /* remove any notifications */
787 if (dir->signaled>0)
788 dir->signaled--;
790 /* clear the event */
791 if (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);
800 end:
801 release_object( dir );
804 DECL_HANDLER(read_change)
806 struct change_record *record;
807 struct dir *dir;
809 dir = get_dir_obj( current->process, req->handle, 0 );
810 if (!dir)
811 return;
813 if ((record = get_first_change_record( dir )) != NULL)
815 reply->action = record->action;
816 set_reply_data( record->name, record->len );
817 free( record );
819 else
820 set_error( STATUS_NO_DATA_DETECTED );
822 /* now signal it */
823 dir->signaled++;
824 dir_signal_changed( dir );
826 release_object( dir );