xref: /php-src/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 #ifdef 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 	return SUCCESS;
39 }
40 /* }}} */
41 
PHP_RSHUTDOWN_FUNCTION(syslog)42 PHP_RSHUTDOWN_FUNCTION(syslog)
43 {
44 	php_closelog();
45 	if (BG(syslog_device)) {
46 		free(BG(syslog_device));
47 		BG(syslog_device) = NULL;
48 	}
49 	return SUCCESS;
50 }
51 
52 
53 /* {{{ Open connection to system logger */
54 /*
55    ** OpenLog("nettopp", $LOG_PID, $LOG_LOCAL1);
56    ** Syslog($LOG_EMERG, "help me!")
57    ** CloseLog();
58  */
PHP_FUNCTION(openlog)59 PHP_FUNCTION(openlog)
60 {
61 	char *ident;
62 	zend_long option, facility;
63 	size_t ident_len;
64 
65 	ZEND_PARSE_PARAMETERS_START(3, 3)
66 		Z_PARAM_STRING(ident, ident_len)
67 		Z_PARAM_LONG(option)
68 		Z_PARAM_LONG(facility)
69 	ZEND_PARSE_PARAMETERS_END();
70 
71 	if (BG(syslog_device)) {
72 		free(BG(syslog_device));
73 	}
74 	BG(syslog_device) = zend_strndup(ident, ident_len);
75 	php_openlog(BG(syslog_device), option, facility);
76 	RETURN_TRUE;
77 }
78 /* }}} */
79 
80 /* {{{ Close connection to system logger */
PHP_FUNCTION(closelog)81 PHP_FUNCTION(closelog)
82 {
83 	ZEND_PARSE_PARAMETERS_NONE();
84 
85 	php_closelog();
86 	if (BG(syslog_device)) {
87 		free(BG(syslog_device));
88 		BG(syslog_device)=NULL;
89 	}
90 	RETURN_TRUE;
91 }
92 /* }}} */
93 
94 /* {{{ Generate a system log message */
PHP_FUNCTION(syslog)95 PHP_FUNCTION(syslog)
96 {
97 	zend_long priority;
98 	zend_string *message;
99 
100 	ZEND_PARSE_PARAMETERS_START(2, 2)
101 		Z_PARAM_LONG(priority)
102 		Z_PARAM_STR(message)
103 	ZEND_PARSE_PARAMETERS_END();
104 
105 	php_syslog_str(priority, message);
106 	RETURN_TRUE;
107 }
108 /* }}} */
109 
110 #endif
111