185 lines
3.8 KiB
C
185 lines
3.8 KiB
C
/*************************************************
|
||
File name: cstamain.c
|
||
Author: JianHui Zheng
|
||
Version: 9:00:00
|
||
Date: 2007-7-2
|
||
Description:cstaCollector模块的main文件,该文件可以使模块独立运行
|
||
调用外部函数
|
||
void iptrans_init();
|
||
void snmp_init(int port);
|
||
调用csta.c文件函数
|
||
void csta_init();
|
||
|
||
History:
|
||
No.
|
||
Author
|
||
Date
|
||
Version
|
||
Description
|
||
*************************************************/
|
||
#include <assert.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <stddef.h>
|
||
|
||
#include "csta.h"
|
||
|
||
static struct itimerval itimer, old_itimer;
|
||
|
||
void onCstaCollectorTimer();
|
||
void setCstaCollectorTimer();
|
||
void setup_daemon(void);
|
||
int timerCounter = 0;
|
||
|
||
/*************************************************
|
||
Function: // main
|
||
Description: // main函数,可以使模块独立运行
|
||
Calls: // iptrans_init; snmp_init; csta_init; setCstaCollectorTimer;
|
||
Called By: // csta.c:csta_init;
|
||
Table Accessed: //
|
||
Table Updated: //
|
||
Input: // int argc, char *argv[]
|
||
Output: //
|
||
Return: //
|
||
Others: //
|
||
*************************************************/
|
||
int main(int argc, char *argv[])
|
||
{
|
||
char runMode;
|
||
|
||
runMode = getopt(argc, argv, "d");
|
||
|
||
//获取运行参数
|
||
switch (runMode)
|
||
{
|
||
case 'd':
|
||
setup_daemon();
|
||
break;
|
||
case '?':
|
||
break;
|
||
}
|
||
|
||
//IP平台模块初始化
|
||
fprintf(stderr, "\n Calling iptrans_init() \n");
|
||
iptrans_init();
|
||
|
||
//snmp模块初始化
|
||
fprintf(stderr, "\n Calling snmp_init() \n");
|
||
snmp_init(4957);
|
||
|
||
//cstaCollector模块初始化
|
||
fprintf(stderr, "\n Calling csta_init() \n");
|
||
csta_init();
|
||
|
||
//设定定时器
|
||
setCstaCollectorTimer();
|
||
while (1) {
|
||
usleep(50);
|
||
}
|
||
|
||
return (1);
|
||
}
|
||
|
||
/*************************************************
|
||
Function: // onCstaCollectorTimer
|
||
Description: // 设定定时器函数
|
||
Calls: // csta_timer;
|
||
Called By: // setCstaCollectorTimer;
|
||
Table Accessed: //
|
||
Table Updated: //
|
||
Input: // int argc; char *argv[];
|
||
Output: //
|
||
Return: //
|
||
Others: //
|
||
*************************************************/
|
||
void onCstaCollectorTimer()
|
||
{
|
||
csta_timer();
|
||
}
|
||
|
||
/*************************************************
|
||
Function: // setCstaCollectorTimer
|
||
Description: // 开启定时器函数
|
||
Calls: // setitimer;
|
||
Called By: // main;
|
||
Table Accessed: //
|
||
Table Updated: //
|
||
Input: //
|
||
Output: //
|
||
Return: //
|
||
Others: //
|
||
*************************************************/
|
||
void setCstaCollectorTimer()
|
||
{
|
||
struct sigaction act;
|
||
act.sa_handler = onCstaCollectorTimer;
|
||
sigemptyset(&act.sa_mask);
|
||
act.sa_flags = 0;
|
||
if (sigaction(SIGALRM, &act, NULL) < 0) {
|
||
perror("Produce Sigaction");
|
||
exitLog("cstaCollector:setCstaCollectorTimer, sigaction function error");
|
||
exit(1);
|
||
}
|
||
|
||
itimer.it_interval.tv_sec = 0;
|
||
itimer.it_interval.tv_usec = 10 * 1000;
|
||
itimer.it_value.tv_sec = 0;
|
||
itimer.it_value.tv_usec = 10 * 1000;
|
||
|
||
if (setitimer(ITIMER_REAL, &itimer, &old_itimer) != 0) {
|
||
printf("Setting Timer error! \n");
|
||
exitLog("cstaCollector:setCstaCollectorTimer, setitimer function error");
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
/*************************************************
|
||
Function: // setup_daemon
|
||
Description: // 将程序设为后台进程
|
||
Calls: // fork;
|
||
Called By: // main;
|
||
Table Accessed: //
|
||
Table Updated: //
|
||
Input: //
|
||
Output: //
|
||
Return: //
|
||
Others: //
|
||
*************************************************/
|
||
void setup_daemon(void)
|
||
{
|
||
|
||
//复制进程
|
||
switch (fork())
|
||
{
|
||
case -1:
|
||
{
|
||
perror("setup_daemon(), 1st fork()");
|
||
exit(2);
|
||
}
|
||
default:
|
||
exit(0);
|
||
case 0:
|
||
if (setsid() == -1)
|
||
{
|
||
perror("setup_daemon(), setsid()");
|
||
exit(3);
|
||
}
|
||
switch (fork())
|
||
{
|
||
case -1:
|
||
{
|
||
perror("setup_daemon(), 2nd fork()");
|
||
exit(3);
|
||
}
|
||
|
||
default:
|
||
exit(0);
|
||
case 0:
|
||
umask(0);
|
||
/* and return with daemon set up */
|
||
return;
|
||
}
|
||
}
|
||
}
|