add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / utils / smbcontrol.c
blob7bb5c3e2c8b37adfa32eb5043f98f55a424b61c7
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 extern BOOL AllowDebugChange;
26 static struct {
27 char *name;
28 int value;
29 } msg_types[] = {
30 {"debug", MSG_DEBUG},
31 {"force-election", MSG_FORCE_ELECTION},
32 {"ping", MSG_PING},
33 {"profile", MSG_PROFILE},
34 {"profilelevel", MSG_REQ_PROFILELEVEL},
35 {"debuglevel", MSG_REQ_DEBUGLEVEL},
36 {"printer-notify", MSG_PRINTER_NOTIFY},
37 {"close-share", MSG_SMB_FORCE_TDIS},
38 {NULL, -1}
41 time_t timeout_start;
43 #define MAX_WAIT 10
45 static void usage(BOOL doexit)
47 int i;
48 if (doexit) {
49 printf("Usage: smbcontrol [-d debuglevel] [-s configfile] [-i]\n");
50 printf(" smbcontrol [-d debuglevel] [-s configfile] <destination> <message-type> <parameters>\n\n");
51 } else {
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);
58 printf("\n");
59 if (doexit) exit(1);
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)
74 pong_count++;
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)
83 int i;
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]);
92 printf("\n");
94 got_level = True;
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)
102 int level;
103 char *s=NULL;
104 memcpy(&level, buf, sizeof(int));
106 if (level) {
107 switch (level) {
108 case 1:
109 s = "off";
110 break;
111 case 3:
112 s = "count only";
113 break;
114 case 7:
115 s = "count and time";
116 break;
118 printf("Profiling %s on PID %u\n",s,(unsigned int)src);
119 } else {
120 printf("Profiling not available on PID %u\n",(unsigned int)src);
122 got_level = True;
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)
130 BOOL retval = False;
131 pid_t pid = 0;
132 TDB_CONTEXT *the_tdb;
134 the_tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0);
135 if (!the_tdb) {
136 fprintf(stderr,"Failed to open connections database in send_message.\n");
137 return False;
140 /* "smbd" is the only broadcast operation */
141 if (strequal(dest,"smbd")) {
142 retval = message_send_all(the_tdb,msg_type, buf, len, duplicates, NULL);
143 } else if (strequal(dest,"nmbd")) {
144 pid = pidfile_pid(dest);
145 if (pid == 0) {
146 fprintf(stderr,"Can't find pid for nmbd\n");
148 } else if (strequal(dest,"self")) {
149 pid = getpid();
150 } else {
151 pid = atoi(dest);
152 if (pid == 0) {
153 fprintf(stderr,"Not a valid pid\n");
157 tdb_close(the_tdb);
158 if (pid)
159 return message_send_pid(pid, msg_type, buf, len, duplicates);
160 else
161 return retval;
164 /****************************************************************************
165 evaluate a message type string
166 ****************************************************************************/
167 static int parse_type(char *mtype)
169 int i;
170 for (i=0;msg_types[i].name;i++) {
171 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
173 return -1;
177 /****************************************************************************
178 do command
179 ****************************************************************************/
180 static BOOL do_command(char *dest, char *msg_name, int iparams, char **params)
182 int i, n, v;
183 int mtype;
184 BOOL retval=False;
185 int debuglevel_class[DBGC_LAST];
187 mtype = parse_type(msg_name);
188 if (mtype == -1) {
189 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
190 return(False);
193 switch (mtype) {
194 case MSG_DEBUG:
195 if (!params || !params[0]) {
196 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
197 return(False);
200 ZERO_ARRAY(debuglevel_class);
201 if (!debug_parse_params(params, debuglevel_class)) {
202 fprintf(stderr, "MSG_DEBUG error. Expected <class name>:level\n");
203 return(False);
204 } else
205 send_message(dest, MSG_DEBUG, debuglevel_class, sizeof(debuglevel_class), False);
206 break;
208 case MSG_PROFILE:
209 if (!params || !params[0]) {
210 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
211 return(False);
213 if (strequal(params[0], "off")) {
214 v = 0;
215 } else if (strequal(params[0], "count")) {
216 v = 1;
217 } else if (strequal(params[0], "on")) {
218 v = 2;
219 } else if (strequal(params[0], "flush")) {
220 v = 3;
221 } else {
222 fprintf(stderr,
223 "MSG_PROFILE parameter must be off, count, on, or flush\n");
224 return(False);
226 send_message(dest, MSG_PROFILE, &v, sizeof(int), False);
227 break;
229 case MSG_FORCE_ELECTION:
230 if (!strequal(dest, "nmbd")) {
231 fprintf(stderr,"force-election can only be sent to nmbd\n");
232 return(False);
234 send_message(dest, MSG_FORCE_ELECTION, NULL, 0, False);
235 break;
237 case MSG_REQ_PROFILELEVEL:
238 if (!profilelevel_registered) {
239 message_register(MSG_PROFILELEVEL, profilelevel_function);
240 profilelevel_registered = True;
242 got_level = False;
243 retval = send_message(dest, MSG_REQ_PROFILELEVEL, NULL, 0, True);
244 if (retval) {
245 timeout_start = time(NULL);
246 while (!got_level) {
247 message_dispatch();
248 if ((time(NULL) - timeout_start) > MAX_WAIT) {
249 fprintf(stderr,"profilelevel timeout\n");
250 break;
254 break;
256 case MSG_REQ_DEBUGLEVEL:
257 if (!debuglevel_registered) {
258 message_register(MSG_DEBUGLEVEL, debuglevel_function);
259 debuglevel_registered = True;
261 got_level = False;
262 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0, True);
263 if (retval) {
264 timeout_start = time(NULL);
265 while (!got_level) {
266 message_dispatch();
267 if ((time(NULL) - timeout_start) > MAX_WAIT) {
268 fprintf(stderr,"debuglevel timeout\n");
269 break;
273 break;
275 case MSG_PRINTER_NOTIFY:
276 if (!strequal(dest, "smbd")) {
277 fprintf(stderr,"printer-notify can only be sent to smbd\n");
278 return(False);
280 if (!params || !params[0]) {
281 fprintf(stderr, "printer-notify needs a printer name\n");
282 return (False);
285 char msg[8 + sizeof(fstring)+4];
286 SIVAL(msg,0,PRINTER_CHANGE_ALL);
287 SIVAL(msg,4,0);
288 fstrcpy(&msg[8], params[0]);
289 SIVAL(msg,8+strlen(params[0])+1, PRINTER_MESSAGE_DRIVER);
291 retval = send_message(dest, MSG_PRINTER_NOTIFY, msg, 8 + strlen(params[0]) + 1 + 4, False);
293 break;
295 case MSG_SMB_FORCE_TDIS:
296 if (!strequal(dest, "smbd")) {
297 fprintf(stderr,"close-share can only be sent to smbd\n");
298 return(False);
300 if (!params || !params[0]) {
301 fprintf(stderr, "close-share needs a share name or '*'\n");
302 return (False);
304 retval = send_message(dest, MSG_SMB_FORCE_TDIS, params[0],
305 strlen(params[0]) + 1, False);
306 break;
308 case MSG_PING:
309 if (!pong_registered) {
310 message_register(MSG_PONG, pong_function);
311 pong_registered = True;
313 if (!params || !params[0]) {
314 fprintf(stderr,"MSG_PING needs a parameter\n");
315 return(False);
317 n = atoi(params[0]);
318 pong_count = 0;
319 for (i=0;i<n;i++) {
320 if (iparams > 1)
321 retval = send_message(dest, MSG_PING, params[1], strlen(params[1]) + 1, True);
322 else
323 retval = send_message(dest, MSG_PING, NULL, 0, True);
324 if (retval == False) break;
326 if (retval) {
327 timeout_start = time(NULL);
328 while (pong_count < n) {
329 message_dispatch();
330 if ((time(NULL) - timeout_start) > MAX_WAIT) {
331 fprintf(stderr,"PING timeout\n");
332 break;
336 break;
340 return (True);
343 int main(int argc, char *argv[])
345 int opt;
346 char temp[255];
347 extern char *optarg;
348 extern int optind;
349 pstring servicesf = CONFIGFILE;
350 BOOL interactive = False;
352 TimeInit();
353 setup_logging(argv[0],True);
355 AllowDebugChange = False;
356 DEBUGLEVEL = 0;
357 charset_initialise();
359 if (argc < 2) usage(True);
361 while ((opt = getopt(argc, argv,"is:d:")) != EOF) {
362 switch (opt) {
363 case 'd':
364 DEBUGLEVEL = atoi(optarg);
365 break;
366 case 'i':
367 interactive = True;
368 break;
369 case 's':
370 pstrcpy(servicesf, optarg);
371 break;
372 default:
373 printf("Unknown option %c (%d)\n", (char)opt, opt);
374 usage(True);
378 lp_load(servicesf,False,False,False);
380 if (!message_init()) exit(1);
382 argc -= optind;
383 argv = &argv[optind];
385 if (!interactive) {
386 if (argc < 2) usage(True);
388 * We want to return !do_command so we get the
389 * right shell semantics (0 = success, 1 = fail)
391 return (!do_command(argv[0],argv[1], argc-2, argc > 2 ? &argv[2] : 0));
394 while (True) {
395 char *myargv[4];
396 int myargc;
398 printf("smbcontrol> ");
399 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
400 myargc = 0;
401 while ((myargc < 4) &&
402 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
403 myargc++;
405 if (!myargc) break;
406 if (strequal(myargv[0],"q")) break;
407 if (myargc < 2)
408 usage(False);
409 else if (!do_command(myargv[0],myargv[1],myargc-2,myargc > 2 ? &myargv[2] : 0))
410 usage(False);
412 return(0);