xref: /PHP-7.2/ext/standard/syslog.c (revision 902d39a3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 /* {{{ PHP_MINIT_FUNCTION
40  */
PHP_MINIT_FUNCTION(syslog)41 PHP_MINIT_FUNCTION(syslog)
42 {
43 	/* error levels */
44 	REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
45 	REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
46 	REGISTER_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_CS | CONST_PERSISTENT); /* critical conditions */
47 	REGISTER_LONG_CONSTANT("LOG_ERR", LOG_ERR, CONST_CS | CONST_PERSISTENT);
48 	REGISTER_LONG_CONSTANT("LOG_WARNING", LOG_WARNING, CONST_CS | CONST_PERSISTENT);
49 	REGISTER_LONG_CONSTANT("LOG_NOTICE", LOG_NOTICE, CONST_CS | CONST_PERSISTENT);
50 	REGISTER_LONG_CONSTANT("LOG_INFO", LOG_INFO, CONST_CS | CONST_PERSISTENT);
51 	REGISTER_LONG_CONSTANT("LOG_DEBUG", LOG_DEBUG, CONST_CS | CONST_PERSISTENT);
52 	/* facility: type of program logging the message */
53 	REGISTER_LONG_CONSTANT("LOG_KERN", LOG_KERN, CONST_CS | CONST_PERSISTENT);
54 	REGISTER_LONG_CONSTANT("LOG_USER", LOG_USER, CONST_CS | CONST_PERSISTENT); /* generic user level */
55 	REGISTER_LONG_CONSTANT("LOG_MAIL", LOG_MAIL, CONST_CS | CONST_PERSISTENT); /* log to email */
56 	REGISTER_LONG_CONSTANT("LOG_DAEMON", LOG_DAEMON, CONST_CS | CONST_PERSISTENT); /* other system daemons */
57 	REGISTER_LONG_CONSTANT("LOG_AUTH", LOG_AUTH, CONST_CS | CONST_PERSISTENT);
58 	REGISTER_LONG_CONSTANT("LOG_SYSLOG", LOG_SYSLOG, CONST_CS | CONST_PERSISTENT);
59 	REGISTER_LONG_CONSTANT("LOG_LPR", LOG_LPR, CONST_CS | CONST_PERSISTENT);
60 #ifdef LOG_NEWS
61 	/* No LOG_NEWS on HP-UX */
62 	REGISTER_LONG_CONSTANT("LOG_NEWS", LOG_NEWS, CONST_CS | CONST_PERSISTENT); /* usenet new */
63 #endif
64 #ifdef LOG_UUCP
65 	/* No LOG_UUCP on HP-UX */
66 	REGISTER_LONG_CONSTANT("LOG_UUCP", LOG_UUCP, CONST_CS | CONST_PERSISTENT);
67 #endif
68 #ifdef LOG_CRON
69 	/* apparently some systems don't have this one */
70 	REGISTER_LONG_CONSTANT("LOG_CRON", LOG_CRON, CONST_CS | CONST_PERSISTENT);
71 #endif
72 #ifdef LOG_AUTHPRIV
73 	/* AIX doesn't have LOG_AUTHPRIV */
74 	REGISTER_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
75 #endif
76 #ifndef PHP_WIN32
77 	REGISTER_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
78 	REGISTER_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
79 	REGISTER_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
80 	REGISTER_LONG_CONSTANT("LOG_LOCAL3", LOG_LOCAL3, CONST_CS | CONST_PERSISTENT);
81 	REGISTER_LONG_CONSTANT("LOG_LOCAL4", LOG_LOCAL4, CONST_CS | CONST_PERSISTENT);
82 	REGISTER_LONG_CONSTANT("LOG_LOCAL5", LOG_LOCAL5, CONST_CS | CONST_PERSISTENT);
83 	REGISTER_LONG_CONSTANT("LOG_LOCAL6", LOG_LOCAL6, CONST_CS | CONST_PERSISTENT);
84 	REGISTER_LONG_CONSTANT("LOG_LOCAL7", LOG_LOCAL7, CONST_CS | CONST_PERSISTENT);
85 #endif
86 	/* options */
87 	REGISTER_LONG_CONSTANT("LOG_PID", LOG_PID, CONST_CS | CONST_PERSISTENT);
88 	REGISTER_LONG_CONSTANT("LOG_CONS", LOG_CONS, CONST_CS | CONST_PERSISTENT);
89 	REGISTER_LONG_CONSTANT("LOG_ODELAY", LOG_ODELAY, CONST_CS | CONST_PERSISTENT);
90 	REGISTER_LONG_CONSTANT("LOG_NDELAY", LOG_NDELAY, CONST_CS | CONST_PERSISTENT);
91 #ifdef LOG_NOWAIT
92 	REGISTER_LONG_CONSTANT("LOG_NOWAIT", LOG_NOWAIT, CONST_CS | CONST_PERSISTENT);
93 #endif
94 #ifdef LOG_PERROR
95 	/* AIX doesn't have LOG_PERROR */
96 	REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
97 #endif
98 	BG(syslog_device)=NULL;
99 
100 	return SUCCESS;
101 }
102 /* }}} */
103 
PHP_RINIT_FUNCTION(syslog)104 PHP_RINIT_FUNCTION(syslog)
105 {
106 	BG(syslog_device) = NULL;
107 	return SUCCESS;
108 }
109 
110 
111 #ifdef PHP_WIN32
PHP_RSHUTDOWN_FUNCTION(syslog)112 PHP_RSHUTDOWN_FUNCTION(syslog)
113 {
114 	closelog();
115 	return SUCCESS;
116 }
117 #endif
118 
PHP_MSHUTDOWN_FUNCTION(syslog)119 PHP_MSHUTDOWN_FUNCTION(syslog)
120 {
121 	if (BG(syslog_device)) {
122 		free(BG(syslog_device));
123 		BG(syslog_device) = NULL;
124 	}
125 	return SUCCESS;
126 }
127 
128 /* {{{ proto bool openlog(string ident, int option, int facility)
129    Open connection to system logger */
130 /*
131    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
132    ** Syslog($LOG_EMERG, "help me!")
133    ** CloseLog();
134  */
PHP_FUNCTION(openlog)135 PHP_FUNCTION(openlog)
136 {
137 	char *ident;
138 	zend_long option, facility;
139 	size_t ident_len;
140 
141 	ZEND_PARSE_PARAMETERS_START(3, 3)
142 		Z_PARAM_STRING(ident, ident_len)
143 		Z_PARAM_LONG(option)
144 		Z_PARAM_LONG(facility)
145 	ZEND_PARSE_PARAMETERS_END();
146 
147 	if (BG(syslog_device)) {
148 		free(BG(syslog_device));
149 	}
150 	BG(syslog_device) = zend_strndup(ident, ident_len);
151 	if(BG(syslog_device) == NULL) {
152 		RETURN_FALSE;
153 	}
154 	openlog(BG(syslog_device), option, facility);
155 	RETURN_TRUE;
156 }
157 /* }}} */
158 
159 /* {{{ proto bool closelog(void)
160    Close connection to system logger */
PHP_FUNCTION(closelog)161 PHP_FUNCTION(closelog)
162 {
163 	if (zend_parse_parameters_none() == FAILURE) {
164 		return;
165 	}
166 
167 	closelog();
168 	if (BG(syslog_device)) {
169 		free(BG(syslog_device));
170 		BG(syslog_device)=NULL;
171 	}
172 	RETURN_TRUE;
173 }
174 /* }}} */
175 
176 /* {{{ proto bool syslog(int priority, string message)
177    Generate a system log message */
PHP_FUNCTION(syslog)178 PHP_FUNCTION(syslog)
179 {
180 	zend_long priority;
181 	char *message;
182 	size_t message_len;
183 
184 	ZEND_PARSE_PARAMETERS_START(2, 2)
185 		Z_PARAM_LONG(priority)
186 		Z_PARAM_STRING(message, message_len)
187 	ZEND_PARSE_PARAMETERS_END();
188 
189 	php_syslog(priority, "%s", message);
190 	RETURN_TRUE;
191 }
192 /* }}} */
193 
194 #endif
195 
196 /*
197  * Local variables:
198  * tab-width: 4
199  * c-basic-offset: 4
200  * End:
201  * vim600: sw=4 ts=4 fdm=marker
202  * vim<600: sw=4 ts=4
203  */
204