📄 mserver_sys_check_back.c
字号:
#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/sem.h>#include<sys/stat.h>#include <stdio.h>#include <unistd.h>#include<stdlib.h>#include<fcntl.h>#include<errno.h>#include<time.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <string.h> #include <errno.h>#include"socket.h"#include"M_check_Mstate_bk.h"#include"M_check_Astate_bk.h"#include"M_check_Bstate_bk.h"#include"write_log.h"#include"liansuo_count.h"#include"print.h"#define STATE_FILE "/zlm/state.log"#define OBFILE "/proc/stat"#define LEN 16348main(void){ /* semaphores */ struct sembuf buf_sem;#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including <sys/sem.h> */#else /* otherwise, according to X/OPEN we have to define it ourselves */ union semun { int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ unsigned short int *array; /* array for GETALL, SETALL */ struct seminfo *__buf; /* buffer for IPC_INFO */ };#endif union semun un; int flag = (IPC_CREAT | IPC_EXCL | 0660); int size = sizeof(struct area); key_t skey = 0x04030203; /* sample keys */ key_t mkey = 0x01020329; int semid; /* semaphore id */ int shmid; /* shared memory id */ int pid_Sys_check; /* Sys_check_state process child task pid */ int pid_server_cmd; /*Mserver_cmd process child task pid */ int pid_Mserver_A_getdata; int pid_Mserver_B_getdata; int pidT; char inbuf[80]; /* get a shared memory area */ shmid=shmget(mkey, size, flag); /* attach the shared memory area */ ap=(struct area *)shmat(shmid, 0, 0); /* initialize account info in shared memory */ ap->Mstate_gl=1; ap->Astate_gl=1; ap->Bstate_gl=1; ap->Amain_server_gl=1; ap->Bmain_server_gl=1; ap->Sys_main_cpu_gl=1; ap->Sys_main_hub_gl=1; ap->Amain_port_gl=8000; ap->Bmain_port_gl=8000; ap->Mhost_name_gl="192.168.1.1"; ap->Ahost_name_gl="192.168.1.3"; ap->Bhost_name_gl="192.168.1.5"; ap->flag_server_cmd=0; ap->flag_Sys_check=0; /* get and initialize a semaphore */ semid=semget(skey, 1, flag); /* set semaphore available */ un.val=1; semctl(semid, 0, SETVAL, un); /* first and only semaphore */ buf_sem.sem_num=0; /* wait if set *//* buf_sem.sem_flg=0; if((pidT=fork())==0) { char *argv[]={"Listing3.8b",(char *)0}; printf("\npidT : %d\n",getpid()); execv("/home/alpha/zlm/udp/chapter3/ch3/Listing3.8b",argv); }*//*if((pid_Mserver_A_getdata==fork())==0) { char *argv[]={"Mserver_A_getdata",(char *)0}; printf("\npid_Mserver_A_getdata is started up : %d\n",getpid()); execv("/zlm/Mserver_A_getdata",argv); }*/if((pid_Mserver_B_getdata=fork())==0)/*Mserver_B_gtdata*/ { char *argv[]={"Mserver_B_getdata",(char *)0}; printf("\npid_Mserver_B_getdata is started up : %d\n",getpid()); execv("/zlm/Mserver_B_getdata",argv); }if((pid_Sys_check=fork())==0) /* Sys_check_state */ { printf("pid for Sys_check_state: %d\n",getpid()); printf("\n...Now Starting System_Check_Programme...\n"); printf("Find the state_information in the file '/zlm/state.log'.\n"); /* attach parent's shared memory */ ap=(struct area *)shmat(shmid, 0, 0); /* note: shmid is still set correctly */ time_t timebuf; int len=strlen(ctime(&timebuf)); char state[LEN]={}; char *buf=malloc(sizeof(char)*(len+1)); time(&timebuf); strncpy(buf,ctime(&timebuf),len+1);//save the current time in the bufwhile(1){ if(ap->flag_Sys_check==-1) {break;}else{ /* set semaphore */ buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); char info[500]={}; char A_main_server[10]={}; char B_main_server[10]={}; char A_main_port[10]={}; char B_main_port[10]={}; char Asocket_info[800]={}; char Bsocket_info[800]={};//M machine check if(Mstate_check()==0) { ap->Mstate_gl=0; ap->Mhost_name_gl="127.0.0.1"; strcpy(info,"\nMstate=0,Mhost_name=127.0.0.1,Reboot the programme or machine\n"); //change the lpudated_log reboot M //update state_file exit(EXIT_FAILURE); } else { ap->Mstate_gl=1; ap->Mhost_name_gl="192.168.1.1"; strcpy(info,"\n\nInformation explain:\nMhost_name=192.168.1.1,Mstate=1"); }//check A machine //get_state(STATE_FILE); Astate_check(); strcpy(Asocket_info,check_error); strcat(info,"\nAhost_name="); strcat(info,ap->Ahost_name_gl); strcat(info,",Amain_server="); sprintf(A_main_server,"%d",ap->Amain_server_gl); strcat(info,A_main_server); strcat(info,",Amain_port="); sprintf(A_main_port,"%d",ap->Amain_port_gl); strcat(info,A_main_port); //check B machine Bstate_check(); strcpy(Bsocket_info,check_error); strcat(info,"\nBhost_name="); strcat(info,ap->Bhost_name_gl); strcat(info,",Bmain_server="); sprintf(B_main_server,"%d",ap->Bmain_server_gl); strcat(info,B_main_server); strcat(info,",Bmain_port="); sprintf(B_main_port,"%d",ap->Bmain_port_gl); strcat(info,B_main_port);//further check for Main_cpu Main_hub if((ap->Astate_gl==1)&&(ap->Bstate_gl==1)) { ap->Sys_main_cpu_gl=1; ap->Sys_main_hub_gl=1; strcat(info,"\nAstate=1,Bstate=1,Sys_main_cpu=1,Sys_main_hub=1\n"); } else if((ap->Astate_gl==1)&&(ap->Bstate_gl==0)) { ap->Sys_main_cpu_gl=1; ap->Sys_main_hub_gl=1; strcat(info,"\nAstate=1,Bstate=0,Sys_main_cpu=1,Sys_main_hub=1\n"); } else if((ap->Astate_gl==0)&&(ap->Bstate_gl==1)) { ap->Sys_main_cpu_gl=2; ap->Sys_main_hub_gl=1; strcat(info,"\nAstate=0,Bstate=1,Sys_main_cpu=2,Sys_main_hub=1\n"); } else { strcat(info,"\nAstate=0,Bstate=0,Sys_main_cpu=0,Sys_main_hub=2\n"); //Sys_main_hub_gl=2; }/*//further check for host_name if(Sys_main_hub_gl==2) { Ahost_name_gl="192.168.1.4"; Bhost_name_gl="192.168.1.6"; }*///get the check detail infomation strcat(info,"\nThe detail info:\n"); strcat(info,Asocket_info); strcat(info,A_info); strcat(info,Bsocket_info); strcat(info,B_info);//update the state file FILE *fp; fp=fopen("/zlm/state.log","w+"); if(fp==NULL) { printf("Error openning! for write_log()\n"); } else fputs(info,fp); fclose(fp); /* clear semaphore */ buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); sleep(1); }//else}//while(1)exit(0);//child normally exit}//if/**parent process*/ int i,fo; char cmd[80],cmd2[80]; char buffer[520]={};//check M if(ap->Mstate_gl==0) { printf("M_server error!\n"); exit(EXIT_FAILURE); } print(); while(i=getchar()) { switch(i) { case '1': { int M; char A_res[10]={},B_res[10]={},M_res[10]={}; printf("\ninput the command:"); scanf("%s%s",cmd,cmd2); strcat(cmd," "); strcat(cmd,cmd2); if((ap->Astate_gl==1)&&(ap->Bstate_gl==1)) { socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd); strcpy(A_res,check_error); socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd); strcpy(B_res,check_error); //compare the result now if(strcmp(A_res,B_res)==0) { if(socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n")==-1) { socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n"); } printf("A_res:%s is accepted\n",A_res); } else { M=liansuo_function(); sprintf(M_res,"%d",M); if(strcmp(A_res,M_res)==0) { socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n"); printf("A_res:%s is accepted\n",A_res); } else if(strcmp(B_res,M_res)==0) { printf("B_res:%s is accepted\n",B_res); socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n"); } } } else if((ap->Astate_gl==0)&&(ap->Bstate_gl==1)) { socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd); strcpy(B_res,check_error); M=liansuo_function(); sprintf(M_res,"%d",M); if(strcmp(B_res,M_res)==0) { printf("B_res:%s is accepted\n",B_res); socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n"); } else printf("重发命令\n"); } else if((ap->Astate_gl==1)&&(ap->Bstate_gl==0)) { socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd); strcpy(A_res,check_error); M=liansuo_function(); sprintf(M_res,"%d",M); if(strcmp(A_res,M_res)==0) { printf("A_res:%s is accepted\n",A_res); socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n"); } else printf("重发命令\n"); } else printf("两台都未启动\n"); print(); break; }//case1 case '2': { printf("\ninput the process name for call:"); scanf("%s%s",cmd,cmd2); strcat(cmd," "); strcat(cmd,cmd2); if((ap->Astate_gl==1)&&(ap->Bstate_gl==1)) { printf("与A B机通信成功!\n"); socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd); printf("%s\n",check_error); socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd); printf("%s\n",check_error); } else if((ap->Astate_gl==1)&&(ap->Bstate_gl==0)) { printf("与A机通信成功,B机需人工启动!\n"); socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd); printf("%s\n",check_error); } else if((ap->Astate_gl==0)&&(ap->Bstate_gl==1)) { printf("与B机通信成功,A机需人工启动!\n"); socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd); printf("%s\n",check_error); } else printf("与A B机通信都不成功,都需人工启动!\n"); print(); break; }//case2 case '0': { /* set semaphore */ buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->flag_Sys_check=-1; /* clear semaphore */ buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); break; } }//switch if(i=='0') break; }//while(i) waitpid(pid_Sys_check); /* remove shared memory, semaphore, and exit */ shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0); semctl(semid, 0, IPC_RMID, un); return;}//main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -