void select_loop(int server_s) { FD_ZERO(&block_read_fdset); FD_ZERO(&block_write_fdset); /* set server_s and req_timeout */ req_timeout.tv_sec = (ka_timeout ? ka_timeout : REQUEST_TIMEOUT); req_timeout.tv_usec = 0l; /* reset timeout */ /* preset max_fd */ max_fd = -1; while (1) { if (sighup_flag) sighup_run(); if (sigchld_flag) sigchld_run(); if (sigalrm_flag) sigalrm_run(); if (sigterm_flag) { if (sigterm_flag == 1) sigterm_stage1_run(server_s); if (sigterm_flag == 2 && !request_ready && !request_block) { sigterm_stage2_run(); } } /* reset max_fd */ max_fd = -1; if (request_block) /* move selected req's from request_block to request_ready */ fdset_update(); /* any blocked req's move from request_ready to request_block */ process_requests(server_s); if (!sigterm_flag && total_connections < (max_connections - 10)) { BOA_FD_SET(server_s, &block_read_fdset); /* server always set */ #ifdef SERVER_SSL if (do_sock < 2) BOA_FD_SET(server_ssl, &block_read_fdset); /* server always set */ #endif } req_timeout.tv_sec = (request_ready ? 0 : (ka_timeout ? ka_timeout : REQUEST_TIMEOUT)); req_timeout.tv_usec = 0l; /* reset timeout */ if (select(max_fd + 1, &block_read_fdset, &block_write_fdset, NULL, (request_ready || request_block ? &req_timeout : NULL)) == -1) { /* what is the appropriate thing to do here on EBADF */ if (errno == EINTR) continue; /* while(1) */ else if (errno != EBADF) { DIE("select"); } } time(¤t_time); if (FD_ISSET(server_s, &block_read_fdset)) pending_requests = 1; } }
void loop(int server_s) { FD_ZERO(BOA_READ); FD_ZERO(BOA_WRITE); max_fd = -1; while (1) { /* handle signals here */ if (sighup_flag) sighup_run(); if (sigchld_flag) sigchld_run(); if (sigalrm_flag) sigalrm_run(); if (sigterm_flag) { /* sigterm_flag: * 1. caught, unprocessed. * 2. caught, stage 1 processed */ if (sigterm_flag == 1) { sigterm_stage1_run(); BOA_FD_CLR(req, server_s, BOA_READ); close(server_s); /* make sure the server isn't in the block list */ server_s = -1; } if (sigterm_flag == 2 && !request_ready && !request_block) { sigterm_stage2_run(); /* terminal */ } } else { if (total_connections > max_connections) { /* FIXME: for poll we don't subtract 20. why? */ BOA_FD_CLR(req, server_s, BOA_READ); } else { BOA_FD_SET(req, server_s, BOA_READ); /* server always set */ } } pending_requests = 0; /* max_fd is > 0 when something is blocked */ if (max_fd) { struct timeval req_timeout; /* timeval for select */ req_timeout.tv_sec = (request_ready ? 0 : default_timeout); req_timeout.tv_usec = 0l; /* reset timeout */ if (select(max_fd + 1, BOA_READ, BOA_WRITE, NULL, (request_ready || request_block ? &req_timeout : NULL)) == -1) { /* what is the appropriate thing to do here on EBADF */ if (errno == EINTR) continue; /* while(1) */ else if (errno != EBADF) { DIE("select"); } } /* FIXME: optimize for when select returns 0 (timeout). * Thus avoiding many operations in fdset_update * and others. */ if (!sigterm_flag && FD_ISSET(server_s, BOA_READ)) { pending_requests = 1; } time(¤t_time); /* for "new" requests if we've been in * select too long */ /* if we skip this section (for example, if max_fd == 0), * then we aren't listening anyway, so we can't accept * new conns. Don't worry about it. */ } /* reset max_fd */ max_fd = -1; if (request_block) { /* move selected req's from request_block to request_ready */ fdset_update(); } /* any blocked req's move from request_ready to request_block */ if (pending_requests || request_ready) { process_requests(server_s); } } }
void select_loop(int server_s) { FD_ZERO(&block_read_fdset); FD_ZERO(&block_write_fdset); /* set server_s and req_timeout */ req_timeout.tv_sec = (ka_timeout ? ka_timeout : REQUEST_TIMEOUT); req_timeout.tv_usec = 0l; /* reset timeout */ /* preset max_fd */ max_fd = -1; while (1) { if (sighup_flag) sighup_run(); if (sigchld_flag) sigchld_run(); if (sigalrm_flag) sigalrm_run(); if (sigterm_flag) { if (sigterm_flag == 1) sigterm_stage1_run(server_s); if (sigterm_flag == 2 /*&& !request_ready && !request_block*/) { sigterm_stage2_run(); } } /* reset max_fd */ max_fd = -1; if (request_block) { /* move selected req's from request_block to request_ready */ //fprintf(stderr, "fdsetupdated\n"); fdset_update(); } /* any blocked req's move from request_ready to request_block */ process_requests(server_s); if (!sigterm_flag && total_connections < (max_connections - 10) #if defined(TCSUPPORT_WEBSERVER_SSL) && http_mode != HTTPS_ONLY #endif ) { BOA_FD_SET(server_s, &block_read_fdset); } #if defined(TCSUPPORT_WEBSERVER_SSL) if (!sigterm_flag && total_connections < (max_connections - 10) && http_mode != HTTP_ONLY) { BOA_FD_SET(server_ssl, &block_read_fdset); } #endif req_timeout.tv_sec = (request_ready ? 0 : (ka_timeout ? ka_timeout : REQUEST_TIMEOUT)); req_timeout.tv_usec = 0l; /* reset timeout */ if (select(max_fd + 1, &block_read_fdset, &block_write_fdset, NULL, (request_ready || request_block ? &req_timeout : NULL)) == -1) { /* what is the appropriate thing to do here on EBADF */ if (errno == EINTR) { continue; /* while(1) */ } else if (errno != EBADF) { DIE("select"); } } time(¤t_time); if (FD_ISSET(server_s, &block_read_fdset)) { pending_requests = 1; } #if defined(TCSUPPORT_WEBSERVER_SSL) if (FD_ISSET(server_ssl, &block_read_fdset)) { ssl_pending_requests = 1; } #endif } }
void loop(int server_s) { struct pollfd pfd1[2][MAX_FD]; short which = 0, other = 1, temp; int server_pfd, watch_server; pfds = pfd1[which]; pfd_len = server_pfd = 0; watch_server = 1; while (1) { int timeout; time(¤t_time); if (sighup_flag) sighup_run(); if (sigchld_flag) sigchld_run(); if (sigalrm_flag) sigalrm_run(); if (sigterm_flag) { if (sigterm_flag == 1) { sigterm_stage1_run(); close(server_s); server_s = -1; /* remove server_pfd */ { unsigned int i, j; for(i = 0, j = 0;i < pfd_len;++i) { if (i == (unsigned) server_pfd) continue; pfd1[other][j].fd = pfd1[which][j].fd; pfd1[other][j].events = pfd1[which][j].events; ++j; } pfd_len = j; pfds = pfd1[other]; temp = other; other = which; which = temp; } watch_server = 0; } if (sigterm_flag == 2 && !request_ready && !request_block) { sigterm_stage2_run(); } } else { if (total_connections < max_connections) { server_pfd = pfd_len++; pfds[server_pfd].fd = server_s; pfds[server_pfd].events = BOA_READ; watch_server = 1; } else { watch_server = 0; } } /* If there are any requests ready, the timeout is 0. * If not, and there are any requests blocking, the * timeout is ka_timeout ? ka_timeout * 1000, otherwise * REQUEST_TIMEOUT * 1000. * -1 means forever */ pending_requests = 0; if (pfd_len) { timeout = (request_ready ? 0 : (request_block ? default_timeout : -1)); if (poll(pfds, pfd_len, timeout) == -1) { if (errno == EINTR) continue; /* while(1) */ } if (!sigterm_flag && watch_server) { /* */ if (pfds[server_pfd].revents & (POLLNVAL|POLLERR)) { /* problem with the server socket, unexpected */ log_error("server pfd revent contains " "POLLNVAL or POLLERR! Exiting."); exit(1); } else if (pfds[server_pfd].revents & BOA_READ) { pending_requests = 1; } } time(¤t_time); /* if pfd_len is 0, we didn't poll, so the current time * should be up-to-date, and we *won't* be accepting anyway */ } /* go through blocked and unblock them if possible */ /* also resets pfd_len and pfd to known blocked */ pfd_len = 0; if (request_block) { update_blocked(pfd1[other]); } /* swap pfd */ pfds = pfd1[other]; temp = other; other = which; which = temp; /* process any active requests */ process_requests(server_s); } }
void loop(int server_s) { FD_ZERO(BOA_READ); FD_ZERO(BOA_WRITE); max_fd = -1; while (1) { /* handle signals here */ if (sighup_flag) sighup_run(); if (sigchld_flag) sigchld_run(); if (sigalrm_flag) sigalrm_run(); if (sigterm_flag) { /* sigterm_flag: * 1. caught, unprocessed. * 2. caught, stage 1 processed */ if (sigterm_flag == 1) { sigterm_stage1_run(); BOA_FD_CLR(req, server_s, BOA_READ); close(server_s); /* make sure the server isn't in the block list */ server_s = -1; } if (sigterm_flag == 2 && !request_ready && !request_block) { sigterm_stage2_run(); /* terminal */ } } else { if (total_connections > max_connections) { /* FIXME: for poll we don't subtract 20. why? */ BOA_FD_CLR(req, server_s, BOA_READ); } else { BOA_FD_SET(req, server_s, BOA_READ); /* server always set */ } } if (isREBOOTASP == 1) { if(last_req_after_upgrade != req_after_upgrade){ last_req_after_upgrade = req_after_upgrade; } } pending_requests = 0; /* max_fd is > 0 when something is blocked */ if (max_fd) { struct timeval req_timeout; /* timeval for select */ req_timeout.tv_sec = (request_ready ? 0 : default_timeout); req_timeout.tv_usec = 0l; /* reset timeout */ if (select(max_fd + 1, BOA_READ, BOA_WRITE, NULL, (request_ready || request_block ? &req_timeout : NULL)) == -1) { /* what is the appropriate thing to do here on EBADF */ if (errno == EINTR) { //fprintf(stderr,"####%s:%d isFWUPGRADE=%d isREBOOTASP=%d###\n", __FILE__, __LINE__ ,isFWUPGRADE , isREBOOTASP); //fprintf(stderr,"####%s:%d last_req_after_upgrade=%d req_after_upgrade=%d confirm_last_req=%d###\n", __FILE__, __LINE__ ,last_req_after_upgrade , req_after_upgrade, confirm_last_req); if (isFWUPGRADE !=0 && isREBOOTASP == 1 ) { if (last_req_after_upgrade == req_after_upgrade) confirm_last_req++; if (confirm_last_req >3) goto ToUpgrade; } else if(isCFGUPGRADE ==2 && isREBOOTASP == 1 ) { goto ToReboot; } else if (isFWUPGRADE ==0 && isREBOOTASP == 1) { if (last_req_after_upgrade == req_after_upgrade) confirm_last_req++; if (confirm_last_req >3) { isFWUPGRADE = 0; isREBOOTASP = 0; //isFAKEREBOOT = 0; confirm_last_req=0; } } continue; /* while(1) */ } else if (errno != EBADF) { DIE("select"); } } /* FIXME: optimize for when select returns 0 (timeout). * Thus avoiding many operations in fdset_update * and others. */ if (!sigterm_flag && FD_ISSET(server_s, BOA_READ)) { pending_requests = 1; } time(¤t_time); /* for "new" requests if we've been in * select too long */ /* if we skip this section (for example, if max_fd == 0), * then we aren't listening anyway, so we can't accept * new conns. Don't worry about it. */ } /* reset max_fd */ max_fd = -1; if (request_block) { /* move selected req's from request_block to request_ready */ fdset_update(); } /* any blocked req's move from request_ready to request_block */ if (pending_requests || request_ready) { if (isFWUPGRADE !=0 && isREBOOTASP == 1 ){ req_after_upgrade++; }else if(isFWUPGRADE ==0 && isREBOOTASP == 1){ req_after_upgrade++; } process_requests(server_s); continue; } ToUpgrade: if (isFWUPGRADE !=0 && isREBOOTASP == 1 ) { char buffer[200]; //fprintf(stderr,"\r\n [%s-%u] FirmwareUpgrade start",__FILE__,__LINE__); FirmwareUpgrade(firmware_data, firmware_len, 0, buffer); //fprintf(stderr,"\r\n [%s-%u] FirmwareUpgrade end",__FILE__,__LINE__); //system("echo 7 > /proc/gpio"); // disable system LED isFWUPGRADE=0; isREBOOTASP=0; //reboot_time = 5; break; } ToReboot: if(isCFGUPGRADE == 2 && isREBOOTASP ==1) { isCFGUPGRADE=0; isREBOOTASP=0; system("reboot"); for(;;); } } }