Skip to content
Browse files

fix server disconnect on error

  • Loading branch information...
1 parent d7c1294 commit 2b33eae3027741eaa15dc0ff4ff174d99af78f53 root committed Jun 17, 2010
Showing with 35 additions and 36 deletions.
  1. +15 −14 fcgi_request.c
  2. +17 −1 fcgi_server.c
  3. +2 −0 fcgi_server.h
  4. +1 −21 mod_fastcgi_pass.c
View
29 fcgi_request.c
@@ -143,10 +143,17 @@ int fcgi_request_create(request_rec *r, fcgi_request_t **frP)
return OK;
}
+#define CHECK(e) do { \
+ status = e; \
+ if (status != OK) { \
+ goto err; \
+ } \
+} while (0)
+
int fcgi_request_process(fcgi_request_t *fr)
{
- apr_status_t status;
apr_pool_t *p = fr->r->pool;
+ int status;
unsigned int request_id = (fr->r->connection->id & 0xffff) + 1;
@@ -157,37 +164,31 @@ int fcgi_request_process(fcgi_request_t *fr)
"FastCGI: ==> STEP 1 - send FCGI_BEGIN_REQUEST(id=%u)",
request_id);
- status = fcgi_server_send_begin_record(fr, request_id, record_buffer);
- if (status != OK)
- return status;
+ CHECK(fcgi_server_send_begin_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 2 - send FCGI_PARAMS(id=%u)",
request_id);
- status = fcgi_server_send_params_record(fr, request_id, record_buffer);
- if (status != OK)
- return status;
+ CHECK(fcgi_server_send_params_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 3 - send FCGI_STDIN(id=%u)",
request_id);
- status = fcgi_server_send_stdin_record(fr, request_id, record_buffer);
- if (status != OK)
- return status;
+ CHECK(fcgi_server_send_stdin_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 4 - recv FCGI_STDOUT(id=%u)",
request_id);
- status = fcgi_server_recv_stdout_stderr_record(fr, request_id, record_buffer);
- if (status != OK)
- return status;
+ CHECK(fcgi_server_recv_stdout_stderr_record(fr, request_id, record_buffer));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: ==> STEP 5 - return OK(id=%u)",
request_id);
- return OK;
+err:
+ fcgi_server_disconnect(fr);
+ return status;
}
View
18 fcgi_server.c
@@ -1,4 +1,5 @@
#include <unistd.h>
+#include <sys/socket.h>
#include <apr_lib.h>
#include <apr_strings.h>
@@ -37,6 +38,16 @@ ssize_t socket_recv(int fd, void *buf, size_t len)
return bytes_read;
}
+void fcgi_server_disconnect(fcgi_request_t *fr)
+{
+ if (fr->socket_fd >= 0) {
+ struct linger linger = {0, 0};
+ setsockopt(fr->socket_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
+ close(fr->socket_fd);
+ fr->socket_fd = -1;
+ }
+}
+
int fcgi_server_connect(fcgi_request_t *fr)
{
/* create the socket */
@@ -50,6 +61,7 @@ int fcgi_server_connect(fcgi_request_t *fr)
}
if (fr->socket_fd >= FD_SETSIZE) {
+ fcgi_server_disconnect(fr);
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, fr->r,
"FastCGI: failed to connect to server \"%s\": "
"socket file descriptor (%u) is larger than "
@@ -60,6 +72,7 @@ int fcgi_server_connect(fcgi_request_t *fr)
/* connect the socket */
if (connect(fr->socket_fd, (struct sockaddr *)fr->socket_addr, fr->socket_addr_len) == -1) {
+ fcgi_server_disconnect(fr);
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, fr->r,
"FastCGI: failed to connect to server \"%s\": "
"connect() failed", fr->server);
@@ -543,7 +556,10 @@ int fcgi_server_recv_stdout_stderr_record(fcgi_request_t *fr,
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, fr->r,
"FastCGI: => FCGI_END_REQUEST received (id=%u)", request_id);
seen_eos = 1;
- /* TODO: what about this? force close any sockets, etc? */
+
+ /* force close the socket after receiving
+ * an FCGI_END_REQUEST packet */
+ fcgi_server_disconnect(fr);
break;
case FCGI_STDOUT:
View
2 fcgi_server.h
@@ -5,6 +5,8 @@
#include "fcgi_request.h"
+void fcgi_server_disconnect(fcgi_request_t *fr);
+
int fcgi_server_connect(fcgi_request_t *fr);
int fcgi_server_send_begin_record(fcgi_request_t *fr, uint16_t request_id,
View
22 mod_fastcgi_pass.c
@@ -3,22 +3,6 @@
#include "fcgi_request.h"
#include "fcgi_server.h"
-/*******************************************************************************
- * Close the connection to the FastCGI server. This is normally called by
- * do_work(), but may also be called as in request pool cleanup.
- */
-static
-void close_connection_to_fs(fcgi_request_t *fr)
-{
- if (fr->socket_fd >= 0) {
- struct linger linger = {0, 0};
- /* abort the connection entirely */
- setsockopt(fr->socket_fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
- close(fr->socket_fd);
- fr->socket_fd = -1;
- }
-}
-
static
int fastcgi_pass_handler(request_rec *r)
{
@@ -39,11 +23,7 @@ int fastcgi_pass_handler(request_rec *r)
}
/* Step 3: process the request */
- if ((ret = fcgi_request_process(fr)) != OK) {
- return ret;
- }
-
- return OK;
+ return fcgi_request_process(fr);
}
static

0 comments on commit 2b33eae

Please sign in to comment.
Something went wrong with that request. Please try again.