int main(int argc, char *argv[], char *envp[]) { mog_upgrade_prepare(argc, argv, envp); /* hack for older gcov + gcc, see nostd/setproctitle.h */ spt_init(argc, argv, envp); set_program_name(argv[0]); mog_intr_disable(); setup(argc, argv); /* this daemonizes */ mog_process_init(worker_processes); if (worker_processes == 0) run_worker(0); else run_master(); return 0; }
int main (int argc, char **argv) { struct timeval tv; // 初始化相关环境变量与库 #ifdef INIT_SETPROCTITLE_REPLACEMENT spt_init(argc, argv); #endif // 设置当前的`locale`命令里的内容 setlocale(LC_COLLATE, ""); // 自动分配线程安全 zmalloc_enable_thread_safeness(); // 设置自动分配函数句柄(out of memory) zmalloc_set_oom_handler(redisOutOfMemoryHandler); // 时间异或pid生成随机数种子 srand(time(NULL) ^ getpid()); gettimeofday(&tv, NULL); // 设置字典hash值种子 dictSetHashFunctionSeed(tv.tv_sec ^ tv.tv_usec ^ getpid()); // 定点模式检测 server.sentinel_mode = checkForSentinelMode(argc, argv); // 初始化服务配置 initServerConfig(); // 定点模式处理 if (server.sentinel_mode) { printf("deal with sentinel mode\n"); } if (argc >= 2) { int j = 1; // init empty string sds options = sdsempty(); char *configfile = NULL; if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) version(); if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) usage(); if (strcmp(argv[1], "--test-memory") == 0) { if (argc == 3) { // 内存测试还未完全写完 memtest(atoi(argv[2]), 50); exit(0); } else { fprintf(stderr, "Please specify the amount of memory to test in megabytes.\n"); fprintf(stderr, "Example: ./redis-server --test-memory 4096\n\n"); exit(1); } } /* * 第一个参数为'--'开头, j始终为1 * 第一个参数不为'--'开头, j始终为2 * */ // j为1,第1个参数不是'--'开头的是配置文件 if (argv[j][0] != '-' || argv[j][1] != '-') { configfile = argv[j++]; } // 配置文件参数后还有内容 while (j != argc) { // 所有参数有包括'--'开头的参数 if (argv[j][0] == '-' && argv[j][1] == '-') { if (sdslen(options)) options = sdscat(options, "\n"); options = sdscat(options, " "); } else { // append new string, 转义特殊字符, return new pointer options = sdscatrepr(options, argv[j], strlen(argv[j])); options = sdscat(options, " "); } j++; } if (server.sentinel_mode && configfile && *configfile == '-') { printf("need redis log\n"); exit(1); } if (configfile) { server.configfile = getAbsolutePath(configfile); } resetServerSaveParams(); loadServerConfig(configfile, options); sdsfree(options); } else { printf("warning: no config file specified\n"); } // 后台模式 if (server.daemonize) { daemonize(); } // init server initServer(); printf("redis done\n"); return 0; }
// --------------------------------------------------------------------------- // int main() // // the entry of x_agent. // --------------------------------------------------------------------------- // int main(int argc, char* argv[]) { spt_init(argc, argv); int ch; bool isRunDeamon = false; string configFile("./agent.ini"); while((ch = getopt(argc, argv, "c:l:dvh")) != -1) { switch(ch) { case 'c': { if (optarg) { configFile = optarg; } } break; case 'd': { isRunDeamon = true; } break; case 'l': { if (optarg) { int loglevel = atoi(optarg); loglevel = loglevel == 0 ? 2 : loglevel; CSingleton<CLogWriter>::Instance()->SetLogLevel(loglevel); } } break; default: { if (ch == 'v' || (optarg && strcmp(optarg, "-v") == 0)) { fprintf(stderr, "%s version: %s, compile-time: %s\n", argv[0], FullVersion, CompileInfo); } else if (ch == 'h' || (optarg && strcmp(optarg, "-h") == 0)) { fprintf(stderr,"%s", HelpMsg); } else { fprintf(stderr,"\tError:%s -h for help.\n", argv[0]); } fflush(stderr); fflush(stdout); _exit(0); } break; } } // run by daemon mode. if (isRunDeamon) { wyf::DaemonMode(); } //register signal func atexit(before_exit); CatchSignal(); setproctitle("%s:Master.%d", argv[0], getpid()); do { int childPid = fork(); if (childPid == 0) { setsid(); atexit(before_exit); setproctitle("%s:%s", argv[0], "Worker."); CatchSignal(); // child process. StartAgent(configFile); _exit(0); } else if (childPid > 0) { LOG(LL_ERROR, "Start agent worker process:(%lu).", childPid); int checkTimes = 3; do { sleep(1); if (kill(childPid, 0) == 0) { // child exist... checkTimes = 3; sleep(5); } } while (checkTimes-- > 0); LOG(LL_ERROR, "agent worker process:(%lu) exit, try to start again.", childPid); } sleep(1); } while (1); return RET_OK; }