Skip to content

Commit 18cf4e0

Browse files
committed
Fixed fpm-side (tests passes)
1 parent fef18f4 commit 18cf4e0

File tree

9 files changed

+436
-1754
lines changed

9 files changed

+436
-1754
lines changed

main/fastcgi.c

Lines changed: 188 additions & 119 deletions
Large diffs are not rendered by default.

main/fastcgi.h

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ typedef enum _fcgi_role {
4949
FCGI_FILTER = 3
5050
} fcgi_role;
5151

52-
typedef enum _fcgi_code {
53-
FCGI_NOTICE,
54-
FCGI_WARNING,
55-
FCGI_ERROR,
56-
} fcgi_code;
52+
enum {
53+
FCGI_DEBUG = 1,
54+
FCGI_NOTICE = 2,
55+
FCGI_WARNING = 3,
56+
FCGI_ERROR = 4,
57+
FCGI_ALERT = 5,
58+
};
5759

5860
typedef enum _fcgi_request_type {
5961
FCGI_BEGIN_REQUEST = 1, /* [in] */
@@ -116,21 +118,91 @@ typedef struct _fcgi_end_request_rec {
116118

117119
typedef void (*fcgi_apply_func)(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
118120

119-
typedef void (*fcgi_logger)(int type, const char *format, ...);
121+
typedef void (*fcgi_logger)(int type, const char *fmt, ...);
122+
123+
#define FCGI_HASH_TABLE_SIZE 128
124+
#define FCGI_HASH_TABLE_MASK (FCGI_HASH_TABLE_SIZE - 1)
125+
#define FCGI_HASH_SEG_SIZE 4096
126+
127+
typedef struct _fcgi_hash_bucket {
128+
unsigned int hash_value;
129+
unsigned int var_len;
130+
char *var;
131+
unsigned int val_len;
132+
char *val;
133+
struct _fcgi_hash_bucket *next;
134+
struct _fcgi_hash_bucket *list_next;
135+
} fcgi_hash_bucket;
136+
137+
typedef struct _fcgi_hash_buckets {
138+
unsigned int idx;
139+
struct _fcgi_hash_buckets *next;
140+
struct _fcgi_hash_bucket data[FCGI_HASH_TABLE_SIZE];
141+
} fcgi_hash_buckets;
142+
143+
typedef struct _fcgi_data_seg {
144+
char *pos;
145+
char *end;
146+
struct _fcgi_data_seg *next;
147+
char data[1];
148+
} fcgi_data_seg;
149+
150+
typedef struct _fcgi_hash {
151+
fcgi_hash_bucket *hash_table[FCGI_HASH_TABLE_SIZE];
152+
fcgi_hash_bucket *list;
153+
fcgi_hash_buckets *buckets;
154+
fcgi_data_seg *data;
155+
} fcgi_hash;
156+
157+
typedef struct _fcgi_request fcgi_request;
158+
typedef struct _fcgi_req_hook fcgi_req_hook;
159+
160+
struct _fcgi_req_hook {
161+
void(*on_accept)();
162+
void(*on_read)();
163+
void(*on_close)();
164+
};
165+
166+
struct _fcgi_request {
167+
int listen_socket;
168+
int tcp;
169+
int fd;
170+
int id;
171+
int keep;
172+
#ifdef TCP_NODELAY
173+
int nodelay;
174+
#endif
175+
int closed;
176+
177+
fcgi_req_hook hook;
178+
179+
int in_len;
180+
int in_pad;
181+
182+
fcgi_header *out_hdr;
183+
unsigned char *out_pos;
184+
unsigned char out_buf[1024*8];
185+
unsigned char reserved[sizeof(fcgi_end_request_rec)];
120186

121-
typedef struct _fcgi_request fcgi_request;
187+
int has_env;
188+
fcgi_hash env;
189+
};
122190

123191
int fcgi_init(void);
124192
void fcgi_shutdown(void);
125193
int fcgi_is_fastcgi(void);
194+
int fcgi_is_closed(fcgi_request *req);
195+
void fcgi_close(fcgi_request *req, int force, int destroy);
126196
int fcgi_in_shutdown(void);
127197
void fcgi_terminate(void);
128198
int fcgi_listen(const char *path, int backlog);
129-
fcgi_request* fcgi_init_request(int listen_socket);
130-
void fcgi_destroy_request(fcgi_request *req);
199+
fcgi_request* fcgi_init_request(fcgi_request *request, int listen_socket);
200+
void fcgi_set_allowed_clients(char *ip);
131201
int fcgi_accept_request(fcgi_request *req);
132202
int fcgi_finish_request(fcgi_request *req, int force_close);
133-
void fcgi_set_logger(fcgi_logger logger);
203+
void fcgi_set_logger(fcgi_logger lg);
204+
const char *fcgi_get_last_client_ip();
205+
void fcgi_set_in_shutdown(int new_value);
134206

135207
char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
136208
char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);

sapi/cgi/cgi_main.c

Lines changed: 89 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,12 +1034,12 @@ static int is_valid_path(const char *path)
10341034
/* }}} */
10351035

10361036
#define CGI_GETENV(name) \
1037-
((request) ? \
1037+
((request->has_env) ? \
10381038
FCGI_GETENV(request, name) : \
10391039
getenv(name))
10401040

10411041
#define CGI_PUTENV(name, value) \
1042-
((request) ? \
1042+
((request->has_env) ? \
10431043
FCGI_PUTENV(request, name, value) : \
10441044
_sapi_cgi_putenv(name, sizeof(name)-1, value))
10451045

@@ -1732,7 +1732,7 @@ int main(int argc, char *argv[])
17321732
int fastcgi;
17331733
char *bindpath = NULL;
17341734
int fcgi_fd = 0;
1735-
fcgi_request *request = NULL;
1735+
fcgi_request request = {0};
17361736
int warmup_repeats = 0;
17371737
int repeats = 1;
17381738
int benchmark = 0;
@@ -1967,109 +1967,109 @@ consult the installation file that came with this distribution, or visit \n\
19671967
php_import_environment_variables = cgi_php_import_environment_variables;
19681968

19691969
/* library is already initialized, now init our request */
1970-
request = fcgi_init_request(fcgi_fd);
1970+
fcgi_init_request(&request, fcgi_fd);
19711971

19721972
#ifndef PHP_WIN32
1973-
/* Pre-fork, if required */
1974-
if (getenv("PHP_FCGI_CHILDREN")) {
1975-
char * children_str = getenv("PHP_FCGI_CHILDREN");
1976-
children = atoi(children_str);
1977-
if (children < 0) {
1978-
fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
1979-
return FAILURE;
1973+
/* Pre-fork, if required */
1974+
if (getenv("PHP_FCGI_CHILDREN")) {
1975+
char * children_str = getenv("PHP_FCGI_CHILDREN");
1976+
children = atoi(children_str);
1977+
if (children < 0) {
1978+
fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
1979+
return FAILURE;
1980+
}
1981+
fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, children_str, strlen(children_str));
1982+
/* This is the number of concurrent requests, equals FCGI_MAX_CONNS */
1983+
fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, children_str, strlen(children_str));
1984+
} else {
1985+
fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, "1", sizeof("1")-1);
1986+
fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, "1", sizeof("1")-1);
19801987
}
1981-
fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, children_str, strlen(children_str));
1982-
/* This is the number of concurrent requests, equals FCGI_MAX_CONNS */
1983-
fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, children_str, strlen(children_str));
1984-
} else {
1985-
fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, "1", sizeof("1")-1);
1986-
fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, "1", sizeof("1")-1);
1987-
}
19881988

