r23792: convert Samba4 to GPLv3
[samba.git] / source4 / ntvfs / posix / pvfs_wait.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - async request wait routines
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/util/dlinklist.h"
25 #include "vfs_posix.h"
26 #include "smbd/service_stream.h"
27 #include "lib/messaging/irpc.h"
28
29 /* the context for a single wait instance */
30 struct pvfs_wait {
31         struct pvfs_wait *next, *prev;
32         struct pvfs_state *pvfs;
33         void (*handler)(void *, enum pvfs_wait_notice);
34         void *private;
35         int msg_type;
36         struct messaging_context *msg_ctx;
37         struct event_context *ev;
38         struct ntvfs_request *req;
39         enum pvfs_wait_notice reason;
40 };
41
42 /*
43   called from the ntvfs layer when we have requested setup of an async
44   call.  this ensures that async calls runs with the right state of
45   previous ntvfs handlers in the chain (such as security context)
46 */
47 NTSTATUS pvfs_async_setup(struct ntvfs_module_context *ntvfs,
48                           struct ntvfs_request *req, void *private)
49 {
50         struct pvfs_wait *pwait = private;
51         pwait->handler(pwait->private, pwait->reason);
52         return NT_STATUS_OK;
53 }
54
55 /*
56   receive a completion message for a wait
57 */
58 static void pvfs_wait_dispatch(struct messaging_context *msg, void *private, uint32_t msg_type, 
59                                struct server_id src, DATA_BLOB *data)
60 {
61         struct pvfs_wait *pwait = private;
62         struct ntvfs_request *req;
63         void *p = NULL;
64
65         /* we need to check that this one is for us. See
66            messaging_send_ptr() for the other side of this.
67          */
68         if (data->length == sizeof(void *)) {
69                 void **pp;
70                 pp = (void **)data->data;
71                 p = *pp;
72         }
73         if (p == NULL || p != pwait->private) {
74                 return;
75         }
76
77         pwait->reason = PVFS_WAIT_EVENT;
78         req = pwait->req;
79
80         /* the extra reference here is to ensure that the req
81            structure is not destroyed when the async request reply is
82            sent, which would cause problems with the other ntvfs
83            modules above us */
84         talloc_reference(msg, req);
85         ntvfs_async_setup(pwait->req, pwait);
86         talloc_unlink(msg, req);
87 }
88
89
90 /*
91   receive a timeout on a message wait
92 */
93 static void pvfs_wait_timeout(struct event_context *ev, 
94                               struct timed_event *te, struct timeval t, void *private)
95 {
96         struct pvfs_wait *pwait = talloc_get_type(private, struct pvfs_wait);
97         struct ntvfs_request *req = pwait->req;
98
99         pwait->reason = PVFS_WAIT_TIMEOUT;
100
101         talloc_increase_ref_count(req);
102         ntvfs_async_setup(pwait->req, pwait);
103         talloc_free(req);
104 }
105
106
107 /*
108   destroy a pending wait
109  */
110 static int pvfs_wait_destructor(struct pvfs_wait *pwait)
111 {
112         if (pwait->msg_type != -1) {
113                 messaging_deregister(pwait->msg_ctx, pwait->msg_type, pwait);
114         }
115         DLIST_REMOVE(pwait->pvfs->wait_list, pwait);
116         return 0;
117 }
118
119 /*
120   setup a request to wait on a message of type msg_type, with a
121   timeout (given as an expiry time)
122
123   the return value is a handle. To stop waiting talloc_free this
124   handle.
125
126   if msg_type == -1 then no message is registered, and it is assumed
127   that the caller handles any messaging setup needed
128 */
129 void *pvfs_wait_message(struct pvfs_state *pvfs, 
130                         struct ntvfs_request *req, 
131                         int msg_type, 
132                         struct timeval end_time,
133                         void (*fn)(void *, enum pvfs_wait_notice),
134                         void *private)
135 {
136         struct pvfs_wait *pwait;
137
138         pwait = talloc(pvfs, struct pvfs_wait);
139         if (pwait == NULL) {
140                 return NULL;
141         }
142
143         pwait->private = private;
144         pwait->handler = fn;
145         pwait->msg_ctx = pvfs->ntvfs->ctx->msg_ctx;
146         pwait->ev = pvfs->ntvfs->ctx->event_ctx;
147         pwait->msg_type = msg_type;
148         pwait->req = talloc_reference(pwait, req);
149         pwait->pvfs = pvfs;
150
151         if (!timeval_is_zero(&end_time)) {
152                 /* setup a timer */
153                 event_add_timed(pwait->ev, pwait, end_time, pvfs_wait_timeout, pwait);
154         }
155
156         /* register with the messaging subsystem for this message
157            type */
158         if (msg_type != -1) {
159                 messaging_register(pwait->msg_ctx,
160                                    pwait,
161                                    msg_type,
162                                    pvfs_wait_dispatch);
163         }
164
165         /* tell the main smb server layer that we will be replying 
166            asynchronously */
167         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
168
169         DLIST_ADD(pvfs->wait_list, pwait);
170
171         /* make sure we cleanup the timer and message handler */
172         talloc_set_destructor(pwait, pvfs_wait_destructor);
173
174         return pwait;
175 }
176
177
178 /*
179   cancel an outstanding async request
180 */
181 NTSTATUS pvfs_cancel(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req)
182 {
183         struct pvfs_state *pvfs = ntvfs->private_data;
184         struct pvfs_wait *pwait;
185
186         for (pwait=pvfs->wait_list;pwait;pwait=pwait->next) {
187                 if (pwait->req == req) {
188                         /* trigger a cancel on the request */
189                         pwait->reason = PVFS_WAIT_CANCEL;
190                         ntvfs_async_setup(pwait->req, pwait);
191                         return NT_STATUS_OK;
192                 }
193         }
194
195         return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
196 }