php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #51118
Patch php-syslog.v4.patch.txt revision 2011-07-05 13:31 UTC by fat@php.net
Patch php-syslog.patch revision 2011-01-30 13:31 UTC by fat@php.net
revision 2011-01-30 13:23 UTC by fat@php.net
revision 2011-01-30 10:58 UTC by fat@php.net
Patch php_syslog_multiple_context.patch revision 2011-01-29 13:02 UTC by fat@php.net
revision 2010-07-23 03:23 UTC by fat@php.net

Patch php-syslog.v4.patch.txt for *General Issues Bug #51118

Patch version 2011-07-05 13:31 UTC

Return to Bug #51118 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: fat@php.net

Index: sapi/milter/php_milter.c
===================================================================
--- sapi/milter/php_milter.c	(revision 312930)
+++ sapi/milter/php_milter.c	(working copy)
@@ -1162,25 +1162,23 @@
 			if (stat(sock,&junk) == 0) unlink(sock);
 		}
 
-		openlog("php-milter", LOG_PID, LOG_MAIL);
-		
 		if ((exit_status = mlfi_init())) {
-			syslog(1, "mlfi_init failed.");
-			closelog();
+			php_syslog2("php-milter", LOG_PID | LOG_CONS, LOG_MAIL, 1, "mlfi_init failed.");
+			php_closelog();
 			goto err;
 		}
 
 		smfi_setconn(sock);
 		if (smfi_register(smfilter) == MI_FAILURE) {
-			syslog(1, "smfi_register failed.");
+			php_syslog2("php-milter", LOG_PID | LOG_CONS, LOG_MAIL, 1, "smfi_register failed.");
 			fprintf(stderr, "smfi_register failed\n");
-			closelog();
+			php_closelog();
 			goto err;
 		} else {
 			exit_status = smfi_main();
 		}			
 
-		closelog();
+		php_closelog();
 
 		if (milter_sapi_module.php_ini_path_override) {
 			free(milter_sapi_module.php_ini_path_override);
Index: ext/standard/basic_functions.h
===================================================================
--- ext/standard/basic_functions.h	(revision 312930)
+++ ext/standard/basic_functions.h	(working copy)
@@ -200,7 +200,9 @@
 	zend_bool mt_rand_is_seeded; /* Whether mt_rand() has been seeded */
     
 	/* syslog.c */
-	char *syslog_device;
+	char *syslog_ident;
+	int syslog_option;
+	int syslog_facility;
 
 	/* var.c */
 	zend_class_entry *incomplete_class;
Index: ext/standard/syslog.c
===================================================================
--- ext/standard/syslog.c	(revision 312930)
+++ ext/standard/syslog.c	(working copy)
@@ -36,10 +36,26 @@
 #include "basic_functions.h"
 #include "php_ext_syslog.h"
 
+static int le_syslog;
+
+static void php_syslog_context_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+{
+	php_syslog_context *context = (php_syslog_context*)rsrc->ptr;
+
+	if (context) {
+		if (context->ident) {
+			efree(context->ident);
+		}
+		efree(context);
+	}
+}
+
 /* {{{ PHP_MINIT_FUNCTION
  */
 PHP_MINIT_FUNCTION(syslog)
 {
+	le_syslog = zend_register_list_destructors_ex(php_syslog_context_dtor, NULL, PHP_SYSLOG_RES_NAME, module_number);
+
 	/* error levels */
 	REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
 	REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
@@ -95,7 +111,9 @@
 	/* AIX doesn't have LOG_PERROR */
 	REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
 #endif
-	BG(syslog_device)=NULL;
+	BG(syslog_ident) = NULL;
+	BG(syslog_option) = 0;
+	BG(syslog_facility) = 0;
 
 	return SUCCESS;
 }
@@ -103,7 +121,9 @@
 
 PHP_RINIT_FUNCTION(syslog)
 {
-	BG(syslog_device) = NULL;
+	BG(syslog_ident) = NULL;
+	BG(syslog_option) = 0;
+	BG(syslog_facility) = 0;
 	return SUCCESS;
 }
 
@@ -111,17 +131,19 @@
 #ifdef PHP_WIN32
 PHP_RSHUTDOWN_FUNCTION(syslog)
 {
-	closelog();
+	php_closelog();
 	return SUCCESS;
 }
 #endif
 
 PHP_MSHUTDOWN_FUNCTION(syslog)
 {
-	if (BG(syslog_device)) {
-		free(BG(syslog_device));
-		BG(syslog_device) = NULL;
+	if (BG(syslog_ident)) {
+		free(BG(syslog_ident));
+		BG(syslog_ident) = NULL;
 	}
+	BG(syslog_option)=0;
+	BG(syslog_facility)=0;
 	return SUCCESS;
 }
 
@@ -137,17 +159,27 @@
 	char *ident;
 	long option, facility;
 	int ident_len;
+	php_syslog_context *context;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &ident,
 							  &ident_len, &option, &facility) == FAILURE) {
 		return;
 	}
