2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
20 composite API helper functions
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"
33 create a new composite_context structure
36 _PUBLIC_ struct composite_context *composite_create(TALLOC_CTX *mem_ctx,
37 struct event_context *ev)
39 struct composite_context *c;
41 c = talloc_zero(mem_ctx, struct composite_context);
43 c->state = COMPOSITE_STATE_IN_PROGRESS;
50 block until a composite function has completed, then return the status
52 _PUBLIC_ NTSTATUS composite_wait(struct composite_context *c)
54 if (c == NULL) return NT_STATUS_NO_MEMORY;
58 while (c->state < COMPOSITE_STATE_DONE) {
59 if (event_loop_once(c->event_ctx) != 0) {
60 return NT_STATUS_UNSUCCESSFUL;
69 * Some composite helpers that are handy if you write larger composite
72 _PUBLIC_ bool composite_is_ok(struct composite_context *ctx)
74 if (NT_STATUS_IS_OK(ctx->status)) {
77 ctx->state = COMPOSITE_STATE_ERROR;
78 if (ctx->async.fn != NULL) {
85 callback from composite_done() and composite_error()
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
97 static void composite_trigger(struct event_context *ev, struct timed_event *te,
98 struct timeval t, void *ptr)
100 struct composite_context *c = talloc_get_type(ptr, struct composite_context);
107 _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
109 if (!ctx->used_wait && !ctx->async.fn) {
110 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
112 ctx->status = status;
113 SMB_ASSERT(!composite_is_ok(ctx));
116 _PUBLIC_ bool composite_nomem(const void *p, struct composite_context *ctx)
121 composite_error(ctx, NT_STATUS_NO_MEMORY);
125 _PUBLIC_ void composite_done(struct composite_context *ctx)
127 if (!ctx->used_wait && !ctx->async.fn) {
128 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
130 ctx->state = COMPOSITE_STATE_DONE;
131 if (ctx->async.fn != NULL) {
136 _PUBLIC_ void composite_continue(struct composite_context *ctx,
137 struct composite_context *new_ctx,
138 void (*continuation)(struct composite_context *),
141 if (composite_nomem(new_ctx, ctx)) return;
142 new_ctx->async.fn = continuation;
143 new_ctx->async.private_data = private_data;
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);
153 _PUBLIC_ void composite_continue_rpc(struct composite_context *ctx,
154 struct rpc_request *new_req,
155 void (*continuation)(struct rpc_request *),
158 if (composite_nomem(new_req, ctx)) return;
159 new_req->async.callback = continuation;
160 new_req->async.private_data = private_data;
163 _PUBLIC_ void composite_continue_irpc(struct composite_context *ctx,
164 struct irpc_request *new_req,
165 void (*continuation)(struct irpc_request *),
168 if (composite_nomem(new_req, ctx)) return;
169 new_req->async.fn = continuation;
170 new_req->async.private = private_data;
173 _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
174 struct smbcli_request *new_req,
175 void (*continuation)(struct smbcli_request *),
178 if (composite_nomem(new_req, ctx)) return;
179 new_req->async.fn = continuation;
180 new_req->async.private = private_data;
183 _PUBLIC_ void composite_continue_smb2(struct composite_context *ctx,
184 struct smb2_request *new_req,
185 void (*continuation)(struct smb2_request *),
188 if (composite_nomem(new_req, ctx)) return;
189 new_req->async.fn = continuation;
190 new_req->async.private = private_data;
193 _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
194 struct nbt_name_request *new_req,
195 void (*continuation)(struct nbt_name_request *),
198 if (composite_nomem(new_req, ctx)) return;
199 new_req->async.fn = continuation;
200 new_req->async.private = private_data;