blob: 2d426443794758104f20f4984197584e730d2c40 [file] [log] [blame]
Alexandre Julliard63cb0f81998-12-31 15:43:48 +00001/*
2 * Server-side change notification management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000019 */
20
Alexandre Julliard3e588e32003-03-26 23:41:43 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000024#include <assert.h>
Alexandre Julliard3e588e32003-03-26 23:41:43 +000025#include <fcntl.h>
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000026#include <stdio.h>
27#include <stdlib.h>
Alexandre Julliard3e588e32003-03-26 23:41:43 +000028#include <signal.h>
Hans Leidekkerff496522004-02-05 01:45:58 +000029#include <sys/stat.h>
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000030
Alexandre Julliard435e2e62002-12-10 22:56:43 +000031#include "windef.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000032
Alexandre Julliard3e588e32003-03-26 23:41:43 +000033#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000034#include "handle.h"
35#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000036#include "request.h"
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000037
Huw Daviesbdb63ec2004-02-10 20:08:24 +000038#ifdef linux
39#ifndef F_NOTIFY
40#define F_NOTIFY 1026
41#define DN_ACCESS 0x00000001 /* File accessed */
42#define DN_MODIFY 0x00000002 /* File modified */
43#define DN_CREATE 0x00000004 /* File created */
44#define DN_DELETE 0x00000008 /* File removed */
45#define DN_RENAME 0x00000010 /* File renamed */
46#define DN_ATTRIB 0x00000020 /* File changed attibutes */
47#define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
48#endif
49#endif
50
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000051struct change
52{
53 struct object obj; /* object header */
Alexandre Julliard3e588e32003-03-26 23:41:43 +000054 struct fd *fd; /* file descriptor to the directory */
55 struct list entry; /* entry in global change notifications list */
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000056 int subtree; /* watch all the subtree */
Alexandre Julliard3e588e32003-03-26 23:41:43 +000057 unsigned int filter; /* notification filter */
58 long notified; /* SIGIO counter */
59 long signaled; /* the file changed */
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000060};
61
62static void change_dump( struct object *obj, int verbose );
63static int change_signaled( struct object *obj, struct thread *thread );
Alexandre Julliard3e588e32003-03-26 23:41:43 +000064static void change_destroy( struct object *obj );
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000065
66static const struct object_ops change_ops =
67{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000068 sizeof(struct change), /* size */
69 change_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 change_signaled, /* signaled */
73 no_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000074 no_get_fd, /* get_fd */
Alexandre Julliard3e588e32003-03-26 23:41:43 +000075 change_destroy /* destroy */
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000076};
77
Alexandre Julliard3e588e32003-03-26 23:41:43 +000078static struct list change_list = LIST_INIT(change_list);
Alexandre Julliard63cb0f81998-12-31 15:43:48 +000079
Alexandre Julliard3e588e32003-03-26 23:41:43 +000080static void adjust_changes( int fd, unsigned int filter )
81{
Alexandre Julliarde8a339c2004-03-01 21:32:02 +000082#if defined(F_SETSIG) && defined(F_NOTIFY)
Alexandre Julliard3e588e32003-03-26 23:41:43 +000083 unsigned int val;
84 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
85 return;
86
87 val = DN_MULTISHOT;
88 if( filter & FILE_NOTIFY_CHANGE_FILE_NAME )
89 val |= DN_RENAME | DN_DELETE | DN_CREATE;
90 if( filter & FILE_NOTIFY_CHANGE_DIR_NAME )
91 val |= DN_RENAME | DN_DELETE | DN_CREATE;
92 if( filter & FILE_NOTIFY_CHANGE_ATTRIBUTES )
93 val |= DN_ATTRIB;
94 if( filter & FILE_NOTIFY_CHANGE_SIZE )
95 val |= DN_MODIFY;
96 if( filter & FILE_NOTIFY_CHANGE_LAST_WRITE )
97 val |= DN_MODIFY;
Hans Leidekkerff496522004-02-05 01:45:58 +000098 if( filter & FILE_NOTIFY_CHANGE_LAST_ACCESS )
99 val |= DN_ACCESS;
100 if( filter & FILE_NOTIFY_CHANGE_CREATION )
101 val |= DN_CREATE;
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000102 if( filter & FILE_NOTIFY_CHANGE_SECURITY )
103 val |= DN_ATTRIB;
104 fcntl( fd, F_NOTIFY, val );
105#endif
106}
107
108/* insert change in the global list */
109static inline void insert_change( struct change *change )
110{
111 sigset_t sigset;
112
113 sigemptyset( &sigset );
114 sigaddset( &sigset, SIGIO );
115 sigprocmask( SIG_BLOCK, &sigset, NULL );
116 list_add_head( &change_list, &change->entry );
117 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
118}
119
120/* remove change from the global list */
121static inline void remove_change( struct change *change )
122{
123 sigset_t sigset;
124
125 sigemptyset( &sigset );
126 sigaddset( &sigset, SIGIO );
127 sigprocmask( SIG_BLOCK, &sigset, NULL );
128 list_remove( &change->entry );
129 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
130}
131
132static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
Alexandre Julliard63cb0f81998-12-31 15:43:48 +0000133{
134 struct change *change;
Hans Leidekkerff496522004-02-05 01:45:58 +0000135 struct stat st;
136 int unix_fd = get_unix_fd( fd );
137
138 if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
139 {
140 set_error( STATUS_NOT_A_DIRECTORY );
141 return NULL;
142 }
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000143
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000144 if ((change = alloc_object( &change_ops )))
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000145 {
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000146 change->fd = (struct fd *)grab_object( fd );
147 change->subtree = subtree;
148 change->filter = filter;
149 change->notified = 0;
150 change->signaled = 0;
151 insert_change( change );
Hans Leidekkerff496522004-02-05 01:45:58 +0000152 adjust_changes( unix_fd, filter );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000153 }
154 return change;
Alexandre Julliard63cb0f81998-12-31 15:43:48 +0000155}
156
157static void change_dump( struct object *obj, int verbose )
158{
159 struct change *change = (struct change *)obj;
160 assert( obj->ops == &change_ops );
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000161 fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
162 change->fd, change->subtree, change->filter );
Alexandre Julliard63cb0f81998-12-31 15:43:48 +0000163}
164
165static int change_signaled( struct object *obj, struct thread *thread )
166{
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000167 struct change *change = (struct change *)obj;
168
169 return change->signaled != 0;
170}
171
172static void change_destroy( struct object *obj )
173{
174 struct change *change = (struct change *)obj;
175
176 release_object( change->fd );
177 remove_change( change );
178}
179
180/* enter here directly from SIGIO signal handler */
181void do_change_notify( int unix_fd )
182{
183 struct list *ptr;
184
185 /* FIXME: this is O(n) ... probably can be improved */
186 LIST_FOR_EACH( ptr, &change_list )
187 {
188 struct change *change = LIST_ENTRY( ptr, struct change, entry );
189 if (get_unix_fd( change->fd ) != unix_fd) continue;
190 interlocked_xchg_add( &change->notified, 1 );
191 break;
192 }
193}
194
195/* SIGIO callback, called synchronously with the poll loop */
196void sigio_callback(void)
197{
198 struct list *ptr;
199
200 LIST_FOR_EACH( ptr, &change_list )
201 {
202 struct change *change = LIST_ENTRY( ptr, struct change, entry );
203 long count = interlocked_xchg( &change->notified, 0 );
204 if (count)
205 {
206 change->signaled += count;
207 if (change->signaled == count) /* was it 0? */
208 wake_up( &change->obj, 0 );
209 }
210 }
Alexandre Julliard63cb0f81998-12-31 15:43:48 +0000211}
212
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000213/* create a change notification */
214DECL_HANDLER(create_change_notification)
215{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000216 struct change *change;
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000217 struct file *file;
218 struct fd *fd;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000219
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000220 if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
221 fd = get_obj_fd( (struct object *)file );
222 release_object( file );
223 if (!fd) return;
224
225 if ((change = create_change_notification( fd, req->subtree, req->filter )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000226 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000227 reply->handle = alloc_handle( current->process, change,
228 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000229 release_object( change );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000230 }
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000231 release_object( fd );
232}
233
234/* move to the next change notification */
235DECL_HANDLER(next_change_notification)
236{
237 struct change *change;
238
239 if ((change = (struct change *)get_handle_obj( current->process, req->handle,
240 0, &change_ops )))
241 {
242 if (change->signaled > 0) change->signaled--;
243 release_object( change );
244 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000245}