Add name_lookup function in object_ops.
[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 #include <sys/stat.h>
30
31 #include "windef.h"
32
33 #include "file.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37
38 #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
51 struct change
52 {
53     struct object  obj;      /* object header */
54     struct fd     *fd;       /* file descriptor to the directory */
55     struct list    entry;    /* entry in global change notifications list */
56     int            subtree;  /* watch all the subtree */
57     unsigned int   filter;   /* notification filter */
58     int            notified; /* SIGIO counter */
59     long           signaled; /* the file changed */
60 };
61
62 static void change_dump( struct object *obj, int verbose );
63 static int change_signaled( struct object *obj, struct thread *thread );
64 static void change_destroy( struct object *obj );
65
66 static const struct object_ops change_ops =
67 {
68     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 */
74     no_signal,                /* signal */
75     no_get_fd,                /* get_fd */
76     no_lookup_name,           /* lookup_name */
77     no_close_handle,          /* close_handle */
78     change_destroy            /* destroy */
79 };
80
81 static struct list change_list = LIST_INIT(change_list);
82
83 static void adjust_changes( int fd, unsigned int filter )
84 {
85 #if defined(F_SETSIG) && defined(F_NOTIFY)
86     unsigned int val;
87     if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
88         return;
89
90     val = DN_MULTISHOT;
91     if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
92         val |= DN_RENAME | DN_DELETE | DN_CREATE;
93     if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
94         val |= DN_RENAME | DN_DELETE | DN_CREATE;
95     if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
96         val |= DN_ATTRIB;
97     if (filter & FILE_NOTIFY_CHANGE_SIZE)
98         val |= DN_MODIFY;
99     if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
100         val |= DN_MODIFY;
101     if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
102         val |= DN_ACCESS;
103     if (filter & FILE_NOTIFY_CHANGE_CREATION)
104         val |= DN_CREATE;
105     if (filter & FILE_NOTIFY_CHANGE_SECURITY)
106         val |= DN_ATTRIB;
107     fcntl( fd, F_NOTIFY, val );
108 #endif
109 }
110
111 /* insert change in the global list */
112 static inline void insert_change( struct change *change )
113 {
114     sigset_t sigset;
115
116     sigemptyset( &sigset );
117     sigaddset( &sigset, SIGIO );
118     sigprocmask( SIG_BLOCK, &sigset, NULL );
119     list_add_head( &change_list, &change->entry );
120     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
121 }
122
123 /* remove change from the global list */
124 static inline void remove_change( struct change *change )
125 {
126     sigset_t sigset;
127
128     sigemptyset( &sigset );
129     sigaddset( &sigset, SIGIO );
130     sigprocmask( SIG_BLOCK, &sigset, NULL );
131     list_remove( &change->entry );
132     sigprocmask( SIG_UNBLOCK, &sigset, NULL );
133 }
134
135 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
136 {
137     struct change *change;
138     struct stat st;
139     int unix_fd = get_unix_fd( fd );
140
141     if (unix_fd == -1) return NULL;
142
143     if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
144     {
145         set_error( STATUS_NOT_A_DIRECTORY );
146         return NULL;
147     }
148
149     if ((change = alloc_object( &change_ops )))
150     {
151         change->fd       = (struct fd *)grab_object( fd );
152         change->subtree  = subtree;
153         change->filter   = filter;
154         change->notified = 0;
155         change->signaled = 0;
156         insert_change( change );
157         adjust_changes( unix_fd, filter );
158     }
159     return change;
160 }
161
162 static void change_dump( struct object *obj, int verbose )
163 {
164     struct change *change = (struct change *)obj;
165     assert( obj->ops == &change_ops );
166     fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
167              change->fd, change->subtree, change->filter );
168 }
169
170 static int change_signaled( struct object *obj, struct thread *thread )
171 {
172     struct change *change = (struct change *)obj;
173
174     return change->signaled != 0;
175 }
176
177 static void change_destroy( struct object *obj )
178 {
179     struct change *change = (struct change *)obj;
180
181     release_object( change->fd );
182     remove_change( change );
183 }
184
185 /* enter here directly from SIGIO signal handler */
186 void do_change_notify( int unix_fd )
187 {
188     struct list *ptr;
189
190     /* FIXME: this is O(n) ... probably can be improved */
191     LIST_FOR_EACH( ptr, &change_list )
192     {
193         struct change *change = LIST_ENTRY( ptr, struct change, entry );
194         if (get_unix_fd( change->fd ) != unix_fd) continue;
195         interlocked_xchg_add( &change->notified, 1 );
196         break;
197     }
198 }
199
200 /* SIGIO callback, called synchronously with the poll loop */
201 void sigio_callback(void)
202 {
203     struct list *ptr;
204
205     LIST_FOR_EACH( ptr, &change_list )
206     {
207         struct change *change = LIST_ENTRY( ptr, struct change, entry );
208         long count = interlocked_xchg( &change->notified, 0 );
209         if (count)
210         {
211             change->signaled += count;
212             if (change->signaled == count)  /* was it 0? */
213                 wake_up( &change->obj, 0 );
214         }
215     }
216 }
217
218 /* create a change notification */
219 DECL_HANDLER(create_change_notification)
220 {
221     struct change *change;
222     struct file *file;
223     struct fd *fd;
224
225     if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
226     fd = get_obj_fd( (struct object *)file );
227     release_object( file );
228     if (!fd) return;
229
230     if ((change = create_change_notification( fd, req->subtree, req->filter )))
231     {
232         reply->handle = alloc_handle( current->process, change,
233                                       STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
234         release_object( change );
235     }
236     release_object( fd );
237 }
238
239 /* move to the next change notification */
240 DECL_HANDLER(next_change_notification)
241 {
242     struct change *change;
243
244     if ((change = (struct change *)get_handle_obj( current->process, req->handle,
245                                                    0, &change_ops )))
246     {
247         if (change->signaled > 0) change->signaled--;
248         release_object( change );
249     }
250 }