Add support for some MCIWNDF_ styles, indicate that we do not support
[wine] / server / change.c
1 /*
2  * Server-side change notification management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29
30 #include "windef.h"
31
32 #include "file.h"
33 #include "handle.h"
34 #include "thread.h"
35 #include "request.h"
36
37 struct change
38 {
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 */
46 };
47
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 );
51
52 static const struct object_ops change_ops =
53 {
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 */
62 };
63
64 static struct list change_list = LIST_INIT(change_list);
65
66 static void adjust_changes( int fd, unsigned int filter )
67 {
68 #ifdef F_NOTIFY
69     unsigned int val;
70     if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
71         return;
72
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
88 }
89
90 /* insert change in the global list */
91 static inline void insert_change( struct change *change )
92 {
93     sigset_t sigset;
94
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 );
100 }
101
102 /* remove change from the global list */
103 static inline void remove_change( struct change *change )
104 {
105     sigset_t sigset;
106
107     sigemptyset( &sigset );
108     sigaddset( &sigset, SIGIO );
109     sigprocmask( SIG_BLOCK, &sigset, NULL );
110     list_remove( &change->entry );
111     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
112 }
113
114 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
115 {
116     struct change *change;
117
118     if ((change = alloc_object( &change_ops )))
119     {
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 );
127     }
128     return change;
129 }
130
131 static void change_dump( struct object *obj, int verbose )
132 {
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 );
137 }
138
139 static int change_signaled( struct object *obj, struct thread *thread )
140 {
141     struct change *change = (struct change *)obj;
142
143     return change->signaled != 0;
144 }
145
146 static void change_destroy( struct object *obj )
147 {
148     struct change *change = (struct change *)obj;
149
150     release_object( change->fd );
151     remove_change( change );
152 }
153
154 /* enter here directly from SIGIO signal handler */
155 void do_change_notify( int unix_fd )
156 {
157     struct list *ptr;
158
159     /* FIXME: this is O(n) ... probably can be improved */
160     LIST_FOR_EACH( ptr, &change_list )
161     {
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;
166     }
167 }
168
169 /* SIGIO callback, called synchronously with the poll loop */
170 void sigio_callback(void)
171 {
172     struct list *ptr;
173
174     LIST_FOR_EACH( ptr, &change_list )
175     {
176         struct change *change = LIST_ENTRY( ptr, struct change, entry );
177         long count = interlocked_xchg( &change->notified, 0 );
178         if (count)
179         {
180             change->signaled += count;
181             if (change->signaled == count)  /* was it 0? */
182                 wake_up( &change->obj, 0 );
183         }
184     }
185 }
186
187 /* create a change notification */
188 DECL_HANDLER(create_change_notification)
189 {
190     struct change *change;
191     struct file *file;
192     struct fd *fd;
193
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;
198
199     if ((change = create_change_notification( fd, req->subtree, req->filter )))
200     {
201         reply->handle = alloc_handle( current->process, change,
202                                       STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
203         release_object( change );
204     }
205     release_object( fd );
206 }
207
208 /* move to the next change notification */
209 DECL_HANDLER(next_change_notification)
210 {
211     struct change *change;
212
213     if ((change = (struct change *)get_handle_obj( current->process, req->handle,
214                                                    0, &change_ops )))
215     {
216         if (change->signaled > 0) change->signaled--;
217         release_object( change );
218     }
219 }