2 * Server-side change notification management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
40 struct object obj; /* object header */
41 struct fd *fd; /* file descriptor to the directory */
42 struct list entry; /* entry in global change notifications list */
43 int subtree; /* watch all the subtree */
44 unsigned int filter; /* notification filter */
45 long notified; /* SIGIO counter */
46 long signaled; /* the file changed */
49 static void change_dump( struct object *obj, int verbose );
50 static int change_signaled( struct object *obj, struct thread *thread );
51 static void change_destroy( struct object *obj );
53 static const struct object_ops change_ops =
55 sizeof(struct change), /* size */
56 change_dump, /* dump */
57 add_queue, /* add_queue */
58 remove_queue, /* remove_queue */
59 change_signaled, /* signaled */
60 no_satisfied, /* satisfied */
61 no_get_fd, /* get_fd */
62 change_destroy /* destroy */
65 static struct list change_list = LIST_INIT(change_list);
67 static void adjust_changes( int fd, unsigned int filter )
71 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
75 if( filter & FILE_NOTIFY_CHANGE_FILE_NAME )
76 val |= DN_RENAME | DN_DELETE | DN_CREATE;
77 if( filter & FILE_NOTIFY_CHANGE_DIR_NAME )
78 val |= DN_RENAME | DN_DELETE | DN_CREATE;
79 if( filter & FILE_NOTIFY_CHANGE_ATTRIBUTES )
81 if( filter & FILE_NOTIFY_CHANGE_SIZE )
83 if( filter & FILE_NOTIFY_CHANGE_LAST_WRITE )
85 if( filter & FILE_NOTIFY_CHANGE_SECURITY )
87 fcntl( fd, F_NOTIFY, val );
91 /* insert change in the global list */
92 static inline void insert_change( struct change *change )
96 sigemptyset( &sigset );
97 sigaddset( &sigset, SIGIO );
98 sigprocmask( SIG_BLOCK, &sigset, NULL );
99 list_add_head( &change_list, &change->entry );
100 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
103 /* remove change from the global list */
104 static inline void remove_change( struct change *change )
108 sigemptyset( &sigset );
109 sigaddset( &sigset, SIGIO );
110 sigprocmask( SIG_BLOCK, &sigset, NULL );
111 list_remove( &change->entry );
112 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
115 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
117 struct change *change;
119 if ((change = alloc_object( &change_ops )))
121 change->fd = (struct fd *)grab_object( fd );
122 change->subtree = subtree;
123 change->filter = filter;
124 change->notified = 0;
125 change->signaled = 0;
126 insert_change( change );
127 adjust_changes( get_unix_fd(fd), filter );
132 static void change_dump( struct object *obj, int verbose )
134 struct change *change = (struct change *)obj;
135 assert( obj->ops == &change_ops );
136 fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
137 change->fd, change->subtree, change->filter );
140 static int change_signaled( struct object *obj, struct thread *thread )
142 struct change *change = (struct change *)obj;
144 return change->signaled != 0;
147 static void change_destroy( struct object *obj )
149 struct change *change = (struct change *)obj;
151 release_object( change->fd );
152 remove_change( change );
155 /* enter here directly from SIGIO signal handler */
156 void do_change_notify( int unix_fd )
160 /* FIXME: this is O(n) ... probably can be improved */
161 LIST_FOR_EACH( ptr, &change_list )
163 struct change *change = LIST_ENTRY( ptr, struct change, entry );
164 if (get_unix_fd( change->fd ) != unix_fd) continue;
165 interlocked_xchg_add( &change->notified, 1 );
170 /* SIGIO callback, called synchronously with the poll loop */
171 void sigio_callback(void)
175 LIST_FOR_EACH( ptr, &change_list )
177 struct change *change = LIST_ENTRY( ptr, struct change, entry );
178 long count = interlocked_xchg( &change->notified, 0 );
181 change->signaled += count;
182 if (change->signaled == count) /* was it 0? */
183 wake_up( &change->obj, 0 );
188 /* create a change notification */
189 DECL_HANDLER(create_change_notification)
191 struct change *change;
195 if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
196 fd = get_obj_fd( (struct object *)file );
197 release_object( file );
200 if ((change = create_change_notification( fd, req->subtree, req->filter )))
202 reply->handle = alloc_handle( current->process, change,
203 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
204 release_object( change );
206 release_object( fd );
209 /* move to the next change notification */
210 DECL_HANDLER(next_change_notification)
212 struct change *change;
214 if ((change = (struct change *)get_handle_obj( current->process, req->handle,
217 if (change->signaled > 0) change->signaled--;
218 release_object( change );