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 {"debuglevel", MSG_REQ_DEBUGLEVEL},
33 {"printer-notify", MSG_PRINTER_NOTIFY},
41 static void usage(BOOL doexit)
45 printf("Usage: smbcontrol -i\n");
46 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
48 printf("<destination> <message-type> <parameters>\n\n");
50 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
51 printf("\t<message-type> is one of: ");
52 for (i=0; msg_types[i].name; i++)
53 printf("%s%s", i?", ":"",msg_types[i].name);
58 static int pong_count;
59 static BOOL got_level;
60 static BOOL pong_registered = False;
61 static BOOL debuglevel_registered = False;
64 /****************************************************************************
65 a useful function for testing the message system
66 ****************************************************************************/
67 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
70 printf("PONG from PID %d\n",src);
73 /****************************************************************************
74 Prints out the current Debug level returned by MSG_DEBUGLEVEL
75 ****************************************************************************/
76 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
79 memcpy(&level, buf, sizeof(int));
81 printf("Current debug level of PID %d is %d\n",src,level);
85 /****************************************************************************
86 send a message to a named destination
87 ****************************************************************************/
88 static BOOL send_message(char *dest, int msg_type, void *buf, int len)
92 /* "smbd" is the only broadcast operation */
93 if (strequal(dest,"smbd")) {
94 return message_send_all(msg_type, buf, len);
95 } else if (strequal(dest,"nmbd")) {
96 pid = pidfile_pid(dest);
98 fprintf(stderr,"Can't find pid for nmbd\n");
104 fprintf(stderr,"Not a valid pid\n");
109 return message_send_pid(pid, msg_type, buf, len);
112 /****************************************************************************
113 evaluate a message type string
114 ****************************************************************************/
115 static int parse_type(char *mtype)
118 for (i=0;msg_types[i].name;i++) {
119 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
125 /****************************************************************************
127 ****************************************************************************/
128 static BOOL do_command(char *dest, char *msg_name, char *params)
134 mtype = parse_type(msg_name);
136 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
143 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
147 send_message(dest, MSG_DEBUG, &v, sizeof(int));
152 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
155 if (strequal(params, "off")) {
157 } else if (strequal(params, "count")) {
159 } else if (strequal(params, "on")) {
161 } else if (strequal(params, "flush")) {
165 "MSG_PROFILE parameter must be off, count, on, or flush\n");
168 send_message(dest, MSG_PROFILE, &v, sizeof(int));
171 case MSG_FORCE_ELECTION:
172 if (!strequal(dest, "nmbd")) {
173 fprintf(stderr,"force-election can only be sent to nmbd\n");
176 send_message(dest, MSG_FORCE_ELECTION, NULL, 0);
179 case MSG_REQ_DEBUGLEVEL:
180 if (!debuglevel_registered) {
181 message_register(MSG_DEBUGLEVEL, debuglevel_function);
182 debuglevel_registered = True;
185 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0);
187 timeout_start = time(NULL);
190 if ((time(NULL) - timeout_start) > MAX_WAIT) {
191 fprintf(stderr,"debuglevel timeout\n");
198 case MSG_PRINTER_NOTIFY:
199 if (!strequal(dest, "smbd")) {
200 fprintf(stderr,"printer-notify can only be sent to smbd\n");
204 fprintf(stderr, "printer-notify needs a printer name\n");
207 retval = send_message(dest, MSG_PRINTER_NOTIFY, params,
212 if (!pong_registered) {
213 message_register(MSG_PONG, pong_function);
214 pong_registered = True;
217 fprintf(stderr,"MSG_PING needs a parameter\n");
223 retval = send_message(dest, MSG_PING, NULL, 0);
224 if (retval == False) break;
227 timeout_start = time(NULL);
228 while (pong_count < n) {
230 if ((time(NULL) - timeout_start) > MAX_WAIT) {
231 fprintf(stderr,"PING timeout\n");
243 int main(int argc, char *argv[])
248 pstring servicesf = CONFIGFILE;
249 BOOL interactive = False;
252 setup_logging(argv[0],True);
254 charset_initialise();
255 lp_load(servicesf,False,False,False);
257 if (!message_init()) exit(1);
259 if (argc < 2) usage(True);
261 while ((opt = getopt(argc, argv,"i")) != EOF) {
267 printf("Unknown option %c (%d)\n", (char)opt, opt);
273 argv = &argv[optind];
276 if (argc < 2) usage(True);
277 return (do_command(argv[0],argv[1],argc > 2 ? argv[2] : 0));
284 printf("smbcontrol> ");
285 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
287 while ((myargc < 3) &&
288 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
292 if (strequal(myargv[0],"q")) break;
295 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? myargv[2] : 0))