]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
net: Avoid the use of g_lock_do()
authorVolker Lendecke <vl@samba.org>
Thu, 10 Oct 2019 11:38:17 +0000 (13:38 +0200)
committerJeremy Allison <jra@samba.org>
Wed, 6 Nov 2019 20:36:34 +0000 (20:36 +0000)
g_lock_do() does too much in g_lock.c, and it's rarely used.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/utils/net_g_lock.c

index 8b839d2e09f8adcac797dd4ce31302eb68bedbdd..8dd58b2c1bad1bd537954bb0fe1b149a40b49bd3 100644 (file)
@@ -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 <lockname> <timeout> "
                         "<command>\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,