replace/waf: add rt to deps at this place
[Samba/gebeck_regimport.git] / source4 / libcli / composite / composite.c
blob96d663b693e314fa6688118b24ee1e620ba7b619
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2005
5 Copyright (C) Andrew Tridgell 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 composite API helper functions
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/smb2/smb2.h"
28 #include "libcli/composite/composite.h"
29 #include "lib/messaging/irpc.h"
30 #include "../libcli/nbt/libnbt.h"
33 create a new composite_context structure
34 and initialize it
36 _PUBLIC_ struct composite_context *composite_create(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev)
39 struct composite_context *c;
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;
46 return c;
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;
56 c->used_wait = true;
58 while (c->state < COMPOSITE_STATE_DONE) {
59 if (event_loop_once(c->event_ctx) != 0) {
60 return NT_STATUS_UNSUCCESSFUL;
64 return c->status;
68 block until a composite function has completed, then return the status.
69 Free the composite context before returning
71 _PUBLIC_ NTSTATUS composite_wait_free(struct composite_context *c)
73 NTSTATUS status = composite_wait(c);
74 talloc_free(c);
75 return status;
78 /*
79 callback from composite_done() and composite_error()
81 this is used to allow for a composite function to complete without
82 going through any state transitions. When that happens the caller
83 has had no opportunity to fill in the async callback fields
84 (ctx->async.fn and ctx->async.private_data) which means the usual way of
85 dealing with composite functions doesn't work. To cope with this,
86 we trigger a timer event that will happen then the event loop is
87 re-entered. This gives the caller a chance to setup the callback,
88 and allows the caller to ignore the fact that the composite
89 function completed early
91 static void composite_trigger(struct tevent_context *ev, struct tevent_timer *te,
92 struct timeval t, void *ptr)
94 struct composite_context *c = talloc_get_type(ptr, struct composite_context);
95 if (c->async.fn) {
96 c->async.fn(c);
101 _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
103 /* you are allowed to pass NT_STATUS_OK to composite_error(), in which
104 case it is equivalent to composite_done() */
105 if (NT_STATUS_IS_OK(status)) {
106 composite_done(ctx);
107 return;
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 ctx->state = COMPOSITE_STATE_ERROR;
114 if (ctx->async.fn != NULL) {
115 ctx->async.fn(ctx);
119 _PUBLIC_ bool composite_nomem(const void *p, struct composite_context *ctx)
121 if (p != NULL) {
122 return false;
124 composite_error(ctx, NT_STATUS_NO_MEMORY);
125 return true;
128 _PUBLIC_ bool composite_is_ok(struct composite_context *ctx)
130 if (NT_STATUS_IS_OK(ctx->status)) {
131 return true;
133 composite_error(ctx, ctx->status);
134 return false;
137 _PUBLIC_ void composite_done(struct composite_context *ctx)
139 if (!ctx->used_wait && !ctx->async.fn) {
140 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
142 ctx->state = COMPOSITE_STATE_DONE;
143 if (ctx->async.fn != NULL) {
144 ctx->async.fn(ctx);
148 _PUBLIC_ void composite_continue(struct composite_context *ctx,
149 struct composite_context *new_ctx,
150 void (*continuation)(struct composite_context *),
151 void *private_data)
153 if (composite_nomem(new_ctx, ctx)) return;
154 new_ctx->async.fn = continuation;
155 new_ctx->async.private_data = private_data;
157 /* if we are setting up a continuation, and the context has
158 already finished, then we should run the callback with an
159 immediate event, otherwise we can be stuck forever */
160 if (new_ctx->state >= COMPOSITE_STATE_DONE && continuation) {
161 event_add_timed(new_ctx->event_ctx, new_ctx, timeval_zero(), composite_trigger, new_ctx);
165 _PUBLIC_ void composite_continue_irpc(struct composite_context *ctx,
166 struct irpc_request *new_req,
167 void (*continuation)(struct irpc_request *),
168 void *private_data)
170 if (composite_nomem(new_req, ctx)) return;
171 new_req->async.fn = continuation;
172 new_req->async.private_data = private_data;
175 _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
176 struct smbcli_request *new_req,
177 void (*continuation)(struct smbcli_request *),
178 void *private_data)
180 if (composite_nomem(new_req, ctx)) return;
181 new_req->async.fn = continuation;
182 new_req->async.private_data = private_data;
185 _PUBLIC_ void composite_continue_smb2(struct composite_context *ctx,
186 struct smb2_request *new_req,
187 void (*continuation)(struct smb2_request *),
188 void *private_data)
190 if (composite_nomem(new_req, ctx)) return;
191 new_req->async.fn = continuation;
192 new_req->async.private_data = private_data;
195 _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
196 struct nbt_name_request *new_req,
197 void (*continuation)(struct nbt_name_request *),
198 void *private_data)
200 if (composite_nomem(new_req, ctx)) return;
201 new_req->async.fn = continuation;
202 new_req->async.private_data = private_data;