set_var.cc 111 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
unknown's avatar
unknown committed
2 3 4

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
  Handling of MySQL SQL variables

  To add a new variable, one has to do the following:

  - Use one of the 'sys_var... classes from set_var.h or write a specific
    one for the variable type.
  - Define it in the 'variable definition list' in this file.
24 25
  - If the variable is thread specific, add it to 'system_variables' struct.
    If not, add it to mysqld.cc and an declaration in 'mysql_priv.h'
unknown's avatar
unknown committed
26
  - If the variable should be changed from the command line, add a definition
unknown's avatar
unknown committed
27
    of it in the my_option structure list in mysqld.cc
28 29
  - Don't forget to initialize new fields in global_system_variables and
    max_system_variables!
unknown's avatar
unknown committed
30

31 32 33 34 35 36
  NOTES:
    - Be careful with var->save_result: sys_var::check() only updates
    ulonglong_value; so other members of the union are garbage then; to use
    them you must first assign a value to them (in specific ::check() for
    example).

unknown's avatar
unknown committed
37 38 39 40 41 42 43 44
  TODO:
    - Add full support for the variable character_set (for 4.1)

    - When updating myisam_delay_key_write, we should do a 'flush tables'
      of all MyISAM tables to ensure that they are reopen with the
      new attribute.
*/

45
#ifdef USE_PRAGMA_IMPLEMENTATION
unknown's avatar
unknown committed
46 47 48 49
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
50
#include <mysql.h>
unknown's avatar
unknown committed
51
#include "slave.h"
52
#include "rpl_mi.h"
unknown's avatar
unknown committed
53
#include <my_getopt.h>
54
#include <thr_alarm.h>
unknown's avatar
unknown committed
55
#include <myisam.h>
56
#include <my_dir.h>
57

58
#include "events.h"
59

60
/* WITH_NDBCLUSTER_STORAGE_ENGINE */
61
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
62
extern ulong ndb_cache_check_time;
63
extern char opt_ndb_constrbuf[];
unknown's avatar
unknown committed
64
extern ulong ndb_extra_logging;
65 66
#endif

unknown's avatar
unknown committed
67 68 69 70
#ifdef HAVE_NDB_BINLOG
extern ulong ndb_report_thresh_binlog_epoch_slip;
extern ulong ndb_report_thresh_binlog_mem_usage;
#endif
71

unknown's avatar
unknown committed
72
extern CHARSET_INFO *character_set_filesystem;
73 74


unknown's avatar
unknown committed
75
static DYNAMIC_ARRAY fixed_show_vars;
unknown's avatar
unknown committed
76
static HASH system_variable_hash;
unknown's avatar
unknown committed
77

unknown's avatar
unknown committed
78 79 80
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
81
  array_elements(bool_type_names)-1, "", bool_type_names, NULL
unknown's avatar
unknown committed
82 83
};

84 85 86
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
87 88
  array_elements(delay_key_write_type_names)-1, "",
  delay_key_write_type_names, NULL
89 90
};

