1998-12-31 15:43:48 +00:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
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>
|
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
|
|
|
|
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
|
|
|
|
|
1998-12-31 15:43:48 +00:00
|
|
|
struct change
|
|
|
|
{
|
|
|
|
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 */
|
1998-12-31 15:43:48 +00:00
|
|
|
int subtree; /* watch all the subtree */
|
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 */
|
2003-03-26 23:41:43 +00:00
|
|
|
long signaled; /* the file changed */
|
1998-12-31 15:43:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose );
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread );
|
2003-03-26 23:41:43 +00:00
|
|
|
static void change_destroy( struct object *obj );
|
1998-12-31 15:43:48 +00:00
|
|
|
|
|
|
|
static const struct object_ops change_ops =
|
|
|
|
{
|
2000-01-01 00:56:27 +00: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 17:35:52 +00:00
|
|
|
no_signal, /* signal */
|
2000-12-19 02:12:45 +00:00
|
|
|
no_get_fd, /* get_fd */
|
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 */
|
2003-03-26 23:41:43 +00:00
|
|
|
change_destroy /* destroy */
|
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);
|
|
|
|
|
|
|
|
static void adjust_changes( int fd, unsigned int filter )
|
|
|
|
{
|
2004-03-01 21:32:02 +00:00
|
|
|
#if defined(F_SETSIG) && defined(F_NOTIFY)
|
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 */
|
|
|
|
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 15:43:48 +00:00
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
|
1998-12-31 15:43:48 +00:00
|
|
|
{
|
|
|
|
struct change *change;
|
2004-02-05 01:45:58 +00:00
|
|
|
struct stat st;
|
|
|
|
int unix_fd = get_unix_fd( fd );
|
|
|
|
|
2005-08-08 15:11:03 +00:00
|
|
|
if (unix_fd == -1) return NULL;
|
|
|
|
|
2004-02-05 01:45:58 +00:00
|
|
|
if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NOT_A_DIRECTORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-26 23:41:43 +00:00
|
|
|
|
2003-02-19 00:33:32 +00:00
|
|
|
if ((change = alloc_object( &change_ops )))
|
1999-06-26 08:43:26 +00:00
|
|
|
{
|
2003-03-26 23:41:43 +00: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 01:45:58 +00:00
|
|
|
adjust_changes( unix_fd, filter );
|
1999-06-26 08:43:26 +00:00
|
|
|
}
|
|
|
|
return change;
|
1998-12-31 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct change *change = (struct change *)obj;
|
|
|
|
assert( obj->ops == &change_ops );
|
2003-03-26 23:41:43 +00:00
|
|
|
fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
|
|
|
|
change->fd, change->subtree, change->filter );
|
1998-12-31 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
2003-03-26 23:41:43 +00: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 15:43:48 +00:00
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
/* create a change notification */
|
|
|
|
DECL_HANDLER(create_change_notification)
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
struct change *change;
|
2003-03-26 23:41:43 +00: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 10:48:19 +00:00
|
|
|
|
2003-03-26 23:41:43 +00:00
|
|
|
if ((change = create_change_notification( fd, req->subtree, req->filter )))
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
2001-11-30 18:46:42 +00:00
|
|
|
reply->handle = alloc_handle( current->process, change,
|
|
|
|
STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
|
1999-06-26 08:43:26 +00:00
|
|
|
release_object( change );
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
2003-03-26 23:41:43 +00: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 10:48:19 +00:00
|
|
|
}
|