d3d11: Make extracting input signature more robust.
[wine.git] / server / change.c
blob14fb2f1e76220befafd966dad48eab5230273aa2
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
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 "process.h"
48 #include "security.h"
49 #include "winternl.h"
51 /* dnotify support */
53 #ifdef linux
54 #ifndef F_NOTIFY
55 #define F_NOTIFY 1026
56 #define DN_ACCESS 0x00000001 /* File accessed */
57 #define DN_MODIFY 0x00000002 /* File modified */
58 #define DN_CREATE 0x00000004 /* File created */
59 #define DN_DELETE 0x00000008 /* File removed */
60 #define DN_RENAME 0x00000010 /* File renamed */
61 #define DN_ATTRIB 0x00000020 /* File changed attributes */
62 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
63 #endif
64 #endif
66 /* inotify support */
68 #ifdef HAVE_SYS_INOTIFY_H
69 #include <sys/inotify.h>
70 #define USE_INOTIFY
71 #elif defined(__linux__) && defined(__i386__)
73 #define SYS_inotify_init 291
74 #define SYS_inotify_add_watch 292
75 #define SYS_inotify_rm_watch 293
77 struct inotify_event {
78 int wd;
79 unsigned int mask;
80 unsigned int cookie;
81 unsigned int len;
82 char name[1];
85 #define IN_ACCESS 0x00000001
86 #define IN_MODIFY 0x00000002
87 #define IN_ATTRIB 0x00000004
88 #define IN_CLOSE_WRITE 0x00000008
89 #define IN_CLOSE_NOWRITE 0x00000010
90 #define IN_OPEN 0x00000020
91 #define IN_MOVED_FROM 0x00000040
92 #define IN_MOVED_TO 0x00000080
93 #define IN_CREATE 0x00000100
94 #define IN_DELETE 0x00000200
95 #define IN_DELETE_SELF 0x00000400
97 #define IN_ISDIR 0x40000000
99 static inline int inotify_init( void )
101 return syscall( SYS_inotify_init );
104 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
106 return syscall( SYS_inotify_add_watch, fd, name, mask );
109 static inline int inotify_rm_watch( int fd, int wd )
111 return syscall( SYS_inotify_rm_watch, fd, wd );
114 #define USE_INOTIFY
116 #endif
118 struct inode;
120 static void free_inode( struct inode *inode );
122 static struct fd *inotify_fd;
124 struct change_record {
125 struct list entry;
126 unsigned int cookie;
127 struct filesystem_event event;
130 struct dir
132 struct object obj; /* object header */
133 struct fd *fd; /* file descriptor to the directory */
134 mode_t mode; /* file stat.st_mode */
135 uid_t uid; /* file stat.st_uid */
136 struct list entry; /* entry in global change notifications list */
137 unsigned int filter; /* notification filter */
138 int notified; /* SIGIO counter */
139 int want_data; /* return change data */
140 int subtree; /* do we want to watch subdirectories? */
141 struct list change_records; /* data for the change */
142 struct list in_entry; /* entry in the inode dirs list */
143 struct inode *inode; /* inode of the associated directory */
144 struct process *client_process; /* client process that has a cache for this directory */
145 int client_entry; /* entry in client process cache */
148 static struct fd *dir_get_fd( struct object *obj );
149 static struct security_descriptor *dir_get_sd( struct object *obj );
150 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
151 unsigned int set_info );
152 static void dir_dump( struct object *obj, int verbose );
153 static struct object_type *dir_get_type( struct object *obj );
154 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
155 static void dir_destroy( struct object *obj );
157 static const struct object_ops dir_ops =
159 sizeof(struct dir), /* size */
160 dir_dump, /* dump */
161 dir_get_type, /* get_type */
162 add_queue, /* add_queue */
163 remove_queue, /* remove_queue */
164 default_fd_signaled, /* signaled */
165 no_satisfied, /* satisfied */
166 no_signal, /* signal */
167 dir_get_fd, /* get_fd */
168 default_fd_map_access, /* map_access */
169 dir_get_sd, /* get_sd */
170 dir_set_sd, /* set_sd */
171 no_lookup_name, /* lookup_name */
172 no_link_name, /* link_name */
173 NULL, /* unlink_name */
174 no_open_file, /* open_file */
175 dir_close_handle, /* close_handle */
176 dir_destroy /* destroy */
179 static int dir_get_poll_events( struct fd *fd );
180 static enum server_fd_type dir_get_fd_type( struct fd *fd );
182 static const struct fd_ops dir_fd_ops =
184 dir_get_poll_events, /* get_poll_events */
185 default_poll_event, /* poll_event */
186 dir_get_fd_type, /* get_fd_type */
187 no_fd_read, /* read */
188 no_fd_write, /* write */
189 no_fd_flush, /* flush */
190 default_fd_ioctl, /* ioctl */
191 default_fd_queue_async, /* queue_async */
192 default_fd_reselect_async /* reselect_async */
195 static struct list change_list = LIST_INIT(change_list);
197 /* per-process structure to keep track of cache entries on the client size */
198 struct dir_cache
200 unsigned int size;
201 unsigned int count;
202 unsigned char state[1];
205 enum dir_cache_state
207 DIR_CACHE_STATE_FREE,
208 DIR_CACHE_STATE_INUSE,
209 DIR_CACHE_STATE_RELEASED
212 /* return an array of cache entries that can be freed on the client side */
213 static int *get_free_dir_cache_entries( struct process *process, data_size_t *size )
215 int *ret;
216 struct dir_cache *cache = process->dir_cache;
217 unsigned int i, j, count;
219 if (!cache) return NULL;
220 for (i = count = 0; i < cache->count && count < *size / sizeof(*ret); i++)
221 if (cache->state[i] == DIR_CACHE_STATE_RELEASED) count++;
222 if (!count) return NULL;
224 if ((ret = malloc( count * sizeof(*ret) )))
226 for (i = j = 0; j < count; i++)
228 if (cache->state[i] != DIR_CACHE_STATE_RELEASED) continue;
229 cache->state[i] = DIR_CACHE_STATE_FREE;
230 ret[j++] = i;
232 *size = count * sizeof(*ret);
234 return ret;
237 /* allocate a new client-side directory cache entry */
238 static int alloc_dir_cache_entry( struct dir *dir, struct process *process )
240 unsigned int i = 0;
241 struct dir_cache *cache = process->dir_cache;
243 if (cache)
244 for (i = 0; i < cache->count; i++)
245 if (cache->state[i] == DIR_CACHE_STATE_FREE) goto found;
247 if (!cache || cache->count == cache->size)
249 unsigned int size = cache ? cache->size * 2 : 256;
250 if (!(cache = realloc( cache, offsetof( struct dir_cache, state[size] ))))
252 set_error( STATUS_NO_MEMORY );
253 return -1;
255 process->dir_cache = cache;
256 cache->size = size;
258 cache->count = i + 1;
260 found:
261 cache->state[i] = DIR_CACHE_STATE_INUSE;
262 return i;
265 /* release a directory cache entry; it will be freed on the client side on the next cache request */
266 static void release_dir_cache_entry( struct dir *dir )
268 struct dir_cache *cache;
270 if (!dir->client_process) return;
271 cache = dir->client_process->dir_cache;
272 cache->state[dir->client_entry] = DIR_CACHE_STATE_RELEASED;
273 release_object( dir->client_process );
274 dir->client_process = NULL;
277 static void dnotify_adjust_changes( struct dir *dir )
279 #if defined(F_SETSIG) && defined(F_NOTIFY)
280 int fd = get_unix_fd( dir->fd );
281 unsigned int filter = dir->filter;
282 unsigned int val;
283 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
284 return;
286 val = DN_MULTISHOT;
287 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
288 val |= DN_RENAME | DN_DELETE | DN_CREATE;
289 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
290 val |= DN_RENAME | DN_DELETE | DN_CREATE;
291 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
292 val |= DN_ATTRIB;
293 if (filter & FILE_NOTIFY_CHANGE_SIZE)
294 val |= DN_MODIFY;
295 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
296 val |= DN_MODIFY;
297 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
298 val |= DN_ACCESS;
299 if (filter & FILE_NOTIFY_CHANGE_CREATION)
300 val |= DN_CREATE;
301 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
302 val |= DN_ATTRIB;
303 fcntl( fd, F_NOTIFY, val );
304 #endif
307 /* insert change in the global list */
308 static inline void insert_change( struct dir *dir )
310 sigset_t sigset;
312 sigemptyset( &sigset );
313 sigaddset( &sigset, SIGIO );
314 sigprocmask( SIG_BLOCK, &sigset, NULL );
315 list_add_head( &change_list, &dir->entry );
316 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
319 /* remove change from the global list */
320 static inline void remove_change( struct dir *dir )
322 sigset_t sigset;
324 sigemptyset( &sigset );
325 sigaddset( &sigset, SIGIO );
326 sigprocmask( SIG_BLOCK, &sigset, NULL );
327 list_remove( &dir->entry );
328 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
331 static void dir_dump( struct object *obj, int verbose )
333 struct dir *dir = (struct dir *)obj;
334 assert( obj->ops == &dir_ops );
335 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
338 static struct object_type *dir_get_type( struct object *obj )
340 static const WCHAR name[] = {'F','i','l','e'};
341 static const struct unicode_str str = { name, sizeof(name) };
342 return get_object_type( &str );
345 /* enter here directly from SIGIO signal handler */
346 void do_change_notify( int unix_fd )
348 struct dir *dir;
350 /* FIXME: this is O(n) ... probably can be improved */
351 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
353 if (get_unix_fd( dir->fd ) != unix_fd) continue;
354 interlocked_xchg_add( &dir->notified, 1 );
355 break;
359 /* SIGIO callback, called synchronously with the poll loop */
360 void sigio_callback(void)
362 struct dir *dir;
364 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
366 if (interlocked_xchg( &dir->notified, 0 ))
367 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
371 static struct fd *dir_get_fd( struct object *obj )
373 struct dir *dir = (struct dir *)obj;
374 assert( obj->ops == &dir_ops );
375 return (struct fd *)grab_object( dir->fd );
378 static int get_dir_unix_fd( struct dir *dir )
380 return get_unix_fd( dir->fd );
383 static struct security_descriptor *dir_get_sd( struct object *obj )
385 struct dir *dir = (struct dir *)obj;
386 int unix_fd;
387 struct stat st;
388 struct security_descriptor *sd;
389 assert( obj->ops == &dir_ops );
391 unix_fd = get_dir_unix_fd( dir );
393 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
394 return obj->sd;
396 /* mode and uid the same? if so, no need to re-generate security descriptor */
397 if (obj->sd &&
398 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
399 (st.st_uid == dir->uid))
400 return obj->sd;
402 sd = mode_to_sd( st.st_mode,
403 security_unix_uid_to_sid( st.st_uid ),
404 token_get_primary_group( current->process->token ));
405 if (!sd) return obj->sd;
407 dir->mode = st.st_mode;
408 dir->uid = st.st_uid;
409 free( obj->sd );
410 obj->sd = sd;
411 return sd;
414 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
415 unsigned int set_info )
417 struct dir *dir = (struct dir *)obj;
418 const SID *owner;
419 struct stat st;
420 mode_t mode;
421 int unix_fd;
423 assert( obj->ops == &dir_ops );
425 unix_fd = get_dir_unix_fd( dir );
427 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
429 if (set_info & OWNER_SECURITY_INFORMATION)
431 owner = sd_get_owner( sd );
432 if (!owner)
434 set_error( STATUS_INVALID_SECURITY_DESCR );
435 return 0;
437 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
439 /* FIXME: get Unix uid and call fchown */
442 else if (obj->sd)
443 owner = sd_get_owner( obj->sd );
444 else
445 owner = token_get_user( current->process->token );
447 if (set_info & DACL_SECURITY_INFORMATION)
449 /* keep the bits that we don't map to access rights in the ACL */
450 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
451 mode |= sd_to_mode( sd, owner );
453 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
455 file_set_error();
456 return 0;
459 return 1;
462 static struct change_record *get_first_change_record( struct dir *dir )
464 struct list *ptr = list_head( &dir->change_records );
465 if (!ptr) return NULL;
466 list_remove( ptr );
467 return LIST_ENTRY( ptr, struct change_record, entry );
470 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
472 struct dir *dir = (struct dir *)obj;
474 if (!fd_close_handle( obj, process, handle )) return 0;
475 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
476 return 1; /* ok to close */
479 static void dir_destroy( struct object *obj )
481 struct change_record *record;
482 struct dir *dir = (struct dir *)obj;
483 assert (obj->ops == &dir_ops);
485 if (dir->filter)
486 remove_change( dir );
488 if (dir->inode)
490 list_remove( &dir->in_entry );
491 free_inode( dir->inode );
494 while ((record = get_first_change_record( dir ))) free( record );
496 release_dir_cache_entry( dir );
497 release_object( dir->fd );
499 if (inotify_fd && list_empty( &change_list ))
501 release_object( inotify_fd );
502 inotify_fd = NULL;
506 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
508 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
511 static int dir_get_poll_events( struct fd *fd )
513 return 0;
516 static enum server_fd_type dir_get_fd_type( struct fd *fd )
518 return FD_TYPE_DIR;
521 #ifdef USE_INOTIFY
523 #define HASH_SIZE 31
525 struct inode {
526 struct list ch_entry; /* entry in the children list */
527 struct list children; /* children of this inode */
528 struct inode *parent; /* parent of this inode */
529 struct list dirs; /* directory handles watching this inode */
530 struct list ino_entry; /* entry in the inode hash */
531 struct list wd_entry; /* entry in the watch descriptor hash */
532 dev_t dev; /* device number */
533 ino_t ino; /* device's inode number */
534 int wd; /* inotify's watch descriptor */
535 char *name; /* basename name of the inode */
538 static struct list inode_hash[ HASH_SIZE ];
539 static struct list wd_hash[ HASH_SIZE ];
541 static int inotify_add_dir( char *path, unsigned int filter );
543 static struct inode *inode_from_wd( int wd )
545 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
546 struct inode *inode;
548 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
549 if (inode->wd == wd)
550 return inode;
552 return NULL;
555 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
557 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
560 static struct inode *find_inode( dev_t dev, ino_t ino )
562 struct list *bucket = get_hash_list( dev, ino );
563 struct inode *inode;
565 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
566 if (inode->ino == ino && inode->dev == dev)
567 return inode;
569 return NULL;
572 static struct inode *create_inode( dev_t dev, ino_t ino )
574 struct inode *inode;
576 inode = malloc( sizeof *inode );
577 if (inode)
579 list_init( &inode->children );
580 list_init( &inode->dirs );
581 inode->ino = ino;
582 inode->dev = dev;
583 inode->wd = -1;
584 inode->parent = NULL;
585 inode->name = NULL;
586 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
588 return inode;
591 static struct inode *get_inode( dev_t dev, ino_t ino )
593 struct inode *inode;
595 inode = find_inode( dev, ino );
596 if (inode)
597 return inode;
598 return create_inode( dev, ino );
601 static void inode_set_wd( struct inode *inode, int wd )
603 if (inode->wd != -1)
604 list_remove( &inode->wd_entry );
605 inode->wd = wd;
606 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
609 static void inode_set_name( struct inode *inode, const char *name )
611 free (inode->name);
612 inode->name = name ? strdup( name ) : NULL;
615 static void free_inode( struct inode *inode )
617 int subtree = 0, watches = 0;
618 struct inode *tmp, *next;
619 struct dir *dir;
621 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
623 subtree |= dir->subtree;
624 watches++;
627 if (!subtree && !inode->parent)
629 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
630 struct inode, ch_entry )
632 assert( tmp != inode );
633 assert( tmp->parent == inode );
634 free_inode( tmp );
638 if (watches)
639 return;
641 if (inode->parent)
642 list_remove( &inode->ch_entry );
644 /* disconnect remaining children from the parent */
645 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
647 list_remove( &tmp->ch_entry );
648 tmp->parent = NULL;
651 if (inode->wd != -1)
653 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
654 list_remove( &inode->wd_entry );
656 list_remove( &inode->ino_entry );
658 free( inode->name );
659 free( inode );
662 static struct inode *inode_add( struct inode *parent,
663 dev_t dev, ino_t ino, const char *name )
665 struct inode *inode;
667 inode = get_inode( dev, ino );
668 if (!inode)
669 return NULL;
671 if (!inode->parent)
673 list_add_tail( &parent->children, &inode->ch_entry );
674 inode->parent = parent;
675 assert( inode != parent );
677 inode_set_name( inode, name );
679 return inode;
682 static struct inode *inode_from_name( struct inode *inode, const char *name )
684 struct inode *i;
686 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
687 if (i->name && !strcmp( i->name, name ))
688 return i;
689 return NULL;
692 static int inotify_get_poll_events( struct fd *fd );
693 static void inotify_poll_event( struct fd *fd, int event );
695 static const struct fd_ops inotify_fd_ops =
697 inotify_get_poll_events, /* get_poll_events */
698 inotify_poll_event, /* poll_event */
699 NULL, /* flush */
700 NULL, /* get_fd_type */
701 NULL, /* ioctl */
702 NULL, /* queue_async */
703 NULL /* reselect_async */
706 static int inotify_get_poll_events( struct fd *fd )
708 return POLLIN;
711 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
712 unsigned int cookie, const char *relpath )
714 struct change_record *record;
716 assert( dir->obj.ops == &dir_ops );
718 if (dir->want_data)
720 size_t len = strlen(relpath);
721 record = malloc( offsetof(struct change_record, event.name[len]) );
722 if (!record)
723 return;
725 record->cookie = cookie;
726 record->event.action = action;
727 memcpy( record->event.name, relpath, len );
728 record->event.len = len;
730 list_add_tail( &dir->change_records, &record->entry );
733 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
736 static unsigned int filter_from_event( struct inotify_event *ie )
738 unsigned int filter = 0;
740 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
741 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
742 if (ie->mask & IN_MODIFY)
743 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
744 if (ie->mask & IN_ATTRIB)
745 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
746 if (ie->mask & IN_ACCESS)
747 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
748 if (ie->mask & IN_CREATE)
749 filter |= FILE_NOTIFY_CHANGE_CREATION;
751 if (ie->mask & IN_ISDIR)
752 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
753 else
754 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
756 return filter;
759 /* scan up the parent directories for watches */
760 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
762 unsigned int filter = 0;
763 struct dir *dir;
765 /* combine filters from parents watching subtrees */
766 while (inode)
768 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
769 if (dir->subtree || !is_parent)
770 filter |= dir->filter;
771 is_parent = 1;
772 inode = inode->parent;
775 return filter;
778 static char *inode_get_path( struct inode *inode, int sz )
780 struct list *head;
781 char *path;
782 int len;
784 if (!inode)
785 return NULL;
787 head = list_head( &inode->dirs );
788 if (head)
790 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
791 path = malloc ( 32 + sz );
792 if (path)
793 sprintf( path, "/proc/self/fd/%u/", unix_fd );
794 return path;
797 if (!inode->name)
798 return NULL;
800 len = strlen( inode->name );
801 path = inode_get_path( inode->parent, sz + len + 1 );
802 if (!path)
803 return NULL;
805 strcat( path, inode->name );
806 strcat( path, "/" );
808 return path;
811 static void inode_check_dir( struct inode *parent, const char *name )
813 char *path;
814 unsigned int filter;
815 struct inode *inode;
816 struct stat st;
817 int wd = -1;
819 path = inode_get_path( parent, strlen(name) );
820 if (!path)
821 return;
823 strcat( path, name );
825 if (stat( path, &st ) < 0)
826 goto end;
828 filter = filter_from_inode( parent, 1 );
829 if (!filter)
830 goto end;
832 inode = inode_add( parent, st.st_dev, st.st_ino, name );
833 if (!inode || inode->wd != -1)
834 goto end;
836 wd = inotify_add_dir( path, filter );
837 if (wd != -1)
838 inode_set_wd( inode, wd );
839 else
840 free_inode( inode );
842 end:
843 free( path );
846 static int prepend( char **path, const char *segment )
848 int extra;
849 char *p;
851 extra = strlen( segment ) + 1;
852 if (*path)
854 int len = strlen( *path ) + 1;
855 p = realloc( *path, len + extra );
856 if (!p) return 0;
857 memmove( &p[ extra ], p, len );
858 p[ extra - 1 ] = '/';
859 memcpy( p, segment, extra - 1 );
861 else
863 p = malloc( extra );
864 if (!p) return 0;
865 memcpy( p, segment, extra );
868 *path = p;
870 return 1;
873 static void inotify_notify_all( struct inotify_event *ie )
875 unsigned int filter, action;
876 struct inode *inode, *i;
877 char *path = NULL;
878 struct dir *dir;
880 inode = inode_from_wd( ie->wd );
881 if (!inode)
883 fprintf( stderr, "no inode matches %d\n", ie->wd);
884 return;
887 filter = filter_from_event( ie );
889 if (ie->mask & IN_CREATE)
891 if (ie->mask & IN_ISDIR)
892 inode_check_dir( inode, ie->name );
894 action = FILE_ACTION_ADDED;
896 else if (ie->mask & IN_DELETE)
897 action = FILE_ACTION_REMOVED;
898 else if (ie->mask & IN_MOVED_FROM)
899 action = FILE_ACTION_RENAMED_OLD_NAME;
900 else if (ie->mask & IN_MOVED_TO)
901 action = FILE_ACTION_RENAMED_NEW_NAME;
902 else
903 action = FILE_ACTION_MODIFIED;
906 * Work our way up the inode hierarchy
907 * extending the relative path as we go
908 * and notifying all recursive watches.
910 if (!prepend( &path, ie->name ))
911 return;
913 for (i = inode; i; i = i->parent)
915 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
916 if ((filter & dir->filter) && (i==inode || dir->subtree))
917 inotify_do_change_notify( dir, action, ie->cookie, path );
919 if (!i->name || !prepend( &path, i->name ))
920 break;
923 free( path );
925 if (ie->mask & IN_DELETE)
927 i = inode_from_name( inode, ie->name );
928 if (i)
929 free_inode( i );
933 static void inotify_poll_event( struct fd *fd, int event )
935 int r, ofs, unix_fd;
936 char buffer[0x1000];
937 struct inotify_event *ie;
939 unix_fd = get_unix_fd( fd );
940 r = read( unix_fd, buffer, sizeof buffer );
941 if (r < 0)
943 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
944 return;
947 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
949 ie = (struct inotify_event*) &buffer[ofs];
950 if (!ie->len)
951 break;
952 ofs += offsetof( struct inotify_event, name[ie->len] );
953 if (ofs > r) break;
954 inotify_notify_all( ie );
958 static inline struct fd *create_inotify_fd( void )
960 int unix_fd;
962 unix_fd = inotify_init();
963 if (unix_fd<0)
964 return NULL;
965 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
968 static int map_flags( unsigned int filter )
970 unsigned int mask;
972 /* always watch these so we can track subdirectories in recursive watches */
973 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
975 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
976 mask |= IN_ATTRIB;
977 if (filter & FILE_NOTIFY_CHANGE_SIZE)
978 mask |= IN_MODIFY;
979 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
980 mask |= IN_MODIFY;
981 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
982 mask |= IN_ACCESS;
983 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
984 mask |= IN_ATTRIB;
986 return mask;
989 static int inotify_add_dir( char *path, unsigned int filter )
991 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
992 path, map_flags( filter ) );
993 if (wd != -1)
994 set_fd_events( inotify_fd, POLLIN );
995 return wd;
998 static int init_inotify( void )
1000 int i;
1002 if (inotify_fd)
1003 return 1;
1005 inotify_fd = create_inotify_fd();
1006 if (!inotify_fd)
1007 return 0;
1009 for (i=0; i<HASH_SIZE; i++)
1011 list_init( &inode_hash[i] );
1012 list_init( &wd_hash[i] );
1015 return 1;
1018 static int inotify_adjust_changes( struct dir *dir )
1020 unsigned int filter;
1021 struct inode *inode;
1022 struct stat st;
1023 char path[32];
1024 int wd, unix_fd;
1026 if (!inotify_fd)
1027 return 0;
1029 unix_fd = get_unix_fd( dir->fd );
1031 inode = dir->inode;
1032 if (!inode)
1034 /* check if this fd is already being watched */
1035 if (-1 == fstat( unix_fd, &st ))
1036 return 0;
1038 inode = get_inode( st.st_dev, st.st_ino );
1039 if (!inode)
1040 inode = create_inode( st.st_dev, st.st_ino );
1041 if (!inode)
1042 return 0;
1043 list_add_tail( &inode->dirs, &dir->in_entry );
1044 dir->inode = inode;
1047 filter = filter_from_inode( inode, 0 );
1049 sprintf( path, "/proc/self/fd/%u", unix_fd );
1050 wd = inotify_add_dir( path, filter );
1051 if (wd == -1) return 0;
1053 inode_set_wd( inode, wd );
1055 return 1;
1058 static char *get_basename( const char *link )
1060 char *buffer, *name = NULL;
1061 int r, n = 0x100;
1063 while (1)
1065 buffer = malloc( n );
1066 if (!buffer) return NULL;
1068 r = readlink( link, buffer, n );
1069 if (r < 0)
1070 break;
1072 if (r < n)
1074 name = buffer;
1075 break;
1077 free( buffer );
1078 n *= 2;
1081 if (name)
1083 while (r > 0 && name[ r - 1 ] == '/' )
1084 r--;
1085 name[ r ] = 0;
1087 name = strrchr( name, '/' );
1088 if (name)
1089 name = strdup( &name[1] );
1092 free( buffer );
1093 return name;
1096 static int dir_add_to_existing_notify( struct dir *dir )
1098 struct inode *inode, *parent;
1099 unsigned int filter = 0;
1100 struct stat st, st_new;
1101 char link[35], *name;
1102 int wd, unix_fd;
1104 if (!inotify_fd)
1105 return 0;
1107 unix_fd = get_unix_fd( dir->fd );
1109 /* check if it's in the list of inodes we want to watch */
1110 if (-1 == fstat( unix_fd, &st_new ))
1111 return 0;
1112 inode = find_inode( st_new.st_dev, st_new.st_ino );
1113 if (inode)
1114 return 0;
1116 /* lookup the parent */
1117 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1118 if (-1 == stat( link, &st ))
1119 return 0;
1122 * If there's no parent, stop. We could keep going adding
1123 * ../ to the path until we hit the root of the tree or
1124 * find a recursively watched ancestor.
1125 * Assume it's too expensive to search up the tree for now.
1127 parent = find_inode( st.st_dev, st.st_ino );
1128 if (!parent)
1129 return 0;
1131 if (parent->wd == -1)
1132 return 0;
1134 filter = filter_from_inode( parent, 1 );
1135 if (!filter)
1136 return 0;
1138 sprintf( link, "/proc/self/fd/%u", unix_fd );
1139 name = get_basename( link );
1140 if (!name)
1141 return 0;
1142 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1143 free( name );
1144 if (!inode)
1145 return 0;
1147 /* Couldn't find this inode at the start of the function, must be new */
1148 assert( inode->wd == -1 );
1150 wd = inotify_add_dir( link, filter );
1151 if (wd != -1)
1152 inode_set_wd( inode, wd );
1154 return 1;
1157 #else
1159 static int init_inotify( void )
1161 return 0;
1164 static int inotify_adjust_changes( struct dir *dir )
1166 return 0;
1169 static void free_inode( struct inode *inode )
1171 assert( 0 );
1174 static int dir_add_to_existing_notify( struct dir *dir )
1176 return 0;
1179 #endif /* USE_INOTIFY */
1181 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1183 struct dir *dir;
1185 dir = alloc_object( &dir_ops );
1186 if (!dir)
1187 return NULL;
1189 list_init( &dir->change_records );
1190 dir->filter = 0;
1191 dir->notified = 0;
1192 dir->want_data = 0;
1193 dir->inode = NULL;
1194 grab_object( fd );
1195 dir->fd = fd;
1196 dir->mode = mode;
1197 dir->uid = ~(uid_t)0;
1198 dir->client_process = NULL;
1199 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1201 dir_add_to_existing_notify( dir );
1203 return &dir->obj;
1206 /* retrieve (or allocate) the client-side directory cache entry */
1207 DECL_HANDLER(get_directory_cache_entry)
1209 struct dir *dir;
1210 int *free_entries;
1211 data_size_t free_size;
1213 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1215 if (!dir->client_process)
1217 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1218 dir->client_process = (struct process *)grab_object( current->process );
1221 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1222 else set_error( STATUS_SHARING_VIOLATION );
1224 done: /* allow freeing entries even on failure */
1225 free_size = get_reply_max_size();
1226 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1227 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1229 release_object( dir );
1232 /* enable change notifications for a directory */
1233 DECL_HANDLER(read_directory_changes)
1235 struct dir *dir;
1236 struct async *async;
1238 if (!req->filter)
1240 set_error(STATUS_INVALID_PARAMETER);
1241 return;
1244 dir = get_dir_obj( current->process, req->async.handle, 0 );
1245 if (!dir)
1246 return;
1248 /* requests don't timeout */
1249 if (!(async = create_async( current, &req->async, NULL ))) goto end;
1250 if (!fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT )) goto end;
1252 /* assign it once */
1253 if (!dir->filter)
1255 init_inotify();
1256 insert_change( dir );
1257 dir->filter = req->filter;
1258 dir->subtree = req->subtree;
1259 dir->want_data = req->want_data;
1262 /* if there's already a change in the queue, send it */
1263 if (!list_empty( &dir->change_records ))
1264 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1266 /* setup the real notification */
1267 if (!inotify_adjust_changes( dir ))
1268 dnotify_adjust_changes( dir );
1270 set_error(STATUS_PENDING);
1272 end:
1273 if (async) release_object( async );
1274 release_object( dir );
1277 DECL_HANDLER(read_change)
1279 struct change_record *record, *next;
1280 struct dir *dir;
1281 struct list events;
1282 char *data, *event;
1283 int size = 0;
1285 dir = get_dir_obj( current->process, req->handle, 0 );
1286 if (!dir)
1287 return;
1289 list_init( &events );
1290 list_move_tail( &events, &dir->change_records );
1291 release_object( dir );
1293 if (list_empty( &events ))
1295 set_error( STATUS_NO_DATA_DETECTED );
1296 return;
1299 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1301 size += (offsetof(struct filesystem_event, name[record->event.len])
1302 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1305 if (size > get_reply_max_size())
1306 set_error( STATUS_BUFFER_TOO_SMALL );
1307 else if ((data = mem_alloc( size )) != NULL)
1309 event = data;
1310 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1312 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1314 /* FIXME: rename events are sometimes reported as delete/create */
1315 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1317 struct list *elem = list_next( &events, &record->entry );
1318 if (elem)
1319 next = LIST_ENTRY(elem, struct change_record, entry);
1321 if (elem && next->cookie == record->cookie)
1322 next->cookie = 0;
1323 else
1324 record->event.action = FILE_ACTION_REMOVED;
1326 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1327 record->event.action = FILE_ACTION_ADDED;
1329 memcpy( event, &record->event, len );
1330 event += len;
1331 if (len % sizeof(int))
1333 memset( event, 0, sizeof(int) - len % sizeof(int) );
1334 event += sizeof(int) - len % sizeof(int);
1337 set_reply_data_ptr( data, size );
1340 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1342 list_remove( &record->entry );
1343 free( record );