r14542: Remove librpc, libndr and libnbt from includes.h
[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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   composite API helper functions
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "libcli/raw/libcliraw.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   block until a composite function has completed, then return the status
34 */
35 _PUBLIC_ NTSTATUS composite_wait(struct composite_context *c)
36 {
37         if (c == NULL) return NT_STATUS_NO_MEMORY;
38
39         c->used_wait = True;
40
41         while (c->state < COMPOSITE_STATE_DONE) {
42                 if (event_loop_once(c->event_ctx) != 0) {
43                         return NT_STATUS_UNSUCCESSFUL;
44                 }
45         }
46
47         return c->status;
48 }
49
50
51 /*
52  * Some composite helpers that are handy if you write larger composite
53  * functions.
54  */
55 _PUBLIC_ BOOL composite_is_ok(struct composite_context *ctx)
56 {
57         if (NT_STATUS_IS_OK(ctx->status)) {
58                 return True;
59         }
60         ctx->state = COMPOSITE_STATE_ERROR;
61         if (ctx->async.fn != NULL) {
62                 ctx->async.fn(ctx);
63         }
64         return False;
65 }
66
67 /* 
68    callback from composite_done() and composite_error()
69
70    this is used to allow for a composite function to complete without
71    going through any state transitions. When that happens the caller
72    has had no opportunity to fill in the async callback fields
73    (ctx->async.fn and ctx->async.private) which means the usual way of
74    dealing with composite functions doesn't work. To cope with this,
75    we trigger a timer event that will happen then the event loop is
76    re-entered. This gives the caller a chance to setup the callback,
77    and allows the caller to ignore the fact that the composite
78    function completed early
79 */
80 static void composite_trigger(struct event_context *ev, struct timed_event *te,
81                               struct timeval t, void *ptr)
82 {
83         struct composite_context *c = talloc_get_type(ptr, struct composite_context);
84         if (c->async.fn) {
85                 c->async.fn(c);
86         }
87 }
88
89
90 _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
91 {
92         if (!ctx->used_wait && !ctx->async.fn) {
93                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
94         }
95         ctx->status = status;
96         SMB_ASSERT(!composite_is_ok(ctx));
97 }
98
99 _PUBLIC_ BOOL composite_nomem(const void *p, struct composite_context *ctx)
100 {
101         if (p != NULL) {
102                 return False;
103         }
104         composite_error(ctx, NT_STATUS_NO_MEMORY);
105         return True;
106 }
107
108 _PUBLIC_ void composite_done(struct composite_context *ctx)
109 {
110         if (!ctx->used_wait && !ctx->async.fn) {
111                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
112         }
113         ctx->state = COMPOSITE_STATE_DONE;
114         if (ctx->async.fn != NULL) {
115                 ctx->async.fn(ctx);
116         }
117 }
118
119 _PUBLIC_ void composite_continue(struct composite_context *ctx,
120                                  struct composite_context *new_ctx,
121                                  void (*continuation)(struct composite_context *),
122                                  void *private_data)
123 {
124         if (composite_nomem(new_ctx, ctx)) return;
125         new_ctx->async.fn = continuation;
126         new_ctx->async.private_data = private_data;
127 }
128
129 _PUBLIC_ void composite_continue_rpc(struct composite_context *ctx,
130                                      struct rpc_request *new_req,
131                                      void (*continuation)(struct rpc_request *),
132                                      void *private_data)
133 {
134         if (composite_nomem(new_req, ctx)) return;
135         new_req->async.callback = continuation;
136         new_req->async.private = private_data;
137 }
138
139 _PUBLIC_ void composite_continue_irpc(struct composite_context *ctx,
140                                       struct irpc_request *new_req,
141                                       void (*continuation)(struct irpc_request *),
142                                       void *private_data)
143 {
144         if (composite_nomem(new_req, ctx)) return;
145         new_req->async.fn = continuation;
146         new_req->async.private = private_data;
147 }
148
149 _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
150                                      struct smbcli_request *new_req,
151                                      void (*continuation)(struct smbcli_request *),
152                                      void *private_data)
153 {
154         if (composite_nomem(new_req, ctx)) return;
155         new_req->async.fn = continuation;
156         new_req->async.private = private_data;
157 }
158
159 _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
160                                      struct nbt_name_request *new_req,
161                                      void (*continuation)(struct nbt_name_request *),
162                                      void *private_data)
163 {
164         if (composite_nomem(new_req, ctx)) return;
165         new_req->async.fn = continuation;
166         new_req->async.private = private_data;
167 }