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/>.
22 #include "lib/util/server_id.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
);
38 d_fprintf(stderr
, "ERROR: could not init event context\n");
41 msg
= messaging_init(mem_ctx
, ev
);
43 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
46 g_ctx
= g_lock_ctx_init(mem_ctx
, msg
);
48 d_fprintf(stderr
, "ERROR: could not init g_lock context\n");
63 static int net_g_lock_do(struct net_context
*c
, int argc
, const char **argv
)
65 struct g_lock_ctx
*ctx
= NULL
;
67 const char *cmd
= NULL
;
73 d_printf("Usage: net g_lock do <lockname> <timeout> "
77 key
= string_term_tdb_data(argv
[0]);
78 timeout
= atoi(argv
[1]);
81 ctx
= g_lock_ctx_init(c
, c
->msg_ctx
);
83 d_fprintf(stderr
, _("g_lock_ctx_init failed\n"));
86 status
= g_lock_lock(ctx
,
89 tevent_timeval_set(timeout
/ 1000,
93 if (!NT_STATUS_IS_OK(status
)) {
95 _("g_lock_lock failed: %s\n"),
100 result
= system(cmd
);
102 g_lock_unlock(ctx
, key
);
105 d_fprintf(stderr
, "ERROR: system() returned %s\n",
109 d_fprintf(stderr
, "command returned %d\n", result
);
116 static void net_g_lock_dump_fn(struct server_id exclusive
,
118 const struct server_id
*shared
,
123 struct server_id_buf idbuf
;
125 if (exclusive
.pid
!= 0) {
126 d_printf("%s: WRITE\n",
127 server_id_str_buf(exclusive
, &idbuf
));
130 for (i
=0; i
<num_shared
; i
++) {
131 d_printf("%s: READ\n",
132 server_id_str_buf(shared
[i
], &idbuf
));
135 dump_data_file(data
, datalen
, true, stdout
);
138 static int net_g_lock_dump(struct net_context
*c
, int argc
, const char **argv
)
140 struct tevent_context
*ev
= NULL
;
141 struct messaging_context
*msg
= NULL
;
142 struct g_lock_ctx
*g_ctx
= NULL
;
146 d_printf("Usage: net g_lock dump <lockname>\n");
150 if (!net_g_lock_init(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
154 (void)g_lock_dump(g_ctx
, string_term_tdb_data(argv
[0]),
155 net_g_lock_dump_fn
, NULL
);
165 static int net_g_lock_dumpall_fn(TDB_DATA key
, void *private_data
)
167 struct g_lock_ctx
*g_ctx
= talloc_get_type_abort(
168 private_data
, struct g_lock_ctx
);
170 dump_data_file(key
.dptr
, key
.dsize
, true, stdout
);
171 g_lock_dump(g_ctx
, key
, net_g_lock_dump_fn
, NULL
);
177 static int net_g_lock_dumpall(
178 struct net_context
*c
, int argc
, const char **argv
)
180 struct tevent_context
*ev
= NULL
;
181 struct messaging_context
*msg
= NULL
;
182 struct g_lock_ctx
*g_ctx
= NULL
;
186 d_printf("Usage: net g_lock locks\n");
190 if (!net_g_lock_init(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
194 ret
= g_lock_locks_read(g_ctx
, net_g_lock_dumpall_fn
, g_ctx
);
199 return ret
< 0 ? -1 : ret
;
202 static int net_g_lock_locks_fn(TDB_DATA key
, void *private_data
)
204 dump_data_file(key
.dptr
, key
.dsize
, true, stdout
);
208 static int net_g_lock_locks(struct net_context
*c
, int argc
, const char **argv
)
210 struct tevent_context
*ev
= NULL
;
211 struct messaging_context
*msg
= NULL
;
212 struct g_lock_ctx
*g_ctx
= NULL
;
216 d_printf("Usage: net g_lock locks\n");
220 if (!net_g_lock_init(talloc_tos(), &ev
, &msg
, &g_ctx
)) {
224 ret
= g_lock_locks_read(g_ctx
, net_g_lock_locks_fn
, NULL
);
229 return ret
< 0 ? -1 : ret
;
232 int net_g_lock(struct net_context
*c
, int argc
, const char **argv
)
234 struct functable func
[] = {
239 N_("Execute a shell command under a lock"),
240 N_("net g_lock do <lock name> <timeout> <command>\n")
246 N_("List all locknames"),
247 N_("net g_lock locks\n")
253 N_("Dump a g_lock locking table"),
254 N_("net g_lock dump <lock name>\n")
260 N_("Dump all g_lock locking tables"),
261 N_("net g_lock dumpall\n")
263 {NULL
, NULL
, 0, NULL
, NULL
}
266 return net_run_function(c
, argc
, argv
, "net g_lock", func
);