18 #include <ndb_global.h>
20 #include <my_getopt.h>
21 #include <mysql_version.h>
22 #include <ndb_version.h>
25 #include "APIService.hpp"
28 #include <portlib/NdbDir.hpp>
29 #include <BaseString.hpp>
30 #include <logger/Logger.hpp>
31 #include <logger/FileLogHandler.hpp>
32 #include <logger/SysLogHandler.hpp>
36 static const char *work_dir = CPCD_DEFAULT_WORK_DIR;
37 static int unsigned port;
38 static int use_syslog;
39 static const char *logfile = NULL;
40 static const char *user = 0;
42 static struct my_option my_long_options[] =
44 {
"work-dir",
'w',
"Work directory",
45 (uchar**) &work_dir, (uchar**) &work_dir, 0,
46 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
47 {
"port",
'p',
"TCP port to listen on",
48 (uchar**) &port, (uchar**) &port, 0,
49 GET_INT, REQUIRED_ARG, CPCD_DEFAULT_TCP_PORT, 0, 0, 0, 0, 0 },
50 {
"syslog",
'S',
"Log events to syslog",
51 (uchar**) &use_syslog, (uchar**) &use_syslog, 0,
52 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
53 {
"logfile",
'L',
"File to log events to",
54 (uchar**) &logfile, (uchar**) &logfile, 0,
55 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
56 {
"debug",
'D',
"Enable debug mode",
57 (uchar**) &debug, (uchar**) &debug, 0,
58 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
59 {
"user",
'u',
"Run as user",
60 (uchar**) &user, (uchar**) &user, 0,
61 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
62 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
66 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
72 static CPCD * g_cpcd = 0;
75 int main(
int argc,
char** argv){
76 const char *load_default_groups[]= {
"ndb_cpcd",0 };
79 load_defaults(
"ndb_cpcd",load_default_groups,&argc,&argv);
80 if (handle_options(&argc, &argv, my_long_options, get_one_option)) {
81 print_defaults(MYSQL_CONFIG_NAME,load_default_groups);
83 my_print_help(my_long_options);
84 my_print_variables(my_long_options);
88 logger.setCategory(
"ndb_cpcd");
89 logger.enable(Logger::LL_ALL);
92 logger.createConsoleHandler();
95 if(user && runas(user) != 0){
96 logger.critical(
"Unable to change user: %s", user);
103 if(logfile[0] !=
'/')
114 logger.info(
"Starting");
116 #if defined SIGPIPE && !defined _WIN32
117 (void)signal(SIGPIPE, SIG_IGN);
121 (void)signal(SIGCHLD, SIG_IGN);
128 if (access(work_dir, F_OK))
130 logger.info(
"Working directory '%s' does not exist, trying "
131 "to create it", work_dir);
132 if (!NdbDir::create(work_dir,
133 NdbDir::u_rwx() | NdbDir::g_r() | NdbDir::o_r()))
135 logger.error(
"Failed to create working directory, terminating!");
140 if(strlen(work_dir) > 0){
141 logger.debug(
"Changing dir to '%s'", work_dir);
142 if(NdbDir::chdir(work_dir) != 0){
143 logger.error(
"Cannot change directory to '%s', error: %d, terminating!",
153 unsigned short real_port= port;
154 if(!ss->
setup(serv, &real_port)){
155 logger.critical(
"Cannot setup server: %s", strerror(errno));
164 logger.debug(
"Start completed");
166 NdbSleep_MilliSleep(1000);