【redis源码学习】redis启动并读取配置文件的过程

redis-server [configfile] [options]

configfile 参数指定配置文件,options 参数指定启动配置项,可以覆盖配置文件中的配置项。

举个栗子:

redis-server ./redis.conf --port 6380 --protected-mode no


redisServer


关于配置信息,存储在如下结构体中:

//结构体稍微有点庞大(接近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). */

···

}


main函数


这是一个极长的函数,不过没有那个结构体长,而我们此次要学的技法也主要是在这里面,所以还是能接受的。

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

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值