Skip to content

Commit 86de98c

Browse files
committed
Use weak function for fcgi_log
1 parent 18cf4e0 commit 86de98c

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

Zend/zend_portability.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ char *alloca();
240240

241241
#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
242242
# define HAVE_NORETURN_ALIAS
243+
# define HAVE_ATTRIBUTE_WEAK
243244
#endif
244245

245246
#if ZEND_DEBUG

main/fastcgi.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ static int is_impersonate = 0;
133133
#include "fastcgi.h"
134134

135135
/* maybe it's better to use weak name instead */
136-
static fcgi_logger logger;
136+
#ifndef HAVE_ATTRIBUTE_WEAK
137+
static fcgi_logger fcgi_log;
138+
#endif
137139

138140
typedef union _sa_t {
139141
struct sockaddr sa;
@@ -360,9 +362,19 @@ void fcgi_terminate(void)
360362
in_shutdown = 1;
361363
}
362364

365+
#ifndef HAVE_ATTRIBUTE_WEAK
363366
void fcgi_set_logger(fcgi_logger lg) {
364-
logger = lg;
367+
fcgi_log = lg;
365368
}
369+
#else
370+
void __attribute__((weak)) fcgi_log(int type, const char *format, ...) {
371+
va_list ap;
372+
373+
va_start(ap, format);
374+
vfprintf(stderr, format, ap);
375+
va_end(ap);
376+
}
377+
#endif
366378

367379
int fcgi_init(void)
368380
{
@@ -583,10 +595,10 @@ int fcgi_listen(const char *path, int backlog)
583595
hep = gethostbyname(host);
584596
}
585597
if (!hep || hep->h_addrtype != AF_INET || !hep->h_addr_list[0]) {
586-
logger(FCGI_ERROR, "Cannot resolve host name '%s'!\n", host);
598+
fcgi_log(FCGI_ERROR, "Cannot resolve host name '%s'!\n", host);
587599
return -1;
588600
} else if (hep->h_addr_list[1]) {
589-
logger(FCGI_ERROR, "Host '%s' has multiple addresses. You must choose one explicitly!\n", host);
601+
fcgi_log(FCGI_ERROR, "Host '%s' has multiple addresses. You must choose one explicitly!\n", host);
590602
return -1;
591603
}
592604
sa.sa_inet.sin_addr.s_addr = ((struct in_addr*)hep->h_addr_list[0])->s_addr;
@@ -623,7 +635,7 @@ int fcgi_listen(const char *path, int backlog)
623635
int path_len = strlen(path);
624636

625637
if (path_len >= sizeof(sa.sa_unix.sun_path)) {
626-
logger(FCGI_ERROR, "Listening socket's path name is too long.\n");
638+
fcgi_log(FCGI_ERROR, "Listening socket's path name is too long.\n");
627639
return -1;
628640
}
629641

@@ -646,7 +658,7 @@ int fcgi_listen(const char *path, int backlog)
646658
bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 ||
647659
listen(listen_socket, backlog) < 0) {
648660

649-
logger(FCGI_ERROR, "Cannot bind/listen socket - [%d] %s.\n",errno, strerror(errno));
661+
fcgi_log(FCGI_ERROR, "Cannot bind/listen socket - [%d] %s.\n",errno, strerror(errno));
650662
return -1;
651663
}
652664

@@ -683,14 +695,14 @@ int fcgi_listen(const char *path, int backlog)
683695
n++;
684696
#endif
685697
} else {
686-
logger(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
698+
fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
687699
}
688700
cur = end;
689701
}
690702
allowed_clients[n].sa.sa_family = 0;
691703
free(ip);
692704
if (!n) {
693-
logger(FCGI_ERROR, "There are no allowed addresses");
705+
fcgi_log(FCGI_ERROR, "There are no allowed addresses");
694706
/* don't clear allowed_clients as it will create an "open for all" security issue */
695707
}
696708
}
@@ -743,14 +755,14 @@ void fcgi_set_allowed_clients(char *ip)
743755
n++;
744756
#endif
745757
} else {
746-
logger(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
758+
fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
747759
}
748760
cur = end;
749761
}
750762
allowed_clients[n].sa.sa_family = 0;
751763
free(ip);
752764
if (!n) {
753-
logger(FCGI_ERROR, "There are no allowed addresses");
765+
fcgi_log(FCGI_ERROR, "There are no allowed addresses");
754766
/* don't clear allowed_clients as it will create an "open for all" security issue */
755767
}
756768
}
@@ -1222,7 +1234,7 @@ static int fcgi_is_allowed() {
12221234
}
12231235
#endif
12241236