1989-
if (children) {
1990-
int running = 0;
1991-
pid_t pid;
1989+
if (children) {
1990+
int running = 0;
1991+
pid_t pid;
19921992

1993-
/* Create a process group for ourself & children */
1994-
setsid();
1995-
pgroup = getpgrp();
1993+
/* Create a process group for ourself & children */
1994+
setsid();
1995+
pgroup = getpgrp();
19961996
#ifdef DEBUG_FASTCGI
1997-
fprintf(stderr, "Process group %d\n", pgroup);
1997+
fprintf(stderr, "Process group %d\n", pgroup);
19981998
#endif
19991999

2000-
/* Set up handler to kill children upon exit */
2001-
act.sa_flags = 0;
2002-
act.sa_handler = fastcgi_cleanup;
2003-
if (sigaction(SIGTERM, &act, &old_term) ||
2004-
sigaction(SIGINT, &act, &old_int) ||
2005-
sigaction(SIGQUIT, &act, &old_quit)
2006-
) {
2007-
perror("Can't set signals");
2008-
exit(1);
2009-
}
2000+
/* Set up handler to kill children upon exit */
2001+
act.sa_flags = 0;
2002+
act.sa_handler = fastcgi_cleanup;
2003+
if (sigaction(SIGTERM, &act, &old_term) ||
2004+
sigaction(SIGINT, &act, &old_int) ||
2005+
sigaction(SIGQUIT, &act, &old_quit)
2006+
) {
2007+
perror("Can't set signals");
2008+
exit(1);
2009+
}
20102010

2011-
if (fcgi_in_shutdown()) {
2012-
goto parent_out;
2013-
}
2011+
if (fcgi_in_shutdown()) {
2012+
goto parent_out;
2013+
}
20142014

2015-
while (parent) {
2016-
do {
2015+
while (parent) {
2016+
do {
20172017
#ifdef DEBUG_FASTCGI
2018-
fprintf(stderr, "Forking, %d running\n", running);
2019-
#endif
2020-
pid = fork();
2021-
switch (pid) {
2022-
case 0:
2023-
/* One of the children.
2024-
* Make sure we don't go round the
2025-
* fork loop any more
2026-
*/
2027-
parent = 0;
2018+
fprintf(stderr, "Forking, %d running\n", running);
2019+
#endif
2020+
pid = fork();
2021+
switch (pid) {
2022+
case 0:
2023+
/* One of the children.
2024+
* Make sure we don't go round the
2025+
* fork loop any more
2026+
*/
2027+
parent = 0;
20282028

2029-
/* don't catch our signals */
2030-
sigaction(SIGTERM, &old_term, 0);
2031-
sigaction(SIGQUIT, &old_quit, 0);
2032-
sigaction(SIGINT, &old_int, 0);
2033-
break;
2034-
case -1:
2035-
perror("php (pre-forking)");
2036-
exit(1);
2037-
break;
2038-
default:
2039-
/* Fine */
2040-
running++;
2041-
break;
2042-
}
2043-
} while (parent && (running < children));
2029+
/* don't catch our signals */
2030+
sigaction(SIGTERM, &old_term, 0);
2031+
sigaction(SIGQUIT, &old_quit, 0);
2032+
sigaction(SIGINT, &old_int, 0);
2033+
break;
2034+
case -1:
2035+
perror("php (pre-forking)");
2036+
exit(1);
2037+
break;
2038+
default:
2039+
/* Fine */
2040+
running++;
2041+
break;
2042+
}
2043+
} while (parent && (running < children));
20442044

2045-
if (parent) {
2045+
if (parent) {
20462046
#ifdef DEBUG_FASTCGI
2047-
fprintf(stderr, "Wait for kids, pid %d\n", getpid());
2047+
fprintf(stderr, "Wait for kids, pid %d\n", getpid());
20482048
#endif
2049-
parent_waiting = 1;
2050-
while (1) {
2051-
if (wait(&status) >= 0) {
2052-
running--;
2053-
break;
2054-
} else if (exit_signal) {
2055-
break;
2049+
parent_waiting = 1;
2050+
while (1) {
2051+
if (wait(&status) >= 0) {
2052+
running--;
2053+
break;
2054+
} else if (exit_signal) {
2055+
break;
2056+
}
20562057
}
2057-
}
2058-
if (exit_signal) {
2058+
if (exit_signal) {
20592059
#if 0
2060-
while (running > 0) {
2061-
while (wait(&status) < 0) {
2060+
while (running > 0) {
2061+
while (wait(&status) < 0) {
2062+
}
2063+
running--;
20622064
}
2063-
running--;
2064-
}
20652065
#endif
2066-
goto parent_out;
2066+
goto parent_out;
2067+
}
20672068
}
20682069
}
2070+
} else {
2071+
parent = 0;
20692072
}
2070-
} else {
2071-
parent = 0;
2072-
}
20732073

20742074
#endif /* WIN32 */
20752075
}
@@ -2096,9 +2096,6 @@ consult the installation file that came with this distribution, or visit \n\
20962096
break;
20972097
case 'h':
20982098
case '?':
2099-
if (request) {
2100-
fcgi_destroy_request(request);
2101-
}
21022099
fcgi_shutdown();
21032100
no_headers = 1;
21042101
SG(headers_sent) = 1;
@@ -2120,9 +2117,9 @@ consult the installation file that came with this distribution, or visit \n\
21202117
fcgi_impersonate();
21212118
}
21222119
#endif
2123-
while (!fastcgi || fcgi_accept_request(request) >= 0) {
2124-
SG(server_context) = fastcgi ? (void *) request : (void *) 1;
2125-
init_request_info(request);
2120+
while (!fastcgi || fcgi_accept_request(&request) >= 0) {
2121+
SG(server_context) = fastcgi ? (void *)&request : (void *) 1;
2122+
init_request_info(&request);
21262123

21272124
if (!cgi && !fastcgi) {
21282125
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
@@ -2307,7 +2304,7 @@ consult the installation file that came with this distribution, or visit \n\
23072304
* get path_translated */
23082305
if (php_request_startup() == FAILURE) {
23092306
if (fastcgi) {
2310-
fcgi_finish_request(request, 1);
2307+
fcgi_finish_request(&request, 1);
23112308
}
23122309
SG(server_context) = NULL;
23132310
php_module_shutdown();
@@ -2518,7 +2515,7 @@ consult the installation file that came with this distribution, or visit \n\
25182515
/* only fastcgi will get here */
25192516
requests++;
25202517
if (max_requests && (requests == max_requests)) {
2521-
fcgi_finish_request(request, 1);
2518+
fcgi_finish_request(&request, 1);
25222519
if (bindpath) {
25232520
free(bindpath);
25242521
}
@@ -2530,9 +2527,6 @@ consult the installation file that came with this distribution, or visit \n\
25302527
}
25312528
/* end of fastcgi loop */
25322529
}
2533-
if (request) {
2534-
fcgi_destroy_request(request);
2535-
}
25362530
fcgi_shutdown();
25372531

25382532
if (cgi_sapi_module.php_ini_path_override) {

sapi/fpm/config.m4

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,7 @@ if test "$PHP_FPM" != "no"; then
678678

679679
PHP_FPM_CFLAGS="-I$abs_srcdir/sapi/fpm"
680680

681-
PHP_FPM_FILES="fpm/fastcgi.c \
682-
fpm/fpm.c \
681+
PHP_FPM_FILES="fpm/fpm.c \
683682
fpm/fpm_children.c \
684683
fpm/fpm_cleanup.c \
685684
fpm/fpm_clock.c \

0 commit comments

Comments
 (0)