91 92 93
static int  sys_check_ftb_syntax(THD *thd,  set_var *var);
static bool sys_update_ftb_syntax(THD *thd, set_var * var);
static void sys_default_ftb_syntax(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
94 95 96 97
static bool sys_update_init_connect(THD*, set_var*);
static void sys_default_init_connect(THD*, enum_var_type type);
static bool sys_update_init_slave(THD*, set_var*);
static void sys_default_init_slave(THD*, enum_var_type type);
unknown's avatar
unknown committed
98 99
static bool set_option_bit(THD *thd, set_var *var);
static bool set_option_autocommit(THD *thd, set_var *var);
100
static int  check_log_update(THD *thd, set_var *var);
unknown's avatar
unknown committed
101
static bool set_log_update(THD *thd, set_var *var);
102
static int  check_pseudo_thread_id(THD *thd, set_var *var);
103
void fix_binlog_format_after_update(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
104
static void fix_low_priority_updates(THD *thd, enum_var_type type);
105
static int check_tx_isolation(THD *thd, set_var *var);
unknown's avatar
unknown committed
106
static void fix_tx_isolation(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
107 108
static int check_completion_type(THD *thd, set_var *var);
static void fix_completion_type(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
109 110
static void fix_net_read_timeout(THD *thd, enum_var_type type);
static void fix_net_write_timeout(THD *thd, enum_var_type type);
111
static void fix_net_retry_count(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
112 113
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
114
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type);
115
static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type);
116 117
static void fix_max_binlog_size(THD *thd, enum_var_type type);
static void fix_max_relay_log_size(THD *thd, enum_var_type type);
118
static void fix_max_connections(THD *thd, enum_var_type type);
119
static int check_max_delayed_threads(THD *thd, set_var *var);
120 121
static void fix_thd_mem_root(THD *thd, enum_var_type type);
static void fix_trans_mem_root(THD *thd, enum_var_type type);
122
static void fix_server_id(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
123
static KEY_CACHE *create_key_cache(const char *name, uint length);
124
void fix_sql_mode_var(THD *thd, enum_var_type type);
125 126 127
static uchar *get_error_count(THD *thd);
static uchar *get_warning_count(THD *thd);
static uchar *get_tmpdir(THD *thd);
128 129 130 131 132
static int  sys_check_log_path(THD *thd,  set_var *var);
static bool sys_update_general_log_path(THD *thd, set_var * var);
static void sys_default_general_log_path(THD *thd, enum_var_type type);
static bool sys_update_slow_log_path(THD *thd, set_var * var);
static void sys_default_slow_log_path(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
133 134 135 136 137

/*
  Variable definition list

  These are variables that can be set from the command line, in
138 139 140 141
  alphabetic order.

  The variables are linked into the list. A variable is added to
  it in the constructor (see sys_var class for details).
unknown's avatar
unknown committed
142 143
*/

unknown's avatar
unknown committed
144
static sys_var_chain vars = { NULL, NULL };
145

unknown's avatar
unknown committed
146
static sys_var_thd_ulong	sys_auto_increment_increment(&vars, "auto_increment_increment",
147
                                                     &SV::auto_increment_increment);
unknown's avatar
unknown committed
148
static sys_var_thd_ulong	sys_auto_increment_offset(&vars, "auto_increment_offset",
149 150
                                                  &SV::auto_increment_offset);

unknown's avatar
unknown committed
151
static sys_var_bool_ptr	sys_automatic_sp_privileges(&vars, "automatic_sp_privileges",
152 153
					      &sp_automatic_privileges);

unknown's avatar
unknown committed
154 155
static sys_var_const_str       sys_basedir(&vars, "basedir", mysql_home);
static sys_var_long_ptr	sys_binlog_cache_size(&vars, "binlog_cache_size",
unknown's avatar
unknown committed
156
					      &binlog_cache_size);
unknown's avatar
unknown committed
157
static sys_var_thd_binlog_format sys_binlog_format(&vars, "binlog_format",
158
                                            &SV::binlog_format);
unknown's avatar
unknown committed
159
static sys_var_thd_ulong	sys_bulk_insert_buff_size(&vars, "bulk_insert_buffer_size",
unknown's avatar
unknown committed
160
						  &SV::bulk_insert_buff_size);
unknown's avatar
unknown committed
161 162 163 164
static sys_var_character_set_sv	sys_character_set_server(&vars, "character_set_server",
                                        &SV::collation_server,
                                        &default_charset_info);
sys_var_const_str       sys_charset_system(&vars, "character_set_system",
165
                                           (char *)my_charset_utf8_general_ci.name);
unknown's avatar
unknown committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
static sys_var_character_set_database	sys_character_set_database(&vars, "character_set_database");
static sys_var_character_set_sv sys_character_set_client(&vars, "character_set_client",
                                        &SV::character_set_client,
                                        &default_charset_info);
static sys_var_character_set_sv sys_character_set_connection(&vars, "character_set_connection",
                                        &SV::collation_connection,
                                        &default_charset_info);
static sys_var_character_set_sv sys_character_set_results(&vars, "character_set_results",
                                        &SV::character_set_results,
                                        &default_charset_info, true);
static sys_var_character_set_sv sys_character_set_filesystem(&vars, "character_set_filesystem",
                                        &SV::character_set_filesystem,
                                        &character_set_filesystem);
static sys_var_thd_ulong	sys_completion_type(&vars, "completion_type",
unknown's avatar
unknown committed
180 181 182
					 &SV::completion_type,
					 check_completion_type,
					 fix_completion_type);
unknown's avatar
unknown committed
183 184 185 186 187 188 189 190 191 192
static sys_var_collation_sv sys_collation_connection(&vars, "collation_connection",
                                        &SV::collation_connection,
                                        &default_charset_info);
static sys_var_collation_sv sys_collation_database(&vars, "collation_database",
                                        &SV::collation_database,
                                        &default_charset_info);
static sys_var_collation_sv sys_collation_server(&vars, "collation_server",
                                        &SV::collation_server,
                                        &default_charset_info);
static sys_var_long_ptr	sys_concurrent_insert(&vars, "concurrent_insert",
193
                                              &myisam_concurrent_insert);
unknown's avatar
unknown committed
194
static sys_var_long_ptr	sys_connect_timeout(&vars, "connect_timeout",
unknown's avatar
unknown committed
195
					    &connect_timeout);
unknown's avatar
unknown committed
196
static sys_var_const_str       sys_datadir(&vars, "datadir", mysql_real_data_home);
unknown's avatar
unknown committed
197
#ifndef DBUG_OFF
unknown's avatar
unknown committed
198
static sys_var_thd_dbug        sys_dbug(&vars, "debug");
unknown's avatar
unknown committed
199
#endif
unknown's avatar
unknown committed
200
static sys_var_enum		sys_delay_key_write(&vars, "delay_key_write",
201 202 203
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
unknown's avatar
unknown committed
204
static sys_var_long_ptr	sys_delayed_insert_limit(&vars, "delayed_insert_limit",
unknown's avatar
unknown committed
205
						 &delayed_insert_limit);
unknown's avatar
unknown committed
206
static sys_var_long_ptr	sys_delayed_insert_timeout(&vars, "delayed_insert_timeout",
unknown's avatar
unknown committed
207
						   &delayed_insert_timeout);
unknown's avatar
unknown committed
208
static sys_var_long_ptr	sys_delayed_queue_size(&vars, "delayed_queue_size",
unknown's avatar
unknown committed
209
					       &delayed_queue_size);
210

unknown's avatar
unknown committed
211 212
static sys_var_event_scheduler sys_event_scheduler(&vars, "event_scheduler");
static sys_var_long_ptr	sys_expire_logs_days(&vars, "expire_logs_days",
unknown's avatar
unknown committed
213
					     &expire_logs_days);
unknown's avatar
unknown committed
214 215 216
static sys_var_bool_ptr	sys_flush(&vars, "flush", &myisam_flush);
static sys_var_long_ptr	sys_flush_time(&vars, "flush_time", &flush_time);
static sys_var_str             sys_ft_boolean_syntax(&vars, "ft_boolean_syntax",
217 218 219 220
                                         sys_check_ftb_syntax,
                                         sys_update_ftb_syntax,
                                         sys_default_ftb_syntax,
                                         ft_boolean_syntax);
unknown's avatar
unknown committed
221
sys_var_str             sys_init_connect(&vars, "init_connect", 0,
222 223
                                         sys_update_init_connect,
                                         sys_default_init_connect,0);
unknown's avatar
unknown committed
224
sys_var_str             sys_init_slave(&vars, "init_slave", 0,
225 226
                                       sys_update_init_slave,
                                       sys_default_init_slave,0);
unknown's avatar
unknown committed
227
static sys_var_thd_ulong	sys_interactive_timeout(&vars, "interactive_timeout",
unknown's avatar
unknown committed
228
						&SV::net_interactive_timeout);
unknown's avatar
unknown committed
229
static sys_var_thd_ulong	sys_join_buffer_size(&vars, "join_buffer_size",
unknown's avatar
unknown committed
230
					     &SV::join_buff_size);
unknown's avatar
unknown committed
231 232
static sys_var_key_buffer_size	sys_key_buffer_size(&vars, "key_buffer_size");
static sys_var_key_cache_long  sys_key_cache_block_size(&vars, "key_cache_block_size",
unknown's avatar
unknown committed
233 234
						 offsetof(KEY_CACHE,
							  param_block_size));
unknown's avatar
unknown committed
235
static sys_var_key_cache_long	sys_key_cache_division_limit(&vars, "key_cache_division_limit",
unknown's avatar
unknown committed
236 237
						     offsetof(KEY_CACHE,
							      param_division_limit));
unknown's avatar
unknown committed
238
static sys_var_key_cache_long  sys_key_cache_age_threshold(&vars, "key_cache_age_threshold",
unknown's avatar
unknown committed
239 240
						     offsetof(KEY_CACHE,
							      param_age_threshold));
unknown's avatar
unknown committed
241
static sys_var_bool_ptr	sys_local_infile(&vars, "local_infile",
unknown's avatar
unknown committed
242
					 &opt_local_infile);
unknown's avatar
unknown committed
243 244
static sys_var_trust_routine_creators
sys_trust_routine_creators(&vars, "log_bin_trust_routine_creators",
245
                           &trust_function_creators);
unknown's avatar
unknown committed
246 247
static sys_var_bool_ptr       
sys_trust_function_creators(&vars, "log_bin_trust_function_creators",
248
                            &trust_function_creators);
unknown's avatar
unknown committed
249 250
static sys_var_bool_ptr
  sys_log_queries_not_using_indexes(&vars, "log_queries_not_using_indexes",
251
                                    &opt_log_queries_not_using_indexes);
unknown's avatar
unknown committed
252 253
static sys_var_thd_ulong	sys_log_warnings(&vars, "log_warnings", &SV::log_warnings);
static sys_var_thd_ulong	sys_long_query_time(&vars, "long_query_time",
unknown's avatar
unknown committed
254
					     &SV::long_query_time);
unknown's avatar
unknown committed
255
static sys_var_thd_bool	sys_low_priority_updates(&vars, "low_priority_updates",
unknown's avatar
unknown committed
256 257 258
						 &SV::low_priority_updates,
						 fix_low_priority_updates);
#ifndef TO_BE_DELETED	/* Alias for the low_priority_updates */
unknown's avatar
unknown committed
259
static sys_var_thd_bool	sys_sql_low_priority_updates(&vars, "sql_low_priority_updates",
unknown's avatar
unknown committed
260 261 262
						     &SV::low_priority_updates,
						     fix_low_priority_updates);
#endif
unknown's avatar
unknown committed
263
static sys_var_thd_ulong	sys_max_allowed_packet(&vars, "max_allowed_packet",
unknown's avatar
unknown committed
264
					       &SV::max_allowed_packet);
unknown's avatar
unknown committed
265
static sys_var_long_ptr	sys_max_binlog_cache_size(&vars, "max_binlog_cache_size",
unknown's avatar
unknown committed
266
						  &max_binlog_cache_size);
unknown's avatar
unknown committed
267
static sys_var_long_ptr	sys_max_binlog_size(&vars, "max_binlog_size",
268 269
					    &max_binlog_size,
                                            fix_max_binlog_size);
unknown's avatar
unknown committed
270
static sys_var_long_ptr	sys_max_connections(&vars, "max_connections",
271 272
					    &max_connections,
                                            fix_max_connections);
unknown's avatar
unknown committed
273
static sys_var_long_ptr	sys_max_connect_errors(&vars, "max_connect_errors",
unknown's avatar
unknown committed
274
					       &max_connect_errors);
unknown's avatar
unknown committed
275
static sys_var_thd_ulong       sys_max_insert_delayed_threads(&vars, "max_insert_delayed_threads",
276 277 278
						       &SV::max_insert_delayed_threads,
                                                       check_max_delayed_threads,
                                                       fix_max_connections);
unknown's avatar
unknown committed
279
static sys_var_thd_ulong	sys_max_delayed_threads(&vars, "max_delayed_threads",
280
						&SV::max_insert_delayed_threads,
281 282
                                                check_max_delayed_threads,
                                                fix_max_connections);
unknown's avatar
unknown committed
283
static sys_var_thd_ulong	sys_max_error_count(&vars, "max_error_count",
284
					    &SV::max_error_count);
unknown's avatar
unknown committed
285
static sys_var_thd_ulonglong	sys_max_heap_table_size(&vars, "max_heap_table_size",
unknown's avatar
unknown committed
286
						&SV::max_heap_table_size);
unknown's avatar
unknown committed
287
static sys_var_thd_ulong       sys_pseudo_thread_id(&vars, "pseudo_thread_id",
288 289
					     &SV::pseudo_thread_id,
                                             check_pseudo_thread_id, 0);
unknown's avatar
unknown committed
290
static sys_var_thd_ha_rows	sys_max_join_size(&vars, "max_join_size",
unknown's avatar
unknown committed
291 292
					  &SV::max_join_size,
					  fix_max_join_size);
unknown's avatar
unknown committed
293
static sys_var_thd_ulong	sys_max_seeks_for_key(&vars, "max_seeks_for_key",
294
					      &SV::max_seeks_for_key);
unknown's avatar
unknown committed
295
static sys_var_thd_ulong   sys_max_length_for_sort_data(&vars, "max_length_for_sort_data",
296
                                                 &SV::max_length_for_sort_data);
unknown's avatar
unknown committed
297
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
unknown's avatar
unknown committed
298
static sys_var_thd_ha_rows	sys_sql_max_join_size(&vars, "sql_max_join_size",
unknown's avatar
unknown committed
299 300 301
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
302
static sys_var_long_ptr_global
unknown's avatar
unknown committed
303
sys_max_prepared_stmt_count(&vars, "max_prepared_stmt_count",
304 305
                            &max_prepared_stmt_count,
                            &LOCK_prepared_stmt_count);
unknown's avatar
unknown committed
306
static sys_var_long_ptr	sys_max_relay_log_size(&vars, "max_relay_log_size",
307 308
                                               &max_relay_log_size,
                                               fix_max_relay_log_size);
unknown's avatar
unknown committed
309
static sys_var_thd_ulong	sys_max_sort_length(&vars, "max_sort_length",
unknown's avatar
unknown committed
310
					    &SV::max_sort_length);
unknown's avatar
unknown committed
311
static sys_var_thd_ulong	sys_max_sp_recursion_depth(&vars, "max_sp_recursion_depth",
312
                                                   &SV::max_sp_recursion_depth);
unknown's avatar
unknown committed
313 314
static sys_var_max_user_conn   sys_max_user_connections(&vars, "max_user_connections");
static sys_var_thd_ulong	sys_max_tmp_tables(&vars, "max_tmp_tables",
unknown's avatar
unknown committed
315
					   &SV::max_tmp_tables);
unknown's avatar
unknown committed
316
static sys_var_long_ptr	sys_max_write_lock_count(&vars, "max_write_lock_count",
unknown's avatar
unknown committed
317
						 &max_write_lock_count);
unknown's avatar
unknown committed
318
static sys_var_thd_ulong       sys_multi_range_count(&vars, "multi_range_count",
unknown's avatar
unknown committed
319
                                              &SV::multi_range_count);
unknown's avatar
unknown committed
320
static sys_var_long_ptr	sys_myisam_data_pointer_size(&vars, "myisam_data_pointer_size",
unknown's avatar
unknown committed
321
                                                    &myisam_data_pointer_size);
unknown's avatar
unknown committed
322 323 324
static sys_var_thd_ulonglong	sys_myisam_max_sort_file_size(&vars, "myisam_max_sort_file_size", &SV::myisam_max_sort_file_size, fix_myisam_max_sort_file_size, 1);
static sys_var_thd_ulong       sys_myisam_repair_threads(&vars, "myisam_repair_threads", &SV::myisam_repair_threads);
static sys_var_thd_ulong	sys_myisam_sort_buffer_size(&vars, "myisam_sort_buffer_size", &SV::myisam_sort_buff_size);
unknown's avatar
unknown committed
325
static sys_var_bool_ptr	sys_myisam_use_mmap(&vars, "myisam_use_mmap",
unknown's avatar
unknown committed
326
                                            &opt_myisam_use_mmap);
327

unknown's avatar
unknown committed
328
static sys_var_thd_enum         sys_myisam_stats_method(&vars, "myisam_stats_method",
329 330 331 332
                                                &SV::myisam_stats_method,
                                                &myisam_stats_method_typelib,
                                                NULL);

unknown's avatar
unknown committed
333
static sys_var_thd_ulong	sys_net_buffer_length(&vars, "net_buffer_length",
unknown's avatar
unknown committed
334
					      &SV::net_buffer_length);
unknown's avatar
unknown committed
335
static sys_var_thd_ulong	sys_net_read_timeout(&vars, "net_read_timeout",
unknown's avatar
unknown committed
336
					     &SV::net_read_timeout,
337
					     0, fix_net_read_timeout);
unknown's avatar
unknown committed
338
static sys_var_thd_ulong	sys_net_write_timeout(&vars, "net_write_timeout",
unknown's avatar
unknown committed
339
					      &SV::net_write_timeout,
340
					      0, fix_net_write_timeout);
unknown's avatar
unknown committed
341
static sys_var_thd_ulong	sys_net_retry_count(&vars, "net_retry_count",
342
					    &SV::net_retry_count,
343
					    0, fix_net_retry_count);
unknown's avatar
unknown committed
344
static sys_var_thd_bool	sys_new_mode(&vars, "new", &SV::new_mode);
unknown's avatar
unknown committed
345
static sys_var_bool_ptr_readonly sys_old_mode(&vars, "old",
unknown's avatar
unknown committed
346
                                       &global_system_variables.old_mode);
unknown's avatar
unknown committed
347 348 349 350 351
/* these two cannot be static */
sys_var_thd_bool                sys_old_alter_table(&vars, "old_alter_table",
                                            &SV::old_alter_table);
sys_var_thd_bool                sys_old_passwords(&vars, "old_passwords", &SV::old_passwords);
static sys_var_thd_ulong        sys_optimizer_prune_level(&vars, "optimizer_prune_level",
352
                                                  &SV::optimizer_prune_level);
unknown's avatar
unknown committed
353
static sys_var_thd_ulong        sys_optimizer_search_depth(&vars, "optimizer_search_depth",
354
                                                   &SV::optimizer_search_depth);
unknown's avatar
unknown committed
355
static sys_var_thd_ulong        sys_preload_buff_size(&vars, "preload_buffer_size",
unknown's avatar
unknown committed
356
                                              &SV::preload_buff_size);
unknown's avatar
unknown committed
357
static sys_var_thd_ulong	sys_read_buff_size(&vars, "read_buffer_size",
unknown's avatar
unknown committed
358
					   &SV::read_buff_size);
unknown's avatar
unknown committed
359 360
static sys_var_opt_readonly	sys_readonly(&vars, "read_only", &opt_readonly);
static sys_var_thd_ulong	sys_read_rnd_buff_size(&vars, "read_rnd_buffer_size",
unknown's avatar
unknown committed
361
					       &SV::read_rnd_buff_size);
unknown's avatar
unknown committed
362
static sys_var_thd_ulong	sys_div_precincrement(&vars, "div_precision_increment",
unknown's avatar
unknown committed
363
                                              &SV::div_precincrement);
unknown's avatar
unknown committed
364
static sys_var_long_ptr	sys_rpl_recovery_rank(&vars, "rpl_recovery_rank",
unknown's avatar
unknown committed
365
					      &rpl_recovery_rank);
unknown's avatar
unknown committed
366
static sys_var_long_ptr	sys_query_cache_size(&vars, "query_cache_size",
unknown's avatar
unknown committed
367 368
					     &query_cache_size,
					     fix_query_cache_size);
369

unknown's avatar
unknown committed
370
static sys_var_thd_ulong	sys_range_alloc_block_size(&vars, "range_alloc_block_size",
371
						   &SV::range_alloc_block_size);
unknown's avatar
unknown committed
372
static sys_var_thd_ulong	sys_query_alloc_block_size(&vars, "query_alloc_block_size",
373 374
						   &SV::query_alloc_block_size,
						   0, fix_thd_mem_root);
unknown's avatar
unknown committed
375
static sys_var_thd_ulong	sys_query_prealloc_size(&vars, "query_prealloc_size",
376 377
						&SV::query_prealloc_size,
						0, fix_thd_mem_root);
unknown's avatar
unknown committed
378 379
static sys_var_readonly        sys_tmpdir(&vars, "tmpdir", OPT_GLOBAL, SHOW_CHAR, get_tmpdir);
static sys_var_thd_ulong	sys_trans_alloc_block_size(&vars, "transaction_alloc_block_size",
380 381
						   &SV::trans_alloc_block_size,
						   0, fix_trans_mem_root);
unknown's avatar
unknown committed
382
static sys_var_thd_ulong	sys_trans_prealloc_size(&vars, "transaction_prealloc_size",
383 384
						&SV::trans_prealloc_size,
						0, fix_trans_mem_root);
unknown's avatar
unknown committed
385
sys_var_thd_enum        sys_thread_handling(&vars, "thread_handling",
unknown's avatar
unknown committed
386 387 388
                                            &SV::thread_handling,
                                            &thread_handling_typelib,
                                            NULL);
389

unknown's avatar
unknown committed
390
#ifdef HAVE_QUERY_CACHE
unknown's avatar
unknown committed
391
static sys_var_long_ptr	sys_query_cache_limit(&vars, "query_cache_limit",
unknown's avatar
unknown committed
392
					      &query_cache.query_cache_limit);
unknown's avatar
unknown committed
393
static sys_var_long_ptr        sys_query_cache_min_res_unit(&vars, "query_cache_min_res_unit",
394 395
						     &query_cache_min_res_unit,
						     fix_query_cache_min_res_unit);
unknown's avatar
unknown committed
396
static sys_var_thd_enum	sys_query_cache_type(&vars, "query_cache_type",
unknown's avatar
unknown committed
397 398
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
unknown's avatar
unknown committed
399 400
static sys_var_thd_bool
sys_query_cache_wlock_invalidate(&vars, "query_cache_wlock_invalidate",
401
				 &SV::query_cache_wlock_invalidate);
unknown's avatar
unknown committed
402
#endif /* HAVE_QUERY_CACHE */
unknown's avatar
unknown committed
403
static sys_var_bool_ptr	sys_secure_auth(&vars, "secure_auth", &opt_secure_auth);
404
static sys_var_const_str_ptr sys_secure_file_priv(&vars, "secure_file_priv",
405
                                             &opt_secure_file_priv);
unknown's avatar
unknown committed
406 407
static sys_var_long_ptr	sys_server_id(&vars, "server_id", &server_id, fix_server_id);
static sys_var_bool_ptr	sys_slave_compressed_protocol(&vars, "slave_compressed_protocol",
408
						      &opt_slave_compressed_protocol);
unknown's avatar
unknown committed
409
static sys_var_long_ptr	sys_slow_launch_time(&vars, "slow_launch_time",
unknown's avatar
unknown committed
410
					     &slow_launch_time);
unknown's avatar
unknown committed
411
static sys_var_thd_ulong	sys_sort_buffer(&vars, "sort_buffer_size",
unknown's avatar
unknown committed
412
					&SV::sortbuff_size);
unknown's avatar
unknown committed
413
static sys_var_thd_sql_mode    sys_sql_mode(&vars, "sql_mode",
414
                                     &SV::sql_mode);
415 416 417
#ifdef HAVE_OPENSSL
extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
            *opt_ssl_key;
unknown's avatar
unknown committed
418 419 420 421 422
static sys_var_const_str_ptr	sys_ssl_ca(&vars, "ssl_ca", &opt_ssl_ca);
static sys_var_const_str_ptr	sys_ssl_capath(&vars, "ssl_capath", &opt_ssl_capath);
static sys_var_const_str_ptr	sys_ssl_cert(&vars, "ssl_cert", &opt_ssl_cert);
static sys_var_const_str_ptr	sys_ssl_cipher(&vars, "ssl_cipher", &opt_ssl_cipher);
static sys_var_const_str_ptr	sys_ssl_key(&vars, "ssl_key", &opt_ssl_key);
423
#else
unknown's avatar
unknown committed
424 425 426 427 428
static sys_var_const_str	sys_ssl_ca(&vars, "ssl_ca", NULL);
static sys_var_const_str	sys_ssl_capath(&vars, "ssl_capath", NULL);
static sys_var_const_str	sys_ssl_cert(&vars, "ssl_cert", NULL);
static sys_var_const_str	sys_ssl_cipher(&vars, "ssl_cipher", NULL);
static sys_var_const_str	sys_ssl_key(&vars, "ssl_key", NULL);
429
#endif
unknown's avatar
unknown committed
430 431
static sys_var_thd_enum
sys_updatable_views_with_limit(&vars, "updatable_views_with_limit",
432 433
                               &SV::updatable_views_with_limit,
                               &updatable_views_with_limit_typelib);
unknown's avatar
VIEW  
unknown committed
434

unknown's avatar
unknown committed
435 436 437 438 439 440
static sys_var_thd_table_type  sys_table_type(&vars, "table_type",
				       &SV::table_plugin);
static sys_var_thd_storage_engine sys_storage_engine(&vars, "storage_engine",
				       &SV::table_plugin);
static sys_var_bool_ptr	sys_sync_frm(&vars, "sync_frm", &opt_sync_frm);
static sys_var_const_str	sys_system_time_zone(&vars, "system_time_zone",
441
                                             system_time_zone);
unknown's avatar
unknown committed
442
static sys_var_long_ptr	sys_table_def_size(&vars, "table_definition_cache",
unknown's avatar
unknown committed
443
                                           &table_def_size);
unknown's avatar
unknown committed
444
static sys_var_long_ptr	sys_table_cache_size(&vars, "table_open_cache",
unknown's avatar
unknown committed
445
					     &table_cache_size);
unknown's avatar
unknown committed
446
static sys_var_long_ptr	sys_table_lock_wait_timeout(&vars, "table_lock_wait_timeout",
447
                                                    &table_lock_wait_timeout);
unknown's avatar
unknown committed
448
static sys_var_long_ptr	sys_thread_cache_size(&vars, "thread_cache_size",
unknown's avatar
unknown committed
449
					      &thread_cache_size);
unknown's avatar
unknown committed
450
#if HAVE_POOL_OF_THREADS == 1
unknown's avatar
unknown committed
451
sys_var_long_ptr	sys_thread_pool_size(&vars, "thread_pool_size",
unknown's avatar
unknown committed
452 453
					      &thread_pool_size);
#endif
unknown's avatar
unknown committed
454
static sys_var_thd_enum	sys_tx_isolation(&vars, "tx_isolation",
unknown's avatar
unknown committed
455 456
					 &SV::tx_isolation,
					 &tx_isolation_typelib,
457 458
					 fix_tx_isolation,
					 check_tx_isolation);
unknown's avatar
unknown committed
459
static sys_var_thd_ulonglong	sys_tmp_table_size(&vars, "tmp_table_size",
unknown's avatar
unknown committed
460
					   &SV::tmp_table_size);
unknown's avatar
unknown committed
461
static sys_var_bool_ptr  sys_timed_mutexes(&vars, "timed_mutexes",
unknown's avatar
unknown committed
462
                                    &timed_mutexes);
unknown's avatar
unknown committed
463 464
static sys_var_const_str	sys_version(&vars, "version", server_version);
static sys_var_const_str	sys_version_comment(&vars, "version_comment",
465
                                            MYSQL_COMPILATION_COMMENT);
unknown's avatar
unknown committed
466
static sys_var_const_str	sys_version_compile_machine(&vars, "version_compile_machine",
467
                                                    MACHINE_TYPE);
unknown's avatar
unknown committed
468
static sys_var_const_str	sys_version_compile_os(&vars, "version_compile_os",
469
                                               SYSTEM_TYPE);
unknown's avatar
unknown committed
470
static sys_var_thd_ulong	sys_net_wait_timeout(&vars, "wait_timeout",
unknown's avatar
unknown committed
471
					     &SV::net_wait_timeout);
unknown's avatar
unknown committed
472

unknown's avatar
unknown committed
473
/* Condition pushdown to storage engine */
unknown's avatar
unknown committed
474 475
static sys_var_thd_bool
sys_engine_condition_pushdown(&vars, "engine_condition_pushdown",
unknown's avatar
unknown committed
476 477
			      &SV::engine_condition_pushdown);

478
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
479
/* ndb thread specific variable settings */
unknown's avatar
unknown committed
480 481
static sys_var_thd_ulong
sys_ndb_autoincrement_prefetch_sz(&vars, "ndb_autoincrement_prefetch_sz",
482
				  &SV::ndb_autoincrement_prefetch_sz);
unknown's avatar
unknown committed
483 484
static sys_var_thd_bool
sys_ndb_force_send(&vars, "ndb_force_send", &SV::ndb_force_send);
unknown's avatar
unknown committed
485
#ifdef HAVE_NDB_BINLOG
unknown's avatar
unknown committed
486 487
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_epoch_slip(&vars, "ndb_report_thresh_binlog_epoch_slip",
unknown's avatar
unknown committed
488
                                        &ndb_report_thresh_binlog_epoch_slip);
unknown's avatar
unknown committed
489 490
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_mem_usage(&vars, "ndb_report_thresh_binlog_mem_usage",
unknown's avatar
unknown committed
491 492
                                       &ndb_report_thresh_binlog_mem_usage);
#endif
unknown's avatar
unknown committed
493 494 495 496 497 498
static sys_var_thd_bool
sys_ndb_use_exact_count(&vars, "ndb_use_exact_count", &SV::ndb_use_exact_count);
static sys_var_thd_bool
sys_ndb_use_transactions(&vars, "ndb_use_transactions", &SV::ndb_use_transactions);
static sys_var_long_ptr
sys_ndb_cache_check_time(&vars, "ndb_cache_check_time", &ndb_cache_check_time);
499 500
static sys_var_const_str
sys_ndb_connectstring(&vars, "ndb_connectstring", opt_ndb_constrbuf);
unknown's avatar
unknown committed
501 502
static sys_var_thd_bool
sys_ndb_index_stat_enable(&vars, "ndb_index_stat_enable",
503
                          &SV::ndb_index_stat_enable);
unknown's avatar
unknown committed
504 505
static sys_var_thd_ulong
sys_ndb_index_stat_cache_entries(&vars, "ndb_index_stat_cache_entries",
506
                                 &SV::ndb_index_stat_cache_entries);
unknown's avatar
unknown committed
507 508
static sys_var_thd_ulong
sys_ndb_index_stat_update_freq(&vars, "ndb_index_stat_update_freq",
509
                               &SV::ndb_index_stat_update_freq);
unknown's avatar
unknown committed
510 511 512 513
static sys_var_long_ptr
sys_ndb_extra_logging(&vars, "ndb_extra_logging", &ndb_extra_logging);
static sys_var_thd_bool
sys_ndb_use_copying_alter_table(&vars, "ndb_use_copying_alter_table", &SV::ndb_use_copying_alter_table);
514
#endif //WITH_NDBCLUSTER_STORAGE_ENGINE
515 516 517

/* Time/date/datetime formats */

unknown's avatar
unknown committed
518
static sys_var_thd_date_time_format sys_time_format(&vars, "time_format",
519
					     &SV::time_format,
520
					     MYSQL_TIMESTAMP_TIME);
unknown's avatar
unknown committed
521
static sys_var_thd_date_time_format sys_date_format(&vars, "date_format",
522
					     &SV::date_format,
523
					     MYSQL_TIMESTAMP_DATE);
unknown's avatar
unknown committed
524
static sys_var_thd_date_time_format sys_datetime_format(&vars, "datetime_format",
525
						 &SV::datetime_format,
526
						 MYSQL_TIMESTAMP_DATETIME);
527 528

/* Variables that are bits in THD */
unknown's avatar
unknown committed
529

unknown's avatar
unknown committed
530
sys_var_thd_bit sys_autocommit(&vars, "autocommit", 0,
531 532 533
                               set_option_autocommit,
                               OPTION_NOT_AUTOCOMMIT,
                               1);
unknown's avatar
unknown committed
534
static sys_var_thd_bit	sys_big_tables(&vars, "big_tables", 0,
unknown's avatar
unknown committed
535 536 537
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
unknown's avatar
unknown committed
538
static sys_var_thd_bit	sys_sql_big_tables(&vars, "sql_big_tables", 0,
unknown's avatar
unknown committed
539 540 541
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
unknown's avatar
unknown committed
542
static sys_var_thd_bit	sys_big_selects(&vars, "sql_big_selects", 0,
unknown's avatar
unknown committed
543
					set_option_bit,
544
					OPTION_BIG_SELECTS);
unknown's avatar
unknown committed
545
static sys_var_thd_bit	sys_log_off(&vars, "sql_log_off",
546
				    check_log_update,
unknown's avatar
unknown committed
547 548
				    set_option_bit,
				    OPTION_LOG_OFF);
unknown's avatar
unknown committed
549
static sys_var_thd_bit	sys_log_update(&vars, "sql_log_update",
550
                                       check_log_update,
unknown's avatar
unknown committed
551
				       set_log_update,
552
				       OPTION_BIN_LOG);
unknown's avatar
unknown committed
553
static sys_var_thd_bit	sys_log_binlog(&vars, "sql_log_bin",
554
                                       check_log_update,
555
				       set_option_bit,
556
				       OPTION_BIN_LOG);
unknown's avatar
unknown committed
557
static sys_var_thd_bit	sys_sql_warnings(&vars, "sql_warnings", 0,
unknown's avatar
unknown committed
558 559
					 set_option_bit,
					 OPTION_WARNINGS);
unknown's avatar
unknown committed
560
static sys_var_thd_bit	sys_sql_notes(&vars, "sql_notes", 0,
561
					 set_option_bit,
562
					 OPTION_SQL_NOTES);
unknown's avatar
unknown committed
563
static sys_var_thd_bit	sys_auto_is_null(&vars, "sql_auto_is_null", 0,
unknown's avatar
unknown committed
564 565
					 set_option_bit,
					 OPTION_AUTO_IS_NULL);
unknown's avatar
unknown committed
566
static sys_var_thd_bit	sys_safe_updates(&vars, "sql_safe_updates", 0,
unknown's avatar
unknown committed
567 568
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
unknown's avatar
unknown committed
569
static sys_var_thd_bit	sys_buffer_results(&vars, "sql_buffer_result", 0,
unknown's avatar
unknown committed
570 571
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
unknown's avatar
unknown committed
572
static sys_var_thd_bit	sys_quote_show_create(&vars, "sql_quote_show_create", 0,
unknown's avatar
unknown committed
573 574
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
unknown's avatar
unknown committed
575
static sys_var_thd_bit	sys_foreign_key_checks(&vars, "foreign_key_checks", 0,
576 577 578
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
					       1);
unknown's avatar
unknown committed
579
static sys_var_thd_bit	sys_unique_checks(&vars, "unique_checks", 0,
580 581 582 583
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
					  1);

unknown's avatar
unknown committed
584 585
/* Local state variables */

unknown's avatar
unknown committed
586
static sys_var_thd_ha_rows	sys_select_limit(&vars, "sql_select_limit",
unknown's avatar
unknown committed
587
						 &SV::select_limit);
unknown's avatar
unknown committed
588 589 590
static sys_var_timestamp	sys_timestamp(&vars, "timestamp");
static sys_var_last_insert_id	sys_last_insert_id(&vars, "last_insert_id");
static sys_var_last_insert_id	sys_identity(&vars, "identity");
unknown's avatar
unknown committed
591

unknown's avatar
unknown committed
592
static sys_var_thd_lc_time_names       sys_lc_time_names(&vars, "lc_time_names");
unknown's avatar
unknown committed
593

unknown's avatar
unknown committed
594 595
static sys_var_insert_id	sys_insert_id(&vars, "insert_id");
static sys_var_readonly		sys_error_count(&vars, "error_count",
596 597 598
						OPT_SESSION,
						SHOW_LONG,
						get_error_count);
unknown's avatar
unknown committed
599
static sys_var_readonly		sys_warning_count(&vars, "warning_count",
600 601 602 603
						  OPT_SESSION,
						  SHOW_LONG,
						  get_warning_count);

unknown's avatar
unknown committed
604
/* alias for last_insert_id() to be compatible with Sybase */
unknown's avatar
unknown committed
605 606
static sys_var_rand_seed1	sys_rand_seed1(&vars, "rand_seed1");
static sys_var_rand_seed2	sys_rand_seed2(&vars, "rand_seed2");
unknown's avatar
unknown committed
607

unknown's avatar
unknown committed
608
static sys_var_thd_ulong        sys_default_week_format(&vars, "default_week_format",
609
					                &SV::default_week_format);
unknown's avatar
unknown committed
610

unknown's avatar
unknown committed
611
sys_var_thd_ulong               sys_group_concat_max_len(&vars, "group_concat_max_len",
unknown's avatar
unknown committed
612 613
                                                         &SV::group_concat_max_len);

unknown's avatar
unknown committed
614
sys_var_thd_time_zone            sys_time_zone(&vars, "time_zone");
615

616 617
/* Global read-only variable containing hostname */
static sys_var_const_str        sys_hostname(&vars, "hostname", glob_hostname);
618

619
sys_var_thd_bool  sys_keep_files_on_create(&vars, "keep_files_on_create", 
unknown's avatar
unknown committed
620
                                           &SV::keep_files_on_create);
621 622
/* Read only variables */

unknown's avatar
unknown committed
623 624 625 626 627 628 629
static sys_var_have_variable sys_have_compress(&vars, "have_compress", &have_compress);
static sys_var_have_variable sys_have_crypt(&vars, "have_crypt", &have_crypt);
static sys_var_have_plugin sys_have_csv(&vars, "have_csv", C_STRING_WITH_LEN("csv"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_variable sys_have_dlopen(&vars, "have_dynamic_loading", &have_dlopen);
static sys_var_have_variable sys_have_geometry(&vars, "have_geometry", &have_geometry);
static sys_var_have_plugin sys_have_innodb(&vars, "have_innodb", C_STRING_WITH_LEN("innodb"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_plugin sys_have_ndbcluster(&vars, "have_ndbcluster", C_STRING_WITH_LEN("ndbcluster"), MYSQL_STORAGE_ENGINE_PLUGIN);
unknown's avatar
unknown committed
630 631
static sys_var_have_variable sys_have_openssl(&vars, "have_openssl", &have_ssl);
static sys_var_have_variable sys_have_ssl(&vars, "have_ssl", &have_ssl);
unknown's avatar
unknown committed
632 633
static sys_var_have_plugin sys_have_partition_db(&vars, "have_partitioning", C_STRING_WITH_LEN("partition"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_variable sys_have_query_cache(&vars, "have_query_cache",
unknown's avatar
unknown committed
634
                                           &have_query_cache);
unknown's avatar
unknown committed
635 636
static sys_var_have_variable sys_have_rtree_keys(&vars, "have_rtree_keys", &have_rtree_keys);
static sys_var_have_variable sys_have_symlink(&vars, "have_symlink", &have_symlink);
637
/* Global read-only variable describing server license */
unknown's avatar
unknown committed
638
static sys_var_const_str	sys_license(&vars, "license", STRINGIFY_ARG(LICENSE));
639
/* Global variables which enable|disable logging */
unknown's avatar
unknown committed
640
static sys_var_log_state sys_var_general_log(&vars, "general_log", &opt_log,
641
                                      QUERY_LOG_GENERAL);
unknown's avatar
unknown committed
642
static sys_var_log_state sys_var_slow_query_log(&vars, "slow_query_log", &opt_slow_log,
643
                                         QUERY_LOG_SLOW);
unknown's avatar
unknown committed
644
sys_var_str sys_var_general_log_path(&vars, "general_log_file", sys_check_log_path,
645 646 647
				     sys_update_general_log_path,
				     sys_default_general_log_path,
				     opt_logname);
unknown's avatar
unknown committed
648
sys_var_str sys_var_slow_log_path(&vars, "slow_query_log_file", sys_check_log_path,
649 650 651
				  sys_update_slow_log_path, 
				  sys_default_slow_log_path,
				  opt_slow_logname);
unknown's avatar
unknown committed
652
static sys_var_log_output sys_var_log_output_state(&vars, "log_output", &log_output_options,
653 654
					    &log_output_typelib, 0);

655

unknown's avatar
unknown committed
656
/*
unknown's avatar
unknown committed
657 658 659 660
  Additional variables (not derived from sys_var class, not accessible as
  @@varname in SELECT or SET). Sorted in alphabetical order to facilitate
  maintenance - SHOW VARIABLES will sort its output.
  TODO: remove this list completely
unknown's avatar
unknown committed
661 662
*/

unknown's avatar
unknown committed
663 664
#define FIXED_VARS_SIZE (sizeof(fixed_vars) / sizeof(SHOW_VAR))
static SHOW_VAR fixed_vars[]= {
unknown's avatar
unknown committed
665
  {"back_log",                (char*) &back_log,                    SHOW_LONG},
666
  {"character_sets_dir",      mysql_charsets_dir,                   SHOW_CHAR},
unknown's avatar
unknown committed
667
  {"ft_max_word_len",         (char*) &ft_max_word_len,             SHOW_LONG},
668
  {"ft_min_word_len",         (char*) &ft_min_word_len,             SHOW_LONG},
669
  {"ft_query_expansion_limit",(char*) &ft_query_expansion_limit,    SHOW_LONG},
670
  {"ft_stopword_file",        (char*) &ft_stopword_file,            SHOW_CHAR_PTR},
671
  {"init_file",               (char*) &opt_init_file,               SHOW_CHAR_PTR},
unknown's avatar
unknown committed
672
  {"language",                language,                             SHOW_CHAR},
673
  {"large_files_support",     (char*) &opt_large_files,             SHOW_BOOL},
674
  {"large_page_size",         (char*) &opt_large_page_size,         SHOW_INT},
unknown's avatar
unknown committed
675
  {"large_pages",             (char*) &opt_large_pages,             SHOW_MY_BOOL},
unknown's avatar
unknown committed
676
#ifdef HAVE_MLOCKALL
unknown's avatar
unknown committed
677
  {"locked_in_memory",	      (char*) &locked_in_memory,	    SHOW_MY_BOOL},
unknown's avatar
unknown committed
678
#endif
unknown's avatar
unknown committed
679
  {"log",                     (char*) &opt_log,                     SHOW_MY_BOOL},
unknown's avatar
unknown committed
680
  {"log_bin",                 (char*) &opt_bin_log,                 SHOW_BOOL},
681
  {"log_error",               (char*) log_error_file,               SHOW_CHAR},
unknown's avatar
unknown committed
682
  {"log_slow_queries",        (char*) &opt_slow_log,                SHOW_MY_BOOL},
683
  {"lower_case_file_system",  (char*) &lower_case_file_system,      SHOW_MY_BOOL},
684 685
  {"lower_case_table_names",  (char*) &lower_case_table_names,      SHOW_INT},
  {"myisam_recover_options",  (char*) &myisam_recover_options_str,  SHOW_CHAR_PTR},
unknown's avatar
unknown committed
686
#ifdef __NT__
687
  {"named_pipe",	      (char*) &opt_enable_named_pipe,       SHOW_MY_BOOL},
688
#endif
unknown's avatar
unknown committed
689 690
  {"open_files_limit",	      (char*) &open_files_limit,	    SHOW_LONG},
  {"pid_file",                (char*) pidfile_name,                 SHOW_CHAR},
691
  {"plugin_dir",              (char*) opt_plugin_dir,               SHOW_CHAR},
unknown's avatar
unknown committed
692
  {"port",                    (char*) &mysqld_port,                 SHOW_INT},
693
  {"protocol_version",        (char*) &protocol_version,            SHOW_INT},
694 695 696 697
#ifdef HAVE_SMEM
  {"shared_memory",           (char*) &opt_enable_shared_memory,    SHOW_MY_BOOL},
  {"shared_memory_base_name", (char*) &shared_memory_base_name,     SHOW_CHAR_PTR},
#endif
698
  {"skip_external_locking",   (char*) &my_disable_locking,          SHOW_MY_BOOL},
unknown's avatar
unknown committed
699 700
  {"skip_networking",         (char*) &opt_disable_networking,      SHOW_BOOL},
  {"skip_show_database",      (char*) &opt_skip_show_db,            SHOW_BOOL},
701
#ifdef HAVE_SYS_UN_H
unknown's avatar
unknown committed
702
  {"socket",                  (char*) &mysqld_unix_port,            SHOW_CHAR_PTR},
703
#endif
unknown's avatar
unknown committed
704 705 706 707 708 709
#ifdef HAVE_THR_SETCONCURRENCY
  {"thread_concurrency",      (char*) &concurrency,                 SHOW_LONG},
#endif
  {"thread_stack",            (char*) &thread_stack,                SHOW_LONG},
};

710

711 712 713 714 715 716
bool sys_var::check(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
  return 0;
}

717 718 719 720 721 722 723
bool sys_var_str::check(THD *thd, set_var *var)
{
  int res;
  if (!check_func)
    return 0;

  if ((res=(*check_func)(thd, var)) < 0)
724 725
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0),
             name, var->value->str_value.ptr());
726 727 728
  return res;
}

unknown's avatar
unknown committed
729 730 731 732
/*
  Functions to check and update variables
*/

unknown's avatar
unknown committed
733

734 735 736 737 738 739 740 741 742 743
/*
  Update variables 'init_connect, init_slave'.

  In case of 'DEFAULT' value
  (for example: 'set GLOBAL init_connect=DEFAULT')
  'var' parameter is NULL pointer.
*/

bool update_sys_var_str(sys_var_str *var_str, rw_lock_t *var_mutex,
			set_var *var)
unknown's avatar
unknown committed
744
{
745 746 747 748
  char *res= 0, *old_value=(char *)(var ? var->value->str_value.ptr() : 0);
  uint new_length= (var ? var->value->str_value.length() : 0);
  if (!old_value)
    old_value= (char*) "";
749
  if (!(res= my_strndup(old_value, new_length, MYF(0))))
750
    return 1;
unknown's avatar
unknown committed
751 752 753 754
  /*
    Replace the old value in such a way that the any thread using
    the value will work.
  */
755 756 757 758 759
  rw_wrlock(var_mutex);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= new_length;
  rw_unlock(var_mutex);
unknown's avatar
unknown committed
760 761 762 763 764
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  return 0;
}


765 766 767 768 769 770
static bool sys_update_init_connect(THD *thd, set_var *var)
{
  return update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, var);
}


unknown's avatar
unknown committed
771 772
static void sys_default_init_connect(THD* thd, enum_var_type type)
{
773
  update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, 0);
unknown's avatar
unknown committed
774 775 776 777 778
}


static bool sys_update_init_slave(THD *thd, set_var *var)
{
779
  return update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, var);
unknown's avatar
unknown committed
780 781 782 783 784
}


static void sys_default_init_slave(THD* thd, enum_var_type type)
{
785
  update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, 0);
unknown's avatar
unknown committed
786 787
}

788 789
static int sys_check_ftb_syntax(THD *thd,  set_var *var)
{
790
  if (thd->security_ctx->master_access & SUPER_ACL)
791
    return (ft_boolean_check_syntax_string((uchar*)
792 793
                                           var->value->str_value.c_ptr()) ?
            -1 : 0);
794 795 796 797 798 799 800 801 802 803 804
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
}

static bool sys_update_ftb_syntax(THD *thd, set_var * var)
{
  strmake(ft_boolean_syntax, var->value->str_value.c_ptr(),
	  sizeof(ft_boolean_syntax)-1);
805 806 807 808 809

#ifdef HAVE_QUERY_CACHE
  query_cache.flush();
#endif /* HAVE_QUERY_CACHE */

810 811 812 813 814
  return 0;
}

static void sys_default_ftb_syntax(THD *thd, enum_var_type type)
{
815
  strmake(ft_boolean_syntax, def_ft_boolean_syntax,
816 817
	  sizeof(ft_boolean_syntax)-1);
}
unknown's avatar
unknown committed
818

unknown's avatar
unknown committed
819 820 821 822 823 824 825 826

/*
  If one sets the LOW_PRIORIY UPDATES flag, we also must change the
  used lock type
*/

static void fix_low_priority_updates(THD *thd, enum_var_type type)
{
827 828 829 830 831
  if (type == OPT_GLOBAL)
    thr_upgraded_concurrent_insert_lock= 
      (global_system_variables.low_priority_updates ?
       TL_WRITE_LOW_PRIORITY : TL_WRITE);
  else
unknown's avatar
unknown committed
832 833 834 835 836
    thd->update_lock_default= (thd->variables.low_priority_updates ?
			       TL_WRITE_LOW_PRIORITY : TL_WRITE);
}


837 838 839 840 841 842 843
static void
fix_myisam_max_sort_file_size(THD *thd, enum_var_type type)
{
  myisam_max_temp_length=
    (my_off_t) global_system_variables.myisam_max_sort_file_size;
}

unknown's avatar
unknown committed
844 845 846 847 848 849 850 851
/*
  Set the OPTION_BIG_SELECTS flag if max_join_size == HA_POS_ERROR
*/

static void fix_max_join_size(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
  {
852
    if (thd->variables.max_join_size == HA_POS_ERROR)
unknown's avatar
unknown committed
853 854 855 856 857
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
858

unknown's avatar
unknown committed
859

860 861 862 863 864 865 866 867 868 869 870 871 872 873
/*
  Can't change the 'next' tx_isolation while we are already in
  a transaction
*/
static int check_tx_isolation(THD *thd, set_var *var)
{
  if (var->type == OPT_DEFAULT && (thd->server_status & SERVER_STATUS_IN_TRANS))
  {
    my_error(ER_CANT_CHANGE_TX_ISOLATION, MYF(0));
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
874 875 876 877 878 879 880 881 882 883 884
/*
  If one doesn't use the SESSION modifier, the isolation level
  is only active for the next command
*/
static void fix_tx_isolation(THD *thd, enum_var_type type)
{
  if (type == OPT_SESSION)
    thd->session_tx_isolation= ((enum_tx_isolation)
				thd->variables.tx_isolation);
}

885 886
static void fix_completion_type(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused))) {}
unknown's avatar
unknown committed
887 888 889 890 891 892 893 894 895 896 897 898 899

static int check_completion_type(THD *thd, set_var *var)
{
  longlong val= var->value->val_int();
  if (val < 0 || val > 2)
  {
    char buf[64];
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
900 901 902 903 904

/*
  If we are changing the thread variable, we have to copy it to NET too
*/

unknown's avatar
SCRUM  
unknown committed
905
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
906 907 908
static void fix_net_read_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
909
    my_net_set_read_timeout(&thd->net, thd->variables.net_read_timeout);
unknown's avatar
unknown committed
910 911 912 913 914 915
}


static void fix_net_write_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
916
    my_net_set_write_timeout(&thd->net, thd->variables.net_write_timeout);
unknown's avatar
unknown committed
917 918
}

919 920 921 922 923
static void fix_net_retry_count(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    thd->net.retry_count=thd->variables.net_retry_count;
}
unknown's avatar
SCRUM  
unknown committed
924
#else /* HAVE_REPLICATION */
925 926
static void fix_net_read_timeout(THD *thd __attribute__((unused)),
				 enum_var_type type __attribute__((unused)))
927
{}
928 929
static void fix_net_write_timeout(THD *thd __attribute__((unused)),
				  enum_var_type type __attribute__((unused)))
930
{}
931 932
static void fix_net_retry_count(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused)))
933
{}
unknown's avatar
SCRUM  
unknown committed
934
#endif /* HAVE_REPLICATION */
935

unknown's avatar
unknown committed
936 937 938 939

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
940 941 942 943 944 945 946 947
  ulong new_cache_size= query_cache.resize(query_cache_size);

  /*
     Note: query_cache_size is a global variable reflecting the 
     requested cache size. See also query_cache_size_arg
  */

  if (query_cache_size != new_cache_size)
948 949
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			ER_WARN_QC_RESIZE, ER(ER_WARN_QC_RESIZE),
950 951 952
			query_cache_size, new_cache_size);
  
  query_cache_size= new_cache_size;
unknown's avatar
unknown committed
953 954 955 956
#endif
}


957 958 959 960 961 962 963 964 965
#ifdef HAVE_QUERY_CACHE
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type)
{
  query_cache_min_res_unit= 
    query_cache.set_min_res_unit(query_cache_min_res_unit);
}
#endif


966
extern void fix_delay_key_write(THD *thd, enum_var_type type)
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
{
  switch ((enum_delay_key_write) delay_key_write_options) {
  case DELAY_KEY_WRITE_NONE:
    myisam_delay_key_write=0;
    break;
  case DELAY_KEY_WRITE_ON:
    myisam_delay_key_write=1;
    break;
  case DELAY_KEY_WRITE_ALL:
    myisam_delay_key_write=1;
    ha_open_options|= HA_OPEN_DELAY_KEY_WRITE;
    break;
  }
}

982 983 984 985 986 987 988 989 990 991 992 993 994 995

bool sys_var_thd_binlog_format::is_readonly() const
{
  /*
    Under certain circumstances, the variable is read-only (unchangeable):
  */
  THD *thd= current_thd;
  /*
    If RBR and open temporary tables, their CREATE TABLE may not be in the
    binlog, so we can't toggle to SBR in this connection.
    The test below will also prevent SET GLOBAL, well it was not easy to test
    if global or not here.
    And this test will also prevent switching from RBR to RBR (a no-op which
    should not happen too often).
996 997 998

    If we don't have row-based replication compiled in, the variable
    is always read-only.
999 1000 1001 1002 1003 1004 1005 1006
  */
  if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW) &&
      thd->temporary_tables)
  {
    my_error(ER_TEMP_TABLE_PREVENTS_SWITCH_OUT_OF_RBR, MYF(0));
    return 1;
  }
  /*
unknown's avatar
unknown committed
1007
    if in a stored function/trigger, it's too late to change mode
1008
  */
unknown's avatar
unknown committed
1009
  if (thd->in_sub_stmt)
1010 1011 1012 1013 1014 1015 1016
  {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
    return 1;    
  }
  return sys_var_thd_enum::is_readonly();
}

unknown's avatar
unknown committed
1017

1018 1019 1020 1021 1022 1023
void fix_binlog_format_after_update(THD *thd, enum_var_type type)
{
  thd->reset_current_stmt_binlog_row_based();
}


1024
static void fix_max_binlog_size(THD *thd, enum_var_type type)
1025 1026 1027 1028 1029
{
  DBUG_ENTER("fix_max_binlog_size");
  DBUG_PRINT("info",("max_binlog_size=%lu max_relay_log_size=%lu",
                     max_binlog_size, max_relay_log_size));
  mysql_bin_log.set_max_size(max_binlog_size);
1030
#ifdef HAVE_REPLICATION
1031 1032
  if (!max_relay_log_size)
    active_mi->rli.relay_log.set_max_size(max_binlog_size);
unknown's avatar
unknown committed
1033
#endif
1034 1035 1036
  DBUG_VOID_RETURN;
}

1037
static void fix_max_relay_log_size(THD *thd, enum_var_type type)
1038 1039 1040 1041
{
  DBUG_ENTER("fix_max_relay_log_size");
  DBUG_PRINT("info",("max_binlog_size=%lu max_relay_log_size=%lu",
                     max_binlog_size, max_relay_log_size));
1042
#ifdef HAVE_REPLICATION
1043 1044
  active_mi->rli.relay_log.set_max_size(max_relay_log_size ?
                                        max_relay_log_size: max_binlog_size);
1045
#endif
1046 1047
  DBUG_VOID_RETURN;
}
1048

1049

1050 1051
static int check_max_delayed_threads(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
1052
  longlong val= var->value->val_int();
1053
  if (var->type != OPT_GLOBAL && val != 0 &&
unknown's avatar
unknown committed
1054
      val != (longlong) global_system_variables.max_insert_delayed_threads)
1055 1056
  {
    char buf[64];
1057
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
1058 1059 1060 1061 1062
    return 1;
  }
  return 0;
}

1063
static void fix_max_connections(THD *thd, enum_var_type type)
1064
{
1065
#ifndef EMBEDDED_LIBRARY
1066 1067
  resize_thr_alarm(max_connections + 
		   global_system_variables.max_insert_delayed_threads + 10);
1068
#endif
1069 1070 1071 1072 1073 1074
}


static void fix_thd_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
unknown's avatar
unknown committed
1075
    reset_root_defaults(thd->mem_root,
1076 1077 1078 1079 1080 1081 1082
                        thd->variables.query_alloc_block_size,
                        thd->variables.query_prealloc_size);
}


static void fix_trans_mem_root(THD *thd, enum_var_type type)
{
1083
#ifdef USING_TRANSACTIONS
1084 1085 1086 1087
  if (type != OPT_GLOBAL)
    reset_root_defaults(&thd->transaction.mem_root,
                        thd->variables.trans_alloc_block_size,
                        thd->variables.trans_prealloc_size);
1088
#endif
1089 1090
}

1091

1092 1093 1094 1095
static void fix_server_id(THD *thd, enum_var_type type)
{
  server_id_supplied = 1;
}
1096

1097 1098

sys_var_long_ptr::
unknown's avatar
unknown committed
1099
sys_var_long_ptr(sys_var_chain *chain, const char *name_arg, ulong *value_ptr_arg,
1100
                 sys_after_update_func after_update_arg)
unknown's avatar
unknown committed
1101
  :sys_var_long_ptr_global(chain, name_arg, value_ptr_arg,
1102 1103 1104 1105 1106
                           &LOCK_global_system_variables, after_update_arg)
{}


bool sys_var_long_ptr_global::check(THD *thd, set_var *var)
unknown's avatar
unknown committed
1107 1108 1109 1110 1111
{
  longlong v= var->value->val_int();
  var->save_result.ulonglong_value= v < 0 ? 0 : v;
  return 0;
}
1112

1113
bool sys_var_long_ptr_global::update(THD *thd, set_var *var)
unknown's avatar
unknown committed
1114
{
1115
  ulonglong tmp= var->save_result.ulonglong_value;
1116
  pthread_mutex_lock(guard);
unknown's avatar
unknown committed
1117 1118 1119 1120
  if (option_limits)
    *value= (ulong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulong) tmp;
1121
  pthread_mutex_unlock(guard);
unknown's avatar
unknown committed
1122 1123 1124 1125
  return 0;
}


1126
void sys_var_long_ptr_global::set_default(THD *thd, enum_var_type type)
unknown's avatar
unknown committed
1127
{
1128
  pthread_mutex_lock(guard);
unknown's avatar
unknown committed
1129
  *value= (ulong) option_limits->def_value;
1130
  pthread_mutex_unlock(guard);
unknown's avatar
unknown committed
1131 1132 1133
}


1134 1135
bool sys_var_ulonglong_ptr::update(THD *thd, set_var *var)
{
1136
  ulonglong tmp= var->save_result.ulonglong_value;
1137
  pthread_mutex_lock(&LOCK_global_system_variables);
1138 1139 1140 1141
  if (option_limits)
    *value= (ulonglong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulonglong) tmp;
1142
  pthread_mutex_unlock(&LOCK_global_system_variables);
1143 1144 1145 1146 1147 1148
  return 0;
}


void sys_var_ulonglong_ptr::set_default(THD *thd, enum_var_type type)
{
1149
  pthread_mutex_lock(&LOCK_global_system_variables);
1150
  *value= (ulonglong) option_limits->def_value;
1151
  pthread_mutex_unlock(&LOCK_global_system_variables);
1152 1153 1154
}


unknown's avatar
unknown committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
bool sys_var_bool_ptr::update(THD *thd, set_var *var)
{
  *value= (my_bool) var->save_result.ulong_value;
  return 0;
}


void sys_var_bool_ptr::set_default(THD *thd, enum_var_type type)
{
  *value= (my_bool) option_limits->def_value;
}


1168 1169 1170 1171 1172 1173 1174
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


1175
uchar *sys_var_enum::value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
1176
{
1177
  return (uchar*) enum_names->type_names[*value];
1178 1179
}

1180 1181 1182 1183 1184
bool sys_var_thd_ulong::check(THD *thd, set_var *var)
{
  return (sys_var_thd::check(thd, var) ||
          (check_func && (*check_func)(thd, var)));
}
1185

unknown's avatar
unknown committed
1186 1187
bool sys_var_thd_ulong::update(THD *thd, set_var *var)
{
1188
  ulonglong tmp= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
1189 1190 1191 1192 1193

  /* Don't use bigger value than given with --maximum-variable-name=.. */
  if ((ulong) tmp > max_system_variables.*offset)
    tmp= max_system_variables.*offset;

1194 1195 1196 1197 1198 1199
#if SIZEOF_LONG == 4
  /* Avoid overflows on 32 bit systems */
  if (tmp > (ulonglong) ~(ulong) 0)
    tmp= ((ulonglong) ~(ulong) 0);
#endif

unknown's avatar
unknown committed
1200 1201 1202
  if (option_limits)
    tmp= (ulong) getopt_ull_limit_value(tmp, option_limits);
  if (var->type == OPT_GLOBAL)
1203
    global_system_variables.*offset= (ulong) tmp;
unknown's avatar
unknown committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  else
    thd->variables.*offset= (ulong) tmp;
  return 0;
}


void sys_var_thd_ulong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
    /* We will not come here if option_limits is not set */
    global_system_variables.*offset= (ulong) option_limits->def_value;
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1222
uchar *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type,
1223
				   LEX_STRING *base)
unknown's avatar
unknown committed
1224 1225
{
  if (type == OPT_GLOBAL)
1226 1227
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1228 1229 1230
}


1231 1232
bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
{
1233
  ulonglong tmp= var->save_result.ulonglong_value;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

  /* Don't use bigger value than given with --maximum-variable-name=.. */
  if ((ha_rows) tmp > max_system_variables.*offset)
    tmp= max_system_variables.*offset;

  if (option_limits)
    tmp= (ha_rows) getopt_ull_limit_value(tmp, option_limits);
  if (var->type == OPT_GLOBAL)
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);    
    global_system_variables.*offset= (ha_rows) tmp;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= (ha_rows) tmp;
  return 0;
}


void sys_var_thd_ha_rows::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
    /* We will not come here if option_limits is not set */
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.*offset= (ha_rows) option_limits->def_value;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1268
uchar *sys_var_thd_ha_rows::value_ptr(THD *thd, enum_var_type type,
1269
				     LEX_STRING *base)
1270 1271
{
  if (type == OPT_GLOBAL)
1272 1273
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1274 1275
}

unknown's avatar
unknown committed
1276 1277
bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
1278
  ulonglong tmp= var->save_result.ulonglong_value;
1279

1280
  if (tmp > max_system_variables.*offset)
1281 1282 1283
    tmp= max_system_variables.*offset;

  if (option_limits)
1284
    tmp= getopt_ull_limit_value(tmp, option_limits);
unknown's avatar
unknown committed
1285
  if (var->type == OPT_GLOBAL)
1286 1287 1288
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);
1289
    global_system_variables.*offset= (ulonglong) tmp;
1290 1291
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1292
  else
1293
    thd->variables.*offset= (ulonglong) tmp;
unknown's avatar
unknown committed
1294 1295 1296 1297 1298 1299 1300
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
1301 1302
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
1303
    global_system_variables.*offset= (ulonglong) option_limits->def_value;
1304 1305
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1306 1307 1308 1309 1310
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1311
uchar *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type,
1312
				       LEX_STRING *base)
unknown's avatar
unknown committed
1313 1314
{
  if (type == OPT_GLOBAL)
1315 1316
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
}


bool sys_var_thd_bool::update(THD *thd,  set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.*offset= (my_bool) var->save_result.ulong_value;
  else
    thd->variables.*offset= (my_bool) var->save_result.ulong_value;
  return 0;
}


void sys_var_thd_bool::set_default(THD *thd,  enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= (my_bool) option_limits->def_value;
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1339
uchar *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type,
1340
				  LEX_STRING *base)
unknown's avatar
unknown committed
1341 1342
{
  if (type == OPT_GLOBAL)
1343 1344
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1345 1346 1347
}


1348
bool sys_var::check_enum(THD *thd, set_var *var, const TYPELIB *enum_names)
unknown's avatar
unknown committed
1349
{
unknown's avatar
unknown committed
1350
  char buff[STRING_BUFFER_USUAL_SIZE];
1351
  const char *value;
1352
  String str(buff, sizeof(buff), system_charset_info), *res;
unknown's avatar
unknown committed
1353 1354 1355 1356 1357

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
1358 1359
		 (ulong) find_type(enum_names, res->ptr(),
				   res->length(),1)-1)) < 0)
unknown's avatar
unknown committed
1360
    {
1361
      value= res ? res->c_ptr() : "NULL";
unknown's avatar
unknown committed
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
      goto err;
    }
  }
  else
  {
    ulonglong tmp=var->value->val_int();
    if (tmp >= enum_names->count)
    {
      llstr(tmp,buff);
      value=buff;				// Wrong value is here
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;	// Save for update
  }
  return 0;

err:
1379
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, value);
unknown's avatar
unknown committed
1380 1381 1382
  return 1;
}

