Remove additional current process tracking in backtrace_all as it can
[wine.git] / server / change.c
blob3d227b05d0bb62f13d931cdb8d4de7fc65b78bbf
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 #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
51 struct change
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 long notified; /* SIGIO counter */
59 long signaled; /* the file changed */
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 );
66 static const struct object_ops change_ops =
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_close_handle, /* close_handle */
77 change_destroy /* destroy */
80 static struct list change_list = LIST_INIT(change_list);
82 static void adjust_changes( int fd, unsigned int filter )
84 #if defined(F_SETSIG) && defined(F_NOTIFY)
85 unsigned int val;
86 if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
87 return;
89 val = DN_MULTISHOT;
90 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
91 val |= DN_RENAME | DN_DELETE | DN_CREATE;
92 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
93 val |= DN_RENAME | DN_DELETE | DN_CREATE;
94 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
95 val |= DN_ATTRIB;
96 if (filter & FILE_NOTIFY_CHANGE_SIZE)
97 val |= DN_MODIFY;
98 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
99 val |= DN_MODIFY;
100 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
101 val |= DN_ACCESS;
102 if (filter & FILE_NOTIFY_CHANGE_CREATION)
103 val |= DN_CREATE;
104 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
105 val |= DN_ATTRIB;
106 fcntl( fd, F_NOTIFY, val );
107 #endif
110 /* insert change in the global list */
111 static inline void insert_change( struct change *change )
113 sigset_t sigset;
115 sigemptyset( &sigset );
116 sigaddset( &sigset, SIGIO );
117 sigprocmask( SIG_BLOCK, &sigset, NULL );
118 list_add_head( &change_list, &change->entry );
119 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
122 /* remove change from the global list */
123 static inline void remove_change( struct change *change )
125 sigset_t sigset;
127 sigemptyset( &sigset );
128 sigaddset( &sigset, SIGIO );
129 sigprocmask( SIG_BLOCK, &sigset, NULL );
130 list_remove( &change->entry );
131 sigprocmask( SIG_UNBLOCK, &sigset, NULL );
134 static struct change *create_change_notification( struct fd *fd, int subtree, unsigned int filter )
136 struct change *change;
137 struct stat st;
138 int unix_fd = get_unix_fd( fd );
140 if (fstat( unix_fd, &st ) == -1 || !S_ISDIR(st.st_mode))
142 set_error( STATUS_NOT_A_DIRECTORY );
143 return NULL;
146 if ((change = alloc_object( &change_ops )))
148 change->fd = (struct fd *)grab_object( fd );
149 change->subtree = subtree;
150 change->filter = filter;
151 change->notified = 0;
152 change->signaled = 0;
153 insert_change( change );
154 adjust_changes( unix_fd, filter );
156 return change;
159 static void change_dump( struct object *obj, int verbose )
161 struct change *change = (struct change *)obj;
162 assert( obj->ops == &change_ops );
163 fprintf( stderr, "Change notification fd=%p sub=%d filter=%08x\n",
164 change->fd, change->subtree, change->filter );
167 static int change_signaled( struct object *obj, struct thread *thread )
169 struct change *change = (struct change *)obj;
171 return change->signaled != 0;
174 static void change_destroy( struct object *obj )
176 struct change *change = (struct change *)obj;
178 release_object( change->fd );
179 remove_change( change );
182 /* enter here directly from SIGIO signal handler */
183 void do_change_notify( int unix_fd )
185 struct list *ptr;
187 /* FIXME: this is O(n) ... probably can be improved */
188 LIST_FOR_EACH( ptr, &change_list )
190 struct change *change = LIST_ENTRY( ptr, struct change, entry );
191 if (get_unix_fd( change->fd ) != unix_fd) continue;
192 interlocked_xchg_add( &change->notified, 1 );
193 break;
197 /* SIGIO callback, called synchronously with the poll loop */
198 void sigio_callback(void)
200 struct list *ptr;
202 LIST_FOR_EACH( ptr, &change_list )
204 struct change *change = LIST_ENTRY( ptr, struct change, entry );
205 long count = interlocked_xchg( &change->notified, 0 );
206 if (count)
208 change->signaled += count;
209 if (change->signaled == count) /* was it 0? */
210 wake_up( &change->obj, 0 );
215 /* create a change notification */
216 DECL_HANDLER(create_change_notification)
218 struct change *change;
219 struct file *file;
220 struct fd *fd;
222 if (!(file = get_file_obj( current->process, req->handle, 0 ))) return;
223 fd = get_obj_fd( (struct object *)file );
224 release_object( file );
225 if (!fd) return;
227 if ((change = create_change_notification( fd, req->subtree, req->filter )))
229 reply->handle = alloc_handle( current->process, change,
230 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, 0 );
231 release_object( change );
233 release_object( fd );
236 /* move to the next change notification */
237 DECL_HANDLER(next_change_notification)
239 struct change *change;
241 if ((change = (struct change *)get_handle_obj( current->process, req->handle,
242 0, &change_ops )))
244 if (change->signaled > 0) change->signaled--;
245 release_object( change );