server: replace reselect_async by async_event
[wine/hacks.git] / server / change.c
blob2e4a11360ab660756a23a1e86bf34e9bd960e55a
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_SYS_ERRNO_H
36 #include <sys/errno.h>
37 #endif
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "thread.h"
46 #include "request.h"
47 #include "process.h"
48 #include "security.h"
49 #include "winternl.h"
51 /* dnotify support */
53 #ifdef linux
54 #ifndef F_NOTIFY
55 #define F_NOTIFY 1026
56 #define DN_ACCESS 0x00000001 /* File accessed */
57 #define DN_MODIFY 0x00000002 /* File modified */
58 #define DN_CREATE 0x00000004 /* File created */
59 #define DN_DELETE 0x00000008 /* File removed */
60 #define DN_RENAME 0x00000010 /* File renamed */
61 #define DN_ATTRIB 0x00000020 /* File changed attributes */
62 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
63 #endif
64 #endif
66 /* inotify support */
68 #ifdef HAVE_SYS_INOTIFY_H
69 #include <sys/inotify.h>
70 #define USE_INOTIFY
71 #elif defined(__linux__) && defined(__i386__)
73 #define SYS_inotify_init 291
74 #define SYS_inotify_add_watch 292
75 #define SYS_inotify_rm_watch 293
77 struct inotify_event {
78 int wd;
79 unsigned int mask;
80 unsigned int cookie;
81 unsigned int len;
82 char name[1];
85 #define IN_ACCESS 0x00000001
86 #define IN_MODIFY 0x00000002
87 #define IN_ATTRIB 0x00000004
88 #define IN_CLOSE_WRITE 0x00000008
89 #define IN_CLOSE_NOWRITE 0x00000010
90 #define IN_OPEN 0x00000020
91 #define IN_MOVED_FROM 0x00000040
92 #define IN_MOVED_TO 0x00000080
93 #define IN_CREATE 0x00000100
94 #define IN_DELETE 0x00000200
95 #define IN_DELETE_SELF 0x00000400
97 static inline int inotify_init( void )
99 int ret;
100 __asm__ __volatile__( "int $0x80"
101 : "=a" (ret)
102 : "0" (SYS_inotify_init));
103 if (ret<0) { errno = -ret; ret = -1; }
104 return ret;
107 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
109 int ret;
110 __asm__ __volatile__( "pushl %%ebx;\n\t"
111 "movl %2,%%ebx;\n\t"
112 "int $0x80;\n\t"
113 "popl %%ebx"
114 : "=a" (ret) : "0" (SYS_inotify_add_watch),
115 "r" (fd), "c" (name), "d" (mask) );
116 if (ret<0) { errno = -ret; ret = -1; }
117 return ret;
120 static inline int inotify_rm_watch( int fd, int wd )
122 int ret;
123 __asm__ __volatile__( "pushl %%ebx;\n\t"
124 "movl %2,%%ebx;\n\t"
125 "int $0x80;\n\t"
126 "popl %%ebx"
127 : "=a" (ret) : "0" (SYS_inotify_rm_watch),
128 "r" (fd), "c" (wd) );
129 if (ret<0) { errno = -ret; ret = -1; }
130 return ret;
133 #define USE_INOTIFY
135 #endif
137 struct inode;
139 static void free_inode( struct inode *inode );
141 static struct fd *inotify_fd;
143 struct change_record {
144 struct list entry;
145 int action;
146 int len;
147 char name[1];
150 struct dir
152 struct object obj; /* object header */
153 struct fd *fd; /* file descriptor to the directory */
154 mode_t mode; /* file stat.st_mode */
155 uid_t uid; /* file stat.st_uid */
156 struct list entry; /* entry in global change notifications list */
157 unsigned int filter; /* notification filter */
158 int notified; /* SIGIO counter */
159 int want_data; /* return change data */
160 int subtree; /* do we want to watch subdirectories? */
161 struct list change_records; /* data for the change */
162 struct list in_entry; /* entry in the inode dirs list */
163 struct inode *inode; /* inode of the associated directory */
166 static struct fd *dir_get_fd( struct object *obj );
167 static struct security_descriptor *dir_get_sd( struct object *obj );
168 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
169 unsigned int set_info );
170 static void dir_dump( struct object *obj, int verbose );
171 static void dir_destroy( struct object *obj );
173 static const struct object_ops dir_ops =
175 sizeof(struct dir), /* size */
176 dir_dump, /* dump */
177 no_get_type, /* get_type */
178 add_queue, /* add_queue */
179 remove_queue, /* remove_queue */
180 default_fd_signaled, /* signaled */
181 no_satisfied, /* satisfied */
182 no_signal, /* signal */
183 dir_get_fd, /* get_fd */
184 default_fd_map_access, /* map_access */
185 dir_get_sd, /* get_sd */
186 dir_set_sd, /* set_sd */
187 no_lookup_name, /* lookup_name */
188 no_open_file, /* open_file */
189 fd_close_handle, /* close_handle */
190 dir_destroy /* destroy */
193 static int dir_get_poll_events( struct fd *fd );
194 static enum server_fd_type dir_get_fd_type( struct fd *fd );
196 static const struct fd_ops dir_fd_ops =
198 dir_get_poll_events, /* get_poll_events */
199 default_poll_event, /* poll_event */
200 no_flush, /* flush */
201 dir_get_fd_type, /* get_fd_type */
202 default_fd_removable, /* removable */
203 default_fd_ioctl, /* ioctl */
204 default_fd_queue_async, /* queue_async */
205 default_fd_async_event, /* async_event */
206 default_fd_cancel_async /* cancel_async */
209 static struct list change_list = LIST_INIT(change_list);
211 static void dnotify_adjust_changes( struct dir *dir )
213 #if defined(F_SETSIG) && defined(F_NOTIFY)
214 int fd = get_unix_fd( dir->fd );
215 unsigned int filter = dir->filter;
216 unsigned int val;
217 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
218 return;
220 val = DN_MULTISHOT;
221 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
222 val |= DN_RENAME | DN_DELETE | DN_CREATE;
223 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
224 val |= DN_RENAME | DN_DELETE | DN_CREATE;
225 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
226 val |= DN_ATTRIB;
227 if (filter & FILE_NOTIFY_CHANGE_SIZE)
228 val |= DN_MODIFY;
229 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
230 val |= DN_MODIFY;
231 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
232 val |= DN_ACCESS;
233 if (filter & FILE_NOTIFY_CHANGE_CREATION)
234 val |= DN_CREATE;
235 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
236 val |= DN_ATTRIB;
237 fcntl( fd, F_NOTIFY, val );
238 #endif
241 /* insert change in the global list */
242 static inline void insert_change( struct dir *dir )
244 sigset_t sigset;
246 sigemptyset( &sigset );
247 sigaddset( &sigset, SIGIO );
248 sigprocmask( SIG_BLOCK, &sigset, NULL );
249 list_add_head( &change_list, &dir->entry );
250 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
253 /* remove change from the global list */
254 static inline void remove_change( struct dir *dir )
256 sigset_t sigset;
258 sigemptyset( &sigset );
259 sigaddset( &sigset, SIGIO );
260 sigprocmask( SIG_BLOCK, &sigset, NULL );
261 list_remove( &dir->entry );
262 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
265 static void dir_dump( struct object *obj, int verbose )
267 struct dir *dir = (struct dir *)obj;
268 assert( obj->ops == &dir_ops );
269 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
272 /* enter here directly from SIGIO signal handler */
273 void do_change_notify( int unix_fd )
275 struct dir *dir;
277 /* FIXME: this is O(n) ... probably can be improved */
278 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
280 if (get_unix_fd( dir->fd ) != unix_fd) continue;
281 interlocked_xchg_add( &dir->notified, 1 );
282 break;
286 /* SIGIO callback, called synchronously with the poll loop */
287 void sigio_callback(void)
289 struct dir *dir;
291 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
293 if (interlocked_xchg( &dir->notified, 0 ))
294 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
298 static struct fd *dir_get_fd( struct object *obj )
300 struct dir *dir = (struct dir *)obj;
301 assert( obj->ops == &dir_ops );
302 return (struct fd *)grab_object( dir->fd );
305 static int get_dir_unix_fd( struct dir *dir )
307 return get_unix_fd( dir->fd );
310 static struct security_descriptor *dir_get_sd( struct object *obj )
312 struct dir *dir = (struct dir *)obj;
313 int unix_fd;
314 struct stat st;
315 struct security_descriptor *sd;
316 assert( obj->ops == &dir_ops );
318 unix_fd = get_dir_unix_fd( dir );
320 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
321 return obj->sd;
323 /* mode and uid the same? if so, no need to re-generate security descriptor */
324 if (obj->sd &&
325 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
326 (st.st_uid == dir->uid))
327 return obj->sd;
329 sd = mode_to_sd( st.st_mode,
330 security_unix_uid_to_sid( st.st_uid ),
331 token_get_primary_group( current->process->token ));
332 if (!sd) return obj->sd;
334 dir->mode = st.st_mode;
335 dir->uid = st.st_uid;
336 free( obj->sd );
337 obj->sd = sd;
338 return sd;
341 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
342 unsigned int set_info )
344 struct dir *dir = (struct dir *)obj;
345 const SID *owner;
346 struct stat st;
347 mode_t mode;
348 int unix_fd;
350 assert( obj->ops == &dir_ops );
352 unix_fd = get_dir_unix_fd( dir );
354 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
356 if (set_info & OWNER_SECURITY_INFORMATION)
358 owner = sd_get_owner( sd );
359 if (!owner)
361 set_error( STATUS_INVALID_SECURITY_DESCR );
362 return 0;
364 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
366 /* FIXME: get Unix uid and call fchown */
369 else if (obj->sd)
370 owner = sd_get_owner( obj->sd );
371 else
372 owner = token_get_user( current->process->token );
374 if (set_info & DACL_SECURITY_INFORMATION)
376 /* keep the bits that we don't map to access rights in the ACL */
377 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
378 mode |= sd_to_mode( sd, owner );
380 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
382 file_set_error();
383 return 0;
386 return 1;
389 static struct change_record *get_first_change_record( struct dir *dir )
391 struct list *ptr = list_head( &dir->change_records );
392 if (!ptr) return NULL;
393 list_remove( ptr );
394 return LIST_ENTRY( ptr, struct change_record, entry );
397 static void dir_destroy( struct object *obj )
399 struct change_record *record;
400 struct dir *dir = (struct dir *)obj;
401 assert (obj->ops == &dir_ops);
403 if (dir->filter)
404 remove_change( dir );
406 if (dir->inode)
408 list_remove( &dir->in_entry );
409 free_inode( dir->inode );
412 while ((record = get_first_change_record( dir ))) free( record );
414 release_object( dir->fd );
416 if (inotify_fd && list_empty( &change_list ))
418 release_object( inotify_fd );
419 inotify_fd = NULL;
423 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
425 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
428 static int dir_get_poll_events( struct fd *fd )
430 return 0;
433 static enum server_fd_type dir_get_fd_type( struct fd *fd )
435 return FD_TYPE_DIR;
438 #ifdef USE_INOTIFY
440 #define HASH_SIZE 31
442 struct inode {
443 struct list ch_entry; /* entry in the children list */
444 struct list children; /* children of this inode */
445 struct inode *parent; /* parent of this inode */
446 struct list dirs; /* directory handles watching this inode */
447 struct list ino_entry; /* entry in the inode hash */
448 struct list wd_entry; /* entry in the watch descriptor hash */
449 dev_t dev; /* device number */
450 ino_t ino; /* device's inode number */
451 int wd; /* inotify's watch descriptor */
452 char *name; /* basename name of the inode */
455 static struct list inode_hash[ HASH_SIZE ];
456 static struct list wd_hash[ HASH_SIZE ];
458 static int inotify_add_dir( char *path, unsigned int filter );
460 static struct inode *inode_from_wd( int wd )
462 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
463 struct inode *inode;
465 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
466 if (inode->wd == wd)
467 return inode;
469 return NULL;
472 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
474 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
477 static struct inode *find_inode( dev_t dev, ino_t ino )
479 struct list *bucket = get_hash_list( dev, ino );
480 struct inode *inode;
482 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
483 if (inode->ino == ino && inode->dev == dev)
484 return inode;
486 return NULL;
489 static struct inode *create_inode( dev_t dev, ino_t ino )
491 struct inode *inode;
493 inode = malloc( sizeof *inode );
494 if (inode)
496 list_init( &inode->children );
497 list_init( &inode->dirs );
498 inode->ino = ino;
499 inode->dev = dev;
500 inode->wd = -1;
501 inode->parent = NULL;
502 inode->name = NULL;
503 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
505 return inode;
508 static struct inode *get_inode( dev_t dev, ino_t ino )
510 struct inode *inode;
512 inode = find_inode( dev, ino );
513 if (inode)
514 return inode;
515 return create_inode( dev, ino );
518 static void inode_set_wd( struct inode *inode, int wd )
520 if (inode->wd != -1)
521 list_remove( &inode->wd_entry );
522 inode->wd = wd;
523 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
526 static void inode_set_name( struct inode *inode, const char *name )
528 free (inode->name);
529 inode->name = name ? strdup( name ) : NULL;
532 static void free_inode( struct inode *inode )
534 int subtree = 0, watches = 0;
535 struct inode *tmp, *next;
536 struct dir *dir;
538 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
540 subtree |= dir->subtree;
541 watches++;
544 if (!subtree && !inode->parent)
546 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
547 struct inode, ch_entry )
549 assert( tmp != inode );
550 assert( tmp->parent == inode );
551 free_inode( tmp );
555 if (watches)
556 return;
558 if (inode->parent)
559 list_remove( &inode->ch_entry );
561 /* disconnect remaining children from the parent */
562 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
564 list_remove( &tmp->ch_entry );
565 tmp->parent = NULL;
568 if (inode->wd != -1)
570 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
571 list_remove( &inode->wd_entry );
573 list_remove( &inode->ino_entry );
575 free( inode->name );
576 free( inode );
579 static struct inode *inode_add( struct inode *parent,
580 dev_t dev, ino_t ino, const char *name )
582 struct inode *inode;
584 inode = get_inode( dev, ino );
585 if (!inode)
586 return NULL;
588 if (!inode->parent)
590 list_add_tail( &parent->children, &inode->ch_entry );
591 inode->parent = parent;
592 assert( inode != parent );
594 inode_set_name( inode, name );
596 return inode;
599 static struct inode *inode_from_name( struct inode *inode, const char *name )
601 struct inode *i;
603 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
604 if (i->name && !strcmp( i->name, name ))
605 return i;
606 return NULL;
609 static int inotify_get_poll_events( struct fd *fd );
610 static void inotify_poll_event( struct fd *fd, int event );
612 static const struct fd_ops inotify_fd_ops =
614 inotify_get_poll_events, /* get_poll_events */
615 inotify_poll_event, /* poll_event */
616 NULL, /* flush */
617 NULL, /* get_fd_type */
618 NULL, /* removable */
619 NULL, /* ioctl */
620 NULL, /* queue_async */
621 NULL, /* async_event */
622 NULL /* cancel_async */
625 static int inotify_get_poll_events( struct fd *fd )
627 return POLLIN;
630 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
631 const char *relpath )
633 struct change_record *record;
635 assert( dir->obj.ops == &dir_ops );
637 if (dir->want_data)
639 size_t len = strlen(relpath);
640 record = malloc( offsetof(struct change_record, name[len]) );
641 if (!record)
642 return;
644 record->action = action;
645 memcpy( record->name, relpath, len );
646 record->len = len;
648 list_add_tail( &dir->change_records, &record->entry );
651 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
654 static unsigned int filter_from_event( struct inotify_event *ie )
656 unsigned int filter = 0;
658 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
659 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
660 if (ie->mask & IN_MODIFY)
661 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
662 if (ie->mask & IN_ATTRIB)
663 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
664 if (ie->mask & IN_ACCESS)
665 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
666 if (ie->mask & IN_CREATE)
667 filter |= FILE_NOTIFY_CHANGE_CREATION;
669 return filter;
672 /* scan up the parent directories for watches */
673 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
675 unsigned int filter = 0;
676 struct dir *dir;
678 /* combine filters from parents watching subtrees */
679 while (inode)
681 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
682 if (dir->subtree || !is_parent)
683 filter |= dir->filter;
684 is_parent = 1;
685 inode = inode->parent;
688 return filter;
691 static char *inode_get_path( struct inode *inode, int sz )
693 struct list *head;
694 char *path;
695 int len;
697 if (!inode)
698 return NULL;
700 head = list_head( &inode->dirs );
701 if (head)
703 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
704 path = malloc ( 32 + sz );
705 if (path)
706 sprintf( path, "/proc/self/fd/%u/", unix_fd );
707 return path;
710 if (!inode->name)
711 return NULL;
713 len = strlen( inode->name );
714 path = inode_get_path( inode->parent, sz + len + 1 );
715 if (!path)
716 return NULL;
718 strcat( path, inode->name );
719 strcat( path, "/" );
721 return path;
724 static int inode_check_dir( struct inode *parent, const char *name )
726 char *path;
727 unsigned int filter;
728 struct inode *inode;
729 struct stat st;
730 int wd = -1, r = -1;
732 path = inode_get_path( parent, strlen(name) );
733 if (!path)
734 return r;
736 strcat( path, name );
738 r = stat( path, &st );
739 if (r < 0) goto end;
741 if (!S_ISDIR(st.st_mode))
743 r = 0;
744 goto end;
747 r = 1;
749 filter = filter_from_inode( parent, 1 );
750 if (!filter)
751 goto end;
753 inode = inode_add( parent, st.st_dev, st.st_ino, name );
754 if (!inode || inode->wd != -1)
755 goto end;
757 wd = inotify_add_dir( path, filter );
758 if (wd != -1)
759 inode_set_wd( inode, wd );
760 else
761 free_inode( inode );
763 end:
764 free( path );
765 return r;
768 static int prepend( char **path, const char *segment )
770 int extra;
771 char *p;
773 extra = strlen( segment ) + 1;
774 if (*path)
776 int len = strlen( *path ) + 1;
777 p = realloc( *path, len + extra );
778 if (!p) return 0;
779 memmove( &p[ extra ], p, len );
780 p[ extra - 1 ] = '/';
781 memcpy( p, segment, extra - 1 );
783 else
785 p = malloc( extra );
786 if (!p) return 0;
787 memcpy( p, segment, extra );
790 *path = p;
792 return 1;
795 static void inotify_notify_all( struct inotify_event *ie )
797 unsigned int filter, action;
798 struct inode *inode, *i;
799 char *path = NULL;
800 struct dir *dir;
802 inode = inode_from_wd( ie->wd );
803 if (!inode)
805 fprintf( stderr, "no inode matches %d\n", ie->wd);
806 return;
809 filter = filter_from_event( ie );
811 if (ie->mask & IN_CREATE)
813 switch (inode_check_dir( inode, ie->name ))
815 case 1:
816 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
817 break;
818 case 0:
819 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
820 break;
821 default:
822 break;
823 /* Maybe the file disappeared before we could check it? */
825 action = FILE_ACTION_ADDED;
827 else if (ie->mask & IN_DELETE)
828 action = FILE_ACTION_REMOVED;
829 else
830 action = FILE_ACTION_MODIFIED;
833 * Work our way up the inode hierarchy
834 * extending the relative path as we go
835 * and notifying all recursive watches.
837 if (!prepend( &path, ie->name ))
838 return;
840 for (i = inode; i; i = i->parent)
842 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
843 if ((filter & dir->filter) && (i==inode || dir->subtree))
844 inotify_do_change_notify( dir, action, path );
846 if (!i->name || !prepend( &path, i->name ))
847 break;
850 free( path );
852 if (ie->mask & IN_DELETE)
854 i = inode_from_name( inode, ie->name );
855 if (i)
856 free_inode( i );
860 static void inotify_poll_event( struct fd *fd, int event )
862 int r, ofs, unix_fd;
863 char buffer[0x1000];
864 struct inotify_event *ie;
866 unix_fd = get_unix_fd( fd );
867 r = read( unix_fd, buffer, sizeof buffer );
868 if (r < 0)
870 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
871 return;
874 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
876 ie = (struct inotify_event*) &buffer[ofs];
877 if (!ie->len)
878 break;
879 ofs += offsetof( struct inotify_event, name[ie->len] );
880 if (ofs > r) break;
881 inotify_notify_all( ie );
885 static inline struct fd *create_inotify_fd( void )
887 int unix_fd;
889 unix_fd = inotify_init();
890 if (unix_fd<0)
891 return NULL;
892 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
895 static int map_flags( unsigned int filter )
897 unsigned int mask;
899 /* always watch these so we can track subdirectories in recursive watches */
900 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
902 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
903 mask |= IN_ATTRIB;
904 if (filter & FILE_NOTIFY_CHANGE_SIZE)
905 mask |= IN_MODIFY;
906 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
907 mask |= IN_MODIFY;
908 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
909 mask |= IN_ACCESS;
910 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
911 mask |= IN_ATTRIB;
913 return mask;
916 static int inotify_add_dir( char *path, unsigned int filter )
918 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
919 path, map_flags( filter ) );
920 if (wd != -1)
921 set_fd_events( inotify_fd, POLLIN );
922 return wd;
925 static int init_inotify( void )
927 int i;
929 if (inotify_fd)
930 return 1;
932 inotify_fd = create_inotify_fd();
933 if (!inotify_fd)
934 return 0;
936 for (i=0; i<HASH_SIZE; i++)
938 list_init( &inode_hash[i] );
939 list_init( &wd_hash[i] );
942 return 1;
945 static int inotify_adjust_changes( struct dir *dir )
947 unsigned int filter;
948 struct inode *inode;
949 struct stat st;
950 char path[32];
951 int wd, unix_fd;
953 if (!inotify_fd)
954 return 0;
956 unix_fd = get_unix_fd( dir->fd );
958 inode = dir->inode;
959 if (!inode)
961 /* check if this fd is already being watched */
962 if (-1 == fstat( unix_fd, &st ))
963 return 0;
965 inode = get_inode( st.st_dev, st.st_ino );
966 if (!inode)
967 inode = create_inode( st.st_dev, st.st_ino );
968 if (!inode)
969 return 0;
970 list_add_tail( &inode->dirs, &dir->in_entry );
971 dir->inode = inode;
974 filter = filter_from_inode( inode, 0 );
976 sprintf( path, "/proc/self/fd/%u", unix_fd );
977 wd = inotify_add_dir( path, filter );
978 if (wd == -1) return 0;
980 inode_set_wd( inode, wd );
982 return 1;
985 static char *get_basename( const char *link )
987 char *buffer, *name = NULL;
988 int r, n = 0x100;
990 while (1)
992 buffer = malloc( n );
993 if (!buffer) return NULL;
995 r = readlink( link, buffer, n );
996 if (r < 0)
997 break;
999 if (r < n)
1001 name = buffer;
1002 break;
1004 free( buffer );
1005 n *= 2;
1008 if (name)
1010 while (r > 0 && name[ r - 1 ] == '/' )
1011 r--;
1012 name[ r ] = 0;
1014 name = strrchr( name, '/' );
1015 if (name)
1016 name = strdup( &name[1] );
1019 free( buffer );
1020 return name;
1023 static int dir_add_to_existing_notify( struct dir *dir )
1025 struct inode *inode, *parent;
1026 unsigned int filter = 0;
1027 struct stat st, st_new;
1028 char link[35], *name;
1029 int wd, unix_fd;
1031 if (!inotify_fd)
1032 return 0;
1034 unix_fd = get_unix_fd( dir->fd );
1036 /* check if it's in the list of inodes we want to watch */
1037 if (-1 == fstat( unix_fd, &st_new ))
1038 return 0;
1039 inode = find_inode( st_new.st_dev, st_new.st_ino );
1040 if (inode)
1041 return 0;
1043 /* lookup the parent */
1044 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1045 if (-1 == stat( link, &st ))
1046 return 0;
1049 * If there's no parent, stop. We could keep going adding
1050 * ../ to the path until we hit the root of the tree or
1051 * find a recursively watched ancestor.
1052 * Assume it's too expensive to search up the tree for now.
1054 parent = find_inode( st.st_dev, st.st_ino );
1055 if (!parent)
1056 return 0;
1058 if (parent->wd == -1)
1059 return 0;
1061 filter = filter_from_inode( parent, 1 );
1062 if (!filter)
1063 return 0;
1065 sprintf( link, "/proc/self/fd/%u", unix_fd );
1066 name = get_basename( link );
1067 if (!name)
1068 return 0;
1069 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1070 free( name );
1071 if (!inode)
1072 return 0;
1074 /* Couldn't find this inode at the start of the function, must be new */
1075 assert( inode->wd == -1 );
1077 wd = inotify_add_dir( link, filter );
1078 if (wd != -1)
1079 inode_set_wd( inode, wd );
1081 return 1;
1084 #else
1086 static int init_inotify( void )
1088 return 0;
1091 static int inotify_adjust_changes( struct dir *dir )
1093 return 0;
1096 static void free_inode( struct inode *inode )
1098 assert( 0 );
1101 static int dir_add_to_existing_notify( struct dir *dir )
1103 return 0;
1106 #endif /* USE_INOTIFY */
1108 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1110 struct dir *dir;
1112 dir = alloc_object( &dir_ops );
1113 if (!dir)
1114 return NULL;
1116 list_init( &dir->change_records );
1117 dir->filter = 0;
1118 dir->notified = 0;
1119 dir->want_data = 0;
1120 dir->inode = NULL;
1121 grab_object( fd );
1122 dir->fd = fd;
1123 dir->mode = mode;
1124 dir->uid = ~(uid_t)0;
1125 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1127 dir_add_to_existing_notify( dir );
1129 return &dir->obj;
1132 /* enable change notifications for a directory */
1133 DECL_HANDLER(read_directory_changes)
1135 struct dir *dir;
1136 struct async *async;
1138 if (!req->filter)
1140 set_error(STATUS_INVALID_PARAMETER);
1141 return;
1144 dir = get_dir_obj( current->process, req->async.handle, 0 );
1145 if (!dir)
1146 return;
1148 /* requests don't timeout */
1149 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1151 /* assign it once */
1152 if (!dir->filter)
1154 init_inotify();
1155 insert_change( dir );
1156 dir->filter = req->filter;
1157 dir->subtree = req->subtree;
1158 dir->want_data = req->want_data;
1161 /* if there's already a change in the queue, send it */
1162 if (!list_empty( &dir->change_records ))
1163 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1165 /* setup the real notification */
1166 if (!inotify_adjust_changes( dir ))
1167 dnotify_adjust_changes( dir );
1169 release_object( async );
1170 set_error(STATUS_PENDING);
1172 end:
1173 release_object( dir );
1176 DECL_HANDLER(read_change)
1178 struct change_record *record;
1179 struct dir *dir;
1181 dir = get_dir_obj( current->process, req->handle, 0 );
1182 if (!dir)
1183 return;
1185 if ((record = get_first_change_record( dir )) != NULL)
1187 reply->action = record->action;
1188 set_reply_data( record->name, record->len );
1189 free( record );
1191 else
1192 set_error( STATUS_NO_DATA_DETECTED );
1194 release_object( dir );