r25554: Convert last instances of BOOL, True and False to the standard types.
[bbaumbach/samba-autobuild/.git] / source4 / libcli / composite / composite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   composite API helper functions
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/composite/composite.h"
28 #include "lib/messaging/irpc.h"
29 #include "librpc/rpc/dcerpc.h"
30 #include "libcli/nbt/libnbt.h"
31
32 /*
33  create a new composite_context structure
34  and initialize it
35 */
36 _PUBLIC_ struct composite_context *composite_create(TALLOC_CTX *mem_ctx,
37                                                     struct event_context *ev)
38 {
39         struct composite_context *c;
40
41         c = talloc_zero(mem_ctx, struct composite_context);
42         if (!c) return NULL;
43         c->state = COMPOSITE_STATE_IN_PROGRESS;
44         c->event_ctx = ev;
45
46         return c;
47 }
48
49 /*
50   block until a composite function has completed, then return the status
51 */
52 _PUBLIC_ NTSTATUS composite_wait(struct composite_context *c)
53 {
54         if (c == NULL) return NT_STATUS_NO_MEMORY;
55
56         c->used_wait = true;
57
58         while (c->state < COMPOSITE_STATE_DONE) {
59                 if (event_loop_once(c->event_ctx) != 0) {
60                         return NT_STATUS_UNSUCCESSFUL;
61                 }
62         }
63
64         return c->status;
65 }
66
67
68 /*
69  * Some composite helpers that are handy if you write larger composite
70  * functions.
71  */
72 _PUBLIC_ bool composite_is_ok(struct composite_context *ctx)
73 {
74         if (NT_STATUS_IS_OK(ctx->status)) {
75                 return true;
76         }
77         ctx->state = COMPOSITE_STATE_ERROR;
78         if (ctx->async.fn != NULL) {
79                 ctx->async.fn(ctx);
80         }
81         return false;
82 }
83
84 /* 
85    callback from composite_done() and composite_error()
86
87    this is used to allow for a composite function to complete without
88    going through any state transitions. When that happens the caller
89    has had no opportunity to fill in the async callback fields
90    (ctx->async.fn and ctx->async.private) which means the usual way of
91    dealing with composite functions doesn't work. To cope with this,
92    we trigger a timer event that will happen then the event loop is
93    re-entered. This gives the caller a chance to setup the callback,
94    and allows the caller to ignore the fact that the composite
95    function completed early
96 */
97 static void composite_trigger(struct event_context *ev, struct timed_event *te,
98                               struct timeval t, void *ptr)
99 {
100         struct composite_context *c = talloc_get_type(ptr, struct composite_context);
101         if (c->async.fn) {
102                 c->async.fn(c);
103         }
104 }
105
106
107 _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
108 {
109         if (!ctx->used_wait && !ctx->async.fn) {
110                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
111         }
112         ctx->status = status;
113         SMB_ASSERT(!composite_is_ok(ctx));
114 }
115
116 _PUBLIC_ bool composite_nomem(const void *p, struct composite_context *ctx)
117 {
118         if (p != NULL) {
119                 return false;
120         }
121         composite_error(ctx, NT_STATUS_NO_MEMORY);
122         return true;
123 }
124
125 _PUBLIC_ void composite_done(struct composite_context *ctx)
126 {
127         if (!ctx->used_wait && !ctx->async.fn) {
128                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
129         }
130         ctx->state = COMPOSITE_STATE_DONE;
131         if (ctx->async.fn != NULL) {
132                 ctx->async.fn(ctx);
133         }
134 }
135
136 _PUBLIC_ void composite_continue(struct composite_context *ctx,
137                                  struct composite_context *new_ctx,
138                                  void (*continuation)(struct composite_context *),
139                                  void *private_data)
140 {
141         if (composite_nomem(new_ctx, ctx)) return;
142         new_ctx->async.fn = continuation;
143         new_ctx->async.private_data = private_data;
144
145         /* if we are setting up a continuation, and the context has
146            already finished, then we should run the callback with an
147            immediate event, otherwise we can be stuck forever */
148         if (new_ctx->state >= COMPOSITE_STATE_DONE && continuation) {
149                 event_add_timed(new_ctx->event_ctx, new_ctx, timeval_zero(), composite_trigger, new_ctx);
150         }
151 }
152
153 _PUBLIC_ void composite_continue_rpc(struct composite_context *ctx,
154                                      struct rpc_request *new_req,
155                                      void (*continuation)(struct rpc_request *),
156                                      void *private_data)
157 {
158         if (composite_nomem(new_req, ctx)) return;
159         new_req->async.callback = continuation;
160         new_req->async.private_data = private_data;
161 }
162
163 _PUBLIC_ void composite_continue_irpc(struct composite_context *ctx,
164                                       struct irpc_request *new_req,
165                                       void (*continuation)(struct irpc_request *),
166                                       void *private_data)
167 {
168         if (composite_nomem(new_req, ctx)) return;
169         new_req->async.fn = continuation;
170         new_req->async.private = private_data;
171 }
172
173 _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
174                                      struct smbcli_request *new_req,
175                                      void (*continuation)(struct smbcli_request *),
176                                      void *private_data)
177 {
178         if (composite_nomem(new_req, ctx)) return;
179         new_req->async.fn = continuation;
180         new_req->async.private = private_data;
181 }
182
183 _PUBLIC_ void composite_continue_smb2(struct composite_context *ctx,
184                                       struct smb2_request *new_req,
185                                       void (*continuation)(struct smb2_request *),
186                                       void *private_data)
187 {
188         if (composite_nomem(new_req, ctx)) return;
189         new_req->async.fn = continuation;
190         new_req->async.private = private_data;
191 }
192
193 _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
194                                      struct nbt_name_request *new_req,
195                                      void (*continuation)(struct nbt_name_request *),
196                                      void *private_data)
197 {
198         if (composite_nomem(new_req, ctx)) return;
199         new_req->async.fn = continuation;
200         new_req->async.private = private_data;
201 }