1383 1384 1385

bool sys_var::check_set(THD *thd, set_var *var, TYPELIB *enum_names)
{
1386
  bool not_used;
unknown's avatar
unknown committed
1387
  char buff[STRING_BUFFER_USUAL_SIZE], *error= 0;
1388 1389 1390 1391 1392 1393
  uint error_len= 0;
  String str(buff, sizeof(buff), system_charset_info), *res;

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res= var->value->val_str(&str)))
1394
    {
unknown's avatar
unknown committed
1395
      strmov(buff, "NULL");
1396
      goto err;
1397
    }
unknown's avatar
unknown committed
1398 1399
    var->save_result.ulong_value= ((ulong)
				   find_set(enum_names, res->c_ptr(),
1400 1401 1402
					    res->length(),
                                            NULL,
                                            &error, &error_len,
unknown's avatar
unknown committed
1403
					    &not_used));
1404 1405 1406 1407 1408 1409 1410 1411 1412
    if (error_len)
    {
      strmake(buff, error, min(sizeof(buff), error_len));
      goto err;
    }
  }
  else
  {
    ulonglong tmp= var->value->val_int();
1413 1414 1415 1416 1417 1418
   /*
     For when the enum is made to contain 64 elements, as 1ULL<<64 is
     undefined, we guard with a "count<64" test.
   */
    if (unlikely((tmp >= ((ULL(1)) << enum_names->count)) &&
                 (enum_names->count < 64)))
1419 1420 1421 1422 1423 1424 1425 1426 1427
    {
      llstr(tmp, buff);
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;  // Save for update
  }
  return 0;

err:
1428
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buff);
1429 1430 1431 1432
  return 1;
}


