Beginning of infrastructure to support WGL extensions.
[wine/multimedia.git] / server / change.c
blobcc2000b69e4212789e4a4a5e715a9bec8082d414
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>
29 #include <sys/stat.h>
31 #include "windef.h"
33 #include "file.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
38 struct change
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 )
69 #ifdef F_NOTIFY
70 unsigned int val;
71 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
72 return;
74 val = DN_MULTISHOT;
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 )
80 val |= DN_ATTRIB;
81 if( filter & FILE_NOTIFY_CHANGE_SIZE )
82 val |= DN_MODIFY;
83 if( filter & FILE_NOTIFY_CHANGE_LAST_WRITE )
84 val |= DN_MODIFY;
85 if( filter & FILE_NOTIFY_CHANGE_LAST_ACCESS )
86 val |= DN_ACCESS;
87 if( filter & FILE_NOTIFY_CHANGE_CREATION )
88 val |= DN_CREATE;
89 if( filter & FILE_NOTIFY_CHANGE_SECURITY )
90 val |= DN_ATTRIB;
91 fcntl( fd, F_NOTIFY, val );
92 #endif
95 /* insert change in the global list */
96 static inline void insert_change( struct change *change )
98 sigset_t sigset;
100 sigemptyset( &sigset );
101 sigaddset( &sigset, SIGIO );
102 sigprocmask( SIG_BLOCK, &sigset, NULL );
103 list_add_head( &change_list, &change->entry );
104 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
107 /* remove change from the global list */
108 static inline void remove_change( struct change *change )
110 sigset_t sigset;
112 sigemptyset( &sigset );
113 sigaddset( &sigset, SIGIO );
114 sigprocmask( SIG_BLOCK, &sigset, NULL );
115 list_remove( &change->entry );
116 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
119 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
121 struct change *change;
122 struct stat st;
123 int unix_fd = get_unix_fd( fd );
125 if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
127 set_error( STATUS_NOT_A_DIRECTORY );
128 return NULL;
131 if ((change = alloc_object( &change_ops )))
133 change->fd = (struct fd *)grab_object( fd );
134 change->subtree = subtree;
135 change->filter = filter;
136 change->notified = 0;
137 change->signaled = 0;
138 insert_change( change );
139 adjust_changes( unix_fd, filter );
141 return change;
144 static void change_dump( struct object *obj, int verbose )
146 struct change *change = (struct change *)obj;
147 assert( obj->ops == &change_ops );
148 fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
149 change->fd, change->subtree, change->filter );
152 static int change_signaled( struct object *obj, struct thread *thread )
154 struct change *change = (struct change *)obj;
156 return change->signaled != 0;
159 static void change_destroy( struct object *obj )
161 struct change *change = (struct change *)obj;
163 release_object( change->fd );
164 remove_change( change );
167 /* enter here directly from SIGIO signal handler */
168 void do_change_notify( int unix_fd )
170 struct list *ptr;
172 /* FIXME: this is O(n) ... probably can be improved */
173 LIST_FOR_EACH( ptr, &change_list )
175 struct change *change = LIST_ENTRY( ptr, struct change, entry );
176 if (get_unix_fd( change->fd ) != unix_fd) continue;
177 interlocked_xchg_add( &change->notified, 1 );
178 break;
182 /* SIGIO callback, called synchronously with the poll loop */
183 void sigio_callback(void)
185 struct list *ptr;
187 LIST_FOR_EACH( ptr, &change_list )
189 struct change *change = LIST_ENTRY( ptr, struct change, entry );
190 long count = interlocked_xchg( &change->notified, 0 );
191 if (count)
193 change->signaled += count;
194 if (change->signaled == count) /* was it 0? */
195 wake_up( &change->obj, 0 );
200 /* create a change notification */
201 DECL_HANDLER(create_change_notification)
203 struct change *change;
204 struct file *file;
205 struct fd *fd;
207 if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
208 fd = get_obj_fd( (struct object *)file );
209 release_object( file );
210 if (!fd) return;
212 if ((change = create_change_notification( fd, req->subtree, req->filter )))
214 reply->handle = alloc_handle( current->process, change,
215 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
216 release_object( change );
218 release_object( fd );
221 /* move to the next change notification */
222 DECL_HANDLER(next_change_notification)
224 struct change *change;
226 if ((change = (struct change *)get_handle_obj( current->process, req->handle,
227 0, &change_ops )))
229 if (change->signaled > 0) change->signaled--;
230 release_object( change );