FastCGI does not set SO_REUSEADDR
| Bug #41291 | FastCGI does not set SO_REUSEADDR | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2007-05-04 19:13 UTC | Modified: | 2007-05-10 15:22 UTC |
|
||||||||||
| From: | fmajid at kefta dot com | Assigned: | dmitry (profile) | |||||||||||
| Status: | Closed | Package: | CGI/CLI related | |||||||||||
| PHP Version: | 5.2.2 | OS: | Solaris | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2007-05-04 19:13 UTC] fmajid at kefta dot com
Description:
------------
The CGI/FastCGI SAPI does not set the SO_REUSEADDR option. This can prevent the FastCGI server from being restarted if it exits with sockets in the TIME_WAIT state.
The patch below fixes this.
That said, a much better option would be to refactor the fastcgi.c code to use php_network_bind_socket_to_local_addr instead of reinventing the square wheel.
*** php-5.2.1/sapi/cgi/fastcgi.c~ Mon Jan 1 01:36:12 2007
--- php-5.2.1/sapi/cgi/fastcgi.c Fri May 4 11:45:39 2007
***************
*** 253,258 ****
--- 253,261 ----
int listen_socket;
sa_t sa;
socklen_t sock_len;
+ #ifdef SO_REUSEADDR
+ int val = 1;
+ #endif
if ((s = strchr(path, ':'))) {
port = atoi(s+1);
***************
*** 308,313 ****
--- 311,319 ----
/* Create, bind socket and start listen on it */
if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM, 0)) < 0 ||
+ #ifdef SO_REUSEADDR
+ setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val)) < 0 ||
+ #endif
bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 ||
listen(listen_socket, backlog) < 0) {
Reproduce code:
---------------
Run a FastCGI web server and start the FastCGI SAPI using php -b.
Kill the FastCGI process.
Attempt to restart it.
Expected result:
----------------
FastCGI server restarting normally
Actual result:
--------------
alamut ~>php-cgi -b 127.0.0.1:8888 -c /home/majid/web/conf
Cannot bind/listen socket - [125] Address already in use.
Couldn't create FastCGI listen socket on port 127.0.0.1:8888
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2007-05-10 15:22 UTC] dmitry@php.net