kernelbase: Do not forcefully reset console_flags to 0.
[wine/zf.git] / server / change.c
blobff8c3ad1037e676be3056e4b4a9cc2fad32a242b
1 /*
2 * Server-side change notification management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2006 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <limits.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #ifdef HAVE_POLL_H
36 # include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_INOTIFY_H
39 #include <sys/inotify.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
46 #include "file.h"
47 #include "handle.h"
48 #include "thread.h"
49 #include "request.h"
50 #include "process.h"
51 #include "security.h"
52 #include "winternl.h"
54 /* dnotify support */
56 #ifdef linux
57 #ifndef F_NOTIFY
58 #define F_NOTIFY 1026
59 #define DN_ACCESS 0x00000001 /* File accessed */
60 #define DN_MODIFY 0x00000002 /* File modified */
61 #define DN_CREATE 0x00000004 /* File created */
62 #define DN_DELETE 0x00000008 /* File removed */
63 #define DN_RENAME 0x00000010 /* File renamed */
64 #define DN_ATTRIB 0x00000020 /* File changed attributes */
65 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
66 #endif
67 #endif
69 /* inotify support */
71 struct inode;
73 static void free_inode( struct inode *inode );
75 static struct fd *inotify_fd;
77 struct change_record {
78 struct list entry;
79 unsigned int cookie;
80 struct filesystem_event event;
83 struct dir
85 struct object obj; /* object header */
86 struct fd *fd; /* file descriptor to the directory */
87 mode_t mode; /* file stat.st_mode */
88 uid_t uid; /* file stat.st_uid */
89 struct list entry; /* entry in global change notifications list */
90 unsigned int filter; /* notification filter */
91 volatile int notified; /* SIGIO counter */
92 int want_data; /* return change data */
93 int subtree; /* do we want to watch subdirectories? */
94 struct list change_records; /* data for the change */
95 struct list in_entry; /* entry in the inode dirs list */
96 struct inode *inode; /* inode of the associated directory */
97 struct process *client_process; /* client process that has a cache for this directory */
98 int client_entry; /* entry in client process cache */
101 static struct fd *dir_get_fd( struct object *obj );
102 static struct security_descriptor *dir_get_sd( struct object *obj );
103 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
104 unsigned int set_info );
105 static void dir_dump( struct object *obj, int verbose );
106 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
107 static void dir_destroy( struct object *obj );
109 static const struct object_ops dir_ops =
111 sizeof(struct dir), /* size */
112 &file_type, /* type */
113 dir_dump, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 default_fd_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_signal, /* signal */
119 dir_get_fd, /* get_fd */
120 default_map_access, /* map_access */
121 dir_get_sd, /* get_sd */
122 dir_set_sd, /* set_sd */
123 no_get_full_name, /* get_full_name */
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 /* enter here directly from SIGIO signal handler */
295 void do_change_notify( int unix_fd )
297 struct dir *dir;
299 /* FIXME: this is O(n) ... probably can be improved */
300 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
302 if (get_unix_fd( dir->fd ) != unix_fd) continue;
303 dir->notified = 1;
304 break;
308 /* SIGIO callback, called synchronously with the poll loop */
309 void sigio_callback(void)
311 struct dir *dir;
313 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
315 if (!dir->notified) continue;
316 dir->notified = 0;
317 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
321 static struct fd *dir_get_fd( struct object *obj )
323 struct dir *dir = (struct dir *)obj;
324 assert( obj->ops == &dir_ops );
325 return (struct fd *)grab_object( dir->fd );
328 static int get_dir_unix_fd( struct dir *dir )
330 return get_unix_fd( dir->fd );
333 static struct security_descriptor *dir_get_sd( struct object *obj )
335 struct dir *dir = (struct dir *)obj;
336 int unix_fd;
337 struct stat st;
338 struct security_descriptor *sd;
339 assert( obj->ops == &dir_ops );
341 unix_fd = get_dir_unix_fd( dir );
343 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
344 return obj->sd;
346 /* mode and uid the same? if so, no need to re-generate security descriptor */
347 if (obj->sd &&
348 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
349 (st.st_uid == dir->uid))
350 return obj->sd;
352 sd = mode_to_sd( st.st_mode,
353 security_unix_uid_to_sid( st.st_uid ),
354 token_get_primary_group( current->process->token ));
355 if (!sd) return obj->sd;
357 dir->mode = st.st_mode;
358 dir->uid = st.st_uid;
359 free( obj->sd );
360 obj->sd = sd;
361 return sd;
364 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
365 unsigned int set_info )
367 struct dir *dir = (struct dir *)obj;
368 const SID *owner;
369 struct stat st;
370 mode_t mode;
371 int unix_fd;
373 assert( obj->ops == &dir_ops );
375 unix_fd = get_dir_unix_fd( dir );
377 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
379 if (set_info & OWNER_SECURITY_INFORMATION)
381 owner = sd_get_owner( sd );
382 if (!owner)
384 set_error( STATUS_INVALID_SECURITY_DESCR );
385 return 0;
387 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
389 /* FIXME: get Unix uid and call fchown */
392 else if (obj->sd)
393 owner = sd_get_owner( obj->sd );
394 else
395 owner = token_get_user( current->process->token );
397 if (set_info & DACL_SECURITY_INFORMATION)
399 /* keep the bits that we don't map to access rights in the ACL */
400 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
401 mode |= sd_to_mode( sd, owner );
403 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
405 file_set_error();
406 return 0;
409 return 1;
412 static struct change_record *get_first_change_record( struct dir *dir )
414 struct list *ptr = list_head( &dir->change_records );
415 if (!ptr) return NULL;
416 list_remove( ptr );
417 return LIST_ENTRY( ptr, struct change_record, entry );
420 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
422 struct dir *dir = (struct dir *)obj;
424 if (!fd_close_handle( obj, process, handle )) return 0;
425 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
426 return 1; /* ok to close */
429 static void dir_destroy( struct object *obj )
431 struct change_record *record;
432 struct dir *dir = (struct dir *)obj;
433 assert (obj->ops == &dir_ops);
435 if (dir->filter)
436 remove_change( dir );
438 if (dir->inode)
440 list_remove( &dir->in_entry );
441 free_inode( dir->inode );
444 while ((record = get_first_change_record( dir ))) free( record );
446 release_dir_cache_entry( dir );
447 release_object( dir->fd );
449 if (inotify_fd && list_empty( &change_list ))
451 release_object( inotify_fd );
452 inotify_fd = NULL;
456 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
458 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
461 static int dir_get_poll_events( struct fd *fd )
463 return 0;
466 static enum server_fd_type dir_get_fd_type( struct fd *fd )
468 return FD_TYPE_DIR;
471 #ifdef HAVE_SYS_INOTIFY_H
473 #define HASH_SIZE 31
475 struct inode {
476 struct list ch_entry; /* entry in the children list */
477 struct list children; /* children of this inode */
478 struct inode *parent; /* parent of this inode */
479 struct list dirs; /* directory handles watching this inode */
480 struct list ino_entry; /* entry in the inode hash */
481 struct list wd_entry; /* entry in the watch descriptor hash */
482 dev_t dev; /* device number */
483 ino_t ino; /* device's inode number */
484 int wd; /* inotify's watch descriptor */
485 char *name; /* basename name of the inode */
488 static struct list inode_hash[ HASH_SIZE ];
489 static struct list wd_hash[ HASH_SIZE ];
491 static int inotify_add_dir( char *path, unsigned int filter );
493 static struct inode *inode_from_wd( int wd )
495 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
496 struct inode *inode;
498 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
499 if (inode->wd == wd)
500 return inode;
502 return NULL;
505 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
507 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
510 static struct inode *find_inode( dev_t dev, ino_t ino )
512 struct list *bucket = get_hash_list( dev, ino );
513 struct inode *inode;
515 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
516 if (inode->ino == ino && inode->dev == dev)
517 return inode;
519 return NULL;
522 static struct inode *create_inode( dev_t dev, ino_t ino )
524 struct inode *inode;
526 inode = malloc( sizeof *inode );
527 if (inode)
529 list_init( &inode->children );
530 list_init( &inode->dirs );
531 inode->ino = ino;
532 inode->dev = dev;
533 inode->wd = -1;
534 inode->parent = NULL;
535 inode->name = NULL;
536 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
538 return inode;
541 static struct inode *get_inode( dev_t dev, ino_t ino )
543 struct inode *inode;
545 inode = find_inode( dev, ino );
546 if (inode)
547 return inode;
548 return create_inode( dev, ino );
551 static void inode_set_wd( struct inode *inode, int wd )
553 if (inode->wd != -1)
554 list_remove( &inode->wd_entry );
555 inode->wd = wd;
556 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
559 static void inode_set_name( struct inode *inode, const char *name )
561 free (inode->name);
562 inode->name = name ? strdup( name ) : NULL;
565 static void free_inode( struct inode *inode )
567 int subtree = 0, watches = 0;
568 struct inode *tmp, *next;
569 struct dir *dir;
571 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
573 subtree |= dir->subtree;
574 watches++;
577 if (!subtree && !inode->parent)
579 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
580 struct inode, ch_entry )
582 assert( tmp != inode );
583 assert( tmp->parent == inode );
584 free_inode( tmp );
588 if (watches)
589 return;
591 if (inode->parent)
592 list_remove( &inode->ch_entry );
594 /* disconnect remaining children from the parent */
595 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
597 list_remove( &tmp->ch_entry );
598 tmp->parent = NULL;
601 if (inode->wd != -1)
603 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
604 list_remove( &inode->wd_entry );
606 list_remove( &inode->ino_entry );
608 free( inode->name );
609 free( inode );
612 static struct inode *inode_add( struct inode *parent,
613 dev_t dev, ino_t ino, const char *name )
615 struct inode *inode;
617 inode = get_inode( dev, ino );
618 if (!inode)
619 return NULL;
621 if (!inode->parent)
623 list_add_tail( &parent->children, &inode->ch_entry );
624 inode->parent = parent;
625 assert( inode != parent );
627 inode_set_name( inode, name );
629 return inode;
632 static struct inode *inode_from_name( struct inode *inode, const char *name )
634 struct inode *i;
636 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
637 if (i->name && !strcmp( i->name, name ))
638 return i;
639 return NULL;
642 static int inotify_get_poll_events( struct fd *fd );
643 static void inotify_poll_event( struct fd *fd, int event );
645 static const struct fd_ops inotify_fd_ops =
647 inotify_get_poll_events, /* get_poll_events */
648 inotify_poll_event, /* poll_event */
649 NULL, /* flush */
650 NULL, /* get_fd_type */
651 NULL, /* ioctl */
652 NULL, /* queue_async */
653 NULL /* reselect_async */
656 static int inotify_get_poll_events( struct fd *fd )
658 return POLLIN;
661 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
662 unsigned int cookie, const char *relpath )
664 struct change_record *record;
666 assert( dir->obj.ops == &dir_ops );
668 if (dir->want_data)
670 size_t len = strlen(relpath);
671 record = malloc( offsetof(struct change_record, event.name[len]) );
672 if (!record)
673 return;
675 record->cookie = cookie;
676 record->event.action = action;
677 memcpy( record->event.name, relpath, len );
678 record->event.len = len;
680 list_add_tail( &dir->change_records, &record->entry );
683 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
686 static unsigned int filter_from_event( struct inotify_event *ie )
688 unsigned int filter = 0;
690 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
691 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
692 if (ie->mask & IN_MODIFY)
693 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
694 if (ie->mask & IN_ATTRIB)
695 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
696 if (ie->mask & IN_CREATE)
697 filter |= FILE_NOTIFY_CHANGE_CREATION;
699 if (ie->mask & IN_ISDIR)
700 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
701 else
702 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
704 return filter;
707 /* scan up the parent directories for watches */
708 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
710 unsigned int filter = 0;
711 struct dir *dir;
713 /* combine filters from parents watching subtrees */
714 while (inode)
716 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
717 if (dir->subtree || !is_parent)
718 filter |= dir->filter;
719 is_parent = 1;
720 inode = inode->parent;
723 return filter;
726 static char *inode_get_path( struct inode *inode, int sz )
728 struct list *head;
729 char *path;
730 int len;
732 if (!inode)
733 return NULL;
735 head = list_head( &inode->dirs );
736 if (head)
738 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
739 path = malloc ( 32 + sz );
740 if (path)
741 sprintf( path, "/proc/self/fd/%u/", unix_fd );
742 return path;
745 if (!inode->name)
746 return NULL;
748 len = strlen( inode->name );
749 path = inode_get_path( inode->parent, sz + len + 1 );
750 if (!path)
751 return NULL;
753 strcat( path, inode->name );
754 strcat( path, "/" );
756 return path;
759 static void inode_check_dir( struct inode *parent, const char *name )
761 char *path;
762 unsigned int filter;
763 struct inode *inode;
764 struct stat st;
765 int wd = -1;
767 path = inode_get_path( parent, strlen(name) );
768 if (!path)
769 return;
771 strcat( path, name );
773 if (stat( path, &st ) < 0)
774 goto end;
776 filter = filter_from_inode( parent, 1 );
777 if (!filter)
778 goto end;
780 inode = inode_add( parent, st.st_dev, st.st_ino, name );
781 if (!inode || inode->wd != -1)
782 goto end;
784 wd = inotify_add_dir( path, filter );
785 if (wd != -1)
786 inode_set_wd( inode, wd );
787 else
788 free_inode( inode );
790 end:
791 free( path );
794 static int prepend( char **path, const char *segment )
796 int extra;
797 char *p;
799 extra = strlen( segment ) + 1;
800 if (*path)
802 int len = strlen( *path ) + 1;
803 p = realloc( *path, len + extra );
804 if (!p) return 0;
805 memmove( &p[ extra ], p, len );
806 p[ extra - 1 ] = '/';
807 memcpy( p, segment, extra - 1 );
809 else
811 p = malloc( extra );
812 if (!p) return 0;
813 memcpy( p, segment, extra );
816 *path = p;
818 return 1;
821 static void inotify_notify_all( struct inotify_event *ie )
823 unsigned int filter, action;
824 struct inode *inode, *i;
825 char *path = NULL;
826 struct dir *dir;
828 inode = inode_from_wd( ie->wd );
829 if (!inode)
831 fprintf( stderr, "no inode matches %d\n", ie->wd);
832 return;
835 filter = filter_from_event( ie );
837 if (ie->mask & IN_CREATE)
839 if (ie->mask & IN_ISDIR)
840 inode_check_dir( inode, ie->name );
842 action = FILE_ACTION_ADDED;
844 else if (ie->mask & IN_DELETE)
845 action = FILE_ACTION_REMOVED;
846 else if (ie->mask & IN_MOVED_FROM)
847 action = FILE_ACTION_RENAMED_OLD_NAME;
848 else if (ie->mask & IN_MOVED_TO)
849 action = FILE_ACTION_RENAMED_NEW_NAME;
850 else
851 action = FILE_ACTION_MODIFIED;
854 * Work our way up the inode hierarchy
855 * extending the relative path as we go
856 * and notifying all recursive watches.
858 if (!prepend( &path, ie->name ))
859 return;
861 for (i = inode; i; i = i->parent)
863 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
864 if ((filter & dir->filter) && (i==inode || dir->subtree))
865 inotify_do_change_notify( dir, action, ie->cookie, path );
867 if (!i->name || !prepend( &path, i->name ))
868 break;
871 free( path );
873 if (ie->mask & IN_DELETE)
875 i = inode_from_name( inode, ie->name );
876 if (i)
877 free_inode( i );
881 static void inotify_poll_event( struct fd *fd, int event )
883 int r, ofs, unix_fd;
884 char buffer[0x1000];
885 struct inotify_event *ie;
887 unix_fd = get_unix_fd( fd );
888 r = read( unix_fd, buffer, sizeof buffer );
889 if (r < 0)
891 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
892 return;
895 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
897 ie = (struct inotify_event*) &buffer[ofs];
898 ofs += offsetof( struct inotify_event, name[ie->len] );
899 if (ofs > r) break;
900 if (ie->len) inotify_notify_all( ie );
904 static inline struct fd *create_inotify_fd( void )
906 int unix_fd;
908 unix_fd = inotify_init();
909 if (unix_fd<0)
910 return NULL;
911 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
914 static int map_flags( unsigned int filter )
916 unsigned int mask;
918 /* always watch these so we can track subdirectories in recursive watches */
919 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
921 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
922 mask |= IN_ATTRIB;
923 if (filter & FILE_NOTIFY_CHANGE_SIZE)
924 mask |= IN_MODIFY;
925 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
926 mask |= IN_MODIFY;
927 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
928 mask |= IN_MODIFY;
929 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
930 mask |= IN_ATTRIB;
932 return mask;
935 static int inotify_add_dir( char *path, unsigned int filter )
937 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
938 path, map_flags( filter ) );
939 if (wd != -1)
940 set_fd_events( inotify_fd, POLLIN );
941 return wd;
944 static int init_inotify( void )
946 int i;
948 if (inotify_fd)
949 return 1;
951 inotify_fd = create_inotify_fd();
952 if (!inotify_fd)
953 return 0;
955 for (i=0; i<HASH_SIZE; i++)
957 list_init( &inode_hash[i] );
958 list_init( &wd_hash[i] );
961 return 1;
964 static int inotify_adjust_changes( struct dir *dir )
966 unsigned int filter;
967 struct inode *inode;
968 struct stat st;
969 char path[32];
970 int wd, unix_fd;
972 if (!inotify_fd)
973 return 0;
975 unix_fd = get_unix_fd( dir->fd );
977 inode = dir->inode;
978 if (!inode)
980 /* check if this fd is already being watched */
981 if (-1 == fstat( unix_fd, &st ))
982 return 0;
984 inode = get_inode( st.st_dev, st.st_ino );
985 if (!inode)
986 inode = create_inode( st.st_dev, st.st_ino );
987 if (!inode)
988 return 0;
989 list_add_tail( &inode->dirs, &dir->in_entry );
990 dir->inode = inode;
993 filter = filter_from_inode( inode, 0 );
995 sprintf( path, "/proc/self/fd/%u", unix_fd );
996 wd = inotify_add_dir( path, filter );
997 if (wd == -1) return 0;
999 inode_set_wd( inode, wd );
1001 return 1;
1004 static char *get_basename( const char *link )
1006 char *buffer, *name = NULL;
1007 int r, n = 0x100;
1009 while (1)
1011 buffer = malloc( n );
1012 if (!buffer) return NULL;
1014 r = readlink( link, buffer, n );
1015 if (r < 0)
1016 break;
1018 if (r < n)
1020 name = buffer;
1021 break;
1023 free( buffer );
1024 n *= 2;
1027 if (name)
1029 while (r > 0 && name[ r - 1 ] == '/' )
1030 r--;
1031 name[ r ] = 0;
1033 name = strrchr( name, '/' );
1034 if (name)
1035 name = strdup( &name[1] );
1038 free( buffer );
1039 return name;
1042 static int dir_add_to_existing_notify( struct dir *dir )
1044 struct inode *inode, *parent;
1045 unsigned int filter = 0;
1046 struct stat st, st_new;
1047 char link[35], *name;
1048 int wd, unix_fd;
1050 if (!inotify_fd)
1051 return 0;
1053 unix_fd = get_unix_fd( dir->fd );
1055 /* check if it's in the list of inodes we want to watch */
1056 if (-1 == fstat( unix_fd, &st_new ))
1057 return 0;
1058 inode = find_inode( st_new.st_dev, st_new.st_ino );
1059 if (inode)
1060 return 0;
1062 /* lookup the parent */
1063 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1064 if (-1 == stat( link, &st ))
1065 return 0;
1068 * If there's no parent, stop. We could keep going adding
1069 * ../ to the path until we hit the root of the tree or
1070 * find a recursively watched ancestor.
1071 * Assume it's too expensive to search up the tree for now.
1073 parent = find_inode( st.st_dev, st.st_ino );
1074 if (!parent)
1075 return 0;
1077 if (parent->wd == -1)
1078 return 0;
1080 filter = filter_from_inode( parent, 1 );
1081 if (!filter)
1082 return 0;
1084 sprintf( link, "/proc/self/fd/%u", unix_fd );
1085 name = get_basename( link );
1086 if (!name)
1087 return 0;
1088 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1089 free( name );
1090 if (!inode)
1091 return 0;
1093 /* Couldn't find this inode at the start of the function, must be new */
1094 assert( inode->wd == -1 );
1096 wd = inotify_add_dir( link, filter );
1097 if (wd != -1)
1098 inode_set_wd( inode, wd );
1100 return 1;
1103 #else
1105 static int init_inotify( void )
1107 return 0;
1110 static int inotify_adjust_changes( struct dir *dir )
1112 return 0;
1115 static void free_inode( struct inode *inode )
1117 assert( 0 );
1120 static int dir_add_to_existing_notify( struct dir *dir )
1122 return 0;
1125 #endif /* HAVE_SYS_INOTIFY_H */
1127 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1129 struct dir *dir;
1131 dir = alloc_object( &dir_ops );
1132 if (!dir)
1133 return NULL;
1135 list_init( &dir->change_records );
1136 dir->filter = 0;
1137 dir->notified = 0;
1138 dir->want_data = 0;
1139 dir->inode = NULL;
1140 grab_object( fd );
1141 dir->fd = fd;
1142 dir->mode = mode;
1143 dir->uid = ~(uid_t)0;
1144 dir->client_process = NULL;
1145 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1147 dir_add_to_existing_notify( dir );
1149 return &dir->obj;
1152 /* retrieve (or allocate) the client-side directory cache entry */
1153 DECL_HANDLER(get_directory_cache_entry)
1155 struct dir *dir;
1156 int *free_entries;
1157 data_size_t free_size;
1159 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1161 if (!dir->client_process)
1163 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1164 dir->client_process = (struct process *)grab_object( current->process );
1167 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1168 else set_error( STATUS_SHARING_VIOLATION );
1170 done: /* allow freeing entries even on failure */
1171 free_size = get_reply_max_size();
1172 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1173 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1175 release_object( dir );
1178 /* enable change notifications for a directory */
1179 DECL_HANDLER(read_directory_changes)
1181 struct dir *dir;
1182 struct async *async;
1184 if (!req->filter)
1186 set_error(STATUS_INVALID_PARAMETER);
1187 return;
1190 dir = get_dir_obj( current->process, req->async.handle, 0 );
1191 if (!dir)
1192 return;
1194 /* requests don't timeout */
1195 if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
1196 fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
1198 /* assign it once */
1199 if (!dir->filter)
1201 init_inotify();
1202 insert_change( dir );
1203 dir->filter = req->filter;
1204 dir->subtree = req->subtree;
1205 dir->want_data = req->want_data;
1208 /* if there's already a change in the queue, send it */
1209 if (!list_empty( &dir->change_records ))
1210 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1212 /* setup the real notification */
1213 if (!inotify_adjust_changes( dir ))
1214 dnotify_adjust_changes( dir );
1216 set_error(STATUS_PENDING);
1218 release_object( async );
1219 end:
1220 release_object( dir );
1223 DECL_HANDLER(read_change)
1225 struct change_record *record, *next;
1226 struct dir *dir;
1227 struct list events;
1228 char *data, *event;
1229 int size = 0;
1231 dir = get_dir_obj( current->process, req->handle, 0 );
1232 if (!dir)
1233 return;
1235 list_init( &events );
1236 list_move_tail( &events, &dir->change_records );
1237 release_object( dir );
1239 if (list_empty( &events ))
1241 set_error( STATUS_NO_DATA_DETECTED );
1242 return;
1245 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1247 size += (offsetof(struct filesystem_event, name[record->event.len])
1248 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1251 if (size > get_reply_max_size())
1252 set_error( STATUS_BUFFER_TOO_SMALL );
1253 else if ((data = mem_alloc( size )) != NULL)
1255 event = data;
1256 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1258 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1260 /* FIXME: rename events are sometimes reported as delete/create */
1261 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1263 struct list *elem = list_next( &events, &record->entry );
1264 if (elem)
1265 next = LIST_ENTRY(elem, struct change_record, entry);
1267 if (elem && next->cookie == record->cookie)
1268 next->cookie = 0;
1269 else
1270 record->event.action = FILE_ACTION_REMOVED;
1272 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1273 record->event.action = FILE_ACTION_ADDED;
1275 memcpy( event, &record->event, len );
1276 event += len;
1277 if (len % sizeof(int))
1279 memset( event, 0, sizeof(int) - len % sizeof(int) );
1280 event += sizeof(int) - len % sizeof(int);
1283 set_reply_data_ptr( data, size );
1286 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1288 list_remove( &record->entry );
1289 free( record );