dbwrap: add parse_record_send/recv to struct db_context
[Samba.git] / source3 / utils / net_g_lock.c
blobd786f5a4ecb3e6ad92c5db9277a834cb24ef2fa1
1 /*
2 * Samba Unix/Linux SMB client library
3 * Interface to the g_lock facility
4 * Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "net.h"
22 #include "lib/util/server_id.h"
23 #include "g_lock.h"
24 #include "messages.h"
26 static bool net_g_lock_init(TALLOC_CTX *mem_ctx,
27 struct tevent_context **pev,
28 struct messaging_context **pmsg,
29 struct g_lock_ctx **pg_ctx)
31 struct tevent_context *ev = NULL;
32 struct messaging_context *msg = NULL;
33 struct g_lock_ctx *g_ctx = NULL;
35 ev = samba_tevent_context_init(mem_ctx);
36 if (ev == NULL) {
37 d_fprintf(stderr, "ERROR: could not init event context\n");
38 goto fail;
40 msg = messaging_init(mem_ctx, ev);
41 if (msg == NULL) {
42 d_fprintf(stderr, "ERROR: could not init messaging context\n");
43 goto fail;
45 g_ctx = g_lock_ctx_init(mem_ctx, msg);
46 if (g_ctx == NULL) {
47 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
48 goto fail;
51 *pev = ev;
52 *pmsg = msg;
53 *pg_ctx = g_ctx;
54 return true;
55 fail:
56 TALLOC_FREE(g_ctx);
57 TALLOC_FREE(msg);
58 TALLOC_FREE(ev);
59 return false;
62 struct net_g_lock_do_state {
63 const char *cmd;
64 int result;
67 static void net_g_lock_do_fn(void *private_data)
69 struct net_g_lock_do_state *state =
70 (struct net_g_lock_do_state *)private_data;
71 state->result = system(state->cmd);
74 static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
76 struct net_g_lock_do_state state;
77 const char *name, *cmd;
78 int timeout;
79 NTSTATUS status;
81 if (argc != 3) {
82 d_printf("Usage: net g_lock do <lockname> <timeout> "
83 "<command>\n");
84 return -1;
86 name = argv[0];
87 timeout = atoi(argv[1]);
88 cmd = argv[2];
90 state.cmd = cmd;
91 state.result = -1;
93 status = g_lock_do(name, G_LOCK_WRITE,
94 timeval_set(timeout / 1000, timeout % 1000),
95 net_g_lock_do_fn, &state);
96 if (!NT_STATUS_IS_OK(status)) {
97 d_fprintf(stderr, "ERROR: g_lock_do failed: %s\n",
98 nt_errstr(status));
99 goto done;
101 if (state.result == -1) {
102 d_fprintf(stderr, "ERROR: system() returned %s\n",
103 strerror(errno));
104 goto done;
106 d_fprintf(stderr, "command returned %d\n", state.result);
108 done:
109 return state.result;
112 static int net_g_lock_dump_fn(struct server_id pid, enum g_lock_type lock_type,
113 void *private_data)
115 struct server_id_buf idbuf;
116 d_printf("%s: %s\n", server_id_str_buf(pid, &idbuf),
117 (lock_type & 1) ? "WRITE" : "READ");
118 return 0;
121 static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
123 struct tevent_context *ev = NULL;
124 struct messaging_context *msg = NULL;
125 struct g_lock_ctx *g_ctx = NULL;
126 int ret = -1;
128 if (argc != 1) {
129 d_printf("Usage: net g_lock dump <lockname>\n");
130 return -1;
133 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
134 goto done;
137 (void)g_lock_dump(g_ctx, argv[0], net_g_lock_dump_fn, NULL);
139 ret = 0;
140 done:
141 TALLOC_FREE(g_ctx);
142 TALLOC_FREE(msg);
143 TALLOC_FREE(ev);
144 return ret;
147 static int net_g_lock_locks_fn(const char *name, void *private_data)
149 d_printf("%s\n", name);
150 return 0;
153 static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
155 struct tevent_context *ev = NULL;
156 struct messaging_context *msg = NULL;
157 struct g_lock_ctx *g_ctx = NULL;
158 int ret = -1;
160 if (argc != 0) {
161 d_printf("Usage: net g_lock locks\n");
162 return -1;
165 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
166 goto done;
169 ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
170 done:
171 TALLOC_FREE(g_ctx);
172 TALLOC_FREE(msg);
173 TALLOC_FREE(ev);
174 return ret < 0 ? -1 : ret;
177 int net_g_lock(struct net_context *c, int argc, const char **argv)
179 struct functable func[] = {
181 "do",
182 net_g_lock_do,
183 NET_TRANSPORT_LOCAL,
184 N_("Execute a shell command under a lock"),
185 N_("net g_lock do <lock name> <timeout> <command>\n")
188 "locks",
189 net_g_lock_locks,
190 NET_TRANSPORT_LOCAL,
191 N_("List all locknames"),
192 N_("net g_lock locks\n")
195 "dump",
196 net_g_lock_dump,
197 NET_TRANSPORT_LOCAL,
198 N_("Dump a g_lock locking table"),
199 N_("net g_lock dump <lock name>\n")
201 {NULL, NULL, 0, NULL, NULL}
204 return net_run_function(c, argc, argv, "net g_lock", func);