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
6 Copyright (C) Simo Sorce 2002
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 },
44 {"shutdown", MSG_SHUTDOWN },
52 static void usage(BOOL doexit)
56 printf("Usage: smbcontrol -i -s configfile\n");
57 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
59 printf("<destination> <message-type> <parameters>\n\n");
61 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
62 printf("\t<message-type> is one of: ");
63 for (i=0; msg_types[i].name; i++)
64 printf("%s%s", i?", ":"",msg_types[i].name);
69 static int pong_count;
70 static BOOL got_level;
71 static BOOL pong_registered = False;
72 static BOOL debuglevel_registered = False;
73 static BOOL profilelevel_registered = False;
77 * Wait for replies for up to @p *max_secs seconds, or until @p
78 * max_replies are received. max_replies may be NULL in which case it
81 * @note This is a pretty lame timeout; all it means is that after
82 * max_secs we won't look for any more messages.
84 static void wait_for_replies(int max_secs, int *max_replies)
86 time_t timeout_end = time(NULL) + max_secs;
88 while ((!max_replies || (*max_replies)-- > 0)
89 && (time(NULL) < timeout_end)) {
95 /****************************************************************************
96 a useful function for testing the message system
97 ****************************************************************************/
98 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
101 printf("PONG from PID %u\n",(unsigned int)src);
104 /****************************************************************************
105 Prints out the current Debug level returned by MSG_DEBUGLEVEL
106 ****************************************************************************/
107 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
110 char *levels = (char *)buf;
113 printf("Current debug levels of PID %u are:\n",(unsigned int)src);
115 while(next_token(&levels, dbgcl, " ", sizeof(pstring)))
116 printf("%s\n", dbgcl);
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);
249 if (!params || !params[0]) {
250 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
254 /* first pass retrieve total lenght */
255 for (p = params; p && *p ; p++)
256 dim += (strnlen(*p, 1024) +1); /* lenght + space */
257 b = buf = malloc(dim);
259 fprintf(stderr, "Out of memory!");
262 /* now build a single string with all parameters */
263 for(p = params; p && *p; p++) {
264 int l = strnlen(*p, 1024);
271 send_message(dest, MSG_DEBUG, buf, dim, False);
279 if (!params || !params[0]) {
280 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
283 if (strequal(params[0], "off")) {
285 } else if (strequal(params[0], "count")) {
287 } else if (strequal(params[0], "on")) {
289 } else if (strequal(params[0], "flush")) {
293 "MSG_PROFILE parameter must be off, count, on, or flush\n");
296 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
299 case MSG_FORCE_ELECTION:
300 if (!strequal(dest, "nmbd")) {
301 fprintf(stderr,"force-election can only be sent to nmbd\n");
304 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
307 case MSG_REQ_PROFILELEVEL:
308 if (!profilelevel_registered) {
309 message_register(MSG_PROFILELEVEL, profilelevel_function);
310 profilelevel_registered = True;
313 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
315 timeout_start = time(NULL);
318 if ((time(NULL) - timeout_start) > MAX_WAIT) {
319 fprintf(stderr,"profilelevel timeout\n");
326 case MSG_REQ_DEBUGLEVEL:
327 if (!debuglevel_registered) {
328 message_register(MSG_DEBUGLEVEL, debuglevel_function);
329 debuglevel_registered = True;
332 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
334 timeout_start = time(NULL);
337 if ((time(NULL) - timeout_start) > MAX_WAIT) {
338 fprintf(stderr,"debuglevel timeout\n");
345 case MSG_PRINTER_NOTIFY:
346 if (!strequal(dest, "smbd")) {
347 fprintf(stderr,"printer-notify can only be sent to smbd\n");
350 if (!params || !params[0]) {
351 fprintf(stderr, "printer-notify needs a printer name\n");
355 char msg[8 + sizeof(fstring)];
356 SIVAL(msg,0,PRINTER_CHANGE_ALL);
358 fstrcpy(&msg[8], params[0]);
360 retval = send_message(dest, MSG_PRINTER_NOTIFY, msg, 8 + strlen(params[0]) + 1, False);
364 case MSG_SMB_FORCE_TDIS:
365 if (!strequal(dest, "smbd")) {
366 fprintf(stderr,"close-share can only be sent to smbd\n");
369 if (!params || !params[0]) {
370 fprintf(stderr, "close-share needs a share name or '*'\n");
373 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
374 strlen(params[0]) + 1, False);
377 case MSG_SMB_SAM_SYNC:
378 if (!strequal(dest, "smbd")) {
379 fprintf(stderr, "samsync can only be sent to smbd\n");
384 fprintf(stderr, "samsync does not take any parameters\n");
388 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
392 case MSG_SMB_SAM_REPL: {
395 if (!strequal(dest, "smbd")) {
396 fprintf(stderr, "sam repl can only be sent to smbd\n");
400 if (!params || !params[0]) {
401 fprintf(stderr, "SAM_REPL needs a parameter\n");
405 seqnum = atoi(params[0]);
407 retval = send_message(dest, MSG_SMB_SAM_SYNC,
408 (char *)&seqnum, sizeof(uint32), False);
414 if (!pong_registered) {
415 message_register(MSG_PONG, pong_function);
416 pong_registered = True;
418 if (!params || !params[0]) {
419 fprintf(stderr,"MSG_PING needs a parameter\n");
426 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
428 retval = send_message(dest, MSG_PING, NULL, 0, True);
432 wait_for_replies(MAX_WAIT, &n);
434 fprintf(stderr,"PING timeout\n");
438 case MSG_REQ_POOL_USAGE:
439 if (!send_message(dest, MSG_REQ_POOL_USAGE, NULL, 0, True))
441 wait_for_replies(MAX_WAIT, NULL);
445 case MSG_REQ_DMALLOC_LOG_CHANGED:
446 case MSG_REQ_DMALLOC_MARK:
447 if (!send_message(dest, mtype, NULL, 0, False))
452 if (!send_message(dest, MSG_SHUTDOWN, NULL, 0, False))
460 int main(int argc, char *argv[])
465 BOOL interactive = False;
467 AllowDebugChange = False;
470 setup_logging(argv[0],True);
472 if (argc < 2) usage(True);
474 while ((opt = getopt(argc, argv,"is:")) != EOF) {
480 pstrcpy(dyn_CONFIGFILE, optarg);
483 printf("Unknown option %c (%d)\n", (char)opt, opt);
488 lp_load(dyn_CONFIGFILE,False,False,False);
490 if (!message_init()) exit(1);
493 argv = &argv[optind];
498 if (argc < 2) usage(True);
499 /* Need to invert sense of return code -- samba
500 * routines mostly return True==1 for success, but
502 return ! do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0);
509 printf("smbcontrol> ");
510 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
512 while ((myargc < 4) &&
513 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
517 if (strequal(myargv[0],"q")) break;
520 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))