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.
31 {"force-election", MSG_FORCE_ELECTION},
33 {"profile", MSG_PROFILE},
34 {"debuglevel", MSG_REQ_DEBUGLEVEL},
38 static void usage(BOOL doexit)
42 printf("Usage: smbcontrol -i\n");
43 printf(" smbcontrol <destination> <message-type> <parameters>\n\n");
45 printf("<destination> <message-type> <parameters>\n\n");
47 printf("\t<destination> is one of \"nmbd\", \"smbd\" or a process ID\n");
48 printf("\t<message-type> is one of: ");
49 for (i=0; msg_types[i].name; i++)
50 printf("%s%s", i?", ":"",msg_types[i].name);
55 static int pong_count;
56 static BOOL got_level;
57 static BOOL pong_registered = False;
58 static BOOL debuglevel_registered = False;
61 /****************************************************************************
62 a useful function for testing the message system
63 ****************************************************************************/
64 void pong_function(int msg_type, pid_t src, void *buf, size_t len)
70 /****************************************************************************
71 Prints out the current Debug level returned by MSG_DEBUGLEVEL
72 ****************************************************************************/
73 void debuglevel_function(int msg_type, pid_t src, void *buf, size_t len)
76 memcpy(&level, buf, sizeof(int));
78 printf("Current debug level is %d\n",level);
82 /****************************************************************************
83 send a message to a named destination
84 ****************************************************************************/
85 static BOOL send_message(char *dest, int msg_type, void *buf, int len)
89 /* "smbd" is the only broadcast operation */
90 if (strequal(dest,"smbd")) {
91 return message_send_all(msg_type, buf, len);
92 } else if (strequal(dest,"nmbd")) {
93 pid = pidfile_pid(dest);
95 fprintf(stderr,"Can't find pid for nmbd\n");
101 fprintf(stderr,"Not a valid pid\n");
106 return message_send_pid(pid, msg_type, buf, len);
109 /****************************************************************************
110 evaluate a message type string
111 ****************************************************************************/
112 static int parse_type(char *mtype)
115 for (i=0;msg_types[i].name;i++) {
116 if (strequal(mtype, msg_types[i].name)) return msg_types[i].value;
122 /****************************************************************************
124 ****************************************************************************/
125 static BOOL do_command(char *dest, char *msg_name, char *params)
130 mtype = parse_type(msg_name);
132 fprintf(stderr,"Couldn't resolve message type: %s\n", msg_name);
139 fprintf(stderr,"MSG_DEBUG needs a parameter\n");
143 send_message(dest, MSG_DEBUG, &v, sizeof(int));
148 fprintf(stderr,"MSG_PROFILE needs a parameter\n");
151 if (strequal(params, "on")) {
153 } else if (strequal(params, "off")) {
155 } else if (strequal(params, "count")) {
159 "MSG_PROFILE parameter must be on, off, or count\n");
162 send_message(dest, MSG_PROFILE, &v, sizeof(int));
165 case MSG_FORCE_ELECTION:
166 if (!strequal(dest, "nmbd")) {
167 fprintf(stderr,"force-election can only be sent to nmbd\n");
170 send_message(dest, MSG_FORCE_ELECTION, NULL, 0);
173 case MSG_REQ_DEBUGLEVEL:
174 if (!debuglevel_registered) {
175 message_register(MSG_DEBUGLEVEL, debuglevel_function);
176 debuglevel_registered = True;
178 if (strequal(dest, "nmbd") || strequal(dest, "smbd")) {
179 fprintf(stderr,"debuglevel can only be sent to a PID\n");
183 send_message(dest, MSG_REQ_DEBUGLEVEL, NULL, 0);
184 while (!got_level) message_dispatch();
188 if (!pong_registered) {
189 message_register(MSG_PONG, pong_function);
190 pong_registered = True;
193 fprintf(stderr,"MSG_PING needs a parameter\n");
199 send_message(dest, MSG_PING, NULL, 0);
201 while (pong_count < n) message_dispatch();
209 int main(int argc, char *argv[])
214 pstring servicesf = CONFIGFILE;
215 BOOL interactive = False;
218 setup_logging(argv[0],True);
220 charset_initialise();
221 lp_load(servicesf,False,False,False);
225 if (argc < 2) usage(True);
227 while ((opt = getopt(argc, argv,"i")) != EOF) {
233 printf("Unknown option %c (%d)\n", (char)opt, opt);
239 argv = &argv[optind];
242 if (argc < 2) usage(True);
243 return (do_command(argv[0],argv[1],argc > 2 ? argv[2] : 0));
250 printf("smbcontrol> ");
251 if (!gets(temp)) break;
253 while ((myargc < 3) &&
254 (myargv[myargc] = strtok(myargc?NULL:temp," \t"))) {
260 else if (!do_command(myargv[0],myargv[1],myargc > 2 ? myargv[2] : 0))