summaryrefslogtreecommitdiff
path: root/source3
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2019-10-10 10:27:54 +0200
committerJeremy Allison <jra@samba.org>2019-11-06 20:36:34 +0000
commitd9bd074cc8c0859f4f6333080d457a551e6367a4 (patch)
treec623d2aa12ca2d5c40ca92a02c98923632937528 /source3
parentaaad4d012d83b2e203b7974f56b3f6f72d1c9141 (diff)
downloadsamba-d9bd074cc8c0859f4f6333080d457a551e6367a4.tar.gz
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 <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'source3')
-rw-r--r--source3/utils/net_registry.c136
1 files changed, 65 insertions, 71 deletions
diff --git a/source3/utils/net_registry.c b/source3/utils/net_registry.c
index c6a681de42b..4bffe1c0f6a 100644
--- a/source3/utils/net_registry.c
+++ b/source3/utils/net_registry.c
@@ -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;
}