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 },
50 static void usage(BOOL doexit)
54 printf("Usage: smbcontrol -i -s configfile\n");
55 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
57 printf("<destination> <message-type> <parameters>\n\n");
59 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
60 printf("\t<message-type> is one of: ");
61 for (i=0; msg_types[i].name; i++)
62 printf("%s%s", i?", ":"",msg_types[i].name);
67 static int pong_count;
68 static BOOL got_level;
69 static BOOL pong_registered = False;
70 static BOOL debuglevel_registered = False;
71 static BOOL profilelevel_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";
145 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
147 printf("Profiling not available on PID %u\n",(unsigned int)src);
153 * Handle reply from POOL_USAGE.
155 static void pool_usage_cb(int msg_type, pid_t src_pid, void *buf, size_t len)
157 printf("Got POOL_USAGE reply from pid%u:\n%.*s",
158 (unsigned int) src_pid, (int) len, (const char *) buf);
163 * Send a message to a named destination
165 * @return False if an error occurred.
167 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
170 /* "smbd" is the only broadcast operation */
171 if (strequal(dest,"smbd")) {
176 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
178 fprintf(stderr,"Failed to open connections database in send_message.\n");
182 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
184 DEBUG(10,("smbcontrol/send_message: broadcast message to "
185 "%d processes\n", n_sent));
189 } else if (strequal(dest,"nmbd")) {
190 pid = pidfile_pid(dest);
192 fprintf(stderr,"Can't find pid for nmbd\n");
195 } else if (strequal(dest,"self")) {
200 fprintf(stderr,"Not a valid pid\n");
205 DEBUG(10,("smbcontrol/send_message: send message to pid%d\n", pid));
206 return message_send_pid(pid, msg_type, buf, len, duplicates);
209 /****************************************************************************
210 evaluate a message type string
211 ****************************************************************************/
212 static int parse_type(char *mtype)
215 for (i=0;msg_types[i].name;i++) {
216 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
222 static void register_all(void)
224 message_register(MSG_POOL_USAGE, pool_usage_cb);
228 /****************************************************************************
230 ****************************************************************************/
231 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
237 mtype = parse_type(msg_name);
239 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
245 struct debuglevel_message dm;
247 if (!params || !params[0]) {
248 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
253 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
254 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
257 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
262 if (!params || !params[0]) {
263 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
266 if (strequal(params[0], "off")) {
268 } else if (strequal(params[0], "count")) {
270 } else if (strequal(params[0], "on")) {
272 } else if (strequal(params[0], "flush")) {
276 "MSG_PROFILE parameter must be off, count, on, or flush\n");
279 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
282 case MSG_FORCE_ELECTION:
283 if (!strequal(dest, "nmbd")) {
284 fprintf(stderr,"force-election can only be sent to nmbd\n");
287 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
290 case MSG_REQ_PROFILELEVEL:
291 if (!profilelevel_registered) {
292 message_register(MSG_PROFILELEVEL, profilelevel_function);
293 profilelevel_registered = True;
296 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
298 timeout_start = time(NULL);
301 if ((time(NULL) - timeout_start) > MAX_WAIT) {
302 fprintf(stderr,"profilelevel timeout\n");
309 case MSG_REQ_DEBUGLEVEL:
310 if (!debuglevel_registered) {
311 message_register(MSG_DEBUGLEVEL, debuglevel_function);
312 debuglevel_registered = True;
315 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
317 timeout_start = time(NULL);
320 if ((time(NULL) - timeout_start) > MAX_WAIT) {
321 fprintf(stderr,"debuglevel timeout\n");
328 case MSG_PRINTER_NOTIFY:
329 if (!strequal(dest, "smbd")) {
330 fprintf(stderr,"printer-notify can only be sent to smbd\n");
333 if (!params || !params[0]) {
334 fprintf(stderr, "printer-notify needs a printer name\n");
337 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
338 strlen(params[0]) + 1, False);
341 case MSG_SMB_FORCE_TDIS:
342 if (!strequal(dest, "smbd")) {
343 fprintf(stderr,"close-share can only be sent to smbd\n");
346 if (!params || !params[0]) {
347 fprintf(stderr, "close-share needs a share name or '*'\n");
350 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
351 strlen(params[0]) + 1, False);
354 case MSG_SMB_SAM_SYNC:
355 if (!strequal(dest, "smbd")) {
356 fprintf(stderr, "samsync can only be sent to smbd\n");
361 fprintf(stderr, "samsync does not take any parameters\n");
365 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
369 case MSG_SMB_SAM_REPL: {
372 if (!strequal(dest, "smbd")) {
373 fprintf(stderr, "sam repl can only be sent to smbd\n");
377 if (!params || !params[0]) {
378 fprintf(stderr, "SAM_REPL needs a parameter\n");
382 seqnum = atoi(params[0]);
384 retval = send_message(dest, MSG_SMB_SAM_SYNC,
385 (char *)&seqnum, sizeof(uint32), False);
391 if (!pong_registered) {
392 message_register(MSG_PONG, pong_function);
393 pong_registered = True;
395 if (!params || !params[0]) {
396 fprintf(stderr,"MSG_PING needs a parameter\n");
403 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
405 retval = send_message(dest, MSG_PING, NULL, 0, True);
409 wait_for_replies(MAX_WAIT, &n);
411 fprintf(stderr,"PING timeout\n");
415 case MSG_REQ_POOL_USAGE:
416 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
418 wait_for_replies(MAX_WAIT, NULL);
422 case MSG_REQ_DMALLOC_LOG_CHANGED:
423 case MSG_REQ_DMALLOC_MARK:
424 if (!send_message(dest, mtype, NULL, 0, False))
432 int main(int argc, char *argv[])
437 BOOL interactive = False;
439 AllowDebugChange = False;
442 setup_logging(argv[0],True);
444 if (argc < 2) usage(True);
446 while ((opt = getopt(argc, argv,"is:")) != EOF) {
452 pstrcpy(dyn_CONFIGFILE, optarg);
455 printf("Unknown option %c (%d)\n", (char)opt, opt);
460 lp_load(dyn_CONFIGFILE,False,False,False);
462 if (!message_init()) exit(1);
465 argv = &argv[optind];
470 if (argc < 2) usage(True);
471 /* Need to invert sense of return code -- samba
472 * routines mostly return True==1 for success, but
474 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
481 printf("smbcontrol> ");
482 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
484 while ((myargc < 4) &&
485 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
489 if (strequal(myargv[0],"q")) break;
492 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))