change.c 29.5 KB
Newer Older
1 2 3 4
/*
 * Server-side change notification management
 *
 * Copyright (C) 1998 Alexandre Julliard
5
 * Copyright (C) 2006 Mike McCormack
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24
#include "config.h"
#include "wine/port.h"

25
#include <assert.h>
26
#include <fcntl.h>
27 28
#include <stdio.h>
#include <stdlib.h>
29
#include <signal.h>
30
#include <sys/stat.h>
31 32 33
#include <sys/types.h>
#include <limits.h>
#include <dirent.h>
34 35 36 37
#include <errno.h>
#ifdef HAVE_SYS_ERRNO_H
#include <sys/errno.h>
#endif
38

39 40
#include "ntstatus.h"
#define WIN32_NO_STATUS
41
#include "windef.h"
42

43
#include "file.h"
44 45
#include "handle.h"
#include "thread.h"
46
#include "request.h"
47 48
#include "process.h"
#include "security.h"
49
#include "winternl.h"
50

51 52
/* dnotify support */

53 54 55 56 57 58 59 60
#ifdef linux
#ifndef F_NOTIFY
#define F_NOTIFY 1026
#define DN_ACCESS       0x00000001      /* File accessed */
#define DN_MODIFY       0x00000002      /* File modified */
#define DN_CREATE       0x00000004      /* File created */
#define DN_DELETE       0x00000008      /* File removed */
#define DN_RENAME       0x00000010      /* File renamed */
61
#define DN_ATTRIB       0x00000020      /* File changed attributes */
62 63 64 65
#define DN_MULTISHOT    0x80000000      /* Don't remove notifier */
#endif
#endif

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* inotify support */

#if defined(__linux__) && defined(__i386__)

#define SYS_inotify_init	291
#define SYS_inotify_add_watch	292
#define SYS_inotify_rm_watch	293

struct inotify_event {
    int           wd;
    unsigned int  mask;
    unsigned int  cookie;
    unsigned int  len;
    char          name[1];
};

#define IN_ACCESS        0x00000001
#define IN_MODIFY        0x00000002
#define IN_ATTRIB        0x00000004
#define IN_CLOSE_WRITE   0x00000008
#define IN_CLOSE_NOWRITE 0x00000010
#define IN_OPEN          0x00000020
#define IN_MOVED_FROM    0x00000040
#define IN_MOVED_TO      0x00000080
#define IN_CREATE        0x00000100
#define IN_DELETE        0x00000200
#define IN_DELETE_SELF   0x00000400

static inline int inotify_init( void )
{
    int ret;
    __asm__ __volatile__( "int $0x80"
                          : "=a" (ret)
                          : "0" (SYS_inotify_init));
    if (ret<0) { errno = -ret; ret = -1; }
    return ret;
}

static inline int inotify_add_watch( int fd, const char *name, unsigned int mask )
{
    int ret;
    __asm__ __volatile__( "pushl %%ebx;\n\t"
                          "movl %2,%%ebx;\n\t"
                          "int $0x80;\n\t"
                          "popl %%ebx"
                          : "=a" (ret) : "0" (SYS_inotify_add_watch),
                            "r" (fd), "c" (name), "d" (mask) );
    if (ret<0) { errno = -ret; ret = -1; }
    return ret;
}

static inline int inotify_remove_watch( int fd, int wd )
{
    int ret;
    __asm__ __volatile__( "pushl %%ebx;\n\t"
                          "movl %2,%%ebx;\n\t"
                          "int $0x80;\n\t"
                          "popl %%ebx"
                          : "=a" (ret) : "0" (SYS_inotify_rm_watch),
                            "r" (fd), "c" (wd) );
    if (ret<0) { errno = -ret; ret = -1; }
    return ret;
}

130
#define USE_INOTIFY
131 132 133

#endif

134 135 136 137 138 139
struct inode;

static void free_inode( struct inode *inode );

static struct fd *inotify_fd;