unknown's avatar
unknown committed
1433 1434 1435 1436 1437
/*
  Return an Item for a variable.  Used with @@[global.]variable_name
  If type is not given, return local value if exists, else global
*/

1438
Item *sys_var::item(THD *thd, enum_var_type var_type, LEX_STRING *base)
unknown's avatar
unknown committed
1439 1440 1441 1442 1443
{
  if (check_type(var_type))
  {
    if (var_type != OPT_DEFAULT)
    {
1444 1445
      my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0),
               name, var_type == OPT_GLOBAL ? "SESSION" : "GLOBAL");
unknown's avatar
unknown committed
1446 1447 1448 1449 1450
      return 0;
    }
    /* As there was no local variable, return the global value */
    var_type= OPT_GLOBAL;
  }
1451
  switch (show_type()) {
1452 1453 1454 1455 1456 1457
  case SHOW_INT:
  {
    uint value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(uint*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
1458
    return new Item_uint((ulonglong) value);
1459
  }
unknown's avatar
unknown committed
1460
  case SHOW_LONG:
1461 1462 1463 1464 1465
  {
    ulong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(ulong*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
1466
    return new Item_uint((ulonglong) value);
1467
  }
unknown's avatar
unknown committed
1468
  case SHOW_LONGLONG:
1469 1470 1471
  {
    longlong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1472
    value= *(longlong*) value_ptr(thd, var_type, base);
1473 1474 1475
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int(value);
  }
1476
  case SHOW_HA_ROWS:
1477 1478 1479
  {
    ha_rows value;
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1480
    value= *(ha_rows*) value_ptr(thd, var_type, base);
1481 1482 1483
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int((longlong) value);
  }
unknown's avatar
unknown committed
1484
  case SHOW_MY_BOOL:
unknown's avatar
unknown committed
1485 1486 1487 1488 1489 1490 1491
  {
    int32 value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(my_bool*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int(value,1);
  }
unknown's avatar
unknown committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
  case SHOW_CHAR_PTR:
  {
    Item *tmp;
    pthread_mutex_lock(&LOCK_global_system_variables);
    char *str= *(char**) value_ptr(thd, var_type, base);
    if (str)
    {
      uint length= strlen(str);
      tmp= new Item_string(thd->strmake(str, length), length,
                           system_charset_info, DERIVATION_SYSCONST);
    }
    else
    {
      tmp= new Item_null();
      tmp->collation.set(system_charset_info, DERIVATION_SYSCONST);
    }
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return tmp;
  }
unknown's avatar
unknown committed
1511 1512
  case SHOW_CHAR:
  {
unknown's avatar
unknown committed
1513
    Item *tmp;
1514
    pthread_mutex_lock(&LOCK_global_system_variables);
1515
    char *str= (char*) value_ptr(thd, var_type, base);
unknown's avatar
unknown committed
1516 1517 1518 1519 1520 1521 1522 1523
    if (str)
      tmp= new Item_string(str, strlen(str),
                           system_charset_info, DERIVATION_SYSCONST);
    else
    {
      tmp= new Item_null();
      tmp->collation.set(system_charset_info, DERIVATION_SYSCONST);
    }
1524 1525
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return tmp;
unknown's avatar
unknown committed
1526 1527
  }
  default:
1528
    my_error(ER_VAR_CANT_BE_READ, MYF(0), name);
unknown's avatar
unknown committed
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
  }
  return 0;
}


bool sys_var_thd_enum::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.*offset= var->save_result.ulong_value;
  else
    thd->variables.*offset= var->save_result.ulong_value;
  return 0;
}


void sys_var_thd_enum::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= (ulong) option_limits->def_value;
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1553
uchar *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type,
1554
				  LEX_STRING *base)
