wbt_status wbt_conf_reload() { /* TODO 清理已保存的配置信息 */ /* 尝试读取配置文件 */ wbt_file_t wbt_config_file; wbt_config_file.fd = wbt_open_file(wbt_config_file_name.str); if( wbt_config_file.fd <= 0 ) { /* 找不到配置文件 */ wbt_log_print("Can't find config file: %.*s\n", wbt_config_file_name.len, wbt_config_file_name.str); return WBT_ERROR; } #ifndef WIN32 int len = wbt_get_file_path_by_fd(wbt_config_file.fd, wbt_config_file_path.str, wbt_config_file_path.len); if( len <= 0 ) { return WBT_ERROR; } wbt_config_file_path.str[len] = '\0'; #endif struct stat statbuff; if(stat(wbt_config_file_name.str, &statbuff) < 0){ return WBT_ERROR; } wbt_config_file.size = statbuff.st_size; wbt_config_file_content.len = wbt_config_file.size; wbt_config_file_content.str = wbt_malloc( wbt_config_file_content.len ); if( wbt_config_file_content.str == NULL ) { return WBT_ERROR; } if( wbt_read_file( wbt_config_file.fd, wbt_config_file_content.str, wbt_config_file_content.len, 0) != wbt_config_file_content.len ) { wbt_log_print("Read config file failed\n"); return WBT_ERROR; } /* 关闭配置文件 */ wbt_close_file(wbt_config_file.fd); /* 解析配置文件 */ if( wbt_conf_parse(&wbt_config_file_content) == WBT_OK ) { //wbt_rbtree_print(wbt_config_rbtree.root); wbt_free(wbt_config_file_content.str); #ifndef WIN32 char tmp[1024]; snprintf(tmp, sizeof(tmp), "bmq: master process (%.*s)", len, wbt_config_file_path.str ); wbt_set_proc_title(tmp); #endif return WBT_OK; } else { wbt_free(wbt_config_file_content.str); wbt_log_print("Syntax error on config file: line %d, charactor %d\n", wbt_conf_line, wbt_conf_charactor); return WBT_ERROR; } }
void wbt_worker_process() { /* 设置进程标题 */ if( !wbt_conf.run_mode ) { wbt_set_proc_title("webit: worker process"); } /* 设置需要监听的信号(后台模式) */ struct sigaction act; sigset_t set; act.sa_sigaction = wbt_signal; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; sigemptyset(&set); sigaddset(&set, SIGCHLD); sigaddset(&set, SIGTERM); sigaddset(&set, SIGPIPE); if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { wbt_log_add("sigprocmask() failed\n"); } sigaction(SIGINT, &act, NULL); /* 退出信号 */ sigaction(SIGTERM, &act, NULL); /* 退出信号 */ sigaction(SIGPIPE, &act, NULL); /* 忽略 */ wbt_log_add("Webit startup (pid: %d)\n", getpid()); /* 降低 worker 进程的权限 */ const char * user = wbt_conf_get("user"); if ( user != NULL && geteuid() == 0 ) { // 根据用户名查询 // TODO getpwnam 应当在更早的时候调用。以免调用 getpwnam 失败的时候,工作进程被不断重启。 struct passwd * pw = getpwnam(user); if( pw == NULL ) { wbt_log_add("user %s not exists\n", user); return; } if (setgid(pw->pw_gid) == -1) { wbt_log_add("setgid(%d) failed", pw->pw_gid); return; } if (initgroups(user, pw->pw_gid) == -1) { wbt_log_add("initgroups(%s, %d) failed", user, pw->pw_gid); return; } if (setuid(pw->pw_uid) == -1) { wbt_log_add("setuid(%d) failed", pw->pw_uid); return; } } /* 进入事件循环 */ wbt_event_dispatch(); wbt_exit(0); }