lib/messages.c add debug print for receipt of PING and
[Samba/gbeck.git] / source3 / utils / smbcontrol.c
blob57f6149bcdd8df753a7ad60259813d34de02ac3a
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 #define NO_SYSLOG
24 #include "includes.h"
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 {"debuglevel", MSG_REQ_DEBUGLEVEL},
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;
65 /****************************************************************************
66 a useful function for testing the message system
67 ****************************************************************************/
68 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
70 pong_count++;
71 printf("PONG from PID %d\n",src);
74 /****************************************************************************
75 Prints out the current Debug level returned by MSG_DEBUGLEVEL
76 ****************************************************************************/
77 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
79 int level;
80 memcpy(&level, buf, sizeof(int));
82 printf("Current debug level of PID %d is %d\n",src,level);
83 got_level = True;
86 /****************************************************************************
87 send a message to a named destination
88 ****************************************************************************/
89 static BOOL send_message(char *dest, int msg_type, void *buf, int len)
91 pid_t pid;
93 /* "smbd" is the only broadcast operation */
94 if (strequal(dest,"smbd")) {
95 return message_send_all(msg_type, buf, len);
96 } else if (strequal(dest,"nmbd")) {
97 pid = pidfile_pid(dest);
98 if (pid == 0) {
99 fprintf(stderr,"Can't find pid for nmbd\n");
100 return False;
102 } else {
103 pid = atoi(dest);
104 if (pid == 0) {
105 fprintf(stderr,"Not a valid pid\n");
106 return False;
110 return message_send_pid(pid, msg_type, buf, len);
113 /****************************************************************************
114 evaluate a message type string
115 ****************************************************************************/
116 static int parse_type(char *mtype)
118 int i;
119 for (i=0;msg_types[i].name;i++) {
120 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
122 return -1;
126 /****************************************************************************
127 do command
128 ****************************************************************************/
129 static BOOL do_command(char *dest, char *msg_name, char *params)
131 int i, n, v;
132 int mtype;
133 BOOL retval;
135 mtype = parse_type(msg_name);
136 if (mtype == -1) {
137 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
138 return(False);
141 switch (mtype) {
142 case MSG_DEBUG:
143 if (!params) {
144 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
145 return(False);
147 v = atoi(params);
148 send_message(dest, MSG_DEBUG, &v, sizeof(int));
149 break;
151 case MSG_PROFILE:
152 if (!params) {
153 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
154 return(False);
156 if (strequal(params, "off")) {
157 v = 0;
158 } else if (strequal(params, "count")) {
159 v = 1;
160 } else if (strequal(params, "on")) {
161 v = 2;
162 } else if (strequal(params, "flush")) {
163 v = 3;
164 } else {
165 fprintf(stderr,
166 "MSG_PROFILE parameter must be off, count, on, or flush\n");
167 return(False);
169 send_message(dest, MSG_PROFILE, &v, sizeof(int));
170 break;
172 case MSG_FORCE_ELECTION:
173 if (!strequal(dest, "nmbd")) {
174 fprintf(stderr,"force-election can only be sent to nmbd\n");
175 return(False);
177 send_message(dest, MSG_FORCE_ELECTION, NULL, 0);
178 break;
180 case MSG_REQ_DEBUGLEVEL:
181 if (!debuglevel_registered) {
182 message_register(MSG_DEBUGLEVEL, debuglevel_function);
183 debuglevel_registered = True;
185 got_level = False;
186 retval = send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0);
187 if (retval) {
188 timeout_start = time(NULL);
189 while (!got_level) {
190 message_dispatch();
191 if ((time(NULL) - timeout_start) > MAX_WAIT) {
192 fprintf(stderr,"debuglevel timeout\n");
193 break;
197 break;
199 case MSG_PING:
200 if (!pong_registered) {
201 message_register(MSG_PONG, pong_function);
202 pong_registered = True;
204 if (!params) {
205 fprintf(stderr,"MSG_PING needs a parameter\n");
206 return(False);
208 n = atoi(params);
209 pong_count = 0;
210 for (i=0;i<n;i++) {
211 retval = send_message(dest, MSG_PING, NULL, 0);
212 if (retval == False) break;
214 if (retval) {
215 timeout_start = time(NULL);
216 while (pong_count < n) {
217 message_dispatch();
218 if ((time(NULL) - timeout_start) > MAX_WAIT) {
219 fprintf(stderr,"PING timeout\n");
220 break;
224 break;
228 return (True);
231 int main(int argc, char *argv[])
233 int opt;
234 char temp[255];
235 extern int optind;
236 pstring servicesf = CONFIGFILE;
237 BOOL interactive = False;
239 TimeInit();
240 setup_logging(argv[0],True);
242 charset_initialise();
243 lp_load(servicesf,False,False,False);
245 if (!message_init()) exit(1);
247 if (argc < 2) usage(True);
249 while ((opt = getopt(argc, argv,"i")) != EOF) {
250 switch (opt) {
251 case 'i':
252 interactive = True;
253 break;
254 default:
255 printf("Unknown option %c (%d)\n", (char)opt, opt);
256 usage(True);
260 argc -= optind;
261 argv = &argv[optind];
263 if (!interactive) {
264 if (argc < 2) usage(True);
265 return (do_command(argv[0],argv[1],argc > 2 ? argv[2] : 0));
268 while (True) {
269 char *myargv[3];
270 int myargc;
272 printf("smbcontrol> ");
273 if (!fgets(temp, sizeof(temp)-1, stdin)) break;
274 myargc = 0;
275 while ((myargc < 3) &&
276 (myargv[myargc] = strtok(myargc?NULL:temp," \t\n"))) {
277 myargc++;
279 if (!myargc) break;
280 if (strequal(myargv[0],"q")) break;
281 if (myargc < 2)
282 usage(False);
283 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? myargv[2] : 0))
284 usage(False);
286 return(0);