140 141 142 143 144 145 146
struct change_record {
    struct list entry;
    int action;
    int len;
    char name[1];
};

147
struct dir
148 149
{
    struct object  obj;      /* object header */
150
    struct fd     *fd;       /* file descriptor to the directory */
151 152
    mode_t         mode;     /* file stat.st_mode */
    uid_t          uid;      /* file stat.st_uid */
153 154
    struct list    entry;    /* entry in global change notifications list */
    unsigned int   filter;   /* notification filter */
155
    int            notified; /* SIGIO counter */
156
    int            want_data; /* return change data */
157
    int            subtree;  /* do we want to watch subdirectories? */
158
    struct list    change_records;   /* data for the change */
159 160
    struct list    in_entry; /* entry in the inode dirs list */
    struct inode  *inode;    /* inode of the associated directory */
161 162
};

163
static struct fd *dir_get_fd( struct object *obj );
164 165 166
static struct security_descriptor *dir_get_sd( struct object *obj );
static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
                       unsigned int set_info );
167 168
static void dir_dump( struct object *obj, int verbose );
static void dir_destroy( struct object *obj );
169

170
static const struct object_ops dir_ops =
171
{
172 173
    sizeof(struct dir),       /* size */
    dir_dump,                 /* dump */
174
    no_get_type,              /* get_type */
175 176
    add_queue,                /* add_queue */
    remove_queue,             /* remove_queue */
177
    default_fd_signaled,      /* signaled */
178
    no_satisfied,             /* satisfied */
179
    no_signal,                /* signal */
180
    dir_get_fd,               /* get_fd */
181
    default_fd_map_access,    /* map_access */
182 183
    dir_get_sd,               /* get_sd */
    dir_set_sd,               /* set_sd */
184
    no_lookup_name,           /* lookup_name */
185
    no_open_file,             /* open_file */
186
    fd_close_handle,          /* close_handle */
187 188 189 190
    dir_destroy               /* destroy */
};

static int dir_get_poll_events( struct fd *fd );
191
static enum server_fd_type dir_get_fd_type( struct fd *fd );
192 193 194

static const struct fd_ops dir_fd_ops =
{
195 196 197
    dir_get_poll_events,         /* get_poll_events */
    default_poll_event,          /* poll_event */
    no_flush,                    /* flush */
198
    dir_get_fd_type,             /* get_fd_type */
199
    default_fd_ioctl,            /* ioctl */
200 201 202
    default_fd_queue_async,      /* queue_async */
    default_fd_reselect_async,   /* reselect_async */
    default_fd_cancel_async      /* cancel_async */
203 204
};

205 206
static struct list change_list = LIST_INIT(change_list);

207
static void dnotify_adjust_changes( struct dir *dir )
208
{
209
#if defined(F_SETSIG) && defined(F_NOTIFY)
210 211
    int fd = get_unix_fd( dir->fd );
    unsigned int filter = dir->filter;
212 213 214 215 216
    unsigned int val;
    if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
        return;

    val = DN_MULTISHOT;
217
    if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
218
        val |= DN_RENAME | DN_DELETE | DN_CREATE;
219
    if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
220
        val |= DN_RENAME | DN_DELETE | DN_CREATE;
221
    if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
222
        val |= DN_ATTRIB;
223
    if (filter & FILE_NOTIFY_CHANGE_SIZE)
224
        val |= DN_MODIFY;
225
    if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
226
        val |= DN_MODIFY;
227
    if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
228
        val |= DN_ACCESS;
229
    if (filter & FILE_NOTIFY_CHANGE_CREATION)
230
        val |= DN_CREATE;
231
    if (filter & FILE_NOTIFY_CHANGE_SECURITY)
232 233 234 235 236 237
        val |= DN_ATTRIB;
    fcntl( fd, F_NOTIFY, val );
#endif
}

