dinput/tests: Remove unmatched winetest_pop_context().
[wine.git] / server / change.c
blob7a806abc01712ecec31a9cd903bb561ea882ff5d
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"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <limits.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <poll.h>
36 #ifdef HAVE_SYS_INOTIFY_H
37 #include <sys/inotify.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "windef.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "thread.h"
47 #include "request.h"
48 #include "process.h"
49 #include "security.h"
50 #include "winternl.h"
52 /* dnotify support */
54 #ifdef linux
55 #ifndef F_NOTIFY
56 #define F_NOTIFY 1026
57 #define DN_ACCESS 0x00000001 /* File accessed */
58 #define DN_MODIFY 0x00000002 /* File modified */
59 #define DN_CREATE 0x00000004 /* File created */
60 #define DN_DELETE 0x00000008 /* File removed */
61 #define DN_RENAME 0x00000010 /* File renamed */
62 #define DN_ATTRIB 0x00000020 /* File changed attributes */
63 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
64 #endif
65 #endif
67 /* inotify support */
69 struct inode;
71 static void free_inode( struct inode *inode );
73 static struct fd *inotify_fd;
75 struct change_record {
76 struct list entry;
77 unsigned int cookie;
78 struct filesystem_event event;
81 struct dir
83 struct object obj; /* object header */
84 struct fd *fd; /* file descriptor to the directory */
85 mode_t mode; /* file stat.st_mode */
86 uid_t uid; /* file stat.st_uid */
87 struct list entry; /* entry in global change notifications list */
88 unsigned int filter; /* notification filter */
89 volatile int notified; /* SIGIO counter */
90 int want_data; /* return change data */
91 int subtree; /* do we want to watch subdirectories? */
92 struct list change_records; /* data for the change */
93 struct list in_entry; /* entry in the inode dirs list */
94 struct inode *inode; /* inode of the associated directory */
95 struct process *client_process; /* client process that has a cache for this directory */
96 int client_entry; /* entry in client process cache */
99 static struct fd *dir_get_fd( struct object *obj );
100 static struct security_descriptor *dir_get_sd( struct object *obj );
101 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
102 unsigned int set_info );
103 static void dir_dump( struct object *obj, int verbose );
104 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
105 static void dir_destroy( struct object *obj );
107 static const struct object_ops dir_ops =
109 sizeof(struct dir), /* size */
110 &file_type, /* type */
111 dir_dump, /* dump */
112 add_queue, /* add_queue */
113 remove_queue, /* remove_queue */
114 default_fd_signaled, /* signaled */
115 no_satisfied, /* satisfied */
116 no_signal, /* signal */
117 dir_get_fd, /* get_fd */
118 default_map_access, /* map_access */
119 dir_get_sd, /* get_sd */
120 dir_set_sd, /* set_sd */
121 no_get_full_name, /* get_full_name */
122 no_lookup_name, /* lookup_name */
123 no_link_name, /* link_name */
124 NULL, /* unlink_name */
125 no_open_file, /* open_file */
126 no_kernel_obj_list, /* get_kernel_obj_list */
127 dir_close_handle, /* close_handle */
128 dir_destroy /* destroy */
131 static int dir_get_poll_events( struct fd *fd );
132 static enum server_fd_type dir_get_fd_type( struct fd *fd );
134 static const struct fd_ops dir_fd_ops =
136 dir_get_poll_events, /* get_poll_events */
137 default_poll_event, /* poll_event */
138 dir_get_fd_type, /* get_fd_type */
139 no_fd_read, /* read */
140 no_fd_write, /* write */
141 no_fd_flush, /* flush */
142 default_fd_get_file_info, /* get_file_info */
143 no_fd_get_volume_info, /* get_volume_info */
144 default_fd_ioctl, /* ioctl */
145 default_fd_cancel_async, /* cancel_async */
146 default_fd_queue_async, /* queue_async */
147 default_fd_reselect_async /* reselect_async */
150 static struct list change_list = LIST_INIT(change_list);
152 /* per-process structure to keep track of cache entries on the client size */
153 struct dir_cache
155 unsigned int size;
156 unsigned int count;
157 unsigned char state[1];
160 enum dir_cache_state
162 DIR_CACHE_STATE_FREE,
163 DIR_CACHE_STATE_INUSE,
164 DIR_CACHE_STATE_RELEASED
167 /* return an array of cache entries that can be freed on the client side */
168 static int *get_free_dir_cache_entries( struct process *process, data_size_t *size )
170 int *ret;
171 struct dir_cache *cache = process->dir_cache;
172 unsigned int i, j, count;
174 if (!cache) return NULL;
175 for (i = count = 0; i < cache->count && count < *size / sizeof(*ret); i++)
176 if (cache->state[i] == DIR_CACHE_STATE_RELEASED) count++;
177 if (!count) return NULL;
179 if ((ret = malloc( count * sizeof(*ret) )))
181 for (i = j = 0; j < count; i++)
183 if (cache->state[i] != DIR_CACHE_STATE_RELEASED) continue;
184 cache->state[i] = DIR_CACHE_STATE_FREE;
185 ret[j++] = i;
187 *size = count * sizeof(*ret);
189 return ret;
192 /* allocate a new client-side directory cache entry */
193 static int alloc_dir_cache_entry( struct dir *dir, struct process *process )
195 unsigned int i = 0;
196 struct dir_cache *cache = process->dir_cache;
198 if (cache)
199 for (i = 0; i < cache->count; i++)
200 if (cache->state[i] == DIR_CACHE_STATE_FREE) goto found;
202 if (!cache || cache->count == cache->size)
204 unsigned int size = cache ? cache->size * 2 : 256;
205 if (!(cache = realloc( cache, offsetof( struct dir_cache, state[size] ))))
207 set_error( STATUS_NO_MEMORY );
208 return -1;
210 process->dir_cache = cache;
211 cache->size = size;
213 cache->count = i + 1;
215 found:
216 cache->state[i] = DIR_CACHE_STATE_INUSE;
217 return i;
220 /* release a directory cache entry; it will be freed on the client side on the next cache request */
221 static void release_dir_cache_entry( struct dir *dir )
223 struct dir_cache *cache;
225 if (!dir->client_process) return;
226 cache = dir->client_process->dir_cache;
227 cache->state[dir->client_entry] = DIR_CACHE_STATE_RELEASED;
228 release_object( dir->client_process );
229 dir->client_process = NULL;
232 static void dnotify_adjust_changes( struct dir *dir )
234 #if defined(F_SETSIG) && defined(F_NOTIFY)
235 int fd = get_unix_fd( dir->fd );
236 unsigned int filter = dir->filter;
237 unsigned int val;
238 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
239 return;
241 val = DN_MULTISHOT;
242 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
243 val |= DN_RENAME | DN_DELETE | DN_CREATE;
244 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
245 val |= DN_RENAME | DN_DELETE | DN_CREATE;
246 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
247 val |= DN_ATTRIB;
248 if (filter & FILE_NOTIFY_CHANGE_SIZE)
249 val |= DN_MODIFY;
250 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
251 val |= DN_MODIFY;
252 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
253 val |= DN_ACCESS;
254 if (filter & FILE_NOTIFY_CHANGE_CREATION)
255 val |= DN_CREATE;
256 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
257 val |= DN_ATTRIB;
258 fcntl( fd, F_NOTIFY, val );
259 #endif
262 /* insert change in the global list */
263 static inline void insert_change( struct dir *dir )
265 sigset_t sigset;
267 sigemptyset( &sigset );
268 sigaddset( &sigset, SIGIO );
269 sigprocmask( SIG_BLOCK, &sigset, NULL );
270 list_add_head( &change_list, &dir->entry );
271 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
274 /* remove change from the global list */
275 static inline void remove_change( struct dir *dir )
277 sigset_t sigset;
279 sigemptyset( &sigset );
280 sigaddset( &sigset, SIGIO );
281 sigprocmask( SIG_BLOCK, &sigset, NULL );
282 list_remove( &dir->entry );
283 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
286 static void dir_dump( struct object *obj, int verbose )
288 struct dir *dir = (struct dir *)obj;
289 assert( obj->ops == &dir_ops );
290 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
293 /* enter here directly from SIGIO signal handler */
294 void do_change_notify( int unix_fd )
296 struct dir *dir;
298 /* FIXME: this is O(n) ... probably can be improved */
299 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
301 if (get_unix_fd( dir->fd ) != unix_fd) continue;
302 dir->notified = 1;
303 break;
307 /* SIGIO callback, called synchronously with the poll loop */
308 void sigio_callback(void)
310 struct dir *dir;
312 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
314 if (!dir->notified) continue;
315 dir->notified = 0;
316 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
320 static struct fd *dir_get_fd( struct object *obj )
322 struct dir *dir = (struct dir *)obj;
323 assert( obj->ops == &dir_ops );
324 return (struct fd *)grab_object( dir->fd );
327 static int get_dir_unix_fd( struct dir *dir )
329 return get_unix_fd( dir->fd );
332 static struct security_descriptor *dir_get_sd( struct object *obj )
334 struct dir *dir = (struct dir *)obj;
335 int unix_fd;
336 struct stat st;
337 struct security_descriptor *sd;
338 assert( obj->ops == &dir_ops );
340 unix_fd = get_dir_unix_fd( dir );
342 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
343 return obj->sd;
345 /* mode and uid the same? if so, no need to re-generate security descriptor */
346 if (obj->sd &&
347 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
348 (st.st_uid == dir->uid))
349 return obj->sd;
351 sd = mode_to_sd( st.st_mode,
352 security_unix_uid_to_sid( st.st_uid ),
353 token_get_primary_group( current->process->token ));
354 if (!sd) return obj->sd;
356 dir->mode = st.st_mode;
357 dir->uid = st.st_uid;
358 free( obj->sd );
359 obj->sd = sd;
360 return sd;
363 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
364 unsigned int set_info )
366 struct dir *dir = (struct dir *)obj;
367 const struct sid *owner;
368 struct stat st;
369 mode_t mode;
370 int unix_fd;
372 assert( obj->ops == &dir_ops );
374 unix_fd = get_dir_unix_fd( dir );
376 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
378 if (set_info & OWNER_SECURITY_INFORMATION)
380 owner = sd_get_owner( sd );
381 if (!owner)
383 set_error( STATUS_INVALID_SECURITY_DESCR );
384 return 0;
386 if (!obj->sd || !equal_sid( owner, sd_get_owner( obj->sd ) ))
388 /* FIXME: get Unix uid and call fchown */
391 else if (obj->sd)
392 owner = sd_get_owner( obj->sd );
393 else
394 owner = token_get_owner( current->process->token );
396 if (set_info & DACL_SECURITY_INFORMATION)
398 /* keep the bits that we don't map to access rights in the ACL */
399 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
400 mode |= sd_to_mode( sd, owner );
402 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
404 file_set_error();
405 return 0;
408 return 1;
411 static struct change_record *get_first_change_record( struct dir *dir )
413 struct list *ptr = list_head( &dir->change_records );
414 if (!ptr) return NULL;
415 list_remove( ptr );
416 return LIST_ENTRY( ptr, struct change_record, entry );
419 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
421 struct dir *dir = (struct dir *)obj;
423 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
424 return 1; /* ok to close */
427 static void dir_destroy( struct object *obj )
429 struct change_record *record;
430 struct dir *dir = (struct dir *)obj;
431 assert (obj->ops == &dir_ops);
433 if (dir->filter)
434 remove_change( dir );
436 if (dir->inode)
438 list_remove( &dir->in_entry );
439 free_inode( dir->inode );
442 while ((record = get_first_change_record( dir ))) free( record );
444 release_dir_cache_entry( dir );
445 release_object( dir->fd );
447 if (inotify_fd && list_empty( &change_list ))
449 release_object( inotify_fd );
450 inotify_fd = NULL;
454 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
456 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
459 static int dir_get_poll_events( struct fd *fd )
461 return 0;
464 static enum server_fd_type dir_get_fd_type( struct fd *fd )
466 return FD_TYPE_DIR;
469 #ifdef HAVE_SYS_INOTIFY_H
471 #define HASH_SIZE 31
473 struct inode {
474 struct list ch_entry; /* entry in the children list */
475 struct list children; /* children of this inode */
476 struct inode *parent; /* parent of this inode */
477 struct list dirs; /* directory handles watching this inode */
478 struct list ino_entry; /* entry in the inode hash */
479 struct list wd_entry; /* entry in the watch descriptor hash */
480 dev_t dev; /* device number */
481 ino_t ino; /* device's inode number */
482 int wd; /* inotify's watch descriptor */
483 char *name; /* basename name of the inode */
486 static struct list inode_hash[ HASH_SIZE ];
487 static struct list wd_hash[ HASH_SIZE ];
489 static int inotify_add_dir( char *path, unsigned int filter );
491 static struct inode *inode_from_wd( int wd )
493 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
494 struct inode *inode;
496 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
497 if (inode->wd == wd)
498 return inode;
500 return NULL;
503 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
505 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
508 static struct inode *find_inode( dev_t dev, ino_t ino )
510 struct list *bucket = get_hash_list( dev, ino );
511 struct inode *inode;
513 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
514 if (inode->ino == ino && inode->dev == dev)
515 return inode;
517 return NULL;
520 static struct inode *create_inode( dev_t dev, ino_t ino )
522 struct inode *inode;
524 inode = malloc( sizeof *inode );
525 if (inode)
527 list_init( &inode->children );
528 list_init( &inode->dirs );
529 inode->ino = ino;
530 inode->dev = dev;
531 inode->wd = -1;
532 inode->parent = NULL;
533 inode->name = NULL;
534 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
536 return inode;
539 static struct inode *get_inode( dev_t dev, ino_t ino )
541 struct inode *inode;
543 inode = find_inode( dev, ino );
544 if (inode)
545 return inode;
546 return create_inode( dev, ino );
549 static void inode_set_wd( struct inode *inode, int wd )
551 if (inode->wd != -1)
552 list_remove( &inode->wd_entry );
553 inode->wd = wd;
554 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
557 static void inode_set_name( struct inode *inode, const char *name )
559 free (inode->name);
560 inode->name = name ? strdup( name ) : NULL;
563 static void free_inode( struct inode *inode )
565 int subtree = 0, watches = 0;
566 struct inode *tmp, *next;
567 struct dir *dir;
569 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
571 subtree |= dir->subtree;
572 watches++;
575 if (!subtree && !inode->parent)
577 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
578 struct inode, ch_entry )
580 assert( tmp != inode );
581 assert( tmp->parent == inode );
582 free_inode( tmp );
586 if (watches)
587 return;
589 if (inode->parent)
590 list_remove( &inode->ch_entry );
592 /* disconnect remaining children from the parent */
593 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
595 list_remove( &tmp->ch_entry );
596 tmp->parent = NULL;
599 if (inode->wd != -1)
601 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
602 list_remove( &inode->wd_entry );
604 list_remove( &inode->ino_entry );
606 free( inode->name );
607 free( inode );
610 static struct inode *inode_add( struct inode *parent,
611 dev_t dev, ino_t ino, const char *name )
613 struct inode *inode;
615 inode = get_inode( dev, ino );
616 if (!inode)
617 return NULL;
619 if (!inode->parent)
621 list_add_tail( &parent->children, &inode->ch_entry );
622 inode->parent = parent;
623 assert( inode != parent );
625 inode_set_name( inode, name );
627 return inode;
630 static struct inode *inode_from_name( struct inode *inode, const char *name )
632 struct inode *i;
634 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
635 if (i->name && !strcmp( i->name, name ))
636 return i;
637 return NULL;
640 static int inotify_get_poll_events( struct fd *fd );
641 static void inotify_poll_event( struct fd *fd, int event );
643 static const struct fd_ops inotify_fd_ops =
645 inotify_get_poll_events, /* get_poll_events */
646 inotify_poll_event, /* poll_event */
647 NULL, /* flush */
648 NULL, /* get_fd_type */
649 NULL, /* ioctl */
650 NULL, /* queue_async */
651 NULL /* reselect_async */
654 static int inotify_get_poll_events( struct fd *fd )
656 return POLLIN;
659 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
660 unsigned int cookie, const char *relpath )
662 struct change_record *record;
664 assert( dir->obj.ops == &dir_ops );
666 if (dir->want_data)
668 size_t len = strlen(relpath);
669 record = malloc( offsetof(struct change_record, event.name[len]) );
670 if (!record)
671 return;
673 record->cookie = cookie;
674 record->event.action = action;
675 memcpy( record->event.name, relpath, len );
676 record->event.len = len;
678 list_add_tail( &dir->change_records, &record->entry );
681 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
684 static unsigned int filter_from_event( struct inotify_event *ie )
686 unsigned int filter = 0;
688 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
689 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
690 if (ie->mask & IN_MODIFY)
691 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
692 if (ie->mask & IN_ATTRIB)
693 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
694 if (ie->mask & IN_CREATE)
695 filter |= FILE_NOTIFY_CHANGE_CREATION;
697 if (ie->mask & IN_ISDIR)
698 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
699 else
700 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
702 return filter;
705 /* scan up the parent directories for watches */
706 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
708 unsigned int filter = 0;
709 struct dir *dir;
711 /* combine filters from parents watching subtrees */
712 while (inode)
714 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
715 if (dir->subtree || !is_parent)
716 filter |= dir->filter;
717 is_parent = 1;
718 inode = inode->parent;
721 return filter;
724 static char *inode_get_path( struct inode *inode, int sz )
726 struct list *head;
727 char *path;
728 int len;
730 if (!inode)
731 return NULL;
733 head = list_head( &inode->dirs );
734 if (head)
736 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
737 path = malloc ( 32 + sz );
738 if (path)
739 sprintf( path, "/proc/self/fd/%u/", unix_fd );
740 return path;
743 if (!inode->name)
744 return NULL;
746 len = strlen( inode->name );
747 path = inode_get_path( inode->parent, sz + len + 1 );
748 if (!path)
749 return NULL;
751 strcat( path, inode->name );
752 strcat( path, "/" );
754 return path;
757 static void inode_check_dir( struct inode *parent, const char *name )
759 char *path;
760 unsigned int filter;
761 struct inode *inode;
762 struct stat st;
763 int wd = -1;
765 path = inode_get_path( parent, strlen(name) );
766 if (!path)
767 return;
769 strcat( path, name );
771 if (stat( path, &st ) < 0)
772 goto end;
774 filter = filter_from_inode( parent, 1 );
775 if (!filter)
776 goto end;
778 inode = inode_add( parent, st.st_dev, st.st_ino, name );
779 if (!inode || inode->wd != -1)
780 goto end;
782 wd = inotify_add_dir( path, filter );
783 if (wd != -1)
784 inode_set_wd( inode, wd );
785 else
786 free_inode( inode );
788 end:
789 free( path );
792 static int prepend( char **path, const char *segment )
794 int extra;
795 char *p;
797 extra = strlen( segment ) + 1;
798 if (*path)
800 int len = strlen( *path ) + 1;
801 p = realloc( *path, len + extra );
802 if (!p) return 0;
803 memmove( &p[ extra ], p, len );
804 p[ extra - 1 ] = '/';
805 memcpy( p, segment, extra - 1 );
807 else
809 p = malloc( extra );
810 if (!p) return 0;
811 memcpy( p, segment, extra );
814 *path = p;
816 return 1;
819 static void inotify_notify_all( struct inotify_event *ie )
821 unsigned int filter, action;
822 struct inode *inode, *i;
823 char *path = NULL;
824 struct dir *dir;
826 inode = inode_from_wd( ie->wd );
827 if (!inode)
829 fprintf( stderr, "no inode matches %d\n", ie->wd);
830 return;
833 filter = filter_from_event( ie );
835 if (ie->mask & IN_CREATE)
837 if (ie->mask & IN_ISDIR)
838 inode_check_dir( inode, ie->name );
840 action = FILE_ACTION_ADDED;
842 else if (ie->mask & IN_DELETE)
843 action = FILE_ACTION_REMOVED;
844 else if (ie->mask & IN_MOVED_FROM)
845 action = FILE_ACTION_RENAMED_OLD_NAME;
846 else if (ie->mask & IN_MOVED_TO)
847 action = FILE_ACTION_RENAMED_NEW_NAME;
848 else
849 action = FILE_ACTION_MODIFIED;
852 * Work our way up the inode hierarchy
853 * extending the relative path as we go
854 * and notifying all recursive watches.
856 if (!prepend( &path, ie->name ))
857 return;
859 for (i = inode; i; i = i->parent)
861 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
862 if ((filter & dir->filter) && (i==inode || dir->subtree))
863 inotify_do_change_notify( dir, action, ie->cookie, path );
865 if (!i->name || !prepend( &path, i->name ))
866 break;
869 free( path );
871 if (ie->mask & IN_DELETE)
873 i = inode_from_name( inode, ie->name );
874 if (i)
875 free_inode( i );
879 static void inotify_poll_event( struct fd *fd, int event )
881 int r, ofs, unix_fd;
882 char buffer[0x1000];
883 struct inotify_event *ie;
885 unix_fd = get_unix_fd( fd );
886 r = read( unix_fd, buffer, sizeof buffer );
887 if (r < 0)
889 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
890 return;
893 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
895 ie = (struct inotify_event*) &buffer[ofs];
896 ofs += offsetof( struct inotify_event, name[ie->len] );
897 if (ofs > r) break;
898 if (ie->len) inotify_notify_all( ie );
902 static inline struct fd *create_inotify_fd( void )
904 int unix_fd;
906 unix_fd = inotify_init();
907 if (unix_fd<0)
908 return NULL;
909 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
912 static int map_flags( unsigned int filter )
914 unsigned int mask;
916 /* always watch these so we can track subdirectories in recursive watches */
917 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
919 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
920 mask |= IN_ATTRIB;
921 if (filter & FILE_NOTIFY_CHANGE_SIZE)
922 mask |= IN_MODIFY;
923 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
924 mask |= IN_MODIFY;
925 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
926 mask |= IN_MODIFY;
927 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
928 mask |= IN_ATTRIB;
930 return mask;
933 static int inotify_add_dir( char *path, unsigned int filter )
935 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
936 path, map_flags( filter ) );
937 if (wd != -1)
938 set_fd_events( inotify_fd, POLLIN );
939 return wd;
942 static int init_inotify( void )
944 int i;
946 if (inotify_fd)
947 return 1;
949 inotify_fd = create_inotify_fd();
950 if (!inotify_fd)
951 return 0;
953 for (i=0; i<HASH_SIZE; i++)
955 list_init( &inode_hash[i] );
956 list_init( &wd_hash[i] );
959 return 1;
962 static int inotify_adjust_changes( struct dir *dir )
964 unsigned int filter;
965 struct inode *inode;
966 struct stat st;
967 char path[32];
968 int wd, unix_fd;
970 if (!inotify_fd)
971 return 0;
973 unix_fd = get_unix_fd( dir->fd );
975 inode = dir->inode;
976 if (!inode)
978 /* check if this fd is already being watched */
979 if (-1 == fstat( unix_fd, &st ))
980 return 0;
982 inode = get_inode( st.st_dev, st.st_ino );
983 if (!inode)
984 inode = create_inode( st.st_dev, st.st_ino );
985 if (!inode)
986 return 0;
987 list_add_tail( &inode->dirs, &dir->in_entry );
988 dir->inode = inode;
991 filter = filter_from_inode( inode, 0 );
993 sprintf( path, "/proc/self/fd/%u", unix_fd );
994 wd = inotify_add_dir( path, filter );
995 if (wd == -1) return 0;
997 inode_set_wd( inode, wd );
999 return 1;
1002 static char *get_basename( const char *link )
1004 char *buffer, *name = NULL;
1005 int r, n = 0x100;
1007 while (1)
1009 buffer = malloc( n );
1010 if (!buffer) return NULL;
1012 r = readlink( link, buffer, n );
1013 if (r < 0)
1014 break;
1016 if (r < n)
1018 name = buffer;
1019 break;
1021 free( buffer );
1022 n *= 2;
1025 if (name)
1027 while (r > 0 && name[ r - 1 ] == '/' )
1028 r--;
1029 name[ r ] = 0;
1031 name = strrchr( name, '/' );
1032 if (name)
1033 name = strdup( &name[1] );
1036 free( buffer );
1037 return name;
1040 static int dir_add_to_existing_notify( struct dir *dir )
1042 struct inode *inode, *parent;
1043 unsigned int filter = 0;
1044 struct stat st, st_new;
1045 char link[35], *name;
1046 int wd, unix_fd;
1048 if (!inotify_fd)
1049 return 0;
1051 unix_fd = get_unix_fd( dir->fd );
1053 /* check if it's in the list of inodes we want to watch */
1054 if (-1 == fstat( unix_fd, &st_new ))
1055 return 0;
1056 inode = find_inode( st_new.st_dev, st_new.st_ino );
1057 if (inode)
1058 return 0;
1060 /* lookup the parent */
1061 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1062 if (-1 == stat( link, &st ))
1063 return 0;
1066 * If there's no parent, stop. We could keep going adding
1067 * ../ to the path until we hit the root of the tree or
1068 * find a recursively watched ancestor.
1069 * Assume it's too expensive to search up the tree for now.
1071 parent = find_inode( st.st_dev, st.st_ino );
1072 if (!parent)
1073 return 0;
1075 if (parent->wd == -1)
1076 return 0;
1078 filter = filter_from_inode( parent, 1 );
1079 if (!filter)
1080 return 0;
1082 sprintf( link, "/proc/self/fd/%u", unix_fd );
1083 name = get_basename( link );
1084 if (!name)
1085 return 0;
1086 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1087 free( name );
1088 if (!inode)
1089 return 0;
1091 /* Couldn't find this inode at the start of the function, must be new */
1092 assert( inode->wd == -1 );
1094 wd = inotify_add_dir( link, filter );
1095 if (wd != -1)
1096 inode_set_wd( inode, wd );
1098 return 1;
1101 #else
1103 static int init_inotify( void )
1105 return 0;
1108 static int inotify_adjust_changes( struct dir *dir )
1110 return 0;
1113 static void free_inode( struct inode *inode )
1115 assert( 0 );
1118 static int dir_add_to_existing_notify( struct dir *dir )
1120 return 0;
1123 #endif /* HAVE_SYS_INOTIFY_H */
1125 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1127 struct dir *dir;
1129 dir = alloc_object( &dir_ops );
1130 if (!dir)
1131 return NULL;
1133 list_init( &dir->change_records );
1134 dir->filter = 0;
1135 dir->notified = 0;
1136 dir->want_data = 0;
1137 dir->inode = NULL;
1138 grab_object( fd );
1139 dir->fd = fd;
1140 dir->mode = mode;
1141 dir->uid = ~(uid_t)0;
1142 dir->client_process = NULL;
1143 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1145 dir_add_to_existing_notify( dir );
1147 return &dir->obj;
1150 /* retrieve (or allocate) the client-side directory cache entry */
1151 DECL_HANDLER(get_directory_cache_entry)
1153 struct dir *dir;
1154 int *free_entries;
1155 data_size_t free_size;
1157 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1159 if (!dir->client_process)
1161 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1162 dir->client_process = (struct process *)grab_object( current->process );
1165 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1166 else set_error( STATUS_SHARING_VIOLATION );
1168 done: /* allow freeing entries even on failure */
1169 free_size = get_reply_max_size();
1170 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1171 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1173 release_object( dir );
1176 /* enable change notifications for a directory */
1177 DECL_HANDLER(read_directory_changes)
1179 struct dir *dir;
1180 struct async *async;
1182 if (!req->filter)
1184 set_error(STATUS_INVALID_PARAMETER);
1185 return;
1188 dir = get_dir_obj( current->process, req->async.handle, 0 );
1189 if (!dir)
1190 return;
1192 /* requests don't timeout */
1193 if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
1194 fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
1196 /* assign it once */
1197 if (!dir->filter)
1199 init_inotify();
1200 insert_change( dir );
1201 dir->filter = req->filter;
1202 dir->subtree = req->subtree;
1203 dir->want_data = req->want_data;
1206 /* if there's already a change in the queue, send it */
1207 if (!list_empty( &dir->change_records ))
1208 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1210 /* setup the real notification */
1211 if (!inotify_adjust_changes( dir ))
1212 dnotify_adjust_changes( dir );
1214 set_error(STATUS_PENDING);
1216 release_object( async );
1217 end:
1218 release_object( dir );
1221 DECL_HANDLER(read_change)
1223 struct change_record *record, *next;
1224 struct dir *dir;
1225 struct list events;
1226 char *data, *event;
1227 int size = 0;
1229 dir = get_dir_obj( current->process, req->handle, 0 );
1230 if (!dir)
1231 return;
1233 list_init( &events );
1234 list_move_tail( &events, &dir->change_records );
1235 release_object( dir );
1237 if (list_empty( &events ))
1239 set_error( STATUS_NO_DATA_DETECTED );
1240 return;
1243 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1245 size += (offsetof(struct filesystem_event, name[record->event.len])
1246 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1249 if (size > get_reply_max_size())
1250 set_error( STATUS_BUFFER_TOO_SMALL );
1251 else if ((data = mem_alloc( size )) != NULL)
1253 event = data;
1254 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1256 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1258 /* FIXME: rename events are sometimes reported as delete/create */
1259 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1261 struct list *elem = list_next( &events, &record->entry );
1262 if (elem)
1263 next = LIST_ENTRY(elem, struct change_record, entry);
1265 if (elem && next->cookie == record->cookie)
1266 next->cookie = 0;
1267 else
1268 record->event.action = FILE_ACTION_REMOVED;
1270 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1271 record->event.action = FILE_ACTION_ADDED;
1273 memcpy( event, &record->event, len );
1274 event += len;
1275 if (len % sizeof(int))
1277 memset( event, 0, sizeof(int) - len % sizeof(int) );
1278 event += sizeof(int) - len % sizeof(int);
1281 set_reply_data_ptr( data, size );
1284 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1286 list_remove( &record->entry );
1287 free( record );