Skip to content

Commit 2475337

Browse files
pprindevillebukka
authored andcommitted
Add syslog's ident and facility parameters to config
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
1 parent 0c4f448 commit 2475337

File tree

9 files changed

+182
-6
lines changed

9 files changed

+182
-6
lines changed

ext/standard/syslog.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ PHP_MSHUTDOWN_FUNCTION(syslog)
125125
return SUCCESS;
126126
}
127127

128+
void php_openlog(const char *ident, int option, int facility)
129+
{
130+
openlog(ident, option, facility);
131+
PG(have_called_openlog) = 1;
132+
}
133+
128134
/* {{{ proto bool openlog(string ident, int option, int facility)
129135
Open connection to system logger */
130136
/*
@@ -151,7 +157,7 @@ PHP_FUNCTION(openlog)
151157
if(BG(syslog_device) == NULL) {
152158
RETURN_FALSE;
153159
}
154-
openlog(BG(syslog_device), option, facility);
160+
php_openlog(BG(syslog_device), option, facility);
155161
RETURN_TRUE;
156162
}
157163
/* }}} */

main/main.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,143 @@ static char *get_safe_charset_hint(void) {
146146
return lastCodeset;
147147
}
148148

149+
/* {{{ PHP_INI_MH
150+
*/
151+
static PHP_INI_MH(OnSetFacility)
152+
{
153+
const char *facility = ZSTR_VAL(new_value);
154+
155+
#ifdef LOG_AUTH
156+
if (!strcmp(facility, "LOG_AUTH") || !strcmp(facility, "auth") || !strcmp(facility, "security")) {
157+
PG(syslog_facility) = LOG_AUTH;
158+
return SUCCESS;
159+
}
160+
#endif
161+
#ifdef LOG_AUTHPRIV
162+
if (!strcmp(facility, "LOG_AUTHPRIV") || !strcmp(facility, "authpriv")) {
163+
PG(syslog_facility) = LOG_AUTHPRIV;
164+
return SUCCESS;
165+
}
166+
#endif
167+
#ifdef LOG_CRON
168+
if (!strcmp(facility, "LOG_CRON") || !strcmp(facility, "cron")) {
169+
PG(syslog_facility) = LOG_CRON;
170+
return SUCCESS;
171+
}
172+
#endif
173+
#ifdef LOG_DAEMON
174+
if (!strcmp(facility, "LOG_DAEMON") || !strcmp(facility, "daemon")) {
175+
PG(syslog_facility) = LOG_DAEMON;
176+
return SUCCESS;
177+
}
178+
#endif
179+
#ifdef LOG_FTP
180+
if (!strcmp(facility, "LOG_FTP") || !strcmp(facility, "ftp")) {
181+
PG(syslog_facility) = LOG_FTP;
182+
return SUCCESS;
183+
}
184+
#endif
185+
#ifdef LOG_KERN
186+
if (!strcmp(facility, "LOG_KERN") || !strcmp(facility, "kern")) {
187+
PG(syslog_facility) = LOG_KERN;
188+
return SUCCESS;
189+
}
190+
#endif
191+
#ifdef LOG_LPR
192+
if (!strcmp(facility, "LOG_LPR") || !strcmp(facility, "lpr")) {
193+
PG(syslog_facility) = LOG_LPR;
194+
return SUCCESS;
195+
}
196+
#endif
197+
#ifdef LOG_MAIL
198+
if (!strcmp(facility, "LOG_MAIL") || !strcmp(facility, "mail")) {
199+
PG(syslog_facility) = LOG_MAIL;
200+
return SUCCESS;
201+
}
202+
#endif
203+
#ifdef LOG_INTERNAL_MARK
204+
if (!strcmp(facility, "LOG_INTERNAL_MARK") || !strcmp(facility, "mark")) {
205+
PG(syslog_facility) = LOG_INTERNAL_MARK;
206+
return SUCCESS;
207+
}
208+
#endif
209+
#ifdef LOG_NEWS
210+
if (!strcmp(facility, "LOG_NEWS") || !strcmp(facility, "news")) {
211+
PG(syslog_facility) = LOG_NEWS;
212+
return SUCCESS;
213+
}
214+
#endif
215+
#ifdef LOG_SYSLOG
216+
if (!strcmp(facility, "LOG_SYSLOG") || !strcmp(facility, "syslog")) {
217+
PG(syslog_facility) = LOG_SYSLOG;
218+
return SUCCESS;
219+
}
220+
#endif
221+
#ifdef LOG_USER
222+
if (!strcmp(facility, "LOG_USER") || !strcmp(facility, "user")) {
223+
PG(syslog_facility) = LOG_USER;
224+
return SUCCESS;
225+
}
226+
#endif
227+
#ifdef LOG_UUCP
228+
if (!strcmp(facility, "LOG_UUCP") || !strcmp(facility, "uucp")) {
229+
PG(syslog_facility) = LOG_UUCP;
230+
return SUCCESS;
231+
}
232+
#endif
233+
#ifdef LOG_LOCAL0
234+
if (!strcmp(facility, "LOG_LOCAL0") || !strcmp(facility, "local0")) {
235+
PG(syslog_facility) = LOG_LOCAL0;
236+
return SUCCESS;
237+
}
238+
#endif
239+
#ifdef LOG_LOCAL1
240+
if (!strcmp(facility, "LOG_LOCAL1") || !strcmp(facility, "local1")) {
241+
PG(syslog_facility) = LOG_LOCAL1;
242+
return SUCCESS;
243+
}
244+
#endif
245+
#ifdef LOG_LOCAL2
246+
if (!strcmp(facility, "LOG_LOCAL2") || !strcmp(facility, "local2")) {
247+
PG(syslog_facility) = LOG_LOCAL2;
248+
return SUCCESS;
249+
}
250+
#endif
251+
#ifdef LOG_LOCAL3
252+
if (!strcmp(facility, "LOG_LOCAL3") || !strcmp(facility, "local3")) {
253+
PG(syslog_facility) = LOG_LOCAL3;
254+
return SUCCESS;
255+
}
256+
#endif
257+
#ifdef LOG_LOCAL4
258+
if (!strcmp(facility, "LOG_LOCAL4") || !strcmp(facility, "local4")) {
259+
PG(syslog_facility) = LOG_LOCAL4;
260+
return SUCCESS;
261+
}
262+
#endif
263+
#ifdef LOG_LOCAL5
264+
if (!strcmp(facility, "LOG_LOCAL5") || !strcmp(facility, "local5")) {
265+
PG(syslog_facility) = LOG_LOCAL5;
266+
return SUCCESS;
267+
}
268+
#endif
269+
#ifdef LOG_LOCAL6
270+
if (!strcmp(facility, "LOG_LOCAL6") || !strcmp(facility, "local6")) {
271+
PG(syslog_facility) = LOG_LOCAL6;
272+
return SUCCESS;
273+
}
274+
#endif
275+
#ifdef LOG_LOCAL7
276+
if (!strcmp(facility, "LOG_LOCAL7") || !strcmp(facility, "local7")) {
277+
PG(syslog_facility) = LOG_LOCAL7;
278+
return SUCCESS;
279+
}
280+
#endif
281+
282+
return FAILURE;
283+
}
284+
/* }}} */
285+
149286
/* {{{ PHP_INI_MH
150287
*/
151288
static PHP_INI_MH(OnSetPrecision)
@@ -636,6 +773,8 @@ PHP_INI_BEGIN()
636773
#ifdef PHP_WIN32
637774
STD_PHP_INI_BOOLEAN("windows.show_crt_warning", "0", PHP_INI_ALL, OnUpdateBool, windows_show_crt_warning, php_core_globals, core_globals)
638775
#endif
776+
STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
777+
STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
639778
PHP_INI_END()
640779
/* }}} */
641780

