xref: /PHP-5.3/ext/standard/syslog.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Stig S�ther Bakken <ssb@php.net>                             |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 
23 #ifdef HAVE_SYSLOG_H
24 #include "php_ini.h"
25 #include "zend_globals.h"
26 
27 #include <stdlib.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 
32 #include <string.h>
33 #include <errno.h>
34 
35 #include <stdio.h>
36 #include "basic_functions.h"
37 #include "php_ext_syslog.h"
38 
39 static void start_syslog(TSRMLS_D);
40 
41 /* {{{ PHP_MINIT_FUNCTION
42  */
PHP_MINIT_FUNCTION(syslog)43 PHP_MINIT_FUNCTION(syslog)
44 {
45 	/* error levels */
46 	REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
47 	REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
48 	REGISTER_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_CS | CONST_PERSISTENT); /* critical conditions */
49 	REGISTER_LONG_CONSTANT("LOG_ERR", LOG_ERR, CONST_CS | CONST_PERSISTENT);
50 	REGISTER_LONG_CONSTANT("LOG_WARNING", LOG_WARNING, CONST_CS | CONST_PERSISTENT);
51 	REGISTER_LONG_CONSTANT("LOG_NOTICE", LOG_NOTICE, CONST_CS | CONST_PERSISTENT);
52 	REGISTER_LONG_CONSTANT("LOG_INFO", LOG_INFO, CONST_CS | CONST_PERSISTENT);
53 	REGISTER_LONG_CONSTANT("LOG_DEBUG", LOG_DEBUG, CONST_CS | CONST_PERSISTENT);
54 	/* facility: type of program logging the message */
55 	REGISTER_LONG_CONSTANT("LOG_KERN", LOG_KERN, CONST_CS | CONST_PERSISTENT);
56 	REGISTER_LONG_CONSTANT("LOG_USER", LOG_USER, CONST_CS | CONST_PERSISTENT); /* generic user level */
57 	REGISTER_LONG_CONSTANT("LOG_MAIL", LOG_MAIL, CONST_CS | CONST_PERSISTENT); /* log to email */
58 	REGISTER_LONG_CONSTANT("LOG_DAEMON", LOG_DAEMON, CONST_CS | CONST_PERSISTENT); /* other system daemons */
59 	REGISTER_LONG_CONSTANT("LOG_AUTH", LOG_AUTH, CONST_CS | CONST_PERSISTENT);
60 	REGISTER_LONG_CONSTANT("LOG_SYSLOG", LOG_SYSLOG, CONST_CS | CONST_PERSISTENT);
61 	REGISTER_LONG_CONSTANT("LOG_LPR", LOG_LPR, CONST_CS | CONST_PERSISTENT);
62 #ifdef LOG_NEWS
63 	/* No LOG_NEWS on HP-UX */
64 	REGISTER_LONG_CONSTANT("LOG_NEWS", LOG_NEWS, CONST_CS | CONST_PERSISTENT); /* usenet new */
65 #endif
66 #ifdef LOG_UUCP
67 	/* No LOG_UUCP on HP-UX */
68 	REGISTER_LONG_CONSTANT("LOG_UUCP", LOG_UUCP, CONST_CS | CONST_PERSISTENT);
69 #endif
70 #ifdef LOG_CRON
71 	/* apparently some systems don't have this one */
72 	REGISTER_LONG_CONSTANT("LOG_CRON", LOG_CRON, CONST_CS | CONST_PERSISTENT);
73 #endif
74 #ifdef LOG_AUTHPRIV
75 	/* AIX doesn't have LOG_AUTHPRIV */
76 	REGISTER_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
77 #endif
78 #ifndef PHP_WIN32
79 	REGISTER_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
80 	REGISTER_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
81 	REGISTER_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
82 	REGISTER_LONG_CONSTANT("LOG_LOCAL3", LOG_LOCAL3, CONST_CS | CONST_PERSISTENT);
83 	REGISTER_LONG_CONSTANT("LOG_LOCAL4", LOG_LOCAL4, CONST_CS | CONST_PERSISTENT);
84 	REGISTER_LONG_CONSTANT("LOG_LOCAL5", LOG_LOCAL5, CONST_CS | CONST_PERSISTENT);
85 	REGISTER_LONG_CONSTANT("LOG_LOCAL6", LOG_LOCAL6, CONST_CS | CONST_PERSISTENT);
86 	REGISTER_LONG_CONSTANT("LOG_LOCAL7", LOG_LOCAL7, CONST_CS | CONST_PERSISTENT);
87 #endif
88 	/* options */
89 	REGISTER_LONG_CONSTANT("LOG_PID", LOG_PID, CONST_CS | CONST_PERSISTENT);
90 	REGISTER_LONG_CONSTANT("LOG_CONS", LOG_CONS, CONST_CS | CONST_PERSISTENT);
91 	REGISTER_LONG_CONSTANT("LOG_ODELAY", LOG_ODELAY, CONST_CS | CONST_PERSISTENT);
92 	REGISTER_LONG_CONSTANT("LOG_NDELAY", LOG_NDELAY, CONST_CS | CONST_PERSISTENT);
93 #ifdef LOG_NOWAIT
94 	REGISTER_LONG_CONSTANT("LOG_NOWAIT", LOG_NOWAIT, CONST_CS | CONST_PERSISTENT);
95 #endif
96 #ifdef LOG_PERROR
97 	/* AIX doesn't have LOG_PERROR */
98 	REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
99 #endif
100 	BG(syslog_device)=NULL;
101 
102 	return SUCCESS;
103 }
104 /* }}} */
105 
PHP_RINIT_FUNCTION(syslog)106 PHP_RINIT_FUNCTION(syslog)
107 {
108 	if (INI_INT("define_syslog_variables")) {
109 		start_syslog(TSRMLS_C);
110 	} else {
111 		BG(syslog_started)=0;
112 	}
113 	BG(syslog_device) = NULL;
114 	return SUCCESS;
115 }
116 
117 
118 #ifdef PHP_WIN32
PHP_RSHUTDOWN_FUNCTION(syslog)119 PHP_RSHUTDOWN_FUNCTION(syslog)
120 {
121 	closelog();
122 	return SUCCESS;
123 }
124 #endif
125 
PHP_MSHUTDOWN_FUNCTION(syslog)126 PHP_MSHUTDOWN_FUNCTION(syslog)
127 {
128 	if (BG(syslog_device)) {
129 		free(BG(syslog_device));
130 		BG(syslog_device) = NULL;
131 	}
132 	return SUCCESS;
133 }
134 
135 /* {{{ start_syslog
136  */
start_syslog(TSRMLS_D)137 static void start_syslog(TSRMLS_D)
138 {
139 	/* error levels */
140 	SET_VAR_LONG("LOG_EMERG", LOG_EMERG); /* system unusable */
141 	SET_VAR_LONG("LOG_ALERT", LOG_ALERT); /* immediate action required */
142 	SET_VAR_LONG("LOG_CRIT", LOG_CRIT); /* critical conditions */
143 	SET_VAR_LONG("LOG_ERR", LOG_ERR);
144 	SET_VAR_LONG("LOG_WARNING", LOG_WARNING);
145 	SET_VAR_LONG("LOG_NOTICE", LOG_NOTICE);
146 	SET_VAR_LONG("LOG_INFO", LOG_INFO);
147 	SET_VAR_LONG("LOG_DEBUG", LOG_DEBUG);
148 	/* facility: type of program logging the message */
149 	SET_VAR_LONG("LOG_KERN", LOG_KERN);
150 	SET_VAR_LONG("LOG_USER", LOG_USER); /* generic user level */
151 	SET_VAR_LONG("LOG_MAIL", LOG_MAIL); /* log to email */
152 	SET_VAR_LONG("LOG_DAEMON", LOG_DAEMON); /* other system daemons */
153 	SET_VAR_LONG("LOG_AUTH", LOG_AUTH);
154 #ifndef NETWARE
155 	SET_VAR_LONG("LOG_SYSLOG", LOG_SYSLOG);
156 #endif
157 	SET_VAR_LONG("LOG_LPR", LOG_LPR);
158 #ifdef LOG_NEWS
159 	/* No LOG_NEWS on HP-UX */
160 	SET_VAR_LONG("LOG_NEWS", LOG_NEWS); /* usenet new */
161 #endif
162 #ifdef LOG_UUCP
163 	/* No LOG_UUCP on HP-UX */
164 	SET_VAR_LONG("LOG_UUCP", LOG_UUCP);
165 #endif
166 #ifdef LOG_CRON
167 	/* apparently some systems don't have this one */
168 	SET_VAR_LONG("LOG_CRON", LOG_CRON);
169 #endif
170 #ifdef LOG_AUTHPRIV
171 	/* AIX doesn't have LOG_AUTHPRIV */
172 	SET_VAR_LONG("LOG_AUTHPRIV", LOG_AUTHPRIV);
173 #endif
174 #if !defined(PHP_WIN32) && !defined(NETWARE)
175 	SET_VAR_LONG("LOG_LOCAL0", LOG_LOCAL0);
176 	SET_VAR_LONG("LOG_LOCAL1", LOG_LOCAL1);
177 	SET_VAR_LONG("LOG_LOCAL2", LOG_LOCAL2);
178 	SET_VAR_LONG("LOG_LOCAL3", LOG_LOCAL3);
179 	SET_VAR_LONG("LOG_LOCAL4", LOG_LOCAL4);
180 	SET_VAR_LONG("LOG_LOCAL5", LOG_LOCAL5);
181 	SET_VAR_LONG("LOG_LOCAL6", LOG_LOCAL6);
182 	SET_VAR_LONG("LOG_LOCAL7", LOG_LOCAL7);
183 #endif
184 	/* options */
185 	SET_VAR_LONG("LOG_PID", LOG_PID);
186 	SET_VAR_LONG("LOG_CONS", LOG_CONS);
187 	SET_VAR_LONG("LOG_ODELAY", LOG_ODELAY);
188 	SET_VAR_LONG("LOG_NDELAY", LOG_NDELAY);
189 #ifdef LOG_NOWAIT
190 	/* BeOS doesn't have LOG_NOWAIT */
191 	SET_VAR_LONG("LOG_NOWAIT", LOG_NOWAIT);
192 #endif
193 #ifdef LOG_PERROR
194 	/* AIX doesn't have LOG_PERROR */
195 	SET_VAR_LONG("LOG_PERROR", LOG_PERROR); /*log to stderr*/
196 #endif
197 
198 	BG(syslog_started)=1;
199 }
200 /* }}} */
201 
202 /* {{{ proto void define_syslog_variables(void)
203    Initializes all syslog-related variables */
PHP_FUNCTION(define_syslog_variables)204 PHP_FUNCTION(define_syslog_variables)
205 {
206 	if (zend_parse_parameters_none() == FAILURE) {
207 		return;
208 	}
209 
210 	if (!BG(syslog_started)) {
211 		start_syslog(TSRMLS_C);
212 	}
213 }
214 /* }}} */
215 
216 /* {{{ proto bool openlog(string ident, int option, int facility)
217    Open connection to system logger */
218 /*
219    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
220    ** Syslog($LOG_EMERG, "help me!")
221    ** CloseLog();
222  */
PHP_FUNCTION(openlog)223 PHP_FUNCTION(openlog)
224 {
225 	char *ident;
226 	long option, facility;
227 	int ident_len;
228 
229 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &ident,
230 							  &ident_len, &option, &facility) == FAILURE) {
231 		return;
232 	}
233 	if (BG(syslog_device)) {
234 		free(BG(syslog_device));
235 	}
236 	BG(syslog_device) = zend_strndup(ident, ident_len);
237 	openlog(BG(syslog_device), option, facility);
238 	RETURN_TRUE;
239 }
240 /* }}} */
241 
242 /* {{{ proto bool closelog(void)
243    Close connection to system logger */
PHP_FUNCTION(closelog)244 PHP_FUNCTION(closelog)
245 {
246 	if (zend_parse_parameters_none() == FAILURE) {
247 		return;
248 	}
249 
250 	closelog();
251 	if (BG(syslog_device)) {
252 		free(BG(syslog_device));
253 		BG(syslog_device)=NULL;
254 	}
255 	RETURN_TRUE;
256 }
257 /* }}} */
258 
259 /* {{{ proto bool syslog(int priority, string message)
260    Generate a system log message */
PHP_FUNCTION(syslog)261 PHP_FUNCTION(syslog)
262 {
263 	long priority;
264 	char *message;
265 	int message_len;
266 
267 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &priority,
268 							  &message, &message_len) == FAILURE) {
269 		return;
270 	}
271 
272 	php_syslog(priority, "%s", message);
273 	RETURN_TRUE;
274 }
275 /* }}} */
276 
277 #endif
278 
279 /*
280  * Local variables:
281  * tab-width: 4
282  * c-basic-offset: 4
283  * End:
284  * vim600: sw=4 ts=4 fdm=marker
285  * vim<600: sw=4 ts=4
286  */
287