Line data Source code
1 : /*
2 : * Samba Unix/Linux SMB client library
3 : * Interface to the g_lock facility
4 : * Copyright (C) Volker Lendecke 2009
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "includes.h"
21 : #include "net.h"
22 : #include "lib/util/server_id.h"
23 : #include "g_lock.h"
24 : #include "messages.h"
25 : #include "lib/util/util_tdb.h"
26 :
27 0 : static bool net_g_lock_init(TALLOC_CTX *mem_ctx,
28 : struct tevent_context **pev,
29 : struct messaging_context **pmsg,
30 : struct g_lock_ctx **pg_ctx)
31 : {
32 0 : struct tevent_context *ev = NULL;
33 0 : struct messaging_context *msg = NULL;
34 0 : struct g_lock_ctx *g_ctx = NULL;
35 :
36 0 : ev = samba_tevent_context_init(mem_ctx);
37 0 : if (ev == NULL) {
38 0 : d_fprintf(stderr, "ERROR: could not init event context\n");
39 0 : goto fail;
40 : }
41 0 : msg = messaging_init(mem_ctx, ev);
42 0 : if (msg == NULL) {
43 0 : d_fprintf(stderr, "ERROR: could not init messaging context\n");
44 0 : goto fail;
45 : }
46 0 : g_ctx = g_lock_ctx_init(mem_ctx, msg);
47 0 : if (g_ctx == NULL) {
48 0 : d_fprintf(stderr, "ERROR: could not init g_lock context\n");
49 0 : goto fail;
50 : }
51 :
52 0 : *pev = ev;
53 0 : *pmsg = msg;
54 0 : *pg_ctx = g_ctx;
55 0 : return true;
56 0 : fail:
57 0 : TALLOC_FREE(g_ctx);
58 0 : TALLOC_FREE(msg);
59 0 : TALLOC_FREE(ev);
60 0 : return false;
61 : }
62 :
63 0 : static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
64 : {
65 0 : struct g_lock_ctx *ctx = NULL;
66 0 : TDB_DATA key = {0};
67 0 : const char *cmd = NULL;
68 0 : int timeout;
69 0 : NTSTATUS status;
70 0 : int result = -1;
71 :
72 0 : if (argc != 3) {
73 0 : d_printf("Usage: net g_lock do <lockname> <timeout> "
74 : "<command>\n");
75 0 : return -1;
76 : }
77 0 : key = string_term_tdb_data(argv[0]);
78 0 : timeout = atoi(argv[1]);
79 0 : cmd = argv[2];
80 :
81 0 : ctx = g_lock_ctx_init(c, c->msg_ctx);
82 0 : if (ctx == NULL) {
83 0 : d_fprintf(stderr, _("g_lock_ctx_init failed\n"));
84 0 : return -1;
85 : }
86 0 : status = g_lock_lock(ctx,
87 : key,
88 : G_LOCK_WRITE,
89 0 : tevent_timeval_set(timeout / 1000,
90 0 : timeout % 1000),
91 : NULL,
92 : NULL);
93 0 : if (!NT_STATUS_IS_OK(status)) {
94 0 : d_fprintf(stderr,
95 0 : _("g_lock_lock failed: %s\n"),
96 : nt_errstr(status));
97 0 : goto done;
98 : }
99 :
100 0 : result = system(cmd);
101 :
102 0 : g_lock_unlock(ctx, key);
103 :
104 0 : if (result == -1) {
105 0 : d_fprintf(stderr, "ERROR: system() returned %s\n",
106 0 : strerror(errno));
107 0 : goto done;
108 : }
109 0 : d_fprintf(stderr, "command returned %d\n", result);
110 :
111 0 : done:
112 0 : TALLOC_FREE(ctx);
113 0 : return result;
114 : }
115 :
116 0 : static void net_g_lock_dump_fn(struct server_id exclusive,
117 : size_t num_shared,
118 : const struct server_id *shared,
119 : const uint8_t *data,
120 : size_t datalen,
121 : void *private_data)
122 : {
123 0 : struct server_id_buf idbuf;
124 :
125 0 : if (exclusive.pid != 0) {
126 0 : d_printf("%s: WRITE\n",
127 : server_id_str_buf(exclusive, &idbuf));
128 : } else {
129 : size_t i;
130 0 : for (i=0; i<num_shared; i++) {
131 0 : d_printf("%s: READ\n",
132 0 : server_id_str_buf(shared[i], &idbuf));
133 : }
134 : }
135 0 : dump_data_file(data, datalen, true, stdout);
136 0 : }
137 :
138 0 : static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
139 : {
140 0 : struct tevent_context *ev = NULL;
141 0 : struct messaging_context *msg = NULL;
142 0 : struct g_lock_ctx *g_ctx = NULL;
143 0 : int ret = -1;
144 :
145 0 : if (argc != 1) {
146 0 : d_printf("Usage: net g_lock dump <lockname>\n");
147 0 : return -1;
148 : }
149 :
150 0 : if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
151 0 : goto done;
152 : }
153 :
154 0 : (void)g_lock_dump(g_ctx, string_term_tdb_data(argv[0]),
155 : net_g_lock_dump_fn, NULL);
156 :
157 0 : ret = 0;
158 0 : done:
159 0 : TALLOC_FREE(g_ctx);
160 0 : TALLOC_FREE(msg);
161 0 : TALLOC_FREE(ev);
162 0 : return ret;
163 : }
164 :
165 0 : static int net_g_lock_dumpall_fn(TDB_DATA key, void *private_data)
166 : {
167 0 : struct g_lock_ctx *g_ctx = talloc_get_type_abort(
168 : private_data, struct g_lock_ctx);
169 :
170 0 : dump_data_file(key.dptr, key.dsize, true, stdout);
171 0 : g_lock_dump(g_ctx, key, net_g_lock_dump_fn, NULL);
172 0 : printf("\n");
173 :
174 0 : return 0;
175 : }
176 :
177 0 : static int net_g_lock_dumpall(
178 : struct net_context *c, int argc, const char **argv)
179 : {
180 0 : struct tevent_context *ev = NULL;
181 0 : struct messaging_context *msg = NULL;
182 0 : struct g_lock_ctx *g_ctx = NULL;
183 0 : int ret = -1;
184 :
185 0 : if (argc != 0) {
186 0 : d_printf("Usage: net g_lock locks\n");
187 0 : return -1;
188 : }
189 :
190 0 : if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
191 0 : goto done;
192 : }
193 :
194 0 : ret = g_lock_locks(g_ctx, net_g_lock_dumpall_fn, g_ctx);
195 0 : done:
196 0 : TALLOC_FREE(g_ctx);
197 0 : TALLOC_FREE(msg);
198 0 : TALLOC_FREE(ev);
199 0 : return ret < 0 ? -1 : ret;
200 : }
201 :
202 0 : static int net_g_lock_locks_fn(TDB_DATA key, void *private_data)
203 : {
204 0 : dump_data_file(key.dptr, key.dsize, true, stdout);
205 0 : return 0;
206 : }
207 :
208 0 : static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
209 : {
210 0 : struct tevent_context *ev = NULL;
211 0 : struct messaging_context *msg = NULL;
212 0 : struct g_lock_ctx *g_ctx = NULL;
213 0 : int ret = -1;
214 :
215 0 : if (argc != 0) {
216 0 : d_printf("Usage: net g_lock locks\n");
217 0 : return -1;
218 : }
219 :
220 0 : if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
221 0 : goto done;
222 : }
223 :
224 0 : ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
225 0 : done:
226 0 : TALLOC_FREE(g_ctx);
227 0 : TALLOC_FREE(msg);
228 0 : TALLOC_FREE(ev);
229 0 : return ret < 0 ? -1 : ret;
230 : }
231 :
232 0 : int net_g_lock(struct net_context *c, int argc, const char **argv)
233 : {
234 0 : struct functable func[] = {
235 : {
236 : "do",
237 : net_g_lock_do,
238 : NET_TRANSPORT_LOCAL,
239 : N_("Execute a shell command under a lock"),
240 : N_("net g_lock do <lock name> <timeout> <command>\n")
241 : },
242 : {
243 : "locks",
244 : net_g_lock_locks,
245 : NET_TRANSPORT_LOCAL,
246 : N_("List all locknames"),
247 : N_("net g_lock locks\n")
248 : },
249 : {
250 : "dump",
251 : net_g_lock_dump,
252 : NET_TRANSPORT_LOCAL,
253 : N_("Dump a g_lock locking table"),
254 : N_("net g_lock dump <lock name>\n")
255 : },
256 : {
257 : "dumpall",
258 : net_g_lock_dumpall,
259 : NET_TRANSPORT_LOCAL,
260 : N_("Dump all g_lock locking tables"),
261 : N_("net g_lock dumpall\n")
262 : },
263 : {NULL, NULL, 0, NULL, NULL}
264 : };
265 :
266 0 : return net_run_function(c, argc, argv, "net g_lock", func);
267 : }
|