📄 cgi_main.c
字号:
if (pt) { efree(pt); } } else { /* make sure path_info/translated are empty */ script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME",script_path_translated TSRMLS_CC); _sapi_cgibin_putenv("PATH_INFO", NULL TSRMLS_CC); _sapi_cgibin_putenv("PATH_TRANSLATED", NULL TSRMLS_CC); } SG(request_info).request_uri = sapi_cgibin_getenv("SCRIPT_NAME",0 TSRMLS_CC); } else {#endif /* pre 4.3 behaviour, shouldn't be used but provides BC */ if (env_path_info) { SG(request_info).request_uri = env_path_info; } else { SG(request_info).request_uri = env_script_name; }#if !DISCARD_PATH if (env_path_translated) script_path_translated = env_path_translated;#endif#if ENABLE_PATHINFO_CHECK }#endif SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD",0 TSRMLS_CC); SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING",0 TSRMLS_CC); /* some server configurations allow '..' to slip through in the translated path. We'll just refuse to handle such a path. */ if (script_path_translated && !strstr(script_path_translated, "..")) { SG(request_info).path_translated = estrdup(script_path_translated); } SG(request_info).content_type = (content_type ? content_type : "" ); SG(request_info).content_length = (content_length?atoi(content_length):0); /* The CGI RFC allows servers to pass on unvalidated Authorization data */ auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION",0 TSRMLS_CC); php_handle_auth_data(auth TSRMLS_CC); }}/* }}} */static void define_command_line_ini_entry(char *arg){ char *name, *value; name = arg; value = strchr(arg, '='); if (value) { *value = 0; value++; } else { value = "1"; } zend_alter_ini_entry(name, strlen(name)+1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);}static void php_register_command_line_global_vars(char **arg TSRMLS_DC){ char *var, *val; var = *arg; val = strchr(var, '='); if (!val) { printf("No value specified for variable '%s'\n", var); } else { *val++ = '\0'; php_register_variable(var, val, NULL TSRMLS_CC); } efree(*arg);}#if PHP_FASTCGI/** * Clean up child processes upon exit */void fastcgi_cleanup(int signal){#ifdef DEBUG_FASTCGI fprintf( stderr, "FastCGI shutdown, pid %d\n", getpid() );#endif#ifndef PHP_WIN32 sigaction( SIGTERM, &old_term, 0 ); /* Kill all the processes in our process group */ kill( -pgroup, SIGTERM );#endif /* We should exit at this point, but MacOSX doesn't seem to */ exit( 0 );}#endif/* {{{ main */int main(int argc, char *argv[]){ int exit_status = SUCCESS; int cgi = 0, c, i, len; zend_file_handle file_handle = {0}; int retval = FAILURE; char *s;/* temporary locals */ int behavior=PHP_MODE_STANDARD; int no_headers=0; int orig_optind=php_optind; char *orig_optarg=php_optarg; char *script_file=NULL; zend_llist global_vars; int interactive=0;#if FORCE_CGI_REDIRECT long force_redirect = 1; char *redirect_status_env = NULL;#endif/* end of temporary locals */#ifdef ZTS zend_compiler_globals *compiler_globals; zend_executor_globals *executor_globals; php_core_globals *core_globals; sapi_globals_struct *sapi_globals; void ***tsrm_ls;#endif#if PHP_FASTCGI int max_requests = 500; int requests = 0; int fastcgi = !FCGX_IsCGI();#ifndef PHP_WIN32 char *bindpath = NULL;#endif int fcgi_fd = 0; FCGX_Request request;#ifdef PHP_WIN32 long impersonate = 0;#else int status = 0;#endif#endif /* PHP_FASTCGI */#ifdef HAVE_SIGNAL_H#if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so that sockets created via fsockopen() don't kill PHP if the remote site closes it. in apache|apxs mode apache does that for us! thies@thieso.net 20000419 */#endif#endif#ifdef ZTS tsrm_startup(1, 1, 0, NULL);#endif sapi_startup(&cgi_sapi_module);#ifdef PHP_WIN32 _fmode = _O_BINARY; /*sets default for file streams to binary */ setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */ setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */ setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */#endif#if PHP_FASTCGI if (!fastcgi) {#endif /* Make sure we detect we are a cgi - a bit redundancy here, but the default case is that we have to check only the first one. */ if (getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) { cgi = 1; }#if PHP_FASTCGI }#endif if (!cgi#if PHP_FASTCGI /* allow ini override for fastcgi */#endif ) { while ((c=php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0))!=-1) { switch (c) { case 'c': cgi_sapi_module.php_ini_path_override = strdup(php_optarg); break; case 'n': cgi_sapi_module.php_ini_ignore = 1; break;#if PHP_FASTCGI#ifndef PHP_WIN32 /* if we're started on command line, check to see if we are being started as an 'external' fastcgi server by accepting a bindpath parameter. */ case 'b': if (!fastcgi) { bindpath = strdup(php_optarg); } break;#endif#endif } } php_optind = orig_optind; php_optarg = orig_optarg; }#ifdef ZTS compiler_globals = ts_resource(compiler_globals_id); executor_globals = ts_resource(executor_globals_id); core_globals = ts_resource(core_globals_id); sapi_globals = ts_resource(sapi_globals_id); tsrm_ls = ts_resource(0); SG(request_info).path_translated = NULL;#endif cgi_sapi_module.executable_location = argv[0]; /* startup after we get the above ini override se we get things right */ if (php_module_startup(&cgi_sapi_module, NULL, 0) == FAILURE) {#ifdef ZTS tsrm_shutdown();#endif return FAILURE; }#if FORCE_CGI_REDIRECT /* check force_cgi after startup, so we have proper output */ if (cfg_get_long("cgi.force_redirect", &force_redirect) == FAILURE) { force_redirect = 1; } if (cgi && force_redirect) { if (cfg_get_string("cgi.redirect_status_env", &redirect_status_env) == FAILURE) { redirect_status_env = NULL; } /* Apache will generate REDIRECT_STATUS, * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS. * redirect.so and installation instructions available from * http://www.koehntopp.de/php. * -- kk@netuse.de */ if (!getenv("REDIRECT_STATUS") && !getenv ("HTTP_REDIRECT_STATUS") /* this is to allow a different env var to be configured in case some server does something different than above */ && (!redirect_status_env || !getenv(redirect_status_env)) ) { SG(sapi_headers).http_response_code = 400; PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\<p>This PHP CGI binary was compiled with force-cgi-redirect enabled. This\n\means that a page will only be served up if the REDIRECT_STATUS CGI variable is\n\set, e.g. via an Apache Action directive.</p>\n\<p>For more information as to <i>why</i> this behaviour exists, see the <a href=\"http://php.net/security.cgi-bin\">\manual page for CGI security</a>.</p>\n\<p>For more information about changing this behaviour or re-enabling this webserver,\n\consult the installation file that came with this distribution, or visit \n\<a href=\"http://php.net/install.windows\">the manual page</a>.</p>\n");#ifdef ZTS tsrm_shutdown();#endif return FAILURE; } }#endif /* FORCE_CGI_REDIRECT */#if ENABLE_PATHINFO_CHECK if (cfg_get_long("cgi.fix_pathinfo", &fix_pathinfo) == FAILURE) { fix_pathinfo = 0; }#endif#if PHP_FASTCGI#ifndef PHP_WIN32 /* for windows, socket listening is broken in the fastcgi library itself so dissabling this feature on windows till time is available to fix it */ if (bindpath) { /* this must be done to make FCGX_OpenSocket work correctly bug 23664 */ close(0); /* Pass on the arg to the FastCGI library, with one exception. * If just a port is specified, then we prepend a ':' onto the * path (it's what the fastcgi library expects) */ if (strchr(bindpath, ':') == NULL) { char *tmp; tmp = malloc(strlen(bindpath) + 2); tmp[0] = ':'; memcpy(tmp + 1, bindpath, strlen(bindpath) + 1); fcgi_fd = FCGX_OpenSocket(tmp, 128); free(tmp); } else { fcgi_fd = FCGX_OpenSocket(bindpath, 128); } if( fcgi_fd < 0 ) { fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath);#ifdef ZTS tsrm_shutdown();#endif return FAILURE; } fastcgi = !FCGX_IsCGI(); }#endif if (fastcgi) { /* How many times to run PHP scripts before dying */ if( getenv( "PHP_FCGI_MAX_REQUESTS" )) { max_requests = atoi( getenv( "PHP_FCGI_MAX_REQUESTS" )); if( !max_requests ) { fprintf( stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n" ); return FAILURE; } } /* make php call us to get _ENV vars */ php_php_import_environment_variables = php_import_environment_variables; php_import_environment_variables = cgi_php_import_environment_variables; /* library is already initialized, now init our request */ FCGX_Init(); FCGX_InitRequest( &request, fcgi_fd, 0 );#ifndef PHP_WIN32 /* Pre-fork, if required */ if( getenv( "PHP_FCGI_CHILDREN" )) { children = atoi( getenv( "PHP_FCGI_CHILDREN" )); if( !children ) { fprintf( stderr, "PHP_FCGI_CHILDREN is not valid\n" ); return FAILURE; } } if( children ) { int running = 0; pid_t pid; /* Create a process group for ourself & children */ setsid(); pgroup = getpgrp();#ifdef DEBUG_FASTCGI fprintf( stderr, "Process group %d\n", pgroup );#endif /* Set up handler to kill children upon exit */ act.sa_flags = 0; act.sa_handler = fastcgi_cleanup; if( sigaction( SIGTERM, &act, &old_term ) || sigaction( SIGINT, &act, &old_int ) || sigaction( SIGQUIT, &act, &old_quit )) { perror( "Can't set signals" ); exit( 1 ); } while( parent ) { do {#ifdef DEBUG_FASTCGI fprintf( stderr, "Forking, %d running\n", running );#endif pid = fork(); switch( pid ) { case 0: /* One of the children. * Make sure we don't go round the * fork loop any more */ parent = 0; /* don't catch our signals */ sigaction( SIGTERM, &old_term, 0 ); sigaction( SIGQUIT, &old_quit, 0 ); sigaction( SIGINT, &old_int, 0 ); break; case -1: perror( "php (pre-forking)" ); exit( 1 ); break; default: /* Fine */ running++; break; } } while( parent && ( running < children )); if( parent ) {#ifdef DEBUG_FASTCGI fprintf( stderr, "Wait for kids, pid %d\n", getpid() );#endif while (wait( &status ) < 0) { } running--; } } }#endif /* WIN32 */ }#endif /* FASTCGI */ zend_first_try { if (!cgi#if PHP_FASTCGI && !fastcgi#endif ) { while ((c=php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1))!=-1) { switch (c) { case 'h': case '?': no_headers = 1; php_output_startup(); php_output_activate(TSRMLS_C); SG(headers_sent) = 1; php_cgi_usage(argv[0]); php_end_ob_buffers(1 TSRMLS_CC);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -