xref: /PHP-8.0/ext/standard/syslog.c (revision e330f443)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Stig Sæther Bakken <ssb@php.net>                             |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "php.h"
18 
19 #ifdef HAVE_SYSLOG_H
20 #include "php_ini.h"
21 #include "zend_globals.h"
22 
23 #include <stdlib.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
28 #include <string.h>
29 #include <errno.h>
30 
31 #include <stdio.h>
32 #include "basic_functions.h"
33 #include "php_ext_syslog.h"
34 
35 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(syslog)36 PHP_MINIT_FUNCTION(syslog)
37 {
38 	/* error levels */
39 	REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
40 	REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
41 	REGISTER_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_CS | CONST_PERSISTENT); /* critical conditions */
42 	REGISTER_LONG_CONSTANT("LOG_ERR", LOG_ERR, CONST_CS | CONST_PERSISTENT);
43 	REGISTER_LONG_CONSTANT("LOG_WARNING", LOG_WARNING, CONST_CS | CONST_PERSISTENT);
44 	REGISTER_LONG_CONSTANT("LOG_NOTICE", LOG_NOTICE, CONST_CS | CONST_PERSISTENT);
45 	REGISTER_LONG_CONSTANT("LOG_INFO", LOG_INFO, CONST_CS | CONST_PERSISTENT);
46 	REGISTER_LONG_CONSTANT("LOG_DEBUG", LOG_DEBUG, CONST_CS | CONST_PERSISTENT);
47 	/* facility: type of program logging the message */
48 	REGISTER_LONG_CONSTANT("LOG_KERN", LOG_KERN, CONST_CS | CONST_PERSISTENT);
49 	REGISTER_LONG_CONSTANT("LOG_USER", LOG_USER, CONST_CS | CONST_PERSISTENT); /* generic user level */
50 	REGISTER_LONG_CONSTANT("LOG_MAIL", LOG_MAIL, CONST_CS | CONST_PERSISTENT); /* log to email */
51 	REGISTER_LONG_CONSTANT("LOG_DAEMON", LOG_DAEMON, CONST_CS | CONST_PERSISTENT); /* other system daemons */
52 	REGISTER_LONG_CONSTANT("LOG_AUTH", LOG_AUTH, CONST_CS | CONST_PERSISTENT);
53 	REGISTER_LONG_CONSTANT("LOG_SYSLOG", LOG_SYSLOG, CONST_CS | CONST_PERSISTENT);
54 	REGISTER_LONG_CONSTANT("LOG_LPR", LOG_LPR, CONST_CS | CONST_PERSISTENT);
55 #ifdef LOG_NEWS
56 	/* No LOG_NEWS on HP-UX */
57 	REGISTER_LONG_CONSTANT("LOG_NEWS", LOG_NEWS, CONST_CS | CONST_PERSISTENT); /* usenet new */
58 #endif
59 #ifdef LOG_UUCP
60 	/* No LOG_UUCP on HP-UX */
61 	REGISTER_LONG_CONSTANT("LOG_UUCP", LOG_UUCP, CONST_CS | CONST_PERSISTENT);
62 #endif
63 #ifdef LOG_CRON
64 	/* apparently some systems don't have this one */
65 	REGISTER_LONG_CONSTANT("LOG_CRON", LOG_CRON, CONST_CS | CONST_PERSISTENT);
66 #endif
67 #ifdef LOG_AUTHPRIV
68 	/* AIX doesn't have LOG_AUTHPRIV */
69 	REGISTER_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
70 #endif
71 #ifndef PHP_WIN32
72 	REGISTER_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
73 	REGISTER_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
74 	REGISTER_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
75 	REGISTER_LONG_CONSTANT("LOG_LOCAL3", LOG_LOCAL3, CONST_CS | CONST_PERSISTENT);
76 	REGISTER_LONG_CONSTANT("LOG_LOCAL4", LOG_LOCAL4, CONST_CS | CONST_PERSISTENT);
77 	REGISTER_LONG_CONSTANT("LOG_LOCAL5", LOG_LOCAL5, CONST_CS | CONST_PERSISTENT);
78 	REGISTER_LONG_CONSTANT("LOG_LOCAL6", LOG_LOCAL6, CONST_CS | CONST_PERSISTENT);
79 	REGISTER_LONG_CONSTANT("LOG_LOCAL7", LOG_LOCAL7, CONST_CS | CONST_PERSISTENT);
80 #endif
81 	/* options */
82 	REGISTER_LONG_CONSTANT("LOG_PID", LOG_PID, CONST_CS | CONST_PERSISTENT);
83 	REGISTER_LONG_CONSTANT("LOG_CONS", LOG_CONS, CONST_CS | CONST_PERSISTENT);
84 	REGISTER_LONG_CONSTANT("LOG_ODELAY", LOG_ODELAY, CONST_CS | CONST_PERSISTENT);
85 	REGISTER_LONG_CONSTANT("LOG_NDELAY", LOG_NDELAY, CONST_CS | CONST_PERSISTENT);
86 #ifdef LOG_NOWAIT
87 	REGISTER_LONG_CONSTANT("LOG_NOWAIT", LOG_NOWAIT, CONST_CS | CONST_PERSISTENT);
88 #endif
89 #ifdef LOG_PERROR
90 	/* AIX doesn't have LOG_PERROR */
91 	REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
92 #endif
93 	BG(syslog_device)=NULL;
94 
95 	return SUCCESS;
96 }
97 /* }}} */
98 
PHP_RINIT_FUNCTION(syslog)99 PHP_RINIT_FUNCTION(syslog)
100 {
101 	BG(syslog_device) = NULL;
102 	return SUCCESS;
103 }
104 
105 
106 #ifdef PHP_WIN32
PHP_RSHUTDOWN_FUNCTION(syslog)107 PHP_RSHUTDOWN_FUNCTION(syslog)
108 {
109 	closelog();
110 	return SUCCESS;
111 }
112 #endif
113 
PHP_MSHUTDOWN_FUNCTION(syslog)114 PHP_MSHUTDOWN_FUNCTION(syslog)
115 {
116 	if (BG(syslog_device)) {
117 		free(BG(syslog_device));
118 		BG(syslog_device) = NULL;
119 	}
120 	return SUCCESS;
121 }
122 
123 
124 /* {{{ Open connection to system logger */
125 /*
126    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
127    ** Syslog($LOG_EMERG, "help me!")
128    ** CloseLog();
129  */
PHP_FUNCTION(openlog)130 PHP_FUNCTION(openlog)
131 {
132 	char *ident;
133 	zend_long option, facility;
134 	size_t ident_len;
135 
136 	ZEND_PARSE_PARAMETERS_START(3, 3)
137 		Z_PARAM_STRING(ident, ident_len)
138 		Z_PARAM_LONG(option)
139 		Z_PARAM_LONG(facility)
140 	ZEND_PARSE_PARAMETERS_END();
141 
142 	if (BG(syslog_device)) {
143 		free(BG(syslog_device));
144 	}
145 	BG(syslog_device) = zend_strndup(ident, ident_len);
146 	if(BG(syslog_device) == NULL) {
147 		RETURN_FALSE;
148 	}
149 	php_openlog(BG(syslog_device), option, facility);
150 	RETURN_TRUE;
151 }
152 /* }}} */
153 
154 /* {{{ Close connection to system logger */
PHP_FUNCTION(closelog)155 PHP_FUNCTION(closelog)
156 {
157 	ZEND_PARSE_PARAMETERS_NONE();
158 
159 	php_closelog();
160 	if (BG(syslog_device)) {
161 		free(BG(syslog_device));
162 		BG(syslog_device)=NULL;
163 	}
164 	RETURN_TRUE;
165 }
166 /* }}} */
167 
168 /* {{{ Generate a system log message */
PHP_FUNCTION(syslog)169 PHP_FUNCTION(syslog)
170 {
171 	zend_long priority;
172 	char *message;
173 	size_t message_len;
174 
175 	ZEND_PARSE_PARAMETERS_START(2, 2)
176 		Z_PARAM_LONG(priority)
177 		Z_PARAM_STRING(message, message_len)
178 	ZEND_PARSE_PARAMETERS_END();
179 
180 	php_syslog(priority, "%s", message);
181 	RETURN_TRUE;
182 }
183 /* }}} */
184 
185 #endif
186