| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- #include <signal.h> // for signal,kill...
- #include "h.h"
- #include "hsysinfo.h"
- #include "hmain.h"
- main_ctx_t g_main_ctx;
- static int create_pidfile();
- static void delete_pidfile();
- static pid_t getpid_from_pidfile();
- int create_pidfile() {
- FILE* fp = fopen(g_main_ctx.pidfile, "w");
- if (fp == NULL) {
- printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
- return -10;
- }
- char pid[16] = {0};
- snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
- fwrite(pid, 1, strlen(pid), fp);
- fclose(fp); atexit(delete_pidfile);
- hlogd("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
- return 0;
- }
- void delete_pidfile() {
- remove(g_main_ctx.pidfile);
- hlogd("delete_pidfile [%s]", g_main_ctx.pidfile);
- }
- pid_t getpid_from_pidfile() {
- FILE* fp = fopen(g_main_ctx.pidfile, "r");
- if (fp == NULL) {
- //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
- return -1;
- }
- char pid[64];
- int readbytes = fread(pid, 1, sizeof(pid), fp);
- fclose(fp);
- if (readbytes <= 0) {
- printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
- return -1;
- }
- return atoi(pid);
- }
- int main_ctx_init(int argc, char** argv) {
- g_main_ctx.pid = getpid();
- char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
- if (cwd == NULL) {
- printf("getcwd error\n");
- }
- //printf("run_path=%s\n", g_main_ctx.run_path);
- const char* b = argv[0];
- const char* e = b;
- while (*e) ++e;
- --e;
- while (e >= b) {
- if (*e == '/' || *e == '\\') {
- break;
- }
- --e;
- }
- strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
- #ifdef _WIN32
- if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
- *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
- }
- #endif
- //printf("program_name=%s\n", g_main_ctx.program_name);
- // save arg
- int i = 0;
- g_main_ctx.os_argv = argv;
- g_main_ctx.argc = 0;
- g_main_ctx.arg_len = 0;
- for (i = 0; argv[i]; ++i) {
- g_main_ctx.arg_len += strlen(argv[i]) + 1;
- }
- g_main_ctx.argc = i;
- char* argp = (char*)malloc(g_main_ctx.arg_len);
- memset(argp, 0, g_main_ctx.arg_len);
- g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
- for (i = 0; argv[i]; ++i) {
- g_main_ctx.save_argv[i] = argp;
- strcpy(g_main_ctx.save_argv[i], argv[i]);
- argp += strlen(argv[i]) + 1;
- }
- g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
- // save env
- g_main_ctx.os_envp = environ;
- g_main_ctx.envc = 0;
- g_main_ctx.env_len = 0;
- for (i = 0; environ[i]; ++i) {
- g_main_ctx.env_len += strlen(environ[i]) + 1;
- }
- g_main_ctx.envc = i;
- char* envp = (char*)malloc(g_main_ctx.env_len);
- memset(envp, 0, g_main_ctx.env_len);
- g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
- for (i = 0; environ[i]; ++i) {
- g_main_ctx.save_envp[i] = envp;
- strcpy(g_main_ctx.save_envp[i], environ[i]);
- envp += strlen(environ[i]) + 1;
- }
- g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
- // parse env
- for (i = 0; environ[i]; ++i) {
- char* b = environ[i];
- char* delim = strchr(b, '=');
- if (delim == NULL) {
- continue;
- }
- g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
- }
- /*
- // print argv and envp
- printf("---------------arg------------------------------\n");
- for (auto& pair : g_main_ctx.arg_kv) {
- printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
- }
- printf("---------------env------------------------------\n");
- for (auto& pair : g_main_ctx.env_kv) {
- printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
- }
- printf("PWD=%s\n", get_env("PWD"));
- printf("USER=%s\n", get_env("USER"));
- printf("HOME=%s\n", get_env("HOME"));
- printf("LANG=%s\n", get_env("LANG"));
- printf("TERM=%s\n", get_env("TERM"));
- printf("SHELL=%s\n", get_env("SHELL"));
- printf("================================================\n");
- */
- char logpath[MAX_PATH] = {0};
- snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
- MKDIR(logpath);
- snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
- snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
- snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
- g_main_ctx.confile_parser = NULL;
- g_main_ctx.oldpid = getpid_from_pidfile();
- #ifdef __unix__
- if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
- g_main_ctx.oldpid = -1;
- }
- #else
- #endif
- return 0;
- }
- const char* get_arg(const char* key) {
- auto iter = g_main_ctx.arg_kv.find(key);
- if (iter == g_main_ctx.arg_kv.end()) {
- return NULL;
- }
- return iter->second.c_str();
- }
- const char* get_env(const char* key) {
- auto iter = g_main_ctx.env_kv.find(key);
- if (iter == g_main_ctx.env_kv.end()) {
- return NULL;
- }
- return iter->second.c_str();
- }
- #ifdef __unix__
- /*
- * memory layout
- * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
- */
- void setproctitle(const char* title) {
- //printf("proctitle=%s\n", title);
- memset(g_main_ctx.os_argv[0], 0, g_main_ctx.arg_len + g_main_ctx.env_len);
- strncpy(g_main_ctx.os_argv[0], title, g_main_ctx.arg_len + g_main_ctx.env_len);
- }
- #endif
- // unix short style
- static char options[] = "hvc:ts:d";
- static char detail_options[] = "\
- -h : print help\n\
- -v : print version\n\
- -c confile : set configure file, default etc/${program}.conf\n\
- -t : test configure file and exit\n\
- -s signal : send signal to process\n\
- signal=[start, stop, restart, status]\n\
- -d : daemon\n\
- ";
- void print_version() {
- printf("%s version %s\n", g_main_ctx.program_name, get_compile_version());
- }
- void print_help() {
- printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
- printf("Options:\n%s\n", detail_options);
- }
- #define INVALID_OPTION -1
- #define FLAG_OPTION 1
- #define PARMA_OPTION 2
- int get_option(char opt) {
- char* p = options;
- while (*p && *p != opt) ++p;
- if (*p == '\0') return INVALID_OPTION;
- if (*(p+1) == ':') return PARMA_OPTION;
- return FLAG_OPTION;
- }
- int parse_cmdline(int argc, char** argv) {
- int i = 1;
- while (argv[i]) {
- char* p = argv[i];
- if (*p != '-') {
- printf("Invalid argv[%d]: %s\n", i, argv[i]);
- exit(-10);
- }
- while (*++p) {
- switch (get_option(*p)) {
- case INVALID_OPTION:
- printf("Invalid option: '%c'\n", *p);
- exit(-20);
- case FLAG_OPTION:
- g_main_ctx.arg_kv[std::string(p, 1)] = "true";
- break;
- case PARMA_OPTION:
- if (*(p+1) != '\0') {
- g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
- ++i;
- goto next_option;
- } else if (argv[i+1] != NULL) {
- g_main_ctx.arg_kv[std::string(p, 1)] = argv[i+1];
- i += 2;
- goto next_option;
- } else {
- printf("Option '%c' requires param\n", *p);
- exit(-30);
- }
- }
- }
- ++i;
- next_option:
- continue;
- }
- return 0;
- }
- #ifdef __unix__
- // unix use signal
- // we use SIGTERM to quit process
- #define SIGNAL_TERMINATE SIGTERM
- #include <sys/wait.h>
- #define MAXNUM_WORKER_PROCESSES 1024
- #define DEFAULT_WORKER_PROCESSES 4
- static pid_t s_worker_processes[MAXNUM_WORKER_PROCESSES];
- void worker_process_cycle() {
- char proctitle[256] = {0};
- snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
- setproctitle(proctitle);
- while(1) {
- msleep(1);
- }
- }
- int create_worker_process(int worker_processes) {
- for (int i = 0; i < worker_processes; ++i) {
- pid_t pid = fork();
- if (pid < 0) {
- hloge("fork error: %d", errno);
- return errno;
- }
- if (pid == 0) {
- hlogi("worker process start/running, pid=%d", getpid());
- worker_process_cycle();
- exit(0);
- }
- for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
- if (s_worker_processes[i] <= 0) {
- s_worker_processes[i] = pid;
- break;
- }
- }
- }
- return 0;
- }
- static int s_signo = 0;
- void master_process_signal_handler(int signo) {
- hlogd("pid=%d recv signo=%d", getpid(), signo);
- s_signo = signo;
- }
- void master_process_init() {
- for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
- s_worker_processes[i] = -1;
- }
- }
- void master_process_cycle() {
- char proctitle[256] = {0};
- snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
- setproctitle(proctitle);
- signal(SIGINT, master_process_signal_handler);
- signal(SIGCHLD, master_process_signal_handler);
- signal(SIGNAL_TERMINATE, master_process_signal_handler);
- sigset_t sigset;
- sigemptyset(&sigset);
- sigaddset(&sigset, SIGINT);
- sigaddset(&sigset, SIGCHLD);
- sigaddset(&sigset, SIGNAL_TERMINATE);
- sigprocmask(SIG_BLOCK, &sigset, NULL);
- int worker_processes = 0;
- IniParser* ini = (IniParser*)g_main_ctx.confile_parser;
- if (ini) {
- worker_processes = atoi(ini->GetValue("worker_processes", "core").c_str());
- hlogd("core.worker_processes=%d", worker_processes);
- }
- if (worker_processes <= 0 || worker_processes > MAXNUM_WORKER_PROCESSES) {
- worker_processes = get_ncpu();
- hlogd("worker_processes=ncpu=%d", worker_processes);
- }
- if (worker_processes <= 0 || worker_processes > MAXNUM_WORKER_PROCESSES) {
- worker_processes = DEFAULT_WORKER_PROCESSES;
- }
- create_worker_process(worker_processes);
- sigemptyset(&sigset);
- while (1) {
- sigsuspend(&sigset);
- switch (s_signo) {
- case SIGINT:
- case SIGNAL_TERMINATE:
- hlogi("killall worker processes");
- for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
- if (s_worker_processes[i] <= 0) break;
- kill(s_worker_processes[i], SIGKILL);
- s_worker_processes[i] = -1;
- }
- msleep(1000);
- exit(0);
- break;
- case SIGCHLD:
- {
- pid_t pid = 0;
- int status = 0;
- while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
- hlogw("worker process stop/waiting, pid=%d status=%d", pid, status);
- for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
- if (s_worker_processes[i] == pid) {
- s_worker_processes[i] = -1;
- break;
- }
- }
- create_worker_process(1);
- }
- }
- break;
- default:
- break;
- }
- }
- }
- #else
- // win32 use Event
- static HANDLE s_hEventTerm = NULL;
- void master_process_exit() {
- CloseHandle(s_hEventTerm);
- s_hEventTerm = NULL;
- }
- void master_process_init() {
- char eventname[MAX_PATH] = {0};
- snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
- s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
- //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
- atexit(master_process_exit);
- }
- void master_process_cycle() {
- while (1) {
- DWORD ret = WaitForSingleObject(s_hEventTerm, INFINITE);
- exit(0);
- }
- }
- #endif
- int main(int argc, char** argv) {
- main_ctx_init(argc, argv);
- parse_cmdline(argc, argv);
- master_process_init();
- if (get_arg("h")) {
- print_help();
- exit(0);
- }
- if (get_arg("v")) {
- print_version();
- exit(0);
- }
- const char* confile = get_arg("c");
- if (confile) {
- strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
- }
- IniParser* ini = new IniParser;
- int ret = ini->LoadFromFile(g_main_ctx.confile);
- if (ret != 0) {
- printf("Load confile [%s] failed: %d\n", g_main_ctx.confile, ret);
- exit(-40);
- } else {
- g_main_ctx.confile_parser = ini;
- }
- if (get_arg("t")) {
- printf("Test confile [%s] OK!\n", g_main_ctx.confile);
- exit(0);
- }
- const char* signal = get_arg("s");
- if (signal) {
- if (strcmp(signal, "start") == 0) {
- if (g_main_ctx.oldpid > 0) {
- printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
- exit(0);
- }
- } else if (strcmp(signal, "stop") == 0) {
- if (g_main_ctx.oldpid > 0) {
- #ifdef __unix__
- kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
- #else
- SetEvent(s_hEventTerm);
- #endif
- printf("%s stop/waiting\n", g_main_ctx.program_name);
- } else {
- printf("%s is already stopped", g_main_ctx.program_name);
- }
- exit(0);
- } else if (strcmp(signal, "restart") == 0) {
- if (g_main_ctx.oldpid > 0) {
- #ifdef __unix__
- kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
- #else
- SetEvent(s_hEventTerm);
- #endif
- printf("%s stop/waiting\n", g_main_ctx.program_name);
- msleep(1000);
- }
- } else if (strcmp(signal, "status") == 0) {
- if (g_main_ctx.oldpid > 0) {
- printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
- } else {
- printf("%s stop/waiting\n", g_main_ctx.program_name);
- }
- exit(0);
- } else {
- printf("Invalid signal: '%s'\n", signal);
- exit(0);
- }
- printf("%s start/running\n", g_main_ctx.program_name);
- }
- #ifdef __unix__
- if (get_arg("d")) {
- // nochdir, noclose
- int ret = daemon(1, 1);
- if (ret != 0) {
- printf("daemon error: %d\n", ret);
- exit(-10);
- }
- // parent process exit after daemon, so pid changed.
- g_main_ctx.pid = getpid();
- }
- #endif
- hlog_set_file(g_main_ctx.logfile);
- hlog_set_level(LOG_LEVEL_DEBUG);
- hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
- hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
- create_pidfile();
- master_process_cycle();
- return 0;
- }
|