xref: /PHP-7.4/ext/pdo_oci/pdo_oci.c (revision 38363f48)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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: Wez Furlong <wez@php.net>                                    |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "pdo/php_pdo.h"
27 #include "pdo/php_pdo_driver.h"
28 #include "php_pdo_oci.h"
29 #include "php_pdo_oci_int.h"
30 #ifdef ZTS
31 #include <TSRM/TSRM.h>
32 #endif
33 
34 /* {{{ pdo_oci_functions[] */
35 static const zend_function_entry pdo_oci_functions[] = {
36 	PHP_FE_END
37 };
38 /* }}} */
39 
40 /* {{{ pdo_oci_module_entry */
41 
42 static const zend_module_dep pdo_oci_deps[] = {
43 	ZEND_MOD_REQUIRED("pdo")
44 	ZEND_MOD_END
45 };
46 
47 zend_module_entry pdo_oci_module_entry = {
48 	STANDARD_MODULE_HEADER_EX, NULL,
49 	pdo_oci_deps,
50 	"PDO_OCI",
51 	pdo_oci_functions,
52 	PHP_MINIT(pdo_oci),
53 	PHP_MSHUTDOWN(pdo_oci),
54 	PHP_RINIT(pdo_oci),
55 	NULL,
56 	PHP_MINFO(pdo_oci),
57 	PHP_PDO_OCI_VERSION,
58 	STANDARD_MODULE_PROPERTIES
59 };
60 /* }}} */
61 
62 #ifdef COMPILE_DL_PDO_OCI
63 ZEND_GET_MODULE(pdo_oci)
64 #endif
65 
66 const ub4 PDO_OCI_INIT_MODE =
67 #if 0 && defined(OCI_SHARED)
68 			/* shared mode is known to be bad for PHP */
69 			OCI_SHARED
70 #else
71 			OCI_DEFAULT
72 #endif
73 #ifdef OCI_OBJECT
74 			|OCI_OBJECT
75 #endif
76 #ifdef ZTS
77 			|OCI_THREADED
78 #endif
79 			;
80 
81 /* true global environment */
82 OCIEnv *pdo_oci_Env = NULL;
83 
84 #ifdef ZTS
85 /* lock for pdo_oci_Env initialization */
86 static MUTEX_T pdo_oci_env_mutex;
87 #endif
88 
89 /* {{{ PHP_MINIT_FUNCTION
90  */
PHP_MINIT_FUNCTION(pdo_oci)91 PHP_MINIT_FUNCTION(pdo_oci)
92 {
93 	REGISTER_PDO_CLASS_CONST_LONG("OCI_ATTR_ACTION", (zend_long)PDO_OCI_ATTR_ACTION);
94 	REGISTER_PDO_CLASS_CONST_LONG("OCI_ATTR_CLIENT_INFO", (zend_long)PDO_OCI_ATTR_CLIENT_INFO);
95 	REGISTER_PDO_CLASS_CONST_LONG("OCI_ATTR_CLIENT_IDENTIFIER", (zend_long)PDO_OCI_ATTR_CLIENT_IDENTIFIER);
96 	REGISTER_PDO_CLASS_CONST_LONG("OCI_ATTR_MODULE", (zend_long)PDO_OCI_ATTR_MODULE);
97 
98 	php_pdo_register_driver(&pdo_oci_driver);
99 
100 	// Defer OCI init to PHP_RINIT_FUNCTION because with php-fpm,
101 	// NLS_LANG is not yet available here.
102 
103 #ifdef ZTS
104 	pdo_oci_env_mutex = tsrm_mutex_alloc();
105 #endif
106 
107 	return SUCCESS;
108 }
109 /* }}} */
110 
111 /* {{{ PHP_RINIT_FUNCTION
112  */
PHP_RINIT_FUNCTION(pdo_oci)113 PHP_RINIT_FUNCTION(pdo_oci)
114 {
115 	if (!pdo_oci_Env) {
116 #ifdef ZTS
117 		tsrm_mutex_lock(pdo_oci_env_mutex);
118 		if (!pdo_oci_Env) { // double-checked locking idiom
119 #endif
120 #if HAVE_OCIENVCREATE
121 		OCIEnvCreate(&pdo_oci_Env, PDO_OCI_INIT_MODE, NULL, NULL, NULL, NULL, 0, NULL);
122 #else
123 		OCIInitialize(PDO_OCI_INIT_MODE, NULL, NULL, NULL, NULL);
124 		OCIEnvInit(&pdo_oci_Env, OCI_DEFAULT, 0, NULL);
125 #endif
126 #ifdef ZTS
127 		}
128 		tsrm_mutex_unlock(pdo_oci_env_mutex);
129 #endif
130 	}
131 
132 	return SUCCESS;
133 }
134 /* }}} */
135 
136 /* {{{ PHP_MSHUTDOWN_FUNCTION
137  */
PHP_MSHUTDOWN_FUNCTION(pdo_oci)138 PHP_MSHUTDOWN_FUNCTION(pdo_oci)
139 {
140 	php_pdo_unregister_driver(&pdo_oci_driver);
141 
142 	if (pdo_oci_Env) {
143 		OCIHandleFree((dvoid*)pdo_oci_Env, OCI_HTYPE_ENV);
144 	}
145 
146 #ifdef ZTS
147 	tsrm_mutex_free(pdo_oci_env_mutex);
148 #endif
149 
150 	return SUCCESS;
151 }
152 /* }}} */
153 
154 /* {{{ PHP_MINFO_FUNCTION
155  */
PHP_MINFO_FUNCTION(pdo_oci)156 PHP_MINFO_FUNCTION(pdo_oci)
157 {
158 	php_info_print_table_start();
159 	php_info_print_table_header(2, "PDO Driver for OCI 8 and later", "enabled");
160 	php_info_print_table_end();
161 }
162 /* }}} */
163