Merge of Herb's profiling code.
[Samba.git] / source / utils / smbcontrol.c
bloba273f82151c7715ee8a5c9a4f6cbd6e5efe02364
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 %d\n",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 level;
81 memcpy(&level, buf, sizeof(int));
83 printf("Current debug level of PID %d is %d\n",src,level);
84 got_level = True;
87 /****************************************************************************
88 Prints out the current Profile level returned by MSG_PROFILELEVEL
89 ****************************************************************************/
90 void profilelevel_function(int msg_type, pid_t src, void *buf, size_t len)
92 int level;
93 char *s;
94 memcpy(&level, buf, sizeof(int));
96 if (level) {
97 switch (level) {
98 case 1:
99 s = "off";
100 break;
101 case 3:
102 s = "count only";
103 break;
104 case 7:
105 s = "count and time";
106 break;
108 printf("Profiling %s on PID %d\n",s,src);
109 } else {
110 printf("Profiling not available on PID %d\n",src);
112 got_level = True;
115 /****************************************************************************
116 send a message to a named destination
117 ****************************************************************************/
118 static BOOL send_message(char *dest, int msg_type, void *buf, int len)
120 pid_t pid;
122 /* "smbd" is the only broadcast operation */
123 if (strequal(dest,"smbd")) {
124 return message_send_all(msg_type, buf, len);
125 } else if (strequal(dest,"nmbd")) {
126 pid = pidfile_pid(dest);
127 if (pid == 0) {
128 fprintf(stderr,"Can't find pid for nmbd\n");
129 return False;
131 } else {
132 pid = atoi(dest);
133 if (pid == 0) {
134 fprintf(stderr,"Not a valid pid\n");
135 return False;
139 return message_send_pid(pid, msg_type, buf, len);
142 /****************************************************************************
143 evaluate a message type string
144 ****************************************************************************/
145 static int parse_type(char *mtype)
147 int i;
148 for (i=0;msg_types[i].name;i++) {
149 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
151 return -1;
155 /****************************************************************************
156 do command
157 ****************************************************************************/
158 static BOOL do_command(char *dest, char *msg_name, char *params)
160 int i, n, v;
161 int mtype;
162 BOOL retval;
164 mtype = parse_type(msg_name);
165 if (mtype == -1) {
166 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
167 return(False);
170 switch (mtype) {
171 case MSG_DEBUG:
172 if (!params) {
173 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
174 return(False);
176 v = atoi(params);
177 send_message(dest, MSG_DEBUG, &v, sizeof(int));
178 break;
180 case MSG_PROFILE:
181 if (!params) {
182 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
183 return(False);
185 if (strequal(params, "off")) {
186 v = 0;
187 } else if (strequal(params, "count")) {
188 v = 1;
189 } else if (strequal(params, "on")) {
190 v = 2;
191 } else if (strequal(params, "flush")) {
192 v = 3;
193 } else {
194 fprintf(stderr,
195 "MSG_PROFILE parameter must be off, count, on, or flush\n");
196 return(False);
198 send_message(dest, MSG_PROFILE, &v, sizeof(int));
199 break;
201 case MSG_FORCE_ELECTION:
202 if (!strequal(dest, "nmbd")) {
203 fprintf(stderr,"force-election can only be sent to nmbd\n");
204 return(False);
206 send_message(dest, MSG_FORCE_ELECTION, NULL, 0);
207 break;
209 case MSG_REQ_PROFILELEVEL:
210 if (!profilelevel_registered) {
211 message_register(MSG_PROFILELEVEL, profilelevel_function);
212 profilelevel_registered = True;
214 got_level = False;
215 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0);
216 if (retval) {
217 timeout_start = time(NULL);
218 while (!got_level) {
219 message_dispatch();
220 if ((time(NULL) - timeout_start) > MAX_WAIT) {
221 fprintf(stderr,"profilelevel timeout\n");
222 break;
226 break;
228 case MSG_REQ_DEBUGLEVEL:
229 if (!debuglevel_registered) {
230 message_register(MSG_DEBUGLEVEL, debuglevel_function);
231 debuglevel_registered = True;
233 got_level = False;
234 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0);
235 if (retval) {
236 timeout_start = time(NULL);
237 while (!got_level) {
238 message_dispatch();
239 if ((time(NULL) - timeout_start) > MAX_WAIT) {
240 fprintf(stderr,"debuglevel timeout\n");
241 break;
245 break;
247 case MSG_PRINTER_NOTIFY:
248 if (!strequal(dest, "smbd")) {
249 fprintf(stderr,"printer-notify can only be sent to smbd\n");
250 return(False);
252 if (!params) {
253 fprintf(stderr, "printer-notify needs a printer name\n");
254 return (False);
256 retval = send_message(dest, MSG_PRINTER_NOTIFY, params,
257 strlen(params) + 1);
258 break;
260 case MSG_PING:
261 if (!pong_registered) {
262 message_register(MSG_PONG, pong_function);
263 pong_registered = True;
265 if (!params) {
266 fprintf(stderr,"MSG_PING needs a parameter\n");
267 return(False);
269 n = atoi(params);
270 pong_count = 0;
271 for (i=0;i<n;i++) {
272 retval = send_message(dest, MSG_PING, NULL, 0);
273 if (retval == False) break;
275 if (retval) {
276 timeout_start = time(NULL);
277 while (pong_count < n) {
278 message_dispatch();
279 if ((time(NULL) - timeout_start) > MAX_WAIT) {
280 fprintf(stderr,"PING timeout\n");
281 break;
285 break;
289 return (True);
292 int main(int argc, char *argv[])
294 int opt;
295 char temp[255];
296 extern int optind;
297 pstring servicesf = CONFIGFILE;
298 BOOL interactive = False;
300 TimeInit();
301 setup_logging(argv[0],True);
303 charset_initialise();
304 lp_load(servicesf,False,False,False);
306 if (!message_init()) exit(1);
308 if (argc < 2) usage(True);
310 while ((opt = getopt(argc, argv,"i")) != EOF) {
311 switch (opt) {
312 case 'i':
313 interactive = True;
314 break;
315 default:
316 printf("Unknown option %c (%d)\n", (char)opt, opt);
317 usage(True);
321 argc -= optind;
322 argv = &argv[optind];
324 if (!interactive) {
325 if (argc < 2) usage(True);
326 return (do_command(argv[0],argv[1],argc > 2 ? argv[2] : 0));
329 while (True) {
330 char *myargv[3];
331 int myargc;
333 printf("smbcontrol> ");
334 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
335 myargc = 0;
336 while ((myargc < 3) &&
337 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
338 myargc++;
340 if (!myargc) break;
341 if (strequal(myargv[0],"q")) break;
342 if (myargc < 2)
343 usage(False);
344 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? myargv[2] : 0))
345 usage(False);
347 return(0);