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