/* insert change in the global list */
238
static inline void insert_change( struct dir *dir )
239 240 241 242 243 244
{
    sigset_t sigset;

    sigemptyset( &sigset );
    sigaddset( &sigset, SIGIO );
    sigprocmask( SIG_BLOCK, &sigset, NULL );
245
    list_add_head( &change_list, &dir->entry );
246 247 248 249
    sigprocmask( SIG_UNBLOCK, &sigset, NULL );
}

/* remove change from the global list */
250
static inline void remove_change( struct dir *dir )
251 252 253 254 255 256
{
    sigset_t sigset;

    sigemptyset( &sigset );
    sigaddset( &sigset, SIGIO );
    sigprocmask( SIG_BLOCK, &sigset, NULL );
257
    list_remove( &dir->entry );
258 259
    sigprocmask( SIG_UNBLOCK, &sigset, NULL );
}
260

261
static void dir_dump( struct object *obj, int verbose )
262
{
263 264
    struct dir *dir = (struct dir *)obj;
    assert( obj->ops == &dir_ops );
265
    fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
266 267 268 269 270
}

/* enter here directly from SIGIO signal handler */
void do_change_notify( int unix_fd )
{
271
    struct dir *dir;
272 273

    /* FIXME: this is O(n) ... probably can be improved */
274
    LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
275
    {
276 277
        if (get_unix_fd( dir->fd ) != unix_fd) continue;
        interlocked_xchg_add( &dir->notified, 1 );
278 279 280 281 282 283 284
        break;
    }
}

/* SIGIO callback, called synchronously with the poll loop */
void sigio_callback(void)
{
285
    struct dir *dir;
286

287
    LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
288
    {
289 290
        if (interlocked_xchg( &dir->notified, 0 ))
            fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_NOTIFY_ENUM_DIR );
291
    }
292 293
}

294
static struct fd *dir_get_fd( struct object *obj )
295
{
296 297 298 299
    struct dir *dir = (struct dir *)obj;
    assert( obj->ops == &dir_ops );
    return (struct fd *)grab_object( dir->fd );
}
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static int get_dir_unix_fd( struct dir *dir )
{
    return get_unix_fd( dir->fd );
}

static struct security_descriptor *dir_get_sd( struct object *obj )
{
    struct dir *dir = (struct dir *)obj;
    int unix_fd;
    struct stat st;
    struct security_descriptor *sd;
    assert( obj->ops == &dir_ops );

    unix_fd = get_dir_unix_fd( dir );

    if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
        return obj->sd;

    /* mode and uid the same? if so, no need to re-generate security descriptor */
    if (obj->sd &&
        (st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
        (st.st_uid == dir->uid))
        return obj->sd;

    sd = mode_to_sd( st.st_mode,
                     security_unix_uid_to_sid( st.st_uid ),
                     token_get_primary_group( current->process->token ));
    if (!sd) return obj->sd;

    dir->mode = st.st_mode;
    dir->uid = st.st_uid;
    free( obj->sd );
    obj->sd = sd;
    return sd;
}

static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
                       unsigned int set_info )
{
    struct dir *dir = (struct dir *)obj;
    const SID *owner;
    mode_t mode;
    int unix_fd;

    assert( obj->ops == &dir_ops );

    unix_fd = get_dir_unix_fd( dir );

    if (unix_fd == -1) return 1;

    if (set_info & OWNER_SECURITY_INFORMATION)
    {
        owner = sd_get_owner( sd );
        if (!owner)
        {
            set_error( STATUS_INVALID_SECURITY_DESCR );
            return 0;
        }
        if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
        {
            /* FIXME: get Unix uid and call fchown */
        }
    }
    else if (obj->sd)
        owner = sd_get_owner( obj->sd );
    else
        owner = token_get_user( current->process->token );

    if (set_info & DACL_SECURITY_INFORMATION)
    {
        /* keep the bits that we don't map to access rights in the ACL */
        mode = dir->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
        mode |= sd_to_mode( sd, owner );

        if (dir->mode != mode)
        {
            if (fchmod( unix_fd, mode ) == -1)
            {
                file_set_error();
                return 0;
            }

            dir->mode = mode;
        }
    }
    return 1;
}

