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, 2002 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 },
42 {"dmalloc-mark", MSG_REQ_DMALLOC_MARK },
43 {"dmalloc-log-changed", MSG_REQ_DMALLOC_LOG_CHANGED },
51 static void usage(BOOL doexit)
55 printf("Usage: smbcontrol -i -s configfile\n");
56 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
58 printf("<destination> <message-type> <parameters>\n\n");
60 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
61 printf("\t<message-type> is one of: ");
62 for (i=0; msg_types[i].name; i++)
63 printf("%s%s", i?", ":"",msg_types[i].name);
68 static int pong_count;
69 static BOOL got_level;
70 static BOOL pong_registered = False;
71 static BOOL debuglevel_registered = False;
72 static BOOL profilelevel_registered = False;
76 * Wait for replies for up to @p *max_secs seconds, or until @p
77 * max_replies are received. max_replies may be NULL in which case it
80 * @note This is a pretty lame timeout; all it means is that after
81 * max_secs we won't look for any more messages.
83 static void wait_for_replies(int max_secs, int *max_replies)
85 time_t timeout_end = time(NULL) + max_secs;
87 while ((!max_replies || (*max_replies)-- > 0)
88 && (time(NULL) < timeout_end)) {
94 /****************************************************************************
95 a useful function for testing the message system
96 ****************************************************************************/
97 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
100 printf("PONG from PID %u\n",(unsigned int)src);
103 /****************************************************************************
104 Prints out the current Debug level returned by MSG_DEBUGLEVEL
105 ****************************************************************************/
106 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
109 int debuglevel_class[DBGC_LAST];
111 memcpy(debuglevel_class, buf, len);
113 printf("Current debug level of PID %u is %d ",(unsigned int)src, debuglevel_class[0]);
114 for (i=1;i<DBGC_LAST;i++)
115 if (debuglevel_class[i])
116 printf("%s:%d ", debug_classname_from_index(i), debuglevel_class[i]);
122 /****************************************************************************
123 Prints out the current Profile level returned by MSG_PROFILELEVEL
124 ****************************************************************************/
125 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
129 memcpy(&level, buf, sizeof(int));
140 s = "count and time";
143 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
145 printf("Profiling not available on PID %u\n",(unsigned int)src);
151 * Handle reply from POOL_USAGE.
153 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
155 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
156 (unsigned int) src_pid, (int) len, (const char *) buf);
161 * Send a message to a named destination
163 * @return False if an error occurred.
165 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
168 /* "smbd" is the only broadcast operation */
169 if (strequal(dest,"smbd")) {
174 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
176 fprintf(stderr,"Failed to open connections database in send_message.\n");
180 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
182 DEBUG(10,("smbcontrol/send_message: broadcast message to "
183 "%d processes\n", n_sent));
187 } else if (strequal(dest,"nmbd")) {
188 pid = pidfile_pid(dest);
190 fprintf(stderr,"Can't find pid for nmbd\n");
193 } else if (strequal(dest,"self")) {
198 fprintf(stderr,"Not a valid pid\n");
203 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
204 return message_send_pid(pid, msg_type, buf, len, duplicates);
207 /****************************************************************************
208 evaluate a message type string
209 ****************************************************************************/
210 static int parse_type(char *mtype)
213 for (i=0;msg_types[i].name;i++) {
214 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
220 static void register_all(void)
222 message_register(MSG_POOL_USAGE, pool_usage_cb);
226 /****************************************************************************
228 ****************************************************************************/
229 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
235 mtype = parse_type(msg_name);
237 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
243 struct debuglevel_message dm;
245 if (!params || !params[0]) {
246 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
251 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
252 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
255 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
260 if (!params || !params[0]) {
261 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
264 if (strequal(params[0], "off")) {
266 } else if (strequal(params[0], "count")) {
268 } else if (strequal(params[0], "on")) {
270 } else if (strequal(params[0], "flush")) {
274 "MSG_PROFILE parameter must be off, count, on, or flush\n");
277 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
280 case MSG_FORCE_ELECTION:
281 if (!strequal(dest, "nmbd")) {
282 fprintf(stderr,"force-election can only be sent to nmbd\n");
285 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
288 case MSG_REQ_PROFILELEVEL:
289 if (!profilelevel_registered) {
290 message_register(MSG_PROFILELEVEL, profilelevel_function);
291 profilelevel_registered = True;
294 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
296 timeout_start = time(NULL);
299 if ((time(NULL) - timeout_start) > MAX_WAIT) {
300 fprintf(stderr,"profilelevel timeout\n");
307 case MSG_REQ_DEBUGLEVEL:
308 if (!debuglevel_registered) {
309 message_register(MSG_DEBUGLEVEL, debuglevel_function);
310 debuglevel_registered = True;
313 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
315 timeout_start = time(NULL);
318 if ((time(NULL) - timeout_start) > MAX_WAIT) {
319 fprintf(stderr,"debuglevel timeout\n");
326 case MSG_PRINTER_NOTIFY:
327 if (!strequal(dest, "smbd")) {
328 fprintf(stderr,"printer-notify can only be sent to smbd\n");
331 if (!params || !params[0]) {
332 fprintf(stderr, "printer-notify needs a printer name\n");
335 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
336 strlen(params[0]) + 1, False);
339 case MSG_SMB_FORCE_TDIS:
340 if (!strequal(dest, "smbd")) {
341 fprintf(stderr,"close-share can only be sent to smbd\n");
344 if (!params || !params[0]) {
345 fprintf(stderr, "close-share needs a share name or '*'\n");
348 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
349 strlen(params[0]) + 1, False);
352 case MSG_SMB_SAM_SYNC:
353 if (!strequal(dest, "smbd")) {
354 fprintf(stderr, "samsync can only be sent to smbd\n");
359 fprintf(stderr, "samsync does not take any parameters\n");
363 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
367 case MSG_SMB_SAM_REPL: {
370 if (!strequal(dest, "smbd")) {
371 fprintf(stderr, "sam repl can only be sent to smbd\n");
375 if (!params || !params[0]) {
376 fprintf(stderr, "SAM_REPL needs a parameter\n");
380 seqnum = atoi(params[0]);
382 retval = send_message(dest, MSG_SMB_SAM_SYNC,
383 (char *)&seqnum, sizeof(uint32), False);
389 if (!pong_registered) {
390 message_register(MSG_PONG, pong_function);
391 pong_registered = True;
393 if (!params || !params[0]) {
394 fprintf(stderr,"MSG_PING needs a parameter\n");
401 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
403 retval = send_message(dest, MSG_PING, NULL, 0, True);
407 wait_for_replies(MAX_WAIT, &n);
409 fprintf(stderr,"PING timeout\n");
413 case MSG_REQ_POOL_USAGE:
414 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
416 wait_for_replies(MAX_WAIT, NULL);
420 case MSG_REQ_DMALLOC_LOG_CHANGED:
421 case MSG_REQ_DMALLOC_MARK:
422 if (!send_message(dest, mtype, NULL, 0, False))
430 int main(int argc, char *argv[])
435 BOOL interactive = False;
437 AllowDebugChange = False;
440 setup_logging(argv[0],True);
442 if (argc < 2) usage(True);
444 while ((opt = getopt(argc, argv,"is:")) != EOF) {
450 pstrcpy(dyn_CONFIGFILE, optarg);
453 printf("Unknown option %c (%d)\n", (char)opt, opt);
458 lp_load(dyn_CONFIGFILE,False,False,False);
460 if (!message_init()) exit(1);
463 argv = &argv[optind];
468 if (argc < 2) usage(True);
469 /* Need to invert sense of return code -- samba
470 * routines mostly return True==1 for success, but
472 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
479 printf("smbcontrol> ");
480 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
482 while ((myargc < 4) &&
483 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
487 if (strequal(myargv[0],"q")) break;
490 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))