1 /*
2 * Server-side change notification management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2006 Mike McCormack
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
38
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "windef.h"
42
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"
50
51 /* dnotify support */
52
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
65
66 /* inotify support */
67
68 #ifdef HAVE_SYS_INOTIFY_H
69 #include <sys/inotify.h>
70 #define USE_INOTIFY
71 #elif defined(__linux__) && defined(__i386__)
72
73 #define SYS_inotify_init 291
74 #define SYS_inotify_add_watch 292
75 #define SYS_inotify_rm_watch 293
76
77 struct inotify_event {
78 int wd;
79 unsigned int mask;
80 unsigned int cookie;
81 unsigned int len;
82 char name[1];
83 };
84
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
96
97 static inline int inotify_init( void )
98 {
99 int ret;
100 __asm__ __volatile__( "int $0x80"
101 : "=a" (ret)
102 : "" (SYS_inotify_init));
103 if (ret<0) { errno = -ret; ret = -1; }
104 return ret;
105 }
106
107 static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
108 {
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) : "" (SYS_inotify_add_watch),
115 "r" (fd), "c" (name), "d" (mask) );
116 if (ret<0) { errno = -ret; ret = -1; }
117 return ret;
118 }
119
120 static inline int inotify_rm_watch( int fd, int wd )
121 {
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) : "" (SYS_inotify_rm_watch),
128 "r" (fd), "c" (wd) );
129 if (ret<0) { errno = -ret; ret = -1; }
130 return ret;
131 }
132
133 #define USE_INOTIFY
134
135 #endif
136
137 struct inode;
138
139 static void free_inode( struct inode *inode );
140
141 static struct fd *inotify_fd;
142
143 struct change_record {
144 struct list entry;
145 int action;
146 int len;
147 char name[1];
148 };
149
150 struct dir
151 {
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 */
164 };
165
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 );
172
173 static const struct object_ops dir_ops =
174 {
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 */
191 };
192
193 static int dir_get_poll_events( struct fd *fd );
194 static enum server_fd_type dir_get_fd_type( struct fd *fd );
195
196 static const struct fd_ops dir_fd_ops =
197 {
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_ioctl, /* ioctl */
203 default_fd_queue_async, /* queue_async */
204 default_fd_reselect_async, /* reselect_async */
205 default_fd_cancel_async /* cancel_async */
206 };
207
208 static struct list change_list = LIST_INIT(change_list);
209
210 static void dnotify_adjust_changes( struct dir *dir )
211 {
212 #if defined(F_SETSIG) && defined(F_NOTIFY)
213 int fd = get_unix_fd( dir->fd );
214 unsigned int filter = dir->filter;
215 unsigned int val;
216 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
217 return;
218
219 val = DN_MULTISHOT;
220 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
221 val |= DN_RENAME | DN_DELETE | DN_CREATE;
222 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
223 val |= DN_RENAME | DN_DELETE | DN_CREATE;
224 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
225 val |= DN_ATTRIB;
226 if (filter & FILE_NOTIFY_CHANGE_SIZE)
227 val |= DN_MODIFY;
228 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
229 val |= DN_MODIFY;
230 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
231 val |= DN_ACCESS;
232 if (filter & FILE_NOTIFY_CHANGE_CREATION)
233 val |= DN_CREATE;
234 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
235 val |= DN_ATTRIB;
236 fcntl( fd, F_NOTIFY, val );
237 #endif
238 }
239
240 /* insert change in the global list */
241 static inline void insert_change( struct dir *dir )
242 {
243 sigset_t sigset;
244
245 sigemptyset( &sigset );
246 sigaddset( &sigset, SIGIO );
247 sigprocmask( SIG_BLOCK, &sigset, NULL );
248 list_add_head( &change_list, &dir->entry );
249 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
250 }
251
252 /* remove change from the global list */
253 static inline void remove_change( struct dir *dir )
254 {
255 sigset_t sigset;
256
257 sigemptyset( &sigset );
258 sigaddset( &sigset, SIGIO );
259 sigprocmask( SIG_BLOCK, &sigset, NULL );
260 list_remove( &dir->entry );
261 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
262 }
263
264 static void dir_dump( struct object *obj, int verbose )
265 {
266 struct dir *dir = (struct dir *)obj;
267 assert( obj->ops == &dir_ops );
268 fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
269 }
270
271 /* enter here directly from SIGIO signal handler */
272 void do_change_notify( int unix_fd )
273 {
274 struct dir *dir;
275
276 /* FIXME: this is O(n) ... probably can be improved */
277 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
278 {
279 if (get_unix_fd( dir->fd ) != unix_fd) continue;
280 interlocked_xchg_add( &dir->notified, 1 );
281 break;
282 }
283 }
284
285 /* SIGIO callback, called synchronously with the poll loop */
286 void sigio_callback(void)
287 {
288 struct dir *dir;
289
290 LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
291 {
292 if (interlocked_xchg( &dir->notified, 0 ))
293 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
294 }
295 }
296
297 static struct fd *dir_get_fd( struct object *obj )
298 {
299 struct dir *dir = (struct dir *)obj;
300 assert( obj->ops == &dir_ops );
301 return (struct fd *)grab_object( dir->fd );
302 }
303
304 static int get_dir_unix_fd( struct dir *dir )
305 {
306 return get_unix_fd( dir->fd );
307 }
308
309 static struct security_descriptor *dir_get_sd( struct object *obj )
310 {
311 struct dir *dir = (struct dir *)obj;
312 int unix_fd;
313 struct stat st;
314 struct security_descriptor *sd;
315 assert( obj->ops == &dir_ops );
316
317 unix_fd = get_dir_unix_fd( dir );
318
319 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
320 return obj->sd;
321
322 /* mode and uid the same? if so, no need to re-generate security descriptor */
323 if (obj->sd &&
324 (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
325 (st.st_uid == dir->uid))
326 return obj->sd;
327
328 sd = mode_to_sd( st.st_mode,
329 security_unix_uid_to_sid( st.st_uid ),
330 token_get_primary_group( current->process->token ));
331 if (!sd) return obj->sd;
332
333 dir->mode = st.st_mode;
334 dir->uid = st.st_uid;
335 free( obj->sd );
336 obj->sd = sd;
337 return sd;
338 }
339
340 static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
341 unsigned int set_info )
342 {
343 struct dir *dir = (struct dir *)obj;
344 const SID *owner;
345 mode_t mode;
346 int unix_fd;
347
348 assert( obj->ops == &dir_ops );
349
350 unix_fd = get_dir_unix_fd( dir );
351
352 if (unix_fd == -1) return 1;
353
354 if (set_info & OWNER_SECURITY_INFORMATION)
355 {
356 owner = sd_get_owner( sd );
357 if (!owner)
358 {
359 set_error( STATUS_INVALID_SECURITY_DESCR );
360 return 0;
361 }
362 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
363 {
364 /* FIXME: get Unix uid and call fchown */
365 }
366 }
367 else if (obj->sd)
368 owner = sd_get_owner( obj->sd );
369 else
370 owner = token_get_user( current->process->token );
371
372 if (set_info & DACL_SECURITY_INFORMATION)
373 {
374 /* keep the bits that we don't map to access rights in the ACL */
375 mode = dir->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
376 mode |= sd_to_mode( sd, owner );
377
378 if (dir->mode != mode)
379 {
380 if (fchmod( unix_fd, mode ) == -1)
381 {
382 file_set_error();
383 return 0;
384 }
385
386 dir->mode = mode;
387 }
388 }
389 return 1;
390 }
391
392 static struct change_record *get_first_change_record( struct dir *dir )
393 {
394 struct list *ptr = list_head( &dir->change_records );
395 if (!ptr) return NULL;
396 list_remove( ptr );
397 return LIST_ENTRY( ptr, struct change_record, entry );
398 }
399
400 static void dir_destroy( struct object *obj )
401 {
402 struct change_record *record;
403 struct dir *dir = (struct dir *)obj;
404 assert (obj->ops == &dir_ops);
405
406 if (dir->filter)
407 remove_change( dir );
408
409 if (dir->inode)
410 {
411 list_remove( &dir->in_entry );
412 free_inode( dir->inode );
413 }
414
415 while ((record = get_first_change_record( dir ))) free( record );
416
417 release_object( dir->fd );
418
419 if (inotify_fd && list_empty( &change_list ))
420 {
421 release_object( inotify_fd );
422 inotify_fd = NULL;
423 }
424 }
425
426 static struct dir *
427 get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
428 {
429 return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
430 }
431
432 static int dir_get_poll_events( struct fd *fd )
433 {
434 return 0;
435 }
436
437 static enum server_fd_type dir_get_fd_type( struct fd *fd )
438 {
439 return FD_TYPE_DIR;
440 }
441
442 #ifdef USE_INOTIFY
443
444 #define HASH_SIZE 31
445
446 struct inode {
447 struct list ch_entry; /* entry in the children list */
448 struct list children; /* children of this inode */
449 struct inode *parent; /* parent of this inode */
450 struct list dirs; /* directory handles watching this inode */
451 struct list ino_entry; /* entry in the inode hash */
452 struct list wd_entry; /* entry in the watch descriptor hash */
453 dev_t dev; /* device number */
454 ino_t ino; /* device's inode number */
455 int wd; /* inotify's watch descriptor */
456 char *name; /* basename name of the inode */
457 };
458
459 struct list inode_hash[ HASH_SIZE ];
460 struct list wd_hash[ HASH_SIZE ];
461
462 static int inotify_add_dir( char *path, unsigned int filter );
463
464 static struct inode *inode_from_wd( int wd )
465 {
466 struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
467 struct inode *inode;
468
469 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
470 if (inode->wd == wd)
471 return inode;
472
473 return NULL;
474 }
475
476 static inline struct list *get_hash_list( dev_t dev, ino_t ino )
477 {
478 return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
479 }
480
481 static struct inode *find_inode( dev_t dev, ino_t ino )
482 {
483 struct list *bucket = get_hash_list( dev, ino );
484 struct inode *inode;
485
486 LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
487 if (inode->ino == ino && inode->dev == dev)
488 return inode;
489
490 return NULL;
491 }
492
493 static struct inode *create_inode( dev_t dev, ino_t ino )
494 {
495 struct inode *inode;
496
497 inode = malloc( sizeof *inode );
498 if (inode)
499 {
500 list_init( &inode->children );
501 list_init( &inode->dirs );
502 inode->ino = ino;
503 inode->dev = dev;
504 inode->wd = -1;
505 inode->parent = NULL;
506 inode->name = NULL;
507 list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
508 }
509 return inode;
510 }
511
512 static struct inode *get_inode( dev_t dev, ino_t ino )
513 {
514 struct inode *inode;
515
516 inode = find_inode( dev, ino );
517 if (inode)
518 return inode;
519 return create_inode( dev, ino );
520 }
521
522 static void inode_set_wd( struct inode *inode, int wd )
523 {
524 if (inode->wd != -1)
525 list_remove( &inode->wd_entry );
526 inode->wd = wd;
527 list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
528 }
529
530 static void inode_set_name( struct inode *inode, const char *name )
531 {
532 free (inode->name);
533 inode->name = name ? strdup( name ) : NULL;
534 }
535
536 static void free_inode( struct inode *inode )
537 {
538 int subtree = 0, watches = 0;
539 struct inode *tmp, *next;
540 struct dir *dir;
541
542 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
543 {
544 subtree |= dir->subtree;
545 watches++;
546 }
547
548 if (!subtree && !inode->parent)
549 {
550 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
551 struct inode, ch_entry )
552 {
553 assert( tmp != inode );
554 assert( tmp->parent == inode );
555 free_inode( tmp );
556 }
557 }
558
559 if (watches)
560 return;
561
562 if (inode->parent)
563 list_remove( &inode->ch_entry );
564
565 /* disconnect remaining children from the parent */
566 LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
567 {
568 list_remove( &tmp->ch_entry );
569 tmp->parent = NULL;
570 }
571
572 if (inode->wd != -1)
573 {
574 inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
575 list_remove( &inode->wd_entry );
576 }
577 list_remove( &inode->ino_entry );
578
579 free( inode->name );
580 free( inode );
581 }
582
583 static struct inode *inode_add( struct inode *parent,
584 dev_t dev, ino_t ino, const char *name )
585 {
586 struct inode *inode;
587
588 inode = get_inode( dev, ino );
589 if (!inode)
590 return NULL;
591
592 if (!inode->parent)
593 {
594 list_add_tail( &parent->children, &inode->ch_entry );
595 inode->parent = parent;
596 assert( inode != parent );
597 }
598 inode_set_name( inode, name );
599
600 return inode;
601 }
602
603 static struct inode *inode_from_name( struct inode *inode, const char *name )
604 {
605 struct inode *i;
606
607 LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
608 if (i->name && !strcmp( i->name, name ))
609 return i;
610 return NULL;
611 }
612
613 static int inotify_get_poll_events( struct fd *fd );
614 static void inotify_poll_event( struct fd *fd, int event );
615
616 static const struct fd_ops inotify_fd_ops =
617 {
618 inotify_get_poll_events, /* get_poll_events */
619 inotify_poll_event, /* poll_event */
620 NULL, /* flush */
621 NULL, /* get_fd_type */
622 NULL, /* ioctl */
623 NULL, /* queue_async */
624 NULL, /* reselect_async */
625 NULL, /* cancel_async */
626 };
627
628 static int inotify_get_poll_events( struct fd *fd )
629 {
630 return POLLIN;
631 }
632
633 static void inotify_do_change_notify( struct dir *dir, unsigned int action,
634 const char *relpath )
635 {
636 struct change_record *record;
637
638 assert( dir->obj.ops == &dir_ops );
639
640 if (dir->want_data)
641 {
642 size_t len = strlen(relpath);
643 record = malloc( offsetof(struct change_record, name[len]) );
644 if (!record)
645 return;
646
647 record->action = action;
648 memcpy( record->name, relpath, len );
649 record->len = len;
650
651 list_add_tail( &dir->change_records, &record->entry );
652 }
653
654 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
655 }
656
657 static unsigned int filter_from_event( struct inotify_event *ie )
658 {
659 unsigned int filter = 0;
660
661 if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
662 filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
663 if (ie->mask & IN_MODIFY)
664 filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
665 if (ie->mask & IN_ATTRIB)
666 filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
667 if (ie->mask & IN_ACCESS)
668 filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
669 if (ie->mask & IN_CREATE)
670 filter |= FILE_NOTIFY_CHANGE_CREATION;
671
672 return filter;
673 }
674
675 /* scan up the parent directories for watches */
676 static unsigned int filter_from_inode( struct inode *inode, int is_parent )
677 {
678 unsigned int filter = 0;
679 struct dir *dir;
680
681 /* combine filters from parents watching subtrees */
682 while (inode)
683 {
684 LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
685 if (dir->subtree || !is_parent)
686 filter |= dir->filter;
687 is_parent = 1;
688 inode = inode->parent;
689 }
690
691 return filter;
692 }
693
694 static char *inode_get_path( struct inode *inode, int sz )
695 {
696 struct list *head;
697 char *path;
698 int len;
699
700 if (!inode)
701 return NULL;
702
703 head = list_head( &inode->dirs );
704 if (head)
705 {
706 int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
707 path = malloc ( 32 + sz );
708 if (path)
709 sprintf( path, "/proc/self/fd/%u/", unix_fd );
710 return path;
711 }
712
713 if (!inode->name)
714 return NULL;
715
716 len = strlen( inode->name );
717 path = inode_get_path( inode->parent, sz + len + 1 );
718 if (!path)
719 return NULL;
720
721 strcat( path, inode->name );
722 strcat( path, "/" );
723
724 return path;
725 }
726
727 static int inode_check_dir( struct inode *parent, const char *name )
728 {
729 char *path;
730 unsigned int filter;
731 struct inode *inode;
732 struct stat st;
733 int wd = -1, r = -1;
734
735 path = inode_get_path( parent, strlen(name) );
736 if (!path)
737 return r;
738
739 strcat( path, name );
740
741 r = stat( path, &st );
742 if (r < 0) goto end;
743
744 if (!S_ISDIR(st.st_mode))
745 {
746 r = 0;
747 goto end;
748 }
749
750 r = 1;
751
752 filter = filter_from_inode( parent, 1 );
753 if (!filter)
754 goto end;
755
756 inode = inode_add( parent, st.st_dev, st.st_ino, name );
757 if (!inode || inode->wd != -1)
758 goto end;
759
760 wd = inotify_add_dir( path, filter );
761 if (wd != -1)
762 inode_set_wd( inode, wd );
763 else
764 free_inode( inode );
765
766 end:
767 free( path );
768 return r;
769 }
770
771 static int prepend( char **path, const char *segment )
772 {
773 int extra;
774 char *p;
775
776 extra = strlen( segment ) + 1;
777 if (*path)
778 {
779 int len = strlen( *path ) + 1;
780 p = realloc( *path, len + extra );
781 if (!p) return 0;
782 memmove( &p[ extra ], p, len );
783 p[ extra - 1 ] = '/';
784 memcpy( p, segment, extra - 1 );
785 }
786 else
787 {
788 p = malloc( extra );
789 if (!p) return 0;
790 memcpy( p, segment, extra );
791 }
792
793 *path = p;
794
795 return 1;
796 }
797
798 static void inotify_notify_all( struct inotify_event *ie )
799 {
800 unsigned int filter, action;
801 struct inode *inode, *i;
802 char *path = NULL;
803 struct dir *dir;
804
805 inode = inode_from_wd( ie->wd );
806 if (!inode)
807 {
808 fprintf( stderr, "no inode matches %d\n", ie->wd);
809 return;
810 }
811
812 filter = filter_from_event( ie );
813
814 if (ie->mask & IN_CREATE)
815 {
816 switch (inode_check_dir( inode, ie->name ))
817 {
818 case 1:
819 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
820 break;
821 case 0:
822 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
823 break;
824 default:
825 break;
826 /* Maybe the file disappeared before we could check it? */
827 }
828 action = FILE_ACTION_ADDED;
829 }
830 else if (ie->mask & IN_DELETE)
831 action = FILE_ACTION_REMOVED;
832 else
833 action = FILE_ACTION_MODIFIED;
834
835 /*
836 * Work our way up the inode hierarchy
837 * extending the relative path as we go
838 * and notifying all recursive watches.
839 */
840 if (!prepend( &path, ie->name ))
841 return;
842
843 for (i = inode; i; i = i->parent)
844 {
845 LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
846 if ((filter & dir->filter) && (i==inode || dir->subtree))
847 inotify_do_change_notify( dir, action, path );
848
849 if (!i->name || !prepend( &path, i->name ))
850 break;
851 }
852
853 free( path );
854
855 if (ie->mask & IN_DELETE)
856 {
857 i = inode_from_name( inode, ie->name );
858 if (i)
859 free_inode( i );
860 }
861 }
862
863 static void inotify_poll_event( struct fd *fd, int event )
864 {
865 int r, ofs, unix_fd;
866 char buffer[0x1000];
867 struct inotify_event *ie;
868
869 unix_fd = get_unix_fd( fd );
870 r = read( unix_fd, buffer, sizeof buffer );
871 if (r < 0)
872 {
873 fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
874 return;
875 }
876
877 for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
878 {
879 ie = (struct inotify_event*) &buffer[ofs];
880 if (!ie->len)
881 break;
882 ofs += offsetof( struct inotify_event, name[ie->len] );
883 if (ofs > r) break;
884 inotify_notify_all( ie );
885 }
886 }
887
888 static inline struct fd *create_inotify_fd( void )
889 {
890 int unix_fd;
891
892 unix_fd = inotify_init();
893 if (unix_fd<0)
894 return NULL;
895 return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
896 }
897
898 static int map_flags( unsigned int filter )
899 {
900 unsigned int mask;
901
902 /* always watch these so we can track subdirectories in recursive watches */
903 mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
904
905 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
906 mask |= IN_ATTRIB;
907 if (filter & FILE_NOTIFY_CHANGE_SIZE)
908 mask |= IN_MODIFY;
909 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
910 mask |= IN_MODIFY;
911 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
912 mask |= IN_ACCESS;
913 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
914 mask |= IN_ATTRIB;
915
916 return mask;
917 }
918
919 static int inotify_add_dir( char *path, unsigned int filter )
920 {
921 int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
922 path, map_flags( filter ) );
923 if (wd != -1)
924 set_fd_events( inotify_fd, POLLIN );
925 return wd;
926 }
927
928 static int init_inotify( void )
929 {
930 int i;
931
932 if (inotify_fd)
933 return 1;
934
935 inotify_fd = create_inotify_fd();
936 if (!inotify_fd)
937 return 0;
938
939 for (i=0; i<HASH_SIZE; i++)
940 {
941 list_init( &inode_hash[i] );
942 list_init( &wd_hash[i] );
943 }
944
945 return 1;
946 }
947
948 static int inotify_adjust_changes( struct dir *dir )
949 {
950 unsigned int filter;
951 struct inode *inode;
952 struct stat st;
953 char path[32];
954 int wd, unix_fd;
955
956 if (!inotify_fd)
957 return 0;
958
959 unix_fd = get_unix_fd( dir->fd );
960
961 inode = dir->inode;
962 if (!inode)
963 {
964 /* check if this fd is already being watched */
965 if (-1 == fstat( unix_fd, &st ))
966 return 0;
967
968 inode = get_inode( st.st_dev, st.st_ino );
969 if (!inode)
970 inode = create_inode( st.st_dev, st.st_ino );
971 if (!inode)
972 return 0;
973 list_add_tail( &inode->dirs, &dir->in_entry );
974 dir->inode = inode;
975 }
976
977 filter = filter_from_inode( inode, 0 );
978
979 sprintf( path, "/proc/self/fd/%u", unix_fd );
980 wd = inotify_add_dir( path, filter );
981 if (wd == -1) return 0;
982
983 inode_set_wd( inode, wd );
984
985 return 1;
986 }
987
988 static char *get_basename( const char *link )
989 {
990 char *buffer, *name = NULL;
991 int r, n = 0x100;
992
993 while (1)
994 {
995 buffer = malloc( n );
996 if (!buffer) return NULL;
997
998 r = readlink( link, buffer, n );
999 if (r < 0)
1000 break;
1001
1002 if (r < n)
1003 {
1004 name = buffer;
1005 break;
1006 }
1007 free( buffer );
1008 n *= 2;
1009 }
1010
1011 if (name)
1012 {
1013 while (r > 0 && name[ r - 1 ] == '/' )
1014 r--;
1015 name[ r ] = 0;
1016
1017 name = strrchr( name, '/' );
1018 if (name)
1019 name = strdup( &name[1] );
1020 }
1021
1022 free( buffer );
1023 return name;
1024 }
1025
1026 static int dir_add_to_existing_notify( struct dir *dir )
1027 {
1028 struct inode *inode, *parent;
1029 unsigned int filter = 0;
1030 struct stat st, st_new;
1031 char link[35], *name;
1032 int wd, unix_fd;
1033
1034 if (!inotify_fd)
1035 return 0;
1036
1037 unix_fd = get_unix_fd( dir->fd );
1038
1039 /* check if it's in the list of inodes we want to watch */
1040 if (-1 == fstat( unix_fd, &st_new ))
1041 return 0;
1042 inode = find_inode( st_new.st_dev, st_new.st_ino );
1043 if (inode)
1044 return 0;
1045
1046 /* lookup the parent */
1047 sprintf( link, "/proc/self/fd/%u/..", unix_fd );
1048 if (-1 == stat( link, &st ))
1049 return 0;
1050
1051 /*
1052 * If there's no parent, stop. We could keep going adding
1053 * ../ to the path until we hit the root of the tree or
1054 * find a recursively watched ancestor.
1055 * Assume it's too expensive to search up the tree for now.
1056 */
1057 parent = find_inode( st.st_dev, st.st_ino );
1058 if (!parent)
1059 return 0;
1060
1061 if (parent->wd == -1)
1062 return 0;
1063
1064 filter = filter_from_inode( parent, 1 );
1065 if (!filter)
1066 return 0;
1067
1068 sprintf( link, "/proc/self/fd/%u", unix_fd );
1069 name = get_basename( link );
1070 if (!name)
1071 return 0;
1072 inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
1073 free( name );
1074 if (!inode)
1075 return 0;
1076
1077 /* Couldn't find this inode at the start of the function, must be new */
1078 assert( inode->wd == -1 );
1079
1080 wd = inotify_add_dir( link, filter );
1081 if (wd != -1)
1082 inode_set_wd( inode, wd );
1083
1084 return 1;
1085 }
1086
1087 #else
1088
1089 static int init_inotify( void )
1090 {
1091 return 0;
1092 }
1093
1094 static int inotify_adjust_changes( struct dir *dir )
1095 {
1096 return 0;
1097 }
1098
1099 static void free_inode( struct inode *inode )
1100 {
1101 assert( 0 );
1102 }
1103
1104 static int dir_add_to_existing_notify( struct dir *dir )
1105 {
1106 return 0;
1107 }
1108
1109 #endif /* USE_INOTIFY */
1110
1111 struct object *create_dir_obj( struct fd *fd )
1112 {
1113 struct dir *dir;
1114
1115 dir = alloc_object( &dir_ops );
1116 if (!dir)
1117 return NULL;
1118
1119 list_init( &dir->change_records );
1120 dir->filter = 0;
1121 dir->notified = 0;
1122 dir->want_data = 0;
1123 dir->inode = NULL;
1124 grab_object( fd );
1125 dir->fd = fd;
1126 set_fd_user( fd, &dir_fd_ops, &dir->obj );
1127
1128 dir_add_to_existing_notify( dir );
1129
1130 return &dir->obj;
1131 }
1132
1133 /* enable change notifications for a directory */
1134 DECL_HANDLER(read_directory_changes)
1135 {
1136 struct dir *dir;
1137 struct async *async;
1138
1139 if (!req->filter)
1140 {
1141 set_error(STATUS_INVALID_PARAMETER);
1142 return;
1143 }
1144
1145 dir = get_dir_obj( current->process, req->async.handle, 0 );
1146 if (!dir)
1147 return;
1148
1149 /* requests don't timeout */
1150 if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1151
1152 /* assign it once */
1153 if (!dir->filter)
1154 {
1155 init_inotify();
1156 insert_change( dir );
1157 dir->filter = req->filter;
1158 dir->subtree = req->subtree;
1159 dir->want_data = req->want_data;
1160 }
1161
1162 /* if there's already a change in the queue, send it */
1163 if (!list_empty( &dir->change_records ))
1164 fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1165
1166 /* setup the real notification */
1167 if (!inotify_adjust_changes( dir ))
1168 dnotify_adjust_changes( dir );
1169
1170 release_object( async );
1171 set_error(STATUS_PENDING);
1172
1173 end:
1174 release_object( dir );
1175 }
1176
1177 DECL_HANDLER(read_change)
1178 {
1179 struct change_record *record;
1180 struct dir *dir;
1181
1182 dir = get_dir_obj( current->process, req->handle, 0 );
1183 if (!dir)
1184 return;
1185
1186 if ((record = get_first_change_record( dir )) != NULL)
1187 {
1188 reply->action = record->action;
1189 set_reply_data( record->name, record->len );
1190 free( record );
1191 }
1192 else
1193 set_error( STATUS_NO_DATA_DETECTED );
1194
1195 release_object( dir );
1196 }
1197
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.