redis-server [configfile] [options]
configfile 参数指定配置文件,options 参数指定启动配置项,可以覆盖配置文件中的配置项。
举个栗子:
redis-server ./redis.conf --port 6380 --protected-mode no
关于配置信息,存储在如下结构体中:
//结构体稍微有点庞大(接近2W字),所以我稍微缩写一点哈,具体后面有用到再拿出来
struct redisServer {
/* General */
pid_t pid; /* Main process pid. */
char configfile; / Absolute config file path, or NULL */
char executable; / Absolute executable file path. */
char *exec_argv; / Executable argv vector (copy). */
···
}
这是一个极长的函数,不过没有那个结构体长,而我们此次要学的技法也主要是在这里面,所以还是能接受的。
int main(int argc, char **argv) {
struct timeval tv;
int j;
#ifdef REDIS_TEST
//这里略去
#endif
/* We need to initialize our libraries, and the server configuration. */
#ifdef INIT_SETPROCTITLE_REPLACEMENT
spt_init(argc, argv); //修改进程名
#endif
//此处略去一批
server.sentinel_mode = checkForSentinelMode(argc,argv); // 检查服务器是否以 Sentinel 模式启动
initServerConfig(); //将配置文件初始化为默认值(详见后文)
//此处再省一批
/* We need to init sentinel right now as parsing the configuration file
-
in sentinel mode will have the effect of populating the sentinel
-
data structures with master nodes to monitor. */
if (server.sentinel_mode) { //今天不讲哨兵,所以不管它
initSentinelConfig();
initSentinel();
}
//略
//开始了
if (argc >= 2) {
j = 1; /* First option to parse in argv[] */
sds options = sdsempty();
char *configfile = NULL;
/* Handle special options --help and --version */
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);
}
}
/* First argument is the config file name? */
if (argv[j][0] != ‘-’ || argv[j][1] != ‘-’) { //如果不是 --开头,那就是配置文件
configfile = a