-	if (BG(syslog_device)) {
-		free(BG(syslog_device));
+	if (BG(syslog_ident)) {
+		free(BG(syslog_ident));
 	}
-	BG(syslog_device) = zend_strndup(ident, ident_len);
-	openlog(BG(syslog_device), option, facility);
-	RETURN_TRUE;
+	BG(syslog_ident) = zend_strndup(ident, ident_len);
+	BG(syslog_option) = option;
+	BG(syslog_facility) = facility;
+
+	php_openlog(BG(syslog_ident), option, facility);
+
+	context = emalloc(sizeof(php_syslog_context));
+	context->ident = estrndup(ident, ident_len);
+	context->options = option;
+	context->facility = facility;
+
+	ZEND_REGISTER_RESOURCE(return_value, context, le_syslog);
 }
 /* }}} */
 
@@ -155,15 +187,31 @@
    Close connection to system logger */
 PHP_FUNCTION(closelog)
 {
-	if (zend_parse_parameters_none() == FAILURE) {
+	zval *zcontext = NULL;
+	php_syslog_context *context;
+	int close = 1;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zcontext) == FAILURE) {
 		return;
 	}
 
-	closelog();
-	if (BG(syslog_device)) {
-		free(BG(syslog_device));
-		BG(syslog_device)=NULL;
+	if (zcontext) {
+		ZEND_FETCH_RESOURCE(context, php_syslog_context*, &zcontext, -1, PHP_SYSLOG_RES_NAME, le_syslog);
+		if (context && strcmp(context->ident, BG(syslog_ident)) != 0) {
+			close = 0;
+		}
+		zend_list_delete(Z_LVAL_P(zcontext));
 	}
+
+	if (close) {
+		php_closelog();
+		if (BG(syslog_ident)) {
+			free(BG(syslog_ident));
+			BG(syslog_ident)=NULL;
+		}
+		BG(syslog_option)=0;
+		BG(syslog_facility)=0;
+	}
 	RETURN_TRUE;
 }
 /* }}} */
@@ -175,13 +223,23 @@
 	long priority;
 	char *message;
 	int message_len;
+	zval *zcontext = NULL;
+	php_syslog_context *context;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &priority,
-							  &message, &message_len) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|r", &priority,
+							  &message, &message_len, &zcontext) == FAILURE) {
 		return;
 	}
 
-	php_syslog(priority, "%s", message);
+	if (zcontext) {
+		ZEND_FETCH_RESOURCE(context, php_syslog_context*, &zcontext, -1, PHP_SYSLOG_RES_NAME, le_syslog);
+		if (context) {
+			php_syslog2(context->ident, context->options, context->facility, priority, "%s", message);
+			RETURN_TRUE;
+		}
+	}
+
+	php_syslog2(BG(syslog_ident), BG(syslog_option), BG(syslog_facility), priority, "%s", message);
 	RETURN_TRUE;
 }
 /* }}} */
Index: ext/standard/php_ext_syslog.h
===================================================================
--- ext/standard/php_ext_syslog.h	(revision 312930)
+++ ext/standard/php_ext_syslog.h	(working copy)
@@ -25,6 +25,14 @@
 
 #include "php_syslog.h"
 
+typedef struct _php_syslog_data {
+	char *ident;
+	int options;
+	int facility;
+} php_syslog_context;
+
+#define PHP_SYSLOG_RES_NAME        "SysLog Context"
+
 PHP_MINIT_FUNCTION(syslog);
 PHP_RINIT_FUNCTION(syslog);
 #ifdef PHP_WIN32
