static inline int _shm_command(App & app){ const char * shm_keypath = app.cmdopt().getoptstr("shm"); bool need_exit = false; bool clear_shm_recover = app.cmdopt().hasopt("shm-clear-recover"); bool clear_shm_backup = app.cmdopt().hasopt("shm-clear-backup"); if (!shm_keypath && (clear_shm_recover || clear_shm_backup)){ GLOG_ERR("not found shm path param for clearing shm!"); return -1; } if (clear_shm_recover){ dcshm_set_delete(dcshm_path_key(shm_keypath, 1)); need_exit = true; } if (clear_shm_backup){ dcshm_set_delete(dcshm_path_key(shm_keypath, 2)); need_exit = true; } if (need_exit){ return 1; } return 0; }
static inline int init_facilities(App & app, AppImpl * impl_){ //3.global logger logger_config_t lconf; lconf.dir = app.cmdopt().getoptstr("log-dir"); lconf.pattern = app.cmdopt().getoptstr("log-file"); lconf.lv = INT_LOG_LEVEL(app.cmdopt().getoptstr("log-level")); lconf.max_file_size = app.cmdopt().getoptint("log-size"); lconf.max_roll = app.cmdopt().getoptint("log-roll"); int ret = default_logger_init(lconf); if (ret){ fprintf(stderr, "logger init error = %d", ret); return -2; } //init timer ret = eztimer_init(); if (ret){ GLOG_ERR("eztimer init error :%d", ret); return -3; } eztimer_set_dispatcher(app_timer_dispatch); impl_->stcp = dctcp_default_pump(); if (!impl_->stcp){ GLOG_SER("dctcp loop init error !"); return -4; } dctcp_event_cb(impl_->stcp, app_stcp_listener, impl_); //control const char * console_listen = app.cmdopt().getoptstr("console-listen"); if (console_listen){ impl_->console = dctcp_listen(impl_->stcp, console_listen, "token:\r\n\r\n", app_console_listener, impl_); if (impl_->console < 0){ GLOG_SER("console init listen error : %d!", impl_->console); return -5; } } impl_->interval = app.cmdopt().getoptint("tick-interval"); impl_->maxtptick = app.cmdopt().getoptint("tick-maxproc"); //tzo set app.gmt_tz_offset(impl_->cmdopt->getoptint("tzo")); ////////////////////////////////////////////////////////////////////////////////// std::vector<dcshmobj_user_t*> shmusers = app.shm_users(); if (!shmusers.empty()){ const char * shmkey = app.cmdopt().getoptstr("shm"); if (!shmkey){ GLOG_ERR("not config shm key path setting !"); return -6; } for (size_t i = 0; i < shmusers.size(); ++i){ impl_->shm_pool.regis(shmusers[i]); } ret = impl_->shm_pool.start(shmkey); if (ret){ GLOG_ERR("shm pool start error ret:%d shm path:%s !", ret, shmkey); return -7; } } return 0; }
//return 0: continue, //return -1(<0): error //return 1(>0): exit success static inline int init_command(App & app, const char * pidfile){ int ret = 0; if (app.cmdopt().hasopt("stop")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGTERM, true); fprintf(stderr, "stoped process with normal stop mode [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("restart")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGUSR1, true); fprintf(stderr, "stoped process with restart mode [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("reload")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGUSR2, true, nullptr, true); fprintf(stderr, "reloaded process [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("console-shell")){ const char * console = app.cmdopt().getoptstr("console-listen"); if (!console){ fprintf(stderr, "has no console-listen option open console shell error !\n"); return -1; } string console_server = "tcp://"; console_server += console; printf("connecting to %s ...\n", console_server.c_str()); int confd = openfd(console_server.c_str(), "w", 3000); if (!confd){ fprintf(stderr, "connect error %s!\n", strerror(errno)); return -1; } enum { CONSOLE_BUFFER_SIZE = 1024*1024}; char * console_buffer = new char[CONSOLE_BUFFER_SIZE]; printf("console server connected ! command <quit> will exit shell\n"); while (true){ int n = readfd(confd, console_buffer, CONSOLE_BUFFER_SIZE, "token:\r\n\r\n", 1000 * 3600); if (n < 0){ fprintf(stderr, "console server closed [ret=%d]!\n", n); break; } printf("%s\n%s$", console_buffer, console); const char * command = fgets(console_buffer, CONSOLE_BUFFER_SIZE, stdin); if (strcasecmp(command, "quit") == 0){ break; } dcs::writefd(confd, command, 0, "token:\r\n\r\n"); } closefd(confd); delete console_buffer; return 1; } /////////////////////////////////////////////////////////////////// ret = _shm_command(app); if (ret < 0){ GLOG_ERR("shm command check error:%d !", ret); return -1; } if (ret > 0){ return 1; } return app.on_cmd_opt(); }