389 390 391 392 393 394 395 396
static struct change_record *get_first_change_record( struct dir *dir )
{
    struct list *ptr = list_head( &dir->change_records );
    if (!ptr) return NULL;
    list_remove( ptr );
    return LIST_ENTRY( ptr, struct change_record, entry );
}

397 398
static void dir_destroy( struct object *obj )
{
399
    struct change_record *record;
400 401
    struct dir *dir = (struct dir *)obj;
    assert (obj->ops == &dir_ops);
402

403 404 405
    if (dir->filter)
        remove_change( dir );

406 407 408 409 410
    if (dir->inode)
    {
        list_remove( &dir->in_entry );
        free_inode( dir->inode );
    }
411

412 413
    while ((record = get_first_change_record( dir ))) free( record );

414
    release_object( dir->fd );
415 416 417 418 419 420

    if (inotify_fd && list_empty( &change_list ))
    {
        release_object( inotify_fd );
        inotify_fd = NULL;
    }
421 422 423 424 425 426 427 428 429 430 431
}

static struct dir *
get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
{
    return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
}

static int dir_get_poll_events( struct fd *fd )
{
    return 0;
432 433
}

434
static enum server_fd_type dir_get_fd_type( struct fd *fd )
435
{
436
    return FD_TYPE_DIR;
437 438
}

439 440
#ifdef USE_INOTIFY

441 442 443
#define HASH_SIZE 31

struct inode {
444 445 446
    struct list ch_entry;    /* entry in the children list */
    struct list children;    /* children of this inode */
    struct inode *parent;    /* parent of this inode */
447 448 449 450 451 452
    struct list dirs;        /* directory handles watching this inode */
    struct list ino_entry;   /* entry in the inode hash */
    struct list wd_entry;    /* entry in the watch descriptor hash */
    dev_t dev;               /* device number */
    ino_t ino;               /* device's inode number */
    int wd;                  /* inotify's watch descriptor */
453
    char *name;              /* basename name of the inode */
454 455 456 457 458
};

struct list inode_hash[ HASH_SIZE ];
struct list wd_hash[ HASH_SIZE ];

459 460
static int inotify_add_dir( char *path, unsigned int filter );

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static struct inode *inode_from_wd( int wd )
{
    struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
    struct inode *inode;

    LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
        if (inode->wd == wd)
            return inode;

    return NULL;
}

static inline struct list *get_hash_list( dev_t dev, ino_t ino )
{
    return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
}

478
static struct inode *find_inode( dev_t dev, ino_t ino )
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
    struct list *bucket = get_hash_list( dev, ino );
    struct inode *inode;

    LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
        if (inode->ino == ino && inode->dev == dev)
             return inode;

    return NULL;
}

static struct inode *create_inode( dev_t dev, ino_t ino )
{
    struct inode *inode;

    inode = malloc( sizeof *inode );
    if (inode)
    {
497
        list_init( &inode->children );
498 499 500 501
        list_init( &inode->dirs );
        inode->ino = ino;
        inode->dev = dev;
        inode->wd = -1;
502 503
        inode->parent = NULL;
        inode->name = NULL;
504 505 506 507 508
        list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
    }
    return inode;
}

509 510 511 512 513 514 515 516 517 518
static struct inode *get_inode( dev_t dev, ino_t ino )
{
    struct inode *inode;

    inode = find_inode( dev, ino );
    if (inode)
        return inode;
    return create_inode( dev, ino );
}

519 520 521 522 523 524 525 526
static void inode_set_wd( struct inode *inode, int wd )
{
    if (inode->wd != -1)
        list_remove( &inode->wd_entry );
    inode->wd = wd;
    list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
}

527 528
static void inode_set_name( struct inode *inode, const char *name )
{
529
    free (inode->name);
530 531 532
    inode->name = name ? strdup( name ) : NULL;
}