unknown's avatar
unknown committed
1555 1556 1557 1558
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
1559
  return (uchar*) enum_names->type_names[tmp];
unknown's avatar
unknown committed
1560 1561
}

1562 1563 1564 1565 1566
bool sys_var_thd_bit::check(THD *thd, set_var *var)
{
  return (check_enum(thd, var, &bool_typelib) ||
          (check_func && (*check_func)(thd, var)));
}
unknown's avatar
unknown committed
1567 1568 1569

bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
1570
  int res= (*update_func)(thd, var);
unknown's avatar
unknown committed
1571 1572 1573 1574
  return res;
}


1575
uchar *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type,
1576
				 LEX_STRING *base)
unknown's avatar
unknown committed
1577 1578 1579 1580 1581 1582 1583
{
  /*
    If reverse is 0 (default) return 1 if bit is set.
    If reverse is 1, return 0 if bit is set
  */
  thd->sys_var_tmp.my_bool_value= ((thd->options & bit_flag) ?
				   !reverse : reverse);
1584
  return (uchar*) &thd->sys_var_tmp.my_bool_value;
unknown's avatar
unknown committed
1585 1586 1587
}


1588 1589 1590 1591 1592 1593 1594
/* Update a date_time format variable based on given value */

void sys_var_thd_date_time_format::update2(THD *thd, enum_var_type type,
					   DATE_TIME_FORMAT *new_value)
{
  DATE_TIME_FORMAT *old;
  DBUG_ENTER("sys_var_date_time_format::update2");
1595
  DBUG_DUMP("positions", (uchar*) new_value->positions,
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
	    sizeof(new_value->positions));

  if (type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    old= (global_system_variables.*offset);
    (global_system_variables.*offset)= new_value;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
  {
    old= (thd->variables.*offset);
    (thd->variables.*offset)= new_value;
  }
  my_free((char*) old, MYF(MY_ALLOW_ZERO_PTR));
  DBUG_VOID_RETURN;
}


bool sys_var_thd_date_time_format::update(THD *thd, set_var *var)
{
  DATE_TIME_FORMAT *new_value;
  /* We must make a copy of the last value to get it into normal memory */
  new_value= date_time_format_copy((THD*) 0,
				   var->save_result.date_time_format);
  if (!new_value)
    return 1;					// Out of memory
  update2(thd, var->type, new_value);		// Can't fail
  return 0;
}


bool sys_var_thd_date_time_format::check(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
1630
  char buff[STRING_BUFFER_USUAL_SIZE];
1631 1632 1633 1634 1635 1636 1637 1638 1639
  String str(buff,sizeof(buff), system_charset_info), *res;
  DATE_TIME_FORMAT *format;

  if (!(res=var->value->val_str(&str)))
    res= &my_empty_string;

  if (!(format= date_time_format_make(date_time_type,
				      res->ptr(), res->length())))
  {
1640
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, res->c_ptr());
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
    return 1;
  }
  
  /*
    We must copy result to thread space to not get a memory leak if
    update is aborted
  */
  var->save_result.date_time_format= date_time_format_copy(thd, format);
  my_free((char*) format, MYF(0));
  return var->save_result.date_time_format == 0;
}


void sys_var_thd_date_time_format::set_default(THD *thd, enum_var_type type)
{
  DATE_TIME_FORMAT *res= 0;

  if (type == OPT_GLOBAL)
  {
    const char *format;
    if ((format= opt_date_time_formats[date_time_type]))
      res= date_time_format_make(date_time_type, format, strlen(format));
  }
  else
  {
    /* Make copy with malloc */
    res= date_time_format_copy((THD *) 0, global_system_variables.*offset);
  }

  if (res)					// Should always be true
    update2(thd, type, res);
}


1675
uchar *sys_var_thd_date_time_format::value_ptr(THD *thd, enum_var_type type,
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
					      LEX_STRING *base)
{
  if (type == OPT_GLOBAL)
  {
    char *res;
    /*
      We do a copy here just to be sure things will work even if someone
      is modifying the original string while the copy is accessed
      (Can't happen now in SQL SHOW, but this is a good safety for the future)
    */
    res= thd->strmake((global_system_variables.*offset)->format.str,
		      (global_system_variables.*offset)->format.length);
1688
    return (uchar*) res;
1689
  }
1690
  return (uchar*) (thd->variables.*offset)->format.str;
1691 1692 1693
}


1694 1695
typedef struct old_names_map_st
{
unknown's avatar
unknown committed
1696 1697 1698
  const char *old_name;
  const char *new_name;
} my_old_conv;
unknown's avatar
unknown committed
1699

unknown's avatar
unknown committed
1700 1701
static my_old_conv old_conv[]= 
{
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
  {	"cp1251_koi8"		,	"cp1251"	},
  {	"cp1250_latin2"		,	"cp1250"	},
  {	"kam_latin2"		,	"keybcs2"	},
  {	"mac_latin2"		,	"MacRoman"	},
  {	"macce_latin2"		,	"MacCE"		},
  {	"pc2_latin2"		,	"pclatin2"	},
  {	"vga_latin2"		,	"pclatin1"	},
  {	"koi8_cp1251"		,	"koi8r"		},
  {	"win1251ukr_koi8_ukr"	,	"win1251ukr"	},
  {	"koi8_ukr_win1251ukr"	,	"koi8u"		},
  {	NULL			,	NULL		}
unknown's avatar
unknown committed
1713
};
unknown's avatar
unknown committed
1714

unknown's avatar
unknown committed
1715
CHARSET_INFO *get_old_charset_by_name(const char *name)
unknown's avatar
unknown committed
1716
{
unknown's avatar
unknown committed
1717
  my_old_conv *conv;
unknown's avatar
unknown committed
1718
 
unknown's avatar
unknown committed
1719
  for (conv= old_conv; conv->old_name; conv++)
1720
  {
unknown's avatar
unknown committed
1721 1722
    if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
      return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
1723
  }
unknown's avatar
unknown committed
1724
  return NULL;
unknown's avatar
unknown committed
1725 1726
}

unknown's avatar
unknown committed
1727

1728
bool sys_var_collation::check(THD *thd, set_var *var)
unknown's avatar
unknown committed
1729 1730
{
  CHARSET_INFO *tmp;
1731
  LINT_INIT(tmp);
unknown's avatar
unknown committed
1732

1733
  if (var->value->result_type() == STRING_RESULT)
1734
  {
unknown's avatar
unknown committed
1735
    char buff[STRING_BUFFER_USUAL_SIZE];
1736 1737 1738
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
    {
1739
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
1740 1741 1742 1743
      return 1;
    }
    if (!(tmp=get_charset_by_name(res->c_ptr(),MYF(0))))
    {
1744
      my_error(ER_UNKNOWN_COLLATION, MYF(0), res->c_ptr());
1745 1746
      return 1;
    }
1747
  }
1748
  else // INT_RESULT
1749
  {
1750
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
1751 1752
    {
      char buf[20];
1753
      int10_to_str((int) var->value->val_int(), buf, -10);
1754
      my_error(ER_UNKNOWN_COLLATION, MYF(0), buf);
1755 1756
      return 1;
    }
1757 1758 1759 1760 1761
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

1762

1763 1764 1765
bool sys_var_character_set::check(THD *thd, set_var *var)
{
  CHARSET_INFO *tmp;
1766
  LINT_INIT(tmp);
1767

1768 1769
  if (var->value->result_type() == STRING_RESULT)
  {
unknown's avatar
unknown committed
1770
    char buff[STRING_BUFFER_USUAL_SIZE];
1771 1772
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
1773
    {
1774 1775
      if (!nullable)
      {
1776
        my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
1777 1778 1779 1780 1781 1782 1783
        return 1;
      }
      tmp= NULL;
    }
    else if (!(tmp=get_charset_by_csname(res->c_ptr(),MY_CS_PRIMARY,MYF(0))) &&
             !(tmp=get_old_charset_by_name(res->c_ptr())))
    {
1784
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), res->c_ptr());
1785 1786 1787
      return 1;
    }
  }
1788
  else // INT_RESULT
unknown's avatar
unknown committed
1789
  {
1790
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
1791 1792
    {
      char buf[20];
1793
      int10_to_str((int) var->value->val_int(), buf, -10);
1794 1795 1796
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), buf);
      return 1;
    }
unknown's avatar
unknown committed
1797 1798 1799 1800 1801
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

1802

1803
bool sys_var_character_set::update(THD *thd, set_var *var)
unknown's avatar
unknown committed
1804
{
1805
  ci_ptr(thd,var->type)[0]= var->save_result.charset;
unknown's avatar
unknown committed
1806
  thd->update_charset();
unknown's avatar
unknown committed
1807 1808 1809
  return 0;
}

1810

1811
uchar *sys_var_character_set::value_ptr(THD *thd, enum_var_type type,
1812
				       LEX_STRING *base)
unknown's avatar
unknown committed
1813
{
1814
  CHARSET_INFO *cs= ci_ptr(thd,type)[0];
1815
  return cs ? (uchar*) cs->csname : (uchar*) NULL;
unknown's avatar
unknown committed
1816 1817
}

1818

unknown's avatar
unknown committed
1819
void sys_var_character_set_sv::set_default(THD *thd, enum_var_type type)
unknown's avatar
unknown committed
1820 1821
{
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
1822
    global_system_variables.*offset= *global_default;
unknown's avatar
unknown committed
1823
  else
unknown's avatar
unknown committed
1824 1825 1826 1827
  {
    thd->variables.*offset= global_system_variables.*offset;
    thd->update_charset();
  }
1828
}
unknown's avatar
unknown committed
1829
CHARSET_INFO **sys_var_character_set_sv::ci_ptr(THD *thd, enum_var_type type)
1830 1831
{
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
1832
    return &(global_system_variables.*offset);
1833
  else
unknown's avatar
unknown committed
1834
    return &(thd->variables.*offset);
1835 1836
}

1837 1838 1839

CHARSET_INFO ** sys_var_character_set_database::ci_ptr(THD *thd,
						       enum_var_type type)
1840 1841
{
  if (type == OPT_GLOBAL)
1842
    return &global_system_variables.collation_database;
1843
  else
1844
    return &thd->variables.collation_database;
1845 1846
}

1847

1848 1849 1850
void sys_var_character_set_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
1851
    global_system_variables.collation_database= default_charset_info;
1852
  else
unknown's avatar
unknown committed
1853
  {
1854
    thd->variables.collation_database= thd->db_charset;
unknown's avatar
unknown committed
1855 1856
    thd->update_charset();
  }
1857 1858
}

1859

unknown's avatar
unknown committed
1860
bool sys_var_collation_sv::update(THD *thd, set_var *var)
1861 1862
{
  if (var->type == OPT_GLOBAL)
unknown's avatar
unknown committed
1863
    global_system_variables.*offset= var->save_result.charset;
1864
  else
unknown's avatar
unknown committed
1865
  {
unknown's avatar
unknown committed
1866
    thd->variables.*offset= var->save_result.charset;
unknown's avatar
unknown committed
1867 1868
    thd->update_charset();
  }
1869 1870 1871
  return 0;
}

1872

unknown's avatar
unknown committed
1873
void sys_var_collation_sv::set_default(THD *thd, enum_var_type type)
1874
{
unknown's avatar
unknown committed
1875 1876
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= *global_default;
1877 1878
  else
  {
unknown's avatar
unknown committed
1879
    thd->variables.*offset= global_system_variables.*offset;
1880 1881 1882 1883 1884
    thd->update_charset();
  }
}


1885
uchar *sys_var_collation_sv::value_ptr(THD *thd, enum_var_type type,
1886
                                       LEX_STRING *base)
1887 1888
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
1889
		     global_system_variables.*offset : thd->variables.*offset);
1890
  return cs ? (uchar*) cs->name : (uchar*) "NULL";
1891 1892 1893
}


1894
LEX_STRING default_key_cache_base= {(char *) "default", 7 };
unknown's avatar
unknown committed
1895

unknown's avatar
unknown committed
1896
static KEY_CACHE zero_key_cache;
unknown's avatar
unknown committed
1897

unknown's avatar
unknown committed
1898
KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
unknown's avatar
unknown committed
1899
{
1900 1901
  safe_mutex_assert_owner(&LOCK_global_system_variables);
  if (!cache_name || ! cache_name->length)
unknown's avatar
unknown committed
1902
    cache_name= &default_key_cache_base;
unknown's avatar
unknown committed
1903
  return ((KEY_CACHE*) find_named(&key_caches,
1904
                                      cache_name->str, cache_name->length, 0));
unknown's avatar
unknown committed
1905 1906
}

1907

1908
uchar *sys_var_key_cache_param::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
1909
					 LEX_STRING *base)
1910
{
unknown's avatar
unknown committed
1911
  KEY_CACHE *key_cache= get_key_cache(base);
unknown's avatar
unknown committed
1912 1913
  if (!key_cache)
    key_cache= &zero_key_cache;
1914
  return (uchar*) key_cache + offset ;
unknown's avatar
unknown committed
1915
}
1916

1917

1918 1919
bool sys_var_key_buffer_size::update(THD *thd, set_var *var)
{
1920
  ulonglong tmp= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
1921
  LEX_STRING *base_name= &var->base;
unknown's avatar
unknown committed
1922
  KEY_CACHE *key_cache;
1923 1924 1925
  bool error= 0;

  /* If no basename, assume it's for the key cache named 'default' */
unknown's avatar
unknown committed
1926
  if (!base_name->length)
1927
    base_name= &default_key_cache_base;
1928 1929 1930

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache= get_key_cache(base_name);
unknown's avatar
unknown committed
1931
                            
1932 1933
  if (!key_cache)
  {
1934
    /* Key cache didn't exists */
1935
    if (!tmp)					// Tried to delete cache
1936 1937 1938 1939 1940 1941
      goto end;					// Ok, nothing to do
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
    {
      error= 1;
      goto end;
    }
1942
  }
1943 1944 1945 1946 1947 1948 1949 1950 1951

  /*
    Abort if some other thread is changing the key cache
    TODO: This should be changed so that we wait until the previous
    assignment is done and then do the new assign
  */
  if (key_cache->in_init)
    goto end;

unknown's avatar
unknown committed
1952
  if (!tmp)					// Zero size means delete
1953
  {
unknown's avatar
unknown committed
1954
    if (key_cache == dflt_key_cache)
1955 1956 1957 1958
    {
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                          ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
                          ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
1959
      goto end;					// Ignore default key cache
1960
    }
1961

unknown's avatar
unknown committed
1962
    if (key_cache->key_cache_inited)		// If initied
1963 1964
    {
      /*
1965 1966
	Move tables using this key cache to the default key cache
	and clear the old key cache.
1967
      */
1968
      NAMED_LIST *list; 
unknown's avatar
unknown committed
1969
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
1970
					      base_name->length, &list);
1971 1972
      key_cache->in_init= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1973
      error= reassign_keycache_tables(thd, key_cache, dflt_key_cache);
1974 1975
      pthread_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init= 0;
1976
    }
1977 1978 1979 1980 1981
    /*
      We don't delete the key cache as some running threads my still be
      in the key cache code with a pointer to the deleted (empty) key cache
    */
    goto end;
1982 1983
  }

unknown's avatar
unknown committed
1984 1985
  key_cache->param_buff_size=
    (ulonglong) getopt_ull_limit_value(tmp, option_limits);
1986

1987 1988 1989 1990
  /* If key cache didn't existed initialize it, else resize it */
  key_cache->in_init= 1;
  pthread_mutex_unlock(&LOCK_global_system_variables);

unknown's avatar
unknown committed
1991
  if (!key_cache->key_cache_inited)
1992
    error= (bool) (ha_init_key_cache("", key_cache));
unknown's avatar
unknown committed
1993
  else
1994
    error= (bool)(ha_resize_key_cache(key_cache));
1995

1996 1997
  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init= 0;  
1998

1999 2000 2001
end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2002 2003
}

2004 2005

bool sys_var_key_cache_long::update(THD *thd, set_var *var)
2006
{
unknown's avatar
unknown committed
2007
  ulong tmp= (ulong) var->value->val_int();
2008
  LEX_STRING *base_name= &var->base;
2009 2010
  bool error= 0;

2011
  if (!base_name->length)
unknown's avatar
unknown committed
2012
    base_name= &default_key_cache_base;
2013 2014

  pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
2015
  KEY_CACHE *key_cache= get_key_cache(base_name);
2016

unknown's avatar
unknown committed
2017 2018
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
				                  base_name->length)))
