1998-12-31 08:43:48 -07:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2006-01-27 04:13:56 -07:00
|
|
|
* Copyright (C) 2006 Mike McCormack
|
2002-03-09 16:29:33 -07: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 08:43:48 -07:00
|
|
|
*/
|
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
1998-12-31 08:43:48 -07:00
|
|
|
#include <assert.h>
|
2003-03-26 16:41:43 -07:00
|
|
|
#include <fcntl.h>
|
1998-12-31 08:43:48 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-03-26 16:41:43 -07:00
|
|
|
#include <signal.h>
|
2004-02-04 18:45:58 -07:00
|
|
|
#include <sys/stat.h>
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2005-11-28 09:32:54 -07:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2002-12-10 15:56:43 -07:00
|
|
|
#include "windef.h"
|
1999-05-15 03:48:19 -07:00
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
#include "file.h"
|
1999-05-15 03:48:19 -07:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 10:26:53 -07:00
|
|
|
#include "request.h"
|
2005-11-28 09:32:54 -07:00
|
|
|
#include "winternl.h"
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2004-02-10 13:08:24 -07: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-27 04:13:56 -07:00
|
|
|
struct dir
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2003-03-26 16:41:43 -07:00
|
|
|
struct fd *fd; /* file descriptor to the directory */
|
|
|
|
struct list entry; /* entry in global change notifications list */
|
2006-01-27 04:13:56 -07:00
|
|
|
struct event *event;
|
2003-03-26 16:41:43 -07:00
|
|
|
unsigned int filter; /* notification filter */
|
2005-09-26 06:51:58 -07:00
|
|
|
int notified; /* SIGIO counter */
|
2003-03-26 16:41:43 -07:00
|
|
|
long signaled; /* the file changed */
|
1998-12-31 08:43:48 -07:00
|
|
|
};
|
|
|
|
|
2006-01-27 04:13:56 -07: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 08:43:48 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static const struct object_ops dir_ops =
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
sizeof(struct dir), /* size */
|
|
|
|
dir_dump, /* dump */
|
1999-12-31 17:56:27 -07:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_signaled, /* signaled */
|
1999-12-31 17:56:27 -07:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 10:35:52 -07:00
|
|
|
no_signal, /* signal */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_get_fd, /* get_fd */
|
|
|
|
dir_map_access, /* map_access */
|
2005-11-22 07:55:42 -07:00
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 08:39:52 -07:00
|
|
|
no_close_handle, /* close_handle */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dir_get_poll_events( struct fd *fd );
|
|
|
|
static int dir_get_info( struct fd *fd );
|
|
|
|
|
|
|
|
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 */
|
|
|
|
default_fd_cancel_async /* cancel_async */
|
1998-12-31 08:43:48 -07:00
|
|
|
};
|
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
static struct list change_list = LIST_INIT(change_list);
|
|
|
|
|
|
|
|
static void adjust_changes( int fd, unsigned int filter )
|
|
|
|
{
|
2004-03-01 14:32:02 -07:00
|
|
|
#if defined(F_SETSIG) && defined(F_NOTIFY)
|
2003-03-26 16:41:43 -07:00
|
|
|
unsigned int val;
|
|
|
|
if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = DN_MULTISHOT;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_ATTRIB;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SIZE)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
|
2004-02-04 18:45:58 -07:00
|
|
|
val |= DN_ACCESS;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_CREATION)
|
2004-02-04 18:45:58 -07:00
|
|
|
val |= DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_ATTRIB;
|
|
|
|
fcntl( fd, F_NOTIFY, val );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert change in the global list */
|
2006-01-27 04:13:56 -07:00
|
|
|
static inline void insert_change( struct dir *dir )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 04:13:56 -07:00
|
|
|
list_add_head( &change_list, &dir->entry );
|
2003-03-26 16:41:43 -07:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove change from the global list */
|
2006-01-27 04:13:56 -07:00
|
|
|
static inline void remove_change( struct dir *dir )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 04:13:56 -07:00
|
|
|
list_remove( &dir->entry );
|
2003-03-26 16:41:43 -07:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
struct object *create_dir_obj( struct fd *fd )
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir;
|
2005-08-08 08:11:03 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
dir = alloc_object( &dir_ops );
|
|
|
|
if (!dir)
|
2004-02-04 18:45:58 -07:00
|
|
|
return NULL;
|
2006-01-27 04:13:56 -07:00
|
|
|
|
|
|
|
dir->event = NULL;
|
|
|
|
dir->filter = 0;
|
|
|
|
dir->notified = 0;
|
|
|
|
dir->signaled = 0;
|
|
|
|
grab_object( fd );
|
|
|
|
dir->fd = fd;
|
|
|
|
set_fd_user( fd, &dir_fd_ops, &dir->obj );
|
|
|
|
|
|
|
|
return &dir->obj;
|
1998-12-31 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static void dir_dump( struct object *obj, int verbose )
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07: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 16:41:43 -07:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static int dir_signaled( struct object *obj, struct thread *thread )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert (obj->ops == &dir_ops);
|
|
|
|
return (dir->event == NULL) && dir->signaled;
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enter here directly from SIGIO signal handler */
|
|
|
|
void do_change_notify( int unix_fd )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
/* FIXME: this is O(n) ... probably can be improved */
|
|
|
|
LIST_FOR_EACH( ptr, &change_list )
|
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = LIST_ENTRY( ptr, struct dir, entry );
|
|
|
|
if (get_unix_fd( dir->fd ) != unix_fd) continue;
|
|
|
|
interlocked_xchg_add( &dir->notified, 1 );
|
2003-03-26 16:41:43 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGIO callback, called synchronously with the poll loop */
|
|
|
|
void sigio_callback(void)
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
LIST_FOR_EACH( ptr, &change_list )
|
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = LIST_ENTRY( ptr, struct dir, entry );
|
|
|
|
long count = interlocked_xchg( &dir->notified, 0 );
|
2003-03-26 16:41:43 -07:00
|
|
|
if (count)
|
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
dir->signaled += count;
|
|
|
|
if (dir->signaled == count) /* was it 0? */
|
|
|
|
{
|
|
|
|
if (dir->event)
|
|
|
|
set_event( dir->event );
|
|
|
|
else
|
|
|
|
wake_up( &dir->obj, 0 );
|
|
|
|
}
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
|
|
|
}
|
1998-12-31 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static struct fd *dir_get_fd( struct object *obj )
|
1999-05-15 03:48:19 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
return (struct fd *)grab_object( dir->fd );
|
|
|
|
}
|
2003-03-26 16:41:43 -07:00
|
|
|
|
2006-01-27 04:13:56 -07: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dir_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert (obj->ops == &dir_ops);
|
1999-05-15 03:48:19 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
if (dir->filter)
|
|
|
|
remove_change( dir );
|
|
|
|
|
|
|
|
if (dir->event)
|
1999-05-15 03:48:19 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
set_event( dir->event );
|
|
|
|
release_object( dir->event );
|
1999-05-15 03:48:19 -07:00
|
|
|
}
|
2006-01-27 04:13:56 -07:00
|
|
|
release_object( dir->fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:41:43 -07:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static int dir_get_info( struct fd *fd )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 16:41:43 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
/* possibly send changes through an event flag */
|
|
|
|
if (req->event)
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
|
|
|
|
if (!event)
|
|
|
|
goto end;
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
2006-01-27 04:13:56 -07:00
|
|
|
|
|
|
|
/* discard the current data, and move onto the next event */
|
|
|
|
if (dir->event) release_object( dir->event );
|
|
|
|
dir->event = event;
|
|
|
|
|
|
|
|
/* assign it once */
|
|
|
|
if (!dir->filter)
|
|
|
|
{
|
|
|
|
insert_change( dir );
|
|
|
|
dir->filter = req->filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove any notifications */
|
|
|
|
if (dir->signaled>0)
|
|
|
|
dir->signaled--;
|
|
|
|
|
|
|
|
adjust_changes( get_unix_fd( dir->fd ), dir->filter );
|
|
|
|
|
|
|
|
set_error(STATUS_PENDING);
|
|
|
|
|
|
|
|
end:
|
|
|
|
release_object( dir );
|
1999-05-15 03:48:19 -07:00
|
|
|
}
|