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.
25 extern BOOL AllowDebugChange;
32 {"force-election", MSG_FORCE_ELECTION},
34 {"profile", MSG_PROFILE},
35 {"profilelevel", MSG_REQ_PROFILELEVEL},
36 {"debuglevel", MSG_REQ_DEBUGLEVEL},
37 {"printer-notify", MSG_PRINTER_NOTIFY},
38 {"close-share", MSG_SMB_FORCE_TDIS},
39 {"samsync", MSG_SMB_SAM_SYNC},
40 {"samrepl", MSG_SMB_SAM_REPL},
41 {"pool-usage", MSG_REQ_POOL_USAGE },
49 static void usage(BOOL doexit)
53 printf("Usage: smbcontrol -i -s configfile\n");
54 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
56 printf("<destination> <message-type> <parameters>\n\n");
58 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
59 printf("\t<message-type> is one of: ");
60 for (i=0; msg_types[i].name; i++)
61 printf("%s%s", i?", ":"",msg_types[i].name);
66 static int pong_count;
67 static BOOL got_level;
68 static BOOL pong_registered = False;
69 static BOOL debuglevel_registered = False;
70 static BOOL profilelevel_registered = False;
71 static BOOL pool_usage_registered = False;
75 * Wait for replies for up to @p *max_secs seconds, or until @p
76 * max_replies are received. max_replies may be NULL in which case it
79 * @note This is a pretty lame timeout; all it means is that after
80 * max_secs we won't look for any more messages.
82 static void wait_for_replies(int max_secs, int *max_replies)
84 time_t timeout_end = time(NULL) + max_secs;
86 while ((!max_replies || (*max_replies)-- > 0)
87 && (time(NULL) < timeout_end)) {
93 /****************************************************************************
94 a useful function for testing the message system
95 ****************************************************************************/
96 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
99 printf("PONG from PID %u\n",(unsigned int)src);
102 /****************************************************************************
103 Prints out the current Debug level returned by MSG_DEBUGLEVEL
104 ****************************************************************************/
105 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
108 int debuglevel_class[DBGC_LAST];
110 memcpy(debuglevel_class, buf, len);
112 printf("Current debug level of PID %u is %d ",(unsigned int)src, debuglevel_class[0]);
113 for (i=1;i<DBGC_LAST;i++)
114 if (debuglevel_class[i])
115 printf("%s:%d ", debug_classname_from_index(i), debuglevel_class[i]);
121 /****************************************************************************
122 Prints out the current Profile level returned by MSG_PROFILELEVEL
123 ****************************************************************************/
124 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
128 memcpy(&level, buf, sizeof(int));
139 s = "count and time";
142 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
144 printf("Profiling not available on PID %u\n",(unsigned int)src);
150 * Handle reply from POOL_USAGE.
152 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
154 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
155 (unsigned int) src_pid, (int) len, (const char *) buf);
160 * Send a message to a named destination
162 * @return False if an error occurred.
164 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
167 /* "smbd" is the only broadcast operation */
168 if (strequal(dest,"smbd")) {
173 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
175 fprintf(stderr,"Failed to open connections database in send_message.\n");
179 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
181 DEBUG(10,("smbcontrol/send_message: broadcast message to "
182 "%d processes\n", n_sent));
186 } else if (strequal(dest,"nmbd")) {
187 pid = pidfile_pid(dest);
189 fprintf(stderr,"Can't find pid for nmbd\n");
192 } else if (strequal(dest,"self")) {
197 fprintf(stderr,"Not a valid pid\n");
202 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
203 return message_send_pid(pid, msg_type, buf, len, duplicates);
206 /****************************************************************************
207 evaluate a message type string
208 ****************************************************************************/
209 static int parse_type(char *mtype)
212 for (i=0;msg_types[i].name;i++) {
213 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
219 /****************************************************************************
221 ****************************************************************************/
222 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
228 mtype = parse_type(msg_name);
230 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
236 struct debuglevel_message dm;
238 if (!params || !params[0]) {
239 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
244 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
245 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
248 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
253 if (!params || !params[0]) {
254 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
257 if (strequal(params[0], "off")) {
259 } else if (strequal(params[0], "count")) {
261 } else if (strequal(params[0], "on")) {
263 } else if (strequal(params[0], "flush")) {
267 "MSG_PROFILE parameter must be off, count, on, or flush\n");
270 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
273 case MSG_FORCE_ELECTION:
274 if (!strequal(dest, "nmbd")) {
275 fprintf(stderr,"force-election can only be sent to nmbd\n");
278 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
281 case MSG_REQ_PROFILELEVEL:
282 if (!profilelevel_registered) {
283 message_register(MSG_PROFILELEVEL, profilelevel_function);
284 profilelevel_registered = True;
287 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
289 timeout_start = time(NULL);
292 if ((time(NULL) - timeout_start) > MAX_WAIT) {
293 fprintf(stderr,"profilelevel timeout\n");
300 case MSG_REQ_DEBUGLEVEL:
301 if (!debuglevel_registered) {
302 message_register(MSG_DEBUGLEVEL, debuglevel_function);
303 debuglevel_registered = True;
306 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
308 timeout_start = time(NULL);
311 if ((time(NULL) - timeout_start) > MAX_WAIT) {
312 fprintf(stderr,"debuglevel timeout\n");
319 case MSG_PRINTER_NOTIFY:
320 if (!strequal(dest, "smbd")) {
321 fprintf(stderr,"printer-notify can only be sent to smbd\n");
324 if (!params || !params[0]) {
325 fprintf(stderr, "printer-notify needs a printer name\n");
328 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
329 strlen(params[0]) + 1, False);
332 case MSG_SMB_FORCE_TDIS:
333 if (!strequal(dest, "smbd")) {
334 fprintf(stderr,"close-share can only be sent to smbd\n");
337 if (!params || !params[0]) {
338 fprintf(stderr, "close-share needs a share name or '*'\n");
341 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
342 strlen(params[0]) + 1, False);
345 case MSG_SMB_SAM_SYNC:
346 if (!strequal(dest, "smbd")) {
347 fprintf(stderr, "samsync can only be sent to smbd\n");
352 fprintf(stderr, "samsync does not take any parameters\n");
356 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
360 case MSG_SMB_SAM_REPL: {
363 if (!strequal(dest, "smbd")) {
364 fprintf(stderr, "sam repl can only be sent to smbd\n");
368 if (!params || !params[0]) {
369 fprintf(stderr, "SAM_REPL needs a parameter\n");
373 seqnum = atoi(params[0]);
375 retval = send_message(dest, MSG_SMB_SAM_SYNC,
376 (char *)&seqnum, sizeof(uint32), False);
382 if (!pong_registered) {
383 message_register(MSG_PONG, pong_function);
384 pong_registered = True;
386 if (!params || !params[0]) {
387 fprintf(stderr,"MSG_PING needs a parameter\n");
394 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
396 retval = send_message(dest, MSG_PING, NULL, 0, True);
400 wait_for_replies(MAX_WAIT, &n);
402 fprintf(stderr,"PING timeout\n");
406 case MSG_REQ_POOL_USAGE:
407 if (!pool_usage_registered) {
408 message_register(MSG_POOL_USAGE, pool_usage_cb);
409 pool_usage_registered = True;
411 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
413 wait_for_replies(MAX_WAIT, NULL);
421 int main(int argc, char *argv[])
426 BOOL interactive = False;
428 AllowDebugChange = False;
431 setup_logging(argv[0],True);
433 if (argc < 2) usage(True);
435 while ((opt = getopt(argc, argv,"is:")) != EOF) {
441 pstrcpy(dyn_CONFIGFILE, optarg);
444 printf("Unknown option %c (%d)\n", (char)opt, opt);
449 lp_load(dyn_CONFIGFILE,False,False,False);
451 if (!message_init()) exit(1);
454 argv = &argv[optind];
457 if (argc < 2) usage(True);
458 /* Need to invert sense of return code -- samba
459 * routines mostly return True==1 for success, but
461 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
468 printf("smbcontrol> ");
469 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
471 while ((myargc < 4) &&
472 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
476 if (strequal(myargv[0],"q")) break;
479 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))