]> 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 08:27:54 +0000 (10:27 +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_registry.c

index c6a681de42b986b669ba377361517f1633ef791f..4bffe1c0f6a7aa79b7a60435169300869f685756 100644 (file)
@@ -568,34 +568,68 @@ done:
        return ret;
 }
 
-struct net_registry_increment_state {
-       const char *keyname;
-       const char *valuename;
+static int net_registry_increment(struct net_context *c, int argc,
+                                 const char **argv)
+{
+       TDB_DATA lock_key = string_term_tdb_data("registry_increment_lock");
+       struct g_lock_ctx *ctx = NULL;
+       const char *keyname = NULL;
+       struct registry_key *key = NULL;
+       const char *valuename = NULL;
+       struct registry_value *value = NULL;
+       uint32_t v;
        uint32_t increment;
        uint32_t newvalue;
+       NTSTATUS status;
        WERROR werr;
-};
+       int ret = -1;
 
-static void net_registry_increment_fn(void *private_data)
-{
-       struct net_registry_increment_state *state =
-               (struct net_registry_increment_state *)private_data;
-       struct registry_value *value;
-       struct registry_key *key = NULL;
-       uint32_t v;
+       if (argc < 2 || c->display_usage) {
+               d_fprintf(stderr, "%s\n%s",
+                         _("Usage:"),
+                         _("net registry increment <key> <valuename> "
+                           "[<increment>]\n"));
+               goto done;
+       }
+
+       keyname = argv[0];
+       valuename = argv[1];
 
-       state->werr = open_key(talloc_tos(), state->keyname,
-                              REG_KEY_READ|REG_KEY_WRITE, &key);
-       if (!W_ERROR_IS_OK(state->werr)) {
+       increment = 1;
+       if (argc == 3) {
+               int error = 0;
+
+               increment = smb_strtoul(
+                       argv[2], NULL, 10, &error, SMB_STR_STANDARD);
+               if (error != 0) {
+                       goto done;
+               }
+       }
+
+       ctx = g_lock_ctx_init(c, c->msg_ctx);
+       if (ctx == NULL) {
+               d_fprintf(stderr, _("g_lock_ctx_init failed\n"));
+               goto done;
+       }
+
+       status = g_lock_lock(ctx, lock_key, G_LOCK_WRITE, timeval_set(600, 0));
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("g_lock_lock failed: %s\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+
+       werr = open_key(c, keyname, REG_KEY_READ|REG_KEY_WRITE, &key);
+       if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, _("open_key failed: %s\n"),
-                         win_errstr(state->werr));
+                         win_errstr(werr));
                goto done;
        }
 
-       state->werr = reg_queryvalue(key, key, state->valuename, &value);
-       if (!W_ERROR_IS_OK(state->werr)) {
+       werr = reg_queryvalue(key, key, valuename, &value);
+       if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
-                         win_errstr(state->werr));
+                         win_errstr(werr));
                goto done;
        }
 
@@ -611,74 +645,34 @@ static void net_registry_increment_fn(void *private_data)
        }
 
        v = IVAL(value->data.data, 0);
-       v += state->increment;
-       state->newvalue = v;
+       v += increment;
+       newvalue = v;
 
        SIVAL(value->data.data, 0, v);
 
-       state->werr = reg_setvalue(key, state->valuename, value);
-       if (!W_ERROR_IS_OK(state->werr)) {
+       werr = reg_setvalue(key, valuename, value);
+       if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
-                         win_errstr(state->werr));
-               goto done;
-       }
-
-done:
-       TALLOC_FREE(key);
-       return;
-}
-
-static int net_registry_increment(struct net_context *c, int argc,
-                                 const char **argv)
-{
-       struct net_registry_increment_state state;
-       NTSTATUS status;
-       int ret = -1;
-
-       if (argc < 2 || c->display_usage) {
-               d_fprintf(stderr, "%s\n%s",
-                         _("Usage:"),
-                         _("net registry increment <key> <valuename> "
-                           "[<increment>]\n"));
+                         win_errstr(werr));
                goto done;
        }
 
-       state.keyname = argv[0];
-       state.valuename = argv[1];
-
-       state.increment = 1;
-       if (argc == 3) {
-               int error = 0;
-
-               state.increment = smb_strtoul(argv[2],
-                                             NULL,
-                                             10,
-                                             &error,
-                                             SMB_STR_STANDARD);
-               if (error != 0) {
-                       goto done;
-               }
-       }
-
-       status = g_lock_do(string_term_tdb_data("registry_increment_lock"),
-                          G_LOCK_WRITE, timeval_set(600, 0),
-                          net_registry_increment_fn, &state);
-       if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, _("g_lock_do failed: %s\n"),
-                         nt_errstr(status));
-               goto done;
-       }
-       if (!W_ERROR_IS_OK(state.werr)) {
+       if (!W_ERROR_IS_OK(werr)) {
                d_fprintf(stderr, _("increment failed: %s\n"),
-                         win_errstr(state.werr));
+                         win_errstr(werr));
                goto done;
        }
 
-       d_printf(_("%u\n"), (unsigned)state.newvalue);
+       g_lock_unlock(ctx, lock_key);
+
+       d_printf(_("%"PRIu32"\n"), newvalue);
 
        ret = 0;
 
 done:
+       TALLOC_FREE(value);
+       TALLOC_FREE(key);
+       TALLOC_FREE(ctx);
        return ret;
 }