2019 2020 2021 2022
  {
    error= 1;
    goto end;
  }
2023

2024 2025 2026 2027 2028 2029 2030 2031 2032
  /*
    Abort if some other thread is changing the key cache
    TODO: This should be changed so that we wait until the previous
    assignment is done and then do the new assign
  */
  if (key_cache->in_init)
    goto end;

  *((ulong*) (((char*) key_cache) + offset))=
2033
    (ulong) getopt_ull_limit_value(tmp, option_limits);
unknown's avatar
unknown committed
2034

2035 2036 2037 2038 2039
  /*
    Don't create a new key cache if it didn't exist
    (key_caches are created only when the user sets block_size)
  */
  key_cache->in_init= 1;
2040

2041
  pthread_mutex_unlock(&LOCK_global_system_variables);
2042

2043 2044 2045 2046 2047 2048 2049 2050
  error= (bool) (ha_resize_key_cache(key_cache));

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init= 0;  

end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2051 2052
}

2053

2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
bool sys_var_log_state::update(THD *thd, set_var *var)
{
  bool res= 0;
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!var->save_result.ulong_value)
    logger.deactivate_log_handler(thd, log_type);
  else
  {
    if ((res= logger.activate_log_handler(thd, log_type)))
    {
      my_error(ER_CANT_ACTIVATE_LOG, MYF(0),
               log_type == QUERY_LOG_GENERAL ? "general" :
               "slow query");
      goto err;
    }
  }
err:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return res;
}

void sys_var_log_state::set_default(THD *thd, enum_var_type type)
{
  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.deactivate_log_handler(thd, log_type);
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


static int  sys_check_log_path(THD *thd,  set_var *var)
{
  char path[FN_REFLEN];
  MY_STAT f_stat;
  const char *var_path= var->value->str_value.ptr();
  bzero(&f_stat, sizeof(MY_STAT));

  (void) unpack_filename(path, var_path);
  if (my_stat(path, &f_stat, MYF(0)))
  {
    /* Check if argument is a file and we have 'write' permission */
    if (!MY_S_ISREG(f_stat.st_mode) ||
        !(f_stat.st_mode & MY_S_IWRITE))
      return -1;
  }
  else
  {
2100
    size_t path_length;
2101 2102 2103 2104
    /*
      Check if directory exists and 
      we have permission to create file & write to file
    */
2105
    (void) dirname_part(path, var_path, &path_length);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
    if (my_access(path, (F_OK|W_OK)))
      return -1;
  }
  return 0;
}


bool update_sys_var_str_path(THD *thd, sys_var_str *var_str,
			     set_var *var, const char *log_ext,
			     bool log_state, uint log_type)
{
unknown's avatar
unknown committed
2117
  MYSQL_QUERY_LOG *file_log;
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
  char buff[FN_REFLEN];
  char *res= 0, *old_value=(char *)(var ? var->value->str_value.ptr() : 0);
  bool result= 0;
  uint str_length= (var ? var->value->str_value.length() : 0);

  switch (log_type) {
  case QUERY_LOG_SLOW:
    file_log= logger.get_slow_log_file_handler();
    break;
  case QUERY_LOG_GENERAL:
    file_log= logger.get_log_file_handler();
    break;
  default:
unknown's avatar
unknown committed
2131
    assert(0);                                  // Impossible
2132 2133 2134 2135 2136 2137 2138
  }

  if (!old_value)
  {
    old_value= make_default_log_name(buff, log_ext);
    str_length= strlen(old_value);
  }
2139
  if (!(res= my_strndup(old_value, str_length, MYF(MY_FAE+MY_WME))))
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
  {
    result= 1;
    goto err;
  }

  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.lock();

  if (file_log && log_state)
    file_log->close(0);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= str_length;
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  if (file_log && log_state)
unknown's avatar
unknown committed
2155 2156 2157
  {
    switch (log_type) {
    case QUERY_LOG_SLOW:
2158
      file_log->open_slow_log(sys_var_slow_log_path.value);
unknown's avatar
unknown committed
2159 2160 2161 2162 2163 2164 2165 2166
      break;
    case QUERY_LOG_GENERAL:
      file_log->open_query_log(sys_var_general_log_path.value);
      break;
    default:
      DBUG_ASSERT(0);
    }
  }
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230

  logger.unlock();
  pthread_mutex_unlock(&LOCK_global_system_variables);

err:
  return result;
}


static bool sys_update_general_log_path(THD *thd, set_var * var)
{
  return update_sys_var_str_path(thd, &sys_var_general_log_path, 
				 var, ".log", opt_log, QUERY_LOG_GENERAL);
}


static void sys_default_general_log_path(THD *thd, enum_var_type type)
{
  (void) update_sys_var_str_path(thd, &sys_var_general_log_path,
				 0, ".log", opt_log, QUERY_LOG_GENERAL);
}


static bool sys_update_slow_log_path(THD *thd, set_var * var)
{
  return update_sys_var_str_path(thd, &sys_var_slow_log_path,
				 var, "-slow.log", opt_slow_log,
                                 QUERY_LOG_SLOW);
}


static void sys_default_slow_log_path(THD *thd, enum_var_type type)
{
  (void) update_sys_var_str_path(thd, &sys_var_slow_log_path,
				 0, "-slow.log", opt_slow_log,
                                 QUERY_LOG_SLOW);
}


bool sys_var_log_output::update(THD *thd, set_var *var)
{
  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.lock();
  logger.init_slow_log(var->save_result.ulong_value);
  logger.init_general_log(var->save_result.ulong_value);
  *value= var->save_result.ulong_value;
  logger.unlock();
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return 0;
}


void sys_var_log_output::set_default(THD *thd, enum_var_type type)
{
  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.lock();
  logger.init_slow_log(LOG_TABLE);
  logger.init_general_log(LOG_TABLE);
  *value= LOG_TABLE;
  logger.unlock();
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


2231
uchar *sys_var_log_output::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
2232
                                    LEX_STRING *base)
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
{
  char buff[256];
  String tmp(buff, sizeof(buff), &my_charset_latin1);
  ulong length;
  ulong val= *value;

  tmp.length(0);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
      tmp.append(log_output_typelib.type_names[i],
                 log_output_typelib.type_lengths[i]);
      tmp.append(',');
    }
  }

  if ((length= tmp.length()))
    length--;
2252
  return (uchar*) thd->strmake(tmp.ptr(), length);
2253 2254 2255
}


2256 2257 2258 2259
/*****************************************************************************
  Functions to handle SET NAMES and SET CHARACTER SET
*****************************************************************************/

unknown's avatar
unknown committed
2260
int set_var_collation_client::check(THD *thd)
2261 2262 2263 2264
{
  return 0;
}

unknown's avatar
unknown committed
2265
int set_var_collation_client::update(THD *thd)
2266
{
2267 2268
  thd->variables.character_set_client= character_set_client;
  thd->variables.character_set_results= character_set_results;
unknown's avatar
unknown committed
2269
  thd->variables.collation_connection= collation_connection;
unknown's avatar
unknown committed
2270
  thd->update_charset();
unknown's avatar
unknown committed
2271 2272
  thd->protocol_text.init(thd);
  thd->protocol_binary.init(thd);
2273 2274 2275 2276 2277
  return 0;
}

/****************************************************************************/

unknown's avatar
unknown committed
2278 2279
bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
2280
  thd->set_time((time_t) var->save_result.ulonglong_value);
unknown's avatar
unknown committed
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
  return 0;
}


void sys_var_timestamp::set_default(THD *thd, enum_var_type type)
{
  thd->user_time=0;
}


2291
uchar *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type,
2292
				   LEX_STRING *base)
unknown's avatar
unknown committed
2293 2294
{
  thd->sys_var_tmp.long_value= (long) thd->start_time;
2295
  return (uchar*) &thd->sys_var_tmp.long_value;
unknown's avatar
unknown committed
2296 2297 2298 2299 2300
}


bool sys_var_last_insert_id::update(THD *thd, set_var *var)
{
2301 2302
  thd->first_successful_insert_id_in_prev_stmt= 
    var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2303 2304 2305 2306
  return 0;
}


2307
uchar *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type,
2308
					LEX_STRING *base)
unknown's avatar
unknown committed
2309
{
2310 2311 2312 2313 2314 2315
  /*
    this tmp var makes it robust againt change of type of 
    read_first_successful_insert_id_in_prev_stmt().
  */
  thd->sys_var_tmp.ulonglong_value= 
    thd->read_first_successful_insert_id_in_prev_stmt();
2316
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
unknown's avatar
unknown committed
2317 2318 2319 2320 2321
}


bool sys_var_insert_id::update(THD *thd, set_var *var)
{
2322
  thd->force_one_auto_inc_interval(var->save_result.ulonglong_value);
unknown's avatar
unknown committed
2323 2324 2325 2326
  return 0;
}


2327
uchar *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type,
2328
				   LEX_STRING *base)
unknown's avatar
unknown committed
2329
{
2330 2331
  thd->sys_var_tmp.ulonglong_value= 
    thd->auto_inc_intervals_forced.minimum();
2332
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
unknown's avatar
unknown committed
2333 2334 2335
}


2336 2337
bool sys_var_rand_seed1::update(THD *thd, set_var *var)
{
2338
  thd->rand.seed1= (ulong) var->save_result.ulonglong_value;
2339 2340 2341 2342 2343
  return 0;
}

bool sys_var_rand_seed2::update(THD *thd, set_var *var)
{
2344
  thd->rand.seed2= (ulong) var->save_result.ulonglong_value;
2345 2346 2347 2348
  return 0;
}


2349 2350
bool sys_var_thd_time_zone::check(THD *thd, set_var *var)
{
2351
  char buff[MAX_TIME_ZONE_NAME_LENGTH];
2352 2353 2354
  String str(buff, sizeof(buff), &my_charset_latin1);
  String *res= var->value->val_str(&str);

2355
  if (!(var->save_result.time_zone= my_tz_find(thd, res)))
2356
  {
2357
    my_error(ER_UNKNOWN_TIME_ZONE, MYF(0), res ? res->c_ptr() : "NULL");
2358 2359 2360 2361 2362 2363 2364 2365
    return 1;
  }
  return 0;
}


bool sys_var_thd_time_zone::update(THD *thd, set_var *var)
{
2366 2367 2368 2369 2370 2371 2372 2373 2374
  /* We are using Time_zone object found during check() phase. */
  if (var->type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.time_zone= var->save_result.time_zone;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.time_zone= var->save_result.time_zone;
2375 2376 2377 2378
  return 0;
}


2379
uchar *sys_var_thd_time_zone::value_ptr(THD *thd, enum_var_type type,
2380 2381 2382 2383 2384 2385 2386
				       LEX_STRING *base)
{
  /* 
    We can use ptr() instead of c_ptr() here because String contaning
    time zone name is guaranteed to be zero ended.
  */
  if (type == OPT_GLOBAL)
2387
    return (uchar *)(global_system_variables.time_zone->get_name()->ptr());
2388
  else
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
  {
    /*
      This is an ugly fix for replication: we don't replicate properly queries
      invoking system variables' values to update tables; but
      CONVERT_TZ(,,@@session.time_zone) is so popular that we make it
      replicable (i.e. we tell the binlog code to store the session
      timezone). If it's the global value which was used we can't replicate
      (binlog code stores session value only).
    */
    thd->time_zone_used= 1;
2399
    return (uchar *)(thd->variables.time_zone->get_name()->ptr());
2400
  }
2401 2402 2403 2404 2405
}


void sys_var_thd_time_zone::set_default(THD *thd, enum_var_type type)
{
2406
 pthread_mutex_lock(&LOCK_global_system_variables);
2407 2408 2409 2410 2411
 if (type == OPT_GLOBAL)
 {
   if (default_tz_name)
   {
     String str(default_tz_name, &my_charset_latin1);
2412 2413 2414 2415
     /*
       We are guaranteed to find this time zone since its existence
       is checked during start-up.
     */
2416
     global_system_variables.time_zone= my_tz_find(thd, &str);
2417 2418 2419 2420 2421 2422
   }
   else
     global_system_variables.time_zone= my_tz_SYSTEM;
 }
 else
   thd->variables.time_zone= global_system_variables.time_zone;
2423
 pthread_mutex_unlock(&LOCK_global_system_variables);
2424 2425
}

2426 2427 2428 2429 2430 2431 2432 2433 2434

bool sys_var_max_user_conn::check(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    return sys_var_thd::check(thd, var);
  else
  {
    /*
      Per-session values of max_user_connections can't be set directly.
2435
      May be we should have a separate error message for this?
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
    */
    my_error(ER_GLOBAL_VARIABLE, MYF(0), name);
    return TRUE;
  }
}

bool sys_var_max_user_conn::update(THD *thd, set_var *var)
{
  DBUG_ASSERT(var->type == OPT_GLOBAL);
  pthread_mutex_lock(&LOCK_global_system_variables);
2446
  max_user_connections= (uint)var->save_result.ulonglong_value;
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return 0;
}


void sys_var_max_user_conn::set_default(THD *thd, enum_var_type type)
{
  DBUG_ASSERT(type == OPT_GLOBAL);
  pthread_mutex_lock(&LOCK_global_system_variables);
  max_user_connections= (ulong) option_limits->def_value;
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


2461
uchar *sys_var_max_user_conn::value_ptr(THD *thd, enum_var_type type,
2462 2463 2464 2465
                                       LEX_STRING *base)
{
  if (type != OPT_GLOBAL &&
      thd->user_connect && thd->user_connect->user_resources.user_conn)
2466 2467
    return (uchar*) &(thd->user_connect->user_resources.user_conn);
  return (uchar*) &(max_user_connections);
2468 2469
}

2470

unknown's avatar
unknown committed
2471 2472
bool sys_var_thd_lc_time_names::check(THD *thd, set_var *var)
{
2473
  MY_LOCALE *locale_match;
unknown's avatar
unknown committed
2474

2475
  if (var->value->result_type() == INT_RESULT)
unknown's avatar
unknown committed
2476
  {
2477 2478 2479 2480 2481 2482 2483
    if (!(locale_match= my_locale_by_number((uint) var->value->val_int())))
    {
      char buf[20];
      int10_to_str((int) var->value->val_int(), buf, -10);
      my_printf_error(ER_UNKNOWN_ERROR, "Unknown locale: '%s'", MYF(0), buf);
      return 1;
    }
unknown's avatar
unknown committed
2484
  }
2485
  else // STRING_RESULT
unknown's avatar
unknown committed
2486
  {
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
    char buff[6]; 
    String str(buff, sizeof(buff), &my_charset_latin1), *res;
    if (!(res=var->value->val_str(&str)))
    {
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
      return 1;
    }
    const char *locale_str= res->c_ptr();
    if (!(locale_match= my_locale_by_name(locale_str)))
    {
      my_printf_error(ER_UNKNOWN_ERROR,
                      "Unknown locale: '%s'", MYF(0), locale_str);
      return 1;
    }
unknown's avatar
unknown committed
2501
  }
2502

2503 2504
  var->save_result.locale_value= locale_match;
  return 0;
unknown's avatar
unknown committed
2505 2506 2507 2508 2509
}


bool sys_var_thd_lc_time_names::update(THD *thd, set_var *var)
{
2510 2511 2512 2513
  if (var->type == OPT_GLOBAL)
    global_system_variables.lc_time_names= var->save_result.locale_value;
  else
    thd->variables.lc_time_names= var->save_result.locale_value;
unknown's avatar
unknown committed
2514 2515 2516 2517
  return 0;
}


2518
uchar *sys_var_thd_lc_time_names::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
2519 2520
					  LEX_STRING *base)
{
2521
  return type == OPT_GLOBAL ?
2522 2523
                 (uchar *) global_system_variables.lc_time_names->name :
                 (uchar *) thd->variables.lc_time_names->name;
unknown's avatar
unknown committed
2524 2525 2526 2527 2528
}


void sys_var_thd_lc_time_names::set_default(THD *thd, enum_var_type type)
{
2529 2530 2531 2532
  if (type == OPT_GLOBAL)
    global_system_variables.lc_time_names= my_default_lc_time_names;
  else
    thd->variables.lc_time_names= global_system_variables.lc_time_names;
unknown's avatar
unknown committed
2533
}
2534

unknown's avatar
unknown committed
2535 2536 2537 2538 2539 2540
/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
2541 2542 2543
  sys_var_thd_bit *sys_var= ((sys_var_thd_bit*) var->var);
  if ((var->save_result.ulong_value != 0) == sys_var->reverse)
    thd->options&= ~sys_var->bit_flag;
unknown's avatar
unknown committed
2544
  else
2545
    thd->options|= sys_var->bit_flag;
unknown's avatar
unknown committed
2546 2547 2548 2549 2550 2551 2552 2553
  return 0;
}


static bool set_option_autocommit(THD *thd, set_var *var)
{
  /* The test is negative as the flag we use is NOT autocommit */

2554
  ulonglong org_options= thd->options;
unknown's avatar
unknown committed
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565

  if (var->save_result.ulong_value != 0)
    thd->options&= ~((sys_var_thd_bit*) var->var)->bit_flag;
  else
    thd->options|= ((sys_var_thd_bit*) var->var)->bit_flag;

  if ((org_options ^ thd->options) & OPTION_NOT_AUTOCOMMIT)
  {
    if ((org_options & OPTION_NOT_AUTOCOMMIT))
    {
      /* We changed to auto_commit mode */
unknown's avatar
unknown committed
2566
      thd->options&= ~(ulonglong) (OPTION_BEGIN | OPTION_KEEP_LOG);
2567
      thd->no_trans_update.all= FALSE;
unknown's avatar
unknown committed
2568 2569 2570 2571 2572 2573
      thd->server_status|= SERVER_STATUS_AUTOCOMMIT;
      if (ha_commit(thd))
	return 1;
    }
    else
    {
2574
      thd->no_trans_update.all= FALSE;
unknown's avatar
unknown committed
2575 2576 2577 2578 2579 2580
      thd->server_status&= ~SERVER_STATUS_AUTOCOMMIT;
    }
  }
  return 0;
}

