xref: /PHP-8.1/ext/standard/syslog.c (revision b4208c8f)
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    | https://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 
94 	return SUCCESS;
95 }
96 /* }}} */
97 
PHP_RSHUTDOWN_FUNCTION(syslog)98 PHP_RSHUTDOWN_FUNCTION(syslog)
99 {
100 	php_closelog();
101 	if (BG(syslog_device)) {
102 		free(BG(syslog_device));
103 		BG(syslog_device) = NULL;
104 	}
105 	return SUCCESS;
106 }
107 
108 
109 /* {{{ Open connection to system logger */
110 /*
111    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
112    ** Syslog($LOG_EMERG, "help me!")
113    ** CloseLog();
114  */
PHP_FUNCTION(openlog)115 PHP_FUNCTION(openlog)
116 {
117 	char *ident;
118 	zend_long option, facility;
119 	size_t ident_len;
120 
121 	ZEND_PARSE_PARAMETERS_START(3, 3)
122 		Z_PARAM_STRING(ident, ident_len)
123 		Z_PARAM_LONG(option)
124 		Z_PARAM_LONG(facility)
125 	ZEND_PARSE_PARAMETERS_END();
126 
127 	if (BG(syslog_device)) {
128 		free(BG(syslog_device));
129 	}
130 	BG(syslog_device) = zend_strndup(ident, ident_len);
131 	if(BG(syslog_device) == NULL) {
132 		RETURN_FALSE;
133 	}
134 	php_openlog(BG(syslog_device), option, facility);
135 	RETURN_TRUE;
136 }
137 /* }}} */
138 
139 /* {{{ Close connection to system logger */
PHP_FUNCTION(closelog)140 PHP_FUNCTION(closelog)
141 {
142 	ZEND_PARSE_PARAMETERS_NONE();
143 
144 	php_closelog();
145 	if (BG(syslog_device)) {
146 		free(BG(syslog_device));
147 		BG(syslog_device)=NULL;
148 	}
149 	RETURN_TRUE;
150 }
151 /* }}} */
152 
153 /* {{{ Generate a system log message */
PHP_FUNCTION(syslog)154 PHP_FUNCTION(syslog)
155 {
156 	zend_long priority;
157 	zend_string *message;
158 
159 	ZEND_PARSE_PARAMETERS_START(2, 2)
160 		Z_PARAM_LONG(priority)
161 		Z_PARAM_STR(message)
162 	ZEND_PARSE_PARAMETERS_END();
163 
164 	php_syslog_str(priority, message);
165 	RETURN_TRUE;
166 }
167 /* }}} */
168 
169 #endif
170