Back to index

php5  5.3.10
syslog.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Stig Sæther Bakken <ssb@php.net>                             |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: syslog.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 
00023 #ifdef HAVE_SYSLOG_H
00024 #include "php_ini.h"
00025 #include "zend_globals.h"
00026 
00027 #include <stdlib.h>
00028 #if HAVE_UNISTD_H
00029 #include <unistd.h>
00030 #endif
00031 
00032 #include <string.h>
00033 #include <errno.h>
00034 
00035 #include <stdio.h>
00036 #include "basic_functions.h"
00037 #include "php_ext_syslog.h"
00038 
00039 static void start_syslog(TSRMLS_D);
00040 
00041 /* {{{ PHP_MINIT_FUNCTION
00042  */
00043 PHP_MINIT_FUNCTION(syslog)
00044 {
00045        /* error levels */
00046        REGISTER_LONG_CONSTANT("LOG_EMERG", LOG_EMERG, CONST_CS | CONST_PERSISTENT); /* system unusable */
00047        REGISTER_LONG_CONSTANT("LOG_ALERT", LOG_ALERT, CONST_CS | CONST_PERSISTENT); /* immediate action required */
00048        REGISTER_LONG_CONSTANT("LOG_CRIT", LOG_CRIT, CONST_CS | CONST_PERSISTENT); /* critical conditions */
00049        REGISTER_LONG_CONSTANT("LOG_ERR", LOG_ERR, CONST_CS | CONST_PERSISTENT); 
00050        REGISTER_LONG_CONSTANT("LOG_WARNING", LOG_WARNING, CONST_CS | CONST_PERSISTENT);
00051        REGISTER_LONG_CONSTANT("LOG_NOTICE", LOG_NOTICE, CONST_CS | CONST_PERSISTENT);
00052        REGISTER_LONG_CONSTANT("LOG_INFO", LOG_INFO, CONST_CS | CONST_PERSISTENT);
00053        REGISTER_LONG_CONSTANT("LOG_DEBUG", LOG_DEBUG, CONST_CS | CONST_PERSISTENT);
00054        /* facility: type of program logging the message */
00055        REGISTER_LONG_CONSTANT("LOG_KERN", LOG_KERN, CONST_CS | CONST_PERSISTENT);
00056        REGISTER_LONG_CONSTANT("LOG_USER", LOG_USER, CONST_CS | CONST_PERSISTENT); /* generic user level */
00057        REGISTER_LONG_CONSTANT("LOG_MAIL", LOG_MAIL, CONST_CS | CONST_PERSISTENT); /* log to email */
00058        REGISTER_LONG_CONSTANT("LOG_DAEMON", LOG_DAEMON, CONST_CS | CONST_PERSISTENT); /* other system daemons */
00059        REGISTER_LONG_CONSTANT("LOG_AUTH", LOG_AUTH, CONST_CS | CONST_PERSISTENT);
00060        REGISTER_LONG_CONSTANT("LOG_SYSLOG", LOG_SYSLOG, CONST_CS | CONST_PERSISTENT);
00061        REGISTER_LONG_CONSTANT("LOG_LPR", LOG_LPR, CONST_CS | CONST_PERSISTENT);
00062 #ifdef LOG_NEWS
00063        /* No LOG_NEWS on HP-UX */
00064        REGISTER_LONG_CONSTANT("LOG_NEWS", LOG_NEWS, CONST_CS | CONST_PERSISTENT); /* usenet new */
00065 #endif
00066 #ifdef LOG_UUCP
00067        /* No LOG_UUCP on HP-UX */
00068        REGISTER_LONG_CONSTANT("LOG_UUCP", LOG_UUCP, CONST_CS | CONST_PERSISTENT);
00069 #endif
00070 #ifdef LOG_CRON
00071        /* apparently some systems don't have this one */
00072        REGISTER_LONG_CONSTANT("LOG_CRON", LOG_CRON, CONST_CS | CONST_PERSISTENT);
00073 #endif
00074 #ifdef LOG_AUTHPRIV
00075        /* AIX doesn't have LOG_AUTHPRIV */
00076        REGISTER_LONG_CONSTANT("LOG_AUTHPRIV", LOG_AUTHPRIV, CONST_CS | CONST_PERSISTENT);
00077 #endif
00078 #ifndef PHP_WIN32
00079        REGISTER_LONG_CONSTANT("LOG_LOCAL0", LOG_LOCAL0, CONST_CS | CONST_PERSISTENT);
00080        REGISTER_LONG_CONSTANT("LOG_LOCAL1", LOG_LOCAL1, CONST_CS | CONST_PERSISTENT);
00081        REGISTER_LONG_CONSTANT("LOG_LOCAL2", LOG_LOCAL2, CONST_CS | CONST_PERSISTENT);
00082        REGISTER_LONG_CONSTANT("LOG_LOCAL3", LOG_LOCAL3, CONST_CS | CONST_PERSISTENT);
00083        REGISTER_LONG_CONSTANT("LOG_LOCAL4", LOG_LOCAL4, CONST_CS | CONST_PERSISTENT);
00084        REGISTER_LONG_CONSTANT("LOG_LOCAL5", LOG_LOCAL5, CONST_CS | CONST_PERSISTENT);
00085        REGISTER_LONG_CONSTANT("LOG_LOCAL6", LOG_LOCAL6, CONST_CS | CONST_PERSISTENT);
00086        REGISTER_LONG_CONSTANT("LOG_LOCAL7", LOG_LOCAL7, CONST_CS | CONST_PERSISTENT);
00087 #endif
00088        /* options */
00089        REGISTER_LONG_CONSTANT("LOG_PID", LOG_PID, CONST_CS | CONST_PERSISTENT);
00090        REGISTER_LONG_CONSTANT("LOG_CONS", LOG_CONS, CONST_CS | CONST_PERSISTENT);
00091        REGISTER_LONG_CONSTANT("LOG_ODELAY", LOG_ODELAY, CONST_CS | CONST_PERSISTENT);
00092        REGISTER_LONG_CONSTANT("LOG_NDELAY", LOG_NDELAY, CONST_CS | CONST_PERSISTENT);
00093 #ifdef LOG_NOWAIT
00094        REGISTER_LONG_CONSTANT("LOG_NOWAIT", LOG_NOWAIT, CONST_CS | CONST_PERSISTENT);
00095 #endif
00096 #ifdef LOG_PERROR
00097        /* AIX doesn't have LOG_PERROR */
00098        REGISTER_LONG_CONSTANT("LOG_PERROR", LOG_PERROR, CONST_CS | CONST_PERSISTENT); /*log to stderr*/
00099 #endif
00100        BG(syslog_device)=NULL;
00101 
00102        return SUCCESS;
00103 }
00104 /* }}} */
00105 
00106 PHP_RINIT_FUNCTION(syslog)
00107 {
00108        if (INI_INT("define_syslog_variables")) {
00109               start_syslog(TSRMLS_C);
00110        } else {
00111               BG(syslog_started)=0;
00112        }
00113        BG(syslog_device) = NULL;
00114        return SUCCESS;
00115 }
00116 
00117 
00118 #ifdef PHP_WIN32
00119 PHP_RSHUTDOWN_FUNCTION(syslog)
00120 {
00121        closelog();
00122        return SUCCESS;
00123 }
00124 #endif
00125 
00126 PHP_MSHUTDOWN_FUNCTION(syslog)
00127 {
00128        if (BG(syslog_device)) {
00129               free(BG(syslog_device));
00130               BG(syslog_device) = NULL;
00131        }
00132        return SUCCESS;
00133 }
00134 
00135 /* {{{ start_syslog
00136  */
00137 static void start_syslog(TSRMLS_D)
00138 {
00139        /* error levels */
00140        SET_VAR_LONG("LOG_EMERG", LOG_EMERG); /* system unusable */
00141        SET_VAR_LONG("LOG_ALERT", LOG_ALERT); /* immediate action required */
00142        SET_VAR_LONG("LOG_CRIT", LOG_CRIT); /* critical conditions */
00143        SET_VAR_LONG("LOG_ERR", LOG_ERR); 
00144        SET_VAR_LONG("LOG_WARNING", LOG_WARNING);
00145        SET_VAR_LONG("LOG_NOTICE", LOG_NOTICE);
00146        SET_VAR_LONG("LOG_INFO", LOG_INFO);
00147        SET_VAR_LONG("LOG_DEBUG", LOG_DEBUG);
00148        /* facility: type of program logging the message */
00149        SET_VAR_LONG("LOG_KERN", LOG_KERN);
00150        SET_VAR_LONG("LOG_USER", LOG_USER); /* generic user level */
00151        SET_VAR_LONG("LOG_MAIL", LOG_MAIL); /* log to email */
00152        SET_VAR_LONG("LOG_DAEMON", LOG_DAEMON); /* other system daemons */
00153        SET_VAR_LONG("LOG_AUTH", LOG_AUTH);
00154 #ifndef NETWARE
00155        SET_VAR_LONG("LOG_SYSLOG", LOG_SYSLOG);
00156 #endif
00157        SET_VAR_LONG("LOG_LPR", LOG_LPR);
00158 #ifdef LOG_NEWS
00159        /* No LOG_NEWS on HP-UX */
00160        SET_VAR_LONG("LOG_NEWS", LOG_NEWS); /* usenet new */
00161 #endif
00162 #ifdef LOG_UUCP
00163        /* No LOG_UUCP on HP-UX */
00164        SET_VAR_LONG("LOG_UUCP", LOG_UUCP);
00165 #endif
00166 #ifdef LOG_CRON
00167        /* apparently some systems don't have this one */
00168        SET_VAR_LONG("LOG_CRON", LOG_CRON);
00169 #endif
00170 #ifdef LOG_AUTHPRIV
00171        /* AIX doesn't have LOG_AUTHPRIV */
00172        SET_VAR_LONG("LOG_AUTHPRIV", LOG_AUTHPRIV);
00173 #endif
00174 #if !defined(PHP_WIN32) && !defined(NETWARE)
00175        SET_VAR_LONG("LOG_LOCAL0", LOG_LOCAL0);
00176        SET_VAR_LONG("LOG_LOCAL1", LOG_LOCAL1);
00177        SET_VAR_LONG("LOG_LOCAL2", LOG_LOCAL2);
00178        SET_VAR_LONG("LOG_LOCAL3", LOG_LOCAL3);
00179        SET_VAR_LONG("LOG_LOCAL4", LOG_LOCAL4);
00180        SET_VAR_LONG("LOG_LOCAL5", LOG_LOCAL5);
00181        SET_VAR_LONG("LOG_LOCAL6", LOG_LOCAL6);
00182        SET_VAR_LONG("LOG_LOCAL7", LOG_LOCAL7);
00183 #endif
00184        /* options */
00185        SET_VAR_LONG("LOG_PID", LOG_PID);
00186        SET_VAR_LONG("LOG_CONS", LOG_CONS);
00187        SET_VAR_LONG("LOG_ODELAY", LOG_ODELAY);
00188        SET_VAR_LONG("LOG_NDELAY", LOG_NDELAY);
00189 #ifdef LOG_NOWAIT
00190        /* BeOS doesn't have LOG_NOWAIT */
00191        SET_VAR_LONG("LOG_NOWAIT", LOG_NOWAIT);
00192 #endif
00193 #ifdef LOG_PERROR
00194        /* AIX doesn't have LOG_PERROR */
00195        SET_VAR_LONG("LOG_PERROR", LOG_PERROR); /*log to stderr*/
00196 #endif
00197 
00198        BG(syslog_started)=1;
00199 }
00200 /* }}} */
00201 
00202 /* {{{ proto void define_syslog_variables(void)
00203    Initializes all syslog-related variables */
00204 PHP_FUNCTION(define_syslog_variables)
00205 {
00206        if (zend_parse_parameters_none() == FAILURE) {
00207               return;
00208        }
00209 
00210        if (!BG(syslog_started)) {
00211               start_syslog(TSRMLS_C);
00212        }
00213 }
00214 /* }}} */
00215 
00216 /* {{{ proto bool openlog(string ident, int option, int facility)
00217    Open connection to system logger */
00218 /*
00219    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
00220    ** Syslog($LOG_EMERG, "help me!")
00221    ** CloseLog();
00222  */
00223 PHP_FUNCTION(openlog)
00224 {
00225        char *ident;
00226        long option, facility;
00227        int ident_len;
00228 
00229        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &ident,
00230                                                    &ident_len, &option, &facility) == FAILURE) {
00231               return;
00232        }
00233        if (BG(syslog_device)) {
00234               free(BG(syslog_device));
00235        }
00236        BG(syslog_device) = zend_strndup(ident, ident_len);
00237        openlog(BG(syslog_device), option, facility);
00238        RETURN_TRUE;
00239 }
00240 /* }}} */
00241 
00242 /* {{{ proto bool closelog(void)
00243    Close connection to system logger */
00244 PHP_FUNCTION(closelog)
00245 {
00246        if (zend_parse_parameters_none() == FAILURE) {
00247               return;
00248        }
00249 
00250        closelog();
00251        if (BG(syslog_device)) {
00252               free(BG(syslog_device));
00253               BG(syslog_device)=NULL;
00254        }
00255        RETURN_TRUE;
00256 }
00257 /* }}} */
00258 
00259 /* {{{ proto bool syslog(int priority, string message)
00260    Generate a system log message */
00261 PHP_FUNCTION(syslog)
00262 {
00263        long priority;
00264        char *message;
00265        int message_len;
00266 
00267        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &priority,
00268                                                    &message, &message_len) == FAILURE) {
00269               return;
00270        }
00271 
00272        php_syslog(priority, "%s", message);
00273        RETURN_TRUE;
00274 }
00275 /* }}} */
00276 
00277 #endif
00278 
00279 /*
00280  * Local variables:
00281  * tab-width: 4
00282  * c-basic-offset: 4
00283  * End:
00284  * vim600: sw=4 ts=4 fdm=marker
00285  * vim<600: sw=4 ts=4
00286  */