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