wined3d: Use the texture dimension helpers in texture3d_prepare_texture().
[wine.git] / server / change.c
blob36a1997673f2eef8ba7c81683ef8bdf791fe06a1
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 struct object_type *dir_get_type( struct object *obj );
152 static void dir_destroy( struct object *obj );
154 static const struct object_ops dir_ops =
156 sizeof(struct dir), /* size */
157 dir_dump, /* dump */
158 dir_get_type, /* get_type */
159 add_queue, /* add_queue */
160 remove_queue, /* remove_queue */
161 default_fd_signaled, /* signaled */
162 no_satisfied, /* satisfied */
163 no_signal, /* signal */
164 dir_get_fd, /* get_fd */
165 default_fd_map_access, /* map_access */
166 dir_get_sd, /* get_sd */
167 dir_set_sd, /* set_sd */
168 no_lookup_name, /* lookup_name */
169 no_link_name, /* link_name */
170 NULL, /* unlink_name */
171 no_open_file, /* open_file */
172 fd_close_handle, /* close_handle */
173 dir_destroy /* destroy */
176 static int dir_get_poll_events( struct fd *fd );
177 static enum server_fd_type dir_get_fd_type( struct fd *fd );
179 static const struct fd_ops dir_fd_ops =
181 dir_get_poll_events, /* get_poll_events */
182 default_poll_event, /* poll_event */
183 dir_get_fd_type, /* get_fd_type */
184 no_fd_read, /* read */
185 no_fd_write, /* write */
186 no_fd_flush, /* flush */
187 default_fd_ioctl, /* ioctl */
188 default_fd_queue_async, /* queue_async */
189 default_fd_reselect_async, /* reselect_async */
190 default_fd_cancel_async /* cancel_async */
193 static struct list change_list = LIST_INIT(change_list);
195 static void dnotify_adjust_changes( struct dir *dir )
197 #if defined(F_SETSIG) && defined(F_NOTIFY)
198 int fd = get_unix_fd( dir->fd );
199 unsigned int filter = dir->filter;
200 unsigned int val;
201 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
202 return;
204 val = DN_MULTISHOT;
205 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
206 val |= DN_RENAME | DN_DELETE | DN_CREATE;
207 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
208 val |= DN_RENAME | DN_DELETE | DN_CREATE;
209 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
210 val |= DN_ATTRIB;
211 if (filter & FILE_NOTIFY_CHANGE_SIZE)
212 val |= DN_MODIFY;
213 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
214 val |= DN_MODIFY;
215 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
216 val |= DN_ACCESS;
217 if (filter & FILE_NOTIFY_CHANGE_CREATION)
218 val |= DN_CREATE;
219 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
220 val |= DN_ATTRIB;
221 fcntl( fd, F_NOTIFY, val );
222 #endif
225 /* insert change in the global list */
226 static inline void insert_change( struct dir *dir )
228 sigset_t sigset;
230 sigemptyset( &sigset );
231 sigaddset( &sigset, SIGIO );
232 sigprocmask( SIG_BLOCK, &sigset, NULL );
233 list_add_head( &change_list, &dir->entry );
234 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
237 /* remove change from the global list */
238 static inline void remove_change( struct dir *dir )
240 sigset_t sigset;
242 sigemptyset( &sigset );
243 sigaddset( &sigset, SIGIO );
244 sigprocmask( SIG_BLOCK, &sigset, NULL );
245 list_remove( &dir->entry );
246 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
249 static void dir_dump( struct object *obj, int verbose )
251 struct dir *dir = (struct dir *)obj;
252 assert( obj->ops == &dir_ops );
253 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
256 static struct object_type *dir_get_type( struct object *obj )
258 static const WCHAR name[] = {'F','i','l','e'};
259 static const struct unicode_str str = { name, sizeof(name) };
260 return get_object_type( &str );
263 /* enter here directly from SIGIO signal handler */
264 void do_change_notify( int unix_fd )
266 struct dir *dir;
268 /* FIXME: this is O(n) ... probably can be improved */
269 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
271 if (get_unix_fd( dir->fd ) != unix_fd) continue;
272 interlocked_xchg_add( &dir->notified, 1 );
273 break;
277 /* SIGIO callback, called synchronously with the poll loop */
278 void sigio_callback(void)
280 struct dir *dir;
282 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
284 if (interlocked_xchg( &dir->notified, 0 ))
285 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
289 static struct fd *dir_get_fd( struct object *obj )
291 struct dir *dir = (struct dir *)obj;
292 assert( obj->ops == &dir_ops );
293 return (struct fd *)grab_object( dir->fd );
296 static int get_dir_unix_fd( struct dir *dir )
298 return get_unix_fd( dir->fd );
301 static struct security_descriptor *dir_get_sd( struct object *obj )
303 struct dir *dir = (struct dir *)obj;
304 int unix_fd;
305 struct stat st;
306 struct security_descriptor *sd;
307 assert( obj->ops == &dir_ops );
309 unix_fd = get_dir_unix_fd( dir );
311 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
312 return obj->sd;
314 /* mode and uid the same? if so, no need to re-generate security descriptor */
315 if (obj->sd &&
316 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
317 (st.st_uid == dir->uid))
318 return obj->sd;
320 sd = mode_to_sd( st.st_mode,
321 security_unix_uid_to_sid( st.st_uid ),
322 token_get_primary_group( current->process->token ));
323 if (!sd) return obj->sd;
325 dir->mode = st.st_mode;
326 dir->uid = st.st_uid;
327 free( obj->sd );
328 obj->sd = sd;
329 return sd;
332 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
333 unsigned int set_info )
335 struct dir *dir = (struct dir *)obj;
336 const SID *owner;
337 struct stat st;
338 mode_t mode;
339 int unix_fd;
341 assert( obj->ops == &dir_ops );
343 unix_fd = get_dir_unix_fd( dir );
345 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
347 if (set_info & OWNER_SECURITY_INFORMATION)
349 owner = sd_get_owner( sd );
350 if (!owner)
352 set_error( STATUS_INVALID_SECURITY_DESCR );
353 return 0;
355 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
357 /* FIXME: get Unix uid and call fchown */
360 else if (obj->sd)
361 owner = sd_get_owner( obj->sd );
362 else
363 owner = token_get_user( current->process->token );
365 if (set_info & DACL_SECURITY_INFORMATION)
367 /* keep the bits that we don't map to access rights in the ACL */
368 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
369 mode |= sd_to_mode( sd, owner );
371 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
373 file_set_error();
374 return 0;
377 return 1;
380 static struct change_record *get_first_change_record( struct dir *dir )
382 struct list *ptr = list_head( &dir->change_records );
383 if (!ptr) return NULL;
384 list_remove( ptr );
385 return LIST_ENTRY( ptr, struct change_record, entry );
388 static void dir_destroy( struct object *obj )
390 struct change_record *record;
391 struct dir *dir = (struct dir *)obj;
392 assert (obj->ops == &dir_ops);
394 if (dir->filter)
395 remove_change( dir );
397 if (dir->inode)
399 list_remove( &dir->in_entry );
400 free_inode( dir->inode );
403 while ((record = get_first_change_record( dir ))) free( record );
405 release_object( dir->fd );
407 if (inotify_fd && list_empty( &change_list ))
409 release_object( inotify_fd );
410 inotify_fd = NULL;
414 struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
416 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
419 static int dir_get_poll_events( struct fd *fd )
421 return 0;
424 static enum server_fd_type dir_get_fd_type( struct fd *fd )
426 return FD_TYPE_DIR;
429 #ifdef USE_INOTIFY
431 #define HASH_SIZE 31
433 struct inode {
434 struct list ch_entry; /* entry in the children list */
435 struct list children; /* children of this inode */
436 struct inode *parent; /* parent of this inode */
437 struct list dirs; /* directory handles watching this inode */
438 struct list ino_entry; /* entry in the inode hash */
439 struct list wd_entry; /* entry in the watch descriptor hash */
440 dev_t dev; /* device number */
441 ino_t ino; /* device's inode number */
442 int wd; /* inotify's watch descriptor */
443 char *name; /* basename name of the inode */
446 static struct list inode_hash[ HASH_SIZE ];
447 static struct list wd_hash[ HASH_SIZE ];
449 static int inotify_add_dir( char *path, unsigned int filter );
451 static struct inode *inode_from_wd( int wd )
453 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
454 struct inode *inode;
456 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
457 if (inode->wd == wd)
458 return inode;
460 return NULL;
463 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
465 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
468 static struct inode *find_inode( dev_t dev, ino_t ino )
470 struct list *bucket = get_hash_list( dev, ino );
471 struct inode *inode;
473 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
474 if (inode->ino == ino && inode->dev == dev)
475 return inode;
477 return NULL;
480 static struct inode *create_inode( dev_t dev, ino_t ino )
482 struct inode *inode;
484 inode = malloc( sizeof *inode );
485 if (inode)
487 list_init( &inode->children );
488 list_init( &inode->dirs );
489 inode->ino = ino;
490 inode->dev = dev;
491 inode->wd = -1;
492 inode->parent = NULL;
493 inode->name = NULL;
494 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
496 return inode;
499 static struct inode *get_inode( dev_t dev, ino_t ino )
501 struct inode *inode;
503 inode = find_inode( dev, ino );
504 if (inode)
505 return inode;
506 return create_inode( dev, ino );
509 static void inode_set_wd( struct inode *inode, int wd )
511 if (inode->wd != -1)
512 list_remove( &inode->wd_entry );
513 inode->wd = wd;
514 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
517 static void inode_set_name( struct inode *inode, const char *name )
519 free (inode->name);
520 inode->name = name ? strdup( name ) : NULL;
523 static void free_inode( struct inode *inode )
525 int subtree = 0, watches = 0;
526 struct inode *tmp, *next;
527 struct dir *dir;
529 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
531 subtree |= dir->subtree;
532 watches++;
535 if (!subtree && !inode->parent)
537 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
538 struct inode, ch_entry )
540 assert( tmp != inode );
541 assert( tmp->parent == inode );
542 free_inode( tmp );
546 if (watches)
547 return;
549 if (inode->parent)
550 list_remove( &inode->ch_entry );
552 /* disconnect remaining children from the parent */
553 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
555 list_remove( &tmp->ch_entry );
556 tmp->parent = NULL;
559 if (inode->wd != -1)
561 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
562 list_remove( &inode->wd_entry );
564 list_remove( &inode->ino_entry );
566 free( inode->name );
567 free( inode );
570 static struct inode *inode_add( struct inode *parent,
571 dev_t dev, ino_t ino, const char *name )
573 struct inode *inode;
575 inode = get_inode( dev, ino );
576 if (!inode)
577 return NULL;
579 if (!inode->parent)
581 list_add_tail( &parent->children, &inode->ch_entry );
582 inode->parent = parent;
583 assert( inode != parent );
585 inode_set_name( inode, name );
587 return inode;
590 static struct inode *inode_from_name( struct inode *inode, const char *name )
592 struct inode *i;
594 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
595 if (i->name && !strcmp( i->name, name ))
596 return i;
597 return NULL;
600 static int inotify_get_poll_events( struct fd *fd );
601 static void inotify_poll_event( struct fd *fd, int event );
603 static const struct fd_ops inotify_fd_ops =
605 inotify_get_poll_events, /* get_poll_events */
606 inotify_poll_event, /* poll_event */
607 NULL, /* flush */
608 NULL, /* get_fd_type */
609 NULL, /* ioctl */
610 NULL, /* queue_async */
611 NULL, /* reselect_async */
612 NULL, /* cancel_async */
615 static int inotify_get_poll_events( struct fd *fd )
617 return POLLIN;
620 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
621 unsigned int cookie, const char *relpath )
623 struct change_record *record;
625 assert( dir->obj.ops == &dir_ops );
627 if (dir->want_data)
629 size_t len = strlen(relpath);
630 record = malloc( offsetof(struct change_record, event.name[len]) );
631 if (!record)
632 return;
634 record->cookie = cookie;
635 record->event.action = action;
636 memcpy( record->event.name, relpath, len );
637 record->event.len = len;
639 list_add_tail( &dir->change_records, &record->entry );
642 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
645 static unsigned int filter_from_event( struct inotify_event *ie )
647 unsigned int filter = 0;
649 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
650 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
651 if (ie->mask & IN_MODIFY)
652 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
653 if (ie->mask & IN_ATTRIB)
654 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
655 if (ie->mask & IN_ACCESS)
656 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
657 if (ie->mask & IN_CREATE)
658 filter |= FILE_NOTIFY_CHANGE_CREATION;
660 if (ie->mask & IN_ISDIR)
661 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
662 else
663 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
665 return filter;
668 /* scan up the parent directories for watches */
669 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
671 unsigned int filter = 0;
672 struct dir *dir;
674 /* combine filters from parents watching subtrees */
675 while (inode)
677 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
678 if (dir->subtree || !is_parent)
679 filter |= dir->filter;
680 is_parent = 1;
681 inode = inode->parent;
684 return filter;
687 static char *inode_get_path( struct inode *inode, int sz )
689 struct list *head;
690 char *path;
691 int len;
693 if (!inode)
694 return NULL;
696 head = list_head( &inode->dirs );
697 if (head)
699 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
700 path = malloc ( 32 + sz );
701 if (path)
702 sprintf( path, "/proc/self/fd/%u/", unix_fd );
703 return path;
706 if (!inode->name)
707 return NULL;
709 len = strlen( inode->name );
710 path = inode_get_path( inode->parent, sz + len + 1 );
711 if (!path)
712 return NULL;
714 strcat( path, inode->name );
715 strcat( path, "/" );
717 return path;
720 static void inode_check_dir( struct inode *parent, const char *name )
722 char *path;
723 unsigned int filter;
724 struct inode *inode;
725 struct stat st;
726 int wd = -1;
728 path = inode_get_path( parent, strlen(name) );
729 if (!path)
730 return;
732 strcat( path, name );
734 if (stat( path, &st ) < 0)
735 goto end;
737 filter = filter_from_inode( parent, 1 );
738 if (!filter)
739 goto end;
741 inode = inode_add( parent, st.st_dev, st.st_ino, name );
742 if (!inode || inode->wd != -1)
743 goto end;
745 wd = inotify_add_dir( path, filter );
746 if (wd != -1)
747 inode_set_wd( inode, wd );
748 else
749 free_inode( inode );
751 end:
752 free( path );
755 static int prepend( char **path, const char *segment )
757 int extra;
758 char *p;
760 extra = strlen( segment ) + 1;
761 if (*path)
763 int len = strlen( *path ) + 1;
764 p = realloc( *path, len + extra );
765 if (!p) return 0;
766 memmove( &p[ extra ], p, len );
767 p[ extra - 1 ] = '/';
768 memcpy( p, segment, extra - 1 );
770 else
772 p = malloc( extra );
773 if (!p) return 0;
774 memcpy( p, segment, extra );
777 *path = p;
779 return 1;
782 static void inotify_notify_all( struct inotify_event *ie )
784 unsigned int filter, action;
785 struct inode *inode, *i;
786 char *path = NULL;
787 struct dir *dir;
789 inode = inode_from_wd( ie->wd );
790 if (!inode)
792 fprintf( stderr, "no inode matches %d\n", ie->wd);
793 return;
796 filter = filter_from_event( ie );
798 if (ie->mask & IN_CREATE)
800 if (ie->mask & IN_ISDIR)
801 inode_check_dir( inode, ie->name );
803 action = FILE_ACTION_ADDED;
805 else if (ie->mask & IN_DELETE)
806 action = FILE_ACTION_REMOVED;
807 else if (ie->mask & IN_MOVED_FROM)
808 action = FILE_ACTION_RENAMED_OLD_NAME;
809 else if (ie->mask & IN_MOVED_TO)
810 action = FILE_ACTION_RENAMED_NEW_NAME;
811 else
812 action = FILE_ACTION_MODIFIED;
815 * Work our way up the inode hierarchy
816 * extending the relative path as we go
817 * and notifying all recursive watches.
819 if (!prepend( &path, ie->name ))
820 return;
822 for (i = inode; i; i = i->parent)
824 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
825 if ((filter & dir->filter) && (i==inode || dir->subtree))
826 inotify_do_change_notify( dir, action, ie->cookie, path );
828 if (!i->name || !prepend( &path, i->name ))
829 break;
832 free( path );
834 if (ie->mask & IN_DELETE)
836 i = inode_from_name( inode, ie->name );
837 if (i)
838 free_inode( i );
842 static void inotify_poll_event( struct fd *fd, int event )
844 int r, ofs, unix_fd;
845 char buffer[0x1000];
846 struct inotify_event *ie;
848 unix_fd = get_unix_fd( fd );
849 r = read( unix_fd, buffer, sizeof buffer );
850 if (r < 0)
852 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
853 return;
856 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
858 ie = (struct inotify_event*) &buffer[ofs];
859 if (!ie->len)
860 break;
861 ofs += offsetof( struct inotify_event, name[ie->len] );
862 if (ofs > r) break;
863 inotify_notify_all( ie );
867 static inline struct fd *create_inotify_fd( void )
869 int unix_fd;
871 unix_fd = inotify_init();
872 if (unix_fd<0)
873 return NULL;
874 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
877 static int map_flags( unsigned int filter )
879 unsigned int mask;
881 /* always watch these so we can track subdirectories in recursive watches */
882 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
884 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
885 mask |= IN_ATTRIB;
886 if (filter & FILE_NOTIFY_CHANGE_SIZE)
887 mask |= IN_MODIFY;
888 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
889 mask |= IN_MODIFY;
890 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
891 mask |= IN_ACCESS;
892 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
893 mask |= IN_ATTRIB;
895 return mask;
898 static int inotify_add_dir( char *path, unsigned int filter )
900 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
901 path, map_flags( filter ) );
902 if (wd != -1)
903 set_fd_events( inotify_fd, POLLIN );
904 return wd;
907 static int init_inotify( void )
909 int i;
911 if (inotify_fd)
912 return 1;
914 inotify_fd = create_inotify_fd();
915 if (!inotify_fd)
916 return 0;
918 for (i=0; i<HASH_SIZE; i++)
920 list_init( &inode_hash[i] );
921 list_init( &wd_hash[i] );
924 return 1;
927 static int inotify_adjust_changes( struct dir *dir )
929 unsigned int filter;
930 struct inode *inode;
931 struct stat st;
932 char path[32];
933 int wd, unix_fd;
935 if (!inotify_fd)
936 return 0;
938 unix_fd = get_unix_fd( dir->fd );
940 inode = dir->inode;
941 if (!inode)
943 /* check if this fd is already being watched */
944 if (-1 == fstat( unix_fd, &st ))
945 return 0;
947 inode = get_inode( st.st_dev, st.st_ino );
948 if (!inode)
949 inode = create_inode( st.st_dev, st.st_ino );
950 if (!inode)
951 return 0;
952 list_add_tail( &inode->dirs, &dir->in_entry );
953 dir->inode = inode;
956 filter = filter_from_inode( inode, 0 );
958 sprintf( path, "/proc/self/fd/%u", unix_fd );
959 wd = inotify_add_dir( path, filter );
960 if (wd == -1) return 0;
962 inode_set_wd( inode, wd );
964 return 1;
967 static char *get_basename( const char *link )
969 char *buffer, *name = NULL;
970 int r, n = 0x100;
972 while (1)
974 buffer = malloc( n );
975 if (!buffer) return NULL;
977 r = readlink( link, buffer, n );
978 if (r < 0)
979 break;
981 if (r < n)
983 name = buffer;
984 break;
986 free( buffer );
987 n *= 2;
990 if (name)
992 while (r > 0 && name[ r - 1 ] == '/' )
993 r--;
994 name[ r ] = 0;
996 name = strrchr( name, '/' );
997 if (name)
998 name = strdup( &name[1] );
1001 free( buffer );
1002 return name;
1005 static int dir_add_to_existing_notify( struct dir *dir )
1007 struct inode *inode, *parent;
1008 unsigned int filter = 0;
1009 struct stat st, st_new;
1010 char link[35], *name;
1011 int wd, unix_fd;
1013 if (!inotify_fd)
1014 return 0;
1016 unix_fd = get_unix_fd( dir->fd );
1018 /* check if it's in the list of inodes we want to watch */
1019 if (-1 == fstat( unix_fd, &st_new ))
1020 return 0;
1021 inode = find_inode( st_new.st_dev, st_new.st_ino );
1022 if (inode)
1023 return 0;
1025 /* lookup the parent */
1026 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1027 if (-1 == stat( link, &st ))
1028 return 0;
1031 * If there's no parent, stop. We could keep going adding
1032 * ../ to the path until we hit the root of the tree or
1033 * find a recursively watched ancestor.
1034 * Assume it's too expensive to search up the tree for now.
1036 parent = find_inode( st.st_dev, st.st_ino );
1037 if (!parent)
1038 return 0;
1040 if (parent->wd == -1)
1041 return 0;
1043 filter = filter_from_inode( parent, 1 );
1044 if (!filter)
1045 return 0;
1047 sprintf( link, "/proc/self/fd/%u", unix_fd );
1048 name = get_basename( link );
1049 if (!name)
1050 return 0;
1051 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1052 free( name );
1053 if (!inode)
1054 return 0;
1056 /* Couldn't find this inode at the start of the function, must be new */
1057 assert( inode->wd == -1 );
1059 wd = inotify_add_dir( link, filter );
1060 if (wd != -1)
1061 inode_set_wd( inode, wd );
1063 return 1;
1066 #else
1068 static int init_inotify( void )
1070 return 0;
1073 static int inotify_adjust_changes( struct dir *dir )
1075 return 0;
1078 static void free_inode( struct inode *inode )
1080 assert( 0 );
1083 static int dir_add_to_existing_notify( struct dir *dir )
1085 return 0;
1088 #endif /* USE_INOTIFY */
1090 struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
1092 struct dir *dir;
1094 dir = alloc_object( &dir_ops );
1095 if (!dir)
1096 return NULL;
1098 list_init( &dir->change_records );
1099 dir->filter = 0;
1100 dir->notified = 0;
1101 dir->want_data = 0;
1102 dir->inode = NULL;
1103 grab_object( fd );
1104 dir->fd = fd;
1105 dir->mode = mode;
1106 dir->uid = ~(uid_t)0;
1107 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1109 dir_add_to_existing_notify( dir );
1111 return &dir->obj;
1114 /* enable change notifications for a directory */
1115 DECL_HANDLER(read_directory_changes)
1117 struct dir *dir;
1118 struct async *async;
1120 if (!req->filter)
1122 set_error(STATUS_INVALID_PARAMETER);
1123 return;
1126 dir = get_dir_obj( current->process, req->async.handle, 0 );
1127 if (!dir)
1128 return;
1130 /* requests don't timeout */
1131 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1133 /* assign it once */
1134 if (!dir->filter)
1136 init_inotify();
1137 insert_change( dir );
1138 dir->filter = req->filter;
1139 dir->subtree = req->subtree;
1140 dir->want_data = req->want_data;
1143 /* if there's already a change in the queue, send it */
1144 if (!list_empty( &dir->change_records ))
1145 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1147 /* setup the real notification */
1148 if (!inotify_adjust_changes( dir ))
1149 dnotify_adjust_changes( dir );
1151 release_object( async );
1152 set_error(STATUS_PENDING);
1154 end:
1155 release_object( dir );
1158 DECL_HANDLER(read_change)
1160 struct change_record *record, *next;
1161 struct dir *dir;
1162 struct list events;
1163 char *data, *event;
1164 int size = 0;
1166 dir = get_dir_obj( current->process, req->handle, 0 );
1167 if (!dir)
1168 return;
1170 list_init( &events );
1171 list_move_tail( &events, &dir->change_records );
1172 release_object( dir );
1174 if (list_empty( &events ))
1176 set_error( STATUS_NO_DATA_DETECTED );
1177 return;
1180 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1182 size += (offsetof(struct filesystem_event, name[record->event.len])
1183 + sizeof(int)-1) / sizeof(int) * sizeof(int);
1186 if (size > get_reply_max_size())
1187 set_error( STATUS_BUFFER_TOO_SMALL );
1188 else if ((data = mem_alloc( size )) != NULL)
1190 event = data;
1191 LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
1193 data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
1195 /* FIXME: rename events are sometimes reported as delete/create */
1196 if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
1198 struct list *elem = list_next( &events, &record->entry );
1199 if (elem)
1200 next = LIST_ENTRY(elem, struct change_record, entry);
1202 if (elem && next->cookie == record->cookie)
1203 next->cookie = 0;
1204 else
1205 record->event.action = FILE_ACTION_REMOVED;
1207 else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
1208 record->event.action = FILE_ACTION_ADDED;
1210 memcpy( event, &record->event, len );
1211 event += len;
1212 if (len % sizeof(int))
1214 memset( event, 0, sizeof(int) - len % sizeof(int) );
1215 event += sizeof(int) - len % sizeof(int);
1218 set_reply_data_ptr( data, size );
1221 LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
1223 list_remove( &record->entry );
1224 free( record );