server: Add get_sd and set_sd object operations to allow the security descriptor...
[wine] / server / async.c
1 /*
2  * Server-side async I/O support
3  *
4  * Copyright (C) 2007 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30
31 #include "object.h"
32 #include "file.h"
33 #include "request.h"
34
35 struct async
36 {
37     struct object        obj;             /* object header */
38     struct thread       *thread;          /* owning thread */
39     struct list          queue_entry;     /* entry in async queue list */
40     struct async_queue  *queue;           /* queue containing this async */
41     unsigned int         status;          /* current status */
42     struct timeout_user *timeout;
43     unsigned int         timeout_status;  /* status to report upon timeout */
44     struct event        *event;
45     async_data_t         data;            /* data for async I/O call */
46 };
47
48 static void async_dump( struct object *obj, int verbose );
49 static void async_destroy( struct object *obj );
50
51 static const struct object_ops async_ops =
52 {
53     sizeof(struct async),      /* size */
54     async_dump,                /* dump */
55     no_add_queue,              /* add_queue */
56     NULL,                      /* remove_queue */
57     NULL,                      /* signaled */
58     NULL,                      /* satisfied */
59     no_signal,                 /* signal */
60     no_get_fd,                 /* get_fd */
61     no_map_access,             /* map_access */
62     default_get_sd,            /* get_sd */
63     default_set_sd,            /* set_sd */
64     no_lookup_name,            /* lookup_name */
65     no_open_file,              /* open_file */
66     no_close_handle,           /* close_handle */
67     async_destroy              /* destroy */
68 };
69
70
71 struct async_queue
72 {
73     struct object        obj;             /* object header */
74     struct fd           *fd;              /* file descriptor owning this queue */
75     struct list          queue;           /* queue of async objects */
76 };
77
78 static void async_queue_dump( struct object *obj, int verbose );
79
80 static const struct object_ops async_queue_ops =
81 {
82     sizeof(struct async_queue),      /* size */
83     async_queue_dump,                /* dump */
84     no_add_queue,                    /* add_queue */
85     NULL,                            /* remove_queue */
86     NULL,                            /* signaled */
87     NULL,                            /* satisfied */
88     no_signal,                       /* signal */
89     no_get_fd,                       /* get_fd */
90     no_map_access,                   /* map_access */
91     default_get_sd,                  /* get_sd */
92     default_set_sd,                  /* set_sd */
93     no_lookup_name,                  /* lookup_name */
94     no_open_file,                    /* open_file */
95     no_close_handle,                 /* close_handle */
96     no_destroy                       /* destroy */
97 };
98
99
100 static inline void async_reselect( struct async *async )
101 {
102     if (async->queue->fd) fd_reselect_async( async->queue->fd, async->queue );
103 }
104
105 static void async_dump( struct object *obj, int verbose )
106 {
107     struct async *async = (struct async *)obj;
108     assert( obj->ops == &async_ops );
109     fprintf( stderr, "Async thread=%p\n", async->thread );
110 }
111
112 static void async_destroy( struct object *obj )
113 {
114     struct async *async = (struct async *)obj;
115     assert( obj->ops == &async_ops );
116
117     list_remove( &async->queue_entry );
118     async_reselect( async );
119
120     if (async->timeout) remove_timeout_user( async->timeout );
121     if (async->event) release_object( async->event );
122     release_object( async->queue );
123     release_object( async->thread );
124 }
125
126 static void async_queue_dump( struct object *obj, int verbose )
127 {
128     struct async_queue *async_queue = (struct async_queue *)obj;
129     assert( obj->ops == &async_queue_ops );
130     fprintf( stderr, "Async queue fd=%p\n", async_queue->fd );
131 }
132
133 /* notifies client thread of new status of its async request */
134 void async_terminate( struct async *async, unsigned int status )
135 {
136     apc_call_t data;
137
138     assert( status != STATUS_PENDING );
139
140     if (async->status != STATUS_PENDING)
141     {
142         /* already terminated, just update status */
143         async->status = status;
144         return;
145     }
146
147     memset( &data, 0, sizeof(data) );
148     data.type            = APC_ASYNC_IO;
149     data.async_io.func   = async->data.callback;
150     data.async_io.user   = async->data.arg;
151     data.async_io.sb     = async->data.iosb;
152     data.async_io.status = status;
153     thread_queue_apc( async->thread, &async->obj, &data );
154     async->status = status;
155     async_reselect( async );
156     release_object( async );  /* so that it gets destroyed when the async is done */
157 }
158
159 /* callback for timeout on an async request */
160 static void async_timeout( void *private )
161 {
162     struct async *async = private;
163
164     async->timeout = NULL;
165     async_terminate( async, async->timeout_status );
166 }
167
168 /* create a new async queue for a given fd */
169 struct async_queue *create_async_queue( struct fd *fd )
170 {
171     struct async_queue *queue = alloc_object( &async_queue_ops );
172
173     if (queue)
174     {
175         queue->fd = fd;
176         list_init( &queue->queue );
177     }
178     return queue;
179 }
180
181 /* free an async queue, cancelling all async operations */
182 void free_async_queue( struct async_queue *queue )
183 {
184     if (!queue) return;
185     queue->fd = NULL;
186     async_wake_up( queue, STATUS_HANDLES_CLOSED );
187     release_object( queue );
188 }
189
190 /* create an async on a given queue of a fd */
191 struct async *create_async( struct thread *thread, struct async_queue *queue, const async_data_t *data )
192 {
193     struct event *event = NULL;
194     struct async *async;
195
196     if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
197         return NULL;
198
199     if (!(async = alloc_object( &async_ops )))
200     {
201         if (event) release_object( event );
202         return NULL;
203     }
204
205     async->thread  = (struct thread *)grab_object( thread );
206     async->event   = event;
207     async->status  = STATUS_PENDING;
208     async->data    = *data;
209     async->timeout = NULL;
210     async->queue   = (struct async_queue *)grab_object( queue );
211
212     list_add_tail( &queue->queue, &async->queue_entry );
213     grab_object( async );
214
215     if (queue->fd) set_fd_signaled( queue->fd, 0 );
216     if (event) reset_event( event );
217     return async;
218 }
219
220 /* set the timeout of an async operation */
221 void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
222 {
223     if (async->timeout) remove_timeout_user( async->timeout );
224     if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
225     else async->timeout = NULL;
226     async->timeout_status = status;
227 }
228
229 /* store the result of the client-side async callback */
230 void async_set_result( struct object *obj, unsigned int status )
231 {
232     struct async *async = (struct async *)obj;
233
234     if (obj->ops != &async_ops) return;  /* in case the client messed up the APC results */
235
236     assert( async->status != STATUS_PENDING );  /* it must have been woken up if we get a result */
237
238     if (status == STATUS_PENDING)  /* restart it */
239     {
240         status = async->status;
241         async->status = STATUS_PENDING;
242         grab_object( async );
243
244         if (status != STATUS_ALERTED)  /* it was terminated in the meantime */
245             async_terminate( async, status );
246         else
247             async_reselect( async );
248     }
249     else
250     {
251         if (async->timeout) remove_timeout_user( async->timeout );
252         async->timeout = NULL;
253         async->status = status;
254         if (async->data.apc)
255         {
256             apc_call_t data;
257             data.type         = APC_USER;
258             data.user.func    = async->data.apc;
259             data.user.args[0] = (unsigned long)async->data.arg;
260             data.user.args[1] = (unsigned long)async->data.iosb;
261             data.user.args[2] = 0;
262             thread_queue_apc( async->thread, NULL, &data );
263         }
264         if (async->event) set_event( async->event );
265         else if (async->queue->fd) set_fd_signaled( async->queue->fd, 1 );
266     }
267 }
268
269 /* check if an async operation is waiting to be alerted */
270 int async_waiting( struct async_queue *queue )
271 {
272     struct list *ptr;
273     struct async *async;
274
275     if (!queue) return 0;
276     if (!(ptr = list_head( &queue->queue ))) return 0;
277     async = LIST_ENTRY( ptr, struct async, queue_entry );
278     return async->status == STATUS_PENDING;
279 }
280
281 /* wake up async operations on the queue */
282 void async_wake_up( struct async_queue *queue, unsigned int status )
283 {
284     struct list *ptr, *next;
285
286     if (!queue) return;
287
288     LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
289     {
290         struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
291         async_terminate( async, status );
292         if (status == STATUS_ALERTED) break;  /* only wake up the first one */
293     }
294 }