1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 /**
29   @file mysys/my_static.cc
30   Static variables for mysys library. All defined here for easy making of
31   a shared library.
32 */
33 
34 #include "mysys/my_static.h"
35 
36 #include "my_config.h"
37 
38 #include <stdarg.h>
39 #include <stddef.h>
40 
41 #include "my_compiler.h"
42 #include "my_loglevel.h"
43 #include "mysql/psi/mysql_cond.h"
44 #include "mysql/psi/mysql_mutex.h"
45 #include "mysql/psi/psi_base.h"
46 #include "mysql/psi/psi_memory.h"
47 #include "mysql/psi/psi_stage.h"
48 #include "mysys/mysys_priv.h"  // IWYU pragma: keep
49 
50 /* get memory in hunks */
51 constexpr uint ONCE_ALLOC_INIT = 4096 - MALLOC_OVERHEAD;
52 
53 PSI_memory_key key_memory_charset_file;
54 PSI_memory_key key_memory_charset_loader;
55 PSI_memory_key key_memory_lf_node;
56 PSI_memory_key key_memory_lf_dynarray;
57 PSI_memory_key key_memory_lf_slist;
58 PSI_memory_key key_memory_LIST;
59 PSI_memory_key key_memory_IO_CACHE;
60 PSI_memory_key key_memory_KEY_CACHE;
61 PSI_memory_key key_memory_SAFE_HASH_ENTRY;
62 PSI_memory_key key_memory_MY_BITMAP_bitmap;
63 PSI_memory_key key_memory_my_compress_alloc;
64 PSI_memory_key key_memory_my_err_head;
65 PSI_memory_key key_memory_my_file_info;
66 PSI_memory_key key_memory_max_alloca;
67 PSI_memory_key key_memory_MY_DIR;
68 PSI_memory_key key_memory_MY_TMPDIR_full_list;
69 PSI_memory_key key_memory_DYNAMIC_STRING;
70 PSI_memory_key key_memory_TREE;
71 
72 PSI_thread_key key_thread_timer_notifier;
73 
74 #ifdef _WIN32
75 PSI_memory_key key_memory_win_SECURITY_ATTRIBUTES;
76 PSI_memory_key key_memory_win_PACL;
77 PSI_memory_key key_memory_win_IP_ADAPTER_ADDRESSES;
78 PSI_memory_key key_memory_win_handle_info;
79 #endif /* _WIN32 */
80 
81 /* from my_init */
82 char *home_dir = nullptr;
83 const char *my_progname = nullptr;
84 char curr_dir[FN_REFLEN] = {0}, home_dir_buff[FN_REFLEN] = {0};
85 
86 ulong my_tmp_file_created = 0;
87 
88 ulong my_stream_opened = 0;
89 ulong my_file_opened = 0;
90 ulong my_file_total_opened = 0;
91 
92 namespace file_info {
93 /**
94    Increment status variables.
95    @relates file_info::CountFileOpen
96 
97    @param pt previous file_type (only relevant when assigning an fd to a stream
98    in my_fdopen):
99    @param ct current file type (to differentiate betweeen streams and files).
100  */
CountFileOpen(OpenType pt,OpenType ct)101 void CountFileOpen(OpenType pt, OpenType ct) {
102   mysql_mutex_assert_owner(&THR_LOCK_open);
103   DBUG_ASSERT(my_file_opened + my_stream_opened == my_file_total_opened);
104   DBUG_ASSERT(pt == OpenType::UNOPEN || ct == OpenType::STREAM_BY_FDOPEN);
105   switch (ct) {
106     case OpenType::UNOPEN:
107       DBUG_ASSERT(false);
108       return;
109 
110     case OpenType::STREAM_BY_FDOPEN:
111       if (pt != OpenType::UNOPEN) {
112         // If fd was opened through mysys, we have already counted
113         // it in my_file_opened_. Since we will now increment
114         // my_file_stream_opened_ for it, we decrement my_file_opened_
115         // so that it is not counted twice.
116         DBUG_ASSERT(pt != OpenType::STREAM_BY_FOPEN &&
117                     pt != OpenType::STREAM_BY_FDOPEN);
118         --my_file_opened;
119         ++my_stream_opened;
120         DBUG_ASSERT(my_file_opened + my_stream_opened == my_file_total_opened);
121         return;
122       }
123       // Fallthrough
124     case OpenType::STREAM_BY_FOPEN:
125       ++my_stream_opened;
126       break;
127 
128     default:
129       ++my_file_opened;
130   }
131   ++my_file_total_opened;
132   DBUG_ASSERT(my_file_opened + my_stream_opened == my_file_total_opened);
133 }
134 
135 /**
136    Decrement status variables.
137    @relates file_info::CountFileClose
138 
139    @param ft file type (to differentiate betweeen streams and files).
140  */
CountFileClose(OpenType ft)141 void CountFileClose(OpenType ft) {
142   mysql_mutex_assert_owner(&THR_LOCK_open);
143   DBUG_ASSERT(my_file_opened + my_stream_opened == my_file_total_opened);
144   switch (ft) {
145     case OpenType::UNOPEN:
146       return;
147     case OpenType::STREAM_BY_FOPEN:
148     case OpenType::STREAM_BY_FDOPEN:
149       --my_stream_opened;
150       break;
151     default:
152       --my_file_opened;
153   };
154   --my_file_total_opened;
155   DBUG_ASSERT(my_file_opened + my_stream_opened == my_file_total_opened);
156 }
157 }  // namespace file_info
158 
159 int my_umask = 0664, my_umask_dir = 0777;
160 
161 /* from mf_reccache.c */
162 ulong my_default_record_cache_size = RECORD_CACHE_SIZE;
163 
164 /* from my_malloc */
165 USED_MEM *my_once_root_block = nullptr; /* pointer to first block */
166 uint my_once_extra = ONCE_ALLOC_INIT;   /* Memory to alloc / block */
167 
168 /* from errors.c */
169 void (*error_handler_hook)(uint error, const char *str,
170                            myf MyFlags) = my_message_stderr;
171 void (*fatal_error_handler_hook)(uint error, const char *str,
172                                  myf MyFlags) = my_message_stderr;
173 void (*local_message_hook)(enum loglevel ll, uint ecode,
174                            va_list args) = my_message_local_stderr;
175 
enter_cond_dummy(void * a MY_ATTRIBUTE ((unused)),mysql_cond_t * b MY_ATTRIBUTE ((unused)),mysql_mutex_t * c MY_ATTRIBUTE ((unused)),const PSI_stage_info * d MY_ATTRIBUTE ((unused)),PSI_stage_info * e MY_ATTRIBUTE ((unused)),const char * f MY_ATTRIBUTE ((unused)),const char * g MY_ATTRIBUTE ((unused)),int h MY_ATTRIBUTE ((unused)))176 static void enter_cond_dummy(void *a MY_ATTRIBUTE((unused)),
177                              mysql_cond_t *b MY_ATTRIBUTE((unused)),
178                              mysql_mutex_t *c MY_ATTRIBUTE((unused)),
179                              const PSI_stage_info *d MY_ATTRIBUTE((unused)),
180                              PSI_stage_info *e MY_ATTRIBUTE((unused)),
181                              const char *f MY_ATTRIBUTE((unused)),
182                              const char *g MY_ATTRIBUTE((unused)),
183                              int h MY_ATTRIBUTE((unused))) {}
184 
exit_cond_dummy(void * a MY_ATTRIBUTE ((unused)),const PSI_stage_info * b MY_ATTRIBUTE ((unused)),const char * c MY_ATTRIBUTE ((unused)),const char * d MY_ATTRIBUTE ((unused)),int e MY_ATTRIBUTE ((unused)))185 static void exit_cond_dummy(void *a MY_ATTRIBUTE((unused)),
186                             const PSI_stage_info *b MY_ATTRIBUTE((unused)),
187                             const char *c MY_ATTRIBUTE((unused)),
188                             const char *d MY_ATTRIBUTE((unused)),
189                             int e MY_ATTRIBUTE((unused))) {}
190 
enter_stage_dummy(void * a MY_ATTRIBUTE ((unused)),const PSI_stage_info * b MY_ATTRIBUTE ((unused)),PSI_stage_info * c MY_ATTRIBUTE ((unused)),const char * d MY_ATTRIBUTE ((unused)),const char * e MY_ATTRIBUTE ((unused)),int f MY_ATTRIBUTE ((unused)))191 static void enter_stage_dummy(void *a MY_ATTRIBUTE((unused)),
192                               const PSI_stage_info *b MY_ATTRIBUTE((unused)),
193                               PSI_stage_info *c MY_ATTRIBUTE((unused)),
194                               const char *d MY_ATTRIBUTE((unused)),
195                               const char *e MY_ATTRIBUTE((unused)),
196                               int f MY_ATTRIBUTE((unused))) {}
197 
set_waiting_for_disk_space_dummy(void * a MY_ATTRIBUTE ((unused)),bool b MY_ATTRIBUTE ((unused)))198 static void set_waiting_for_disk_space_dummy(void *a MY_ATTRIBUTE((unused)),
199                                              bool b MY_ATTRIBUTE((unused))) {}
200 
is_killed_dummy(const void * a MY_ATTRIBUTE ((unused)))201 static int is_killed_dummy(const void *a MY_ATTRIBUTE((unused))) { return 0; }
202 
203 /*
204   Initialize these hooks to dummy implementations. The real server
205   implementations will be set during server startup by
206   init_server_components().
207 */
208 void (*enter_cond_hook)(void *, mysql_cond_t *, mysql_mutex_t *,
209                         const PSI_stage_info *, PSI_stage_info *, const char *,
210                         const char *, int) = enter_cond_dummy;
211 
212 void (*exit_cond_hook)(void *, const PSI_stage_info *, const char *,
213                        const char *, int) = exit_cond_dummy;
214 
215 void (*enter_stage_hook)(void *, const PSI_stage_info *, PSI_stage_info *,
216                          const char *, const char *, int) = enter_stage_dummy;
217 
218 void (*set_waiting_for_disk_space_hook)(void *, bool) =
219     set_waiting_for_disk_space_dummy;
220 
221 int (*is_killed_hook)(const void *) = is_killed_dummy;
222 
223 #if defined(ENABLED_DEBUG_SYNC)
224 /**
225    Global pointer to be set if callback function is defined
226    (e.g. in mysqld). See sql/debug_sync.cc.
227  */
228 DebugSyncCallbackFp debug_sync_C_callback_ptr;
229 #endif /* defined(ENABLED_DEBUG_SYNC) */
230 
231 /* How to disable options */
232 bool my_disable_locking = false;
233 bool my_enable_symlinks = false;
234