2581 2582 2583
static int check_log_update(THD *thd, set_var *var)
{
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2584
  if (!(thd->security_ctx->master_access & SUPER_ACL))
2585 2586 2587 2588 2589 2590 2591
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#endif
  return 0;
}
unknown's avatar
unknown committed
2592 2593 2594

static bool set_log_update(THD *thd, set_var *var)
{
2595 2596 2597 2598 2599
  /*
    The update log is not supported anymore since 5.0.
    See sql/mysqld.cc/, comments in function init_server_components() for an
    explaination of the different warnings we send below
  */
2600

unknown's avatar
unknown committed
2601
  if (opt_sql_bin_update)
2602 2603 2604 2605 2606 2607 2608 2609 2610
  {
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
                 ER_UPDATE_LOG_DEPRECATED_TRANSLATED,
                 ER(ER_UPDATE_LOG_DEPRECATED_TRANSLATED));
  }
  else
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
                 ER_UPDATE_LOG_DEPRECATED_IGNORED,
                 ER(ER_UPDATE_LOG_DEPRECATED_IGNORED));
unknown's avatar
unknown committed
2611 2612 2613 2614 2615
  set_option_bit(thd, var);
  return 0;
}


2616 2617 2618 2619
static int check_pseudo_thread_id(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2620
  if (thd->security_ctx->master_access & SUPER_ACL)
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
    return 0;
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#else
  return 0;
#endif
}
2631

2632
static uchar *get_warning_count(THD *thd)
2633 2634 2635
{
  thd->sys_var_tmp.long_value=
    (thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_NOTE] +
2636
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR] +
2637
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_WARN]);
2638
  return (uchar*) &thd->sys_var_tmp.long_value;
2639 2640
}

2641
static uchar *get_error_count(THD *thd)
2642 2643 2644
{
  thd->sys_var_tmp.long_value= 
    thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR];
2645
  return (uchar*) &thd->sys_var_tmp.long_value;
2646 2647
}

2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

/*
  Get the tmpdir that was specified or chosen by default

  SYNOPSIS
    get_tmpdir()
    thd		thread handle

  DESCRIPTION
    This is necessary because if the user does not specify a temporary
    directory via the command line, one is chosen based on the environment
    or system defaults.  But we can't just always use mysql_tmpdir, because
    that is actually a call to my_tmpdir() which cycles among possible
    temporary directories.

  RETURN VALUES
    ptr		pointer to NUL-terminated string
 */
2666
static uchar *get_tmpdir(THD *thd)
2667 2668
{
  if (opt_mysql_tmpdir)
2669 2670
    return (uchar *)opt_mysql_tmpdir;
  return (uchar*)mysql_tmpdir;
2671 2672
}

unknown's avatar
unknown committed
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
/****************************************************************************
  Main handling of variables:
  - Initialisation
  - Searching during parsing
  - Update loop
****************************************************************************/

/*
  Find variable name in option my_getopt structure used for command line args

  SYNOPSIS
    find_option()
    opt		option structure array to search in
    name	variable name

  RETURN VALUES
    0		Error
    ptr		pointer to option structure
*/

static struct my_option *find_option(struct my_option *opt, const char *name) 
{
  uint length=strlen(name);
  for (; opt->name; opt++)
  {
    if (!getopt_compare_strings(opt->name, name, length) &&
	!opt->name[length])
    {
      /*
	Only accept the option if one can set values through it.
	If not, there is no default value or limits in the option.
      */
      return (opt->value) ? opt : 0;
    }
  }
  return 0;
}


/*
  Return variable name and length for hashing of variables
*/

2716 2717
static uchar *get_sys_var_length(const sys_var *var, size_t *length,
                                 my_bool first)
unknown's avatar
unknown committed
2718 2719
{
  *length= var->name_length;
2720
  return (uchar*) var->name;
unknown's avatar
unknown committed
2721 2722 2723 2724
}


/*
unknown's avatar
unknown committed
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
  Add variables to the dynamic hash of system variables
  
  SYNOPSIS
    mysql_add_sys_var_chain()
    first       Pointer to first system variable to add
    long_opt    (optional)command line arguments may be tied for limit checks.
  
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
unknown's avatar
unknown committed
2735 2736 2737
*/


unknown's avatar
unknown committed
2738
int mysql_add_sys_var_chain(sys_var *first, struct my_option *long_options)
unknown's avatar
unknown committed
2739
{
2740
  sys_var *var;
unknown's avatar
unknown committed
2741 2742 2743 2744
  
  /* A write lock should be held on LOCK_system_variables_hash */
  
  for (var= first; var; var= var->next)
unknown's avatar
unknown committed
2745
  {
2746
    var->name_length= strlen(var->name);
unknown's avatar
unknown committed
2747
    /* this fails if there is a conflicting variable name. see HASH_UNIQUE */
2748
    if (my_hash_insert(&system_variable_hash, (uchar*) var))
unknown's avatar
unknown committed
2749 2750 2751
      goto error;
    if (long_options)
      var->option_limits= find_option(long_options, var->name);
unknown's avatar
unknown committed
2752
  }
unknown's avatar
unknown committed
2753 2754 2755 2756
  return 0;

error:
  for (; first != var; first= first->next)
2757
    hash_delete(&system_variable_hash, (uchar*) first);
unknown's avatar
unknown committed
2758 2759
  return 1;
}
2760 2761
 
 
unknown's avatar
unknown committed
2762 2763
/*
  Remove variables to the dynamic hash of system variables
2764
   
unknown's avatar
unknown committed
2765 2766 2767
  SYNOPSIS
    mysql_del_sys_var_chain()
    first       Pointer to first system variable to remove
2768
   
unknown's avatar
unknown committed
2769 2770 2771 2772
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/
2773
 
unknown's avatar
unknown committed
2774 2775 2776
int mysql_del_sys_var_chain(sys_var *first)
{
  int result= 0;
2777
 
unknown's avatar
unknown committed
2778
  /* A write lock should be held on LOCK_system_variables_hash */
2779
   
unknown's avatar
unknown committed
2780
  for (sys_var *var= first; var; var= var->next)
2781
    result|= hash_delete(&system_variable_hash, (uchar*) var);
unknown's avatar
unknown committed
2782 2783 2784

  return result;
}
2785 2786
 
 
unknown's avatar
unknown committed
2787 2788 2789 2790
static int show_cmp(SHOW_VAR *a, SHOW_VAR *b)
{
  return strcmp(a->name, b->name);
}
2791 2792
 
 
unknown's avatar
unknown committed
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
/*
  Constructs an array of system variables for display to the user.
  
  SYNOPSIS
    enumerate_sys_vars()
    thd         current thread
    sorted      If TRUE, the system variables should be sorted
  
  RETURN VALUES
    pointer     Array of SHOW_VAR elements for display
    NULL        FAILURE
*/

SHOW_VAR* enumerate_sys_vars(THD *thd, bool sorted)
{
  int count= system_variable_hash.records, i;
  int fixed_count= fixed_show_vars.elements;
  int size= sizeof(SHOW_VAR) * (count + fixed_count + 1);
  SHOW_VAR *result= (SHOW_VAR*) thd->alloc(size);

  if (result)
  {
    SHOW_VAR *show= result + fixed_count;
    memcpy(result, fixed_show_vars.buffer, fixed_count * sizeof(SHOW_VAR));

    for (i= 0; i < count; i++)
    {
      sys_var *var= (sys_var*) hash_element(&system_variable_hash, i);
      show->name= var->name;
      show->value= (char*) var;
      show->type= SHOW_SYS;
      show++;
    }

    /* sort into order */
    if (sorted)
      qsort(result, count + fixed_count, sizeof(SHOW_VAR), (qsort_cmp)show_cmp);
    
    /* make last element empty */
    bzero(show, sizeof(SHOW_VAR));
  }
  return result;
}


/*
  Initialize the system variables
  
  SYNOPSIS
    set_var_init()
  
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/

int set_var_init()
{
  uint count= 0;
  DBUG_ENTER("set_var_init");
  
  for (sys_var *var=vars.first; var; var= var->next, count++);

  if (my_init_dynamic_array(&fixed_show_vars, sizeof(SHOW_VAR),
                            FIXED_VARS_SIZE + 64, 64))
    goto error;

  fixed_show_vars.elements= FIXED_VARS_SIZE;
  memcpy(fixed_show_vars.buffer, fixed_vars, sizeof(fixed_vars));

  if (hash_init(&system_variable_hash, system_charset_info, count, 0,
                0, (hash_get_key) get_sys_var_length, 0, HASH_UNIQUE))
    goto error;

  vars.last->next= NULL;
  if (mysql_add_sys_var_chain(vars.first, my_long_options))
    goto error;

unknown's avatar
unknown committed
2871 2872 2873 2874 2875 2876 2877
  /*
    Special cases
    Needed because MySQL can't find the limits for a variable it it has
    a different name than the command line option.
    As these variables are deprecated, this code will disappear soon...
  */
  sys_sql_max_join_size.option_limits= sys_max_join_size.option_limits;
unknown's avatar
unknown committed
2878 2879 2880 2881 2882 2883

  DBUG_RETURN(0);

error:
  fprintf(stderr, "failed to initialize system variables");
  DBUG_RETURN(1);
unknown's avatar
unknown committed
2884 2885 2886 2887 2888 2889
}


void set_var_free()
{
  hash_free(&system_variable_hash);
unknown's avatar
unknown committed
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
  delete_dynamic(&fixed_show_vars);
}


/*
  Add elements to the dynamic list of read-only system variables.
  
  SYNOPSIS
    mysql_append_static_vars()
    show_vars	Pointer to start of array
    count       Number of elements
  
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/
int mysql_append_static_vars(const SHOW_VAR *show_vars, uint count)
{
  for (; count > 0; count--, show_vars++)
unknown's avatar
unknown committed
2909
    if (insert_dynamic(&fixed_show_vars, (uchar*) show_vars))
unknown's avatar
unknown committed
2910 2911
      return 1;
  return 0;
unknown's avatar
unknown committed
2912 2913 2914 2915 2916 2917 2918
}


/*
  Find a user set-table variable

  SYNOPSIS
unknown's avatar
unknown committed
2919
    intern_find_sys_var()
unknown's avatar
unknown committed
2920 2921 2922 2923 2924 2925 2926 2927 2928
    str		Name of system variable to find
    length	Length of variable.  zero means that we should use strlen()
		on the variable

  RETURN VALUES
    pointer	pointer to variable definitions
    0		Unknown variable (error message is given)
*/

unknown's avatar
unknown committed
2929
sys_var *intern_find_sys_var(const char *str, uint length, bool no_error)
unknown's avatar
unknown committed
2930
{
unknown's avatar
unknown committed
2931 2932 2933 2934 2935 2936 2937
  sys_var *var;

  /*
    This function is only called from the sql_plugin.cc.
    A lock on LOCK_system_variable_hash should be held
  */
  var= (sys_var*) hash_search(&system_variable_hash,
2938
			      (uchar*) str, length ? length : strlen(str));
unknown's avatar
unknown committed
2939
  if (!(var || no_error))
2940
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
unknown's avatar
unknown committed
2941

unknown's avatar
unknown committed
2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
  return var;
}


/*
  Execute update of all variables

  SYNOPSIS

  sql_set
    THD		Thread id
    set_var	List of variables to update

  DESCRIPTION
    First run a check of all variables that all updates will go ok.
    If yes, then execute all updates, returning an error if any one failed.

    This should ensure that in all normal cases none all or variables are
    updated

    RETURN VALUE
    0	ok
2964 2965
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
unknown's avatar
unknown committed
2966 2967
*/

2968
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
unknown's avatar
unknown committed
2969
{
unknown's avatar
unknown committed
2970
  int error;
unknown's avatar
unknown committed
2971
  List_iterator_fast<set_var_base> it(*var_list);
unknown's avatar
unknown committed
2972
  DBUG_ENTER("sql_set_variables");
unknown's avatar
unknown committed
2973 2974 2975 2976

  set_var_base *var;
  while ((var=it++))
  {
unknown's avatar
unknown committed
2977
    if ((error= var->check(thd)))
2978
      goto err;
unknown's avatar
unknown committed
2979
  }
unknown's avatar
unknown committed
2980
  if (!(error= test(thd->net.report_error)))
2981 2982 2983 2984 2985
  {
    it.rewind();
    while ((var= it++))
      error|= var->update(thd);         // Returns 0, -1 or 1
  }
unknown's avatar
unknown committed
2986

2987 2988
err:
  free_underlaid_joins(thd, &thd->lex->select_lex);
unknown's avatar
unknown committed
2989
  DBUG_RETURN(error);
unknown's avatar
unknown committed
2990 2991 2992
}


2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
/*
  Say if all variables set by a SET support the ONE_SHOT keyword (currently,
  only character set and collation do; later timezones will).

  SYNOPSIS

  not_all_support_one_shot
    set_var	List of variables to update

  NOTES
    It has a "not_" because it makes faster tests (no need to "!")

    RETURN VALUE
    0	all variables of the list support ONE_SHOT
    1	at least one does not support ONE_SHOT
*/

bool not_all_support_one_shot(List<set_var_base> *var_list)
{
  List_iterator_fast<set_var_base> it(*var_list);
  set_var_base *var;
  while ((var= it++))
  {
    if (var->no_support_one_shot())
      return 1;
  }
  return 0;
}


unknown's avatar
unknown committed
3023 3024 3025 3026
/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

3027
int set_var::check(THD *thd)
unknown's avatar
unknown committed
3028
{
3029 3030 3031 3032 3033
  if (var->is_readonly())
  {
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->name, "read only");
    return -1;
  }
unknown's avatar
unknown committed
3034 3035
  if (var->check_type(type))
  {
unknown's avatar
unknown committed
3036
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3037
    my_error(err, MYF(0), var->name);
3038
    return -1;
unknown's avatar
unknown committed
3039 3040 3041 3042 3043 3044 3045 3046
  }
  if ((type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL)))
    return 1;
  /* value is a NULL pointer if we are using SET ... = DEFAULT */
  if (!value)
  {
    if (var->check_default(type))
    {
3047
      my_error(ER_NO_DEFAULT, MYF(0), var->name);
3048
      return -1;
unknown's avatar
unknown committed
3049 3050 3051 3052
    }
    return 0;
  }

unknown's avatar
unknown committed
3053
  if ((!value->fixed &&
3054
       value->fix_fields(thd, &value)) || value->check_cols(1))
3055
    return -1;
unknown's avatar
unknown committed
3056 3057
  if (var->check_update_type(value->result_type()))
  {
3058
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
3059
    return -1;
3060
  }
3061
  return var->check(thd, this) ? -1 : 0;
unknown's avatar
unknown committed
3062 3063 3064
}


unknown's avatar
unknown committed
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
/*
  Check variable, but without assigning value (used by PS)

  SYNOPSIS
    set_var::light_check()
    thd		thread handler

  RETURN VALUE
    0	ok
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
*/
3077 3078 3079 3080
int set_var::light_check(THD *thd)
{
  if (var->check_type(type))
  {
unknown's avatar
unknown committed
3081
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3082
    my_error(err, MYF(0), var->name);
3083 3084
    return -1;
  }
unknown's avatar
unknown committed
3085
  if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL))
3086 3087
    return 1;

3088
  if (value && ((!value->fixed && value->fix_fields(thd, &value)) ||
unknown's avatar
unknown committed
3089
                value->check_cols(1)))
3090 3091 3092 3093 3094
    return -1;
  return 0;
}


3095
int set_var::update(THD *thd)
unknown's avatar
unknown committed
3096 3097 3098 3099
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
3100
    return -1;				// should never happen
unknown's avatar
unknown committed
3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


/*****************************************************************************
  Functions to handle SET @user_variable=const_expr
*****************************************************************************/

3111
int set_var_user::check(THD *thd)
unknown's avatar
unknown committed
3112
{
unknown's avatar
unknown committed
3113 3114
  /*
    Item_func_set_user_var can't substitute something else on its place =>
3115
    0 can be passed as last argument (reference on item)
unknown's avatar
unknown committed
3116
  */
3117
  return (user_var_item->fix_fields(thd, (Item**) 0) ||
3118
	  user_var_item->check(0)) ? -1 : 0;
unknown's avatar
unknown committed
3119 3120 3121
}


unknown's avatar
unknown committed
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
/*
  Check variable, but without assigning value (used by PS)

  SYNOPSIS
    set_var_user::light_check()
    thd		thread handler

  RETURN VALUE
    0	ok
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
*/
3134 3135 3136 3137 3138 3139
int set_var_user::light_check(THD *thd)
{
  /*
    Item_func_set_user_var can't substitute something else on its place =>
    0 can be passed as last argument (reference on item)
  */
3140
  return (user_var_item->fix_fields(thd, (Item**) 0));
3141 3142 3143
}


3144
int set_var_user::update(THD *thd)
unknown's avatar
unknown committed
3145 3146 3147 3148
{
  if (user_var_item->update())
  {
    /* Give an error if it's not given already */
unknown's avatar
unknown committed
3149
    my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
3150
    return -1;
unknown's avatar
unknown committed
3151 3152 3153 3154 3155 3156 3157 3158 3159
  }
  return 0;
}


/*****************************************************************************
  Functions to handle SET PASSWORD
*****************************************************************************/

3160
int set_var_password::check(THD *thd)
unknown's avatar
unknown committed
3161
{
unknown's avatar
unknown committed
3162
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
3163
  if (!user->host.str)
3164
  {
3165
    if (*thd->security_ctx->priv_host != 0)
3166
    {
3167 3168
      user->host.str= (char *) thd->security_ctx->priv_host;
      user->host.length= strlen(thd->security_ctx->priv_host);
3169 3170 3171 3172 3173 3174 3175
    }
    else
    {
      user->host.str= (char *)"%";
      user->host.length= 1;
    }
  }
3176
  /* Returns 1 as the function sends error to client */
3177 3178
  return check_change_password(thd, user->host.str, user->user.str,
                               password, strlen(password)) ? 1 : 0;
3179
#else
unknown's avatar
unknown committed
3180 3181
  return 0;
#endif
unknown's avatar
unknown committed
3182 3183
}

3184
int set_var_password::update(THD *thd)
unknown's avatar
unknown committed
3185
{
unknown's avatar
unknown committed
3186
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3187
  /* Returns 1 as the function sends error to client */
3188 3189
  return change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0;
unknown's avatar
unknown committed
3190 3191 3192
#else
  return 0;
#endif
unknown's avatar
unknown committed
3193 3194
}

