版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ldhomeapachehttpdserverlsmaincmain.cuxlncxhomeapachehttpdserverconstcharconstargv{arconfnameSERVERCONFIGFILEhardefserverrootHTTPDROOTartemperrorlogNULLrorcessrecprocessrconfltpgloballtpconfaprpooltplogPooloflogstreams,reset_after_eachreadofconf*/aprpooltptempPoolfortemporaryconfigstuff,resetoften*/aprpooltpcommands*Poolfor-D,-Cand-cswitches*/topttoptatustrvmodule*mod;targAPR_OPTIONAL_FN_TYPE(ap_signal_server)*signal_server;AP_MONCONTROL(0);/*turnoffprofilingofstartup*/processinitprocessargc&argv);pglobalprocesspool;pconfprocesspconf;serverargvprocessshortnameHARSETEBCDICif(ap_init_ebcdic(pglobal)!=APR_SUCCESS){destroyandexitprocessprocess1);}poolcreatepcommandspglobalrpooltagpcommandspcommandsapserverprereadconfigaprarraymakepcommands1,sizeof(char*));apserverpostreadconfigaprarraymakepcommands1,sizeof(char*));apserverconfigdefinesaprarraymakepcommands1,sizeof(char*));tupprelinkedmodulesprocessif(error){aplogerrorAPLOGMARKAPLOGSTARTUPAPLOGEMERG0,NULL,"%s:%s",erverargverrordestroyandexitprocessprocess1);}rewriteargsprocessMaintainAPSERVERBASEARGSlistinhttp_main.htoallowtheMPM*tosafelypassonourargsfromitsrewrite_args()handler.aprgetoptinitoptpcommandsprocessargc,process->argv);while((rv=apr_getopt(opt,AP_SERVER_BASEARGS,&c,&optarg))==APR_SUCCESS){switch(c){newchar)apr_array_push(ap_server_post_read_config);newaprpstrduppcommandsoptarg);newchar)apr_array_push(ap_server_pre_read_config);newaprpstrduppcommandsoptarg);rootoptargnewchar)apr_array_push(ap_server_config_defines);newaprpstrduppcommandsoptarg);SettingDDUMP_VHOSTSisequivalenttosetting-S*/optargDUMPVHOSTSconfigtestonly=1;SettingDDUMP_MODULESisequivalenttosetting-M*/optargDUMPMODULESconfigtestonly=1;if(strcasecmp(optarg,"emerg")==0){aultloglevelAPLOGEMERG}elseif(strcasecmp(optarg,"alert")==0){aultloglevelAPLOGALERT}elseif(strcasecmp(optarg,"crit")==0){aultloglevelAPLOGCRIT}elseif(strncasecmp(optarg,"err",3)==0){aultloglevelAPLOGERR}elseif(strncasecmp(optarg,"warn",4)==0){aultloglevelAPLOGWARNING}elseif(strcasecmp(optarg,"notice")==0){aultloglevelAPLOGNOTICE}elseif(strcasecmp(optarg,"info")==0){apdefaultloglevelAPLOGINFO}elseif(strcasecmp(optarg,"debug")==0){aultloglevelAPLOGDEBUG}else{geprocess}perrorlogaprpstrdupprocesspooloptargnewchar)apr_array_push(ap_server_config_defines);ewDEBUGargprintfServerversionsn",ap_get_server_description());printfServerbuilts\n",ap_get_server_built());troyandexitprocessprocessngstroyandexitprocessprocessodulestroyandexitprocessprocessdirectivestroyandexitprocessprocessconfigtestonly=1;configtestonly=1;newchar)apr_array_push(ap_server_config_defines);wDUMPVHOSTSconfigtestonly=1;newchar)apr_array_push(ap_server_config_defines);wDUMPMODULESgeprocess}}badcmdlineoption?thenwedie*/if(rv!=APR_EOF||opt->ind<opt->argc){geprocess}poolcreateplogpglobalprpooltagplogplogpoolcreateptemppconfprpooltagptempptempNotethatwepreflighttheconfigfileonce*beforereadingit_again_inthemainloop.hisallowsthingslogfilesconfiguration*forexample,tosettledown.rverrootdefserverrootif(temp_error_log){apreplacestderrlogprocesspooltemp_error_log);}//*===========下面这一段是对配置文件进行处理==================*/erconfapreadconfigprocessptempconfnameapconftreeif(!server_conf){destroyandexitprocessprocess1);}if(ap_run_pre_config(pconf,plog,ptemp)!=OK){aplogerrorAPLOGMARKAPLOG_STARTUP|APLOG_ERR,0,NULL"Pre-configurationfailed");destroyandexitprocessprocess1);}rvapprocessconfigtreeserver_conf,ap_conftree,rocesspconfptempif(rv==OK){apfixupvirtualhostspconfserverconfapfinivhostconfigpconfserverconfooksortallif(configtestonly){untestconfigpconfserverconfaplogerrorAPLOGMARKAPLOGSTARTUPNULL,"SyntaxOK");destroyandexitprocessprocess;}}======上面这一段是对配置文件进行处理==================*/rverAPRRETRIEVEOPTIONALFNapsignalserverif(signal_server){if(signal_server(&exit_status,pconf)!=0){oyandexitprocessprocessexitstatus}}Ifourconfigfailed,dealwiththathere.*/if(rv!=OK){destroyandexitprocessprocess1);}lclearplogif(ap_run_open_logs(pconf,plog,ptemp,server_conf)!=OK){plogerrorAPLOGMARKAPLOGSTARTUPAPLOGERRNULLUnabletoopenlogs");destroyandexitprocessprocess1);}if(ap_run_post_config(pconf,plog,ptemp,server_conf)!=OK){aplogerrorAPLOGMARKAPLOG_STARTUP|APLOG_ERR,0,NULL"ConfigurationFailed");destroyandexitprocessprocess1);}ldestroyptempforfor(;;){ookderegisteralllclearpconffor(mod=ap_prelinked_modules;*mod!=NULL;mod++){egisterhooksmodpconf}Thisisahackuntilwefinishthecodesothatitonlyreads*theconfigfileonceandjustoperatesonthetreealreadyinmoryrbbreeNULLpoolcreateptemppconfprpooltagptempptemprverrootdefserverroot//*===========下面这一段是对配置文件进行处理,每次循环都要处理==================*/confapreadconfigprocessptempconfnameapconftreeif(!server_conf){destroyandexitprocessprocess1);}if(ap_run_pre_config(pconf,plog,ptemp)!=OK){plogerrorAPLOGMARKAPLOGSTARTUPAPLOGERRULLPreconfigurationfaileddestroyandexitprocessprocess1);}figtreeserverconfapconftreeprocesspconfptemp)!=OK){destroyandexitprocessprocess1);}apfixupvirtualhostspconfserverconfapfinivhostconfigpconfserverconfooksortalllclearplogif(ap_run_open_logs(pconf,plog,ptemp,server_conf)!=OK){plogerrorAPLOGMARKAPLOGSTARTUPAPLOGERRNULLUnabletoopenlogs");destroyandexitprocessprocess1);}if(ap_run_post_config(pconf,plog,ptemp,server_conf)!=OK){plogerrorAPLOGMARKAPLOGSTARTUPAPLOGERRLLConfigurationFaileddestroyandexitprocessprocess1);}======上面这一段是对配置文件进行处理,每次循环都要处理==================*/ldestroyptempaprpoollock(pconf,1);optionalfnretrieveaprpoollockpconf父父进程的这个循环,每次都会重新读入配置文件进行处理。然后调用ap_mpm_run()重新生成子进程。aprpoollockpconftroyandexitprocessprocessreturn/*Termination'ok'*/}mpmrunaprpooltpconfaprpooltplogserverrecs{tostartatustrvapap_log_pid(pconf,ap_pid_fname);tserverlimitserverlimitif(changed_limit_at_restart){aplog_error(APLOG_MARK,APLOG_WARNING,0,s,WARNING:AttempttochangeServerLimit"restartangedlimitatrestart}Initializecrossprocessacceptlock*/aplockfnameaprpsprintfpconfs.%"APR_PID_T_FMT,pserverrootrelativepconfaplockfnamervrv=apr_proc_mutex_create(&accept_mutex,ap_lock_fname,ceptlockmechpconfif(ap_accept_lock_mech==APR_LOCK_SYSVSEM){rvunixdsetprocmutexpermsacceptmutex);}if(!is_graceful){if(ap_run_pre_mpm(s->process->pool,SB_SHARED)!=OK){mpmstate=AP_MPMQ_STOPPING;return1;}fixthegenerationnumberintheglobalscore;wejustgotanew,edscoreboardapscoreboardimageglobalrunninggenerationapmygeneration;}Apachectlrestart,apachectlstop,apachectlgraceful,这些命令将会导致信号处理函数设定hutdownpendingestartpendingeisgracefulifif(one_process){APMONCONTROL);make_child(ap_server_conf,0);}else{ifapdaemonsmaxfreeapdaemonsminfreeDontthrash..*/ap_daemons_max_free=ap_daemons_min_free+1;*Ifwe'redoingagraceful_restartthenwe'regoingtoseealotofchildrenexitingimmediatelywhenwegetintothemainloopbelowbecausewejustsentthemAPSIG_GRACEFUL).Thishappensprettyrapidlyandforeachonethatexitswe'llstartanewoneuntilwereachatleastdaemonsminfreeButwemaybepermittedtostartmorethanthatsowelljustkeeptrackofhowmanywe'resupposedtostartupwithoutthesecondpenaltybetweeneachfork.remainingchildrentostartap_daemons_to_start;if(remaining_children_to_start>ap_daemons_limit){remainingchildrentostart=ap_daemons_limit;}if(!is_graceful){renremainingchildrentostartremainingchildrentostart=0;}else{givethesystemsometimetorecoverbeforekickingintontialmodehold_off_on_exponential_spawning=10;}aplogerrorAPLOGMARKAPLOGNOTICE0,ap_server_conf,uredresumingnormaloperationstserverdescriptionaplogerrorAPLOGMARKAPLOGINFO0,ap_server_conf,erbuiltsapgetserverbuiltdefAPMPMWANTSETACCEPTLOCKMECHaplogerrorAPLOGMARKAPLOGDEBUG0,ap_server_conf,utexsdefaultsocmutexnameacceptmutexrocmutexdefnamerestartpendingshutdown_pending=0;mpmstateAP_MPMQ_RUNNING;while(!restart_pending&&!shutdown_pending){:这里每隔一秒就往返一次。一直进行不停itwhyeexitwhytusthisisamemoryleak,butI'llfixitlater.*/ctpidapap_wait_or_timeout(&exitwhy,&status,&pid,pconf);XXXifittakeslongerthan1secondforallourchildrentostartupandgetintoIDLEstatethenwemayspawnanildif(pid.pid!=-1){processedstatusapprocesschildstatus&pid,exitwhy,status);if(processed_status==APEXIT_CHILDFATAL){mpmstate=AP_MPMQ_STOPPING;return1;}nonfataldeathnotethatit'sgoneinthescoreboard.*/slotfindchildbypidpidif(child_slot>=0){tSERVERDEADif(processed_status==APEXIT_CHILDSICK){childdetectedaresourceshortage(E[NM]FILE,ENOBUFS,etc)uttheforkratetotheminimumwnrate}emainingchildrentostart&&child_slot<ap_daemons_limit){werestilldoinga1-for-1replacementofdeadrenwithnewchildrenmake_child(ap_server_conf,child_slot);remainingchildrentostart;}HASOTHERCHILD}elseif(apr_proc_other_child_alert(&pid,APR_OC_REASON_DEATH,status)==APR_SUCCESS){handled/}elseif(is_graceful){Greatwe'veprobablyjustlostaslotinthescoreboardSomehowwedon'tknowaboutthis*child.gerrorAPLOGMARKAPLOGWARNINGrverconflonglostchildcamehomepidldlongpidpid}Dontperformidlemaintenancewhenachilddies,*onlydoitwhenthere'satimeout.Rememberonlyafinitenumberofchildrencandieandit'sprettyathologicalforalottodiesuddenly}elseif(remaining_children_to_start){wehitasecondtimeoutinwhichnoneofthepreviousgenerationofchildrenneededtobereaped...soassume*they'realldone,andpickuptheslackifanyisleft.renremainingchildrentostartremainingchildrentostart=0;InanyeventwereallyshouldntdothecodebelowbecausefewoftheserverswejuststartedareintheIDLEstateyetsowedmistakenlycreateanextraserver.}rformidleservermaintenancepconf它主要进行子进程的检查。循环的条件是由信号处理,以及对子进行检查的结果来决定的。在启用apachectlrestart命令后,父进程收到SIGHUPreceived.Attemptingto}}/*one_process*/mpmstate=AP_MPMQ_STOPPING;if(shutdown_pending&&!is_graceful){Timetoshutdown*Killchildprocesses,tellthemtocallchild_exit,etc...if(unixd_killpg(getpgrp(),SIGTERM)<0){aplogerrorAPLOGMARKAPLOGWARNINGerrnoapserver_conf,"killpgSIGTERM");}apreclaimchildprocesses/*StartwithSIGTERM*/cleanuppidfileonnormalshutdown*/{pidfileNULLpidfileapserverrootrelative(pconf,ap_pid_fname);dfileNULLunlinkpidfileaplogerrorAPLOGMARKAPLOGINFO,rverconfmovedPIDfilespidldpidfilelonggetpid());}aplogerrorAPLOGMARKAPLOGNOTICE0,ap_server_conf,TERMshuttingdownreturn1;}elseif(shutdown_pending){Timetoperformagracefulshutdown:Reaptheinactivechildrenandasktheactiveonestoclosetheirlistenersthenwaituntiltheyare*alldonetoexit.aprtimetcutoff0;Stoplistening*/selistenerskillofftheidleones*/apmpmpodkillpgpodapmaxdaemons_limit);SendSIGUSRtotheactivechildren*/activechildrenfor(index=0;index<ap_daemons_limit;++index){if(ap_scoreboard_image->servers[index][0].status!=SERVER_DEAD){Askeachchildtocloseitslisteners.*/apmpmsafekillMPMCHILDPIDindexAPSIGGRACEFUL);hildren}}Alloweachchildwhichactuallyfinishedtoexit*/evechildprocessescleanuppidfile*/{pidfileNULLpidfileapserverrootrelative(pconf,ap_pid_fname);dfileNULLunlinkpidfileaplogerrorAPLOGMARKAPLOGINFO,rverconfmovedPIDfilespidldpidfilelonggetpid());}aplogerrorAPLOGMARKAPLOGNOTICE0,ap_server_conf,aughtAPSIGGRACEFULSTOPSTRINGshuttingdowngracefullyif(ap_graceful_shutdown_timeout){cutoffaprtimenow()+prtimefromsecapgracefulshutdowntimeout}Dontreallyexituntileachchildhasfinished*/ndingdo{Pauseforasecond*/Relieveanychildrenwhichhavenowexited*/evechildprocessesactivechildrenfor(index=0;index<ap_daemons_limit;++index){if(ap_mpm_safe_kill(MPM_CHILD_PID(index),0)==APR_SUCCESS){active_children=1;Havingjustonechildisenoughtostayaround*/}}shutdownpendingactivechildrenoffWemightbeherebecausewereceivedSIGTERM,eitherwaytryandmakesurethatallofourprocessesare*reallydead.unixdkillpggetpgrpSIGTERM);return1;}wevebeentoldtorestart*/aprsignalSIGHUPSIGIGNaprsignalAPSIGGRACEFULSIGIGN);if(one_process){notworththinkingabout*/return1;}advancetothenextgeneration*//*XXX:wereallyneedtomakesurethisnewgenerationnumberisn'tin*usebyanyofthechildren.nerationapscoreboardimageglobalrunninggenerationapmy_generation;if(is_graceful){aplogerrorAPLOGMARKAPLOGNOTICE0,ap_server_conf,startrequesteddoingrestartkillofftheidleones*/apmpmpodkillpgpodapmaxdaemons_limit);Thisismostlyfordebugging...sothatweknowwhatisstillgracefullydealingwithexistingrequestThiswillbreakinaverynastywayifweeverhavethescoreboardtotallylebasednosharedmemoryfor(index=0;index<ap_daemons_limit;++index){boardimageserversindexstatusSERVERDEADapscoreboardimageserversindexstatus=SERVER_GRACEFUL;/*Askeachchildtocloseitslisteners.*NOTEweusethescoreboardbecauseifwesendSIGUSR1toeveryprocessinthegroupthismayincludeCGI's,pipedloggersetcTheyalmostcertainlywon'thandleefullyapmpmsafekillapscoreboardimageparentindexpidAP_SIG_GRACEFUL);}}}else{Killemoff*/if(unixd_killpg(getpgrp(),SIGHUP)<0){aplogerrorAPLOGMARKAPLOGWARNINGerrnoapserver_conf,"killpgSIGHUP");}apreclaimchildprocesses/*Notwhenjuststartingup*/aplogerrorAPLOGMARKAPLOGNOTICE0,ap_server_conf,eivedAttemptingtorestart}return}这这个child_main()函数是重中之重!!!!ildmainintchildnumarg{ltptransocatortallocatoratuststatustenreclrlsettpollsethandletsbhbucketalloctbucketallocmpm_state=AP_MPMQ_STARTING;/*forbenefitofanyhooksthatrunasthisdinitializesmychild_num=child_num_arg;mypidgetpidrequeststhischild=0;apfatalsignalchildsetupapserverconfGetasubcontextforglobalallocationsinthischild,sothat*wecanhavecleanupsoccurwhenthechildexits.locatorcreateallocatoraprallocatormaxfreesetallocatorapmaxmemfree);aprpoolcreateexpchildpconfNULLallocator);allocatorownersetallocatorpchildpoolcreateptranspchildrpooltagptranstransactionneedstobedonebeforeweswitchUIDssowehavepermissions*/apreopenscoreboardpchildNULL);usaprprocmutexchildinitacceptmutexaplockfnamepchildif(status!=APR_SUCCESS){aplogerrorAPLOGMARKAPLOGEMERGstatus,ap_server_conf,sdaplockfnameapacceptlockmech;exitAPEXITCHILDFATAL}if(unixd_setup_child()){exitAPEXITCHILDFATAL}aprunchildinitpchildapserverconf;apcreatesbhandlesbhpchildmy_child_num,0);equestrecNULLSetupthepollfdarray*/#checkthestatus*/ockspchildfor(lr=ap_listeners,i=num_listensocks;i--;lr=lr->next){aprpollfdtpfd={0};pfddesctypeAPRPOLL_SOCKET;pfddescs=lr->sd;dreqeventsAPRPOLLINpfd.client_data=lr;#checkthestatus*/}mpmstateAP_MPMQ_RUNNING;bucketallocaprbucketalloc_create(pchild);dienowissetwhenAP_SIG_GRACEFULisreceivedinthechild;shutdownpendingissetwhenSIGTERMisreceivedwhenrunninginsingleprocessmode.*/while(!die_now&&!shutdown_pending){urrentconnsd*(Re)initializethischildtoapre-connectionstate.aprpoolclearptrans;第一次执行axrequestsperchild&&requests_this_child++>=ap_max_requests_per_child)){ldexit}equestrecNULL*Waitforanacceptableconnectiontoarrive.Lockaround"accept",ifnecessary*/FEACCEPTacceptmutexonif(num_listensocks==1){Thereisonlyonelistenerrecordsorefertothatone.*/steners}multiplelisteningsockets-needtopoll*/for(;;){ttnumdescollfdtpdesctimeout=-1==waitforever*/usaprpollsetpollpollsetnumdescpdescif(status!=APR_SUCCESS){if(APR_STATUS_IS_EINTR(status)){if(one_process&&shutdown_pending){}elseif(die_now){Ingracefulstoprestartdropthemutexandterminatethechild.*/FEACCEPTacceptmutexoffldexit}}SingleUnixdocumentsselectasreturningerrnosEBADFEINTRandEINVAL...andinnoneofthosecasesdoesitmakesensetocontinue.InfactonLinuxxweseemtoendupwithEFAULT*occasionally,andwe'dloopforeverduetoit.logerrorAPLOGMARKAPLOGERRstatusapserverconfaprpollsetpolllisten);FEACCEPTacceptmutexoffldexit}Wecanalwaysusepdesc0],butsocketsatpositionNcouldendupcompletelystarvedofattentioninaverybusyserverTherefore,weround-robinacrossthereturnedsetofdescriptorsWhileitispossiblethatthereturnedsetofdescriptorsmightfliparoundandcontinuetostarvesomesockets,wehappentoknowtheternalpollsetimplementationretainsorderingstabilityofthesocketsThus,theround-robinshouldensurethatasocketwilleventuallybeservicedcGrabalistenerrecordfromtheclient_dataofthepolldescriptorandadvanceoursavedindextoround-robinxtfetch*hmmthisdescriptormighthavePOLLERRratheranPOLLINollidxclientdatafd}}*ifweaccept()somethingwedon'twanttodie,sowehavetofertheexitstatuslracceptfunccsdlrptrans);子进程会在这里等待。如果有访问,就会执行SAFEACCEPTacceptmutexoff;/*unlockafter"accept"*/if(status==APR_EGENERAL){resourceshortageorshould-not-occuroccured*/ldexit}elseif(status!=APR_SUCCESS){}Wenowhaveaconnectionsosetitupwiththeappropriate*socketoptions,filedescriptors,andread/writebuffers.if(current_conn){}/*CheckthepodandthegenerationnumberafterprocessingaconnectionsothatwellgoawayifagracefulrestartoccurredwhilewewereprocessingtheconnectionorwearetheluckydleserverprocessthatgetstodiempmpodcheckpodAPRSUCCESSselectedasidledienow=1;}elseif(ap_my_generation!=apscoreboardimageglobalrunninggeneration){/*restart?*/yeahthiscouldbenon-gracefulrestart,inwhichcasetheparentwillkillussoonenoughbutwhybotherchecking?dienow=1;}}ldexit}RedHatEnterpriseLinuxASrelease3(TaroonUpdate4)kernel7.ELglibc95.30apache.53Serverversi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年技术开发外包合同
- 协议买房合同范本
- 《微细加工技术》课件
- 17难忘的泼水节公开课一等奖创新教学设计
- 1《故都的秋》 公开课一等奖创新教学设计统编版高中语文必修上册
- 活动策划简历新人培训
- 年产xx燃料油添加剂项目建议书
- 中小学校岗位安全工作
- 年产xx动物胶项目建议书
- 年产xx光固化机项目建议书
- 追觅在线测评28题
- (新版)糖尿病知识竞赛考试题库300题(含答案)
- 五年级上册小数四则混合运算练习300道及答案
- (2024)全国交管12123学法减分必考题库和答案(完整版)
- 第四章 光现象章节练习2024-2025学年人教版八年级物理上册
- 2024年教资考试时政高频考点141条
- 2024年中国商用VOIP语音系统市场调查研究报告
- 2024数智化绿色低碳管理体系要求
- 2024-2025学年七年级语文上册第四单元测试卷(统编版2024新教材)
- 《土地整治学》期末考试复习题库资料(含答案)
- DL∕T 325-2010 电力行业职业健康监护技术规范
评论
0/150
提交评论