Fdfs_trackerd.c文件中tracker的运行函数架构
main
|-----log_init,getExeAbsoluteFilename
|-----tracker_load_from_conf_file
|-----tracker_load_status_from_file
|-----base64_init_ex
|-----tracker_mem_init
|-----socketServer
|-----tcpsetserveropt
|-----daemon_init
|-----tracker_service_init
|-----sigUsrHandler,sigHupHandler,sigQuitHandler,sigDumpHandler
|-----tracker_httpd_start
|-----tracker_http_check_start
|-----set_run_by
|-----sched_start
|-----tracker_accept_loop
|-----tracker_terminate_threads,tracker_http_check_stop
|-----tracker_mem_destroy,tracker_service_destroy,log_destroy
通过tracker目录下的Fdfs_trackerd.c文件,可以分析出tracker的执行流程
-
int main(int argc, char *argv[])
-
{
-
char *conf_filename; //配置文件名称
-
int result;
-
int sock; //sock id
-
pthread_t schedule_tid;
-
struct sigaction act; //信号
-
ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT];
-
ScheduleArray scheduleArray;
-
-
if (argc < 2)
-
{
-
printf("Usage: %s \n", argv[0]);
-
return 1;
-
}
-
-
g_up_time = time(NULL);
-
log_init();
-
-
#if defined(DEBUG_FLAG) && defined(OS_LINUX)
-
if (getExeAbsoluteFilename(argv[0], g_exe_name, \ //获取配置文件的绝对路径名
-
sizeof(g_exe_name)) == NULL)
-
{
-
log_destroy();
-
return errno != 0 ? errno : ENOENT;
-
}
-
#endif
-
-
conf_filename = argv[1]; //tracker的配置文件
-
memset(bind_addr, 0, sizeof(bind_addr));
-
if ((result=tracker_load_from_conf_file(conf_filename, \ //解析配置文件,初始化全局参数
-
bind_addr, sizeof(bind_addr))) != 0)
-
{
-
log_destroy();
-
return result;
-
}
-
-
if ((result=tracker_load_status_from_file(&g_tracker_last_status)) != 0) //从状态文件中读取状态信息
-
{
-
log_destroy();
-
return result;
-
}
-
-
base64_init_ex(&g_base64_context, 0, '-', '_', '.'); //
-
if ((result=set_rand_seed()) != 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"set_rand_seed fail, program exit!", __LINE__);
-
return result;
-
}
-
-
if ((result=tracker_mem_init()) != 0) //所有的空间分配操作,数据读取操作,锁的初始化操作
-
{
-
log_destroy();
-
return result;
-
}
-
-
sock = socketServer(bind_addr, g_server_port, &result); //创建socket,绑定,监听
-
if (sock < 0)
-
{
-
log_destroy();
-
return result;
-
}
-
-
if ((result=tcpsetserveropt(sock, g_fdfs_network_timeout)) != 0) //设定socket的选项
-
{
-
log_destroy();
-
return result;
-
}
-
-
daemon_init(true); //使该进程成功一个守护进程
-
umask(0); //重设文件掩码
-
-
if (dup2(g_log_context.log_fd, STDOUT_FILENO) < 0 || \
-
dup2(g_log_context.log_fd, STDERR_FILENO) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call dup2 fail, errno: %d, error info: %s, " \
-
"program exit!", __LINE__, errno, STRERROR(errno));
-
g_continue_flag = false;
-
return errno;
-
}
-
-
if ((result=tracker_service_init()) != 0) //初始化tracker service
-
{
-
log_destroy();
-
return result;
-
}
-
-
memset(&act, 0, sizeof(act)); //初始化信号内存
-
sigemptyset(&act.sa_mask);
-
-
act.sa_handler = sigUsrHandler; //设置信号SIGUSR1与SIGUSR2的处理程序
-
if(sigaction(SIGUSR1, &act, NULL) < 0 || \
-
sigaction(SIGUSR2, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
-
act.sa_handler = sigHupHandler; //设置信号SIGHUP的处理程序
-
if(sigaction(SIGHUP, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
-
act.sa_handler = SIG_IGN; //设置信号SIGPIPE的处理程序
-
if(sigaction(SIGPIPE, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
-
act.sa_handler = sigQuitHandler; //设置信号SIGINT的处理程序
-
if(sigaction(SIGINT, &act, NULL) < 0 || \
-
sigaction(SIGTERM, &act, NULL) < 0 || \
-
sigaction(SIGQUIT, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
-
#if defined(DEBUG_FLAG)
-
/*
-
#if defined(OS_LINUX)
-
memset(&act, 0, sizeof(act));
-
sigemptyset(&act.sa_mask);
-
act.sa_sigaction = sigSegvHandler;
-
act.sa_flags = SA_SIGINFO;
-
if (sigaction(SIGSEGV, &act, NULL) < 0 || \ //设置信号SIGSEGV与SIGABRT的处理程序
-
sigaction(SIGABRT, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
#endif
-
*/
-
-
memset(&act, 0, sizeof(act));
-
sigemptyset(&act.sa_mask);
-
act.sa_handler = sigDumpHandler;
-
if(sigaction(SIGUSR1, &act, NULL) < 0 || \
-
sigaction(SIGUSR2, &act, NULL) < 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"call sigaction fail, errno: %d, error info: %s", \
-
__LINE__, errno, STRERROR(errno));
-
return errno;
-
}
-
#endif
-
-
#ifdef WITH_HTTPD //http设置
-
if (!g_http_params.disabled)
-
{
-
if ((result=tracker_httpd_start(bind_addr)) != 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"tracker_httpd_start fail, program exit!", \
-
__LINE__);
-
return result;
-
}
-
-
}
-
-
if ((result=tracker_http_check_start()) != 0)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"tracker_http_check_start fail, " \
-
"program exit!", __LINE__);
-
return result;
-
}
-
#endif
-
-
if ((result=set_run_by(g_run_by_group, g_run_by_user)) != 0) //设置用户启动组与启动用户
-
{
-
log_destroy();
-
return result;
-
}
-
-
scheduleArray.entries = scheduleEntries; //初始化调度数组
-
scheduleArray.count = SCHEDULE_ENTRIES_COUNT;
-
-
memset(scheduleEntries, 0, sizeof(scheduleEntries));
-
scheduleEntries[0].id = 1;
-
scheduleEntries[0].time_base.hour = TIME_NONE;
-
scheduleEntries[0].time_base.minute = TIME_NONE;
-
scheduleEntries[0].interval = g_sync_log_buff_interval;
-
scheduleEntries[0].task_func = log_sync_func; //处理函数
-
scheduleEntries[0].func_args = &g_log_context; //参数
-
-
scheduleEntries[1].id = 2;
-
scheduleEntries[1].time_base.hour = TIME_NONE;
-
scheduleEntries[1].time_base.minute = TIME_NONE;
-
scheduleEntries[1].interval = g_check_active_interval;
-
scheduleEntries[1].task_func = tracker_mem_check_alive;
-
scheduleEntries[1].func_args = NULL;
-
-
scheduleEntries[2].id = 3;
-
scheduleEntries[2].time_base.hour = 0;
-
scheduleEntries[2].time_base.minute = 0;
-
scheduleEntries[2].interval = TRACKER_SYNC_STATUS_FILE_INTERVAL;
-
scheduleEntries[2].task_func = tracker_write_status_to_file;
-
scheduleEntries[2].func_args = NULL;
-
if ((result=sched_start(&scheduleArray, &schedule_tid, \ //调度开始
-
g_thread_stack_size, &g_continue_flag)) != 0)
-
{
-
log_destroy();
-
return result;
-
}
-
-
log_set_cache(true);
-
-
bTerminateFlag = false;
-
bAcceptEndFlag = false;
-
-
tracker_accept_loop(sock); //开始接受连接请求
-
bAcceptEndFlag = true;
-
if (g_schedule_flag)
-
{
-
pthread_kill(schedule_tid, SIGINT);//发送SIGINT信号
-
}
-
tracker_terminate_threads();
-
-
#ifdef WITH_HTTPD
-
if (g_http_check_flag)
-
{
-
tracker_http_check_stop();
-
}
-
-
while (g_http_check_flag)
-
{
-
usleep(50000);
-
}
-
#endif
-
-
while ((g_tracker_thread_count != 0) || g_schedule_flag)
-
{
-
-
/*
-
#if defined(DEBUG_FLAG) && defined(OS_LINUX)
-
if (bSegmentFault)
-
{
-
sleep(5);
-
break;
-
}
-
#endif
-
*/
-
-
usleep(50000);
-
}
-
-
tracker_mem_destroy(); //清理内存
-
tracker_service_destroy(); //清理服务
-
-
logInfo("exit nomally.\n");
-
log_destroy(); //清理日志
-
-
return 0;
-
}
-
函数执行步骤如下:
(1) 日志系统初始化,调用函数log_init,并获取配置文件的绝对文件名称,调用函数getExeAbsoluteFilename
(2) 从配置文件中读取配置数据,并初始化全局变量,调用函数tracker_load_from_conf_file
(3) 从状态配置文件中读取状态数据,并初始化全局变量,调用函数tracker_load_status_from_file
(4) 调用base64_init_ex函数,初始化g_base64_context变量
(5) 初始化所需要的内存与锁,调用函数tracker_mem_init
(6) 创建socket,调用函数socketServer
(7) 设置socket,调用函数tcpsetserveropt
(8) 使进程变成守护进程,调用函数daemon_init
(9) 初始化tracker service,调用函数tracker_service_init
(10)设定信号处理函数
(11)启动http服务,调用函数tracker_httpd_start
(12)tracker_http_check_start
(13)设置运行用户与用户组,调用函数set_run_by
(14)调度进程启动,调用函数sched_start
(15)主循环启动,调用函数tracker_accept_loop
(16)清理
阅读(1321) | 评论(0) | 转发(0) |