Index: configure.in
===================================================================
--- configure.in	(revision 312930)
+++ configure.in	(working copy)
@@ -1445,7 +1445,7 @@
        php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
        strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c \
        network.c php_open_temporary_file.c php_logos.c \
-       output.c getopt.c)
+       output.c getopt.c php_syslog.c)
 
 PHP_ADD_SOURCES(main/streams, streams.c cast.c memory.c filter.c \
        plain_wrapper.c userspace.c transports.c xp_socket.c mmap.c \
Index: php.ini-production
===================================================================
--- php.ini-production	(revision 312930)
+++ php.ini-production	(working copy)
@@ -582,6 +582,12 @@
 ; Log errors to syslog (Event Log on NT, not valid in Windows 95).
 ;error_log = syslog
 
+; Set the syslog facility when error_log is set to 'syslog'
+;error_log_facility = LOG_USER
+
+; Set the syslog ident when error_log is set to 'syslog'
+;error_log_facility = php
+
 ;;;;;;;;;;;;;;;;;
 ; Data Handling ;
 ;;;;;;;;;;;;;;;;;
Index: main/php_globals.h
===================================================================
--- main/php_globals.h	(revision 312930)
+++ main/php_globals.h	(working copy)
@@ -160,6 +160,15 @@
 	char *mail_log;
 
 	zend_bool in_error_log;
+
+#ifdef HAVE_SYSLOG_H
+	char *error_log_ident;
+	long error_log_facility;
+
+	char *syslog_ident;
+	int syslog_facility;
+	int syslog_option;
+#endif
 };
 
 
Index: main/main.c
===================================================================
--- main/main.c	(revision 312930)
+++ main/main.c	(working copy)
@@ -244,7 +244,18 @@
 }
 /* }}} */
 
+#ifdef HAVE_SYSLOG_H
 /* {{{ PHP_INI_MH
+*/
+static PHP_INI_MH(OnUpdateSyslog)
+{
+	PG(error_log_facility) = php_syslog_get_facility_from_string(new_value TSRMLS_CC);
+	return SUCCESS;
+}
+/* }}} */
+#endif
+
+/* {{{ PHP_INI_MH
  */
 static PHP_INI_MH(OnUpdateTimeout)
 {
@@ -462,6 +473,10 @@
 	STD_PHP_INI_ENTRY("default_charset",		SAPI_DEFAULT_CHARSET,	PHP_INI_ALL,	OnUpdateString,			default_charset,		sapi_globals_struct,sapi_globals)
 	STD_PHP_INI_ENTRY("default_mimetype",		SAPI_DEFAULT_MIMETYPE,	PHP_INI_ALL,	OnUpdateString,			default_mimetype,		sapi_globals_struct,sapi_globals)
 	STD_PHP_INI_ENTRY("error_log",				NULL,		PHP_INI_ALL,		OnUpdateErrorLog,			error_log,				php_core_globals,	core_globals)
+#ifdef HAVE_SYSLOG_H
+	PHP_INI_ENTRY("error_log_facility",		"LOG_USER",	PHP_INI_ALL,		OnUpdateSyslog)
+	STD_PHP_INI_ENTRY("error_log_ident",		"php",		PHP_INI_ALL,		OnUpdateString,				error_log_ident,		php_core_globals,	core_globals)
+#endif
 	STD_PHP_INI_ENTRY("extension_dir",			PHP_EXTENSION_DIR,		PHP_INI_SYSTEM,		OnUpdateStringUnempty,	extension_dir,			php_core_globals,	core_globals)
 	STD_PHP_INI_ENTRY("include_path",			PHP_INCLUDE_PATH,		PHP_INI_ALL,		OnUpdateStringUnempty,	include_path,			php_core_globals,	core_globals)
 	PHP_INI_ENTRY("max_execution_time",			"30",		PHP_INI_ALL,			OnUpdateTimeout)
@@ -552,7 +567,7 @@
 	if (PG(error_log) != NULL) {
 #ifdef HAVE_SYSLOG_H
 		if (!strcmp(PG(error_log), "syslog")) {
-			php_syslog(LOG_NOTICE, "%s", log_message);
+			php_syslog2(INI_STR("error_log_ident"), LOG_PID | LOG_CONS, PG(error_log_facility), LOG_NOTICE, "%s", log_message);
 			PG(in_error_log) = 0;
 			return;
 		}
@@ -1776,6 +1791,7 @@
 	memset(core_globals, 0, sizeof(*core_globals));
 
 	php_startup_ticks(TSRMLS_C);
+	php_startup_syslog(TSRMLS_C);
 }
 /* }}} */
 #endif
@@ -1797,6 +1813,7 @@
 		free(core_globals->disable_classes);
 	}
 