533 534
static void free_inode( struct inode *inode )
{
535
    int subtree = 0, watches = 0;
536
    struct inode *tmp, *next;
537 538 539 540 541 542 543 544 545 546
    struct dir *dir;

    LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
    {
        subtree |= dir->subtree;
        watches++;
    }

    if (!subtree && !inode->parent)
    {
547 548
        LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
                                  struct inode, ch_entry )
549 550 551 552 553 554 555 556
        {
            assert( tmp != inode );
            assert( tmp->parent == inode );
            free_inode( tmp );
        }
    }

    if (watches)
557 558
        return;

559 560 561
    if (inode->parent)
        list_remove( &inode->ch_entry );

562 563 564 565 566 567 568
    /* disconnect remaining children from the parent */
    LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
    {
        list_remove( &tmp->ch_entry );
        tmp->parent = NULL;
    }

569 570 571 572 573 574
    if (inode->wd != -1)
    {
        inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
        list_remove( &inode->wd_entry );
    }
    list_remove( &inode->ino_entry );
575 576

    free( inode->name );
577 578 579
    free( inode );
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
static struct inode *inode_add( struct inode *parent,
                                dev_t dev, ino_t ino, const char *name )
{
    struct inode *inode;
 
    inode = get_inode( dev, ino );
    if (!inode)
        return NULL;
 
    if (!inode->parent)
    {
        list_add_tail( &parent->children, &inode->ch_entry );
        inode->parent = parent;
        assert( inode != parent );
    }
    inode_set_name( inode, name );

    return inode;
}

static struct inode *inode_from_name( struct inode *inode, const char *name )
{
    struct inode *i;

    LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
        if (i->name && !strcmp( i->name, name ))
            return i;
    return NULL;
}

610 611 612 613 614
static int inotify_get_poll_events( struct fd *fd );
static void inotify_poll_event( struct fd *fd, int event );

static const struct fd_ops inotify_fd_ops =
{
615 616
    inotify_get_poll_events,     /* get_poll_events */
    inotify_poll_event,          /* poll_event */
617 618
    NULL,                        /* flush */
    NULL,                        /* get_fd_type */
619
    NULL,                        /* ioctl */
620 621 622
    NULL,                        /* queue_async */
    NULL,                        /* reselect_async */
    NULL,                        /* cancel_async */
623 624 625 626 627 628 629
};

static int inotify_get_poll_events( struct fd *fd )
{
    return POLLIN;
}

630 631
static void inotify_do_change_notify( struct dir *dir, unsigned int action,
                                      const char *relpath )
