winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / source3 / utils / net_g_lock.c
blob8b839d2e09f8adcac797dd4ce31302eb68bedbdd
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"
25 #include "lib/util/util_tdb.h"
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)
32 struct tevent_context *ev = NULL;
33 struct messaging_context *msg = NULL;
34 struct g_lock_ctx *g_ctx = NULL;
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;
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;
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;
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;
63 struct net_g_lock_do_state {
64 const char *cmd;
65 int result;
68 static void net_g_lock_do_fn(void *private_data)
70 struct net_g_lock_do_state *state =
71 (struct net_g_lock_do_state *)private_data;
72 state->result = system(state->cmd);
75 static int net_g_lock_do(struct net_context *c, int argc, const char **argv)
77 struct net_g_lock_do_state state;
78 const char *name, *cmd;
79 int timeout;
80 NTSTATUS status;
82 if (argc != 3) {
83 d_printf("Usage: net g_lock do <lockname> <timeout> "
84 "<command>\n");
85 return -1;
87 name = argv[0];
88 timeout = atoi(argv[1]);
89 cmd = argv[2];
91 state.cmd = cmd;
92 state.result = -1;
94 status = g_lock_do(string_term_tdb_data(name), G_LOCK_WRITE,
95 timeval_set(timeout / 1000, timeout % 1000),
96 net_g_lock_do_fn, &state);
97 if (!NT_STATUS_IS_OK(status)) {
98 d_fprintf(stderr, "ERROR: g_lock_do failed: %s\n",
99 nt_errstr(status));
100 goto done;
102 if (state.result == -1) {
103 d_fprintf(stderr, "ERROR: system() returned %s\n",
104 strerror(errno));
105 goto done;
107 d_fprintf(stderr, "command returned %d\n", state.result);
109 done:
110 return state.result;
113 static void net_g_lock_dump_fn(const struct g_lock_rec *locks,
114 size_t num_locks,
115 const uint8_t *data,
116 size_t datalen,
117 void *private_data)
119 size_t i;
121 for (i=0; i<num_locks; i++) {
122 const struct g_lock_rec *l = &locks[i];
123 struct server_id_buf idbuf;
124 d_printf("%s: %s\n", server_id_str_buf(l->pid, &idbuf),
125 (l->lock_type & 1) ? "WRITE" : "READ");
127 dump_data_file(data, datalen, true, stdout);
130 static int net_g_lock_dump(struct net_context *c, int argc, const char **argv)
132 struct tevent_context *ev = NULL;
133 struct messaging_context *msg = NULL;
134 struct g_lock_ctx *g_ctx = NULL;
135 int ret = -1;
137 if (argc != 1) {
138 d_printf("Usage: net g_lock dump <lockname>\n");
139 return -1;
142 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
143 goto done;
146 (void)g_lock_dump(g_ctx, string_term_tdb_data(argv[0]),
147 net_g_lock_dump_fn, NULL);
149 ret = 0;
150 done:
151 TALLOC_FREE(g_ctx);
152 TALLOC_FREE(msg);
153 TALLOC_FREE(ev);
154 return ret;
157 static int net_g_lock_locks_fn(TDB_DATA key, void *private_data)
159 if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
160 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
161 return 0;
163 d_printf("%s\n", (const char *)key.dptr);
164 return 0;
167 static int net_g_lock_locks(struct net_context *c, int argc, const char **argv)
169 struct tevent_context *ev = NULL;
170 struct messaging_context *msg = NULL;
171 struct g_lock_ctx *g_ctx = NULL;
172 int ret = -1;
174 if (argc != 0) {
175 d_printf("Usage: net g_lock locks\n");
176 return -1;
179 if (!net_g_lock_init(talloc_tos(), &ev, &msg, &g_ctx)) {
180 goto done;
183 ret = g_lock_locks(g_ctx, net_g_lock_locks_fn, NULL);
184 done:
185 TALLOC_FREE(g_ctx);
186 TALLOC_FREE(msg);
187 TALLOC_FREE(ev);
188 return ret < 0 ? -1 : ret;
191 int net_g_lock(struct net_context *c, int argc, const char **argv)
193 struct functable func[] = {
195 "do",
196 net_g_lock_do,
197 NET_TRANSPORT_LOCAL,
198 N_("Execute a shell command under a lock"),
199 N_("net g_lock do <lock name> <timeout> <command>\n")
202 "locks",
203 net_g_lock_locks,
204 NET_TRANSPORT_LOCAL,
205 N_("List all locknames"),
206 N_("net g_lock locks\n")
209 "dump",
210 net_g_lock_dump,
211 NET_TRANSPORT_LOCAL,
212 N_("Dump a g_lock locking table"),
213 N_("net g_lock dump <lock name>\n")
215 {NULL, NULL, 0, NULL, NULL}
218 return net_run_function(c, argc, argv, "net g_lock", func);