amstream: Leave critical section on success (Coverity).
[wine.git] / server / change.c
blob6091d21f7f210917fdfe601901093fb40b579467
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 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 interlocked_xchg_add( &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 (interlocked_xchg( &dir->notified, 0 ))
323 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
327 static struct fd *dir_get_fd( struct object *obj )
329 struct dir *dir = (struct dir *)obj;
330 assert( obj->ops == &dir_ops );
331 return (struct fd *)grab_object( dir->fd );
334 static int get_dir_unix_fd( struct dir *dir )
336 return get_unix_fd( dir->fd );
339 static struct security_descriptor *dir_get_sd( struct object *obj )
341 struct dir *dir = (struct dir *)obj;
342 int unix_fd;
343 struct stat st;
344 struct security_descriptor *sd;
345 assert( obj->ops == &dir_ops );
347 unix_fd = get_dir_unix_fd( dir );
349 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
350 return obj->sd;
352 /* mode and uid the same? if so, no need to re-generate security descriptor */
353 if (obj->sd &&
354 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
355 (st.st_uid == dir->uid))
356 return obj->sd;
358 sd = mode_to_sd( st.st_mode,
359 security_unix_uid_to_sid( st.st_uid ),
360 token_get_primary_group( current->process->token ));
361 if (!sd) return obj->sd;
363 dir->mode = st.st_mode;
364 dir->uid = st.st_uid;
365 free( obj->sd );
366 obj->sd = sd;
367 return sd;
370 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
371 unsigned int set_info )
373 struct dir *dir = (struct dir *)obj;
374 const SID *owner;
375 struct stat st;
376 mode_t mode;
377 int unix_fd;
379 assert( obj->ops == &dir_ops );
381 unix_fd = get_dir_unix_fd( dir );
383 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
385 if (set_info & OWNER_SECURITY_INFORMATION)
387 owner = sd_get_owner( sd );
388 if (!owner)
390 set_error( STATUS_INVALID_SECURITY_DESCR );
391 return 0;
393 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
395 /* FIXME: get Unix uid and call fchown */
398 else if (obj->sd)
399 owner = sd_get_owner( obj->sd );
400 else
401 owner = token_get_user( current->process->token );
403 if (set_info & DACL_SECURITY_INFORMATION)
405 /* keep the bits that we don't map to access rights in the ACL */
406 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
407 mode |= sd_to_mode( sd, owner );
409 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
411 file_set_error();
412 return 0;
415 return 1;
418 static struct change_record *get_first_change_record( struct dir *dir )
420 struct list *ptr = list_head( &dir->change_records );
421 if (!ptr) return NULL;
422 list_remove( ptr );
423 return LIST_ENTRY( ptr, struct change_record, entry );
426 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
428 struct dir *dir = (struct dir *)obj;
430 if (!fd_close_handle( obj, process, handle )) return 0;
431 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
432 return 1; /* ok to close */
435 static void dir_destroy( struct object *obj )
437 struct change_record *record;
438 struct dir *dir = (struct dir *)obj;
439 assert (obj->ops == &dir_ops);
441 if (dir->filter)
442 remove_change( dir );
444 if (dir->inode)
446 list_remove( &dir->in_entry );
447 free_inode( dir->inode );
450 while ((record = get_first_change_record( dir ))) free( record );
452 release_dir_cache_entry( dir );
453 release_object( dir->fd );
455 if (inotify_fd && list_empty( &change_list ))
457 release_object( inotify_fd );
458 inotify_fd = NULL;
462 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
464 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
467 static int dir_get_poll_events( struct fd *fd )
469 return 0;
472 static enum server_fd_type dir_get_fd_type( struct fd *fd )
474 return FD_TYPE_DIR;
477 #ifdef HAVE_SYS_INOTIFY_H
479 #define HASH_SIZE 31
481 struct inode {
482 struct list ch_entry; /* entry in the children list */
483 struct list children; /* children of this inode */
484 struct inode *parent; /* parent of this inode */
485 struct list dirs; /* directory handles watching this inode */
486 struct list ino_entry; /* entry in the inode hash */
487 struct list wd_entry; /* entry in the watch descriptor hash */
488 dev_t dev; /* device number */
489 ino_t ino; /* device's inode number */
490 int wd; /* inotify's watch descriptor */
491 char *name; /* basename name of the inode */
494 static struct list inode_hash[ HASH_SIZE ];
495 static struct list wd_hash[ HASH_SIZE ];
497 static int inotify_add_dir( char *path, unsigned int filter );
499 static struct inode *inode_from_wd( int wd )
501 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
502 struct inode *inode;
504 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
505 if (inode->wd == wd)
506 return inode;
508 return NULL;
511 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
513 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
516 static struct inode *find_inode( dev_t dev, ino_t ino )
518 struct list *bucket = get_hash_list( dev, ino );
519 struct inode *inode;
521 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
522 if (inode->ino == ino && inode->dev == dev)
523 return inode;
525 return NULL;
528 static struct inode *create_inode( dev_t dev, ino_t ino )
530 struct inode *inode;
532 inode = malloc( sizeof *inode );
533 if (inode)
535 list_init( &inode->children );
536 list_init( &inode->dirs );
537 inode->ino = ino;
538 inode->dev = dev;
539 inode->wd = -1;
540 inode->parent = NULL;
541 inode->name = NULL;
542 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
544 return inode;
547 static struct inode *get_inode( dev_t dev, ino_t ino )
549 struct inode *inode;
551 inode = find_inode( dev, ino );
552 if (inode)
553 return inode;
554 return create_inode( dev, ino );
557 static void inode_set_wd( struct inode *inode, int wd )
559 if (inode->wd != -1)
560 list_remove( &inode->wd_entry );
561 inode->wd = wd;
562 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
565 static void inode_set_name( struct inode *inode, const char *name )
567 free (inode->name);
568 inode->name = name ? strdup( name ) : NULL;
571 static void free_inode( struct inode *inode )
573 int subtree = 0, watches = 0;
574 struct inode *tmp, *next;
575 struct dir *dir;
577 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
579 subtree |= dir->subtree;
580 watches++;
583 if (!subtree && !inode->parent)
585 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
586 struct inode, ch_entry )
588 assert( tmp != inode );
589 assert( tmp->parent == inode );
590 free_inode( tmp );
594 if (watches)
595 return;
597 if (inode->parent)
598 list_remove( &inode->ch_entry );
600 /* disconnect remaining children from the parent */
601 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
603 list_remove( &tmp->ch_entry );
604 tmp->parent = NULL;
607 if (inode->wd != -1)
609 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
610 list_remove( &inode->wd_entry );
612 list_remove( &inode->ino_entry );
614 free( inode->name );
615 free( inode );
618 static struct inode *inode_add( struct inode *parent,
619 dev_t dev, ino_t ino, const char *name )
621 struct inode *inode;
623 inode = get_inode( dev, ino );
624 if (!inode)
625 return NULL;
627 if (!inode->parent)
629 list_add_tail( &parent->children, &inode->ch_entry );
630 inode->parent = parent;
631 assert( inode != parent );
633 inode_set_name( inode, name );
635 return inode;
638 static struct inode *inode_from_name( struct inode *inode, const char *name )
640 struct inode *i;
642 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
643 if (i->name && !strcmp( i->name, name ))
644 return i;
645 return NULL;
648 static int inotify_get_poll_events( struct fd *fd );
649 static void inotify_poll_event( struct fd *fd, int event );
651 static const struct fd_ops inotify_fd_ops =
653 inotify_get_poll_events, /* get_poll_events */
654 inotify_poll_event, /* poll_event */
655 NULL, /* flush */
656 NULL, /* get_fd_type */
657 NULL, /* ioctl */
658 NULL, /* queue_async */
659 NULL /* reselect_async */
662 static int inotify_get_poll_events( struct fd *fd )
664 return POLLIN;
667 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
668 unsigned int cookie, const char *relpath )
670 struct change_record *record;
672 assert( dir->obj.ops == &dir_ops );
674 if (dir->want_data)
676 size_t len = strlen(relpath);
677 record = malloc( offsetof(struct change_record, event.name[len]) );
678 if (!record)
679 return;
681 record->cookie = cookie;
682 record->event.action = action;
683 memcpy( record->event.name, relpath, len );
684 record->event.len = len;
686 list_add_tail( &dir->change_records, &record->entry );
689 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
692 static unsigned int filter_from_event( struct inotify_event *ie )
694 unsigned int filter = 0;
696 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
697 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
698 if (ie->mask & IN_MODIFY)
699 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
700 if (ie->mask & IN_ATTRIB)
701 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
702 if (ie->mask & IN_CREATE)
703 filter |= FILE_NOTIFY_CHANGE_CREATION;
705 if (ie->mask & IN_ISDIR)
706 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
707 else
708 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
710 return filter;
713 /* scan up the parent directories for watches */
714 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
716 unsigned int filter = 0;
717 struct dir *dir;
719 /* combine filters from parents watching subtrees */
720 while (inode)
722 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
723 if (dir->subtree || !is_parent)
724 filter |= dir->filter;
725 is_parent = 1;
726 inode = inode->parent;
729 return filter;
732 static char *inode_get_path( struct inode *inode, int sz )
734 struct list *head;
735 char *path;
736 int len;
738 if (!inode)
739 return NULL;
741 head = list_head( &inode->dirs );
742 if (head)
744 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
745 path = malloc ( 32 + sz );
746 if (path)
747 sprintf( path, "/proc/self/fd/%u/", unix_fd );
748 return path;
751 if (!inode->name)
752 return NULL;
754 len = strlen( inode->name );
755 path = inode_get_path( inode->parent, sz + len + 1 );
756 if (!path)
757 return NULL;
759 strcat( path, inode->name );
760 strcat( path, "/" );
762 return path;
765 static void inode_check_dir( struct inode *parent, const char *name )
767 char *path;
768 unsigned int filter;
769 struct inode *inode;
770 struct stat st;
771 int wd = -1;
773 path = inode_get_path( parent, strlen(name) );
774 if (!path)
775 return;
777 strcat( path, name );
779 if (stat( path, &st ) < 0)
780 goto end;
782 filter = filter_from_inode( parent, 1 );
783 if (!filter)
784 goto end;
786 inode = inode_add( parent, st.st_dev, st.st_ino, name );
787 if (!inode || inode->wd != -1)
788 goto end;
790 wd = inotify_add_dir( path, filter );
791 if (wd != -1)
792 inode_set_wd( inode, wd );
793 else
794 free_inode( inode );
796 end:
797 free( path );
800 static int prepend( char **path, const char *segment )
802 int extra;
803 char *p;
805 extra = strlen( segment ) + 1;
806 if (*path)
808 int len = strlen( *path ) + 1;
809 p = realloc( *path, len + extra );
810 if (!p) return 0;
811 memmove( &p[ extra ], p, len );
812 p[ extra - 1 ] = '/';
813 memcpy( p, segment, extra - 1 );
815 else
817 p = malloc( extra );
818 if (!p) return 0;
819 memcpy( p, segment, extra );
822 *path = p;
824 return 1;
827 static void inotify_notify_all( struct inotify_event *ie )
829 unsigned int filter, action;
830 struct inode *inode, *i;
831 char *path = NULL;
832 struct dir *dir;
834 inode = inode_from_wd( ie->wd );
835 if (!inode)
837 fprintf( stderr, "no inode matches %d\n", ie->wd);
838 return;
841 filter = filter_from_event( ie );
843 if (ie->mask & IN_CREATE)
845 if (ie->mask & IN_ISDIR)
846 inode_check_dir( inode, ie->name );
848 action = FILE_ACTION_ADDED;
850 else if (ie->mask & IN_DELETE)
851 action = FILE_ACTION_REMOVED;
852 else if (ie->mask & IN_MOVED_FROM)
853 action = FILE_ACTION_RENAMED_OLD_NAME;
854 else if (ie->mask & IN_MOVED_TO)
855 action = FILE_ACTION_RENAMED_NEW_NAME;
856 else
857 action = FILE_ACTION_MODIFIED;
860 * Work our way up the inode hierarchy
861 * extending the relative path as we go
862 * and notifying all recursive watches.
864 if (!prepend( &path, ie->name ))
865 return;
867 for (i = inode; i; i = i->parent)
869 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
870 if ((filter & dir->filter) && (i==inode || dir->subtree))
871 inotify_do_change_notify( dir, action, ie->cookie, path );
873 if (!i->name || !prepend( &path, i->name ))
874 break;
877 free( path );
879 if (ie->mask & IN_DELETE)
881 i = inode_from_name( inode, ie->name );
882 if (i)
883 free_inode( i );
887 static void inotify_poll_event( struct fd *fd, int event )
889 int r, ofs, unix_fd;
890 char buffer[0x1000];
891 struct inotify_event *ie;
893 unix_fd = get_unix_fd( fd );
894 r = read( unix_fd, buffer, sizeof buffer );
895 if (r < 0)
897 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
898 return;
901 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
903 ie = (struct inotify_event*) &buffer[ofs];
904 ofs += offsetof( struct inotify_event, name[ie->len] );
905 if (ofs > r) break;
906 if (ie->len) inotify_notify_all( ie );
910 static inline struct fd *create_inotify_fd( void )
912 int unix_fd;
914 unix_fd = inotify_init();
915 if (unix_fd<0)
916 return NULL;
917 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
920 static int map_flags( unsigned int filter )
922 unsigned int mask;
924 /* always watch these so we can track subdirectories in recursive watches */
925 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
927 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
928 mask |= IN_ATTRIB;
929 if (filter & FILE_NOTIFY_CHANGE_SIZE)
930 mask |= IN_MODIFY;
931 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
932 mask |= IN_MODIFY;
933 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
934 mask |= IN_MODIFY;
935 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
936 mask |= IN_ATTRIB;
938 return mask;
941 static int inotify_add_dir( char *path, unsigned int filter )
943 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
944 path, map_flags( filter ) );
945 if (wd != -1)
946 set_fd_events( inotify_fd, POLLIN );
947 return wd;
950 static int init_inotify( void )
952 int i;
954 if (inotify_fd)
955 return 1;
957 inotify_fd = create_inotify_fd();
958 if (!inotify_fd)
959 return 0;
961 for (i=0; i<HASH_SIZE; i++)
963 list_init( &inode_hash[i] );
964 list_init( &wd_hash[i] );
967 return 1;
970 static int inotify_adjust_changes( struct dir *dir )
972 unsigned int filter;
973 struct inode *inode;
974 struct stat st;
975 char path[32];
976 int wd, unix_fd;
978 if (!inotify_fd)
979 return 0;
981 unix_fd = get_unix_fd( dir->fd );
983 inode = dir->inode;
984 if (!inode)
986 /* check if this fd is already being watched */
987 if (-1 == fstat( unix_fd, &st ))
988 return 0;
990 inode = get_inode( st.st_dev, st.st_ino );
991 if (!inode)
992 inode = create_inode( st.st_dev, st.st_ino );
993 if (!inode)
994 return 0;
995 list_add_tail( &inode->dirs, &dir->in_entry );
996 dir->inode = inode;
999 filter = filter_from_inode( inode, 0 );
1001 sprintf( path, "/proc/self/fd/%u", unix_fd );
1002 wd = inotify_add_dir( path, filter );
1003 if (wd == -1) return 0;
1005 inode_set_wd( inode, wd );
1007 return 1;
1010 static char *get_basename( const char *link )
1012 char *buffer, *name = NULL;
1013 int r, n = 0x100;
1015 while (1)
1017 buffer = malloc( n );
1018 if (!buffer) return NULL;
1020 r = readlink( link, buffer, n );
1021 if (r < 0)
1022 break;
1024 if (r < n)
1026 name = buffer;
1027 break;
1029 free( buffer );
1030 n *= 2;
1033 if (name)
1035 while (r > 0 && name[ r - 1 ] == '/' )
1036 r--;
1037 name[ r ] = 0;
1039 name = strrchr( name, '/' );
1040 if (name)
1041 name = strdup( &name[1] );
1044 free( buffer );
1045 return name;
1048 static int dir_add_to_existing_notify( struct dir *dir )
1050 struct inode *inode, *parent;
1051 unsigned int filter = 0;
1052 struct stat st, st_new;
1053 char link[35], *name;
1054 int wd, unix_fd;
1056 if (!inotify_fd)
1057 return 0;
1059 unix_fd = get_unix_fd( dir->fd );
1061 /* check if it's in the list of inodes we want to watch */
1062 if (-1 == fstat( unix_fd, &st_new ))
1063 return 0;
1064 inode = find_inode( st_new.st_dev, st_new.st_ino );
1065 if (inode)
1066 return 0;
1068 /* lookup the parent */
1069 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1070 if (-1 == stat( link, &st ))
1071 return 0;
1074 * If there's no parent, stop. We could keep going adding
1075 * ../ to the path until we hit the root of the tree or
1076 * find a recursively watched ancestor.
1077 * Assume it's too expensive to search up the tree for now.
1079 parent = find_inode( st.st_dev, st.st_ino );
1080 if (!parent)
1081 return 0;
1083 if (parent->wd == -1)
1084 return 0;
1086 filter = filter_from_inode( parent, 1 );
1087 if (!filter)
1088 return 0;
1090 sprintf( link, "/proc/self/fd/%u", unix_fd );
1091 name = get_basename( link );
1092 if (!name)
1093 return 0;
1094 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1095 free( name );
1096 if (!inode)
1097 return 0;
1099 /* Couldn't find this inode at the start of the function, must be new */
1100 assert( inode->wd == -1 );
1102 wd = inotify_add_dir( link, filter );
1103 if (wd != -1)
1104 inode_set_wd( inode, wd );
1106 return 1;
1109 #else
1111 static int init_inotify( void )
1113 return 0;
1116 static int inotify_adjust_changes( struct dir *dir )
1118 return 0;
1121 static void free_inode( struct inode *inode )
1123 assert( 0 );
1126 static int dir_add_to_existing_notify( struct dir *dir )
1128 return 0;
1131 #endif /* HAVE_SYS_INOTIFY_H */
1133 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1135 struct dir *dir;
1137 dir = alloc_object( &dir_ops );
1138 if (!dir)
1139 return NULL;
1141 list_init( &dir->change_records );
1142 dir->filter = 0;
1143 dir->notified = 0;
1144 dir->want_data = 0;
1145 dir->inode = NULL;
1146 grab_object( fd );
1147 dir->fd = fd;
1148 dir->mode = mode;
1149 dir->uid = ~(uid_t)0;
1150 dir->client_process = NULL;
1151 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1153 dir_add_to_existing_notify( dir );
1155 return &dir->obj;
1158 /* retrieve (or allocate) the client-side directory cache entry */
1159 DECL_HANDLER(get_directory_cache_entry)
1161 struct dir *dir;
1162 int *free_entries;
1163 data_size_t free_size;
1165 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1167 if (!dir->client_process)
1169 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1170 dir->client_process = (struct process *)grab_object( current->process );
1173 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1174 else set_error( STATUS_SHARING_VIOLATION );
1176 done: /* allow freeing entries even on failure */
1177 free_size = get_reply_max_size();
1178 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1179 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1181 release_object( dir );
1184 /* enable change notifications for a directory */
1185 DECL_HANDLER(read_directory_changes)
1187 struct dir *dir;
1188 struct async *async;
1190 if (!req->filter)
1192 set_error(STATUS_INVALID_PARAMETER);
1193 return;
1196 dir = get_dir_obj( current->process, req->async.handle, 0 );
1197 if (!dir)
1198 return;
1200 /* requests don't timeout */
1201 if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
1202 fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
1204 /* assign it once */
1205 if (!dir->filter)
1207 init_inotify();
1208 insert_change( dir );
1209 dir->filter = req->filter;
1210 dir->subtree = req->subtree;
1211 dir->want_data = req->want_data;
1214 /* if there's already a change in the queue, send it */
1215 if (!list_empty( &dir->change_records ))
1216 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1218 /* setup the real notification */
1219 if (!inotify_adjust_changes( dir ))
1220 dnotify_adjust_changes( dir );
1222 set_error(STATUS_PENDING);
1224 release_object( async );
1225 end:
1226 release_object( dir );
1229 DECL_HANDLER(read_change)
1231 struct change_record *record, *next;
1232 struct dir *dir;
1233 struct list events;
1234 char *data, *event;
1235 int size = 0;
1237 dir = get_dir_obj( current->process, req->handle, 0 );
1238 if (!dir)
1239 return;
1241 list_init( &events );
1242 list_move_tail( &events, &dir->change_records );
1243 release_object( dir );
1245 if (list_empty( &events ))
1247 set_error( STATUS_NO_DATA_DETECTED );
1248 return;
1251 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1253 size += (offsetof(struct filesystem_event, name[record->event.len])
1254 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1257 if (size > get_reply_max_size())
1258 set_error( STATUS_BUFFER_TOO_SMALL );
1259 else if ((data = mem_alloc( size )) != NULL)
1261 event = data;
1262 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1264 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1266 /* FIXME: rename events are sometimes reported as delete/create */
1267 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1269 struct list *elem = list_next( &events, &record->entry );
1270 if (elem)
1271 next = LIST_ENTRY(elem, struct change_record, entry);
1273 if (elem && next->cookie == record->cookie)
1274 next->cookie = 0;
1275 else
1276 record->event.action = FILE_ACTION_REMOVED;
1278 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1279 record->event.action = FILE_ACTION_ADDED;
1281 memcpy( event, &record->event, len );
1282 event += len;
1283 if (len % sizeof(int))
1285 memset( event, 0, sizeof(int) - len % sizeof(int) );
1286 event += sizeof(int) - len % sizeof(int);
1289 set_reply_data_ptr( data, size );
1292 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1294 list_remove( &record->entry );
1295 free( record );