18 #include <ndb_global.h>
21 #include "MgmtSrvr.hpp"
22 #include "EventLogger.hpp"
26 #include <kernel_types.h>
27 #include <portlib/ndb_daemon.h>
28 #include <NdbConfig.h>
30 #include <portlib/NdbDir.hpp>
31 #include <ndb_version.h>
32 #include <mgmapi_config_parameters.h>
33 #include <NdbAutoPtr.hpp>
34 #include <ndb_mgmclient.hpp>
36 #include <EventLogger.hpp>
40 const char *load_default_groups[]= {
"mysql_cluster",
"ndb_mgmd",0 };
46 #elif !defined(__NETWARE__)
47 #include <readline/readline.h>
48 extern "C" int add_history(
const char *command);
54 read_and_execute(
Ndb_mgmclient* com,
const char * prompt,
int _try_reconnect)
56 static char *line_read = (
char *)NULL;
63 line_read = (
char *)NULL;
67 line_read = readline (prompt);
69 if (line_read && *line_read)
70 add_history (line_read);
72 static char linebuffer[254];
73 fputs(prompt, stdout);
74 linebuffer[
sizeof(linebuffer)-1]=0;
75 line_read = fgets(linebuffer,
sizeof(linebuffer)-1, stdin);
76 if (line_read == linebuffer) {
80 line_read= strdup(linebuffer);
83 return com->execute(line_read,_try_reconnect);
87 bool g_StopServer=
false;
88 bool g_RestartServer=
false;
91 static const char* opt_logname =
"MgmtSrvr";
92 static const char* opt_nowait_nodes = 0;
94 static struct my_option my_long_options[] =
96 NDB_STD_OPTS(
"ndb_mgmd"),
97 {
"config-file",
'f',
"Specify cluster configuration file",
98 (uchar**) &opts.config_filename, (uchar**) &opts.config_filename, 0,
99 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
100 {
"print-full-config",
'P',
"Print full config and exit",
101 (uchar**) &opts.print_full_config, (uchar**) &opts.print_full_config, 0,
102 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
103 {
"daemon",
'd',
"Run ndb_mgmd in daemon mode (default)",
104 (uchar**) &opts.daemon, (uchar**) &opts.daemon, 0,
105 GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0 },
106 {
"interactive", NDB_OPT_NOSHORT,
107 "Run interactive. Not supported but provided for testing purposes",
108 (uchar**) &opts.interactive, (uchar**) &opts.interactive, 0,
109 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
110 {
"no-nodeid-checks", NDB_OPT_NOSHORT,
111 "Do not provide any node id checks",
112 (uchar**) &opts.no_nodeid_checks, (uchar**) &opts.no_nodeid_checks, 0,
113 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
114 {
"nodaemon", NDB_OPT_NOSHORT,
115 "Don't run as daemon, but don't read from stdin",
116 (uchar**) &opts.non_interactive, (uchar**) &opts.non_interactive, 0,
117 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
118 {
"mycnf", NDB_OPT_NOSHORT,
119 "Read cluster config from my.cnf",
120 (uchar**) &opts.mycnf, (uchar**) &opts.mycnf, 0,
121 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
122 {
"bind-address", NDB_OPT_NOSHORT,
123 "Local bind address",
124 (uchar**) &opts.bind_address, (uchar**) &opts.bind_address, 0,
125 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
126 {
"configdir", NDB_OPT_NOSHORT,
127 "Directory for the binary configuration files (alias for --config-dir)",
128 (uchar**) &opts.configdir, (uchar**) &opts.configdir, 0,
129 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
130 {
"config-dir", NDB_OPT_NOSHORT,
131 "Directory for the binary configuration files",
132 (uchar**) &opts.configdir, (uchar**) &opts.configdir, 0,
133 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
134 {
"config-cache", NDB_OPT_NOSHORT,
135 "Enable configuration cache and change management",
136 (uchar**) &opts.config_cache, (uchar**) &opts.config_cache, 0,
137 GET_BOOL, NO_ARG, 1, 0, 1, 0, 0, 0 },
139 "Write more log messages",
140 (uchar**) &opts.verbose, (uchar**) &opts.verbose, 0,
141 GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
142 {
"reload", NDB_OPT_NOSHORT,
143 "Reload config from config.ini or my.cnf if it has changed on startup",
144 (uchar**) &opts.reload, (uchar**) &opts.reload, 0,
145 GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
146 {
"initial", NDB_OPT_NOSHORT,
147 "Delete all binary config files and start from config.ini or my.cnf",
148 (uchar**) &opts.initial, (uchar**) &opts.initial, 0,
149 GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
150 {
"log-name", NDB_OPT_NOSHORT,
151 "Name to use when logging messages for this node",
152 (uchar**) &opt_logname, (uchar**) &opt_logname, 0,
153 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
154 {
"nowait-nodes", NDB_OPT_NOSHORT,
155 "Nodes that will not be waited for during start",
156 (uchar**) &opt_nowait_nodes, (uchar**) &opt_nowait_nodes, 0,
157 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
158 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
161 static void short_usage_sub(
void)
163 ndb_short_usage_sub(NULL);
164 ndb_service_print_options(
"ndb_mgmd");
169 ndb_usage(short_usage_sub, load_default_groups, my_long_options);
172 static char **defaults_argv;
174 static void mgmd_exit(
int result)
176 g_eventLogger->
close();
179 free_defaults(defaults_argv);
181 ndb_end(opt_ndb_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
183 ndb_daemon_exit(result);
186 #include "../common/util/parse_mask.hpp"
188 static int mgmd_main(
int argc,
char** argv)
192 printf(
"MySQL Cluster Management Server %s\n", NDB_VERSION_STRING);
194 ndb_opt_set_usage_funcs(short_usage_sub, usage);
196 load_defaults(
"my",load_default_groups,&argc,&argv);
201 opt_debug= IF_WIN(
"d:t:i:F:o,c:\\ndb_mgmd.trace",
202 "d:t:i:F:o,/tmp/ndb_mgmd.trace");
205 if ((ho_error=handle_options(&argc, &argv, my_long_options,
206 ndb_std_get_one_option)))
209 if (opts.interactive ||
210 opts.non_interactive ||
211 opts.print_full_config) {
215 if (opts.mycnf && opts.config_filename)
217 fprintf(stderr,
"ERROR: Both --mycnf and -f is not supported\n");
221 if (opt_nowait_nodes)
223 int res = parse_mask(opt_nowait_nodes, opts.nowait_nodes);
224 if(res == -2 || (res > 0 && opts.nowait_nodes.
get(0)))
226 fprintf(stderr,
"ERROR: Invalid nodeid specified in nowait-nodes: '%s'\n",
232 fprintf(stderr,
"ERROR: Unable to parse nowait-nodes argument: '%s'\n",
250 g_eventLogger->
enable(Logger::LL_ALL);
256 #if !defined NDB_WIN32
257 signal(SIGPIPE, SIG_IGN);
260 while (!g_StopServer)
264 g_eventLogger->
critical(
"Out of memory, couldn't create MgmtSrvr");
274 if (NdbDir::chdir(NdbConfig_get_path(NULL)) != 0)
276 g_eventLogger->
warning(
"Cannot change directory to '%s', error: %d",
277 NdbConfig_get_path(NULL), errno);
283 NodeId localNodeId= mgm->getOwnNodeId();
284 if (localNodeId == 0) {
285 g_eventLogger->
error(
"Couldn't get own node id");
290 char *lockfile= NdbConfig_PidFileName(localNodeId);
291 char *logfile= NdbConfig_StdoutFileName(localNodeId);
292 if (ndb_daemonize(lockfile, logfile))
294 g_eventLogger->
error(
"Couldn't start as daemon, error: '%s'",
306 if (opts.interactive) {
309 if(opts.bind_address)
310 con_str.
appfmt(
"host=%s:%d", opts.bind_address, port);
312 con_str.
appfmt(
"localhost:%d", port);
314 while(!g_StopServer){
315 if (!read_and_execute(&com,
"ndb_mgm> ", 1))
321 g_eventLogger->
info(
"MySQL Cluster Management Server %s started",
324 while (!g_StopServer)
325 NdbSleep_MilliSleep(500);
328 g_eventLogger->
info(
"Shutting down server...");
330 g_eventLogger->
info(
"Shutdown complete");
333 g_eventLogger->
info(
"Restarting server...");
334 g_RestartServer= g_StopServer=
false;
343 static void mgmd_stop(
void)
349 int main(
int argc,
char** argv)
351 return ndb_daemon_init(argc, argv, mgmd_main, mgmd_stop,
352 "ndb_mgmd",
"MySQL Cluster Management Server");