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
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 "librpc/rpc/dcerpc.h"
31 #include "libcli/nbt/libnbt.h"
34 create a new composite_context structure
37 _PUBLIC_
struct composite_context
*composite_create(TALLOC_CTX
*mem_ctx
,
38 struct event_context
*ev
)
40 struct composite_context
*c
;
42 c
= talloc_zero(mem_ctx
, struct composite_context
);
44 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
45 c
->event_ctx
= talloc_reference(c
, ev
);
55 block until a composite function has completed, then return the status
57 _PUBLIC_ NTSTATUS
composite_wait(struct composite_context
*c
)
59 if (c
== NULL
) return NT_STATUS_NO_MEMORY
;
63 while (c
->state
< COMPOSITE_STATE_DONE
) {
64 if (event_loop_once(c
->event_ctx
) != 0) {
65 return NT_STATUS_UNSUCCESSFUL
;
73 block until a composite function has completed, then return the status.
74 Free the composite context before returning
76 _PUBLIC_ NTSTATUS
composite_wait_free(struct composite_context
*c
)
78 NTSTATUS status
= composite_wait(c
);
84 callback from composite_done() and composite_error()
86 this is used to allow for a composite function to complete without
87 going through any state transitions. When that happens the caller
88 has had no opportunity to fill in the async callback fields
89 (ctx->async.fn and ctx->async.private) which means the usual way of
90 dealing with composite functions doesn't work. To cope with this,
91 we trigger a timer event that will happen then the event loop is
92 re-entered. This gives the caller a chance to setup the callback,
93 and allows the caller to ignore the fact that the composite
94 function completed early
96 static void composite_trigger(struct event_context
*ev
, struct timed_event
*te
,
97 struct timeval t
, void *ptr
)
99 struct composite_context
*c
= talloc_get_type(ptr
, struct composite_context
);
106 _PUBLIC_
void composite_error(struct composite_context
*ctx
, NTSTATUS status
)
108 /* you are allowed to pass NT_STATUS_OK to composite_error(), in which
109 case it is equivalent to composite_done() */
110 if (NT_STATUS_IS_OK(status
)) {
114 if (!ctx
->used_wait
&& !ctx
->async
.fn
) {
115 event_add_timed(ctx
->event_ctx
, ctx
, timeval_zero(), composite_trigger
, ctx
);
117 ctx
->status
= status
;
118 ctx
->state
= COMPOSITE_STATE_ERROR
;
119 if (ctx
->async
.fn
!= NULL
) {
124 _PUBLIC_
bool composite_nomem(const void *p
, struct composite_context
*ctx
)
129 composite_error(ctx
, NT_STATUS_NO_MEMORY
);
133 _PUBLIC_
bool composite_is_ok(struct composite_context
*ctx
)
135 if (NT_STATUS_IS_OK(ctx
->status
)) {
138 composite_error(ctx
, ctx
->status
);
142 _PUBLIC_
void composite_done(struct composite_context
*ctx
)
144 if (!ctx
->used_wait
&& !ctx
->async
.fn
) {
145 event_add_timed(ctx
->event_ctx
, ctx
, timeval_zero(), composite_trigger
, ctx
);
147 ctx
->state
= COMPOSITE_STATE_DONE
;
148 if (ctx
->async
.fn
!= NULL
) {
153 _PUBLIC_
void composite_continue(struct composite_context
*ctx
,
154 struct composite_context
*new_ctx
,
155 void (*continuation
)(struct composite_context
*),
158 if (composite_nomem(new_ctx
, ctx
)) return;
159 new_ctx
->async
.fn
= continuation
;
160 new_ctx
->async
.private_data
= private_data
;
162 /* if we are setting up a continuation, and the context has
163 already finished, then we should run the callback with an
164 immediate event, otherwise we can be stuck forever */
165 if (new_ctx
->state
>= COMPOSITE_STATE_DONE
&& continuation
) {
166 event_add_timed(new_ctx
->event_ctx
, new_ctx
, timeval_zero(), composite_trigger
, new_ctx
);
170 _PUBLIC_
void composite_continue_rpc(struct composite_context
*ctx
,
171 struct rpc_request
*new_req
,
172 void (*continuation
)(struct rpc_request
*),
175 if (composite_nomem(new_req
, ctx
)) return;
176 new_req
->async
.callback
= continuation
;
177 new_req
->async
.private_data
= private_data
;
180 _PUBLIC_
void composite_continue_irpc(struct composite_context
*ctx
,
181 struct irpc_request
*new_req
,
182 void (*continuation
)(struct irpc_request
*),
185 if (composite_nomem(new_req
, ctx
)) return;
186 new_req
->async
.fn
= continuation
;
187 new_req
->async
.private = private_data
;
190 _PUBLIC_
void composite_continue_smb(struct composite_context
*ctx
,
191 struct smbcli_request
*new_req
,
192 void (*continuation
)(struct smbcli_request
*),
195 if (composite_nomem(new_req
, ctx
)) return;
196 new_req
->async
.fn
= continuation
;
197 new_req
->async
.private = private_data
;
200 _PUBLIC_
void composite_continue_smb2(struct composite_context
*ctx
,
201 struct smb2_request
*new_req
,
202 void (*continuation
)(struct smb2_request
*),
205 if (composite_nomem(new_req
, ctx
)) return;
206 new_req
->async
.fn
= continuation
;
207 new_req
->async
.private_data
= private_data
;
210 _PUBLIC_
void composite_continue_nbt(struct composite_context
*ctx
,
211 struct nbt_name_request
*new_req
,
212 void (*continuation
)(struct nbt_name_request
*),
215 if (composite_nomem(new_req
, ctx
)) return;
216 new_req
->async
.fn
= continuation
;
217 new_req
->async
.private_data
= private_data
;