From 9dd65d1f991722bf6686ae21e4607192fc2bf327 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 Oct 2019 13:38:17 +0200 Subject: [PATCH] net: Avoid the use of g_lock_do() g_lock_do() does too much in g_lock.c, and it's rarely used. Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- source3/utils/net_g_lock.c | 51 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/source3/utils/net_g_lock.c b/source3/utils/net_g_lock.c index 8b839d2e09f..8dd58b2c1ba 100644 --- a/source3/utils/net_g_lock.c +++ b/source3/utils/net_g_lock.c @@ -60,54 +60,55 @@ fail: return false; } -struct net_g_lock_do_state { - const char *cmd; - int result; -}; - -static void net_g_lock_do_fn(void *private_data) -{ - struct net_g_lock_do_state *state = - (struct net_g_lock_do_state *)private_data; - state->result = system(state->cmd); -} - static int net_g_lock_do(struct net_context *c, int argc, const char **argv) { - struct net_g_lock_do_state state; - const char *name, *cmd; + struct g_lock_ctx *ctx = NULL; + TDB_DATA key = {0}; + const char *cmd = NULL; int timeout; NTSTATUS status; + int result = -1; if (argc != 3) { d_printf("Usage: net g_lock do " "\n"); return -1; } - name = argv[0]; + key = string_term_tdb_data(argv[0]); timeout = atoi(argv[1]); cmd = argv[2]; - state.cmd = cmd; - state.result = -1; - - status = g_lock_do(string_term_tdb_data(name), G_LOCK_WRITE, - timeval_set(timeout / 1000, timeout % 1000), - net_g_lock_do_fn, &state); + ctx = g_lock_ctx_init(c, c->msg_ctx); + if (ctx == NULL) { + d_fprintf(stderr, _("g_lock_ctx_init failed\n")); + return -1; + } + status = g_lock_lock( + ctx, + key, + G_LOCK_WRITE, + timeval_set(timeout / 1000, timeout % 1000)); if (!NT_STATUS_IS_OK(status)) { - d_fprintf(stderr, "ERROR: g_lock_do failed: %s\n", + d_fprintf(stderr, + _("g_lock_lock failed: %s\n"), nt_errstr(status)); goto done; } - if (state.result == -1) { + + result = system(cmd); + + g_lock_unlock(ctx, key); + + if (result == -1) { d_fprintf(stderr, "ERROR: system() returned %s\n", strerror(errno)); goto done; } - d_fprintf(stderr, "command returned %d\n", state.result); + d_fprintf(stderr, "command returned %d\n", result); done: - return state.result; + TALLOC_FREE(ctx); + return result; } static void net_g_lock_dump_fn(const struct g_lock_rec *locks,