Release 20031118.
[wine/multimedia.git] / server / change.c
blob2f1c7ca8a0d8b8c50504744281cb0b3bf7140cb1
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
30 #include "windef.h"
32 #include "file.h"
33 #include "handle.h"
34 #include "thread.h"
35 #include "request.h"
37 struct change
39 struct object obj; /* object header */
40 struct fd *fd; /* file descriptor to the directory */
41 struct list entry; /* entry in global change notifications list */
42 int subtree; /* watch all the subtree */
43 unsigned int filter; /* notification filter */
44 long notified; /* SIGIO counter */
45 long signaled; /* the file changed */
48 static void change_dump( struct object *obj, int verbose );
49 static int change_signaled( struct object *obj, struct thread *thread );
50 static void change_destroy( struct object *obj );
52 static const struct object_ops change_ops =
54 sizeof(struct change), /* size */
55 change_dump, /* dump */
56 add_queue, /* add_queue */
57 remove_queue, /* remove_queue */
58 change_signaled, /* signaled */
59 no_satisfied, /* satisfied */
60 no_get_fd, /* get_fd */
61 change_destroy /* destroy */
64 static struct list change_list = LIST_INIT(change_list);
66 static void adjust_changes( int fd, unsigned int filter )
68 #ifdef F_NOTIFY
69 unsigned int val;
70 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
71 return;
73 val = DN_MULTISHOT;
74 if( filter & FILE_NOTIFY_CHANGE_FILE_NAME )
75 val |= DN_RENAME | DN_DELETE | DN_CREATE;
76 if( filter & FILE_NOTIFY_CHANGE_DIR_NAME )
77 val |= DN_RENAME | DN_DELETE | DN_CREATE;
78 if( filter & FILE_NOTIFY_CHANGE_ATTRIBUTES )
79 val |= DN_ATTRIB;
80 if( filter & FILE_NOTIFY_CHANGE_SIZE )
81 val |= DN_MODIFY;
82 if( filter & FILE_NOTIFY_CHANGE_LAST_WRITE )
83 val |= DN_MODIFY;
84 if( filter & FILE_NOTIFY_CHANGE_SECURITY )
85 val |= DN_ATTRIB;
86 fcntl( fd, F_NOTIFY, val );
87 #endif
90 /* insert change in the global list */
91 static inline void insert_change( struct change *change )
93 sigset_t sigset;
95 sigemptyset( &sigset );
96 sigaddset( &sigset, SIGIO );
97 sigprocmask( SIG_BLOCK, &sigset, NULL );
98 list_add_head( &change_list, &change->entry );
99 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
102 /* remove change from the global list */
103 static inline void remove_change( struct change *change )
105 sigset_t sigset;
107 sigemptyset( &sigset );
108 sigaddset( &sigset, SIGIO );
109 sigprocmask( SIG_BLOCK, &sigset, NULL );
110 list_remove( &change->entry );
111 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
114 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
116 struct change *change;
118 if ((change = alloc_object( &change_ops )))
120 change->fd = (struct fd *)grab_object( fd );
121 change->subtree = subtree;
122 change->filter = filter;
123 change->notified = 0;
124 change->signaled = 0;
125 insert_change( change );
126 adjust_changes( get_unix_fd(fd), filter );
128 return change;
131 static void change_dump( struct object *obj, int verbose )
133 struct change *change = (struct change *)obj;
134 assert( obj->ops == &change_ops );
135 fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
136 change->fd, change->subtree, change->filter );
139 static int change_signaled( struct object *obj, struct thread *thread )
141 struct change *change = (struct change *)obj;
143 return change->signaled != 0;
146 static void change_destroy( struct object *obj )
148 struct change *change = (struct change *)obj;
150 release_object( change->fd );
151 remove_change( change );
154 /* enter here directly from SIGIO signal handler */
155 void do_change_notify( int unix_fd )
157 struct list *ptr;
159 /* FIXME: this is O(n) ... probably can be improved */
160 LIST_FOR_EACH( ptr, &change_list )
162 struct change *change = LIST_ENTRY( ptr, struct change, entry );
163 if (get_unix_fd( change->fd ) != unix_fd) continue;
164 interlocked_xchg_add( &change->notified, 1 );
165 break;
169 /* SIGIO callback, called synchronously with the poll loop */
170 void sigio_callback(void)
172 struct list *ptr;
174 LIST_FOR_EACH( ptr, &change_list )
176 struct change *change = LIST_ENTRY( ptr, struct change, entry );
177 long count = interlocked_xchg( &change->notified, 0 );
178 if (count)
180 change->signaled += count;
181 if (change->signaled == count) /* was it 0? */
182 wake_up( &change->obj, 0 );
187 /* create a change notification */
188 DECL_HANDLER(create_change_notification)
190 struct change *change;
191 struct file *file;
192 struct fd *fd;
194 if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
195 fd = get_obj_fd( (struct object *)file );
196 release_object( file );
197 if (!fd) return;
199 if ((change = create_change_notification( fd, req->subtree, req->filter )))
201 reply->handle = alloc_handle( current->process, change,
202 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
203 release_object( change );
205 release_object( fd );
208 /* move to the next change notification */
209 DECL_HANDLER(next_change_notification)
211 struct change *change;
213 if ((change = (struct change *)get_handle_obj( current->process, req->handle,
214 0, &change_ops )))
216 if (change->signaled > 0) change->signaled--;
217 release_object( change );