git-svn-id: http://172.17.0.253/svn/soft_pkgs/sys_nicmonitor@223 09c3743a-b0dd-45d3-b506-aa74c7a2a6ef
This commit is contained in:
jli
2010-10-12 00:09:47 +00:00
parent 498b17bb4d
commit 84006ba7b0
2 changed files with 138 additions and 50 deletions

View File

@@ -17,7 +17,7 @@
#define IPLEN 16
#define CONF_FILE "/conf/nic/sys_netcard_conf.txt"
#define MNIC_VERSION "1.2"
#define MNIC_VERSION "1.4"
#define NIC_UNKNOWN 0
#define NIC_DOWN 1
@@ -94,6 +94,7 @@ static char process_name[64];
char log_path[1024];
char shm_path[1024];
char sem_path[1024];
char conf_path[1024];
int host_name_size = 0;
char host_name[HOST_NAME_SIZE];
@@ -200,7 +201,7 @@ static void init_sem(int *semid, CONFIG_FILE_ST *ptr)
record_log(error_str);
return;
}
if(semctl(*semid, 0, SETVAL, 1) == -1){
if(semctl(*semid, 0, SETVAL, 0) == -1){
snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno));
record_log(error_str);
semctl(*semid, 0, IPC_RMID);
@@ -234,14 +235,18 @@ static void init_shm(SHM **ptr, CONFIG_FILE_ST *p)
static void get_sem(int semid)
{
struct sembuf lock;
struct sembuf lock[2];
char error_str[200];
lock.sem_num = 0;
lock.sem_op = -1;
lock.sem_flg = SEM_UNDO;
lock[0].sem_num = 0;
lock[0].sem_op = 0;
lock[0].sem_flg = SEM_UNDO;
lock[1].sem_num = 0;
lock[1].sem_op = 1;
lock[1].sem_flg = SEM_UNDO;
while(semop(semid, &lock, 1)){
while(semop(semid, lock, 2)){
if(errno == EINTR)continue;
snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno));
record_log(error_str);
@@ -256,7 +261,7 @@ static void release_sem(int semid)
struct sembuf unlock;
unlock.sem_num = 0;
unlock.sem_op = 1;
unlock.sem_op = -1;
unlock.sem_flg = SEM_UNDO;
if((ret = semop(semid, &unlock, 1)) == -1){
@@ -898,11 +903,10 @@ DOTCONF_CB(cb_str)
while(cmd->data.list[i]){
if((int)(inet_addr(cmd->data.list[i])) == -1){
sprintf(wrong_ip,"The wring IP address:'%s'",cmd->data.list[i]);
sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]);
strcat(wrong_ip,"\n");
record_log(wrong_ip);
} else {
} else{
strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i]));
}
i++;
@@ -976,20 +980,20 @@ DOTCONF_CB(cb_list)
int get_conf()
{
// int uid;
char buf[1024];
// char buf[1024];
// int i;
struct passwd *user;
// struct passwd *user;
configfile_t *configfile;
memset(buf, 0, sizeof(buf));
// memset(buf, 0, sizeof(buf));
// uid=getuid();
user = getpwnam("d5000");
// user = getpwnam("d5000");
// user = getpwuid(uid);
sprintf(buf,"%s", user->pw_dir);
strcat(buf,CONF_FILE);
// sprintf(buf,"%s", user->pw_dir);
// strcat(buf,conf_path);
configfile = dotconf_create(buf, options, NULL, CASE_INSENSITIVE);
configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE);
if (!configfile)
{
record_log("Warning can't reading config file\n");
@@ -1086,24 +1090,81 @@ int get_broad_ip(void)
return 0;
}
void create_logdir(void)
void create_dir(char *create_path)
{
int ret = 0;
int i,len = strlen(create_path);
//strcpy(log_path, LOG_PATH);
mode_t mask_tmp;
mask_tmp = umask(0000);
if(mkdir(log_path, 0777) != 0){
if(errno != EEXIST){
strcpy(log_path, "/tmp/");
len = strlen(create_path);
for(i=1; i<len; i++)
{
if(create_path[i]=='/')
{
create_path[i] = 0;
if( access(create_path, F_OK)!=0 )
{
if(mkdir(create_path, 0755)==-1)
{
perror("mkdir error");
}
}
create_path[i] = '/';
}
}
}
void create_semdir()
{
int ret = 0;
mode_t mask_tmp;
mask_tmp = umask(0000);
create_dir(sem_path);
if((ret = open(sem_path, O_RDWR | O_CREAT, 0666)) != -1){
close(ret);
}
}
umask(mask_tmp);
}
void create_shmdir()
{
int ret = 0;
mode_t mask_tmp;
mask_tmp = umask(0000);
create_dir(shm_path);
if((ret = open(shm_path, O_RDWR | O_CREAT, 0666)) != -1){
close(ret);
}
umask(mask_tmp);
}
void create_confdir()
{
int ret = 0;
mode_t mask_tmp;
mask_tmp = umask(0000);
create_dir(conf_path);
if((ret = open(conf_path, O_RDWR | O_CREAT, 0666)) != -1){
close(ret);
}
umask(mask_tmp);
}
void create_logdir()
{
int len = 0;
mode_t mask_tmp;
mask_tmp = umask(0000);
len = strlen(log_path);
if(log_path[len-1]!='/')
strcat(log_path, "/");
create_dir(log_path);
umask(mask_tmp);
}
/* create netcard name list */
int create_name_list(NET_NAME_ST *all_name_list, NET_NAME_ST *opt_name_list)
{
@@ -1237,14 +1298,6 @@ int main(int argc, char ** argv)
struct passwd *user;
// proc_invocation prcm;
user = getpwnam("d5000");
sprintf(log_path, "%s", user->pw_dir);
strcat(log_path, LOG_PATH);
sprintf(shm_path, "%s", user->pw_dir);
strcat(shm_path, SHM_PATH);
sprintf(sem_path, "%s", user->pw_dir);
strcat(sem_path, SEM_PATH);
memset(&conf, 0, sizeof(CONFIG_FILE_ST));
if(isrun(argv[0]) == 1){
printf("the program is already started!\n");
@@ -1267,8 +1320,22 @@ int main(int argc, char ** argv)
sigaction(SIGINT, &sig, NULL);
sigaction(SIGSEGV, &sig, NULL);
/* log file path */
/* create dir file path */
if((user = getpwnam("d5000")) != NULL){
sprintf(log_path,"%s",user->pw_dir);
sprintf(shm_path,"%s",user->pw_dir);
sprintf(sem_path,"%s",user->pw_dir);
sprintf(conf_path,"%s",user->pw_dir);
}
strcat(log_path,LOG_PATH);
strcat(shm_path,SHM_PATH);
strcat(sem_path,SEM_PATH);
strcat(conf_path,CONF_FILE);
create_semdir();
create_shmdir();
create_logdir();
create_confdir();
/* get process name */
memset(process_name, 0, sizeof(process_name));

View File

@@ -11,12 +11,13 @@
#include <sys/time.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include "nicinfo_shm.h"
#define SHM_PATH "/home/d5000/tmp/sys_netcard_shm_path"
#define SEM_PATH "/home/d5000/tmp/sys_netcard_sem_path"
#define LOG_PATH "/home/d5000/var/log/netcard/"
#define SHM_PATH "/tmp/sys_netcard_shm_path"
#define SEM_PATH "/tmp/sys_netcard_sem_path"
#define LOG_PATH "/var/log/netcard/"
typedef struct net_info{
NETCARD_INFO info[MAXNICNUM];
@@ -24,6 +25,8 @@ typedef struct net_info{
static int semid = 0;
static char log_path[1024];
static char shm_path[1024];
static char sem_path[1024];
SHM *ptr = NULL;
/* write error in logfile */
@@ -61,13 +64,17 @@ void record_log(char *str)
void get_sem(int semid)
{
char err_str[200];
struct sembuf lock;
struct sembuf lock[2];
lock.sem_num = 0;
lock.sem_op = -1;
lock.sem_flg = SEM_UNDO;
lock[0].sem_num = 0;
lock[0].sem_op = 0;
lock[0].sem_flg = SEM_UNDO;
while(semop(semid, &lock, 1)){
lock[1].sem_num = 0;
lock[1].sem_op = 1;
lock[1].sem_flg = SEM_UNDO;
while(semop(semid, lock, 2)){
if (errno == EINTR) {
continue;
}
@@ -84,7 +91,7 @@ void release_sem(int semid)
struct sembuf unlock;
unlock.sem_num = 0;
unlock.sem_op = 1;
unlock.sem_op = -1;
unlock.sem_flg = SEM_UNDO;
if((ret = semop(semid, &unlock, 1)) == -1){
@@ -103,7 +110,7 @@ int init_nic_info(void)
key_t key;
if ((key=ftok(SEM_PATH, SEM_PROJ_ID)) == -1) {
if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) {
snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno));
record_log(err_str);
return -1;
@@ -113,6 +120,7 @@ int init_nic_info(void)
record_log(err_str);
return -1;
}
/*
if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno));
record_log(err_str);
@@ -127,8 +135,9 @@ int init_nic_info(void)
return -1;
}
}
if((fd = open(SHM_PATH, O_RDONLY)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n", SHM_PATH, strerror(errno));
*/
if((fd = open(shm_path, O_RDONLY)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno));
record_log(err_str);
return -1;
}
@@ -151,10 +160,20 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
char *s;
char buf[128];
char err_str[200];
struct passwd *user;
tamp = time(NULL);
localtime_r(&tamp, &tmptr);
strcpy(log_path, LOG_PATH);
if((user = getpwnam("d5000"))!= NULL){
sprintf(log_path,"%s",user->pw_dir);
sprintf(shm_path,"%s",user->pw_dir);
sprintf(sem_path,"%s",user->pw_dir);
}
strcat(log_path,LOG_PATH);
strcat(shm_path,SHM_PATH);
strcat(sem_path,SEM_PATH);
if((dir = opendir(log_path)) == NULL){
if(errno == ENOENT){
strcpy(log_path, "/tmp/");
@@ -172,15 +191,17 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
if((ret = init_nic_info()) == -1){
return -1;
}
get_sem(semid);
for(i = 0; ptr->info[i].charname[0] != 0; i++){
if(!strcmp(ptr->info[i].charname, nic_name)){
get_sem(semid);
// get_sem(semid);
memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO));
release_sem(semid);
munmap(ptr, sizeof(SHM));
return 0;
}
}
release_sem(semid);
snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name);
record_log(err_str);
munmap(ptr, sizeof(SHM));