2 Unix SMB/Netbios implementation.
4 program to send control messages to Samba processes
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) 2001 by Martin Pool
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 {"force-election", MSG_FORCE_ELECTION},
32 {"profile", MSG_PROFILE},
33 {"profilelevel", MSG_REQ_PROFILELEVEL},
34 {"debuglevel", MSG_REQ_DEBUGLEVEL},
35 {"printer-notify", MSG_PRINTER_NOTIFY},
36 {"close-share", MSG_SMB_FORCE_TDIS},
37 {"samsync", MSG_SMB_SAM_SYNC},
38 {"samrepl", MSG_SMB_SAM_REPL},
39 {"pool-usage", MSG_REQ_POOL_USAGE },
47 static void usage(BOOL doexit)
51 printf("Usage: smbcontrol -i -s configfile\n");
52 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
54 printf("<destination> <message-type> <parameters>\n\n");
56 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
57 printf("\t<message-type> is one of: ");
58 for (i=0; msg_types[i].name; i++)
59 printf("%s%s", i?", ":"",msg_types[i].name);
64 static int pong_count;
65 static BOOL got_level;
66 static BOOL pong_registered = False;
67 static BOOL debuglevel_registered = False;
68 static BOOL profilelevel_registered = False;
69 static BOOL pool_usage_registered = False;
73 * Wait for replies for up to @p *max_secs seconds, or until @p
74 * max_replies are received. max_replies may be NULL in which case it
77 * @note This is a pretty lame timeout; all it means is that after
78 * max_secs we won't look for any more messages.
80 static void wait_for_replies(int max_secs, int *max_replies)
82 time_t timeout_end = time(NULL) + max_secs;
84 while ((!max_replies || (*max_replies)-- > 0)
85 && (time(NULL) < timeout_end)) {
91 /****************************************************************************
92 a useful function for testing the message system
93 ****************************************************************************/
94 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
97 printf("PONG from PID %u\n",(unsigned int)src);
100 /****************************************************************************
101 Prints out the current Debug level returned by MSG_DEBUGLEVEL
102 ****************************************************************************/
103 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
106 int debuglevel_class[DBGC_LAST];
108 memcpy(debuglevel_class, buf, len);
110 printf("Current debug level of PID %u is %d ",(unsigned int)src, debuglevel_class[0]);
111 for (i=1;i<DBGC_LAST;i++)
112 if (debuglevel_class[i])
113 printf("%s:%d ", debug_classname_from_index(i), debuglevel_class[i]);
119 /****************************************************************************
120 Prints out the current Profile level returned by MSG_PROFILELEVEL
121 ****************************************************************************/
122 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
126 memcpy(&level, buf, sizeof(int));
137 s = "count and time";
140 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
142 printf("Profiling not available on PID %u\n",(unsigned int)src);
148 * Handle reply from POOL_USAGE.
150 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
152 printf("Got POOL_USAGE reply from %u\n", (unsigned int) src_pid);
157 * Send a message to a named destination
159 * @return False if an error occurred.
161 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
164 /* "smbd" is the only broadcast operation */
165 if (strequal(dest,"smbd")) {
170 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
172 fprintf(stderr,"Failed to open connections database in send_message.\n");
176 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
178 DEBUG(10,("smbcontrol/send_message: broadcast message to "
179 "%d processes\n", n_sent));
183 } else if (strequal(dest,"nmbd")) {
184 pid = pidfile_pid(dest);
186 fprintf(stderr,"Can't find pid for nmbd\n");
189 } else if (strequal(dest,"self")) {
194 fprintf(stderr,"Not a valid pid\n");
199 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
200 return message_send_pid(pid, msg_type, buf, len, duplicates);
203 /****************************************************************************
204 evaluate a message type string
205 ****************************************************************************/
206 static int parse_type(char *mtype)
209 for (i=0;msg_types[i].name;i++) {
210 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
216 /****************************************************************************
218 ****************************************************************************/
219 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
225 mtype = parse_type(msg_name);
227 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
233 struct debuglevel_message dm;
235 if (!params || !params[0]) {
236 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
241 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
242 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
245 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
250 if (!params || !params[0]) {
251 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
254 if (strequal(params[0], "off")) {
256 } else if (strequal(params[0], "count")) {
258 } else if (strequal(params[0], "on")) {
260 } else if (strequal(params[0], "flush")) {
264 "MSG_PROFILE parameter must be off, count, on, or flush\n");
267 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
270 case MSG_FORCE_ELECTION:
271 if (!strequal(dest, "nmbd")) {
272 fprintf(stderr,"force-election can only be sent to nmbd\n");
275 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
278 case MSG_REQ_PROFILELEVEL:
279 if (!profilelevel_registered) {
280 message_register(MSG_PROFILELEVEL, profilelevel_function);
281 profilelevel_registered = True;
284 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
286 timeout_start = time(NULL);
289 if ((time(NULL) - timeout_start) > MAX_WAIT) {
290 fprintf(stderr,"profilelevel timeout\n");
297 case MSG_REQ_DEBUGLEVEL:
298 if (!debuglevel_registered) {
299 message_register(MSG_DEBUGLEVEL, debuglevel_function);
300 debuglevel_registered = True;
303 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
305 timeout_start = time(NULL);
308 if ((time(NULL) - timeout_start) > MAX_WAIT) {
309 fprintf(stderr,"debuglevel timeout\n");
316 case MSG_PRINTER_NOTIFY:
317 if (!strequal(dest, "smbd")) {
318 fprintf(stderr,"printer-notify can only be sent to smbd\n");
321 if (!params || !params[0]) {
322 fprintf(stderr, "printer-notify needs a printer name\n");
325 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
326 strlen(params[0]) + 1, False);
329 case MSG_SMB_FORCE_TDIS:
330 if (!strequal(dest, "smbd")) {
331 fprintf(stderr,"close-share can only be sent to smbd\n");
334 if (!params || !params[0]) {
335 fprintf(stderr, "close-share needs a share name or '*'\n");
338 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
339 strlen(params[0]) + 1, False);
342 case MSG_SMB_SAM_SYNC:
343 if (!strequal(dest, "smbd")) {
344 fprintf(stderr, "samsync can only be sent to smbd\n");
349 fprintf(stderr, "samsync does not take any parameters\n");
353 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
357 case MSG_SMB_SAM_REPL: {
360 if (!strequal(dest, "smbd")) {
361 fprintf(stderr, "sam repl can only be sent to smbd\n");
365 if (!params || !params[0]) {
366 fprintf(stderr, "SAM_REPL needs a parameter\n");
370 seqnum = atoi(params[0]);
372 retval = send_message(dest, MSG_SMB_SAM_SYNC,
373 (char *)&seqnum, sizeof(uint32), False);
379 if (!pong_registered) {
380 message_register(MSG_PONG, pong_function);
381 pong_registered = True;
383 if (!params || !params[0]) {
384 fprintf(stderr,"MSG_PING needs a parameter\n");
391 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
393 retval = send_message(dest, MSG_PING, NULL, 0, True);
397 wait_for_replies(MAX_WAIT, &n);
399 fprintf(stderr,"PING timeout\n");
403 case MSG_REQ_POOL_USAGE:
405 if (!pool_usage_registered) {
406 message_register(MSG_POOL_USAGE, pool_usage_cb);
407 pool_usage_registered = True;
409 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
411 wait_for_replies(MAX_WAIT, NULL);
420 int main(int argc, char *argv[])
425 BOOL interactive = False;
427 setup_logging(argv[0],True);
429 if (argc < 2) usage(True);
431 while ((opt = getopt(argc, argv,"is:")) != EOF) {
437 pstrcpy(dyn_CONFIGFILE, optarg);
440 printf("Unknown option %c (%d)\n", (char)opt, opt);
445 lp_load(dyn_CONFIGFILE,False,False,False);
447 if (!message_init()) exit(1);
450 argv = &argv[optind];
453 if (argc < 2) usage(True);
454 return (do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0));
461 printf("smbcontrol> ");
462 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
464 while ((myargc < 4) &&
465 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
469 if (strequal(myargv[0],"q")) break;
472 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))