Fix crash in smbcontrol Gerald pointed out when no parameter given to debug.
[Samba/gbeck.git] / source / utils / smbcontrol.c
blob5bb4e602f005248e14d9b742412fb6eb2a6e97c3
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
22 #include "includes.h"
24 static struct {
25 char *name;
26 int value;
27 } msg_types[] = {
28 {"debug", MSG_DEBUG},
29 {"force-election", MSG_FORCE_ELECTION},
30 {"ping", MSG_PING},
31 {"profile", MSG_PROFILE},
32 {"profilelevel", MSG_REQ_PROFILELEVEL},
33 {"debuglevel", MSG_REQ_DEBUGLEVEL},
34 {"printer-notify", MSG_PRINTER_NOTIFY},
35 {NULL, -1}
38 time_t timeout_start;
40 #define MAX_WAIT 10
42 static void usage(BOOL doexit)
44 int i;
45 if (doexit) {
46 printf("Usage: smbcontrol -i\n");
47 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
48 } else {
49 printf("<destination> <message-type> <parameters>\n\n");
51 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
52 printf("\t<message-type> is one of: ");
53 for (i=0; msg_types[i].name; i++)
54 printf("%s%s", i?", ":"",msg_types[i].name);
55 printf("\n");
56 if (doexit) exit(1);
59 static int pong_count;
60 static BOOL got_level;
61 static BOOL pong_registered = False;
62 static BOOL debuglevel_registered = False;
63 static BOOL profilelevel_registered = False;
66 /****************************************************************************
67 a useful function for testing the message system
68 ****************************************************************************/
69 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
71 pong_count++;
72 printf("PONG from PID %u\n",(unsigned int)src);
75 /****************************************************************************
76 Prints out the current Debug level returned by MSG_DEBUGLEVEL
77 ****************************************************************************/
78 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
80 int i;
81 int debuglevel_class[DBGC_LAST];
83 memcpy(debuglevel_class, buf, len);
85 printf("Current debug level of PID %u is %d ",(unsigned int)src, debuglevel_class[0]);
86 for (i=1;i<DBGC_LAST;i++)
87 if (debuglevel_class[i])
88 printf("%s:%d ", debug_classname_from_index(i), debuglevel_class[i]);
89 printf("\n");
91 got_level = True;
94 /****************************************************************************
95 Prints out the current Profile level returned by MSG_PROFILELEVEL
96 ****************************************************************************/
97 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
99 int level;
100 char *s=NULL;
101 memcpy(&level, buf, sizeof(int));
103 if (level) {
104 switch (level) {
105 case 1:
106 s = "off";
107 break;
108 case 3:
109 s = "count only";
110 break;
111 case 7:
112 s = "count and time";
113 break;
115 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
116 } else {
117 printf("Profiling not available on PID %u\n",(unsigned int)src);
119 got_level = True;
122 /****************************************************************************
123 send a message to a named destination
124 ****************************************************************************/
125 static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates)
127 pid_t pid;
128 TDB_CONTEXT *the_tdb;
130 the_tdb = tdb_open(lock_path("connections.tdb"), 0, 0, O_RDONLY, 0);
131 if (!the_tdb) {
132 fprintf(stderr,"Failed to open connections database in send_message.\n");
133 return False;
136 /* "smbd" is the only broadcast operation */
137 if (strequal(dest,"smbd")) {
138 return message_send_all(the_tdb,msg_type, buf, len, duplicates);
139 } else if (strequal(dest,"nmbd")) {
140 pid = pidfile_pid(dest);
141 if (pid == 0) {
142 fprintf(stderr,"Can't find pid for nmbd\n");
143 return False;
145 } else if (strequal(dest,"self")) {
146 pid = getpid();
147 } else {
148 pid = atoi(dest);
149 if (pid == 0) {
150 fprintf(stderr,"Not a valid pid\n");
151 return False;
155 return message_send_pid(pid, msg_type, buf, len, duplicates);
158 /****************************************************************************
159 evaluate a message type string
160 ****************************************************************************/
161 static int parse_type(char *mtype)
163 int i;
164 for (i=0;msg_types[i].name;i++) {
165 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
167 return -1;
171 /****************************************************************************
172 do command
173 ****************************************************************************/
174 static BOOL do_command(char *dest, char *msg_name, char **params)
176 int i, n, v;
177 int mtype;
178 BOOL retval=False;
179 int debuglevel_class[DBGC_LAST];
181 mtype = parse_type(msg_name);
182 if (mtype == -1) {
183 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
184 return(False);
187 switch (mtype) {
188 case MSG_DEBUG:
189 if (!params || !params[0]) {
190 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
191 return(False);
194 ZERO_ARRAY(debuglevel_class);
195 if (!debug_parse_params(params, debuglevel_class)) {
196 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
197 return(False);
198 } else
199 send_message(dest, MSG_DEBUG, debuglevel_class, sizeof(debuglevel_class), False);
200 break;
202 case MSG_PROFILE:
203 if (!params[0]) {
204 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
205 return(False);
207 if (strequal(params[0], "off")) {
208 v = 0;
209 } else if (strequal(params[0], "count")) {
210 v = 1;
211 } else if (strequal(params[0], "on")) {
212 v = 2;
213 } else if (strequal(params[0], "flush")) {
214 v = 3;
215 } else {
216 fprintf(stderr,
217 "MSG_PROFILE parameter must be off, count, on, or flush\n");
218 return(False);
220 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
221 break;
223 case MSG_FORCE_ELECTION:
224 if (!strequal(dest, "nmbd")) {
225 fprintf(stderr,"force-election can only be sent to nmbd\n");
226 return(False);
228 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
229 break;
231 case MSG_REQ_PROFILELEVEL:
232 if (!profilelevel_registered) {
233 message_register(MSG_PROFILELEVEL, profilelevel_function);
234 profilelevel_registered = True;
236 got_level = False;
237 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
238 if (retval) {
239 timeout_start = time(NULL);
240 while (!got_level) {
241 message_dispatch();
242 if ((time(NULL) - timeout_start) > MAX_WAIT) {
243 fprintf(stderr,"profilelevel timeout\n");
244 break;
248 break;
250 case MSG_REQ_DEBUGLEVEL:
251 if (!debuglevel_registered) {
252 message_register(MSG_DEBUGLEVEL, debuglevel_function);
253 debuglevel_registered = True;
255 got_level = False;
256 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
257 if (retval) {
258 timeout_start = time(NULL);
259 while (!got_level) {
260 message_dispatch();
261 if ((time(NULL) - timeout_start) > MAX_WAIT) {
262 fprintf(stderr,"debuglevel timeout\n");
263 break;
267 break;
269 case MSG_PRINTER_NOTIFY:
270 if (!strequal(dest, "smbd")) {
271 fprintf(stderr,"printer-notify can only be sent to smbd\n");
272 return(False);
274 if (!params[0]) {
275 fprintf(stderr, "printer-notify needs a printer name\n");
276 return (False);
278 retval = send_message(dest, MSG_PRINTER_NOTIFY, params[0],
279 strlen(params[0]) + 1, False);
280 break;
282 case MSG_PING:
283 if (!pong_registered) {
284 message_register(MSG_PONG, pong_function);
285 pong_registered = True;
287 if (!params[0]) {
288 fprintf(stderr,"MSG_PING needs a parameter\n");
289 return(False);
291 n = atoi(params[0]);
292 pong_count = 0;
293 for (i=0;i<n;i++) {
294 retval = send_message(dest, MSG_PING, NULL, 0, True);
295 if (retval == False) break;
297 if (retval) {
298 timeout_start = time(NULL);
299 while (pong_count < n) {
300 message_dispatch();
301 if ((time(NULL) - timeout_start) > MAX_WAIT) {
302 fprintf(stderr,"PING timeout\n");
303 break;
307 break;
311 return (True);
314 int main(int argc, char *argv[])
316 int opt;
317 char temp[255];
318 extern int optind;
319 pstring servicesf = CONFIGFILE;
320 BOOL interactive = False;
322 TimeInit();
323 setup_logging(argv[0],True);
325 charset_initialise();
326 lp_load(servicesf,False,False,False);
328 if (!message_init()) exit(1);
330 if (argc < 2) usage(True);
332 while ((opt = getopt(argc, argv,"i")) != EOF) {
333 switch (opt) {
334 case 'i':
335 interactive = True;
336 break;
337 default:
338 printf("Unknown option %c (%d)\n", (char)opt, opt);
339 usage(True);
343 argc -= optind;
344 argv = &argv[optind];
346 if (!interactive) {
347 if (argc < 2) usage(True);
348 return (do_command(argv[0],argv[1],argc > 2 ? &argv[2] : 0));
351 while (True) {
352 char *myargv[3];
353 int myargc;
355 printf("smbcontrol> ");
356 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
357 myargc = 0;
358 while ((myargc < 3) &&
359 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
360 myargc++;
362 if (!myargc) break;
363 if (strequal(myargv[0],"q")) break;
364 if (myargc < 2)
365 usage(False);
366 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? &myargv[2] : 0))
367 usage(False);
369 return(0);