xref: /PHP-7.0/ext/standard/syslog.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll", &ident,
142 							  &ident_len, &option, &facility) == FAILURE) {
143 		return;
144 	}
145 	if (BG(syslog_device)) {
146 		free(BG(syslog_device));
147 	}
148 	BG(syslog_device) = zend_strndup(ident, ident_len);
149 	if(BG(syslog_device) == NULL) {
150 		RETURN_FALSE;
151 	}
152 	openlog(BG(syslog_device), option, facility);
153 	RETURN_TRUE;
154 }
155 /* }}} */
156 
157 /* {{{ proto bool closelog(void)
158    Close connection to system logger */
PHP_FUNCTION(closelog)159 PHP_FUNCTION(closelog)
160 {
161 	if (zend_parse_parameters_none() == FAILURE) {
162 		return;
163 	}
164 
165 	closelog();
166 	if (BG(syslog_device)) {
167 		free(BG(syslog_device));
168 		BG(syslog_device)=NULL;
169 	}
170 	RETURN_TRUE;
171 }
172 /* }}} */
173 
174 /* {{{ proto bool syslog(int priority, string message)
175    Generate a system log message */
PHP_FUNCTION(syslog)176 PHP_FUNCTION(syslog)
177 {
178 	zend_long priority;
179 	char *message;
180 	size_t message_len;
181 
182 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls", &priority,
183 							  &message, &message_len) == FAILURE) {
184 		return;
185 	}
186 
187 	php_syslog(priority, "%s", message);
188 	RETURN_TRUE;
189 }
190 /* }}} */
191 
192 #endif
193 
194 /*
195  * Local variables:
196  * tab-width: 4
197  * c-basic-offset: 4
198  * End:
199  * vim600: sw=4 ts=4 fdm=marker
200  * vim<600: sw=4 ts=4
201  */
202