ll_index; do { if ( ppool->sub_process[p_idx].pid != -1 ) { break; } p_idx = ( p_idx + 1 ) % ppool->cur_process_num; } while ( p_idx != roll_index ); //roll polling all the child process,but they are //all run error.so p_idx equals to roll_index. if ( ppool->sub_process[p_idx].pid < 0 ) { ppool->stop = 1; break; } roll_index = ( p_idx + 1 ) % ppool->cur_process_num; if ( Send( ppool->sub_process[p_idx].pipefd[0], (char *)&onebyte, 1, 0 ) < 0 ) { PRINTINFO( LEVEL5, "Send error!!!" ); ppool->stop = 1; break; } } //receive signal from signal handler. else if ( (event_fd == sig_pipefd[1]) && (events[i].events & EPOLLIN) ) { PRINTINFO( LEVEL0, "event:parent receive signal" ); int sig; char signals[1024]; ret = recv( sig_pipefd[1], signals, sizeof(signals), 0 ); if ( ret <= 0 ) { continue; } else { parent_signal_handle( ppool, signals, ret ); } } } } _free_source: free( events ); events = NULL; close( ppool->epollfd ); } void run( processpool *ppool ) { if ( ppool->index != -1 ) { run_child( ppool ); return; } run_parent( ppool ); }
client_handle.h:
#ifndef _CLIENT_HANDLE_H #define _CLIENT_HANDLE_H #include
#include
#include
#include
#include
#include
#include
#include
#define MAXRECVBUF 1024 typedef struct client_param { //用于recv返回出错移除sockfd int epollfd; int sockfd; struct sockaddr_in addr; char buf[ MAXRECVBUF ]; } client_param; void client_param_init( client_param *cparam, int connfd, struct sockaddr_in *addr ); void client_handle( client_param *cparam ); #endif
client_handle.c:
#include "client_handle.h" #include "util.h" void client_param_init( client_param *cparam, int connfd, struct sockaddr_in *addr ) { if ( !cparam ) { PRINTINFO( LEVEL4, "cparam is null!!!" ); return; } cparam->sockfd = connfd; memcpy( &cparam->addr, addr, sizeof(struct sockaddr_in) ); memset( cparam->buf, 0, sizeof(cparam->buf) ); } void client_handle( client_param *cparam ) { int i, ret; while ( 1 ) { memset( cparam->buf, 0, sizeof(cparam->buf) ); ret = recv( cparam->sockfd, cparam->buf, sizeof(cparam->buf), 0 ); if ( ret < 0 ) { if ( errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR ) { Epoll_del_fd( cparam->epollfd, cparam->sockfd ); } close( cparam->sockfd ); break; } else if ( 0 == ret ) { Epoll_del_fd( cparam->epollfd, cparam->sockfd ); close( cparam->sockfd ); break; } else { if ( ret < 15 ) { close( cparam->sockfd ); break; } //PRINTINFO( LEVEL0, "child receive buf:\n%s", cparam->buf ); fflush(stdout); char *p_get = strstr( cparam->buf, "GET" ); if ( !p_get ) { close( cparam->sockfd ); break; } char *p_http = strstr( cparam->buf, "HTTP" ); if ( !p_http ) { close( cparam->sockfd ); break; } cparam->buf[ret] = '\0'; //GET filename HTTP/1.1 ..... char file_name[20] = {0}; int file_name_len = p_http - p_get - 6; memcpy( file_name, p_get + 5, file_name_len ); if ( access( file_name, F_OK ) == -1 ) { PRINTINFO( LEVEL3, "file:(%s) dosen't exist!!", file_name ); Epoll_del_fd( cparam->epollfd, cparam->sockfd ); close( cparam->sockfd ); break; } PRINTINFO( LEVEL0, "file name:%s--", file_name ); ret = fork(); if ( ret == -1 ) { Epoll_del_fd( cparam->epollfd, cparam->sockfd ); close( cparam->sockfd ); break; } else if ( ret > 0 ) { Epoll_del_fd( cparam->epollfd, cparam->sockfd ); close( cparam->sockfd ); break; } else { close( STDOUT_FILENO ); //relocate the stdou to sockfd PRINTINFO( LEVEL0, "sockfd:%d", cparam->sockfd ); dup( cparam->sockfd ); //printf("likun\n"); execl( file_name, file_name, NULL ); fflush(stdout); Epoll_del_fd( cparam->epollfd, cparam->sockfd ); close( cparam->sockfd ); exit( 0 ); } } } }
cgisrv.c:
#include "myhshappool.h" #include "client_handle.h" #include "util.h" #include "epoll_wrapper.h" //进程池最大数量 #define MAXPROCESSNUMBER 16 //每个进程支持客户连接任务 #define USERPERPROCESS 65535 //epool最大支持的监听事件数 #define MAXEPOLLEVENT 10000 pr