s3: Update waf build to include missed dependancy on Lion.
[Samba/gebeck_regimport.git] / source3 / utils / net_g_lock.c
blobb9d99160bc5fdceaa4fd808f4b53ca36cee066fd
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 "g_lock.h"
23 #include "messages.h"
25 static bool net_g_lock_init(TALLOC_CTX *mem_ctx,
26 struct tevent_context **pev,
27 struct messaging_context **pmsg,
28 struct g_lock_ctx **pg_ctx)
30 struct tevent_context *ev = NULL;
31 struct messaging_context *msg = NULL;
32 struct g_lock_ctx *g_ctx = NULL;
34 ev = tevent_context_init(mem_ctx);
35 if (ev == NULL) {
36 d_fprintf(stderr, "ERROR: could not init event context\n");
37 goto fail;
39 msg = messaging_init(mem_ctx, ev);
40 if (msg == NULL) {
41 d_fprintf(stderr, "ERROR: could not init messaging context\n");
42 goto fail;
44 g_ctx = g_lock_ctx_init(mem_ctx, msg);
45 if (g_ctx == NULL) {
46 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
47 goto fail;
50 *pev = ev;
51 *pmsg = msg;
52 *pg_ctx = g_ctx;
53 return true;
54 fail:
55 TALLOC_FREE(g_ctx);
56 TALLOC_FREE(msg);
57 TALLOC_FREE(ev);
58 return false;
61 struct net_g_lock_do_state {
62 const char *cmd;
63 int result;
66 static void net_g_lock_do_fn(void *private_data)
68 struct net_g_lock_do_state *state =
69 (struct net_g_lock_do_state *)private_data;
70 state->result = system(state->cmd);
73 static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
75 struct net_g_lock_do_state state;
76 const char *name, *cmd;
77 int timeout;
78 NTSTATUS status;
80 if (argc != 3) {
81 d_printf("Usage: net g_lock do <lockname> <timeout> "
82 "<command>\n");
83 return -1;
85 name = argv[0];
86 timeout = atoi(argv[1]);
87 cmd = argv[2];
89 state.cmd = cmd;
90 state.result = -1;
92 status = g_lock_do(name, G_LOCK_WRITE,
93 timeval_set(timeout / 1000, timeout % 1000),
94 net_g_lock_do_fn, &state);
95 if (!NT_STATUS_IS_OK(status)) {
96 d_fprintf(stderr, "ERROR: g_lock_do failed: %s\n",
97 nt_errstr(status));
98 goto done;
100 if (state.result == -1) {
101 d_fprintf(stderr, "ERROR: system() returned %s\n",
102 strerror(errno));
103 goto done;
105 d_fprintf(stderr, "command returned %d\n", state.result);
107 done:
108 return state.result;
111 static int net_g_lock_dump_fn(struct server_id pid, enum g_lock_type lock_type,
112 void *private_data)
114 char *pidstr;
116 pidstr = server_id_str(talloc_tos(), &pid);
117 d_printf("%s: %s (%s)\n", pidstr,
118 (lock_type & 1) ? "WRITE" : "READ",
119 (lock_type & G_LOCK_PENDING) ? "pending" : "holder");
120 TALLOC_FREE(pidstr);
121 return 0;
124 static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
126 struct tevent_context *ev = NULL;
127 struct messaging_context *msg = NULL;
128 struct g_lock_ctx *g_ctx = NULL;
129 NTSTATUS status;
130 int ret = -1;
132 if (argc != 1) {
133 d_printf("Usage: net g_lock dump <lockname>\n");
134 return -1;
137 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
138 goto done;
141 status = g_lock_dump(g_ctx, argv[0], net_g_lock_dump_fn, NULL);
143 ret = 0;
144 done:
145 TALLOC_FREE(g_ctx);
146 TALLOC_FREE(msg);
147 TALLOC_FREE(ev);
148 return ret;
151 static int net_g_lock_locks_fn(const char *name, void *private_data)
153 d_printf("%s\n", name);
154 return 0;
157 static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
159 struct tevent_context *ev = NULL;
160 struct messaging_context *msg = NULL;
161 struct g_lock_ctx *g_ctx = NULL;
162 int ret = -1;
164 if (argc != 0) {
165 d_printf("Usage: net g_lock locks\n");
166 return -1;
169 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
170 goto done;
173 ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
174 done:
175 TALLOC_FREE(g_ctx);
176 TALLOC_FREE(msg);
177 TALLOC_FREE(ev);
178 return ret < 0 ? -1 : ret;
181 int net_g_lock(struct net_context *c, int argc, const char **argv)
183 struct functable func[] = {
185 "do",
186 net_g_lock_do,
187 NET_TRANSPORT_LOCAL,
188 N_("Execute a shell command under a lock"),
189 N_("net g_lock do <lock name> <timeout> <command>\n")
192 "locks",
193 net_g_lock_locks,
194 NET_TRANSPORT_LOCAL,
195 N_("List all locknames"),
196 N_("net g_lock locks\n")
199 "dump",
200 net_g_lock_dump,
201 NET_TRANSPORT_LOCAL,
202 N_("Dump a g_lock locking table"),
203 N_("net g_lock dump <lock name>\n")
205 {NULL, NULL, 0, NULL, NULL}
208 return net_run_function(c, argc, argv, "net g_lock", func);