DIB Engine: implement AlphaBlend
[wine/hacks.git] / server / change.c
blobc19f7eb3709d786ce1841595628f57f42797adec
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_async_terminated, /* async_terminated */
207 default_fd_cancel_async /* cancel_async */
210 static struct list change_list = LIST_INIT(change_list);
212 static void dnotify_adjust_changes( struct dir *dir )
214 #if defined(F_SETSIG) && defined(F_NOTIFY)
215 int fd = get_unix_fd( dir->fd );
216 unsigned int filter = dir->filter;
217 unsigned int val;
218 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
219 return;
221 val = DN_MULTISHOT;
222 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
223 val |= DN_RENAME | DN_DELETE | DN_CREATE;
224 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
225 val |= DN_RENAME | DN_DELETE | DN_CREATE;
226 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
227 val |= DN_ATTRIB;
228 if (filter & FILE_NOTIFY_CHANGE_SIZE)
229 val |= DN_MODIFY;
230 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
231 val |= DN_MODIFY;
232 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
233 val |= DN_ACCESS;
234 if (filter & FILE_NOTIFY_CHANGE_CREATION)
235 val |= DN_CREATE;
236 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
237 val |= DN_ATTRIB;
238 fcntl( fd, F_NOTIFY, val );
239 #endif
242 /* insert change in the global list */
243 static inline void insert_change( struct dir *dir )
245 sigset_t sigset;
247 sigemptyset( &sigset );
248 sigaddset( &sigset, SIGIO );
249 sigprocmask( SIG_BLOCK, &sigset, NULL );
250 list_add_head( &change_list, &dir->entry );
251 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
254 /* remove change from the global list */
255 static inline void remove_change( struct dir *dir )
257 sigset_t sigset;
259 sigemptyset( &sigset );
260 sigaddset( &sigset, SIGIO );
261 sigprocmask( SIG_BLOCK, &sigset, NULL );
262 list_remove( &dir->entry );
263 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
266 static void dir_dump( struct object *obj, int verbose )
268 struct dir *dir = (struct dir *)obj;
269 assert( obj->ops == &dir_ops );
270 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
273 /* enter here directly from SIGIO signal handler */
274 void do_change_notify( int unix_fd )
276 struct dir *dir;
278 /* FIXME: this is O(n) ... probably can be improved */
279 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
281 if (get_unix_fd( dir->fd ) != unix_fd) continue;
282 interlocked_xchg_add( &dir->notified, 1 );
283 break;
287 /* SIGIO callback, called synchronously with the poll loop */
288 void sigio_callback(void)
290 struct dir *dir;
292 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
294 if (interlocked_xchg( &dir->notified, 0 ))
295 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
299 static struct fd *dir_get_fd( struct object *obj )
301 struct dir *dir = (struct dir *)obj;
302 assert( obj->ops == &dir_ops );
303 return (struct fd *)grab_object( dir->fd );
306 static int get_dir_unix_fd( struct dir *dir )
308 return get_unix_fd( dir->fd );
311 static struct security_descriptor *dir_get_sd( struct object *obj )
313 struct dir *dir = (struct dir *)obj;
314 int unix_fd;
315 struct stat st;
316 struct security_descriptor *sd;
317 assert( obj->ops == &dir_ops );
319 unix_fd = get_dir_unix_fd( dir );
321 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
322 return obj->sd;
324 /* mode and uid the same? if so, no need to re-generate security descriptor */
325 if (obj->sd &&
326 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
327 (st.st_uid == dir->uid))
328 return obj->sd;
330 sd = mode_to_sd( st.st_mode,
331 security_unix_uid_to_sid( st.st_uid ),
332 token_get_primary_group( current->process->token ));
333 if (!sd) return obj->sd;
335 dir->mode = st.st_mode;
336 dir->uid = st.st_uid;
337 free( obj->sd );
338 obj->sd = sd;
339 return sd;
342 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
343 unsigned int set_info )
345 struct dir *dir = (struct dir *)obj;
346 const SID *owner;
347 struct stat st;
348 mode_t mode;
349 int unix_fd;
351 assert( obj->ops == &dir_ops );
353 unix_fd = get_dir_unix_fd( dir );
355 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
357 if (set_info & OWNER_SECURITY_INFORMATION)
359 owner = sd_get_owner( sd );
360 if (!owner)
362 set_error( STATUS_INVALID_SECURITY_DESCR );
363 return 0;
365 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
367 /* FIXME: get Unix uid and call fchown */
370 else if (obj->sd)
371 owner = sd_get_owner( obj->sd );
372 else
373 owner = token_get_user( current->process->token );
375 if (set_info & DACL_SECURITY_INFORMATION)
377 /* keep the bits that we don't map to access rights in the ACL */
378 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
379 mode |= sd_to_mode( sd, owner );
381 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
383 file_set_error();
384 return 0;
387 return 1;
390 static struct change_record *get_first_change_record( struct dir *dir )
392 struct list *ptr = list_head( &dir->change_records );
393 if (!ptr) return NULL;
394 list_remove( ptr );
395 return LIST_ENTRY( ptr, struct change_record, entry );
398 static void dir_destroy( struct object *obj )
400 struct change_record *record;
401 struct dir *dir = (struct dir *)obj;
402 assert (obj->ops == &dir_ops);
404 if (dir->filter)
405 remove_change( dir );
407 if (dir->inode)
409 list_remove( &dir->in_entry );
410 free_inode( dir->inode );
413 while ((record = get_first_change_record( dir ))) free( record );
415 release_object( dir->fd );
417 if (inotify_fd && list_empty( &change_list ))
419 release_object( inotify_fd );
420 inotify_fd = NULL;
424 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
426 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
429 static int dir_get_poll_events( struct fd *fd )
431 return 0;
434 static enum server_fd_type dir_get_fd_type( struct fd *fd )
436 return FD_TYPE_DIR;
439 #ifdef USE_INOTIFY
441 #define HASH_SIZE 31
443 struct inode {
444 struct list ch_entry; /* entry in the children list */
445 struct list children; /* children of this inode */
446 struct inode *parent; /* parent of this inode */
447 struct list dirs; /* directory handles watching this inode */
448 struct list ino_entry; /* entry in the inode hash */
449 struct list wd_entry; /* entry in the watch descriptor hash */
450 dev_t dev; /* device number */
451 ino_t ino; /* device's inode number */
452 int wd; /* inotify's watch descriptor */
453 char *name; /* basename name of the inode */
456 static struct list inode_hash[ HASH_SIZE ];
457 static struct list wd_hash[ HASH_SIZE ];
459 static int inotify_add_dir( char *path, unsigned int filter );
461 static struct inode *inode_from_wd( int wd )
463 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
464 struct inode *inode;
466 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
467 if (inode->wd == wd)
468 return inode;
470 return NULL;
473 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
475 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
478 static struct inode *find_inode( dev_t dev, ino_t ino )
480 struct list *bucket = get_hash_list( dev, ino );
481 struct inode *inode;
483 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
484 if (inode->ino == ino && inode->dev == dev)
485 return inode;
487 return NULL;
490 static struct inode *create_inode( dev_t dev, ino_t ino )
492 struct inode *inode;
494 inode = malloc( sizeof *inode );
495 if (inode)
497 list_init( &inode->children );
498 list_init( &inode->dirs );
499 inode->ino = ino;
500 inode->dev = dev;
501 inode->wd = -1;
502 inode->parent = NULL;
503 inode->name = NULL;
504 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
506 return inode;
509 static struct inode *get_inode( dev_t dev, ino_t ino )
511 struct inode *inode;
513 inode = find_inode( dev, ino );
514 if (inode)
515 return inode;
516 return create_inode( dev, ino );
519 static void inode_set_wd( struct inode *inode, int wd )
521 if (inode->wd != -1)
522 list_remove( &inode->wd_entry );
523 inode->wd = wd;
524 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
527 static void inode_set_name( struct inode *inode, const char *name )
529 free (inode->name);
530 inode->name = name ? strdup( name ) : NULL;
533 static void free_inode( struct inode *inode )
535 int subtree = 0, watches = 0;
536 struct inode *tmp, *next;
537 struct dir *dir;
539 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
541 subtree |= dir->subtree;
542 watches++;
545 if (!subtree && !inode->parent)
547 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
548 struct inode, ch_entry )
550 assert( tmp != inode );
551 assert( tmp->parent == inode );
552 free_inode( tmp );
556 if (watches)
557 return;
559 if (inode->parent)
560 list_remove( &inode->ch_entry );
562 /* disconnect remaining children from the parent */
563 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
565 list_remove( &tmp->ch_entry );
566 tmp->parent = NULL;
569 if (inode->wd != -1)
571 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
572 list_remove( &inode->wd_entry );
574 list_remove( &inode->ino_entry );
576 free( inode->name );
577 free( inode );
580 static struct inode *inode_add( struct inode *parent,
581 dev_t dev, ino_t ino, const char *name )
583 struct inode *inode;
585 inode = get_inode( dev, ino );
586 if (!inode)
587 return NULL;
589 if (!inode->parent)
591 list_add_tail( &parent->children, &inode->ch_entry );
592 inode->parent = parent;
593 assert( inode != parent );
595 inode_set_name( inode, name );
597 return inode;
600 static struct inode *inode_from_name( struct inode *inode, const char *name )
602 struct inode *i;
604 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
605 if (i->name && !strcmp( i->name, name ))
606 return i;
607 return NULL;
610 static int inotify_get_poll_events( struct fd *fd );
611 static void inotify_poll_event( struct fd *fd, int event );
613 static const struct fd_ops inotify_fd_ops =
615 inotify_get_poll_events, /* get_poll_events */
616 inotify_poll_event, /* poll_event */
617 NULL, /* flush */
618 NULL, /* get_fd_type */
619 NULL, /* removable */
620 NULL, /* ioctl */
621 NULL, /* queue_async */
622 NULL, /* async_event */
623 NULL, /* async_terminated */
624 NULL /* cancel_async */
627 static int inotify_get_poll_events( struct fd *fd )
629 return POLLIN;
632 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
633 const char *relpath )
635 struct change_record *record;
637 assert( dir->obj.ops == &dir_ops );
639 if (dir->want_data)
641 size_t len = strlen(relpath);
642 record = malloc( offsetof(struct change_record, name[len]) );
643 if (!record)
644 return;
646 record->action = action;
647 memcpy( record->name, relpath, len );
648 record->len = len;
650 list_add_tail( &dir->change_records, &record->entry );
653 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
656 static unsigned int filter_from_event( struct inotify_event *ie )
658 unsigned int filter = 0;
660 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
661 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
662 if (ie->mask & IN_MODIFY)
663 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
664 if (ie->mask & IN_ATTRIB)
665 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
666 if (ie->mask & IN_ACCESS)
667 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
668 if (ie->mask & IN_CREATE)
669 filter |= FILE_NOTIFY_CHANGE_CREATION;
671 return filter;
674 /* scan up the parent directories for watches */
675 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
677 unsigned int filter = 0;
678 struct dir *dir;
680 /* combine filters from parents watching subtrees */
681 while (inode)
683 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
684 if (dir->subtree || !is_parent)
685 filter |= dir->filter;
686 is_parent = 1;
687 inode = inode->parent;
690 return filter;
693 static char *inode_get_path( struct inode *inode, int sz )
695 struct list *head;
696 char *path;
697 int len;
699 if (!inode)
700 return NULL;
702 head = list_head( &inode->dirs );
703 if (head)
705 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
706 path = malloc ( 32 + sz );
707 if (path)
708 sprintf( path, "/proc/self/fd/%u/", unix_fd );
709 return path;
712 if (!inode->name)
713 return NULL;
715 len = strlen( inode->name );
716 path = inode_get_path( inode->parent, sz + len + 1 );
717 if (!path)
718 return NULL;
720 strcat( path, inode->name );
721 strcat( path, "/" );
723 return path;
726 static int inode_check_dir( struct inode *parent, const char *name )
728 char *path;
729 unsigned int filter;
730 struct inode *inode;
731 struct stat st;
732 int wd = -1, r = -1;
734 path = inode_get_path( parent, strlen(name) );
735 if (!path)
736 return r;
738 strcat( path, name );
740 r = stat( path, &st );
741 if (r < 0) goto end;
743 if (!S_ISDIR(st.st_mode))
745 r = 0;
746 goto end;
749 r = 1;
751 filter = filter_from_inode( parent, 1 );
752 if (!filter)
753 goto end;
755 inode = inode_add( parent, st.st_dev, st.st_ino, name );
756 if (!inode || inode->wd != -1)
757 goto end;
759 wd = inotify_add_dir( path, filter );
760 if (wd != -1)
761 inode_set_wd( inode, wd );
762 else
763 free_inode( inode );
765 end:
766 free( path );
767 return r;
770 static int prepend( char **path, const char *segment )
772 int extra;
773 char *p;
775 extra = strlen( segment ) + 1;
776 if (*path)
778 int len = strlen( *path ) + 1;
779 p = realloc( *path, len + extra );
780 if (!p) return 0;
781 memmove( &p[ extra ], p, len );
782 p[ extra - 1 ] = '/';
783 memcpy( p, segment, extra - 1 );
785 else
787 p = malloc( extra );
788 if (!p) return 0;
789 memcpy( p, segment, extra );
792 *path = p;
794 return 1;
797 static void inotify_notify_all( struct inotify_event *ie )
799 unsigned int filter, action;
800 struct inode *inode, *i;
801 char *path = NULL;
802 struct dir *dir;
804 inode = inode_from_wd( ie->wd );
805 if (!inode)
807 fprintf( stderr, "no inode matches %d\n", ie->wd);
808 return;
811 filter = filter_from_event( ie );
813 if (ie->mask & IN_CREATE)
815 switch (inode_check_dir( inode, ie->name ))
817 case 1:
818 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
819 break;
820 case 0:
821 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
822 break;
823 default:
824 break;
825 /* Maybe the file disappeared before we could check it? */
827 action = FILE_ACTION_ADDED;
829 else if (ie->mask & IN_DELETE)
830 action = FILE_ACTION_REMOVED;
831 else
832 action = FILE_ACTION_MODIFIED;
835 * Work our way up the inode hierarchy
836 * extending the relative path as we go
837 * and notifying all recursive watches.
839 if (!prepend( &path, ie->name ))
840 return;
842 for (i = inode; i; i = i->parent)
844 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
845 if ((filter & dir->filter) && (i==inode || dir->subtree))
846 inotify_do_change_notify( dir, action, path );
848 if (!i->name || !prepend( &path, i->name ))
849 break;
852 free( path );
854 if (ie->mask & IN_DELETE)
856 i = inode_from_name( inode, ie->name );
857 if (i)
858 free_inode( i );
862 static void inotify_poll_event( struct fd *fd, int event )
864 int r, ofs, unix_fd;
865 char buffer[0x1000];
866 struct inotify_event *ie;
868 unix_fd = get_unix_fd( fd );
869 r = read( unix_fd, buffer, sizeof buffer );
870 if (r < 0)
872 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
873 return;
876 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
878 ie = (struct inotify_event*) &buffer[ofs];
879 if (!ie->len)
880 break;
881 ofs += offsetof( struct inotify_event, name[ie->len] );
882 if (ofs > r) break;
883 inotify_notify_all( ie );
887 static inline struct fd *create_inotify_fd( void )
889 int unix_fd;
891 unix_fd = inotify_init();
892 if (unix_fd<0)
893 return NULL;
894 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
897 static int map_flags( unsigned int filter )
899 unsigned int mask;
901 /* always watch these so we can track subdirectories in recursive watches */
902 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
904 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
905 mask |= IN_ATTRIB;
906 if (filter & FILE_NOTIFY_CHANGE_SIZE)
907 mask |= IN_MODIFY;
908 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
909 mask |= IN_MODIFY;
910 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
911 mask |= IN_ACCESS;
912 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
913 mask |= IN_ATTRIB;
915 return mask;
918 static int inotify_add_dir( char *path, unsigned int filter )
920 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
921 path, map_flags( filter ) );
922 if (wd != -1)
923 set_fd_events( inotify_fd, POLLIN );
924 return wd;
927 static int init_inotify( void )
929 int i;
931 if (inotify_fd)
932 return 1;
934 inotify_fd = create_inotify_fd();
935 if (!inotify_fd)
936 return 0;
938 for (i=0; i<HASH_SIZE; i++)
940 list_init( &inode_hash[i] );
941 list_init( &wd_hash[i] );
944 return 1;
947 static int inotify_adjust_changes( struct dir *dir )
949 unsigned int filter;
950 struct inode *inode;
951 struct stat st;
952 char path[32];
953 int wd, unix_fd;
955 if (!inotify_fd)
956 return 0;
958 unix_fd = get_unix_fd( dir->fd );
960 inode = dir->inode;
961 if (!inode)
963 /* check if this fd is already being watched */
964 if (-1 == fstat( unix_fd, &st ))
965 return 0;
967 inode = get_inode( st.st_dev, st.st_ino );
968 if (!inode)
969 inode = create_inode( st.st_dev, st.st_ino );
970 if (!inode)
971 return 0;
972 list_add_tail( &inode->dirs, &dir->in_entry );
973 dir->inode = inode;
976 filter = filter_from_inode( inode, 0 );
978 sprintf( path, "/proc/self/fd/%u", unix_fd );
979 wd = inotify_add_dir( path, filter );
980 if (wd == -1) return 0;
982 inode_set_wd( inode, wd );
984 return 1;
987 static char *get_basename( const char *link )
989 char *buffer, *name = NULL;
990 int r, n = 0x100;
992 while (1)
994 buffer = malloc( n );
995 if (!buffer) return NULL;
997 r = readlink( link, buffer, n );
998 if (r < 0)
999 break;
1001 if (r < n)
1003 name = buffer;
1004 break;
1006 free( buffer );
1007 n *= 2;
1010 if (name)
1012 while (r > 0 && name[ r - 1 ] == '/' )
1013 r--;
1014 name[ r ] = 0;
1016 name = strrchr( name, '/' );
1017 if (name)
1018 name = strdup( &name[1] );
1021 free( buffer );
1022 return name;
1025 static int dir_add_to_existing_notify( struct dir *dir )
1027 struct inode *inode, *parent;
1028 unsigned int filter = 0;
1029 struct stat st, st_new;
1030 char link[35], *name;
1031 int wd, unix_fd;
1033 if (!inotify_fd)
1034 return 0;
1036 unix_fd = get_unix_fd( dir->fd );
1038 /* check if it's in the list of inodes we want to watch */
1039 if (-1 == fstat( unix_fd, &st_new ))
1040 return 0;
1041 inode = find_inode( st_new.st_dev, st_new.st_ino );
1042 if (inode)
1043 return 0;
1045 /* lookup the parent */
1046 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1047 if (-1 == stat( link, &st ))
1048 return 0;
1051 * If there's no parent, stop. We could keep going adding
1052 * ../ to the path until we hit the root of the tree or
1053 * find a recursively watched ancestor.
1054 * Assume it's too expensive to search up the tree for now.
1056 parent = find_inode( st.st_dev, st.st_ino );
1057 if (!parent)
1058 return 0;
1060 if (parent->wd == -1)
1061 return 0;
1063 filter = filter_from_inode( parent, 1 );
1064 if (!filter)
1065 return 0;
1067 sprintf( link, "/proc/self/fd/%u", unix_fd );
1068 name = get_basename( link );
1069 if (!name)
1070 return 0;
1071 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1072 free( name );
1073 if (!inode)
1074 return 0;
1076 /* Couldn't find this inode at the start of the function, must be new */
1077 assert( inode->wd == -1 );
1079 wd = inotify_add_dir( link, filter );
1080 if (wd != -1)
1081 inode_set_wd( inode, wd );
1083 return 1;
1086 #else
1088 static int init_inotify( void )
1090 return 0;
1093 static int inotify_adjust_changes( struct dir *dir )
1095 return 0;
1098 static void free_inode( struct inode *inode )
1100 assert( 0 );
1103 static int dir_add_to_existing_notify( struct dir *dir )
1105 return 0;
1108 #endif /* USE_INOTIFY */
1110 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1112 struct dir *dir;
1114 dir = alloc_object( &dir_ops );
1115 if (!dir)
1116 return NULL;
1118 list_init( &dir->change_records );
1119 dir->filter = 0;
1120 dir->notified = 0;
1121 dir->want_data = 0;
1122 dir->inode = NULL;
1123 grab_object( fd );
1124 dir->fd = fd;
1125 dir->mode = mode;
1126 dir->uid = ~(uid_t)0;
1127 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1129 dir_add_to_existing_notify( dir );
1131 return &dir->obj;
1134 /* enable change notifications for a directory */
1135 DECL_HANDLER(read_directory_changes)
1137 struct dir *dir;
1138 struct async *async;
1140 if (!req->filter)
1142 set_error(STATUS_INVALID_PARAMETER);
1143 return;
1146 dir = get_dir_obj( current->process, req->async.handle, 0 );
1147 if (!dir)
1148 return;
1150 /* requests don't timeout */
1151 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1153 /* assign it once */
1154 if (!dir->filter)
1156 init_inotify();
1157 insert_change( dir );
1158 dir->filter = req->filter;
1159 dir->subtree = req->subtree;
1160 dir->want_data = req->want_data;
1163 /* if there's already a change in the queue, send it */
1164 if (!list_empty( &dir->change_records ))
1165 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1167 /* setup the real notification */
1168 if (!inotify_adjust_changes( dir ))
1169 dnotify_adjust_changes( dir );
1171 release_object( async );
1172 set_error(STATUS_PENDING);
1174 end:
1175 release_object( dir );
1178 DECL_HANDLER(read_change)
1180 struct change_record *record;
1181 struct dir *dir;
1183 dir = get_dir_obj( current->process, req->handle, 0 );
1184 if (!dir)
1185 return;
1187 if ((record = get_first_change_record( dir )) != NULL)
1189 reply->action = record->action;
1190 set_reply_data( record->name, record->len );
1191 free( record );
1193 else
1194 set_error( STATUS_NO_DATA_DETECTED );
1196 release_object( dir );