Skip to content

Commit

Permalink
Add syslog's ident and facility parameters to config
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
  • Loading branch information
pprindeville committed Aug 23, 2017
1 parent 3cb6407 commit a56e2d9
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/standard/syslog.c
Expand Up @@ -152,6 +152,7 @@ PHP_FUNCTION(openlog)
RETURN_FALSE;
}
openlog(BG(syslog_device), option, facility);
have_called_openlog = 1;
RETURN_TRUE;
}
/* }}} */
Expand Down
139 changes: 139 additions & 0 deletions main/main.c
Expand Up @@ -119,6 +119,143 @@ PHPAPI int core_globals_id;

#define SAFE_FILENAME(f) ((f)?(f):"-")

/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnSetFacility)
{
const char *facility = ZSTR_VAL(new_value);

#ifdef LOG_AUTH
if (!strcmp(facility, "LOG_AUTH") || !strcmp(facility, "auth") || !strcmp(facility, "security")) {
PG(syslog_facility) = LOG_AUTH;
return SUCCESS;
}
#endif
#ifdef LOG_AUTHPRIV
if (!strcmp(facility, "LOG_AUTHPRIV") || !strcmp(facility, "authpriv")) {
PG(syslog_facility) = LOG_AUTHPRIV;
return SUCCESS;
}
#endif
#ifdef LOG_CRON
if (!strcmp(facility, "LOG_CRON") || !strcmp(facility, "cron")) {
PG(syslog_facility) = LOG_CRON;
return SUCCESS;
}
#endif
#ifdef LOG_DAEMON
if (!strcmp(facility, "LOG_DAEMON") || !strcmp(facility, "daemon")) {
PG(syslog_facility) = LOG_DAEMON;
return SUCCESS;
}
#endif
#ifdef LOG_FTP
if (!strcmp(facility, "LOG_FTP") || !strcmp(facility, "ftp")) {
PG(syslog_facility) = LOG_FTP;
return SUCCESS;
}
#endif
#ifdef LOG_KERN
if (!strcmp(facility, "LOG_KERN") || !strcmp(facility, "kern")) {
PG(syslog_facility) = LOG_KERN;
return SUCCESS;
}
#endif
#ifdef LOG_LPR
if (!strcmp(facility, "LOG_LPR") || !strcmp(facility, "lpr")) {
PG(syslog_facility) = LOG_LPR;
return SUCCESS;
}
#endif
#ifdef LOG_MAIL
if (!strcmp(facility, "LOG_MAIL") || !strcmp(facility, "mail")) {
PG(syslog_facility) = LOG_MAIL;
return SUCCESS;
}
#endif
#ifdef LOG_INTERNAL_MARK
if (!strcmp(facility, "LOG_INTERNAL_MARK") || !strcmp(facility, "mark")) {
PG(syslog_facility) = LOG_INTERNAL_MARK;
return SUCCESS;
}
#endif
#ifdef LOG_NEWS
if (!strcmp(facility, "LOG_NEWS") || !strcmp(facility, "news")) {
PG(syslog_facility) = LOG_NEWS;
return SUCCESS;
}
#endif
#ifdef LOG_SYSLOG
if (!strcmp(facility, "LOG_SYSLOG") || !strcmp(facility, "syslog")) {
PG(syslog_facility) = LOG_SYSLOG;
return SUCCESS;
}
#endif
#ifdef LOG_USER
if (!strcmp(facility, "LOG_USER") || !strcmp(facility, "user")) {
PG(syslog_facility) = LOG_USER;
return SUCCESS;
}
#endif
#ifdef LOG_UUCP
if (!strcmp(facility, "LOG_UUCP") || !strcmp(facility, "uucp")) {
PG(syslog_facility) = LOG_UUCP;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL0
if (!strcmp(facility, "LOG_LOCAL0") || !strcmp(facility, "local0")) {
PG(syslog_facility) = LOG_LOCAL0;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL1
if (!strcmp(facility, "LOG_LOCAL1") || !strcmp(facility, "local1")) {
PG(syslog_facility) = LOG_LOCAL1;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL2
if (!strcmp(facility, "LOG_LOCAL2") || !strcmp(facility, "local2")) {
PG(syslog_facility) = LOG_LOCAL2;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL3
if (!strcmp(facility, "LOG_LOCAL3") || !strcmp(facility, "local3")) {
PG(syslog_facility) = LOG_LOCAL3;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL4
if (!strcmp(facility, "LOG_LOCAL4") || !strcmp(facility, "local4")) {
PG(syslog_facility) = LOG_LOCAL4;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL5
if (!strcmp(facility, "LOG_LOCAL5") || !strcmp(facility, "local5")) {
PG(syslog_facility) = LOG_LOCAL5;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL6
if (!strcmp(facility, "LOG_LOCAL6") || !strcmp(facility, "local6")) {
PG(syslog_facility) = LOG_LOCAL6;
return SUCCESS;
}
#endif
#ifdef LOG_LOCAL7
if (!strcmp(facility, "LOG_LOCAL7") || !strcmp(facility, "local7")) {
PG(syslog_facility) = LOG_LOCAL7;
return SUCCESS;
}
#endif

return FAILURE;
}
/* }}} */

/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnSetPrecision)
Expand Down Expand Up @@ -611,6 +748,8 @@ PHP_INI_BEGIN()
#ifdef PHP_WIN32
STD_PHP_INI_BOOLEAN("windows.show_crt_warning", "0", PHP_INI_ALL, OnUpdateBool, windows_show_crt_warning, php_core_globals, core_globals)
#endif
STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
PHP_INI_END()
/* }}} */

Expand Down
3 changes: 3 additions & 0 deletions main/php_globals.h
Expand Up @@ -166,6 +166,9 @@ struct _php_core_globals {
#ifdef PHP_WIN32
zend_bool windows_show_crt_warning;
#endif

zend_long syslog_facility;
char *syslog_ident;
};


Expand Down
13 changes: 13 additions & 0 deletions main/php_syslog.c
Expand Up @@ -26,6 +26,7 @@
#include "php_syslog.h"

#include "zend.h"
#include "zend_types.h"
#include "zend_smart_string.h"

/*
Expand All @@ -37,6 +38,8 @@
#define syslog std_syslog
#endif

zend_bool have_called_openlog = 0;

#ifdef PHP_WIN32
PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
{
Expand All @@ -56,6 +59,16 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
smart_string sbuf = {0};
va_list args;

/*
* don't rely on openlog() being called by syslog() if it's
* not already been done; call it ourselves and pass the
* correct parameters!
*/
if (!have_called_openlog) {
openlog(PG(syslog_ident), 0, PG(syslog_facility));
have_called_openlog = 1;
}

va_start(args, format);
zend_printf_to_smart_string(&fbuf, format, args);
smart_string_0(&fbuf);
Expand Down
2 changes: 2 additions & 0 deletions main/php_syslog.h
Expand Up @@ -32,6 +32,8 @@
#endif
#endif

extern zend_bool have_called_openlog;

BEGIN_EXTERN_C()
PHPAPI void php_syslog(int, const char *format, ...);
END_EXTERN_C()
Expand Down
2 changes: 2 additions & 0 deletions php.ini-development
Expand Up @@ -577,6 +577,8 @@ html_errors = On
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
;syslog_ident = 'php'
;syslog_facility = LOG_USER

;windows.show_crt_warning
; Default value: 0
Expand Down
2 changes: 1 addition & 1 deletion win32/wsyslog.c
Expand Up @@ -97,7 +97,7 @@ void vsyslog(int priority, const char *message, va_list args)

/* default event source */
if (INVALID_HANDLE_VALUE == PW32G(log_source))
openlog("php", LOG_PID, LOG_SYSLOG);
openlog(PG(syslog_ident), LOG_PID, PG(syslog_facility));

switch (priority) { /* translate UNIX type into NT type */
case LOG_ALERT:
Expand Down

0 comments on commit a56e2d9

Please sign in to comment.