r12116: got rid of composite_trigger_done() and composite_trigger_error(), and
authorAndrew Tridgell <tridge@samba.org>
Thu, 8 Dec 2005 01:13:45 +0000 (01:13 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:47:11 +0000 (13:47 -0500)
instead make the normal composite_done() and composite_error()
functions automatically trigger a delayed callback if the caller has
had no opportunity to setup a async callback

this removes one of the common mistakes in writing a composite function
(This used to be commit f9413ce792ded682e05134b66d433eeec293e6f1)

14 files changed:
source4/lib/socket/connect.c
source4/lib/socket/connect_multi.c
source4/libcli/composite/composite.c
source4/libcli/composite/composite.h
source4/libcli/ldap/ldap_client.c
source4/libcli/resolve/resolve.c
source4/libcli/smb2/connect.c
source4/libcli/smb2/session.c
source4/librpc/rpc/dcerpc.c
source4/librpc/rpc/dcerpc_auth.c
source4/torture/rpc/xplogin.c
source4/winbind/wb_async_helpers.c
source4/winbind/wb_sid2domain.c
source4/wrepl_server/wrepl_out_helpers.c

index 1ab4892128d5e963d8b4f660300ae672188bba96..116a8c67d866026b8761b5935d2e9e438c5e6973 100644 (file)
@@ -138,7 +138,7 @@ struct composite_context *socket_connect_send(struct socket_context *sock,
        return result;
 
 failed:
-       composite_trigger_error(result);
+       composite_error(result, result->status);
        return result;
 }
 
index 3948c1e2a123563718750f77abca36f29199e8fc..258560cec5705a7061f5dc9a8a0ebabd5b85d1a7 100644 (file)
@@ -119,7 +119,7 @@ struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
        return result;
 
  failed:
-       composite_trigger_error(result);
+       composite_error(result, result->status);
        return result;
 }
 
