--- cgi_main.c 2008-03-28 13:00:29.000000000 +0700 +++ cgi_main.c.mod 2008-03-31 14:24:48.000000000 +0700 @@ -1247,13 +1247,24 @@ STANDARD_MODULE_PROPERTIES }; +static int is_port_number(const char *bindpath) +{ + while (*bindpath) { + if (*bindpath < '0' || *bindpath > '9') { + return 0; + } + bindpath++; + } + return 1; +} + /* {{{ main */ int main(int argc, char *argv[]) { int free_query_string = 0; int exit_status = SUCCESS; - int cgi = 0, c, i, len; + int cgi = 0, c, i, len, tcp = 0; zend_file_handle file_handle; int retval = FAILURE; char *s; @@ -1264,6 +1275,8 @@ char *orig_optarg = php_optarg; char *script_file = NULL; int ini_entries_len = 0; + int markpid; + pid_t *childrenpid; /* end of temporary locals */ #ifdef ZTS @@ -1457,9 +1470,23 @@ } } #endif /* FORCE_CGI_REDIRECT */ - #if PHP_FASTCGI if (bindpath) { + int port; + char *s; + + if ((s = strchr(bindpath, ':'))) { + port = atoi(s+1); + if (port != 0 && (s-bindpath) < MAXPATHLEN) { + tcp = 1; + } + } else if (is_port_number(bindpath)) { + port = atoi(bindpath); + if (port != 0) { + tcp = 1; + } + } + fcgi_fd = fcgi_listen(bindpath, 128); if (fcgi_fd < 0) { fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath); @@ -1486,8 +1513,8 @@ php_import_environment_variables = cgi_php_import_environment_variables; /* library is already initialized, now init our request */ + if (tcp) fcgi_init_request(&request, fcgi_fd); - #ifndef PHP_WIN32 /* Pre-fork, if required */ if (getenv("PHP_FCGI_CHILDREN")) { @@ -1501,7 +1528,10 @@ if (children) { int running = 0; pid_t pid; - + if (!tcp) { + childrenpid=malloc(sizeof(pid_t)*children); + memset(childrenpid,0,sizeof(pid_t)*children); + } /* Create a process group for ourself & children */ setsid(); pgroup = getpgrp(); @@ -1528,6 +1558,12 @@ #ifdef DEBUG_FASTCGI fprintf(stderr, "Forking, %d running\n", running); #endif + if(!tcp) { + markpid=0; + while(childrenpid[markpid] != 0 && markpid < children) + markpid++; + } + pid = fork(); switch (pid) { case 0: @@ -1548,6 +1584,8 @@ break; default: /* Fine */ + if (!tcp) + childrenpid[markpid]=pid; running++; break; } @@ -1557,9 +1595,15 @@ #ifdef DEBUG_FASTCGI fprintf(stderr, "Wait for kids, pid %d\n", getpid()); #endif - while (wait(&status) < 0) { + while ((pid=wait(&status)) < 0) { } running--; + if (!tcp) { + markpid=0; + while(childrenpid[markpid] != pid && markpid < children) + markpid++; + childrenpid[markpid]=0; + } } } } else { @@ -1605,6 +1651,30 @@ #if PHP_FASTCGI /* start of FAST CGI loop */ /* Initialise FastCGI request structure */ +/* start hack; unix socket only */ + if (!tcp) { + char *newbindpath; + + newbindpath=malloc(strlen(bindpath)+8); /* max 9999 */ + sprintf(newbindpath,"%s_%d",bindpath,markpid); + free(bindpath); + bindpath=newbindpath; + + fcgi_fd = fcgi_listen(bindpath, 128); + if (fcgi_fd < 0) { + fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath); + fcgi_shutdown(); + #ifdef ZTS + tsrm_shutdown(); + #endif + return FAILURE; + } + fastcgi = fcgi_is_fastcgi(); + /* library is already initialized, now init our request */ + fcgi_init_request(&request, fcgi_fd); + } +/* end hack */ + #ifdef PHP_WIN32 /* attempt to set security impersonation for fastcgi will only happen on NT based OS, others will ignore it. */