632
{
633 634
    struct change_record *record;

635 636
    assert( dir->obj.ops == &dir_ops );

637 638
    if (dir->want_data)
    {
639
        size_t len = strlen(relpath);
640
        record = malloc( offsetof(struct change_record, name[len]) );
641 642 643
        if (!record)
            return;

644 645
        record->action = action;
        memcpy( record->name, relpath, len );
646
        record->len = len;
647 648 649

        list_add_tail( &dir->change_records, &record->entry );
    }
650

651
    fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
652 653
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static unsigned int filter_from_event( struct inotify_event *ie )
{
    unsigned int filter = 0;

    if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
        filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
    if (ie->mask & IN_MODIFY)
        filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
    if (ie->mask & IN_ATTRIB)
        filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
    if (ie->mask & IN_ACCESS)
        filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
    if (ie->mask & IN_CREATE)
        filter |= FILE_NOTIFY_CHANGE_CREATION;

    return filter;
}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
/* scan up the parent directories for watches */
static unsigned int filter_from_inode( struct inode *inode, int is_parent )
{
    unsigned int filter = 0;
    struct dir *dir;

    /* combine filters from parents watching subtrees */
    while (inode)
    {
        LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
            if (dir->subtree || !is_parent)
                filter |= dir->filter;
        is_parent = 1;
        inode = inode->parent;
    }

    return filter;
}

static char *inode_get_path( struct inode *inode, int sz )
692 693
{
    struct list *head;
694 695 696 697 698
    char *path;
    int len;

    if (!inode)
        return NULL;
699 700

    head = list_head( &inode->dirs );
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    if (head)
    {
        int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
        path = malloc ( 32 + sz );
        if (path)
            sprintf( path, "/proc/self/fd/%u/", unix_fd );
        return path;
    }

    if (!inode->name)
        return NULL;

    len = strlen( inode->name );
    path = inode_get_path( inode->parent, sz + len + 1 );
    if (!path)
        return NULL;
    
    strcat( path, inode->name );
    strcat( path, "/" );

    return path;
}

static int inode_check_dir( struct inode *parent, const char *name )
{
    char *path;
    unsigned int filter;
    struct inode *inode;
    struct stat st;
    int wd = -1, r = -1;
731

732
    path = inode_get_path( parent, strlen(name) );
733
    if (!path)
734 735 736
        return r;

    strcat( path, name );
737 738

    r = stat( path, &st );
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    if (r < 0) goto end;

    if (!S_ISDIR(st.st_mode))
    {
        r = 0;
        goto end;
    }

    r = 1;

    filter = filter_from_inode( parent, 1 );
    if (!filter)
        goto end;

    inode = inode_add( parent, st.st_dev, st.st_ino, name );
    if (!inode || inode->wd != -1)
        goto end;

    wd = inotify_add_dir( path, filter );
    if (wd != -1)
        inode_set_wd( inode, wd );
    else
        free_inode( inode );

end:
764
    free( path );
765 766
    return r;
}
767

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
static int prepend( char **path, const char *segment )
{
    int extra;
    char *p;

    extra = strlen( segment ) + 1;
    if (*path)
    {
        int len = strlen( *path ) + 1;
        p = realloc( *path, len + extra );
        if (!p) return 0;
        memmove( &p[ extra ], p, len );
        p[ extra - 1 ] = '/';
        memcpy( p, segment, extra - 1 );
    }
    else
    {
        p = malloc( extra );
        if (!p) return 0;
        memcpy( p, segment, extra );
    }

    *path = p;

    return 1;
793 794
}

795 796
static void inotify_notify_all( struct inotify_event *ie )
{
797
    unsigned int filter, action;
798 799
    struct inode *inode, *i;
    char *path = NULL;
800 801 802 803 804 805 806 807 808 809
    struct dir *dir;

    inode = inode_from_wd( ie->wd );
    if (!inode)
    {
        fprintf( stderr, "no inode matches %d\n", ie->wd);
        return;
    }

    filter = filter_from_event( ie );
810 811 812
    
    if (ie->mask & IN_CREATE)
    {
813
        switch (inode_check_dir( inode, ie->name ))
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
        {
        case 1:
            filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
            break;
        case 0:
            filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
            break;
        default:
            break;
            /* Maybe the file disappeared before we could check it? */
        }
        action = FILE_ACTION_ADDED;
    }
    else if (ie->mask & IN_DELETE)
        action = FILE_ACTION_REMOVED;
    else
        action = FILE_ACTION_MODIFIED;
831

832
    /*
833
     * Work our way up the inode hierarchy
834
     *  extending the relative path as we go
835
     *  and notifying all recursive watches.
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
     */
    if (!prepend( &path, ie->name ))
        return;

    for (i = inode; i; i = i->parent)
    {
        LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
            if ((filter & dir->filter) && (i==inode || dir->subtree))
                inotify_do_change_notify( dir, action, path );

        if (!i->name || !prepend( &path, i->name ))
            break;
    }

    free( path );

    if (ie->mask & IN_DELETE)
    {
        i = inode_from_name( inode, ie->name );
        if (i)
            free_inode( i );
    }
858 859
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873
static void inotify_poll_event( struct fd *fd, int event )
{
    int r, ofs, unix_fd;
    char buffer[0x1000];
    struct inotify_event *ie;

    unix_fd = get_unix_fd( fd );
    r = read( unix_fd, buffer, sizeof buffer );
    if (r < 0)
    {
        fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
        return;
    }

874
    for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
875 876 877 878
    {
        ie = (struct inotify_event*) &buffer[ofs];
        if (!ie->len)
            break;
879 880
        ofs += offsetof( struct inotify_event, name[ie->len] );
        if (ofs > r) break;
881
        inotify_notify_all( ie );
882 883 884
    }
}

885
static inline struct fd *create_inotify_fd( void )
886 887 888 889 890 891
{
    int unix_fd;

    unix_fd = inotify_init();
    if (unix_fd<0)
        return NULL;
892
    return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
893 894
}

895
static int map_flags( unsigned int filter )
896
{
897 898 899 900
    unsigned int mask;

    /* always watch these so we can track subdirectories in recursive watches */
    mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
901

902 903 904 905 906 907 908 909 910 911 912
    if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
        mask |= IN_ATTRIB;
    if (filter & FILE_NOTIFY_CHANGE_SIZE)
        mask |= IN_MODIFY;
    if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
        mask |= IN_MODIFY;
    if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
        mask |= IN_ACCESS;
    if (filter & FILE_NOTIFY_CHANGE_SECURITY)
        mask |= IN_ATTRIB;

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    return mask;
}

static int inotify_add_dir( char *path, unsigned int filter )
{
    int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
                                path, map_flags( filter ) );
    if (wd != -1)
        set_fd_events( inotify_fd, POLLIN );
    return wd;
}

static int init_inotify( void )
{
    int i;

    if (inotify_fd)
        return 1;

    inotify_fd = create_inotify_fd();
    if (!inotify_fd)
        return 0;

    for (i=0; i<HASH_SIZE; i++)
    {
        list_init( &inode_hash[i] );
        list_init( &wd_hash[i] );
    }

    return 1;
}

static int inotify_adjust_changes( struct dir *dir )
{
    unsigned int filter;
    struct inode *inode;
    struct stat st;
    char path[32];
    int wd, unix_fd;

    if (!inotify_fd)
        return 0;

    unix_fd = get_unix_fd( dir->fd );

    inode = dir->inode;
    if (!inode)
    {
        /* check if this fd is already being watched */
        if (-1 == fstat( unix_fd, &st ))
            return 0;

        inode = get_inode( st.st_dev, st.st_ino );
        if (!inode)
            inode = create_inode( st.st_dev, st.st_ino );
        if (!inode)
            return 0;
        list_add_tail( &inode->dirs, &dir->in_entry );
        dir->inode = inode;
    }

974
    filter = filter_from_inode( inode, 0 );
975 976 977 978 979 980 981

    sprintf( path, "/proc/self/fd/%u", unix_fd );
    wd = inotify_add_dir( path, filter );
    if (wd == -1) return 0;

    inode_set_wd( inode, wd );

982
    return 1;
983 984
}

985 986 987 988 989 990 991 992
static char *get_basename( const char *link )
{
    char *buffer, *name = NULL;
    int r, n = 0x100;

    while (1)
    {
        buffer = malloc( n );
993
        if (!buffer) return NULL;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

        r = readlink( link, buffer, n );
        if (r < 0)
            break;

        if (r < n)
        {
            name = buffer;
            break;
        }
        free( buffer );
        n *= 2;
    }

    if (name)
    {
        while (r > 0 && name[ r - 1 ] == '/' )
            r--;
        name[ r ] = 0;

        name = strrchr( name, '/' );
        if (name)
            name = strdup( &name[1] );
    }

    free( buffer );
    return name;
}

static int dir_add_to_existing_notify( struct dir *dir )
{
    struct inode *inode, *parent;
    unsigned int filter = 0;
    struct stat st, st_new;
    char link[35], *name;
    int wd, unix_fd;

    if (!inotify_fd)
        return 0;

    unix_fd = get_unix_fd( dir->fd );

    /* check if it's in the list of inodes we want to watch */
    if (-1 == fstat( unix_fd, &st_new ))
        return 0;
    inode = find_inode( st_new.st_dev, st_new.st_ino );
    if (inode)
        return 0;

    /* lookup the parent */
    sprintf( link, "/proc/self/fd/%u/..", unix_fd );
    if (-1 == stat( link, &st ))
        return 0;

    /*
     * If there's no parent, stop.  We could keep going adding
     *  ../ to the path until we hit the root of the tree or
     *  find a recursively watched ancestor.
     * Assume it's too expensive to search up the tree for now.
     */
    parent = find_inode( st.st_dev, st.st_ino );
    if (!parent)
        return 0;

    if (parent->wd == -1)
        return 0;

    filter = filter_from_inode( parent, 1 );
    if (!filter)
        return 0;

    sprintf( link, "/proc/self/fd/%u", unix_fd );
    name = get_basename( link );
    if (!name)
        return 0;
    inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
    free( name );
    if (!inode)
        return 0;

    /* Couldn't find this inode at the start of the function, must be new */
    assert( inode->wd == -1 );

    wd = inotify_add_dir( link, filter );
    if (wd != -1)
        inode_set_wd( inode, wd );

    return 1;
}

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
#else

static int init_inotify( void )
{
    return 0;
}

static int inotify_adjust_changes( struct dir *dir )
{
    return 0;
}

static void free_inode( struct inode *inode )
{
    assert( 0 );
}

1101 1102 1103 1104 1105
static int dir_add_to_existing_notify( struct dir *dir )
{
    return 0;
}

1106
#endif  /* USE_INOTIFY */
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
struct object *create_dir_obj( struct fd *fd )
{
    struct dir *dir;

    dir = alloc_object( &dir_ops );
    if (!dir)
        return NULL;

    list_init( &dir->change_records );
    dir->filter = 0;
    dir->notified = 0;
    dir->want_data = 0;
    dir->inode = NULL;
    grab_object( fd );
    dir->fd = fd;
    set_fd_user( fd, &dir_fd_ops, &dir->obj );

1125 1126
    dir_add_to_existing_notify( dir );

1127 1128 1129
    return &dir->obj;
}

1130 1131 1132 1133
/* enable change notifications for a directory */
DECL_HANDLER(read_directory_changes)
{
    struct dir *dir;
1134
    struct async *async;
1135 1136 1137 1138 1139 1140 1141

    if (!req->filter)
    {
        set_error(STATUS_INVALID_PARAMETER);
        return;
    }

1142
    dir = get_dir_obj( current->process, req->async.handle, 0 );
1143 1144
    if (!dir)
        return;
1145

1146
    /* requests don't timeout */
1147
    if (!(async = fd_queue_async( dir->fd, &req->async, ASYNC_TYPE_WAIT ))) goto end;
1148

1149 1150 1151
    /* assign it once */
    if (!dir->filter)
    {
1152
        init_inotify();
1153 1154
        insert_change( dir );
        dir->filter = req->filter;
1155
        dir->subtree = req->subtree;
1156
        dir->want_data = req->want_data;
1157 1158
    }

1159
    /* if there's already a change in the queue, send it */
1160
    if (!list_empty( &dir->change_records ))
1161
        fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
1162

1163
    /* setup the real notification */
1164
    if (!inotify_adjust_changes( dir ))
1165
        dnotify_adjust_changes( dir );
1166

1167
    release_object( async );
1168 1169 1170 1171
    set_error(STATUS_PENDING);

end:
    release_object( dir );
1172
}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

DECL_HANDLER(read_change)
{
    struct change_record *record;
    struct dir *dir;

    dir = get_dir_obj( current->process, req->handle, 0 );
    if (!dir)
        return;

    if ((record = get_first_change_record( dir )) != NULL)
    {
        reply->action = record->action;
        set_reply_data( record->name, record->len );
        free( record );
    }
    else
        set_error( STATUS_NO_DATA_DETECTED );

    release_object( dir );
}