2 Unix SMB/CIFS implementation.
3 program to send control messages to Samba processes
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) 2001, 2002 by Martin Pool
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern BOOL AllowDebugChange;
31 {"force-election", MSG_FORCE_ELECTION},
33 {"profile", MSG_PROFILE},
34 {"profilelevel", MSG_REQ_PROFILELEVEL},
35 {"debuglevel", MSG_REQ_DEBUGLEVEL},
36 {"printer-notify", MSG_PRINTER_NOTIFY},
37 {"close-share", MSG_SMB_FORCE_TDIS},
38 {"samsync", MSG_SMB_SAM_SYNC},
39 {"samrepl", MSG_SMB_SAM_REPL},
40 {"pool-usage", MSG_REQ_POOL_USAGE },
41 {"dmalloc-mark", MSG_REQ_DMALLOC_MARK },
42 {"dmalloc-log-changed", MSG_REQ_DMALLOC_LOG_CHANGED },
43 {"shutdown", MSG_SHUTDOWN },
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";
146 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
148 printf("Profiling not available on PID %u\n",(unsigned int)src);
154 * Handle reply from POOL_USAGE.
156 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
158 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
159 (unsigned int) src_pid, (int) len, (const char *) buf);
164 * Send a message to a named destination
166 * @return False if an error occurred.
168 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
171 /* "smbd" is the only broadcast operation */
172 if (strequal(dest,"smbd")) {
177 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
179 fprintf(stderr,"Failed to open connections database in send_message.\n");
183 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
185 DEBUG(10,("smbcontrol/send_message: broadcast message to "
186 "%d processes\n", n_sent));
190 } else if (strequal(dest,"nmbd")) {
191 pid = pidfile_pid(dest);
193 fprintf(stderr,"Can't find pid for nmbd\n");
196 } else if (strequal(dest,"self")) {
201 fprintf(stderr,"Not a valid pid\n");
206 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
207 return message_send_pid(pid, msg_type, buf, len, duplicates);
210 /****************************************************************************
211 evaluate a message type string
212 ****************************************************************************/
213 static int parse_type(char *mtype)
216 for (i=0;msg_types[i].name;i++) {
217 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
223 static void register_all(void)
225 message_register(MSG_POOL_USAGE, pool_usage_cb);
229 /****************************************************************************
231 ****************************************************************************/
232 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
238 mtype = parse_type(msg_name);
240 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
246 struct debuglevel_message dm;
248 if (!params || !params[0]) {
249 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
254 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
255 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
258 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
263 if (!params || !params[0]) {
264 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
267 if (strequal(params[0], "off")) {
269 } else if (strequal(params[0], "count")) {
271 } else if (strequal(params[0], "on")) {
273 } else if (strequal(params[0], "flush")) {
277 "MSG_PROFILE parameter must be off, count, on, or flush\n");
280 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
283 case MSG_FORCE_ELECTION:
284 if (!strequal(dest, "nmbd")) {
285 fprintf(stderr,"force-election can only be sent to nmbd\n");
288 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
291 case MSG_REQ_PROFILELEVEL:
292 if (!profilelevel_registered) {
293 message_register(MSG_PROFILELEVEL, profilelevel_function);
294 profilelevel_registered = True;
297 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
299 timeout_start = time(NULL);
302 if ((time(NULL) - timeout_start) > MAX_WAIT) {
303 fprintf(stderr,"profilelevel timeout\n");
310 case MSG_REQ_DEBUGLEVEL:
311 if (!debuglevel_registered) {
312 message_register(MSG_DEBUGLEVEL, debuglevel_function);
313 debuglevel_registered = True;
316 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
318 timeout_start = time(NULL);
321 if ((time(NULL) - timeout_start) > MAX_WAIT) {
322 fprintf(stderr,"debuglevel timeout\n");
329 case MSG_PRINTER_NOTIFY:
330 if (!strequal(dest, "smbd")) {
331 fprintf(stderr,"printer-notify can only be sent to smbd\n");
334 if (!params || !params[0]) {
335 fprintf(stderr, "printer-notify needs a printer name\n");
339 char msg[8 + sizeof(fstring)];
340 SIVAL(msg,0,PRINTER_CHANGE_ALL);
342 fstrcpy(&msg[8], params[0]);
344 retval = send_message(dest, MSG_PRINTER_NOTIFY, msg, 8 + strlen(params[0]) + 1, False);
348 case MSG_SMB_FORCE_TDIS:
349 if (!strequal(dest, "smbd")) {
350 fprintf(stderr,"close-share can only be sent to smbd\n");
353 if (!params || !params[0]) {
354 fprintf(stderr, "close-share needs a share name or '*'\n");
357 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
358 strlen(params[0]) + 1, False);
361 case MSG_SMB_SAM_SYNC:
362 if (!strequal(dest, "smbd")) {
363 fprintf(stderr, "samsync can only be sent to smbd\n");
368 fprintf(stderr, "samsync does not take any parameters\n");
372 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
376 case MSG_SMB_SAM_REPL: {
379 if (!strequal(dest, "smbd")) {
380 fprintf(stderr, "sam repl can only be sent to smbd\n");
384 if (!params || !params[0]) {
385 fprintf(stderr, "SAM_REPL needs a parameter\n");
389 seqnum = atoi(params[0]);
391 retval = send_message(dest, MSG_SMB_SAM_SYNC,
392 (char *)&seqnum, sizeof(uint32), False);
398 if (!pong_registered) {
399 message_register(MSG_PONG, pong_function);
400 pong_registered = True;
402 if (!params || !params[0]) {
403 fprintf(stderr,"MSG_PING needs a parameter\n");
410 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
412 retval = send_message(dest, MSG_PING, NULL, 0, True);
416 wait_for_replies(MAX_WAIT, &n);
418 fprintf(stderr,"PING timeout\n");
422 case MSG_REQ_POOL_USAGE:
423 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
425 wait_for_replies(MAX_WAIT, NULL);
429 case MSG_REQ_DMALLOC_LOG_CHANGED:
430 case MSG_REQ_DMALLOC_MARK:
431 if (!send_message(dest, mtype, NULL, 0, False))
436 if (!send_message(dest, MSG_SHUTDOWN, NULL, 0, False))
444 int main(int argc, char *argv[])
449 BOOL interactive = False;
451 AllowDebugChange = False;
454 setup_logging(argv[0],True);
456 if (argc < 2) usage(True);
458 while ((opt = getopt(argc, argv,"is:")) != EOF) {
464 pstrcpy(dyn_CONFIGFILE, optarg);
467 printf("Unknown option %c (%d)\n", (char)opt, opt);
472 lp_load(dyn_CONFIGFILE,False,False,False);
474 if (!message_init()) exit(1);
477 argv = &argv[optind];
482 if (argc < 2) usage(True);
483 /* Need to invert sense of return code -- samba
484 * routines mostly return True==1 for success, but
486 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
493 printf("smbcontrol> ");
494 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
496 while ((myargc < 4) &&
497 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
501 if (strequal(myargv[0],"q")) break;
504 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))