2 Unix SMB/Netbios implementation.
4 program to send control messages to Samba processes
5 Copyright (C) Andrew Tridgell 1994-1998
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.
29 {"force-election", MSG_FORCE_ELECTION},
31 {"profile", MSG_PROFILE},
32 {"profilelevel", MSG_REQ_PROFILELEVEL},
33 {"debuglevel", MSG_REQ_DEBUGLEVEL},
34 {"printer-notify", MSG_PRINTER_NOTIFY},
35 {"close-share", MSG_SMB_FORCE_TDIS},
36 {"samsync", MSG_SMB_SAM_SYNC},
37 {"samrepl", MSG_SMB_SAM_REPL},
45 static void usage(BOOL doexit)
49 printf("Usage: smbcontrol -i -s configfile\n");
50 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
52 printf("<destination> <message-type> <parameters>\n\n");
54 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
55 printf("\t<message-type> is one of: ");
56 for (i=0; msg_types[i].name; i++)
57 printf("%s%s", i?", ":"",msg_types[i].name);
62 static int pong_count;
63 static BOOL got_level;
64 static BOOL pong_registered = False;
65 static BOOL debuglevel_registered = False;
66 static BOOL profilelevel_registered = False;
69 /****************************************************************************
70 a useful function for testing the message system
71 ****************************************************************************/
72 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
75 printf("PONG from PID %u\n",(unsigned int)src);
78 /****************************************************************************
79 Prints out the current Debug level returned by MSG_DEBUGLEVEL
80 ****************************************************************************/
81 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
84 int debuglevel_class[DBGC_LAST];
86 memcpy(debuglevel_class, buf, len);
88 printf("Current debug level of PID %u is %d ",(unsigned int)src, debuglevel_class[0]);
89 for (i=1;i<DBGC_LAST;i++)
90 if (debuglevel_class[i])
91 printf("%s:%d ", debug_classname_from_index(i), debuglevel_class[i]);
97 /****************************************************************************
98 Prints out the current Profile level returned by MSG_PROFILELEVEL
99 ****************************************************************************/
100 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
104 memcpy(&level, buf, sizeof(int));
115 s = "count and time";
118 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
120 printf("Profiling not available on PID %u\n",(unsigned int)src);
125 /****************************************************************************
126 send a message to a named destination
127 ****************************************************************************/
128 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
131 /* "smbd" is the only broadcast operation */
132 if (strequal(dest,"smbd")) {
136 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
138 fprintf(stderr,"Failed to open connections database in send_message.\n");
142 ret = message_send_all(tdb,msg_type, buf, len, duplicates);
146 } else if (strequal(dest,"nmbd")) {
147 pid = pidfile_pid(dest);
149 fprintf(stderr,"Can't find pid for nmbd\n");
152 } else if (strequal(dest,"self")) {
157 fprintf(stderr,"Not a valid pid\n");
162 return message_send_pid(pid, msg_type, buf, len, duplicates);
165 /****************************************************************************
166 evaluate a message type string
167 ****************************************************************************/
168 static int parse_type(char *mtype)
171 for (i=0;msg_types[i].name;i++) {
172 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
178 /****************************************************************************
180 ****************************************************************************/
181 static BOOL do_command(char *dest, char *msg_name, char **params)
187 mtype = parse_type(msg_name);
189 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
195 struct debuglevel_message dm;
197 if (!params || !params[0]) {
198 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
203 if (!debug_parse_params(params, dm.debuglevel_class, dm.debuglevel_class_isset)) {
204 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
207 send_message(dest, MSG_DEBUG, &dm, sizeof(dm), False);
212 if (!params || !params[0]) {
213 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
216 if (strequal(params[0], "off")) {
218 } else if (strequal(params[0], "count")) {
220 } else if (strequal(params[0], "on")) {
222 } else if (strequal(params[0], "flush")) {
226 "MSG_PROFILE parameter must be off, count, on, or flush\n");
229 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
232 case MSG_FORCE_ELECTION:
233 if (!strequal(dest, "nmbd")) {
234 fprintf(stderr,"force-election can only be sent to nmbd\n");
237 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
240 case MSG_REQ_PROFILELEVEL:
241 if (!profilelevel_registered) {
242 message_register(MSG_PROFILELEVEL, profilelevel_function);
243 profilelevel_registered = True;
246 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
248 timeout_start = time(NULL);
251 if ((time(NULL) - timeout_start) > MAX_WAIT) {
252 fprintf(stderr,"profilelevel timeout\n");
259 case MSG_REQ_DEBUGLEVEL:
260 if (!debuglevel_registered) {
261 message_register(MSG_DEBUGLEVEL, debuglevel_function);
262 debuglevel_registered = True;
265 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
267 timeout_start = time(NULL);
270 if ((time(NULL) - timeout_start) > MAX_WAIT) {
271 fprintf(stderr,"debuglevel timeout\n");
278 case MSG_PRINTER_NOTIFY:
279 if (!strequal(dest, "smbd")) {
280 fprintf(stderr,"printer-notify can only be sent to smbd\n");
283 if (!params || !params[0]) {
284 fprintf(stderr, "printer-notify needs a printer name\n");
287 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
288 strlen(params[0]) + 1, False);
291 case MSG_SMB_FORCE_TDIS:
292 if (!strequal(dest, "smbd")) {
293 fprintf(stderr,"close-share can only be sent to smbd\n");
296 if (!params || !params[0]) {
297 fprintf(stderr, "close-share needs a share name or '*'\n");
300 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
301 strlen(params[0]) + 1, False);
304 case MSG_SMB_SAM_SYNC:
305 if (!strequal(dest, "smbd")) {
306 fprintf(stderr, "samsync can only be sent to smbd\n");
311 fprintf(stderr, "samsync does not take any parameters\n");
315 retval = send_message(dest, MSG_SMB_SAM_SYNC, NULL, 0, False);
319 case MSG_SMB_SAM_REPL: {
322 if (!strequal(dest, "smbd")) {
323 fprintf(stderr, "sam repl can only be sent to smbd\n");
327 if (!params || !params[0]) {
328 fprintf(stderr, "SAM_REPL needs a parameter\n");
332 seqnum = atoi(params[0]);
334 retval = send_message(dest, MSG_SMB_SAM_SYNC,
335 (char *)&seqnum, sizeof(uint32), False);
341 if (!pong_registered) {
342 message_register(MSG_PONG, pong_function);
343 pong_registered = True;
345 if (!params || !params[0]) {
346 fprintf(stderr,"MSG_PING needs a parameter\n");
352 retval = send_message(dest, MSG_PING, NULL, 0, True);
353 if (retval == False) break;
356 timeout_start = time(NULL);
357 while (pong_count < n) {
359 if ((time(NULL) - timeout_start) > MAX_WAIT) {
360 fprintf(stderr,"PING timeout\n");
372 int main(int argc, char *argv[])
377 BOOL interactive = False;
380 setup_logging(argv[0],True);
382 if (argc < 2) usage(True);
384 while ((opt = getopt(argc, argv,"is:")) != EOF) {
390 pstrcpy(dyn_CONFIGFILE, optarg);
393 printf("Unknown option %c (%d)\n", (char)opt, opt);
398 lp_load(dyn_CONFIGFILE,False,False,False);
400 if (!message_init()) exit(1);
403 argv = &argv[optind];
406 if (argc < 2) usage(True);
407 return (do_command(argv[0],argv[1],argc > 2 ? &argv[2] : 0));
414 printf("smbcontrol> ");
415 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
417 while ((myargc < 3) &&
418 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
422 if (strequal(myargv[0],"q")) break;
425 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? &myargv[2] : 0))