gdi32: Update the external keys after all fonts are loaded.
[wine.git] / server / change.c
blob8e4a272bc8b3697aad3099bd40907c0eeb86f00a
1 /*
2 * Server-side change notification management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2006 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <limits.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #ifdef HAVE_POLL_H
36 # include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_INOTIFY_H
39 #include <sys/inotify.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
46 #include "file.h"
47 #include "handle.h"
48 #include "thread.h"
49 #include "request.h"
50 #include "process.h"
51 #include "security.h"
52 #include "winternl.h"
54 /* dnotify support */
56 #ifdef linux
57 #ifndef F_NOTIFY
58 #define F_NOTIFY 1026
59 #define DN_ACCESS 0x00000001 /* File accessed */
60 #define DN_MODIFY 0x00000002 /* File modified */
61 #define DN_CREATE 0x00000004 /* File created */
62 #define DN_DELETE 0x00000008 /* File removed */
63 #define DN_RENAME 0x00000010 /* File renamed */
64 #define DN_ATTRIB 0x00000020 /* File changed attributes */
65 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
66 #endif
67 #endif
69 /* inotify support */
71 struct inode;
73 static void free_inode( struct inode *inode );
75 static struct fd *inotify_fd;
77 struct change_record {
78 struct list entry;
79 unsigned int cookie;
80 struct filesystem_event event;
83 struct dir
85 struct object obj; /* object header */
86 struct fd *fd; /* file descriptor to the directory */
87 mode_t mode; /* file stat.st_mode */
88 uid_t uid; /* file stat.st_uid */
89 struct list entry; /* entry in global change notifications list */
90 unsigned int filter; /* notification filter */
91 volatile int notified; /* SIGIO counter */
92 int want_data; /* return change data */
93 int subtree; /* do we want to watch subdirectories? */
94 struct list change_records; /* data for the change */
95 struct list in_entry; /* entry in the inode dirs list */
96 struct inode *inode; /* inode of the associated directory */
97 struct process *client_process; /* client process that has a cache for this directory */
98 int client_entry; /* entry in client process cache */
101 static struct fd *dir_get_fd( struct object *obj );
102 static struct security_descriptor *dir_get_sd( struct object *obj );
103 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
104 unsigned int set_info );
105 static void dir_dump( struct object *obj, int verbose );
106 static struct object_type *dir_get_type( struct object *obj );
107 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
108 static void dir_destroy( struct object *obj );
110 static const struct object_ops dir_ops =
112 sizeof(struct dir), /* size */
113 dir_dump, /* dump */
114 dir_get_type, /* get_type */
115 add_queue, /* add_queue */
116 remove_queue, /* remove_queue */
117 default_fd_signaled, /* signaled */
118 no_satisfied, /* satisfied */
119 no_signal, /* signal */
120 dir_get_fd, /* get_fd */
121 default_fd_map_access, /* map_access */
122 dir_get_sd, /* get_sd */
123 dir_set_sd, /* set_sd */
124 no_get_full_name, /* get_full_name */
125 no_lookup_name, /* lookup_name */
126 no_link_name, /* link_name */
127 NULL, /* unlink_name */
128 no_open_file, /* open_file */
129 no_kernel_obj_list, /* get_kernel_obj_list */
130 dir_close_handle, /* close_handle */
131 dir_destroy /* destroy */
134 static int dir_get_poll_events( struct fd *fd );
135 static enum server_fd_type dir_get_fd_type( struct fd *fd );
137 static const struct fd_ops dir_fd_ops =
139 dir_get_poll_events, /* get_poll_events */
140 default_poll_event, /* poll_event */
141 dir_get_fd_type, /* get_fd_type */
142 no_fd_read, /* read */
143 no_fd_write, /* write */
144 no_fd_flush, /* flush */
145 default_fd_get_file_info, /* get_file_info */
146 no_fd_get_volume_info, /* get_volume_info */
147 default_fd_ioctl, /* ioctl */
148 default_fd_queue_async, /* queue_async */
149 default_fd_reselect_async /* reselect_async */
152 static struct list change_list = LIST_INIT(change_list);
154 /* per-process structure to keep track of cache entries on the client size */
155 struct dir_cache
157 unsigned int size;
158 unsigned int count;
159 unsigned char state[1];
162 enum dir_cache_state
164 DIR_CACHE_STATE_FREE,
165 DIR_CACHE_STATE_INUSE,
166 DIR_CACHE_STATE_RELEASED
169 /* return an array of cache entries that can be freed on the client side */
170 static int *get_free_dir_cache_entries( struct process *process, data_size_t *size )
172 int *ret;
173 struct dir_cache *cache = process->dir_cache;
174 unsigned int i, j, count;
176 if (!cache) return NULL;
177 for (i = count = 0; i < cache->count && count < *size / sizeof(*ret); i++)
178 if (cache->state[i] == DIR_CACHE_STATE_RELEASED) count++;
179 if (!count) return NULL;
181 if ((ret = malloc( count * sizeof(*ret) )))
183 for (i = j = 0; j < count; i++)
185 if (cache->state[i] != DIR_CACHE_STATE_RELEASED) continue;
186 cache->state[i] = DIR_CACHE_STATE_FREE;
187 ret[j++] = i;
189 *size = count * sizeof(*ret);
191 return ret;
194 /* allocate a new client-side directory cache entry */
195 static int alloc_dir_cache_entry( struct dir *dir, struct process *process )
197 unsigned int i = 0;
198 struct dir_cache *cache = process->dir_cache;
200 if (cache)
201 for (i = 0; i < cache->count; i++)
202 if (cache->state[i] == DIR_CACHE_STATE_FREE) goto found;
204 if (!cache || cache->count == cache->size)
206 unsigned int size = cache ? cache->size * 2 : 256;
207 if (!(cache = realloc( cache, offsetof( struct dir_cache, state[size] ))))
209 set_error( STATUS_NO_MEMORY );
210 return -1;
212 process->dir_cache = cache;
213 cache->size = size;
215 cache->count = i + 1;
217 found:
218 cache->state[i] = DIR_CACHE_STATE_INUSE;
219 return i;
222 /* release a directory cache entry; it will be freed on the client side on the next cache request */
223 static void release_dir_cache_entry( struct dir *dir )
225 struct dir_cache *cache;
227 if (!dir->client_process) return;
228 cache = dir->client_process->dir_cache;
229 cache->state[dir->client_entry] = DIR_CACHE_STATE_RELEASED;
230 release_object( dir->client_process );
231 dir->client_process = NULL;
234 static void dnotify_adjust_changes( struct dir *dir )
236 #if defined(F_SETSIG) && defined(F_NOTIFY)
237 int fd = get_unix_fd( dir->fd );
238 unsigned int filter = dir->filter;
239 unsigned int val;
240 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
241 return;
243 val = DN_MULTISHOT;
244 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
245 val |= DN_RENAME | DN_DELETE | DN_CREATE;
246 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
247 val |= DN_RENAME | DN_DELETE | DN_CREATE;
248 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
249 val |= DN_ATTRIB;
250 if (filter & FILE_NOTIFY_CHANGE_SIZE)
251 val |= DN_MODIFY;
252 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
253 val |= DN_MODIFY;
254 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
255 val |= DN_ACCESS;
256 if (filter & FILE_NOTIFY_CHANGE_CREATION)
257 val |= DN_CREATE;
258 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
259 val |= DN_ATTRIB;
260 fcntl( fd, F_NOTIFY, val );
261 #endif
264 /* insert change in the global list */
265 static inline void insert_change( struct dir *dir )
267 sigset_t sigset;
269 sigemptyset( &sigset );
270 sigaddset( &sigset, SIGIO );
271 sigprocmask( SIG_BLOCK, &sigset, NULL );
272 list_add_head( &change_list, &dir->entry );
273 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
276 /* remove change from the global list */
277 static inline void remove_change( struct dir *dir )
279 sigset_t sigset;
281 sigemptyset( &sigset );
282 sigaddset( &sigset, SIGIO );
283 sigprocmask( SIG_BLOCK, &sigset, NULL );
284 list_remove( &dir->entry );
285 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
288 static void dir_dump( struct object *obj, int verbose )
290 struct dir *dir = (struct dir *)obj;
291 assert( obj->ops == &dir_ops );
292 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
295 static struct object_type *dir_get_type( struct object *obj )
297 static const WCHAR name[] = {'F','i','l','e'};
298 static const struct unicode_str str = { name, sizeof(name) };
299 return get_object_type( &str );
302 /* enter here directly from SIGIO signal handler */
303 void do_change_notify( int unix_fd )
305 struct dir *dir;
307 /* FIXME: this is O(n) ... probably can be improved */
308 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
310 if (get_unix_fd( dir->fd ) != unix_fd) continue;
311 dir->notified = 1;
312 break;
316 /* SIGIO callback, called synchronously with the poll loop */
317 void sigio_callback(void)
319 struct dir *dir;
321 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
323 if (!dir->notified) continue;
324 dir->notified = 0;
325 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
329 static struct fd *dir_get_fd( struct object *obj )
331 struct dir *dir = (struct dir *)obj;
332 assert( obj->ops == &dir_ops );
333 return (struct fd *)grab_object( dir->fd );
336 static int get_dir_unix_fd( struct dir *dir )
338 return get_unix_fd( dir->fd );
341 static struct security_descriptor *dir_get_sd( struct object *obj )
343 struct dir *dir = (struct dir *)obj;
344 int unix_fd;
345 struct stat st;
346 struct security_descriptor *sd;
347 assert( obj->ops == &dir_ops );
349 unix_fd = get_dir_unix_fd( dir );
351 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
352 return obj->sd;
354 /* mode and uid the same? if so, no need to re-generate security descriptor */
355 if (obj->sd &&
356 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
357 (st.st_uid == dir->uid))
358 return obj->sd;
360 sd = mode_to_sd( st.st_mode,
361 security_unix_uid_to_sid( st.st_uid ),
362 token_get_primary_group( current->process->token ));
363 if (!sd) return obj->sd;
365 dir->mode = st.st_mode;
366 dir->uid = st.st_uid;
367 free( obj->sd );
368 obj->sd = sd;
369 return sd;
372 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
373 unsigned int set_info )
375 struct dir *dir = (struct dir *)obj;
376 const SID *owner;
377 struct stat st;
378 mode_t mode;
379 int unix_fd;
381 assert( obj->ops == &dir_ops );
383 unix_fd = get_dir_unix_fd( dir );
385 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
387 if (set_info & OWNER_SECURITY_INFORMATION)
389 owner = sd_get_owner( sd );
390 if (!owner)
392 set_error( STATUS_INVALID_SECURITY_DESCR );
393 return 0;
395 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
397 /* FIXME: get Unix uid and call fchown */
400 else if (obj->sd)
401 owner = sd_get_owner( obj->sd );
402 else
403 owner = token_get_user( current->process->token );
405 if (set_info & DACL_SECURITY_INFORMATION)
407 /* keep the bits that we don't map to access rights in the ACL */
408 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
409 mode |= sd_to_mode( sd, owner );
411 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
413 file_set_error();
414 return 0;
417 return 1;
420 static struct change_record *get_first_change_record( struct dir *dir )
422 struct list *ptr = list_head( &dir->change_records );
423 if (!ptr) return NULL;
424 list_remove( ptr );
425 return LIST_ENTRY( ptr, struct change_record, entry );
428 static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
430 struct dir *dir = (struct dir *)obj;
432 if (!fd_close_handle( obj, process, handle )) return 0;
433 if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
434 return 1; /* ok to close */
437 static void dir_destroy( struct object *obj )
439 struct change_record *record;
440 struct dir *dir = (struct dir *)obj;
441 assert (obj->ops == &dir_ops);
443 if (dir->filter)
444 remove_change( dir );
446 if (dir->inode)
448 list_remove( &dir->in_entry );
449 free_inode( dir->inode );
452 while ((record = get_first_change_record( dir ))) free( record );
454 release_dir_cache_entry( dir );
455 release_object( dir->fd );
457 if (inotify_fd && list_empty( &change_list ))
459 release_object( inotify_fd );
460 inotify_fd = NULL;
464 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
466 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
469 static int dir_get_poll_events( struct fd *fd )
471 return 0;
474 static enum server_fd_type dir_get_fd_type( struct fd *fd )
476 return FD_TYPE_DIR;
479 #ifdef HAVE_SYS_INOTIFY_H
481 #define HASH_SIZE 31
483 struct inode {
484 struct list ch_entry; /* entry in the children list */
485 struct list children; /* children of this inode */
486 struct inode *parent; /* parent of this inode */
487 struct list dirs; /* directory handles watching this inode */
488 struct list ino_entry; /* entry in the inode hash */
489 struct list wd_entry; /* entry in the watch descriptor hash */
490 dev_t dev; /* device number */
491 ino_t ino; /* device's inode number */
492 int wd; /* inotify's watch descriptor */
493 char *name; /* basename name of the inode */
496 static struct list inode_hash[ HASH_SIZE ];
497 static struct list wd_hash[ HASH_SIZE ];
499 static int inotify_add_dir( char *path, unsigned int filter );
501 static struct inode *inode_from_wd( int wd )
503 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
504 struct inode *inode;
506 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
507 if (inode->wd == wd)
508 return inode;
510 return NULL;
513 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
515 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
518 static struct inode *find_inode( dev_t dev, ino_t ino )
520 struct list *bucket = get_hash_list( dev, ino );
521 struct inode *inode;
523 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
524 if (inode->ino == ino && inode->dev == dev)
525 return inode;
527 return NULL;
530 static struct inode *create_inode( dev_t dev, ino_t ino )
532 struct inode *inode;
534 inode = malloc( sizeof *inode );
535 if (inode)
537 list_init( &inode->children );
538 list_init( &inode->dirs );
539 inode->ino = ino;
540 inode->dev = dev;
541 inode->wd = -1;
542 inode->parent = NULL;
543 inode->name = NULL;
544 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
546 return inode;
549 static struct inode *get_inode( dev_t dev, ino_t ino )
551 struct inode *inode;
553 inode = find_inode( dev, ino );
554 if (inode)
555 return inode;
556 return create_inode( dev, ino );
559 static void inode_set_wd( struct inode *inode, int wd )
561 if (inode->wd != -1)
562 list_remove( &inode->wd_entry );
563 inode->wd = wd;
564 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
567 static void inode_set_name( struct inode *inode, const char *name )
569 free (inode->name);
570 inode->name = name ? strdup( name ) : NULL;
573 static void free_inode( struct inode *inode )
575 int subtree = 0, watches = 0;
576 struct inode *tmp, *next;
577 struct dir *dir;
579 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
581 subtree |= dir->subtree;
582 watches++;
585 if (!subtree && !inode->parent)
587 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
588 struct inode, ch_entry )
590 assert( tmp != inode );
591 assert( tmp->parent == inode );
592 free_inode( tmp );
596 if (watches)
597 return;
599 if (inode->parent)
600 list_remove( &inode->ch_entry );
602 /* disconnect remaining children from the parent */
603 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
605 list_remove( &tmp->ch_entry );
606 tmp->parent = NULL;
609 if (inode->wd != -1)
611 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
612 list_remove( &inode->wd_entry );
614 list_remove( &inode->ino_entry );
616 free( inode->name );
617 free( inode );
620 static struct inode *inode_add( struct inode *parent,
621 dev_t dev, ino_t ino, const char *name )
623 struct inode *inode;
625 inode = get_inode( dev, ino );
626 if (!inode)
627 return NULL;
629 if (!inode->parent)
631 list_add_tail( &parent->children, &inode->ch_entry );
632 inode->parent = parent;
633 assert( inode != parent );
635 inode_set_name( inode, name );
637 return inode;
640 static struct inode *inode_from_name( struct inode *inode, const char *name )
642 struct inode *i;
644 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
645 if (i->name && !strcmp( i->name, name ))
646 return i;
647 return NULL;
650 static int inotify_get_poll_events( struct fd *fd );
651 static void inotify_poll_event( struct fd *fd, int event );
653 static const struct fd_ops inotify_fd_ops =
655 inotify_get_poll_events, /* get_poll_events */
656 inotify_poll_event, /* poll_event */
657 NULL, /* flush */
658 NULL, /* get_fd_type */
659 NULL, /* ioctl */
660 NULL, /* queue_async */
661 NULL /* reselect_async */
664 static int inotify_get_poll_events( struct fd *fd )
666 return POLLIN;
669 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
670 unsigned int cookie, const char *relpath )
672 struct change_record *record;
674 assert( dir->obj.ops == &dir_ops );
676 if (dir->want_data)
678 size_t len = strlen(relpath);
679 record = malloc( offsetof(struct change_record, event.name[len]) );
680 if (!record)
681 return;
683 record->cookie = cookie;
684 record->event.action = action;
685 memcpy( record->event.name, relpath, len );
686 record->event.len = len;
688 list_add_tail( &dir->change_records, &record->entry );
691 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
694 static unsigned int filter_from_event( struct inotify_event *ie )
696 unsigned int filter = 0;
698 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
699 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
700 if (ie->mask & IN_MODIFY)
701 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
702 if (ie->mask & IN_ATTRIB)
703 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
704 if (ie->mask & IN_CREATE)
705 filter |= FILE_NOTIFY_CHANGE_CREATION;
707 if (ie->mask & IN_ISDIR)
708 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
709 else
710 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
712 return filter;
715 /* scan up the parent directories for watches */
716 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
718 unsigned int filter = 0;
719 struct dir *dir;
721 /* combine filters from parents watching subtrees */
722 while (inode)
724 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
725 if (dir->subtree || !is_parent)
726 filter |= dir->filter;
727 is_parent = 1;
728 inode = inode->parent;
731 return filter;
734 static char *inode_get_path( struct inode *inode, int sz )
736 struct list *head;
737 char *path;
738 int len;
740 if (!inode)
741 return NULL;
743 head = list_head( &inode->dirs );
744 if (head)
746 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
747 path = malloc ( 32 + sz );
748 if (path)
749 sprintf( path, "/proc/self/fd/%u/", unix_fd );
750 return path;
753 if (!inode->name)
754 return NULL;
756 len = strlen( inode->name );
757 path = inode_get_path( inode->parent, sz + len + 1 );
758 if (!path)
759 return NULL;
761 strcat( path, inode->name );
762 strcat( path, "/" );
764 return path;
767 static void inode_check_dir( struct inode *parent, const char *name )
769 char *path;
770 unsigned int filter;
771 struct inode *inode;
772 struct stat st;
773 int wd = -1;
775 path = inode_get_path( parent, strlen(name) );
776 if (!path)
777 return;
779 strcat( path, name );
781 if (stat( path, &st ) < 0)
782 goto end;
784 filter = filter_from_inode( parent, 1 );
785 if (!filter)
786 goto end;
788 inode = inode_add( parent, st.st_dev, st.st_ino, name );
789 if (!inode || inode->wd != -1)
790 goto end;
792 wd = inotify_add_dir( path, filter );
793 if (wd != -1)
794 inode_set_wd( inode, wd );
795 else
796 free_inode( inode );
798 end:
799 free( path );
802 static int prepend( char **path, const char *segment )
804 int extra;
805 char *p;
807 extra = strlen( segment ) + 1;
808 if (*path)
810 int len = strlen( *path ) + 1;
811 p = realloc( *path, len + extra );
812 if (!p) return 0;
813 memmove( &p[ extra ], p, len );
814 p[ extra - 1 ] = '/';
815 memcpy( p, segment, extra - 1 );
817 else
819 p = malloc( extra );
820 if (!p) return 0;
821 memcpy( p, segment, extra );
824 *path = p;
826 return 1;
829 static void inotify_notify_all( struct inotify_event *ie )
831 unsigned int filter, action;
832 struct inode *inode, *i;
833 char *path = NULL;
834 struct dir *dir;
836 inode = inode_from_wd( ie->wd );
837 if (!inode)
839 fprintf( stderr, "no inode matches %d\n", ie->wd);
840 return;
843 filter = filter_from_event( ie );
845 if (ie->mask & IN_CREATE)
847 if (ie->mask & IN_ISDIR)
848 inode_check_dir( inode, ie->name );
850 action = FILE_ACTION_ADDED;
852 else if (ie->mask & IN_DELETE)
853 action = FILE_ACTION_REMOVED;
854 else if (ie->mask & IN_MOVED_FROM)
855 action = FILE_ACTION_RENAMED_OLD_NAME;
856 else if (ie->mask & IN_MOVED_TO)
857 action = FILE_ACTION_RENAMED_NEW_NAME;
858 else
859 action = FILE_ACTION_MODIFIED;
862 * Work our way up the inode hierarchy
863 * extending the relative path as we go
864 * and notifying all recursive watches.
866 if (!prepend( &path, ie->name ))
867 return;
869 for (i = inode; i; i = i->parent)
871 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
872 if ((filter & dir->filter) && (i==inode || dir->subtree))
873 inotify_do_change_notify( dir, action, ie->cookie, path );
875 if (!i->name || !prepend( &path, i->name ))
876 break;
879 free( path );
881 if (ie->mask & IN_DELETE)
883 i = inode_from_name( inode, ie->name );
884 if (i)
885 free_inode( i );
889 static void inotify_poll_event( struct fd *fd, int event )
891 int r, ofs, unix_fd;
892 char buffer[0x1000];
893 struct inotify_event *ie;
895 unix_fd = get_unix_fd( fd );
896 r = read( unix_fd, buffer, sizeof buffer );
897 if (r < 0)
899 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
900 return;
903 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
905 ie = (struct inotify_event*) &buffer[ofs];
906 ofs += offsetof( struct inotify_event, name[ie->len] );
907 if (ofs > r) break;
908 if (ie->len) inotify_notify_all( ie );
912 static inline struct fd *create_inotify_fd( void )
914 int unix_fd;
916 unix_fd = inotify_init();
917 if (unix_fd<0)
918 return NULL;
919 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
922 static int map_flags( unsigned int filter )
924 unsigned int mask;
926 /* always watch these so we can track subdirectories in recursive watches */
927 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
929 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
930 mask |= IN_ATTRIB;
931 if (filter & FILE_NOTIFY_CHANGE_SIZE)
932 mask |= IN_MODIFY;
933 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
934 mask |= IN_MODIFY;
935 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
936 mask |= IN_MODIFY;
937 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
938 mask |= IN_ATTRIB;
940 return mask;
943 static int inotify_add_dir( char *path, unsigned int filter )
945 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
946 path, map_flags( filter ) );
947 if (wd != -1)
948 set_fd_events( inotify_fd, POLLIN );
949 return wd;
952 static int init_inotify( void )
954 int i;
956 if (inotify_fd)
957 return 1;
959 inotify_fd = create_inotify_fd();
960 if (!inotify_fd)
961 return 0;
963 for (i=0; i<HASH_SIZE; i++)
965 list_init( &inode_hash[i] );
966 list_init( &wd_hash[i] );
969 return 1;
972 static int inotify_adjust_changes( struct dir *dir )
974 unsigned int filter;
975 struct inode *inode;
976 struct stat st;
977 char path[32];
978 int wd, unix_fd;
980 if (!inotify_fd)
981 return 0;
983 unix_fd = get_unix_fd( dir->fd );
985 inode = dir->inode;
986 if (!inode)
988 /* check if this fd is already being watched */
989 if (-1 == fstat( unix_fd, &st ))
990 return 0;
992 inode = get_inode( st.st_dev, st.st_ino );
993 if (!inode)
994 inode = create_inode( st.st_dev, st.st_ino );
995 if (!inode)
996 return 0;
997 list_add_tail( &inode->dirs, &dir->in_entry );
998 dir->inode = inode;
1001 filter = filter_from_inode( inode, 0 );
1003 sprintf( path, "/proc/self/fd/%u", unix_fd );
1004 wd = inotify_add_dir( path, filter );
1005 if (wd == -1) return 0;
1007 inode_set_wd( inode, wd );
1009 return 1;
1012 static char *get_basename( const char *link )
1014 char *buffer, *name = NULL;
1015 int r, n = 0x100;
1017 while (1)
1019 buffer = malloc( n );
1020 if (!buffer) return NULL;
1022 r = readlink( link, buffer, n );
1023 if (r < 0)
1024 break;
1026 if (r < n)
1028 name = buffer;
1029 break;
1031 free( buffer );
1032 n *= 2;
1035 if (name)
1037 while (r > 0 && name[ r - 1 ] == '/' )
1038 r--;
1039 name[ r ] = 0;
1041 name = strrchr( name, '/' );
1042 if (name)
1043 name = strdup( &name[1] );
1046 free( buffer );
1047 return name;
1050 static int dir_add_to_existing_notify( struct dir *dir )
1052 struct inode *inode, *parent;
1053 unsigned int filter = 0;
1054 struct stat st, st_new;
1055 char link[35], *name;
1056 int wd, unix_fd;
1058 if (!inotify_fd)
1059 return 0;
1061 unix_fd = get_unix_fd( dir->fd );
1063 /* check if it's in the list of inodes we want to watch */
1064 if (-1 == fstat( unix_fd, &st_new ))
1065 return 0;
1066 inode = find_inode( st_new.st_dev, st_new.st_ino );
1067 if (inode)
1068 return 0;
1070 /* lookup the parent */
1071 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1072 if (-1 == stat( link, &st ))
1073 return 0;
1076 * If there's no parent, stop. We could keep going adding
1077 * ../ to the path until we hit the root of the tree or
1078 * find a recursively watched ancestor.
1079 * Assume it's too expensive to search up the tree for now.
1081 parent = find_inode( st.st_dev, st.st_ino );
1082 if (!parent)
1083 return 0;
1085 if (parent->wd == -1)
1086 return 0;
1088 filter = filter_from_inode( parent, 1 );
1089 if (!filter)
1090 return 0;
1092 sprintf( link, "/proc/self/fd/%u", unix_fd );
1093 name = get_basename( link );
1094 if (!name)
1095 return 0;
1096 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1097 free( name );
1098 if (!inode)
1099 return 0;
1101 /* Couldn't find this inode at the start of the function, must be new */
1102 assert( inode->wd == -1 );
1104 wd = inotify_add_dir( link, filter );
1105 if (wd != -1)
1106 inode_set_wd( inode, wd );
1108 return 1;
1111 #else
1113 static int init_inotify( void )
1115 return 0;
1118 static int inotify_adjust_changes( struct dir *dir )
1120 return 0;
1123 static void free_inode( struct inode *inode )
1125 assert( 0 );
1128 static int dir_add_to_existing_notify( struct dir *dir )
1130 return 0;
1133 #endif /* HAVE_SYS_INOTIFY_H */
1135 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1137 struct dir *dir;
1139 dir = alloc_object( &dir_ops );
1140 if (!dir)
1141 return NULL;
1143 list_init( &dir->change_records );
1144 dir->filter = 0;
1145 dir->notified = 0;
1146 dir->want_data = 0;
1147 dir->inode = NULL;
1148 grab_object( fd );
1149 dir->fd = fd;
1150 dir->mode = mode;
1151 dir->uid = ~(uid_t)0;
1152 dir->client_process = NULL;
1153 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1155 dir_add_to_existing_notify( dir );
1157 return &dir->obj;
1160 /* retrieve (or allocate) the client-side directory cache entry */
1161 DECL_HANDLER(get_directory_cache_entry)
1163 struct dir *dir;
1164 int *free_entries;
1165 data_size_t free_size;
1167 if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
1169 if (!dir->client_process)
1171 if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
1172 dir->client_process = (struct process *)grab_object( current->process );
1175 if (dir->client_process == current->process) reply->entry = dir->client_entry;
1176 else set_error( STATUS_SHARING_VIOLATION );
1178 done: /* allow freeing entries even on failure */
1179 free_size = get_reply_max_size();
1180 free_entries = get_free_dir_cache_entries( current->process, &free_size );
1181 if (free_entries) set_reply_data_ptr( free_entries, free_size );
1183 release_object( dir );
1186 /* enable change notifications for a directory */
1187 DECL_HANDLER(read_directory_changes)
1189 struct dir *dir;
1190 struct async *async;
1192 if (!req->filter)
1194 set_error(STATUS_INVALID_PARAMETER);
1195 return;
1198 dir = get_dir_obj( current->process, req->async.handle, 0 );
1199 if (!dir)
1200 return;
1202 /* requests don't timeout */
1203 if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
1204 fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
1206 /* assign it once */
1207 if (!dir->filter)
1209 init_inotify();
1210 insert_change( dir );
1211 dir->filter = req->filter;
1212 dir->subtree = req->subtree;
1213 dir->want_data = req->want_data;
1216 /* if there's already a change in the queue, send it */
1217 if (!list_empty( &dir->change_records ))
1218 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1220 /* setup the real notification */
1221 if (!inotify_adjust_changes( dir ))
1222 dnotify_adjust_changes( dir );
1224 set_error(STATUS_PENDING);
1226 release_object( async );
1227 end:
1228 release_object( dir );
1231 DECL_HANDLER(read_change)
1233 struct change_record *record, *next;
1234 struct dir *dir;
1235 struct list events;
1236 char *data, *event;
1237 int size = 0;
1239 dir = get_dir_obj( current->process, req->handle, 0 );
1240 if (!dir)
1241 return;
1243 list_init( &events );
1244 list_move_tail( &events, &dir->change_records );
1245 release_object( dir );
1247 if (list_empty( &events ))
1249 set_error( STATUS_NO_DATA_DETECTED );
1250 return;
1253 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1255 size += (offsetof(struct filesystem_event, name[record->event.len])
1256 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1259 if (size > get_reply_max_size())
1260 set_error( STATUS_BUFFER_TOO_SMALL );
1261 else if ((data = mem_alloc( size )) != NULL)
1263 event = data;
1264 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1266 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1268 /* FIXME: rename events are sometimes reported as delete/create */
1269 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1271 struct list *elem = list_next( &events, &record->entry );
1272 if (elem)
1273 next = LIST_ENTRY(elem, struct change_record, entry);
1275 if (elem && next->cookie == record->cookie)
1276 next->cookie = 0;
1277 else
1278 record->event.action = FILE_ACTION_REMOVED;
1280 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1281 record->event.action = FILE_ACTION_ADDED;
1283 memcpy( event, &record->event, len );
1284 event += len;
1285 if (len % sizeof(int))
1287 memset( event, 0, sizeof(int) - len % sizeof(int) );
1288 event += sizeof(int) - len % sizeof(int);
1291 set_reply_data_ptr( data, size );
1294 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1296 list_remove( &record->entry );
1297 free( record );