1998-12-31 16:43:48 +01:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2002-03-10 00:29:33 +01: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 16:43:48 +01:00
|
|
|
*/
|
|
|
|
|
2003-03-27 00:41:43 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
1998-12-31 16:43:48 +01:00
|
|
|
#include <assert.h>
|
2003-03-27 00:41:43 +01:00
|
|
|
#include <fcntl.h>
|
1998-12-31 16:43:48 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-03-27 00:41:43 +01:00
|
|
|
#include <signal.h>
|
2004-02-05 02:45:58 +01:00
|
|
|
#include <sys/stat.h>
|
1998-12-31 16:43:48 +01:00
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2002-12-10 23:56:43 +01:00
|
|
|
#include "windef.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2003-03-27 00:41:43 +01:00
|
|
|
#include "file.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "winternl.h"
|
1998-12-31 16:43:48 +01:00
|
|
|
|
2004-02-10 21:08:24 +01: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
|
|
|
|
|
1998-12-31 16:43:48 +01:00
|
|
|
struct change
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2003-03-27 00:41:43 +01:00
|
|
|
struct fd *fd; /* file descriptor to the directory */
|
|
|
|
struct list entry; /* entry in global change notifications list */
|
1998-12-31 16:43:48 +01:00
|
|
|
int subtree; /* watch all the subtree */
|
2003-03-27 00:41:43 +01:00
|
|
|
unsigned int filter; /* notification filter */
|
2005-09-26 15:51:58 +02:00
|
|
|
int notified; /* SIGIO counter */
|
2003-03-27 00:41:43 +01:00
|
|
|
long signaled; /* the file changed */
|
1998-12-31 16:43:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose );
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread );
|
2003-03-27 00:41:43 +01:00
|
|
|
static void change_destroy( struct object *obj );
|
1998-12-31 16:43:48 +01:00
|
|
|
|
|
|
|
static const struct object_ops change_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct change), /* size */
|
|
|
|
change_dump, /* dump */
|
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
|
|
|
change_signaled, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2000-12-19 03:12:45 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-03-27 00:41:43 +01:00
|
|
|
change_destroy /* destroy */
|
1998-12-31 16:43:48 +01:00
|
|
|
};
|
|
|
|
|
2003-03-27 00:41:43 +01:00
|
|
|
static struct list change_list = LIST_INIT(change_list);
|
|
|
|
|
|
|
|
static void adjust_changes( int fd, unsigned int filter )
|
|
|
|
{
|
2004-03-01 22:32:02 +01:00
|
|
|
#if defined(F_SETSIG) && defined(F_NOTIFY)
|
2003-03-27 00:41:43 +01:00
|
|
|
unsigned int val;
|
|
|
|
if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = DN_MULTISHOT;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_ATTRIB;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SIZE)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
|
2004-02-05 02:45:58 +01:00
|
|
|
val |= DN_ACCESS;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_CREATION)
|
2004-02-05 02:45:58 +01:00
|
|
|
val |= DN_CREATE;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
2003-03-27 00:41:43 +01:00
|
|
|
val |= DN_ATTRIB;
|
|
|
|
fcntl( fd, F_NOTIFY, val );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert change in the global list */
|
|
|
|
static inline void insert_change( struct change *change )
|
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
|
|
|
list_add_head( &change_list, &change->entry );
|
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove change from the global list */
|
|
|
|
static inline void remove_change( struct change *change )
|
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
|
|
|
list_remove( &change->entry );
|
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
1998-12-31 16:43:48 +01:00
|
|
|
|
2003-03-27 00:41:43 +01:00
|
|
|
static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
|
1998-12-31 16:43:48 +01:00
|
|
|
{
|
|
|
|
struct change *change;
|
2004-02-05 02:45:58 +01:00
|
|
|
struct stat st;
|
|
|
|
int unix_fd = get_unix_fd( fd );
|
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
if (unix_fd == -1) return NULL;
|
|
|
|
|
2004-02-05 02:45:58 +01:00
|
|
|
if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NOT_A_DIRECTORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-27 00:41:43 +01:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if ((change = alloc_object( &change_ops )))
|
1999-06-26 10:43:26 +02:00
|
|
|
{
|
2003-03-27 00:41:43 +01:00
|
|
|
change->fd = (struct fd *)grab_object( fd );
|
|
|
|
change->subtree = subtree;
|
|
|
|
change->filter = filter;
|
|
|
|
change->notified = 0;
|
|
|
|
change->signaled = 0;
|
|
|
|
insert_change( change );
|
2004-02-05 02:45:58 +01:00
|
|
|
adjust_changes( unix_fd, filter );
|
1999-06-26 10:43:26 +02:00
|
|
|
}
|
|
|
|
return change;
|
1998-12-31 16:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct change *change = (struct change *)obj;
|
|
|
|
assert( obj->ops == &change_ops );
|
2003-03-27 00:41:43 +01:00
|
|
|
fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
|
|
|
|
change->fd, change->subtree, change->filter );
|
1998-12-31 16:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
2003-03-27 00:41:43 +01:00
|
|
|
struct change *change = (struct change *)obj;
|
|
|
|
|
|
|
|
return change->signaled != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void change_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct change *change = (struct change *)obj;
|
|
|
|
|
|
|
|
release_object( change->fd );
|
|
|
|
remove_change( change );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 )
|
|
|
|
{
|
|
|
|
struct change *change = LIST_ENTRY( ptr, struct change, entry );
|
|
|
|
if (get_unix_fd( change->fd ) != unix_fd) continue;
|
|
|
|
interlocked_xchg_add( &change->notified, 1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGIO callback, called synchronously with the poll loop */
|
|
|
|
void sigio_callback(void)
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
LIST_FOR_EACH( ptr, &change_list )
|
|
|
|
{
|
|
|
|
struct change *change = LIST_ENTRY( ptr, struct change, entry );
|
|
|
|
long count = interlocked_xchg( &change->notified, 0 );
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
change->signaled += count;
|
|
|
|
if (change->signaled == count) /* was it 0? */
|
|
|
|
wake_up( &change->obj, 0 );
|
|
|
|
}
|
|
|
|
}
|
1998-12-31 16:43:48 +01:00
|
|
|
}
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
/* create a change notification */
|
|
|
|
DECL_HANDLER(create_change_notification)
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
struct change *change;
|
2003-03-27 00:41:43 +01:00
|
|
|
struct file *file;
|
|
|
|
struct fd *fd;
|
|
|
|
|
|
|
|
if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
|
|
|
|
fd = get_obj_fd( (struct object *)file );
|
|
|
|
release_object( file );
|
|
|
|
if (!fd) return;
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2003-03-27 00:41:43 +01:00
|
|
|
if ((change = create_change_notification( fd, req->subtree, req->filter )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
2005-12-09 14:52:04 +01:00
|
|
|
reply->handle = alloc_handle( current->process, change, req->access, req->attributes );
|
1999-06-26 10:43:26 +02:00
|
|
|
release_object( change );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
2003-03-27 00:41:43 +01:00
|
|
|
release_object( fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move to the next change notification */
|
|
|
|
DECL_HANDLER(next_change_notification)
|
|
|
|
{
|
|
|
|
struct change *change;
|
|
|
|
|
|
|
|
if ((change = (struct change *)get_handle_obj( current->process, req->handle,
|
|
|
|
0, &change_ops )))
|
|
|
|
{
|
|
|
|
if (change->signaled > 0) change->signaled--;
|
|
|
|
release_object( change );
|
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|