+	php_shutdown_syslog(TSRMLS_C);
 	php_shutdown_ticks(TSRMLS_C);
 }
 /* }}} */
Index: main/php_syslog.c
===================================================================
--- main/php_syslog.c	(revision 0)
+++ main/php_syslog.c	(revision 0)
@@ -0,0 +1,226 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2011 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.01 of the PHP license,      |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_01.txt                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Author: Jerome Loyet <fat@php.net>                                   |
+   +----------------------------------------------------------------------+
+*/
+
+/* $Id$ */
+
+#include "php.h"
+#include "php_syslog.h"
+
+#define SET_LOG(a) { #a, a },
+
+struct facility_name {
+	char *name;
+	int value;
+};
+
+static const struct facility_name facility_names[] = {
+#ifdef LOG_AUTH
+	SET_LOG(LOG_AUTH)
+#endif
+#ifdef LOG_AUTHPRIV
+	SET_LOG(LOG_AUTHPRIV)
+#endif
+#ifdef LOG_CRON
+	SET_LOG(LOG_CRON)
+#endif
+#ifdef LOG_DAEMON
+	SET_LOG(LOG_DAEMON)
+#endif
+#ifdef LOG_FTP
+	SET_LOG(LOG_FTP)
+#endif
+#ifdef LOG_KERN
+	SET_LOG(LOG_KERN)
+#endif
+#ifdef LOG_LRP
+	SET_LOG(LOG_LRP)
+#endif
+#ifdef LOG_MAIL
+	SET_LOG(LOG_MAIL)
+#endif
+#ifdef LOG_NEWS
+	SET_LOG(LOG_NEWS)
+#endif
+#ifdef LOG_SYSLOG
+	SET_LOG(LOG_SYSLOG)
+#endif
+#ifdef LOG_USER
+	SET_LOG(LOG_USER)
+#endif
+#ifdef LOG_UUCP
+	SET_LOG(LOG_UUCP)
+#endif
+#ifdef LOG_LOCAL0
+	SET_LOG(LOG_LOCAL0)
+#endif
+#ifdef LOG_LOCAL1
+	SET_LOG(LOG_LOCAL1)
+#endif
+#ifdef LOG_LOCAL2
+	SET_LOG(LOG_LOCAL2)
+#endif
+#ifdef LOG_LOCAL3
+	SET_LOG(LOG_LOCAL3)
+#endif
+#ifdef LOG_LOCAL4
+	SET_LOG(LOG_LOCAL4)
+#endif
+#ifdef LOG_LOCAL5
+	SET_LOG(LOG_LOCAL5)
+#endif
+#ifdef LOG_LOCAL6
+	SET_LOG(LOG_LOCAL6)
+#endif
+#ifdef LOG_LOCAL7
+	SET_LOG(LOG_LOCAL7)
+#endif
+	
+};
+
+void php_shutdown_syslog(TSRMLS_D) /* {{{ */
+{
+	if (PG(syslog_ident)) {
+		free(PG(syslog_ident));
+		PG(syslog_ident) = NULL;
+	}
+}
+/* }}} */
+
+void php_startup_syslog(TSRMLS_D) /* {{{ */
+{
+	PG(syslog_ident) = NULL;
+	PG(syslog_option) = 0;
+	PG(syslog_facility) = 0;
+}
+/* }}} */
+
+int php_syslog_get_facility_from_string(char *facility TSRMLS_DC) /* {{{ */
+{
+	int i;
+	if (facility) {
+
+		for (i = 0; i < sizeof(facility_names) / sizeof(struct facility_name); i++) {
+
+			if (!strcasecmp(facility, facility_names[i].name)) {
+				return facility_names[i].value;
+			}
+
+			if (strstr(facility_names[i].name, "LOG_") == facility_names[i].name) {
+				char *tmp = (char *)(facility_names[i].name + strlen("LOG_"));
+
+				if (strlen(tmp) > 0 && !strcasecmp(facility, tmp)) {
+					return facility_names[i].value;
+				}
+			}
+		}
+	}
+#ifdef LOG_USER
+	/* normaly, LOG_USER is defined */
+	return LOG_USER;
+#else
+	/* LOG_USER seems to be defined as (1<<3) on many systems */
+	return (1<<3);
+#endif
+}
+
+PHPAPI void php_openlog(char *ident, int option, int facility) /* {{{ */
+{
+	TSRMLS_FETCH();
+
+	/* save current context */
+	if (PG(syslog_ident)) {
+		free(PG(syslog_ident));
+	}
+	PG(syslog_ident) =  ident ? strdup(ident) : NULL;
+	PG(syslog_option) = option;
+	PG(syslog_facility) = facility;
+
+	/* then openlog */
+	openlog(ident, option, facility);
+}
+/* }}} */
+
+PHPAPI void php_syslog2(char *ident, int option, int facility, int priority, const char *fmt, ...) /* {{{ */
+{
+	TSRMLS_FETCH();
+	va_list args;
+
+	if (!ident) {
+		ident = INI_STR("error_log_ident");
+		option = LOG_PID | LOG_CONS;
+		facility = php_syslog_get_facility_from_string(INI_STR("error_log_facility") TSRMLS_CC);
+	}
+
+	if (ident) {
+		if (!PG(syslog_ident) || strcmp(ident, PG(syslog_ident)) !=0 || option != PG(syslog_option) || facility != PG(syslog_facility)) {
+			php_openlog(ident, option, facility);
+		}
+	}
+
+	va_start(args, fmt);
+	php_vsyslog(priority, fmt, args);
+	va_end(args);
+}
+/* }}} */
+
+
+PHPAPI void php_closelog() /* {{{ */
+{
+	TSRMLS_FETCH();
+	/* clear context */
+	if (PG(syslog_ident)) {
+		free(PG(syslog_ident));
+	}
+	PG(syslog_ident) =  NULL;
+	PG(syslog_option) = 0;
+	PG(syslog_facility) = 0;
+
+	/* then closelog */
+	closelog();
+}
+/* }}} */
+
+PHPAPI char *php_syslog_get_ident() /* {{{ */
+{
+	TSRMLS_FETCH();
+	return PG(syslog_ident);
+}
+/* }}} */
+
+PHPAPI int php_syslog_get_option() /* {{{ */
+{
+	TSRMLS_FETCH();
+	return PG(syslog_option);
+}
+/* }}} */
+
+PHPAPI int php_syslog_get_facility() /* {{{ */
+{
+	TSRMLS_FETCH();
+	return PG(syslog_facility);
+}
+/* }}} */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
Index: main/php_syslog.h
===================================================================
--- main/php_syslog.h	(revision 312930)
+++ main/php_syslog.h	(working copy)
@@ -30,6 +30,21 @@
 #endif
 #endif
 
