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
net_g_lock_init(TALLOC_CTX * mem_ctx,struct tevent_context ** pev,struct messaging_context ** pmsg,struct g_lock_ctx ** pg_ctx)27 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 struct tevent_context *ev = NULL;
33 struct messaging_context *msg = NULL;
34 struct g_lock_ctx *g_ctx = NULL;
35
36 ev = samba_tevent_context_init(mem_ctx);
37 if (ev == NULL) {
38 d_fprintf(stderr, "ERROR: could not init event context\n");
39 goto fail;
40 }
41 msg = messaging_init(mem_ctx, ev);
42 if (msg == NULL) {
43 d_fprintf(stderr, "ERROR: could not init messaging context\n");
44 goto fail;
45 }
46 g_ctx = g_lock_ctx_init(mem_ctx, msg);
47 if (g_ctx == NULL) {
48 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
49 goto fail;
50 }
51
52 *pev = ev;
53 *pmsg = msg;
54 *pg_ctx = g_ctx;
55 return true;
56 fail:
57 TALLOC_FREE(g_ctx);
58 TALLOC_FREE(msg);
59 TALLOC_FREE(ev);
60 return false;
61 }
62
net_g_lock_do(struct net_context * c,int argc,const char ** argv)63 static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
64 {
65 struct g_lock_ctx *ctx = NULL;
66 TDB_DATA key = {0};
67 const char *cmd = NULL;
68 int timeout;
69 NTSTATUS status;
70 int result = -1;
71
72 if (argc != 3) {
73 d_printf("Usage: net g_lock do <lockname> <timeout> "
74 "<command>\n");
75 return -1;
76 }
77 key = string_term_tdb_data(argv[0]);
78 timeout = atoi(argv[1]);
79 cmd = argv[2];
80
81 ctx = g_lock_ctx_init(c, c->msg_ctx);
82 if (ctx == NULL) {
83 d_fprintf(stderr, _("g_lock_ctx_init failed\n"));
84 return -1;
85 }
86 status = g_lock_lock(
87 ctx,
88 key,
89 G_LOCK_WRITE,
90 timeval_set(timeout / 1000, timeout % 1000));
91 if (!NT_STATUS_IS_OK(status)) {
92 d_fprintf(stderr,
93 _("g_lock_lock failed: %s\n"),
94 nt_errstr(status));
95 goto done;
96 }
97
98 result = system(cmd);
99
100 g_lock_unlock(ctx, key);
101
102 if (result == -1) {
103 d_fprintf(stderr, "ERROR: system() returned %s\n",
104 strerror(errno));
105 goto done;
106 }
107 d_fprintf(stderr, "command returned %d\n", result);
108
109 done:
110 TALLOC_FREE(ctx);
111 return result;
112 }
113
net_g_lock_dump_fn(struct server_id exclusive,size_t num_shared,struct server_id * shared,const uint8_t * data,size_t datalen,void * private_data)114 static void net_g_lock_dump_fn(struct server_id exclusive,
115 size_t num_shared,
116 struct server_id *shared,
117 const uint8_t *data,
118 size_t datalen,
119 void *private_data)
120 {
121 struct server_id_buf idbuf;
122
123 if (exclusive.pid != 0) {
124 d_printf("%s: WRITE\n",
125 server_id_str_buf(exclusive, &idbuf));
126 } else {
127 size_t i;
128 for (i=0; i<num_shared; i++) {
129 d_printf("%s: READ\n",
130 server_id_str_buf(shared[i], &idbuf));
131 }
132 }
133 dump_data_file(data, datalen, true, stdout);
134 }
135
net_g_lock_dump(struct net_context * c,int argc,const char ** argv)136 static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
137 {
138 struct tevent_context *ev = NULL;
139 struct messaging_context *msg = NULL;
140 struct g_lock_ctx *g_ctx = NULL;
141 int ret = -1;
142
143 if (argc != 1) {
144 d_printf("Usage: net g_lock dump <lockname>\n");
145 return -1;
146 }
147
148 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
149 goto done;
150 }
151
152 (void)g_lock_dump(g_ctx, string_term_tdb_data(argv[0]),
153 net_g_lock_dump_fn, NULL);
154
155 ret = 0;
156 done:
157 TALLOC_FREE(g_ctx);
158 TALLOC_FREE(msg);
159 TALLOC_FREE(ev);
160 return ret;
161 }
162
net_g_lock_locks_fn(TDB_DATA key,void * private_data)163 static int net_g_lock_locks_fn(TDB_DATA key, void *private_data)
164 {
165 if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
166 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
167 return 0;
168 }
169 d_printf("%s\n", (const char *)key.dptr);
170 return 0;
171 }
172
net_g_lock_locks(struct net_context * c,int argc,const char ** argv)173 static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
174 {
175 struct tevent_context *ev = NULL;
176 struct messaging_context *msg = NULL;
177 struct g_lock_ctx *g_ctx = NULL;
178 int ret = -1;
179
180 if (argc != 0) {
181 d_printf("Usage: net g_lock locks\n");
182 return -1;
183 }
184
185 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
186 goto done;
187 }
188
189 ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
190 done:
191 TALLOC_FREE(g_ctx);
192 TALLOC_FREE(msg);
193 TALLOC_FREE(ev);
194 return ret < 0 ? -1 : ret;
195 }
196
net_g_lock(struct net_context * c,int argc,const char ** argv)197 int net_g_lock(struct net_context *c, int argc, const char **argv)
198 {
199 struct functable func[] = {
200 {
201 "do",
202 net_g_lock_do,
203 NET_TRANSPORT_LOCAL,
204 N_("Execute a shell command under a lock"),
205 N_("net g_lock do <lock name> <timeout> <command>\n")
206 },
207 {
208 "locks",
209 net_g_lock_locks,
210 NET_TRANSPORT_LOCAL,
211 N_("List all locknames"),
212 N_("net g_lock locks\n")
213 },
214 {
215 "dump",
216 net_g_lock_dump,
217 NET_TRANSPORT_LOCAL,
218 N_("Dump a g_lock locking table"),
219 N_("net g_lock dump <lock name>\n")
220 },
221 {NULL, NULL, 0, NULL, NULL}
222 };
223
224 return net_run_function(c, argc, argv, "net g_lock", func);
225 }
226