1225-
logger(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip());
1237+
fcgi_log(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip());
12261238
return 0;
12271239
}
12281240

@@ -1337,7 +1349,7 @@ int fcgi_accept_request(fcgi_request *req)
13371349
}
13381350
fcgi_close(req, 1, 0);
13391351
} else {
1340-
logger(FCGI_ERROR, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
1352+
fcgi_log(FCGI_ERROR, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
13411353
fcgi_close(req, 1, 0);
13421354
}
13431355
#endif

main/fastcgi.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ typedef struct _fcgi_end_request_rec {
118118

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

121-
typedef void (*fcgi_logger)(int type, const char *fmt, ...);
122-
123121
#define FCGI_HASH_TABLE_SIZE 128
124122
#define FCGI_HASH_TABLE_MASK (FCGI_HASH_TABLE_SIZE - 1)
125123
#define FCGI_HASH_SEG_SIZE 4096
@@ -200,10 +198,14 @@ fcgi_request* fcgi_init_request(fcgi_request *request, int listen_socket);
200198
void fcgi_set_allowed_clients(char *ip);
201199
int fcgi_accept_request(fcgi_request *req);
202200
int fcgi_finish_request(fcgi_request *req, int force_close);
203-
void fcgi_set_logger(fcgi_logger lg);
204201
const char *fcgi_get_last_client_ip();
205202
void fcgi_set_in_shutdown(int new_value);
206203

204+
#ifndef HAVE_ATTRIBUTE_WEAK
205+
typedef void (*fcgi_logger)(int type, const char *fmt, ...);
206+
void fcgi_set_logger(fcgi_logger lg);
207+
#endif
208+
207209
char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
208210
char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);
209211
char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value);

sapi/cgi/cgi_main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,15 @@ static php_cgi_globals_struct php_cgi_globals;
219219
#define TRANSLATE_SLASHES(path)
220220
#endif
221221

222+
#ifndef HAVE_ATTRIBUTE_WEAK
222223
static void fcgi_log(int type, const char *format, ...) {
223224
va_list ap;
224225

225226
va_start(ap, format);
226227
vfprintf(stderr, format, ap);
227228
va_end(ap);
228229
}
230+
#endif
229231

230232
static int print_module_info(zval *element)
231233
{
@@ -1936,7 +1938,10 @@ consult the installation file that came with this distribution, or visit \n\
19361938
}
19371939
}
19381940

1941+
#ifndef HAVE_ATTRIBUTE_WEAK
19391942
fcgi_set_logger(fcgi_log);
1943+
#endif
1944+
19401945
if (bindpath) {
19411946
int backlog = 128;
19421947
if (getenv("PHP_FCGI_BACKLOG")) {

sapi/fpm/fpm/fpm_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
457457
# define STDIN_FILENO 0
458458
#endif
459459

460+
#ifndef HAVE_ATTRIBUTE_WEAK
460461
static void fpm_fcgi_log(int type, const char *fmt, ...) /* {{{ */
462+
#else
463+
void fcgi_log(int type, const char *fmt, ...)
464+
#endif
461465
{
462466
va_list args;
463467
va_start(args, fmt);
@@ -1586,7 +1590,10 @@ int main(int argc, char *argv[])
15861590
cgi_sapi_module.php_ini_path_override = NULL;
15871591
cgi_sapi_module.php_ini_ignore_cwd = 1;
15881592

1593+
#ifndef HAVE_ATTRIBUTE_WEAK
15891594
fcgi_set_logger(fpm_fcgi_log);
1595+
#endif
1596+
15901597
fcgi_init();
15911598

15921599
#ifdef PHP_WIN32

0 commit comments

Comments
 (0)