1
0
mirror of https://github.com/samba-team/samba.git synced 2025-10-23 11:33:16 +03:00

r12116: got rid of composite_trigger_done() and composite_trigger_error(), and

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 commit is contained in:
Andrew Tridgell
2005-12-08 01:13:45 +00:00
committed by Gerald (Jerry) Carter
parent 99cf7dbb17
commit f9413ce792
14 changed files with 57 additions and 58 deletions

View 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;
}

View 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;
}

View 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);

View File

@@ -57,4 +57,6 @@ struct composite_context {
void (*fn)(struct composite_context *);
void *private_data;
} async;
BOOL used_wait;
};

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}