@@ -2135,6 +2274,8 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
21352274
CWDG(realpath_cache_size_limit) = 0;
21362275
}
21372276

2277+
PG(have_called_openlog) = 0;
2278+
21382279
/* initialize stream wrappers registry
21392280
* (this uses configuration parameters from php.ini)
21402281
*/

main/php_globals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ struct _php_core_globals {
166166
#ifdef PHP_WIN32
167167
zend_bool windows_show_crt_warning;
168168
#endif
169+
170+
zend_long syslog_facility;
171+
char *syslog_ident;
172+
zend_bool have_called_openlog;
169173
};
170174

171175

main/php_syslog.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
4242
{
4343
va_list args;
4444

45+
/*
46+
* don't rely on openlog() being called by syslog() if it's
47+
* not already been done; call it ourselves and pass the
48+
* correct parameters!
49+
*/
50+
if (!PG(have_called_openlog)) {
51+
php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
52+
}
53+
4554
va_start(args, format);
4655
vsyslog(priority, format, args);
4756
va_end(args);
@@ -56,6 +65,15 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
5665
smart_string sbuf = {0};
5766
va_list args;
5867

68+
/*
69+
* don't rely on openlog() being called by syslog() if it's
70+
* not already been done; call it ourselves and pass the
71+
* correct parameters!
72+
*/
73+
if (!PG(have_called_openlog)) {
74+
php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
75+
}
76+
5977
va_start(args, format);
6078
zend_printf_to_smart_string(&fbuf, format, args);
6179
smart_string_0(&fbuf);

main/php_syslog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
BEGIN_EXTERN_C()
3636
PHPAPI void php_syslog(int, const char *format, ...);
37+
PHPAPI void php_openlog(const char *, int, int);
3738
END_EXTERN_C()
3839

3940
#endif

php.ini-development

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ html_errors = On
577577
;error_log = php_errors.log
578578
; Log errors to syslog (Event Log on Windows).
579579
;error_log = syslog
580+
; the next two lines ony happen if the previous line sending errors to syslog
581+
; has been set as well.
582+
;syslog.ident = php
583+
;syslog.facility = user
580584

581585
;windows.show_crt_warning
582586
; Default value: 0

php.ini-production

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,12 @@ html_errors = On
584584
;error_log = php_errors.log
585585
; Log errors to syslog (Event Log on Windows).
586586
;error_log = syslog
587+
; Log errors to syslog (Event Log on Windows).
588+
;error_log = syslog
589+
; the next two lines ony happen if the previous line sending errors to syslog
590+
; has been set as well.
591+
;syslog.ident = php
592+
;syslog.facility = user
587593

588594
;windows.show_crt_warning
589595
; Default value: 0

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */
289289

290290
#ifdef HAVE_SYSLOG_H
291291
if (!strcasecmp(fpm_global_config.error_log, "syslog")) {
292-
openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility);
292+
php_openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility);
293293
fpm_globals.error_log_fd = ZLOG_SYSLOG;
294294
if (fpm_use_error_log()) {
295295
zlog_set_fd(fpm_globals.error_log_fd);

win32/wsyslog.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ void vsyslog(int priority, const char *message, va_list args)
9595
DWORD evid;
9696
wchar_t *strsw[2];
9797

98-
/* default event source */
99-
if (INVALID_HANDLE_VALUE == PW32G(log_source))
100-
openlog("php", LOG_PID, LOG_SYSLOG);
101-
10298
switch (priority) { /* translate UNIX type into NT type */
10399
case LOG_ALERT:
104100
etype = EVENTLOG_ERROR_TYPE;

0 commit comments

Comments
 (0)