summaryrefslogtreecommitdiff
path: root/source3/utils/net_g_lock.c
blob: 8dd58b2c1bad1bd537954bb0fe1b149a40b49bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Samba Unix/Linux SMB client library
 * Interface to the g_lock facility
 * Copyright (C) Volker Lendecke 2009
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "net.h"
#include "lib/util/server_id.h"
#include "g_lock.h"
#include "messages.h"
#include "lib/util/util_tdb.h"

static bool net_g_lock_init(TALLOC_CTX *mem_ctx,
			    struct tevent_context **pev,
			    struct messaging_context **pmsg,
			    struct g_lock_ctx **pg_ctx)
{
	struct tevent_context *ev = NULL;
	struct messaging_context *msg = NULL;
	struct g_lock_ctx *g_ctx = NULL;

	ev = samba_tevent_context_init(mem_ctx);
	if (ev == NULL) {
		d_fprintf(stderr, "ERROR: could not init event context\n");
		goto fail;
	}
	msg = messaging_init(mem_ctx, ev);
	if (msg == NULL) {
		d_fprintf(stderr, "ERROR: could not init messaging context\n");
		goto fail;
	}
	g_ctx = g_lock_ctx_init(mem_ctx, msg);
	if (g_ctx == NULL) {
		d_fprintf(stderr, "ERROR: could not init g_lock context\n");
		goto fail;
	}

	*pev = ev;
	*pmsg = msg;
	*pg_ctx = g_ctx;
	return true;
fail:
	TALLOC_FREE(g_ctx);
	TALLOC_FREE(msg);
	TALLOC_FREE(ev);
	return false;
}

static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
{
	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;
	}
	key = string_term_tdb_data(argv[0]);
	timeout = atoi(argv[1]);
	cmd = argv[2];

	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,
			  _("g_lock_lock failed: %s\n"),
			  nt_errstr(status));
		goto done;
	}

	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", result);

done:
	TALLOC_FREE(ctx);
	return result;
}

static void net_g_lock_dump_fn(const struct g_lock_rec *locks,
			       size_t num_locks,
			       const uint8_t *data,
			       size_t datalen,
			       void *private_data)
{
	size_t i;

	for (i=0; i<num_locks; i++) {
		const struct g_lock_rec *l = &locks[i];
		struct server_id_buf idbuf;
		d_printf("%s: %s\n", server_id_str_buf(l->pid, &idbuf),
			 (l->lock_type & 1) ? "WRITE" : "READ");
	}
	dump_data_file(data, datalen, true, stdout);
}

static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
{
	struct tevent_context *ev = NULL;
	struct messaging_context *msg = NULL;
	struct g_lock_ctx *g_ctx = NULL;
	int ret = -1;

	if (argc != 1) {
		d_printf("Usage: net g_lock dump <lockname>\n");
		return -1;
	}

	if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
		goto done;
	}

	(void)g_lock_dump(g_ctx, string_term_tdb_data(argv[0]),
			  net_g_lock_dump_fn, NULL);

	ret = 0;
done:
	TALLOC_FREE(g_ctx);
	TALLOC_FREE(msg);
	TALLOC_FREE(ev);
	return ret;
}

static int net_g_lock_locks_fn(TDB_DATA key, void *private_data)
{
	if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
		DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
		return 0;
	}
	d_printf("%s\n", (const char *)key.dptr);
	return 0;
}

static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
{
	struct tevent_context *ev = NULL;
	struct messaging_context *msg = NULL;
	struct g_lock_ctx *g_ctx = NULL;
	int ret = -1;

	if (argc != 0) {
		d_printf("Usage: net g_lock locks\n");
		return -1;
	}

	if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
		goto done;
	}

	ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
done:
	TALLOC_FREE(g_ctx);
	TALLOC_FREE(msg);
	TALLOC_FREE(ev);
	return ret < 0 ? -1 : ret;
}

int net_g_lock(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"do",
			net_g_lock_do,
			NET_TRANSPORT_LOCAL,
			N_("Execute a shell command under a lock"),
			N_("net g_lock do <lock name> <timeout> <command>\n")
		},
		{
			"locks",
			net_g_lock_locks,
			NET_TRANSPORT_LOCAL,
			N_("List all locknames"),
			N_("net g_lock locks\n")
		},
		{
			"dump",
			net_g_lock_dump,
			NET_TRANSPORT_LOCAL,
			N_("Dump a g_lock locking table"),
			N_("net g_lock dump <lock name>\n")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	return net_run_function(c, argc, argv, "net g_lock", func);
}