3195 3196 3197 3198
/****************************************************************************
 Functions to handle table_type
****************************************************************************/

unknown's avatar
unknown committed
3199 3200
/* Based upon sys_var::check_enum() */

unknown's avatar
unknown committed
3201
bool sys_var_thd_storage_engine::check(THD *thd, set_var *var)
3202
{
unknown's avatar
unknown committed
3203
  char buff[STRING_BUFFER_USUAL_SIZE];
3204 3205 3206
  const char *value;
  String str(buff, sizeof(buff), &my_charset_latin1), *res;

unknown's avatar
unknown committed
3207
  var->save_result.plugin= NULL;
3208 3209
  if (var->value->result_type() == STRING_RESULT)
  {
unknown's avatar
unknown committed
3210
    LEX_STRING name;
unknown's avatar
unknown committed
3211
    handlerton *hton;
3212
    if (!(res=var->value->val_str(&str)) ||
unknown's avatar
unknown committed
3213
        !(name.str= (char *)res->ptr()) || !(name.length= res->length()) ||
unknown's avatar
unknown committed
3214 3215 3216
	!(var->save_result.plugin= ha_resolve_by_name(thd, &name)) ||
        !(hton= plugin_data(var->save_result.plugin, handlerton *)) ||
        ha_checktype(thd, ha_legacy_type(hton), 1, 0) != hton)
3217 3218 3219 3220 3221 3222
    {
      value= res ? res->c_ptr() : "NULL";
      goto err;
    }
    return 0;
  }
unknown's avatar
unknown committed
3223
  value= "unknown";
3224 3225

err:
3226
  my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), value);
unknown's avatar
unknown committed
3227
  return 1;
3228 3229
}

unknown's avatar
unknown committed
3230

3231
uchar *sys_var_thd_storage_engine::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
3232
					    LEX_STRING *base)
3233
{
3234
  uchar* result;
unknown's avatar
unknown committed
3235 3236 3237 3238 3239 3240 3241
  handlerton *hton;
  LEX_STRING *name;
  plugin_ref plugin= thd->variables.*offset;
  if (type == OPT_GLOBAL)
    plugin= my_plugin_lock(thd, &(global_system_variables.*offset));
  hton= plugin_data(plugin, handlerton*);
  name= &hton2plugin[hton->slot]->name;
3242
  result= (uchar *) thd->strmake(name->str, name->length);
unknown's avatar
unknown committed
3243 3244 3245
  if (type == OPT_GLOBAL)
    plugin_unlock(thd, plugin);
  return result;
3246 3247
}

unknown's avatar
unknown committed
3248

unknown's avatar
unknown committed
3249
void sys_var_thd_storage_engine::set_default(THD *thd, enum_var_type type)
3250
{
unknown's avatar
unknown committed
3251
  plugin_ref old_value, new_value, *value;
3252
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
3253 3254 3255 3256
  {
    value= &(global_system_variables.*offset);
    new_value= ha_lock_engine(NULL, myisam_hton);
  }
3257
  else
unknown's avatar
unknown committed
3258 3259 3260 3261 3262 3263 3264 3265
  {
    value= &(thd->variables.*offset);
    new_value= my_plugin_lock(NULL, &(global_system_variables.*offset));
  }
  DBUG_ASSERT(new_value);
  old_value= *value;
  *value= new_value;
  plugin_unlock(NULL, old_value);
3266 3267
}

unknown's avatar
unknown committed
3268

unknown's avatar
unknown committed
3269
bool sys_var_thd_storage_engine::update(THD *thd, set_var *var)
3270
{
unknown's avatar
unknown committed
3271 3272 3273 3274 3275 3276 3277 3278 3279
  plugin_ref *value= &(global_system_variables.*offset), old_value;
   if (var->type != OPT_GLOBAL)
     value= &(thd->variables.*offset);
  old_value= *value;
  if (old_value != var->save_result.plugin)
  {
    *value= my_plugin_lock(NULL, &var->save_result.plugin);
    plugin_unlock(NULL, old_value);
  }
3280 3281 3282
  return 0;
}

unknown's avatar
unknown committed
3283 3284
void sys_var_thd_table_type::warn_deprecated(THD *thd)
{
3285
  WARN_DEPRECATED(thd, "5.2", "table_type", "'storage_engine'");
unknown's avatar
unknown committed
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
}

void sys_var_thd_table_type::set_default(THD *thd, enum_var_type type)
{
  warn_deprecated(thd);
  sys_var_thd_storage_engine::set_default(thd, type);
}

bool sys_var_thd_table_type::update(THD *thd, set_var *var)
{
  warn_deprecated(thd);
  return sys_var_thd_storage_engine::update(thd, var);
}

unknown's avatar
unknown committed
3300

3301 3302 3303
/****************************************************************************
 Functions to handle sql_mode
****************************************************************************/
3304

3305 3306 3307
/*
  Make string representation of mode

3308 3309 3310
  SYNOPSIS
    thd   in  thread handler
    val   in  sql_mode value
3311
    rep   out pointer pointer to string with sql_mode representation
3312 3313
*/

3314 3315 3316
bool
sys_var_thd_sql_mode::
symbolic_mode_representation(THD *thd, ulonglong val, LEX_STRING *rep)
3317
{
3318
  char buff[STRING_BUFFER_USUAL_SIZE*8];
3319 3320 3321
  String tmp(buff, sizeof(buff), &my_charset_latin1);

  tmp.length(0);
3322

3323 3324 3325 3326
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
3327 3328
      tmp.append(sql_mode_typelib.type_names[i],
                 sql_mode_typelib.type_lengths[i]);
3329 3330 3331
      tmp.append(',');
    }
  }
unknown's avatar
unknown committed
3332

3333 3334 3335 3336 3337 3338 3339 3340
  if (tmp.length())
    tmp.length(tmp.length() - 1); /* trim the trailing comma */

  rep->str= thd->strmake(tmp.ptr(), tmp.length());

  rep->length= rep->str ? tmp.length() : 0;

  return rep->length != tmp.length();
3341 3342
}

unknown's avatar
unknown committed
3343

3344
uchar *sys_var_thd_sql_mode::value_ptr(THD *thd, enum_var_type type,
3345 3346
				      LEX_STRING *base)
{
3347
  LEX_STRING sql_mode;
3348 3349
  ulonglong val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
                  thd->variables.*offset);
3350
  (void) symbolic_mode_representation(thd, val, &sql_mode);
3351
  return (uchar *) sql_mode.str;
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
}


void sys_var_thd_sql_mode::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= 0;
  else
    thd->variables.*offset= global_system_variables.*offset;
}

unknown's avatar
unknown committed
3363

3364 3365 3366 3367 3368 3369
void fix_sql_mode_var(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.sql_mode=
      fix_sql_mode(global_system_variables.sql_mode);
  else
3370
  {
3371
    thd->variables.sql_mode= fix_sql_mode(thd->variables.sql_mode);
3372 3373 3374 3375 3376 3377 3378 3379
    /*
      Update thd->server_status
     */
    if (thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)
      thd->server_status|= SERVER_STATUS_NO_BACKSLASH_ESCAPES;
    else
      thd->server_status&= ~SERVER_STATUS_NO_BACKSLASH_ESCAPES;
  }
3380 3381 3382
}

/* Map database specific bits to function bits */
3383

3384 3385 3386 3387 3388 3389 3390 3391 3392
ulong fix_sql_mode(ulong sql_mode)
{
  /*
    Note that we dont set 
    MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS | MODE_NO_FIELD_OPTIONS
    to allow one to get full use of MySQL in this mode.
  */

  if (sql_mode & MODE_ANSI)
3393
  {
3394
    sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
3395 3396 3397 3398 3399 3400
		MODE_IGNORE_SPACE);
    /* 
      MODE_ONLY_FULL_GROUP_BY removed from ANSI mode because it is currently
      overly restrictive (see BUG#8510).
    */
  }
3401 3402 3403 3404
  if (sql_mode & MODE_ORACLE)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
3405
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
  if (sql_mode & MODE_MSSQL)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
  if (sql_mode & MODE_POSTGRESQL)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
  if (sql_mode & MODE_DB2)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
3421
  if (sql_mode & MODE_MAXDB)
3422 3423 3424
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
3425
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3426
  if (sql_mode & MODE_MYSQL40)
3427
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
3428
  if (sql_mode & MODE_MYSQL323)
3429
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
unknown's avatar
unknown committed
3430 3431 3432
  if (sql_mode & MODE_TRADITIONAL)
    sql_mode|= (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES |
                MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
3433
                MODE_ERROR_FOR_DIVISION_BY_ZERO | MODE_NO_AUTO_CREATE_USER);
3434 3435
  return sql_mode;
}
3436 3437


3438 3439 3440 3441
/****************************************************************************
  Named list handling
****************************************************************************/

3442
uchar* find_named(I_List<NAMED_LIST> *list, const char *name, uint length,
unknown's avatar
unknown committed
3443
		NAMED_LIST **found)
3444 3445 3446 3447 3448 3449
{
  I_List_iterator<NAMED_LIST> it(*list);
  NAMED_LIST *element;
  while ((element= it++))
  {
    if (element->cmp(name, length))
unknown's avatar
unknown committed
3450
    {
3451 3452
      if (found)
        *found= element;
3453
      return element->data;
unknown's avatar
unknown committed
3454
    }
3455 3456 3457 3458 3459
  }
  return 0;
}


3460
void delete_elements(I_List<NAMED_LIST> *list,
3461
		     void (*free_element)(const char *name, uchar*))
3462 3463
{
  NAMED_LIST *element;
unknown's avatar
unknown committed
3464
  DBUG_ENTER("delete_elements");
3465 3466
  while ((element= list->get()))
  {
3467
    (*free_element)(element->name, element->data);
3468 3469
    delete element;
  }
unknown's avatar
unknown committed
3470
  DBUG_VOID_RETURN;
3471 3472 3473 3474 3475
}


/* Key cache functions */

unknown's avatar
unknown committed
3476
static KEY_CACHE *create_key_cache(const char *name, uint length)
3477
{
unknown's avatar
unknown committed
3478
  KEY_CACHE *key_cache;
3479 3480 3481
  DBUG_ENTER("create_key_cache");
  DBUG_PRINT("enter",("name: %.*s", length, name));
  
unknown's avatar
unknown committed
3482
  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
3483
					     MYF(MY_ZEROFILL | MY_WME))))
unknown's avatar
unknown committed
3484
  {
3485
    if (!new NAMED_LIST(&key_caches, name, length, (uchar*) key_cache))
3486 3487
    {
      my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
3488
      key_cache= 0;
3489 3490 3491 3492 3493 3494 3495 3496 3497
    }
    else
    {
      /*
	Set default values for a key cache
	The values in dflt_key_cache_var is set by my_getopt() at startup

	We don't set 'buff_size' as this is used to enable the key cache
      */
unknown's avatar
unknown committed
3498 3499 3500
      key_cache->param_block_size=     dflt_key_cache_var.param_block_size;
      key_cache->param_division_limit= dflt_key_cache_var.param_division_limit;
      key_cache->param_age_threshold=  dflt_key_cache_var.param_age_threshold;
3501
    }
unknown's avatar
unknown committed
3502
  }
3503
  DBUG_RETURN(key_cache);
3504 3505 3506
}


unknown's avatar
unknown committed
3507
KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
3508
{
unknown's avatar
unknown committed
3509
  LEX_STRING key_cache_name;
unknown's avatar
unknown committed
3510
  KEY_CACHE *key_cache;
3511

unknown's avatar
unknown committed
3512 3513
  key_cache_name.str= (char *) name;
  key_cache_name.length= length;
3514 3515
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!(key_cache= get_key_cache(&key_cache_name)))
3516
    key_cache= create_key_cache(name, length);
3517
  pthread_mutex_unlock(&LOCK_global_system_variables);
3518 3519 3520 3521
  return key_cache;
}


unknown's avatar
unknown committed
3522
void free_key_cache(const char *name, KEY_CACHE *key_cache)
3523
{
3524 3525
  ha_end_key_cache(key_cache);
  my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
3526 3527 3528
}


unknown's avatar
unknown committed
3529
bool process_key_caches(int (* func) (const char *name, KEY_CACHE *))
3530
{
unknown's avatar
unknown committed
3531 3532
  I_List_iterator<NAMED_LIST> it(key_caches);
  NAMED_LIST *element;
3533

unknown's avatar
unknown committed
3534 3535
  while ((element= it++))
  {
unknown's avatar
unknown committed
3536
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
3537
    func(element->name, key_cache);
unknown's avatar
unknown committed
3538 3539
  }
  return 0;
3540 3541
}

3542

3543 3544
void sys_var_trust_routine_creators::warn_deprecated(THD *thd)
{
3545 3546
  WARN_DEPRECATED(thd, "5.2", "log_bin_trust_routine_creators",
                      "'log_bin_trust_function_creators'");
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560
}

void sys_var_trust_routine_creators::set_default(THD *thd, enum_var_type type)
{
  warn_deprecated(thd);
  sys_var_bool_ptr::set_default(thd, type);
}

bool sys_var_trust_routine_creators::update(THD *thd, set_var *var)
{
  warn_deprecated(thd);
  return sys_var_bool_ptr::update(thd, var);
}

unknown's avatar
unknown committed
3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
bool sys_var_opt_readonly::update(THD *thd, set_var *var)
{
  bool result;

  DBUG_ENTER("sys_var_opt_readonly::update");

  /* Prevent self dead-lock */
  if (thd->locked_tables || thd->active_transaction())
  {
    my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
    DBUG_RETURN(true);
  }

  if (thd->global_read_lock)
  {
    /*
      This connection already holds the global read lock.
      This can be the case with:
      - FLUSH TABLES WITH READ LOCK
      - SET GLOBAL READ_ONLY = 1
    */
    result= sys_var_bool_ptr::update(thd, var);
    DBUG_RETURN(result);
  }

  /*
    Perform a 'FLUSH TABLES WITH READ LOCK'.
    This is a 3 step process:
    - [1] lock_global_read_lock()
    - [2] close_cached_tables()
    - [3] make_global_read_lock_block_commit()
    [1] prevents new connections from obtaining tables locked for write.
    [2] waits until all existing connections close their tables.
    [3] prevents transactions from being committed.
  */

  if (lock_global_read_lock(thd))
    DBUG_RETURN(true);

  /*
    This call will be blocked by any connection holding a READ or WRITE lock.
    Ideally, we want to wait only for pending WRITE locks, but since:
    con 1> LOCK TABLE T FOR READ;
    con 2> LOCK TABLE T FOR WRITE; (blocked by con 1)
    con 3> SET GLOBAL READ ONLY=1; (blocked by con 2)
    can cause to wait on a read lock, it's required for the client application
    to unlock everything, and acceptable for the server to wait on all locks.
  */
3609
  if (result= close_cached_tables(thd, true, NULL, false))
unknown's avatar
unknown committed
3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
    goto end_with_read_lock;

  if (result= make_global_read_lock_block_commit(thd))
    goto end_with_read_lock;

  /* Change the opt_readonly system variable, safe because the lock is held */
  result= sys_var_bool_ptr::update(thd, var);

end_with_read_lock:
  /* Release the lock */
  unlock_global_read_lock(thd);
  DBUG_RETURN(result);
}


unknown's avatar
unknown committed
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
/* even session variable here requires SUPER, because of -#o,file */
bool sys_var_thd_dbug::check(THD *thd, set_var *var)
{
  return check_global_access(thd, SUPER_ACL);
}

bool sys_var_thd_dbug::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    DBUG_SET_INITIAL(var ? var->value->str_value.c_ptr() : "");
  else
  {
    DBUG_POP();
    DBUG_PUSH(var ? var->value->str_value.c_ptr() : "");
  }
  return 0;
}

3643

3644
uchar *sys_var_thd_dbug::value_ptr(THD *thd, enum_var_type type, LEX_STRING *b)
unknown's avatar
unknown committed
3645 3646 3647 3648 3649 3650
{
  char buf[256];
  if (type == OPT_GLOBAL)
    DBUG_EXPLAIN_INITIAL(buf, sizeof(buf));
  else
    DBUG_EXPLAIN(buf, sizeof(buf));
3651
  return (uchar*) thd->strdup(buf);
unknown's avatar
unknown committed
3652
}
unknown's avatar
unknown committed
3653

3654

3655 3656 3657 3658 3659 3660
bool sys_var_event_scheduler::check(THD *thd, set_var *var)
{
  return check_enum(thd, var, &Events::var_typelib);
}


3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
/*
   The update method of the global variable event_scheduler.
   If event_scheduler is switched from 0 to 1 then the scheduler main
   thread is resumed and if from 1 to 0 the scheduler thread is suspended

   SYNOPSIS
     sys_var_event_scheduler::update()
       thd  Thread context (unused)
       var  The new value

   Returns
     FALSE  OK
     TRUE   Error
*/

bool
sys_var_event_scheduler::update(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
3679
  int res;
3680 3681
  /* here start the thread if not running. */
  DBUG_ENTER("sys_var_event_scheduler::update");
unknown's avatar
unknown committed
3682
  DBUG_PRINT("info", ("new_value: %d", (int) var->save_result.ulong_value));
3683

3684 3685 3686 3687
  enum Events::enum_opt_event_scheduler
    new_state=
    (enum Events::enum_opt_event_scheduler) var->save_result.ulong_value;

3688
  res= Events::switch_event_scheduler_state(new_state);
3689 3690 3691 3692 3693

  DBUG_RETURN((bool) res);
}


3694
uchar *sys_var_event_scheduler::value_ptr(THD *thd, enum_var_type type,
3695 3696
                                         LEX_STRING *base)
{
3697
  return (uchar *) Events::get_opt_event_scheduler_str();
3698 3699 3700
}


unknown's avatar
unknown committed
3701 3702 3703 3704
/****************************************************************************
  Used templates
****************************************************************************/

3705
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
3706
template class List<set_var_base>;
unknown's avatar
unknown committed
3707
template class List_iterator_fast<set_var_base>;
3708
template class I_List_iterator<NAMED_LIST>;
unknown's avatar
unknown committed
3709
#endif