+
+
+void php_shutdown_syslog(TSRMLS_D);
+void php_startup_syslog(TSRMLS_D);
+int php_syslog_get_facility_from_string(char *facility TSRMLS_DC);
+
+BEGIN_EXTERN_C()
+PHPAPI void php_openlog(char *ident, int option, int facility);
+PHPAPI void php_syslog2(char *ident, int option, int facility, int priority, const char *fmt, ...);
+PHPAPI void php_closelog();
+PHPAPI char *php_syslog_get_ident();
+PHPAPI int php_syslog_get_option();
+PHPAPI int php_syslog_get_facility();
+END_EXTERN_C()
+
 /* 
  * The SCO OpenServer 5 Development System (not the UDK)
  * defines syslog to std_syslog.
@@ -49,4 +64,8 @@
 #define php_syslog syslog
 #endif
 
+#ifndef php_vsyslog
+#define php_vsyslog vsyslog
 #endif
+
+#endif
Index: php.ini-development
===================================================================
--- php.ini-development	(revision 312930)
+++ php.ini-development	(working copy)
@@ -582,6 +582,12 @@
 ; Log errors to syslog (Event Log on NT, not valid in Windows 95).
 ;error_log = syslog
 
+; Set the syslog facility when error_log is set to 'syslog'
+;error_log_facility = LOG_USER
+
+; Set the syslog ident when error_log is set to 'syslog'
+;error_log_facility = php
+
 ;;;;;;;;;;;;;;;;;
 ; Data Handling ;
 ;;;;;;;;;;;;;;;;;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 03:01:27 2024 UTC