1998-12-31 15:43:48 +00:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2006-01-27 11:13:56 +00:00
|
|
|
* Copyright (C) 2006 Mike McCormack
|
2002-03-09 23:29:33 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1998-12-31 15:43:48 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
1998-12-31 15:43:48 +00:00
|
|
|
#include <assert.h>
|
2003-03-26 23:41:43 +00:00
|
|
|
#include <fcntl.h>
|
1998-12-31 15:43:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-03-26 23:41:43 +00:00
|
|
|
#include <signal.h>
|
2004-02-05 01:45:58 +00:00
|
|
|
#include <sys/stat.h>
|
2006-02-17 10:40:38 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <dirent.h>
|
2006-01-30 17:15:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#endif
|
1998-12-31 15:43:48 +00:00
|
|
|
|
2005-11-28 16:32:54 +00:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2002-12-10 22:56:43 +00:00
|
|
|
#include "windef.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
#include "file.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 17:26:53 +00:00
|
|
|
#include "request.h"
|
2005-11-28 16:32:54 +00:00
|
|
|
#include "winternl.h"
|
1998-12-31 15:43:48 +00:00
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
/* dnotify support */
|
|
|
|
|
2004-02-10 20:08:24 +00:00
|
|
|
#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 */
|
|
|
|
#define DN_ATTRIB 0x00000020 /* File changed attibutes */
|
|
|
|
#define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2006-02-05 11:19:56 +00:00
|
|
|
#define USE_INOTIFY
|
2006-01-30 17:15:31 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
struct inode;
|
|
|
|
|
|
|
|
static void free_inode( struct inode *inode );
|
|
|
|
|
|
|
|
static struct fd *inotify_fd;
|
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
struct change_record {
|
|
|
|
struct list entry;
|
|
|
|
int action;
|
|
|
|
int len;
|
|
|
|
char name[1];
|
|
|
|
};
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
struct dir
|
1998-12-31 15:43:48 +00:00
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2003-03-26 23:41:43 +00:00
|
|
|
struct fd *fd; /* file descriptor to the directory */
|
|
|
|
struct list entry; /* entry in global change notifications list */
|
2006-01-27 11:13:56 +00:00
|
|
|
struct event *event;
|
2003-03-26 23:41:43 +00:00
|
|
|
unsigned int filter; /* notification filter */
|
2005-09-26 13:51:58 +00:00
|
|
|
int notified; /* SIGIO counter */
|
2006-02-07 15:50:36 +00:00
|
|
|
int want_data; /* return change data */
|
2003-03-26 23:41:43 +00:00
|
|
|
long signaled; /* the file changed */
|
2006-02-06 10:58:55 +00:00
|
|
|
struct list change_q; /* change readers */
|
|
|
|
struct list change_records; /* data for the change */
|
2006-02-17 10:40:38 +00:00
|
|
|
struct list in_entry; /* entry in the inode dirs list */
|
|
|
|
struct inode *inode; /* inode of the associated directory */
|
1998-12-31 15:43:48 +00:00
|
|
|
};
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static struct fd *dir_get_fd( struct object *obj );
|
|
|
|
static unsigned int dir_map_access( struct object *obj, unsigned int access );
|
|
|
|
static void dir_dump( struct object *obj, int verbose );
|
|
|
|
static void dir_destroy( struct object *obj );
|
|
|
|
static int dir_signaled( struct object *obj, struct thread *thread );
|
1998-12-31 15:43:48 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static const struct object_ops dir_ops =
|
1998-12-31 15:43:48 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
sizeof(struct dir), /* size */
|
|
|
|
dir_dump, /* dump */
|
2000-01-01 00:56:27 +00:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2006-01-27 11:13:56 +00:00
|
|
|
dir_signaled, /* signaled */
|
2000-01-01 00:56:27 +00:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 17:35:52 +00:00
|
|
|
no_signal, /* signal */
|
2006-01-27 11:13:56 +00:00
|
|
|
dir_get_fd, /* get_fd */
|
|
|
|
dir_map_access, /* map_access */
|
2005-11-22 14:55:42 +00:00
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 15:39:52 +00:00
|
|
|
no_close_handle, /* close_handle */
|
2006-01-27 11:13:56 +00:00
|
|
|
dir_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dir_get_poll_events( struct fd *fd );
|
|
|
|
static int dir_get_info( struct fd *fd );
|
2006-02-06 10:58:55 +00:00
|
|
|
static void dir_cancel_async( struct fd *fd );
|
2006-01-27 11:13:56 +00:00
|
|
|
|
|
|
|
static const struct fd_ops dir_fd_ops =
|
|
|
|
{
|
|
|
|
dir_get_poll_events, /* get_poll_events */
|
|
|
|
default_poll_event, /* poll_event */
|
|
|
|
no_flush, /* flush */
|
|
|
|
dir_get_info, /* get_file_info */
|
|
|
|
default_fd_queue_async, /* queue_async */
|
2006-02-06 10:58:55 +00:00
|
|
|
dir_cancel_async /* cancel_async */
|
1998-12-31 15:43:48 +00:00
|
|
|
};
|
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
static struct list change_list = LIST_INIT(change_list);
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
static void dnotify_adjust_changes( struct dir *dir )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2004-03-01 21:32:02 +00:00
|
|
|
#if defined(F_SETSIG) && defined(F_NOTIFY)
|
2006-01-30 17:15:31 +00:00
|
|
|
int fd = get_unix_fd( dir->fd );
|
|
|
|
unsigned int filter = dir->filter;
|
2003-03-26 23:41:43 +00:00
|
|
|
unsigned int val;
|
|
|
|
if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = DN_MULTISHOT;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_ATTRIB;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SIZE)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
|
2004-02-05 01:45:58 +00:00
|
|
|
val |= DN_ACCESS;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_CREATION)
|
2004-02-05 01:45:58 +00:00
|
|
|
val |= DN_CREATE;
|
2005-06-10 19:54:46 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
2003-03-26 23:41:43 +00:00
|
|
|
val |= DN_ATTRIB;
|
|
|
|
fcntl( fd, F_NOTIFY, val );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert change in the global list */
|
2006-01-27 11:13:56 +00:00
|
|
|
static inline void insert_change( struct dir *dir )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 11:13:56 +00:00
|
|
|
list_add_head( &change_list, &dir->entry );
|
2003-03-26 23:41:43 +00:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove change from the global list */
|
2006-01-27 11:13:56 +00:00
|
|
|
static inline void remove_change( struct dir *dir )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 11:13:56 +00:00
|
|
|
list_remove( &dir->entry );
|
2003-03-26 23:41:43 +00:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
1998-12-31 15:43:48 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static void dir_dump( struct object *obj, int verbose )
|
1998-12-31 15:43:48 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
fprintf( stderr, "Dirfile fd=%p event=%p filter=%08x\n",
|
|
|
|
dir->fd, dir->event, dir->filter );
|
2003-03-26 23:41:43 +00:00
|
|
|
}
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static int dir_signaled( struct object *obj, struct thread *thread )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert (obj->ops == &dir_ops);
|
|
|
|
return (dir->event == NULL) && dir->signaled;
|
2003-03-26 23:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enter here directly from SIGIO signal handler */
|
|
|
|
void do_change_notify( int unix_fd )
|
|
|
|
{
|
2006-01-30 17:15:31 +00:00
|
|
|
struct dir *dir;
|
2003-03-26 23:41:43 +00:00
|
|
|
|
|
|
|
/* FIXME: this is O(n) ... probably can be improved */
|
2006-01-30 17:15:31 +00:00
|
|
|
LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
if (get_unix_fd( dir->fd ) != unix_fd) continue;
|
|
|
|
interlocked_xchg_add( &dir->notified, 1 );
|
2003-03-26 23:41:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
static void dir_signal_changed( struct dir *dir )
|
|
|
|
{
|
|
|
|
if (dir->event)
|
|
|
|
set_event( dir->event );
|
|
|
|
else
|
|
|
|
wake_up( &dir->obj, 0 );
|
|
|
|
}
|
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
/* SIGIO callback, called synchronously with the poll loop */
|
|
|
|
void sigio_callback(void)
|
|
|
|
{
|
2006-01-30 17:15:31 +00:00
|
|
|
struct dir *dir;
|
2003-03-26 23:41:43 +00:00
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
long count = interlocked_xchg( &dir->notified, 0 );
|
2003-03-26 23:41:43 +00:00
|
|
|
if (count)
|
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
dir->signaled += count;
|
|
|
|
if (dir->signaled == count) /* was it 0? */
|
2006-01-30 17:15:31 +00:00
|
|
|
dir_signal_changed( dir );
|
2003-03-26 23:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
1998-12-31 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static struct fd *dir_get_fd( struct object *obj )
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
return (struct fd *)grab_object( dir->fd );
|
|
|
|
}
|
2003-03-26 23:41:43 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static unsigned int dir_map_access( struct object *obj, unsigned int access )
|
|
|
|
{
|
|
|
|
if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
|
|
|
|
if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
|
|
|
|
if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
|
|
|
|
if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
|
|
|
|
return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
|
|
|
|
}
|
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static void dir_destroy( struct object *obj )
|
|
|
|
{
|
2006-02-06 10:58:55 +00:00
|
|
|
struct change_record *record;
|
2006-01-27 11:13:56 +00:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert (obj->ops == &dir_ops);
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
if (dir->filter)
|
|
|
|
remove_change( dir );
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
if (dir->inode)
|
|
|
|
{
|
|
|
|
list_remove( &dir->in_entry );
|
|
|
|
free_inode( dir->inode );
|
|
|
|
}
|
2006-01-30 17:15:31 +00:00
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
|
|
|
|
while ((record = get_first_change_record( dir ))) free( record );
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
if (dir->event)
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
set_event( dir->event );
|
|
|
|
release_object( dir->event );
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
2006-01-27 11:13:56 +00:00
|
|
|
release_object( dir->fd );
|
2006-02-17 10:40:38 +00:00
|
|
|
|
|
|
|
if (inotify_fd && list_empty( &change_list ))
|
|
|
|
{
|
|
|
|
release_object( inotify_fd );
|
|
|
|
inotify_fd = NULL;
|
|
|
|
}
|
2006-01-27 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2003-03-26 23:41:43 +00:00
|
|
|
}
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
static int dir_get_info( struct fd *fd )
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
static void dir_cancel_async( struct fd *fd )
|
|
|
|
{
|
|
|
|
struct dir *dir = (struct dir *) get_fd_user( fd );
|
|
|
|
async_terminate_queue( &dir->change_q, STATUS_CANCELLED );
|
|
|
|
}
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
|
2006-02-05 11:19:56 +00:00
|
|
|
#ifdef USE_INOTIFY
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
#define HASH_SIZE 31
|
|
|
|
|
|
|
|
struct inode {
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct list inode_hash[ HASH_SIZE ];
|
|
|
|
struct list wd_hash[ HASH_SIZE ];
|
|
|
|
|
|
|
|
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 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode *get_inode( dev_t dev, ino_t ino )
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
list_init( &inode->dirs );
|
|
|
|
inode->ino = ino;
|
|
|
|
inode->dev = dev;
|
|
|
|
inode->wd = -1;
|
|
|
|
list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
|
|
|
|
}
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_inode( struct inode *inode )
|
|
|
|
{
|
|
|
|
if (!list_empty( &inode->dirs ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (inode->wd != -1)
|
|
|
|
{
|
|
|
|
inotify_remove_watch( get_unix_fd( inotify_fd ), inode->wd );
|
|
|
|
list_remove( &inode->wd_entry );
|
|
|
|
}
|
|
|
|
list_remove( &inode->ino_entry );
|
|
|
|
free( inode );
|
|
|
|
}
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
static int inotify_get_poll_events( struct fd *fd );
|
|
|
|
static void inotify_poll_event( struct fd *fd, int event );
|
|
|
|
static int inotify_get_info( struct fd *fd );
|
|
|
|
|
|
|
|
static const struct fd_ops inotify_fd_ops =
|
|
|
|
{
|
|
|
|
inotify_get_poll_events, /* get_poll_events */
|
|
|
|
inotify_poll_event, /* poll_event */
|
|
|
|
no_flush, /* flush */
|
|
|
|
inotify_get_info, /* get_file_info */
|
|
|
|
default_fd_queue_async, /* queue_async */
|
|
|
|
default_fd_cancel_async, /* cancel_async */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int inotify_get_poll_events( struct fd *fd )
|
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
static void inotify_do_change_notify( struct dir *dir, struct inotify_event *ie )
|
2006-01-30 17:15:31 +00:00
|
|
|
{
|
2006-02-06 10:58:55 +00:00
|
|
|
struct change_record *record;
|
|
|
|
|
2006-02-07 15:50:36 +00:00
|
|
|
if (dir->want_data)
|
|
|
|
{
|
2006-02-08 14:06:42 +00:00
|
|
|
size_t len = strlen(ie->name);
|
|
|
|
record = malloc( offsetof(struct change_record, name[len]) );
|
2006-02-07 15:50:36 +00:00
|
|
|
if (!record)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( ie->mask & IN_CREATE )
|
|
|
|
record->action = FILE_ACTION_ADDED;
|
|
|
|
else if( ie->mask & IN_DELETE )
|
|
|
|
record->action = FILE_ACTION_REMOVED;
|
|
|
|
else
|
|
|
|
record->action = FILE_ACTION_MODIFIED;
|
2006-02-08 14:06:42 +00:00
|
|
|
memcpy( record->name, ie->name, len );
|
|
|
|
record->len = len;
|
2006-02-07 15:50:36 +00:00
|
|
|
|
|
|
|
list_add_tail( &dir->change_records, &record->entry );
|
|
|
|
}
|
2006-02-06 10:58:55 +00:00
|
|
|
|
|
|
|
if (!list_empty( &dir->change_q ))
|
|
|
|
async_terminate_head( &dir->change_q, STATUS_ALERTED );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->signaled++;
|
|
|
|
dir_signal_changed( dir );
|
|
|
|
}
|
2006-01-30 17:15:31 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inotify_notify_all( struct inotify_event *ie )
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
struct dir *dir;
|
|
|
|
unsigned int filter;
|
|
|
|
|
|
|
|
inode = inode_from_wd( ie->wd );
|
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "no inode matches %d\n", ie->wd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter = filter_from_event( ie );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
|
|
|
|
if (filter & dir->filter)
|
|
|
|
inotify_do_change_notify( dir, ie );
|
|
|
|
}
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-02-08 14:06:42 +00:00
|
|
|
for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
|
2006-01-30 17:15:31 +00:00
|
|
|
{
|
|
|
|
ie = (struct inotify_event*) &buffer[ofs];
|
|
|
|
if (!ie->len)
|
|
|
|
break;
|
2006-02-08 14:06:42 +00:00
|
|
|
ofs += offsetof( struct inotify_event, name[ie->len] );
|
|
|
|
if (ofs > r) break;
|
2006-02-17 10:40:38 +00:00
|
|
|
inotify_notify_all( ie );
|
2006-01-30 17:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inotify_get_info( struct fd *fd )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
static inline struct fd *create_inotify_fd( void )
|
2006-02-05 11:19:56 +00:00
|
|
|
{
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
unix_fd = inotify_init();
|
|
|
|
if (unix_fd<0)
|
|
|
|
return NULL;
|
2006-02-17 10:40:38 +00:00
|
|
|
return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL );
|
2006-02-05 11:19:56 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
static int map_flags( unsigned int filter )
|
2006-01-30 17:15:31 +00:00
|
|
|
{
|
|
|
|
unsigned int mask = 0;
|
2006-02-05 11:19:56 +00:00
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
|
|
|
|
mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE);
|
|
|
|
if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
|
|
|
|
mask |= (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
|
|
|
|
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_CREATION)
|
|
|
|
mask |= IN_CREATE;
|
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
|
|
|
mask |= IN_ATTRIB;
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
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 unsigned int filter_from_inode( struct inode *inode )
|
|
|
|
{
|
|
|
|
unsigned int filter = 0;
|
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
|
|
|
|
filter |= dir->filter;
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter = filter_from_inode( inode );
|
|
|
|
|
|
|
|
sprintf( path, "/proc/self/fd/%u", unix_fd );
|
|
|
|
wd = inotify_add_dir( path, filter );
|
|
|
|
if (wd == -1) return 0;
|
|
|
|
|
|
|
|
inode_set_wd( inode, wd );
|
|
|
|
|
2006-02-05 11:19:56 +00:00
|
|
|
return 1;
|
2006-01-30 17:15:31 +00:00
|
|
|
}
|
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
#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 );
|
|
|
|
}
|
|
|
|
|
2006-02-05 11:19:56 +00:00
|
|
|
#endif /* USE_INOTIFY */
|
2006-01-30 17:15:31 +00:00
|
|
|
|
2006-02-17 10:40:38 +00:00
|
|
|
struct object *create_dir_obj( struct fd *fd )
|
|
|
|
{
|
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
dir = alloc_object( &dir_ops );
|
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_init( &dir->change_q );
|
|
|
|
list_init( &dir->change_records );
|
|
|
|
dir->event = NULL;
|
|
|
|
dir->filter = 0;
|
|
|
|
dir->notified = 0;
|
|
|
|
dir->signaled = 0;
|
|
|
|
dir->want_data = 0;
|
|
|
|
dir->inode = NULL;
|
|
|
|
grab_object( fd );
|
|
|
|
dir->fd = fd;
|
|
|
|
set_fd_user( fd, &dir_fd_ops, &dir->obj );
|
|
|
|
|
|
|
|
return &dir->obj;
|
|
|
|
}
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
/* enable change notifications for a directory */
|
|
|
|
DECL_HANDLER(read_directory_changes)
|
|
|
|
{
|
|
|
|
struct event *event = NULL;
|
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
if (!req->filter)
|
|
|
|
{
|
|
|
|
set_error(STATUS_INVALID_PARAMETER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = get_dir_obj( current->process, req->handle, 0 );
|
|
|
|
if (!dir)
|
|
|
|
return;
|
2003-03-26 23:41:43 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
/* possibly send changes through an event flag */
|
|
|
|
if (req->event)
|
2003-03-26 23:41:43 +00:00
|
|
|
{
|
2006-01-27 11:13:56 +00:00
|
|
|
event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
|
|
|
|
if (!event)
|
|
|
|
goto end;
|
2003-03-26 23:41:43 +00:00
|
|
|
}
|
2006-01-27 11:13:56 +00:00
|
|
|
|
|
|
|
/* discard the current data, and move onto the next event */
|
|
|
|
if (dir->event) release_object( dir->event );
|
|
|
|
dir->event = event;
|
|
|
|
|
2006-02-06 10:58:55 +00:00
|
|
|
/* requests don't timeout */
|
|
|
|
if ( req->io_apc && !create_async( current, NULL, &dir->change_q,
|
|
|
|
req->io_apc, req->io_user, req->io_sb ))
|
|
|
|
return;
|
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
/* assign it once */
|
|
|
|
if (!dir->filter)
|
|
|
|
{
|
2006-02-17 10:40:38 +00:00
|
|
|
init_inotify();
|
2006-01-27 11:13:56 +00:00
|
|
|
insert_change( dir );
|
|
|
|
dir->filter = req->filter;
|
2006-02-07 15:50:36 +00:00
|
|
|
dir->want_data = req->want_data;
|
2006-01-27 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove any notifications */
|
|
|
|
if (dir->signaled>0)
|
|
|
|
dir->signaled--;
|
|
|
|
|
2006-02-07 15:50:36 +00:00
|
|
|
/* clear the event */
|
|
|
|
if (event)
|
|
|
|
reset_event( event );
|
|
|
|
|
2006-01-30 17:15:31 +00:00
|
|
|
/* setup the real notification */
|
2006-02-05 11:19:56 +00:00
|
|
|
if (!inotify_adjust_changes( dir ))
|
2006-01-30 17:15:31 +00:00
|
|
|
dnotify_adjust_changes( dir );
|
2006-01-27 11:13:56 +00:00
|
|
|
|
|
|
|
set_error(STATUS_PENDING);
|
|
|
|
|
|
|
|
end:
|
|
|
|
release_object( dir );
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
2006-02-06 10:58:55 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
/* now signal it */
|
|
|
|
dir->signaled++;
|
|
|
|
dir_signal_changed( dir );
|
|
|
|
|
|
|
|
release_object( dir );
|
|
|
|
}
|