📄 scan.cpp
字号:
&& active_channel[index].end_flag==1) { sprintf(channel_no,"%d",active_channel[index].channel_id); if(killChn(channel_no)==1) { currentTime = time(NULL); localTime = localtime(¤tTime); sprintf(timeStr,"%d:%d:%d",localTime->tm_hour,localTime->tm_min,localTime->tm_sec); dbg_print2(COMMON_LOG,"STOP NACTIVE program on channel %s at %s.\n",channel_no,timeStr); printf("Stop channel %s\n",channel_no); } // end if } // end if } // end for updateStatusFile(fname_status); tm.tv_sec=SCAN_INTERVAL; tm.tv_usec=0; ret = select(0,NULL,NULL,NULL,&tm); if ( ret<0 ) { printf("receive SIGALRM signal or other.\n"); } else if ( ret==0 ) { printf("select timeout\n"); } // scan actually not active process and kill them for(index=0;index<MAXCHANNELNUM;index++) { if ( active_channel[index].end_flag==0 ) { //active_channel pid_t flag; int statloc; // bandwidth is number, so channel is realtime channel if( active_channel[index].type_flag!=FILE_CH ) continue; flag = -1; if ( active_channel[index].channel_id>0 ) { flag = waitpid(active_channel[index].pid, &statloc,WNOHANG); } if ( flag>0 || flag==-1 ) { // child quit or no child sprintf(channel_no,"%d",active_channel[index].channel_id); killChn(channel_no); currentTime = time(NULL); localTime = localtime(¤tTime); sprintf(timeStr,"%d:%d:%d",localTime->tm_hour,localTime->tm_min,localTime->tm_sec); dbg_print2(COMMON_LOG,"KILL Deadchild on channel %s at %s.\n",channel_no,timeStr); } // end kill not activate process }// end if } // end for }//while(1){} void initChnTable(){ int i; for(i=0;i<MAXCHANNELNUM;i++) { active_channel[i].channel_id=-1; active_channel[i].pid=0; active_channel[i].type_flag = NULL_CH; active_channel[i].end_flag=1; } return;}int createChn(char * channel_id, char * program_ip, char * program_port, char * channel_ip, char * channel_port,char * band_width){ int rc , child_pid,statloc,flag,first_empty; int i; for(i=0;i<MAXCHANNELNUM;i++) if ( active_channel[i].end_flag==0 && (active_channel[i].channel_id==atoi(channel_id)) ) { printf("The channel %s is running, NOT need to creat.\n",channel_id); return 0; } // find first empty item for(i=0;i<MAXCHANNELNUM;i++) if(active_channel[i].channel_id == -1){ first_empty = i; break; }// sleep(1); rc = fork(); if(rc == (pid_t) -1){ printf("fork error.\n"); return 0; } else if(rc > 0){ child_pid = rc; /* sleep(1); flag = waitpid(child_pid,&statloc,WNOHANG); printf("waitpid return %d",flag); if(flag==0) printf("fail to create child.\n"); */ active_channel[first_empty].channel_id = atoi(channel_id); active_channel[first_empty].pid = child_pid; active_channel[first_empty].end_flag=0; for(i=0;i<strlen(band_width);i++) if(band_width[i]<'0'||band_width[i]>'9') break; if(i==strlen(band_width)){ // bandwidth is realtime channel active_channel[first_empty].type_flag = REALTIME_CH; } else { active_channel[first_empty].type_flag = FILE_CH; } printf("pid=%d channel_id=%d\n",active_channel[first_empty].pid,active_channel[first_empty].channel_id); return 1; } else if(rc == 0){ char progIp[128],progPort[128],chnIp[128],chnPort[128],chnId[128],bandWidth[128],localIP[128]; char err_log[128]; sprintf(err_log,"log=err%s",channel_id); sprintf(chnId,"channel_id=%s",channel_id); //sprintf(progIp,"program_ip=%s",program_ip); sprintf(progIp,"program_ip=192.168.0.84"); sprintf(progPort,"program_port=%s",program_port); sprintf(chnIp,"channel_ip=%s",channel_ip); sprintf(chnPort,"channel_port=%s",channel_port); sprintf(localIP, "lip=%s", lip); for(i=0;i<strlen(band_width);i++) if(band_width[i]<'0'||band_width[i]>'9') break; if(i==strlen(band_width)){ sprintf(bandWidth,"band=%f",atof(band_width));// execl("./vodserver","vodserver",progIp,progPort,chnIp,chnPort,chnId,localIP,bandWidth,err_log,"debug=fatal",(char *)0); exit(0); } else{ sprintf(progPort, "lport=0"); strcpy(bandWidth,"configfile="); strcat(bandWidth,band_width); execl(fname_vodfile,fname_vodfile,chnIp,chnPort,chnId,localIP,progPort,bandWidth,err_log,"debug=debug",(char *)0); exit(0); } } return 0;}int killChn(char * channel_id){ for(int i=0;i<MAXCHANNELNUM;i++) if(active_channel[i].channel_id == atoi(channel_id)){ kill(active_channel[i].pid,SIGQUIT); active_channel[i].channel_id=-1; active_channel[i].end_flag=1; active_channel[i].type_flag=NULL_CH; active_channel[i].pid=0; return 1; } return 0; }int updateStatusFile(char *filename){ FILE *fp; fp = fopen(filename,"w+"); if(fp == NULL){ printf("open status file error.\n"); return 0; } for(int i=0;i<MAXCHANNELNUM;i++) if(active_channel[i].channel_id != -1){ fprintf(fp,"channel_id=%d pid=%d\n",active_channel[i].channel_id,active_channel[i].pid); } fclose(fp); return 1; } double CountSecond(char *s) {//turn the XX:xx:xx to xxxx seconds int hour,minute,second; hour=atoi(s); minute=atoi(strchr(s,':')+1); second=atoi(strrchr(s,':')+1); return (hour*3600+minute*60+second); } void recvSign(int signo){ return;}int get(char *name,char *s){ char str[128]; fscanf(fp,"%s",str); if(!strcmp(str,name)){ fscanf(fp,"%s",s); if (!strcmp(name,"source_ip=")){ if(INADDR_NONE==inet_addr(s)){ printf("the source_ip is invalid\n"); return 0; } return 1; }else if(!strcmp(name, "source_port=")){ if ((atoi(s))<0||(atoi(s))>9999){ printf("the source_port is invalid\n"); return 0; } return 1; }else if(!strcmp(name, "target_ip=")){ if(INADDR_NONE==inet_addr(s)){ printf("the target_ip is invalid"); return 0; } return 1; }else if(!strcmp(name, "target_port=")){ if ((atoi(s))<0||(atoi(s))>9999){ printf("the target_port is invalid"); return 0; } return 1; }else if(!strcmp(name, "start_time=")){ return 1; }else if(!strcmp(name, "end_time=")){ return 1; }else if(!strcmp(name, "channel_no=")){ if (atoi(s)<0){ printf("the channel_no is invalid"); return 0; } return 1; }else if( !strcmp(name, "bandwidth=")){ if (atof(s)<0){ printf("the bandwidth is invalid"); return 0; } }else if( !strcmp(name, "describe=")){ return 1; }else if( !strcmp(name, "v_pid=")){ return 1; }else if( !strcmp(name, "a_pid=")){ return 1; }else if( !strcmp(name, "mode=")){ return 1; }else if( !strcmp(name, "type=")){ return 1; } return 1; } return 0;}/* A function to collect dead children before they become zombies... Also, *//* It keeps track of how many children are around... */static void child_quit( int signo ) { int oerrno = errno, wval, wstatus; // wyj need add block SIGCHLD Sigset_t set = sigblock(SIGCHLD); for (;;) { wval = waitpid(-1, &wstatus, WNOHANG); switch (wval) { case -1: if (errno == EINTR) continue; case 0: // wyj need add unblock SIGCHLD sigunblock(set); errno = oerrno; return; default: return; } // end switch }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -