explorer: Return desktop shellview interface.
[wine.git] / server / change.c
blob11d9b936ac47a6a993d7a1273e5d6a3b94a0c705
1 /*
2 * Server-side change notification management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2006 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <limits.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #ifdef HAVE_POLL_H
36 # include <poll.h>
37 #endif
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "thread.h"
46 #include "request.h"
47 #include "process.h"
48 #include "security.h"
49 #include "winternl.h"
51 /* dnotify support */
53 #ifdef linux
54 #ifndef F_NOTIFY
55 #define F_NOTIFY 1026
56 #define DN_ACCESS 0x00000001 /* File accessed */
57 #define DN_MODIFY 0x00000002 /* File modified */
58 #define DN_CREATE 0x00000004 /* File created */
59 #define DN_DELETE 0x00000008 /* File removed */
60 #define DN_RENAME 0x00000010 /* File renamed */
61 #define DN_ATTRIB 0x00000020 /* File changed attributes */
62 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
63 #endif
64 #endif
66 /* inotify support */
68 #ifdef HAVE_SYS_INOTIFY_H
69 #include <sys/inotify.h>
70 #define USE_INOTIFY
71 #elif defined(__linux__) && defined(__i386__)
73 #define SYS_inotify_init 291
74 #define SYS_inotify_add_watch 292
75 #define SYS_inotify_rm_watch 293
77 struct inotify_event {
78 int wd;
79 unsigned int mask;
80 unsigned int cookie;
81 unsigned int len;
82 char name[1];
85 #define IN_ACCESS 0x00000001
86 #define IN_MODIFY 0x00000002
87 #define IN_ATTRIB 0x00000004
88 #define IN_CLOSE_WRITE 0x00000008
89 #define IN_CLOSE_NOWRITE 0x00000010
90 #define IN_OPEN 0x00000020
91 #define IN_MOVED_FROM 0x00000040
92 #define IN_MOVED_TO 0x00000080
93 #define IN_CREATE 0x00000100
94 #define IN_DELETE 0x00000200
95 #define IN_DELETE_SELF 0x00000400
97 #define IN_ISDIR 0x40000000
99 static inline int inotify_init( void )
101 return syscall( SYS_inotify_init );
104 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
106 return syscall( SYS_inotify_add_watch, fd, name, mask );
109 static inline int inotify_rm_watch( int fd, int wd )
111 return syscall( SYS_inotify_rm_watch, fd, wd );
114 #define USE_INOTIFY
116 #endif
118 struct inode;
120 static void free_inode( struct inode *inode );
122 static struct fd *inotify_fd;
124 struct change_record {
125 struct list entry;
126 unsigned int cookie;
127 struct filesystem_event event;
130 struct dir
132 struct object obj; /* object header */
133 struct fd *fd; /* file descriptor to the directory */
134 mode_t mode; /* file stat.st_mode */
135 uid_t uid; /* file stat.st_uid */
136 struct list entry; /* entry in global change notifications list */
137 unsigned int filter; /* notification filter */
138 int notified; /* SIGIO counter */
139 int want_data; /* return change data */
140 int subtree; /* do we want to watch subdirectories? */
141 struct list change_records; /* data for the change */
142 struct list in_entry; /* entry in the inode dirs list */
143 struct inode *inode; /* inode of the associated directory */
146 static struct fd *dir_get_fd( struct object *obj );
147 static struct security_descriptor *dir_get_sd( struct object *obj );
148 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
149 unsigned int set_info );
150 static void dir_dump( struct object *obj, int verbose );
151 static void dir_destroy( struct object *obj );
153 static const struct object_ops dir_ops =
155 sizeof(struct dir), /* size */
156 dir_dump, /* dump */
157 no_get_type, /* get_type */
158 add_queue, /* add_queue */
159 remove_queue, /* remove_queue */
160 default_fd_signaled, /* signaled */
161 no_satisfied, /* satisfied */
162 no_signal, /* signal */
163 dir_get_fd, /* get_fd */
164 default_fd_map_access, /* map_access */
165 dir_get_sd, /* get_sd */
166 dir_set_sd, /* set_sd */
167 no_lookup_name, /* lookup_name */
168 no_open_file, /* open_file */
169 fd_close_handle, /* close_handle */
170 dir_destroy /* destroy */
173 static int dir_get_poll_events( struct fd *fd );
174 static enum server_fd_type dir_get_fd_type( struct fd *fd );
176 static const struct fd_ops dir_fd_ops =
178 dir_get_poll_events, /* get_poll_events */
179 default_poll_event, /* poll_event */
180 dir_get_fd_type, /* get_fd_type */
181 no_fd_read, /* read */
182 no_fd_write, /* write */
183 no_fd_flush, /* flush */
184 default_fd_ioctl, /* ioctl */
185 default_fd_queue_async, /* queue_async */
186 default_fd_reselect_async, /* reselect_async */
187 default_fd_cancel_async /* cancel_async */
190 static struct list change_list = LIST_INIT(change_list);
192 static void dnotify_adjust_changes( struct dir *dir )
194 #if defined(F_SETSIG) && defined(F_NOTIFY)
195 int fd = get_unix_fd( dir->fd );
196 unsigned int filter = dir->filter;
197 unsigned int val;
198 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
199 return;
201 val = DN_MULTISHOT;
202 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
203 val |= DN_RENAME | DN_DELETE | DN_CREATE;
204 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
205 val |= DN_RENAME | DN_DELETE | DN_CREATE;
206 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
207 val |= DN_ATTRIB;
208 if (filter & FILE_NOTIFY_CHANGE_SIZE)
209 val |= DN_MODIFY;
210 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
211 val |= DN_MODIFY;
212 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
213 val |= DN_ACCESS;
214 if (filter & FILE_NOTIFY_CHANGE_CREATION)
215 val |= DN_CREATE;
216 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
217 val |= DN_ATTRIB;
218 fcntl( fd, F_NOTIFY, val );
219 #endif
222 /* insert change in the global list */
223 static inline void insert_change( struct dir *dir )
225 sigset_t sigset;
227 sigemptyset( &sigset );
228 sigaddset( &sigset, SIGIO );
229 sigprocmask( SIG_BLOCK, &sigset, NULL );
230 list_add_head( &change_list, &dir->entry );
231 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
234 /* remove change from the global list */
235 static inline void remove_change( struct dir *dir )
237 sigset_t sigset;
239 sigemptyset( &sigset );
240 sigaddset( &sigset, SIGIO );
241 sigprocmask( SIG_BLOCK, &sigset, NULL );
242 list_remove( &dir->entry );
243 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
246 static void dir_dump( struct object *obj, int verbose )
248 struct dir *dir = (struct dir *)obj;
249 assert( obj->ops == &dir_ops );
250 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
253 /* enter here directly from SIGIO signal handler */
254 void do_change_notify( int unix_fd )
256 struct dir *dir;
258 /* FIXME: this is O(n) ... probably can be improved */
259 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
261 if (get_unix_fd( dir->fd ) != unix_fd) continue;
262 interlocked_xchg_add( &dir->notified, 1 );
263 break;
267 /* SIGIO callback, called synchronously with the poll loop */
268 void sigio_callback(void)
270 struct dir *dir;
272 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
274 if (interlocked_xchg( &dir->notified, 0 ))
275 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
279 static struct fd *dir_get_fd( struct object *obj )
281 struct dir *dir = (struct dir *)obj;
282 assert( obj->ops == &dir_ops );
283 return (struct fd *)grab_object( dir->fd );
286 static int get_dir_unix_fd( struct dir *dir )
288 return get_unix_fd( dir->fd );
291 static struct security_descriptor *dir_get_sd( struct object *obj )
293 struct dir *dir = (struct dir *)obj;
294 int unix_fd;
295 struct stat st;
296 struct security_descriptor *sd;
297 assert( obj->ops == &dir_ops );
299 unix_fd = get_dir_unix_fd( dir );
301 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
302 return obj->sd;
304 /* mode and uid the same? if so, no need to re-generate security descriptor */
305 if (obj->sd &&
306 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
307 (st.st_uid == dir->uid))
308 return obj->sd;
310 sd = mode_to_sd( st.st_mode,
311 security_unix_uid_to_sid( st.st_uid ),
312 token_get_primary_group( current->process->token ));
313 if (!sd) return obj->sd;
315 dir->mode = st.st_mode;
316 dir->uid = st.st_uid;
317 free( obj->sd );
318 obj->sd = sd;
319 return sd;
322 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
323 unsigned int set_info )
325 struct dir *dir = (struct dir *)obj;
326 const SID *owner;
327 struct stat st;
328 mode_t mode;
329 int unix_fd;
331 assert( obj->ops == &dir_ops );
333 unix_fd = get_dir_unix_fd( dir );
335 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
337 if (set_info & OWNER_SECURITY_INFORMATION)
339 owner = sd_get_owner( sd );
340 if (!owner)
342 set_error( STATUS_INVALID_SECURITY_DESCR );
343 return 0;
345 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
347 /* FIXME: get Unix uid and call fchown */
350 else if (obj->sd)
351 owner = sd_get_owner( obj->sd );
352 else
353 owner = token_get_user( current->process->token );
355 if (set_info & DACL_SECURITY_INFORMATION)
357 /* keep the bits that we don't map to access rights in the ACL */
358 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
359 mode |= sd_to_mode( sd, owner );
361 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
363 file_set_error();
364 return 0;
367 return 1;
370 static struct change_record *get_first_change_record( struct dir *dir )
372 struct list *ptr = list_head( &dir->change_records );
373 if (!ptr) return NULL;
374 list_remove( ptr );
375 return LIST_ENTRY( ptr, struct change_record, entry );
378 static void dir_destroy( struct object *obj )
380 struct change_record *record;
381 struct dir *dir = (struct dir *)obj;
382 assert (obj->ops == &dir_ops);
384 if (dir->filter)
385 remove_change( dir );
387 if (dir->inode)
389 list_remove( &dir->in_entry );
390 free_inode( dir->inode );
393 while ((record = get_first_change_record( dir ))) free( record );
395 release_object( dir->fd );
397 if (inotify_fd && list_empty( &change_list ))
399 release_object( inotify_fd );
400 inotify_fd = NULL;
404 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
406 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
409 static int dir_get_poll_events( struct fd *fd )
411 return 0;
414 static enum server_fd_type dir_get_fd_type( struct fd *fd )
416 return FD_TYPE_DIR;
419 #ifdef USE_INOTIFY
421 #define HASH_SIZE 31
423 struct inode {
424 struct list ch_entry; /* entry in the children list */
425 struct list children; /* children of this inode */
426 struct inode *parent; /* parent of this inode */
427 struct list dirs; /* directory handles watching this inode */
428 struct list ino_entry; /* entry in the inode hash */
429 struct list wd_entry; /* entry in the watch descriptor hash */
430 dev_t dev; /* device number */
431 ino_t ino; /* device's inode number */
432 int wd; /* inotify's watch descriptor */
433 char *name; /* basename name of the inode */
436 static struct list inode_hash[ HASH_SIZE ];
437 static struct list wd_hash[ HASH_SIZE ];
439 static int inotify_add_dir( char *path, unsigned int filter );
441 static struct inode *inode_from_wd( int wd )
443 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
444 struct inode *inode;
446 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
447 if (inode->wd == wd)
448 return inode;
450 return NULL;
453 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
455 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
458 static struct inode *find_inode( dev_t dev, ino_t ino )
460 struct list *bucket = get_hash_list( dev, ino );
461 struct inode *inode;
463 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
464 if (inode->ino == ino && inode->dev == dev)
465 return inode;
467 return NULL;
470 static struct inode *create_inode( dev_t dev, ino_t ino )
472 struct inode *inode;
474 inode = malloc( sizeof *inode );
475 if (inode)
477 list_init( &inode->children );
478 list_init( &inode->dirs );
479 inode->ino = ino;
480 inode->dev = dev;
481 inode->wd = -1;
482 inode->parent = NULL;
483 inode->name = NULL;
484 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
486 return inode;
489 static struct inode *get_inode( dev_t dev, ino_t ino )
491 struct inode *inode;
493 inode = find_inode( dev, ino );
494 if (inode)
495 return inode;
496 return create_inode( dev, ino );
499 static void inode_set_wd( struct inode *inode, int wd )
501 if (inode->wd != -1)
502 list_remove( &inode->wd_entry );
503 inode->wd = wd;
504 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
507 static void inode_set_name( struct inode *inode, const char *name )
509 free (inode->name);
510 inode->name = name ? strdup( name ) : NULL;
513 static void free_inode( struct inode *inode )
515 int subtree = 0, watches = 0;
516 struct inode *tmp, *next;
517 struct dir *dir;
519 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
521 subtree |= dir->subtree;
522 watches++;
525 if (!subtree && !inode->parent)
527 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
528 struct inode, ch_entry )
530 assert( tmp != inode );
531 assert( tmp->parent == inode );
532 free_inode( tmp );
536 if (watches)
537 return;
539 if (inode->parent)
540 list_remove( &inode->ch_entry );
542 /* disconnect remaining children from the parent */
543 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
545 list_remove( &tmp->ch_entry );
546 tmp->parent = NULL;
549 if (inode->wd != -1)
551 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
552 list_remove( &inode->wd_entry );
554 list_remove( &inode->ino_entry );
556 free( inode->name );
557 free( inode );
560 static struct inode *inode_add( struct inode *parent,
561 dev_t dev, ino_t ino, const char *name )
563 struct inode *inode;
565 inode = get_inode( dev, ino );
566 if (!inode)
567 return NULL;
569 if (!inode->parent)
571 list_add_tail( &parent->children, &inode->ch_entry );
572 inode->parent = parent;
573 assert( inode != parent );
575 inode_set_name( inode, name );
577 return inode;
580 static struct inode *inode_from_name( struct inode *inode, const char *name )
582 struct inode *i;
584 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
585 if (i->name && !strcmp( i->name, name ))
586 return i;
587 return NULL;
590 static int inotify_get_poll_events( struct fd *fd );
591 static void inotify_poll_event( struct fd *fd, int event );
593 static const struct fd_ops inotify_fd_ops =
595 inotify_get_poll_events, /* get_poll_events */
596 inotify_poll_event, /* poll_event */
597 NULL, /* flush */
598 NULL, /* get_fd_type */
599 NULL, /* ioctl */
600 NULL, /* queue_async */
601 NULL, /* reselect_async */
602 NULL, /* cancel_async */
605 static int inotify_get_poll_events( struct fd *fd )
607 return POLLIN;
610 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
611 unsigned int cookie, const char *relpath )
613 struct change_record *record;
615 assert( dir->obj.ops == &dir_ops );
617 if (dir->want_data)
619 size_t len = strlen(relpath);
620 record = malloc( offsetof(struct change_record, event.name[len]) );
621 if (!record)
622 return;
624 record->cookie = cookie;
625 record->event.action = action;
626 memcpy( record->event.name, relpath, len );
627 record->event.len = len;
629 list_add_tail( &dir->change_records, &record->entry );
632 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
635 static unsigned int filter_from_event( struct inotify_event *ie )
637 unsigned int filter = 0;
639 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
640 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
641 if (ie->mask & IN_MODIFY)
642 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
643 if (ie->mask & IN_ATTRIB)
644 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
645 if (ie->mask & IN_ACCESS)
646 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
647 if (ie->mask & IN_CREATE)
648 filter |= FILE_NOTIFY_CHANGE_CREATION;
650 if (ie->mask & IN_ISDIR)
651 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
652 else
653 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
655 return filter;
658 /* scan up the parent directories for watches */
659 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
661 unsigned int filter = 0;
662 struct dir *dir;
664 /* combine filters from parents watching subtrees */
665 while (inode)
667 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
668 if (dir->subtree || !is_parent)
669 filter |= dir->filter;
670 is_parent = 1;
671 inode = inode->parent;
674 return filter;
677 static char *inode_get_path( struct inode *inode, int sz )
679 struct list *head;
680 char *path;
681 int len;
683 if (!inode)
684 return NULL;
686 head = list_head( &inode->dirs );
687 if (head)
689 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
690 path = malloc ( 32 + sz );
691 if (path)
692 sprintf( path, "/proc/self/fd/%u/", unix_fd );
693 return path;
696 if (!inode->name)
697 return NULL;
699 len = strlen( inode->name );
700 path = inode_get_path( inode->parent, sz + len + 1 );
701 if (!path)
702 return NULL;
704 strcat( path, inode->name );
705 strcat( path, "/" );
707 return path;
710 static void inode_check_dir( struct inode *parent, const char *name )
712 char *path;
713 unsigned int filter;
714 struct inode *inode;
715 struct stat st;
716 int wd = -1;
718 path = inode_get_path( parent, strlen(name) );
719 if (!path)
720 return;
722 strcat( path, name );
724 if (stat( path, &st ) < 0)
725 goto end;
727 filter = filter_from_inode( parent, 1 );
728 if (!filter)
729 goto end;
731 inode = inode_add( parent, st.st_dev, st.st_ino, name );
732 if (!inode || inode->wd != -1)
733 goto end;
735 wd = inotify_add_dir( path, filter );
736 if (wd != -1)
737 inode_set_wd( inode, wd );
738 else
739 free_inode( inode );
741 end:
742 free( path );
745 static int prepend( char **path, const char *segment )
747 int extra;
748 char *p;
750 extra = strlen( segment ) + 1;
751 if (*path)
753 int len = strlen( *path ) + 1;
754 p = realloc( *path, len + extra );
755 if (!p) return 0;
756 memmove( &p[ extra ], p, len );
757 p[ extra - 1 ] = '/';
758 memcpy( p, segment, extra - 1 );
760 else
762 p = malloc( extra );
763 if (!p) return 0;
764 memcpy( p, segment, extra );
767 *path = p;
769 return 1;
772 static void inotify_notify_all( struct inotify_event *ie )
774 unsigned int filter, action;
775 struct inode *inode, *i;
776 char *path = NULL;
777 struct dir *dir;
779 inode = inode_from_wd( ie->wd );
780 if (!inode)
782 fprintf( stderr, "no inode matches %d\n", ie->wd);
783 return;
786 filter = filter_from_event( ie );
788 if (ie->mask & IN_CREATE)
790 if (ie->mask & IN_ISDIR)
791 inode_check_dir( inode, ie->name );
793 action = FILE_ACTION_ADDED;
795 else if (ie->mask & IN_DELETE)
796 action = FILE_ACTION_REMOVED;
797 else if (ie->mask & IN_MOVED_FROM)
798 action = FILE_ACTION_RENAMED_OLD_NAME;
799 else if (ie->mask & IN_MOVED_TO)
800 action = FILE_ACTION_RENAMED_NEW_NAME;
801 else
802 action = FILE_ACTION_MODIFIED;
805 * Work our way up the inode hierarchy
806 * extending the relative path as we go
807 * and notifying all recursive watches.
809 if (!prepend( &path, ie->name ))
810 return;
812 for (i = inode; i; i = i->parent)
814 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
815 if ((filter & dir->filter) && (i==inode || dir->subtree))
816 inotify_do_change_notify( dir, action, ie->cookie, path );
818 if (!i->name || !prepend( &path, i->name ))
819 break;
822 free( path );
824 if (ie->mask & IN_DELETE)
826 i = inode_from_name( inode, ie->name );
827 if (i)
828 free_inode( i );
832 static void inotify_poll_event( struct fd *fd, int event )
834 int r, ofs, unix_fd;
835 char buffer[0x1000];
836 struct inotify_event *ie;
838 unix_fd = get_unix_fd( fd );
839 r = read( unix_fd, buffer, sizeof buffer );
840 if (r < 0)
842 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
843 return;
846 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
848 ie = (struct inotify_event*) &buffer[ofs];
849 if (!ie->len)
850 break;
851 ofs += offsetof( struct inotify_event, name[ie->len] );
852 if (ofs > r) break;
853 inotify_notify_all( ie );
857 static inline struct fd *create_inotify_fd( void )
859 int unix_fd;
861 unix_fd = inotify_init();
862 if (unix_fd<0)
863 return NULL;
864 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
867 static int map_flags( unsigned int filter )
869 unsigned int mask;
871 /* always watch these so we can track subdirectories in recursive watches */
872 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
874 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
875 mask |= IN_ATTRIB;
876 if (filter & FILE_NOTIFY_CHANGE_SIZE)
877 mask |= IN_MODIFY;
878 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
879 mask |= IN_MODIFY;
880 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
881 mask |= IN_ACCESS;
882 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
883 mask |= IN_ATTRIB;
885 return mask;
888 static int inotify_add_dir( char *path, unsigned int filter )
890 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
891 path, map_flags( filter ) );
892 if (wd != -1)
893 set_fd_events( inotify_fd, POLLIN );
894 return wd;
897 static int init_inotify( void )
899 int i;
901 if (inotify_fd)
902 return 1;
904 inotify_fd = create_inotify_fd();
905 if (!inotify_fd)
906 return 0;
908 for (i=0; i<HASH_SIZE; i++)
910 list_init( &inode_hash[i] );
911 list_init( &wd_hash[i] );
914 return 1;
917 static int inotify_adjust_changes( struct dir *dir )
919 unsigned int filter;
920 struct inode *inode;
921 struct stat st;
922 char path[32];
923 int wd, unix_fd;
925 if (!inotify_fd)
926 return 0;
928 unix_fd = get_unix_fd( dir->fd );
930 inode = dir->inode;
931 if (!inode)
933 /* check if this fd is already being watched */
934 if (-1 == fstat( unix_fd, &st ))
935 return 0;
937 inode = get_inode( st.st_dev, st.st_ino );
938 if (!inode)
939 inode = create_inode( st.st_dev, st.st_ino );
940 if (!inode)
941 return 0;
942 list_add_tail( &inode->dirs, &dir->in_entry );
943 dir->inode = inode;
946 filter = filter_from_inode( inode, 0 );
948 sprintf( path, "/proc/self/fd/%u", unix_fd );
949 wd = inotify_add_dir( path, filter );
950 if (wd == -1) return 0;
952 inode_set_wd( inode, wd );
954 return 1;
957 static char *get_basename( const char *link )
959 char *buffer, *name = NULL;
960 int r, n = 0x100;
962 while (1)
964 buffer = malloc( n );
965 if (!buffer) return NULL;
967 r = readlink( link, buffer, n );
968 if (r < 0)
969 break;
971 if (r < n)
973 name = buffer;
974 break;
976 free( buffer );
977 n *= 2;
980 if (name)
982 while (r > 0 && name[ r - 1 ] == '/' )
983 r--;
984 name[ r ] = 0;
986 name = strrchr( name, '/' );
987 if (name)
988 name = strdup( &name[1] );
991 free( buffer );
992 return name;
995 static int dir_add_to_existing_notify( struct dir *dir )
997 struct inode *inode, *parent;
998 unsigned int filter = 0;
999 struct stat st, st_new;
1000 char link[35], *name;
1001 int wd, unix_fd;
1003 if (!inotify_fd)
1004 return 0;
1006 unix_fd = get_unix_fd( dir->fd );
1008 /* check if it's in the list of inodes we want to watch */
1009 if (-1 == fstat( unix_fd, &st_new ))
1010 return 0;
1011 inode = find_inode( st_new.st_dev, st_new.st_ino );
1012 if (inode)
1013 return 0;
1015 /* lookup the parent */
1016 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1017 if (-1 == stat( link, &st ))
1018 return 0;
1021 * If there's no parent, stop. We could keep going adding
1022 * ../ to the path until we hit the root of the tree or
1023 * find a recursively watched ancestor.
1024 * Assume it's too expensive to search up the tree for now.
1026 parent = find_inode( st.st_dev, st.st_ino );
1027 if (!parent)
1028 return 0;
1030 if (parent->wd == -1)
1031 return 0;
1033 filter = filter_from_inode( parent, 1 );
1034 if (!filter)
1035 return 0;
1037 sprintf( link, "/proc/self/fd/%u", unix_fd );
1038 name = get_basename( link );
1039 if (!name)
1040 return 0;
1041 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1042 free( name );
1043 if (!inode)
1044 return 0;
1046 /* Couldn't find this inode at the start of the function, must be new */
1047 assert( inode->wd == -1 );
1049 wd = inotify_add_dir( link, filter );
1050 if (wd != -1)
1051 inode_set_wd( inode, wd );
1053 return 1;
1056 #else
1058 static int init_inotify( void )
1060 return 0;
1063 static int inotify_adjust_changes( struct dir *dir )
1065 return 0;
1068 static void free_inode( struct inode *inode )
1070 assert( 0 );
1073 static int dir_add_to_existing_notify( struct dir *dir )
1075 return 0;
1078 #endif /* USE_INOTIFY */
1080 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1082 struct dir *dir;
1084 dir = alloc_object( &dir_ops );
1085 if (!dir)
1086 return NULL;
1088 list_init( &dir->change_records );
1089 dir->filter = 0;
1090 dir->notified = 0;
1091 dir->want_data = 0;
1092 dir->inode = NULL;
1093 grab_object( fd );
1094 dir->fd = fd;
1095 dir->mode = mode;
1096 dir->uid = ~(uid_t)0;
1097 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1099 dir_add_to_existing_notify( dir );
1101 return &dir->obj;
1104 /* enable change notifications for a directory */
1105 DECL_HANDLER(read_directory_changes)
1107 struct dir *dir;
1108 struct async *async;
1110 if (!req->filter)
1112 set_error(STATUS_INVALID_PARAMETER);
1113 return;
1116 dir = get_dir_obj( current->process, req->async.handle, 0 );
1117 if (!dir)
1118 return;
1120 /* requests don't timeout */
1121 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1123 /* assign it once */
1124 if (!dir->filter)
1126 init_inotify();
1127 insert_change( dir );
1128 dir->filter = req->filter;
1129 dir->subtree = req->subtree;
1130 dir->want_data = req->want_data;
1133 /* if there's already a change in the queue, send it */
1134 if (!list_empty( &dir->change_records ))
1135 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1137 /* setup the real notification */
1138 if (!inotify_adjust_changes( dir ))
1139 dnotify_adjust_changes( dir );
1141 release_object( async );
1142 set_error(STATUS_PENDING);
1144 end:
1145 release_object( dir );
1148 DECL_HANDLER(read_change)
1150 struct change_record *record, *next;
1151 struct dir *dir;
1152 struct list events;
1153 char *data, *event;
1154 int size = 0;
1156 dir = get_dir_obj( current->process, req->handle, 0 );
1157 if (!dir)
1158 return;
1160 list_init( &events );
1161 list_move_tail( &events, &dir->change_records );
1162 release_object( dir );
1164 if (list_empty( &events ))
1166 set_error( STATUS_NO_DATA_DETECTED );
1167 return;
1170 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1172 size += (offsetof(struct filesystem_event, name[record->event.len])
1173 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1176 if (size > get_reply_max_size())
1177 set_error( STATUS_BUFFER_TOO_SMALL );
1178 else if ((data = mem_alloc( size )) != NULL)
1180 event = data;
1181 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1183 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1185 /* FIXME: rename events are sometimes reported as delete/create */
1186 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1188 struct list *elem = list_next( &events, &record->entry );
1189 if (elem)
1190 next = LIST_ENTRY(elem, struct change_record, entry);
1192 if (elem && next->cookie == record->cookie)
1193 next->cookie = 0;
1194 else
1195 record->event.action = FILE_ACTION_REMOVED;
1197 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1198 record->event.action = FILE_ACTION_ADDED;
1200 memcpy( event, &record->event, len );
1201 event += len;
1202 if (len % sizeof(int))
1204 memset( event, 0, sizeof(int) - len % sizeof(int) );
1205 event += sizeof(int) - len % sizeof(int);
1208 set_reply_data_ptr( data, size );
1211 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1213 list_remove( &record->entry );
1214 free( record );