Skip to content

Commit

Permalink
Use weak function for fcgi_log
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed May 23, 2015
1 parent 18cf4e0 commit 86de98c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions Zend/zend_portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ char *alloca();

#if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
# define HAVE_NORETURN_ALIAS
# define HAVE_ATTRIBUTE_WEAK
#endif

#if ZEND_DEBUG
Expand Down
36 changes: 24 additions & 12 deletions main/fastcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ static int is_impersonate = 0;
#include "fastcgi.h"

/* maybe it's better to use weak name instead */
static fcgi_logger logger;
#ifndef HAVE_ATTRIBUTE_WEAK
static fcgi_logger fcgi_log;
#endif

typedef union _sa_t {
struct sockaddr sa;
Expand Down Expand Up @@ -360,9 +362,19 @@ void fcgi_terminate(void)
in_shutdown = 1;
}

#ifndef HAVE_ATTRIBUTE_WEAK
void fcgi_set_logger(fcgi_logger lg) {
logger = lg;
fcgi_log = lg;
}
#else
void __attribute__((weak)) fcgi_log(int type, const char *format, ...) {
va_list ap;

va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
#endif

int fcgi_init(void)
{
Expand Down Expand Up @@ -583,10 +595,10 @@ int fcgi_listen(const char *path, int backlog)
hep = gethostbyname(host);
}
if (!hep || hep->h_addrtype != AF_INET || !hep->h_addr_list[0]) {
logger(FCGI_ERROR, "Cannot resolve host name '%s'!\n", host);
fcgi_log(FCGI_ERROR, "Cannot resolve host name '%s'!\n", host);
return -1;
} else if (hep->h_addr_list[1]) {
logger(FCGI_ERROR, "Host '%s' has multiple addresses. You must choose one explicitly!\n", host);
fcgi_log(FCGI_ERROR, "Host '%s' has multiple addresses. You must choose one explicitly!\n", host);
return -1;
}
sa.sa_inet.sin_addr.s_addr = ((struct in_addr*)hep->h_addr_list[0])->s_addr;
Expand Down Expand Up @@ -623,7 +635,7 @@ int fcgi_listen(const char *path, int backlog)
int path_len = strlen(path);

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

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

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

Expand Down Expand Up @@ -683,14 +695,14 @@ int fcgi_listen(const char *path, int backlog)
n++;
#endif
} else {
logger(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
}
cur = end;
}
allowed_clients[n].sa.sa_family = 0;
free(ip);
if (!n) {
logger(FCGI_ERROR, "There are no allowed addresses");
fcgi_log(FCGI_ERROR, "There are no allowed addresses");
/* don't clear allowed_clients as it will create an "open for all" security issue */
}
}
Expand Down Expand Up @@ -743,14 +755,14 @@ void fcgi_set_allowed_clients(char *ip)
n++;
#endif
} else {
logger(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
fcgi_log(FCGI_ERROR, "Wrong IP address '%s' in listen.allowed_clients", cur);
}
cur = end;
}
allowed_clients[n].sa.sa_family = 0;
free(ip);
if (!n) {
logger(FCGI_ERROR, "There are no allowed addresses");
fcgi_log(FCGI_ERROR, "There are no allowed addresses");
/* don't clear allowed_clients as it will create an "open for all" security issue */
}
}
Expand Down Expand Up @@ -1222,7 +1234,7 @@ static int fcgi_is_allowed() {
}
#endif

logger(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip());
fcgi_log(FCGI_ERROR, "Connection disallowed: IP address '%s' has been dropped.", fcgi_get_last_client_ip());
return 0;
}

Expand Down Expand Up @@ -1337,7 +1349,7 @@ int fcgi_accept_request(fcgi_request *req)
}
fcgi_close(req, 1, 0);
} else {
logger(FCGI_ERROR, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
fcgi_log(FCGI_ERROR, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
fcgi_close(req, 1, 0);
}
#endif
Expand Down
8 changes: 5 additions & 3 deletions main/fastcgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ typedef struct _fcgi_end_request_rec {

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

typedef void (*fcgi_logger)(int type, const char *fmt, ...);

#define FCGI_HASH_TABLE_SIZE 128
#define FCGI_HASH_TABLE_MASK (FCGI_HASH_TABLE_SIZE - 1)
#define FCGI_HASH_SEG_SIZE 4096
Expand Down Expand Up @@ -200,10 +198,14 @@ fcgi_request* fcgi_init_request(fcgi_request *request, int listen_socket);
void fcgi_set_allowed_clients(char *ip);
int fcgi_accept_request(fcgi_request *req);
int fcgi_finish_request(fcgi_request *req, int force_close);
void fcgi_set_logger(fcgi_logger lg);
const char *fcgi_get_last_client_ip();
void fcgi_set_in_shutdown(int new_value);

#ifndef HAVE_ATTRIBUTE_WEAK
typedef void (*fcgi_logger)(int type, const char *fmt, ...);
void fcgi_set_logger(fcgi_logger lg);
#endif

char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);
char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value);
Expand Down
5 changes: 5 additions & 0 deletions sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,15 @@ static php_cgi_globals_struct php_cgi_globals;
#define TRANSLATE_SLASHES(path)
#endif

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

va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
#endif

static int print_module_info(zval *element)
{
Expand Down Expand Up @@ -1936,7 +1938,10 @@ consult the installation file that came with this distribution, or visit \n\
}
}

#ifndef HAVE_ATTRIBUTE_WEAK
fcgi_set_logger(fcgi_log);
#endif

if (bindpath) {
int backlog = 128;
if (getenv("PHP_FCGI_BACKLOG")) {
Expand Down
7 changes: 7 additions & 0 deletions sapi/fpm/fpm/fpm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
# define STDIN_FILENO 0
#endif

#ifndef HAVE_ATTRIBUTE_WEAK
static void fpm_fcgi_log(int type, const char *fmt, ...) /* {{{ */
#else
void fcgi_log(int type, const char *fmt, ...)
#endif
{
va_list args;
va_start(args, fmt);
Expand Down Expand Up @@ -1586,7 +1590,10 @@ int main(int argc, char *argv[])
cgi_sapi_module.php_ini_path_override = NULL;
cgi_sapi_module.php_ini_ignore_cwd = 1;

#ifndef HAVE_ATTRIBUTE_WEAK
fcgi_set_logger(fpm_fcgi_log);
#endif

fcgi_init();

#ifdef PHP_WIN32
Expand Down

0 comments on commit 86de98c

Please sign in to comment.