amstream: Increase parent IAMMediaStream refcount in IDirectDrawMediaStream::CreateSa...
[wine.git] / server / change.c
bloba8f3329c722fdf1afb2aaa83b3b768ffd1a35880
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_POLL_H
36 # include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_INOTIFY_H
39 #include <sys/inotify.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
46 #include "file.h"
47 #include "handle.h"
48 #include "thread.h"
49 #include "request.h"
50 #include "process.h"
51 #include "security.h"
52 #include "winternl.h"
54 /* dnotify support */
56 #ifdef linux
57 #ifndef F_NOTIFY
58 #define F_NOTIFY 1026
59 #define DN_ACCESS 0x00000001 /* File accessed */
60 #define DN_MODIFY 0x00000002 /* File modified */
61 #define DN_CREATE 0x00000004 /* File created */
62 #define DN_DELETE 0x00000008 /* File removed */
63 #define DN_RENAME 0x00000010 /* File renamed */
64 #define DN_ATTRIB 0x00000020 /* File changed attributes */
65 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
66 #endif
67 #endif
69 /* inotify support */
71 struct inode;
73 static void free_inode( struct inode *inode );
75 static struct fd *inotify_fd;
77 struct change_record {
78 struct list entry;
79 unsigned int cookie;
80 struct filesystem_event event;
83 struct dir
85 struct object obj; /* object header */
86 struct fd *fd; /* file descriptor to the directory */
87 mode_t mode; /* file stat.st_mode */
88 uid_t uid; /* file stat.st_uid */
89 struct list entry; /* entry in global change notifications list */
90 unsigned int filter; /* notification filter */
91 volatile int notified; /* SIGIO counter */
92 int want_data; /* return change data */
93 int subtree; /* do we want to watch subdirectories? */
94 struct list change_records; /* data for the change */
95 struct list in_entry; /* entry in the inode dirs list */
96 struct inode *inode; /* inode of the associated directory */
97 struct process *client_process; /* client process that has a cache for this directory */
98 int client_entry; /* entry in client process cache */
101 static struct fd *dir_get_fd( struct object *obj );
102 static struct security_descriptor *dir_get_sd( struct object *obj );
103 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
104 unsigned int set_info );
105 static void dir_dump( struct object *obj, int verbose );
106 static struct object_type *dir_get_type( struct object *obj );
107 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
108 static void dir_destroy( struct object *obj );
110 static const struct object_ops dir_ops =
112 sizeof(struct dir), /* size */
113 dir_dump, /* dump */
114 dir_get_type, /* get_type */
115 add_queue, /* add_queue */
116 remove_queue, /* remove_queue */
117 default_fd_signaled, /* signaled */
118 no_satisfied, /* satisfied */
119 no_signal, /* signal */
120 dir_get_fd, /* get_fd */
121 default_fd_map_access, /* map_access */
122 dir_get_sd, /* get_sd */
123 dir_set_sd, /* set_sd */
124 no_lookup_name, /* lookup_name */
125 no_link_name, /* link_name */
126 NULL, /* unlink_name */
127 no_open_file, /* open_file */
128 no_kernel_obj_list, /* get_kernel_obj_list */
129 dir_close_handle, /* close_handle */
130 dir_destroy /* destroy */
133 static int dir_get_poll_events( struct fd *fd );
134 static enum server_fd_type dir_get_fd_type( struct fd *fd );
136 static const struct fd_ops dir_fd_ops =
138 dir_get_poll_events, /* get_poll_events */
139 default_poll_event, /* poll_event */
140 dir_get_fd_type, /* get_fd_type */
141 no_fd_read, /* read */
142 no_fd_write, /* write */
143 no_fd_flush, /* flush */
144 default_fd_get_file_info, /* get_file_info */
145 no_fd_get_volume_info, /* get_volume_info */
146 default_fd_ioctl, /* ioctl */
147 default_fd_queue_async, /* queue_async */
148 default_fd_reselect_async /* reselect_async */
151 static struct list change_list = LIST_INIT(change_list);
153 /* per-process structure to keep track of cache entries on the client size */
154 struct dir_cache
156 unsigned int size;
157 unsigned int count;
158 unsigned char state[1];
161 enum dir_cache_state
163 DIR_CACHE_STATE_FREE,
164 DIR_CACHE_STATE_INUSE,
165 DIR_CACHE_STATE_RELEASED
168 /* return an array of cache entries that can be freed on the client side */
169 static int *get_free_dir_cache_entries( struct process *process, data_size_t *size )
171 int *ret;
172 struct dir_cache *cache = process->dir_cache;
173 unsigned int i, j, count;
175 if (!cache) return NULL;
176 for (i = count = 0; i < cache->count && count < *size / sizeof(*ret); i++)
177 if (cache->state[i] == DIR_CACHE_STATE_RELEASED) count++;
178 if (!count) return NULL;
180 if ((ret = malloc( count * sizeof(*ret) )))
182 for (i = j = 0; j < count; i++)
184 if (cache->state[i] != DIR_CACHE_STATE_RELEASED) continue;
185 cache->state[i] = DIR_CACHE_STATE_FREE;
186 ret[j++] = i;
188 *size = count * sizeof(*ret);
190 return ret;
193 /* allocate a new client-side directory cache entry */
194 static int alloc_dir_cache_entry( struct dir *dir, struct process *process )
196 unsigned int i = 0;
197 struct dir_cache *cache = process->dir_cache;
199 if (cache)
200 for (i = 0; i < cache->count; i++)
201 if (cache->state[i] == DIR_CACHE_STATE_FREE) goto found;
203 if (!cache || cache->count == cache->size)
205 unsigned int size = cache ? cache->size * 2 : 256;
206 if (!(cache = realloc( cache, offsetof( struct dir_cache, state[size] ))))
208 set_error( STATUS_NO_MEMORY );
209 return -1;
211 process->dir_cache = cache;
212 cache->size = size;
214 cache->count = i + 1;
216 found:
217 cache->state[i] = DIR_CACHE_STATE_INUSE;
218 return i;
221 /* release a directory cache entry; it will be freed on the client side on the next cache request */
222 static void release_dir_cache_entry( struct dir *dir )
224 struct dir_cache *cache;
226 if (!dir->client_process) return;
227 cache = dir->client_process->dir_cache;
228 cache->state[dir->client_entry] = DIR_CACHE_STATE_RELEASED;
229 release_object( dir->client_process );
230 dir->client_process = NULL;
233 static void dnotify_adjust_changes( struct dir *dir )
235 #if defined(F_SETSIG) && defined(F_NOTIFY)
236 int fd = get_unix_fd( dir->fd );
237 unsigned int filter = dir->filter;
238 unsigned int val;
239 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
240 return;
242 val = DN_MULTISHOT;
243 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
244 val |= DN_RENAME | DN_DELETE | DN_CREATE;
245 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
246 val |= DN_RENAME | DN_DELETE | DN_CREATE;
247 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
248 val |= DN_ATTRIB;
249 if (filter & FILE_NOTIFY_CHANGE_SIZE)
250 val |= DN_MODIFY;
251 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
252 val |= DN_MODIFY;
253 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
254 val |= DN_ACCESS;
255 if (filter & FILE_NOTIFY_CHANGE_CREATION)
256 val |= DN_CREATE;
257 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
258 val |= DN_ATTRIB;
259 fcntl( fd, F_NOTIFY, val );
260 #endif
263 /* insert change in the global list */
264 static inline void insert_change( struct dir *dir )
266 sigset_t sigset;
268 sigemptyset( &sigset );
269 sigaddset( &sigset, SIGIO );
270 sigprocmask( SIG_BLOCK, &sigset, NULL );
271 list_add_head( &change_list, &dir->entry );
272 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
275 /* remove change from the global list */
276 static inline void remove_change( struct dir *dir )
278 sigset_t sigset;
280 sigemptyset( &sigset );
281 sigaddset( &sigset, SIGIO );
282 sigprocmask( SIG_BLOCK, &sigset, NULL );
283 list_remove( &dir->entry );
284 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
287 static void dir_dump( struct object *obj, int verbose )
289 struct dir *dir = (struct dir *)obj;
290 assert( obj->ops == &dir_ops );
291 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
294 static struct object_type *dir_get_type( struct object *obj )
296 static const WCHAR name[] = {'F','i','l','e'};
297 static const struct unicode_str str = { name, sizeof(name) };
298 return get_object_type( &str );
301 /* enter here directly from SIGIO signal handler */
302 void do_change_notify( int unix_fd )
304 struct dir *dir;
306 /* FIXME: this is O(n) ... probably can be improved */
307 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
309 if (get_unix_fd( dir->fd ) != unix_fd) continue;
310 dir->notified = 1;
311 break;
315 /* SIGIO callback, called synchronously with the poll loop */
316 void sigio_callback(void)
318 struct dir *dir;
320 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
322 if (!dir->notified) continue;
323 dir->notified = 0;
324 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
328 static struct fd *dir_get_fd( struct object *obj )
330 struct dir *dir = (struct dir *)obj;
331 assert( obj->ops == &dir_ops );
332 return (struct fd *)grab_object( dir->fd );
335 static int get_dir_unix_fd( struct dir *dir )
337 return get_unix_fd( dir->fd );
340 static struct security_descriptor *dir_get_sd( struct object *obj )
342 struct dir *dir = (struct dir *)obj;
343 int unix_fd;
344 struct stat st;
345 struct security_descriptor *sd;
346 assert( obj->ops == &dir_ops );
348 unix_fd = get_dir_unix_fd( dir );
350 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
351 return obj->sd;
353 /* mode and uid the same? if so, no need to re-generate security descriptor */
354 if (obj->sd &&
355 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
356 (st.st_uid == dir->uid))
357 return obj->sd;
359 sd = mode_to_sd( st.st_mode,
360 security_unix_uid_to_sid( st.st_uid ),
361 token_get_primary_group( current->process->token ));
362 if (!sd) return obj->sd;
364 dir->mode = st.st_mode;
365 dir->uid = st.st_uid;
366 free( obj->sd );
367 obj->sd = sd;
368 return sd;
371 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
372 unsigned int set_info )
374 struct dir *dir = (struct dir *)obj;
375 const SID *owner;
376 struct stat st;
377 mode_t mode;
378 int unix_fd;
380 assert( obj->ops == &dir_ops );
382 unix_fd = get_dir_unix_fd( dir );
384 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
386 if (set_info & OWNER_SECURITY_INFORMATION)
388 owner = sd_get_owner( sd );
389 if (!owner)
391 set_error( STATUS_INVALID_SECURITY_DESCR );
392 return 0;
394 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
396 /* FIXME: get Unix uid and call fchown */
399 else if (obj->sd)
400 owner = sd_get_owner( obj->sd );
401 else
402 owner = token_get_user( current->process->token );
404 if (set_info & DACL_SECURITY_INFORMATION)
406 /* keep the bits that we don't map to access rights in the ACL */
407 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
408 mode |= sd_to_mode( sd, owner );
410 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
412 file_set_error();
413 return 0;
416 return 1;
419 static struct change_record *get_first_change_record( struct dir *dir )
421 struct list *ptr = list_head( &dir->change_records );
422 if (!ptr) return NULL;
423 list_remove( ptr );
424 return LIST_ENTRY( ptr, struct change_record, entry );
427 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
429 struct dir *dir = (struct dir *)obj;
431 if (!fd_close_handle( obj, process, handle )) return 0;
432 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
433 return 1; /* ok to close */
436 static void dir_destroy( struct object *obj )
438 struct change_record *record;
439 struct dir *dir = (struct dir *)obj;
440 assert (obj->ops == &dir_ops);
442 if (dir->filter)
443 remove_change( dir );
445 if (dir->inode)
447 list_remove( &dir->in_entry );
448 free_inode( dir->inode );
451 while ((record = get_first_change_record( dir ))) free( record );
453 release_dir_cache_entry( dir );
454 release_object( dir->fd );
456 if (inotify_fd && list_empty( &change_list ))
458 release_object( inotify_fd );
459 inotify_fd = NULL;
463 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
465 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
468 static int dir_get_poll_events( struct fd *fd )
470 return 0;
473 static enum server_fd_type dir_get_fd_type( struct fd *fd )
475 return FD_TYPE_DIR;
478 #ifdef HAVE_SYS_INOTIFY_H
480 #define HASH_SIZE 31
482 struct inode {
483 struct list ch_entry; /* entry in the children list */
484 struct list children; /* children of this inode */
485 struct inode *parent; /* parent of this inode */
486 struct list dirs; /* directory handles watching this inode */
487 struct list ino_entry; /* entry in the inode hash */
488 struct list wd_entry; /* entry in the watch descriptor hash */
489 dev_t dev; /* device number */
490 ino_t ino; /* device's inode number */
491 int wd; /* inotify's watch descriptor */
492 char *name; /* basename name of the inode */
495 static struct list inode_hash[ HASH_SIZE ];
496 static struct list wd_hash[ HASH_SIZE ];
498 static int inotify_add_dir( char *path, unsigned int filter );
500 static struct inode *inode_from_wd( int wd )
502 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
503 struct inode *inode;
505 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
506 if (inode->wd == wd)
507 return inode;
509 return NULL;
512 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
514 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
517 static struct inode *find_inode( dev_t dev, ino_t ino )
519 struct list *bucket = get_hash_list( dev, ino );
520 struct inode *inode;
522 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
523 if (inode->ino == ino && inode->dev == dev)
524 return inode;
526 return NULL;
529 static struct inode *create_inode( dev_t dev, ino_t ino )
531 struct inode *inode;
533 inode = malloc( sizeof *inode );
534 if (inode)
536 list_init( &inode->children );
537 list_init( &inode->dirs );
538 inode->ino = ino;
539 inode->dev = dev;
540 inode->wd = -1;
541 inode->parent = NULL;
542 inode->name = NULL;
543 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
545 return inode;
548 static struct inode *get_inode( dev_t dev, ino_t ino )
550 struct inode *inode;
552 inode = find_inode( dev, ino );
553 if (inode)
554 return inode;
555 return create_inode( dev, ino );
558 static void inode_set_wd( struct inode *inode, int wd )
560 if (inode->wd != -1)
561 list_remove( &inode->wd_entry );
562 inode->wd = wd;
563 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
566 static void inode_set_name( struct inode *inode, const char *name )
568 free (inode->name);
569 inode->name = name ? strdup( name ) : NULL;
572 static void free_inode( struct inode *inode )
574 int subtree = 0, watches = 0;
575 struct inode *tmp, *next;
576 struct dir *dir;
578 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
580 subtree |= dir->subtree;
581 watches++;
584 if (!subtree && !inode->parent)
586 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
587 struct inode, ch_entry )
589 assert( tmp != inode );
590 assert( tmp->parent == inode );
591 free_inode( tmp );
595 if (watches)
596 return;
598 if (inode->parent)
599 list_remove( &inode->ch_entry );
601 /* disconnect remaining children from the parent */
602 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
604 list_remove( &tmp->ch_entry );
605 tmp->parent = NULL;
608 if (inode->wd != -1)
610 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
611 list_remove( &inode->wd_entry );
613 list_remove( &inode->ino_entry );
615 free( inode->name );
616 free( inode );
619 static struct inode *inode_add( struct inode *parent,
620 dev_t dev, ino_t ino, const char *name )
622 struct inode *inode;
624 inode = get_inode( dev, ino );
625 if (!inode)
626 return NULL;
628 if (!inode->parent)
630 list_add_tail( &parent->children, &inode->ch_entry );
631 inode->parent = parent;
632 assert( inode != parent );
634 inode_set_name( inode, name );
636 return inode;
639 static struct inode *inode_from_name( struct inode *inode, const char *name )
641 struct inode *i;
643 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
644 if (i->name && !strcmp( i->name, name ))
645 return i;
646 return NULL;
649 static int inotify_get_poll_events( struct fd *fd );
650 static void inotify_poll_event( struct fd *fd, int event );
652 static const struct fd_ops inotify_fd_ops =
654 inotify_get_poll_events, /* get_poll_events */
655 inotify_poll_event, /* poll_event */
656 NULL, /* flush */
657 NULL, /* get_fd_type */
658 NULL, /* ioctl */
659 NULL, /* queue_async */
660 NULL /* reselect_async */
663 static int inotify_get_poll_events( struct fd *fd )
665 return POLLIN;
668 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
669 unsigned int cookie, const char *relpath )
671 struct change_record *record;
673 assert( dir->obj.ops == &dir_ops );
675 if (dir->want_data)
677 size_t len = strlen(relpath);
678 record = malloc( offsetof(struct change_record, event.name[len]) );
679 if (!record)
680 return;
682 record->cookie = cookie;
683 record->event.action = action;
684 memcpy( record->event.name, relpath, len );
685 record->event.len = len;
687 list_add_tail( &dir->change_records, &record->entry );
690 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
693 static unsigned int filter_from_event( struct inotify_event *ie )
695 unsigned int filter = 0;
697 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
698 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
699 if (ie->mask & IN_MODIFY)
700 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
701 if (ie->mask & IN_ATTRIB)
702 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
703 if (ie->mask & IN_CREATE)
704 filter |= FILE_NOTIFY_CHANGE_CREATION;
706 if (ie->mask & IN_ISDIR)
707 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
708 else
709 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
711 return filter;
714 /* scan up the parent directories for watches */
715 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
717 unsigned int filter = 0;
718 struct dir *dir;
720 /* combine filters from parents watching subtrees */
721 while (inode)
723 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
724 if (dir->subtree || !is_parent)
725 filter |= dir->filter;
726 is_parent = 1;
727 inode = inode->parent;
730 return filter;
733 static char *inode_get_path( struct inode *inode, int sz )
735 struct list *head;
736 char *path;
737 int len;
739 if (!inode)
740 return NULL;
742 head = list_head( &inode->dirs );
743 if (head)
745 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
746 path = malloc ( 32 + sz );
747 if (path)
748 sprintf( path, "/proc/self/fd/%u/", unix_fd );
749 return path;
752 if (!inode->name)
753 return NULL;
755 len = strlen( inode->name );
756 path = inode_get_path( inode->parent, sz + len + 1 );
757 if (!path)
758 return NULL;
760 strcat( path, inode->name );
761 strcat( path, "/" );
763 return path;
766 static void inode_check_dir( struct inode *parent, const char *name )
768 char *path;
769 unsigned int filter;
770 struct inode *inode;
771 struct stat st;
772 int wd = -1;
774 path = inode_get_path( parent, strlen(name) );
775 if (!path)
776 return;
778 strcat( path, name );
780 if (stat( path, &st ) < 0)
781 goto end;
783 filter = filter_from_inode( parent, 1 );
784 if (!filter)
785 goto end;
787 inode = inode_add( parent, st.st_dev, st.st_ino, name );
788 if (!inode || inode->wd != -1)
789 goto end;
791 wd = inotify_add_dir( path, filter );
792 if (wd != -1)
793 inode_set_wd( inode, wd );
794 else
795 free_inode( inode );
797 end:
798 free( path );
801 static int prepend( char **path, const char *segment )
803 int extra;
804 char *p;
806 extra = strlen( segment ) + 1;
807 if (*path)
809 int len = strlen( *path ) + 1;
810 p = realloc( *path, len + extra );
811 if (!p) return 0;
812 memmove( &p[ extra ], p, len );
813 p[ extra - 1 ] = '/';
814 memcpy( p, segment, extra - 1 );
816 else
818 p = malloc( extra );
819 if (!p) return 0;
820 memcpy( p, segment, extra );
823 *path = p;
825 return 1;
828 static void inotify_notify_all( struct inotify_event *ie )
830 unsigned int filter, action;
831 struct inode *inode, *i;
832 char *path = NULL;
833 struct dir *dir;
835 inode = inode_from_wd( ie->wd );
836 if (!inode)
838 fprintf( stderr, "no inode matches %d\n", ie->wd);
839 return;
842 filter = filter_from_event( ie );
844 if (ie->mask & IN_CREATE)
846 if (ie->mask & IN_ISDIR)
847 inode_check_dir( inode, ie->name );
849 action = FILE_ACTION_ADDED;
851 else if (ie->mask & IN_DELETE)
852 action = FILE_ACTION_REMOVED;
853 else if (ie->mask & IN_MOVED_FROM)
854 action = FILE_ACTION_RENAMED_OLD_NAME;
855 else if (ie->mask & IN_MOVED_TO)
856 action = FILE_ACTION_RENAMED_NEW_NAME;
857 else
858 action = FILE_ACTION_MODIFIED;
861 * Work our way up the inode hierarchy
862 * extending the relative path as we go
863 * and notifying all recursive watches.
865 if (!prepend( &path, ie->name ))
866 return;
868 for (i = inode; i; i = i->parent)
870 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
871 if ((filter & dir->filter) && (i==inode || dir->subtree))
872 inotify_do_change_notify( dir, action, ie->cookie, path );
874 if (!i->name || !prepend( &path, i->name ))
875 break;
878 free( path );
880 if (ie->mask & IN_DELETE)
882 i = inode_from_name( inode, ie->name );
883 if (i)
884 free_inode( i );
888 static void inotify_poll_event( struct fd *fd, int event )
890 int r, ofs, unix_fd;
891 char buffer[0x1000];
892 struct inotify_event *ie;
894 unix_fd = get_unix_fd( fd );
895 r = read( unix_fd, buffer, sizeof buffer );
896 if (r < 0)
898 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
899 return;
902 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
904 ie = (struct inotify_event*) &buffer[ofs];
905 ofs += offsetof( struct inotify_event, name[ie->len] );
906 if (ofs > r) break;
907 if (ie->len) inotify_notify_all( ie );
911 static inline struct fd *create_inotify_fd( void )
913 int unix_fd;
915 unix_fd = inotify_init();
916 if (unix_fd<0)
917 return NULL;
918 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
921 static int map_flags( unsigned int filter )
923 unsigned int mask;
925 /* always watch these so we can track subdirectories in recursive watches */
926 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
928 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
929 mask |= IN_ATTRIB;
930 if (filter & FILE_NOTIFY_CHANGE_SIZE)
931 mask |= IN_MODIFY;
932 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
933 mask |= IN_MODIFY;
934 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
935 mask |= IN_MODIFY;
936 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
937 mask |= IN_ATTRIB;
939 return mask;
942 static int inotify_add_dir( char *path, unsigned int filter )
944 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
945 path, map_flags( filter ) );
946 if (wd != -1)
947 set_fd_events( inotify_fd, POLLIN );
948 return wd;
951 static int init_inotify( void )
953 int i;
955 if (inotify_fd)
956 return 1;
958 inotify_fd = create_inotify_fd();
959 if (!inotify_fd)
960 return 0;
962 for (i=0; i<HASH_SIZE; i++)
964 list_init( &inode_hash[i] );
965 list_init( &wd_hash[i] );
968 return 1;
971 static int inotify_adjust_changes( struct dir *dir )
973 unsigned int filter;
974 struct inode *inode;
975 struct stat st;
976 char path[32];
977 int wd, unix_fd;
979 if (!inotify_fd)
980 return 0;
982 unix_fd = get_unix_fd( dir->fd );
984 inode = dir->inode;
985 if (!inode)
987 /* check if this fd is already being watched */
988 if (-1 == fstat( unix_fd, &st ))
989 return 0;
991 inode = get_inode( st.st_dev, st.st_ino );
992 if (!inode)
993 inode = create_inode( st.st_dev, st.st_ino );
994 if (!inode)
995 return 0;
996 list_add_tail( &inode->dirs, &dir->in_entry );
997 dir->inode = inode;
1000 filter = filter_from_inode( inode, 0 );
1002 sprintf( path, "/proc/self/fd/%u", unix_fd );
1003 wd = inotify_add_dir( path, filter );
1004 if (wd == -1) return 0;
1006 inode_set_wd( inode, wd );
1008 return 1;
1011 static char *get_basename( const char *link )
1013 char *buffer, *name = NULL;
1014 int r, n = 0x100;
1016 while (1)
1018 buffer = malloc( n );
1019 if (!buffer) return NULL;
1021 r = readlink( link, buffer, n );
1022 if (r < 0)
1023 break;
1025 if (r < n)
1027 name = buffer;
1028 break;
1030 free( buffer );
1031 n *= 2;
1034 if (name)
1036 while (r > 0 && name[ r - 1 ] == '/' )
1037 r--;
1038 name[ r ] = 0;
1040 name = strrchr( name, '/' );
1041 if (name)
1042 name = strdup( &name[1] );
1045 free( buffer );
1046 return name;
1049 static int dir_add_to_existing_notify( struct dir *dir )
1051 struct inode *inode, *parent;
1052 unsigned int filter = 0;
1053 struct stat st, st_new;
1054 char link[35], *name;
1055 int wd, unix_fd;
1057 if (!inotify_fd)
1058 return 0;
1060 unix_fd = get_unix_fd( dir->fd );
1062 /* check if it's in the list of inodes we want to watch */
1063 if (-1 == fstat( unix_fd, &st_new ))
1064 return 0;
1065 inode = find_inode( st_new.st_dev, st_new.st_ino );
1066 if (inode)
1067 return 0;
1069 /* lookup the parent */
1070 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1071 if (-1 == stat( link, &st ))
1072 return 0;
1075 * If there's no parent, stop. We could keep going adding
1076 * ../ to the path until we hit the root of the tree or
1077 * find a recursively watched ancestor.
1078 * Assume it's too expensive to search up the tree for now.
1080 parent = find_inode( st.st_dev, st.st_ino );
1081 if (!parent)
1082 return 0;
1084 if (parent->wd == -1)
1085 return 0;
1087 filter = filter_from_inode( parent, 1 );
1088 if (!filter)
1089 return 0;
1091 sprintf( link, "/proc/self/fd/%u", unix_fd );
1092 name = get_basename( link );
1093 if (!name)
1094 return 0;
1095 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1096 free( name );
1097 if (!inode)
1098 return 0;
1100 /* Couldn't find this inode at the start of the function, must be new */
1101 assert( inode->wd == -1 );
1103 wd = inotify_add_dir( link, filter );
1104 if (wd != -1)
1105 inode_set_wd( inode, wd );
1107 return 1;
1110 #else
1112 static int init_inotify( void )
1114 return 0;
1117 static int inotify_adjust_changes( struct dir *dir )
1119 return 0;
1122 static void free_inode( struct inode *inode )
1124 assert( 0 );
1127 static int dir_add_to_existing_notify( struct dir *dir )
1129 return 0;
1132 #endif /* HAVE_SYS_INOTIFY_H */
1134 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1136 struct dir *dir;
1138 dir = alloc_object( &dir_ops );
1139 if (!dir)
1140 return NULL;
1142 list_init( &dir->change_records );
1143 dir->filter = 0;
1144 dir->notified = 0;
1145 dir->want_data = 0;
1146 dir->inode = NULL;
1147 grab_object( fd );
1148 dir->fd = fd;
1149 dir->mode = mode;
1150 dir->uid = ~(uid_t)0;
1151 dir->client_process = NULL;
1152 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1154 dir_add_to_existing_notify( dir );
1156 return &dir->obj;
1159 /* retrieve (or allocate) the client-side directory cache entry */
1160 DECL_HANDLER(get_directory_cache_entry)
1162 struct dir *dir;
1163 int *free_entries;
1164 data_size_t free_size;
1166 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1168 if (!dir->client_process)
1170 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1171 dir->client_process = (struct process *)grab_object( current->process );
1174 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1175 else set_error( STATUS_SHARING_VIOLATION );
1177 done: /* allow freeing entries even on failure */
1178 free_size = get_reply_max_size();
1179 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1180 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1182 release_object( dir );
1185 /* enable change notifications for a directory */
1186 DECL_HANDLER(read_directory_changes)
1188 struct dir *dir;
1189 struct async *async;
1191 if (!req->filter)
1193 set_error(STATUS_INVALID_PARAMETER);
1194 return;
1197 dir = get_dir_obj( current->process, req->async.handle, 0 );
1198 if (!dir)
1199 return;
1201 /* requests don't timeout */
1202 if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
1203 fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
1205 /* assign it once */
1206 if (!dir->filter)
1208 init_inotify();
1209 insert_change( dir );
1210 dir->filter = req->filter;
1211 dir->subtree = req->subtree;
1212 dir->want_data = req->want_data;
1215 /* if there's already a change in the queue, send it */
1216 if (!list_empty( &dir->change_records ))
1217 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1219 /* setup the real notification */
1220 if (!inotify_adjust_changes( dir ))
1221 dnotify_adjust_changes( dir );
1223 set_error(STATUS_PENDING);
1225 release_object( async );
1226 end:
1227 release_object( dir );
1230 DECL_HANDLER(read_change)
1232 struct change_record *record, *next;
1233 struct dir *dir;
1234 struct list events;
1235 char *data, *event;
1236 int size = 0;
1238 dir = get_dir_obj( current->process, req->handle, 0 );
1239 if (!dir)
1240 return;
1242 list_init( &events );
1243 list_move_tail( &events, &dir->change_records );
1244 release_object( dir );
1246 if (list_empty( &events ))
1248 set_error( STATUS_NO_DATA_DETECTED );
1249 return;
1252 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1254 size += (offsetof(struct filesystem_event, name[record->event.len])
1255 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1258 if (size > get_reply_max_size())
1259 set_error( STATUS_BUFFER_TOO_SMALL );
1260 else if ((data = mem_alloc( size )) != NULL)
1262 event = data;
1263 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1265 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1267 /* FIXME: rename events are sometimes reported as delete/create */
1268 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1270 struct list *elem = list_next( &events, &record->entry );
1271 if (elem)
1272 next = LIST_ENTRY(elem, struct change_record, entry);
1274 if (elem && next->cookie == record->cookie)
1275 next->cookie = 0;
1276 else
1277 record->event.action = FILE_ACTION_REMOVED;
1279 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1280 record->event.action = FILE_ACTION_ADDED;
1282 memcpy( event, &record->event, len );
1283 event += len;
1284 if (len % sizeof(int))
1286 memset( event, 0, sizeof(int) - len % sizeof(int) );
1287 event += sizeof(int) - len % sizeof(int);
1290 set_reply_data_ptr( data, size );
1293 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1295 list_remove( &record->entry );
1296 free( record );