1998-12-31 08:43:48 -07:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
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
|
|
|
|
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"
|
1998-12-31 08:43:48 -07:00
|
|
|
|
|
|
|
struct change
|
|
|
|
{
|
|
|
|
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 */
|
1998-12-31 08:43:48 -07:00
|
|
|
int subtree; /* watch all the subtree */
|
2003-03-26 16:41:43 -07:00
|
|
|
unsigned int filter; /* notification filter */
|
|
|
|
long notified; /* SIGIO counter */
|
|
|
|
long signaled; /* the file changed */
|
1998-12-31 08:43:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose );
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread );
|
2003-03-26 16:41:43 -07:00
|
|
|
static void change_destroy( struct object *obj );
|
1998-12-31 08:43:48 -07:00
|
|
|
|
|
|
|
static const struct object_ops change_ops =
|
|
|
|
{
|
1999-12-31 17:56:27 -07:00
|
|
|
sizeof(struct change), /* size */
|
|
|
|
change_dump, /* dump */
|
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
|
|
|
change_signaled, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
2000-12-18 19:12:45 -07:00
|
|
|
no_get_fd, /* get_fd */
|
2003-03-26 16:41:43 -07:00
|
|
|
change_destroy /* destroy */
|
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 )
|
|
|
|
{
|
|
|
|
#ifdef F_NOTIFY
|
|
|
|
unsigned int val;
|
|
|
|
if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = DN_MULTISHOT;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_FILE_NAME )
|
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_DIR_NAME )
|
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_ATTRIBUTES )
|
|
|
|
val |= DN_ATTRIB;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_SIZE )
|
|
|
|
val |= DN_MODIFY;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_LAST_WRITE )
|
|
|
|
val |= DN_MODIFY;
|
2004-02-04 18:45:58 -07:00
|
|
|
if( filter & FILE_NOTIFY_CHANGE_LAST_ACCESS )
|
|
|
|
val |= DN_ACCESS;
|
|
|
|
if( filter & FILE_NOTIFY_CHANGE_CREATION )
|
|
|
|
val |= DN_CREATE;
|
2003-03-26 16:41:43 -07:00
|
|
|
if( filter & FILE_NOTIFY_CHANGE_SECURITY )
|
|
|
|
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 08:43:48 -07:00
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
|
|
|
struct change *change;
|
2004-02-04 18:45:58 -07:00
|
|
|
struct stat st;
|
|
|
|
int unix_fd = get_unix_fd( fd );
|
|
|
|
|
|
|
|
if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NOT_A_DIRECTORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-26 16:41:43 -07:00
|
|
|
|
2003-02-18 17:33:32 -07:00
|
|
|
if ((change = alloc_object( &change_ops )))
|
1999-06-26 01:43:26 -07:00
|
|
|
{
|
2003-03-26 16:41:43 -07: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-04 18:45:58 -07:00
|
|
|
adjust_changes( unix_fd, filter );
|
1999-06-26 01:43:26 -07:00
|
|
|
}
|
|
|
|
return change;
|
1998-12-31 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void change_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct change *change = (struct change *)obj;
|
|
|
|
assert( obj->ops == &change_ops );
|
2003-03-26 16:41:43 -07:00
|
|
|
fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
|
|
|
|
change->fd, change->subtree, change->filter );
|
1998-12-31 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int change_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
2003-03-26 16:41:43 -07: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 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
1999-05-15 03:48:19 -07:00
|
|
|
/* create a change notification */
|
|
|
|
DECL_HANDLER(create_change_notification)
|
|
|
|
{
|
1999-06-26 01:43:26 -07:00
|
|
|
struct change *change;
|
2003-03-26 16:41:43 -07: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 03:48:19 -07:00
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
if ((change = create_change_notification( fd, req->subtree, req->filter )))
|
1999-05-15 03:48:19 -07:00
|
|
|
{
|
2001-11-30 11:46:42 -07:00
|
|
|
reply->handle = alloc_handle( current->process, change,
|
|
|
|
STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
|
1999-06-26 01:43:26 -07:00
|
|
|
release_object( change );
|
1999-05-15 03:48:19 -07:00
|
|
|
}
|
2003-03-26 16:41:43 -07: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 03:48:19 -07:00
|
|
|
}
|