ha_status.c
上传用户:bilang918
上传日期:2010-03-24
资源大小:558k
文件大小:5k
- #include "./common/common.h"
- //#include "./common/sybfunc.c"
- #include "./common/commonfunc.c"
- struct ha *shm_p;
- struct sockaddr_in tx_addr,rx_addr,tx_addrb,rx_addrb;
- int sockrxfd, socktxfd,socktxfdb,sockrxfdb;
- char tempstr[100];
- char errtxt[1024];
- struct ha* SetShm(struct ha* shm_pTMP,char *KeyPath,char KeyId)
- {
- key_t key;
- int shmid;
-
- key = ftok(KeyPath, KeyId);
- printf("nKEYID:%c,key:%dn",KeyId,key);
- shmid = shmget(key, sizeof(struct ha), IPC_CREAT|PERMS);
- if(shmid < 0) {
- printf("n shmid < 0n");
- exit(1);
- }
- shm_pTMP = shmat(shmid, 0, 0);
- if(shm_pTMP < 0) {
- printf("n shm_p < 0");
- exit(-1);
- }
- memset(shm_pTMP, 0, sizeof(struct ha));
- return(shm_pTMP);
- }
- void release_shm(struct ha* shm_pTMP,char *KEYPATH,char KEYID)
- {
- struct shmid_ds shm_desc;
- key_t key;
- int shmid;
- key = ftok(KEYPATH, KEYID);
- shmid = shmget(key, sizeof(struct ha), 0);
- if(shmid < 0) {
- perror("release_shm: shmid:");
- }
- if (shmdt(shm_pTMP) == -1){
- perror("release_shm: shmdt:");
- }
- if (shmctl(shmid,IPC_RMID,&shm_desc) == -1){
- perror("release_shm: shmctl:");
- }
- }
- void house_keeper(int sig_num){
- printf("nClearing share memory and malloced memory.n");
- release_shm(shm_p,".",'j');
- printf("nNow exit safely.n");
- exit (0);
- }
- void catch_alarm(int sig_num)
- {
- alarm(10);
- shm_p->status = 0;
- printf("ncheck point.n");
- }
- main(int argc,char **argv)
- {
- unsigned char buffer[100];
- char cluster_ip[20];
- char host_ip[20];
- char content[20];
- int pid,pid1;
- char host_flag[10];
-
- signal(SIGINT, house_keeper);
- bzero(tempstr,100);
- bzero(cluster_ip,20);
- if(argc==4){
- strcpy(tempstr,argv[1]);
- strcpy(host_ip,argv[2]);
- strcpy(host_flag,argv[3]);
- if(strlen(tempstr)==0) exit(0);
- }else if (argc!=4){
- printf("nusage:ha_status <cluster ip addr> <another host ip> <host A/B>n");
- exit (0);
- }
- strcpy(cluster_ip,tempstr);
- shm_p=SetShm(shm_p,".",'j');
- pid = fork();
- if (pid==0) pid1 = fork();
- if ((pid==0)&& (pid1>0)){
- if((sockrxfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- exit(-1);
- }
- bzero(&rx_addr, sizeof(rx_addr));
- rx_addr.sin_family = AF_INET;
- rx_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- rx_addr.sin_port = htons(8250);
- if(bind(sockrxfd, (struct sockaddr *)&rx_addr, sizeof(rx_addr)) < 0) {
- exit(-1);
- }
- bzero(buffer,100);
-
- for(;;){
- signal(SIGALRM, catch_alarm);
- alarm(10);
- bzero(buffer, 100);
- if(recvfrom(sockrxfd, buffer, 100, 0, (struct sockaddr *)0, (int *)0) < 0){
- continue;
- }
- if(buffer[0]=='m'){
- shm_p->status = 1;
- }
- printf("nprocess1: receiving heartbeat SIGn");
- }
- }
- if ((pid > 0) && (pid1 >0)){
- bzero(content,20);
- strcpy(content,"m");
- socktxfd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&tx_addr,sizeof(struct sockaddr_in));
- tx_addr.sin_family=AF_INET;
- tx_addr.sin_port=htons(8250);
- tx_addr.sin_addr.s_addr=htonl(INADDR_ANY);
- if(inet_aton(cluster_ip,&tx_addr.sin_addr)<0){
- printf("Ip error. n");
- exit(1);
- }
- printf("create sockt okn");
- socktxfdb=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&tx_addrb,sizeof(struct sockaddr_in));
- tx_addrb.sin_family=AF_INET;
- tx_addrb.sin_port=htons(8300);
- tx_addrb.sin_addr.s_addr=htonl(INADDR_ANY);
- if(inet_aton(host_ip,&tx_addrb.sin_addr)<0){
- printf("Ip error. n");
- exit(1);
- }
- printf("create sockt okn");
- for(;;){
- sendto(socktxfd,content,strlen(content),0,(struct sockaddr*)&tx_addr,sizeof(struct sockaddr_in));
- if (shm_p->status == 1) sendto(socktxfdb,content,strlen(content),0,(struct sockaddr*)&tx_addrb,sizeof(struct sockaddr_in));
- printf("nprocess2: sending heartbeat SIGn");
- sleep(1);
- }
- }
- if ((pid==0)&&(pid1==0)){
- if((sockrxfdb = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- exit(-1);
- }
- bzero(&rx_addrb, sizeof(rx_addrb));
- rx_addrb.sin_family = AF_INET;
- rx_addrb.sin_addr.s_addr = htonl(INADDR_ANY);
- rx_addrb.sin_port = htons(8300);
- if(bind(sockrxfdb, (struct sockaddr *)&rx_addrb, sizeof(rx_addrb)) < 0) {
- exit(-1);
- }
- bzero(buffer,100);
-
- for(;;){
- bzero(buffer, 100);
- if(recvfrom(sockrxfdb, buffer, 100, 0, (struct sockaddr *)0, (int *)0) < 0){
- continue;
- }
- if((buffer[0]=='m')&&(shm_p->status == 1)){
- if (host_flag[0]=='A'){
- sleep(5);
- printf("nrelease cluster ipn");
- system("/sbin/ifconfig eth0:1 192.168.15.111");
- }
- if (host_flag[0]=='B'){
- printf("nrelease cluster ipn");
- system("/sbin/ifconfig eth0:1 192.168.15.222");
- }
- }
- printf("nprocess1: receiving heartbeat SIGn");
- }
- }
- }
-