index 3842ddfe9fb094b8779866ac4af57ffed9ab9bbd..c9cc20e9235382c62fea87def84ff48b0cd40d71 100644 (file)
@@ -35,6 +35,8 @@ NTSTATUS composite_wait(struct composite_context *c)
 {
        if (c == NULL) return NT_STATUS_NO_MEMORY;
 
+       c->used_wait = True;
+
        while (c->state < COMPOSITE_STATE_DONE) {
                if (event_loop_once(c->event_ctx) != 0) {
                        return NT_STATUS_UNSUCCESSFUL;
@@ -45,44 +47,10 @@ NTSTATUS composite_wait(struct composite_context *c)
 }
 
 
-/* 
-   callback from composite_trigger_done() and composite_trigger_error()
-*/
-static void composite_trigger(struct event_context *ev, struct timed_event *te,
-                             struct timeval t, void *ptr)
-{
-       struct composite_context *c = talloc_get_type(ptr, struct composite_context);
-       if (c->async.fn) {
-               c->async.fn(c);
-       }
-}
-
-
-/*
-  trigger an immediate 'done' event on a composite context
-  this is used when the composite code works out that the call
-  can be completed without waiting for any external event
-*/
-void composite_trigger_done(struct composite_context *c)
-{
-       c->state = COMPOSITE_STATE_DONE;
-       /* a zero timeout means immediate */
-       event_add_timed(c->event_ctx, c, timeval_zero(), composite_trigger, c);
-}
-
-void composite_trigger_error(struct composite_context *c)
-{
-       c->state = COMPOSITE_STATE_ERROR;
-       /* a zero timeout means immediate */
-       event_add_timed(c->event_ctx, c, timeval_zero(), composite_trigger, c);
-}
-
-
 /*
  * Some composite helpers that are handy if you write larger composite
  * functions.
  */
-
 BOOL composite_is_ok(struct composite_context *ctx)
 {
        if (NT_STATUS_IS_OK(ctx->status)) {
@@ -95,8 +63,34 @@ BOOL composite_is_ok(struct composite_context *ctx)
        return False;
 }
 
+/* 
+   callback from composite_done() and composite_error()
+
+   this is used to allow for a composite function to complete without
+   going through any state transitions. When that happens the caller
+   has had no opportunity to fill in the async callback fields
+   (ctx->async.fn and ctx->async.private) which means the usual way of
+   dealing with composite functions doesn't work. To cope with this,
+   we trigger a timer event that will happen then the event loop is
+   re-entered. This gives the caller a chance to setup the callback,
+   and allows the caller to ignore the fact that the composite
+   function completed early
+*/
+static void composite_trigger(struct event_context *ev, struct timed_event *te,
+                             struct timeval t, void *ptr)
+{
+       struct composite_context *c = talloc_get_type(ptr, struct composite_context);
+       if (c->async.fn) {
+               c->async.fn(c);
+       }
+}
+
+
 void composite_error(struct composite_context *ctx, NTSTATUS status)
 {
+       if (!ctx->used_wait && !ctx->async.fn) {
+               event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
+       }
        ctx->status = status;
        SMB_ASSERT(!composite_is_ok(ctx));
 }
@@ -112,6 +106,9 @@ BOOL composite_nomem(const void *p, struct composite_context *ctx)
 
 void composite_done(struct composite_context *ctx)
 {
+       if (!ctx->used_wait && !ctx->async.fn) {
+               event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
+       }
        ctx->state = COMPOSITE_STATE_DONE;
        if (ctx->async.fn != NULL) {
                ctx->async.fn(ctx);
index 1e6e9f5dc85e39ef8442ae4fd47b0a55f3abf941..26490f1c4a2c5b8143bb49268cedafd72e634048 100644 (file)
@@ -57,4 +57,6 @@ struct composite_context {
                void (*fn)(struct composite_context *);
                void *private_data;
        } async;
+
+       BOOL used_wait;
 };
index a5f647939fc5b7ad38b84f0830fb4df7105b6535..0a787bbf5780329709da626aa0a48f9bc8786482 100644 (file)
@@ -341,7 +341,7 @@ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
        state->ctx->status = ldap_parse_basic_url(conn, url, &conn->host,
                                                  &conn->port, &conn->ldaps);
        if (!NT_STATUS_IS_OK(state->ctx->status)) {
-               composite_trigger_error(state->ctx);
+               composite_error(state->ctx, state->ctx->status);
                return result;
        }
 
index db5aefeaeb0ef1e58c8184a404e7da8bc111984b..bbed931eedfd83615a06dff7718658ec2a3d357d 100644 (file)
@@ -153,7 +153,7 @@ struct composite_context *resolve_name_send(struct nbt_name *name, struct event_
                struct ipv4_addr ip = interpret_addr2(state->name.name);
                state->reply_addr = talloc_strdup(state, sys_inet_ntoa(ip));
                if (!state->reply_addr) goto failed;
-               composite_trigger_done(c);
+               composite_done(c);
                return c;
        }
 
index 7ed3f97bf3f49cb9732a28185ab8e1382b0e0b7a..d15f370feba8d6df8c2be59a641363a3db781910 100644 (file)
@@ -201,7 +201,7 @@ struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
        return c;
 
 failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index 07970747c498100d925a7bad3da8254920499acc..208e2a94de48f1796d8c5ec01dc2156a939ca290 100644 (file)
@@ -253,7 +253,7 @@ struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *se
        return c;
 
 failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index aabc71a258fe71163aa60ffc5bbe4f859fd8535a..d8fb3e4e6a3b96a3faef2f2e078c1f79f913948f 100644 (file)
@@ -722,7 +722,7 @@ struct composite_context *dcerpc_bind_send(struct dcerpc_pipe *p,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -1633,7 +1633,7 @@ struct composite_context *dcerpc_alter_context_send(struct dcerpc_pipe *p,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index 1dbc3b8aef6e1c3bc5f925a6206cc9cc4b63cbe9..dcf3334212786018d35a7627e883675c9288c845 100644 (file)
@@ -47,7 +47,7 @@ struct composite_context *dcerpc_bind_auth_none_send(TALLOC_CTX *mem_ctx,
        if (!NT_STATUS_IS_OK(c->status)) {
                DEBUG(2,("Invalid uuid string in "
                         "dcerpc_bind_auth_none_send\n"));
-               composite_trigger_error(c);
+               composite_error(c, c->status);
                return c;
        }
 
@@ -277,7 +277,7 @@ struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
                NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED);
 
        if (state->credentials.length == 0) {
-               composite_trigger_done(c);
+               composite_done(c);
                return c;
        }
 
@@ -296,7 +296,7 @@ struct composite_context *dcerpc_bind_auth_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index 055b857e8e899ba979d739f09fe920e0da31be21..cc91e510820a9f522f5b0f1e29b508ee08055687 100644 (file)
@@ -373,7 +373,7 @@ static struct composite_context *lsa_enumtrust_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -475,7 +475,7 @@ static struct composite_context *get_netlogon_schannel_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -655,7 +655,7 @@ static struct composite_context *lookupsids_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -898,7 +898,7 @@ static struct composite_context *get_samr_domain_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -1109,7 +1109,7 @@ static struct composite_context *domadmins_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -1420,7 +1420,7 @@ static struct composite_context *memberships_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -1550,7 +1550,7 @@ static struct composite_context *ntconfig_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
@@ -1689,7 +1689,7 @@ static struct composite_context *xp_login_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index 8efd19f96b3d3ad2872eee232e7dd9b8154d5a47..f6c61c8c3631ec1a4387404a7e27f9a893582593 100644 (file)
@@ -230,7 +230,7 @@ struct composite_context *wb_get_schannel_creds_send(TALLOC_CTX *mem_ctx,
        return c;
 
  failed:
-       composite_trigger_error(c);
+       composite_error(c, c->status);
        return c;
 }
 
index 70f8f4c6263c60798325ed15e5f29d2603b132fc..2c19ef858f465b663ba41008fc79d1e383214a67 100644 (file)
@@ -85,7 +85,7 @@ struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
        state->domain = find_domain_from_sid(service, sid);
        if (state->domain != NULL) {
                result->status = NT_STATUS_OK;
-               composite_trigger_done(result);
+               composite_done(result);
                return result;
        }
 
index 36d189a4d00a6e86201487f0137f4f26b09ccb3a..c7c6f55767c1bccc40550c1601f868fdefa6908d 100644 (file)
@@ -156,12 +156,12 @@ static struct composite_context *wreplsrv_out_connect_send(struct wreplsrv_partn
                if (!wreplconn->sock->dead) {
                        state->stage    = WREPLSRV_OUT_CONNECT_STAGE_DONE;
                        state->wreplconn= wreplconn;
-                       composite_trigger_done(c);
+                       composite_done(c);
                        return c;
                } else if (!cached_connection) {
                        state->stage    = WREPLSRV_OUT_CONNECT_STAGE_DONE;
                        state->wreplconn= NULL;
-                       composite_trigger_done(c);
+                       composite_done(c);
                        return c;
                } else {
                        talloc_free(wreplconn);
@@ -328,7 +328,7 @@ struct composite_context *wreplsrv_pull_table_send(TALLOC_CTX *mem_ctx, struct w
                state->table_io.out.num_partners        = io->in.num_owners;
                state->table_io.out.partners            = io->in.owners;
                state->stage                            = WREPLSRV_PULL_TABLE_STAGE_DONE;
-               composite_trigger_done(c);
+               composite_done(c);
                return c;
        }