heartbeat

suilingxi 2012-10-23 01:07:45
static int
initialize_heartbeat()
{
/*
* Things we have to do:
*
* Create all our pipes
* Open all our heartbeat channels
* fork all our children, and start the old ticker going...
*
* Everything is forked from the parent process. That's easier to
* monitor, and easier to shut down.
*/

int j;
struct stat buf;
int pid;
int ourproc = 0;
int (*getgen)(seqno_t * generation) = IncrGeneration;
struct tmpproc_track hostcache_info;
struct tmpproc_track delcache_info;

localdie = NULL;


change_logfile_ownership();

if (timebasedgenno) {
getgen = GetTimeBasedGeneration;
}
if (getgen(&config->generation) != HA_OK) {
cl_perror("Cannot get/increment generation number");
return HA_FAIL;
}
cl_log(LOG_INFO, "Heartbeat generation: %lu", config->generation);

if(GetUUID(config, curnode->nodename, &config->uuid) != HA_OK){
cl_log(LOG_ERR, "getting uuid for the local node failed");
return HA_FAIL;
}

if (ANYDEBUG){
char uuid_str[UU_UNPARSE_SIZEOF];
cl_uuid_unparse(&config->uuid, uuid_str);
cl_log(LOG_DEBUG, "uuid is:%s", uuid_str);
}

memset(&hostcache_info, 0, sizeof(hostcache_info));
write_hostcachefile = G_main_add_TriggerHandler(PRI_WRITECACHE
, write_hostcachedata, &hostcache_info, NULL);
hostcache_info.trigger = write_hostcachefile;

memset(&delcache_info, 0, sizeof(delcache_info));
write_delcachefile = G_main_add_TriggerHandler(PRI_WRITECACHE
, write_delcachedata, &delcache_info, NULL);
hostcache_info.trigger = write_delcachefile;

add_uuidtable(&config->uuid, curnode);
cl_uuid_copy(&curnode->uuid, &config->uuid);

/*
* We _really_ only need to write out the uuid file if we're not yet
* in the host cache file on disk.
*/
G_main_set_trigger(write_hostcachefile);

if (stat(FIFONAME, &buf) < 0 || !S_ISFIFO(buf.st_mode)) {
cl_log(LOG_INFO, "Creating FIFO %s.", FIFONAME);
unlink(FIFONAME);
if (mkfifo(FIFONAME, FIFOMODE) < 0) {
cl_perror("Cannot make fifo %s.", FIFONAME);
return HA_FAIL;
}
}else{
chmod(FIFONAME, FIFOMODE);
}

if (stat(FIFONAME, &buf) < 0) {
cl_log(LOG_ERR, "FIFO %s does not exist", FIFONAME);
return HA_FAIL;
}else if (!S_ISFIFO(buf.st_mode)) {
cl_log(LOG_ERR, "%s is not a FIFO", FIFONAME);
return HA_FAIL;
}



/* THIS IS RESOURCE WORK! FIXME */
/* Clean up tmp files from our resource scripts */
if (system("rm -fr " RSC_TMPDIR) <= 0) {
cl_log(LOG_INFO, "Removing %s failed, recreating.", RSC_TMPDIR);
}

/* Remake the temporary directory ... */
mkdir(RSC_TMPDIR
, S_IRUSR|S_IWUSR|S_IXUSR
| S_IRGRP|S_IWGRP|S_IXGRP
| S_IROTH|S_IWOTH|S_IXOTH | S_ISVTX /* sticky bit */);

/* Open all our heartbeat channels */

for (j=0; j < nummedia; ++j) {
struct hb_media* smj = sysmedia[j];

if (ipc_channel_pair(smj->wchan) != IPC_OK) {
cl_perror("cannot create hb write channel IPC");
return HA_FAIL;
}
if (ipc_channel_pair(smj->rchan) != IPC_OK) {
cl_perror("cannot create hb read channel IPC");
return HA_FAIL;
}
if (ANYDEBUG) {
cl_log(LOG_DEBUG, "opening %s %s (%s)", smj->type
, smj->name, smj->description);
}

}

PILSetDebugLevel(PluginLoadingSystem, NULL, NULL, debug_level);
CoreProcessCount = 0;
procinfo->nprocs = 0;
ourproc = procinfo->nprocs;
curproc = &procinfo->info[ourproc];
curproc->type = PROC_MST_CONTROL;
cl_malloc_setstats(&curproc->memstats);
cl_msg_setstats(&curproc->msgstats);
NewTrackedProc(getpid(), 0, PT_LOGVERBOSE, GINT_TO_POINTER(ourproc)
, &CoreProcessTrackOps);

curproc->pstat = RUNNING;

/* We need to at least ignore SIGINTs early on */
hb_signal_set_common(NULL);


/* Now the fun begins... */
/*
* Optimal starting order:
* fifo_child();
* write_child();
* read_child();
* master_control_process();
*
*/

SetupFifoChild();

ourproc = procinfo->nprocs;

for (j=0; j < nummedia; ++j) {
struct hb_media* mp = sysmedia[j];

ourproc = procinfo->nprocs;

if (mp->vf->open(mp) != HA_OK){
cl_log(LOG_ERR, "cannot open %s %s",
mp->type,
mp->name);
return HA_FAIL;
}

switch ((pid=fork())) {
case -1: cl_perror("Can't fork write proc.");
return HA_FAIL;
break;

case 0: /* Child */
close(watchdogfd);
curproc = &procinfo->info[ourproc];
cl_malloc_setstats(&curproc->memstats);
cl_msg_setstats(&curproc->msgstats);
curproc->type = PROC_HBWRITE;
while (curproc->pid != getpid()) {
sleep(1);
}
write_child(mp);
cl_perror("write process exiting");
cleanexit(1);
default:
mp->wchan[P_WRITEFD]->farside_pid = pid;


}
NewTrackedProc(pid, 0, PT_LOGVERBOSE
, GINT_TO_POINTER(ourproc)
, &CoreProcessTrackOps);

ourproc = procinfo->nprocs;

if (ANYDEBUG) {
cl_log(LOG_DEBUG, "write process pid: %d", pid);
}

switch ((pid=fork())) {
case -1: cl_perror("Can't fork read process");
return HA_FAIL;
break;

case 0: /* Child */
close(watchdogfd);
curproc = &procinfo->info[ourproc];
cl_malloc_setstats(&curproc->memstats);
cl_msg_setstats(&curproc->msgstats);
curproc->type = PROC_HBREAD;
while (curproc->pid != getpid()) {
sleep(1);
}
read_child(mp);
cl_perror("read_child() exiting");
cleanexit(1);
default:
mp->rchan[P_WRITEFD]->farside_pid = pid;
}
if (ANYDEBUG) {
cl_log(LOG_DEBUG, "read child process pid: %d", pid);
}
NewTrackedProc(pid, 0, PT_LOGVERBOSE, GINT_TO_POINTER(ourproc)
, &CoreProcessTrackOps);


if (mp->vf->close(mp) != HA_OK){
cl_log(LOG_ERR, "cannot close %s %s",
mp->type,
mp->name);
return HA_FAIL;
}
}




ourproc = procinfo->nprocs;
master_control_process();

/*NOTREACHED*/
cl_log(LOG_ERR, "master_control_process exiting?");
cleanexit(LSB_EXIT_GENERIC);
/*NOTREACHED*/
return HA_FAIL;
}

如何分析这个函数
...全文
326 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