mysqld.cc 145 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   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.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19
   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 */

#include "mysql_priv.h"
#include <mysql.h>
#include <m_ctype.h>
20
#include <my_dir.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
21
#include "sql_acl.h"
22
#include "slave.h"
23
#include "sql_repl.h"
24
#include "repl_failsafe.h"
25
#include "stacktrace.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
26 27 28
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"
#endif
29
#ifdef HAVE_INNOBASE_DB
30
#include "ha_innodb.h"
31
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
32 33 34 35 36
#include "ha_myisam.h"
#include <nisam.h>
#include <thr_alarm.h>
#include <ft_global.h>

37 38 39 40
#ifndef DBUG_OFF
#define ONE_THREAD
#endif

41 42 43 44 45 46 47
/* do stack traces are only supported on linux intel */
#if defined(__linux__)  && defined(__i386__) && defined(USE_PSTACK)
#define	HAVE_STACK_TRACE_ON_SEGV
#include "../pstack/pstack.h"
char pstack_file_name[80];
#endif /* __linux__ */

48 49 50 51
#if defined(HAVE_DEC_3_2_THREADS) || defined(SIGNALS_DONT_BREAK_READ)
#define HAVE_CLOSE_SERVER_SOCK 1
#endif  

bk@work.mysql.com's avatar
bk@work.mysql.com committed
52 53 54 55
extern "C" {					// Because of SCO 3.2V4.2
#include <errno.h>
#include <sys/stat.h>
#ifndef __GNU_LIBRARY__
56
#define __GNU_LIBRARY__				// Skip warnings in getopt.h
bk@work.mysql.com's avatar
bk@work.mysql.com committed
57 58 59 60 61 62 63 64 65 66 67 68
#endif
#include <getopt.h>
#ifdef HAVE_SYSENT_H
#include <sysent.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>				// For getpwent
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
69 70 71
#if defined(OS2)
#  include <sys/un.h>
#elif !defined( __WIN__)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
72 73 74 75 76 77 78 79 80 81 82 83
#include <sys/resource.h>
#ifdef HAVE_SYS_UN_H
#  include <sys/un.h>
#endif
#include <netdb.h>
#ifdef HAVE_SELECT_H
#  include <select.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/utsname.h>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
84
#endif /* __WIN__ */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
85 86 87 88 89 90 91 92 93 94 95

#ifdef HAVE_LIBWRAP
#include <tcpd.h>
#include <syslog.h>
#ifdef NEED_SYS_SYSLOG_H
#include <sys/syslog.h>
#endif /* NEED_SYS_SYSLOG_H */
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif /* HAVE_LIBWRAP */

96 97 98 99
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
100
#ifdef _AIX41
101
int initgroups(const char *,unsigned int);
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
102 103
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
104 105 106 107 108 109 110 111 112 113 114 115 116
#if defined(__FreeBSD__) && defined(HAVE_IEEEFP_H)
#include <ieeefp.h>
#ifdef HAVE_FP_EXCEPT				// Fix type conflict
typedef fp_except fp_except_t;
#endif

  /* We can't handle floating point expections with threads, so disable
     this on freebsd
  */

inline void reset_floating_point_exceptions()
{
  /* Don't fall for overflow, underflow,divide-by-zero or loss of precision */
117 118 119
#if defined(__i386__)
  fpsetmask(~(FP_X_INV | FP_X_DNML | FP_X_OFL | FP_X_UFL | FP_X_DZ |
	      FP_X_IMP));
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
120
#else
121 122 123
 fpsetmask(~(FP_X_INV |             FP_X_OFL | FP_X_UFL | FP_X_DZ |
	     FP_X_IMP));
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
124 125 126 127 128
}
#else
#define reset_floating_point_exceptions()
#endif /* __FreeBSD__ && HAVE_IEEEFP_H */

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
129 130
} /* cplusplus */

bk@work.mysql.com's avatar
bk@work.mysql.com committed
131 132 133 134 135 136 137 138 139 140 141 142

#if defined(HAVE_LINUXTHREADS)
#define THR_KILL_SIGNAL SIGINT
#else
#define THR_KILL_SIGNAL SIGUSR2		// Can't use this with LinuxThreads
#endif

#ifdef HAVE_GLIBC2_STYLE_GETHOSTBYNAME_R
#include <sys/types.h>
#else
#include <my_pthread.h>			// For thr_setconcurency()
#endif
143
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) && !defined(HAVE_mit_thread)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
144 145 146 147 148 149 150 151 152 153 154
#define SET_RLIMIT_NOFILE
#endif

#ifdef SOLARIS
extern "C" int gethostname(char *name, int namelen);
#endif

#define MYSQL_KILL_SIGNAL SIGTERM

#ifndef DBUG_OFF
static const char* default_dbug_option=IF_WIN("d:t:i:O,\\mysqld.trace",
155
					      "d:t:i:o,/tmp/mysqld.trace");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
156 157 158 159 160 161 162 163 164 165 166
#endif

#ifdef __NT__
static char szPipeName [ 257 ];
static SECURITY_ATTRIBUTES saPipeSecurity;
static SECURITY_DESCRIPTOR sdPipeDescriptor;
static HANDLE hPipe = INVALID_HANDLE_VALUE;
static pthread_cond_t COND_handler_count;
static uint handler_count;
#endif
#ifdef __WIN__
monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
167
static bool opt_console=0,start_mode=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
168 169
#endif

monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/* Set prefix for windows binary */
#ifdef __WIN__
#undef MYSQL_SERVER_SUFFIX
#ifdef __NT__
#if defined(HAVE_INNOBASE_DB) || defined(HAVE_BERKELEY_DB)
#define MYSQL_SERVER_SUFFIX "-max-nt"
#else
#define MYSQL_SERVER_SUFFIX "-nt"
#endif /* ...DB */
#elif defined(HAVE_INNOBASE_DB) || defined(HAVE_BERKELEY_DB)
#define MYSQL_SERVER_SUFFIX "-max"
#else
#define MYSQL_SERVER_SUFFIX ""
#endif /* __NT__ */
#endif

186 187 188 189 190 191
#ifdef HAVE_BERKELEY_DB
SHOW_COMP_OPTION have_berkeley_db=SHOW_OPTION_YES;
#else
SHOW_COMP_OPTION have_berkeley_db=SHOW_OPTION_NO;
#endif
#ifdef HAVE_INNOBASE_DB
192
SHOW_COMP_OPTION have_innodb=SHOW_OPTION_YES;
193
#else
194
SHOW_COMP_OPTION have_innodb=SHOW_OPTION_NO;
195
#endif
196
#ifdef HAVE_ISAM
197 198 199 200
SHOW_COMP_OPTION have_isam=SHOW_OPTION_YES;
#else
SHOW_COMP_OPTION have_isam=SHOW_OPTION_NO;
#endif
201 202 203 204 205 206
#ifdef USE_RAID
SHOW_COMP_OPTION have_raid=SHOW_OPTION_YES;
#else
SHOW_COMP_OPTION have_raid=SHOW_OPTION_NO;
#endif
#ifdef HAVE_OPENSSL
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
207
SHOW_COMP_OPTION have_openssl=SHOW_OPTION_YES;
208
#else
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
209
SHOW_COMP_OPTION have_openssl=SHOW_OPTION_NO;
210
#endif
211
SHOW_COMP_OPTION have_symlink=SHOW_OPTION_YES;
212 213


214
bool opt_skip_slave_start = 0; // If set, slave is not autostarted
215
static bool opt_do_pstack = 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
216 217
static ulong opt_specialflag=SPECIAL_ENGLISH;
static ulong back_log,connect_timeout,concurrency;
218 219
static ulong opt_myisam_block_size;
static my_socket unix_sock= INVALID_SOCKET,ip_sock= INVALID_SOCKET;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
220
static my_string opt_logname=0,opt_update_logname=0,
221
       opt_binlog_index_name = 0,opt_slow_logname=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
222 223 224
static char mysql_home[FN_REFLEN],pidfile_name[FN_REFLEN];
static pthread_t select_thread;
static bool opt_log,opt_update_log,opt_bin_log,opt_slow_log,opt_noacl,
225
	    opt_disable_networking=0, opt_bootstrap=0,opt_skip_show_db=0,
226
	    opt_myisam_log=0,
227
            opt_large_files=sizeof(my_off_t) > 4;
228
bool opt_sql_bin_update = 0, opt_log_slave_updates = 0, opt_safe_show_db=0,
229
  opt_show_slave_auth_info = 0, opt_old_rpl_compat = 0,
230
  opt_safe_user_create = 0, opt_no_mix_types = 0;
231
volatile bool  mqh_used = 0;
232
FILE *bootstrap_file=0;
233
int segfaulted = 0; // ensure we do not enter SIGSEGV handler twice
bk@work.mysql.com's avatar
bk@work.mysql.com committed
234

235 236 237 238
/*
  If sql_bin_update is true, SQL_LOG_UPDATE and SQL_LOG_BIN are kept in sync,
  and are treated as aliases for each other
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
239 240 241 242 243 244

static bool kill_in_progress=FALSE;
static struct rand_struct sql_rand;
static int cleanup_done;
static char **defaults_argv,time_zone[30];
static const char *default_table_type_name;
245
char glob_hostname[FN_REFLEN];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
246

247
#include "sslopt-vars.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
248
#ifdef HAVE_OPENSSL
249
char *des_key_file = 0;
250
struct st_VioSSLAcceptorFd * ssl_acceptor_fd = 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
251 252
#endif /* HAVE_OPENSSL */

sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
253
I_List <i_string_pair> replicate_rewrite_db;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
254 255 256 257
I_List<i_string> replicate_do_db, replicate_ignore_db;
// allow the user to tell us which db to replicate and which to ignore
I_List<i_string> binlog_do_db, binlog_ignore_db;

258 259 260 261
/* if we guessed server_id , we need to know about it */
uint32 server_id = 0;
bool server_id_supplied = 0;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
262
uint mysql_port;
263
uint test_flags = 0, select_errors=0, dropping_tables=0,ha_open_options=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
264
uint volatile thread_count=0, thread_running=0, kill_cached_threads=0,
265
	      wake_thread=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
266
ulong thd_startup_options=(OPTION_UPDATE_LOG | OPTION_AUTO_IS_NULL |
267
			   OPTION_BIN_LOG | OPTION_QUOTE_SHOW_CREATE );
bk@work.mysql.com's avatar
bk@work.mysql.com committed
268 269 270 271 272 273
uint protocol_version=PROTOCOL_VERSION;
ulong keybuff_size,sortbuff_size,max_item_sort_length,table_cache_size,
      max_join_size,join_buff_size,tmp_table_size,thread_stack,
      thread_stack_min,net_wait_timeout,what_to_log= ~ (1L << (uint) COM_TIME),
      query_buff_size, lower_case_table_names, mysqld_net_retry_count,
      net_interactive_timeout, slow_launch_time = 2L,
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
274
      net_read_timeout,net_write_timeout,slave_open_temp_tables=0,
275
      open_files_limit=0, max_binlog_size, record_rnd_cache_size;
276
ulong com_stat[(uint) SQLCOM_END], com_other;
277
ulong slave_net_timeout;
278
ulong thread_cache_size=0, binlog_cache_size=0, max_binlog_cache_size=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
279
ulong query_cache_size=0, query_cache_limit=0, query_cache_startup_type=1; 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
280 281
volatile ulong cached_thread_count=0;

282
// replication parameters, if master_host is not NULL, we are a slave
bk@work.mysql.com's avatar
bk@work.mysql.com committed
283
my_string master_user = (char*) "test", master_password = 0, master_host=0,
284 285 286
  master_info_file = (char*) "master.info",
  relay_log_info_file = (char*) "relay-log.info",
  master_ssl_key=0, master_ssl_cert=0;
287
my_string report_user = 0, report_password = 0, report_host=0;
288
 
289
const char *localhost=LOCAL_HOST;
290
const char *delayed_user="DELAYED";
bk@work.mysql.com's avatar
bk@work.mysql.com committed
291
uint master_port = MYSQL_PORT, master_connect_retry = 60;
292
uint report_port = MYSQL_PORT;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
293
bool master_ssl = 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
294

295
ulong max_tmp_tables,max_heap_table_size,master_retry_count=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
296 297
ulong bytes_sent = 0L, bytes_received = 0L;

298
bool opt_endinfo,using_udf_functions,low_priority_updates, locked_in_memory;
299
bool opt_using_transactions, using_update_log, opt_warnings=0;
300
bool volatile abort_loop,select_thread_in_use,grant_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
301 302 303 304 305
bool volatile ready_to_exit,shutdown_in_progress;
ulong refresh_version=1L,flush_version=1L;	/* Increments on each reload */
ulong query_id=1L,long_query_count,long_query_time,aborted_threads,
      aborted_connects,delayed_insert_timeout,delayed_insert_limit,
      delayed_queue_size,delayed_insert_threads,delayed_insert_writes,
306
      delayed_rows_in_use,delayed_insert_errors,flush_time, thread_created;
307
ulong filesort_rows, filesort_range_count, filesort_scan_count;
308
ulong filesort_merge_passes;
309 310
ulong select_range_check_count, select_range_count, select_scan_count;
ulong select_full_range_join_count,select_full_join_count;
311 312
ulong specialflag=0,opened_tables=0,created_tmp_tables=0,
      created_tmp_disk_tables=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
313
ulong max_connections,max_insert_delayed_threads,max_used_connections,
314
      max_connect_errors, max_user_connections = 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
315 316
ulong thread_id=1L,current_pid;
ulong slow_launch_threads = 0;
317 318
ulong myisam_max_sort_file_size, myisam_max_extra_sort_file_size;
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
319
char mysql_real_data_home[FN_REFLEN],
320
     language[LIBLEN],reg_ext[FN_EXTLEN],
bk@work.mysql.com's avatar
bk@work.mysql.com committed
321 322 323
     default_charset[LIBLEN],mysql_charsets_dir[FN_REFLEN], *charsets_list,
     blob_newline,f_fyllchar,max_sort_char,*mysqld_user,*mysqld_chroot,
     *opt_init_file;
324 325
#ifndef EMBEDDED_LIBRARY
char mysql_data_home_buff[2], *mysql_data_home=mysql_data_home_buff;
326
bool mysql_embedded=0;
327 328
#else
char *mysql_data_home=mysql_real_data_home;
329
bool mysql_embedded=1;
330 331
#endif

332
char *opt_bin_logname = 0; // this one needs to be seen in sql_parse.cc
333
char *opt_relay_logname = 0, *opt_relaylog_index_name=0;
334
char server_version[SERVER_VERSION_LENGTH]=MYSQL_SERVER_VERSION;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
335 336
const char *first_keyword="first";
const char **errmesg;			/* Error messages */
337
const char *myisam_recover_options_str="OFF";
338
const char *sql_mode_str="OFF";
339
const char *default_tx_isolation_name;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
340
enum_tx_isolation default_tx_isolation=ISO_READ_COMMITTED;
341

342 343
uint rpl_recovery_rank=0;

344
my_string mysql_unix_port=NULL, mysql_tmpdir=NULL, allocated_mysql_tmpdir=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
345 346 347 348 349 350
ulong my_bind_addr;			/* the address we bind to */
DATE_FORMAT dayord;
double log_10[32];			/* 10 potences */
I_List<THD> threads,thread_cache;
time_t start_time;

351 352 353
ulong opt_sql_mode = 0L;
const char *sql_mode_names[] =
{ "REAL_AS_FLOAT", "PIPES_AS_CONCAT", "ANSI_QUOTES", "IGNORE_SPACE",
354
  "SERIALIZE","ONLY_FULL_GROUP_BY", "NO_UNSIGNED_SUBTRACTION",NullS };
355
TYPELIB sql_mode_typelib= {array_elements(sql_mode_names)-1,"",
356
			   sql_mode_names};
357

358
MY_BITMAP temp_pool;
359
bool use_temp_pool=0;
360

bk@work.mysql.com's avatar
bk@work.mysql.com committed
361 362 363 364 365 366 367
pthread_key(MEM_ROOT*,THR_MALLOC);
pthread_key(THD*, THR_THD);
pthread_key(NET*, THR_NET);
pthread_mutex_t LOCK_mysql_create_db, LOCK_Acl, LOCK_open, LOCK_thread_count,
		LOCK_mapped_file, LOCK_status, LOCK_grant,
		LOCK_error_log,
		LOCK_delayed_insert, LOCK_delayed_status, LOCK_delayed_create,
368
		LOCK_crypt, LOCK_bytes_sent, LOCK_bytes_received,
369 370
	        LOCK_server_id,
		LOCK_user_conn, LOCK_slave_list, LOCK_active_mi;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
371

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
372 373
Query_cache query_cache;

374
pthread_cond_t COND_refresh,COND_thread_count,COND_binlog_update,
375
  COND_slave_stopped, COND_slave_start;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
376 377 378 379 380 381 382 383
pthread_cond_t COND_thread_cache,COND_flush_thread_cache;
pthread_t signal_thread;
pthread_attr_t connection_attrib;
enum db_type default_table_type=DB_TYPE_MYISAM;

#ifdef __WIN__
#undef	 getpid
#include <process.h>
384
#if !defined(EMBEDDED_LIBRARY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
385 386 387 388
HANDLE hEventShutdown;
#include "nt_servc.h"
static	 NTService  Service;	      // Service object for WinNT
#endif
389
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
390

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
391 392 393 394
#ifdef OS2
pthread_cond_t eventShutdown;
#endif

395
static void start_signal_handler(void);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
396 397 398 399 400 401
static void *signal_hand(void *arg);
static void set_options(void);
static void get_options(int argc,char **argv);
static char *get_relative_path(const char *path);
static void fix_paths(void);
static pthread_handler_decl(handle_connections_sockets,arg);
402
static pthread_handler_decl(kill_server_thread,arg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
403
static int bootstrap(FILE *file);
404
static void close_server_sock();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
405 406 407 408 409 410 411 412
static bool read_init_file(char *file_name);
#ifdef __NT__
static pthread_handler_decl(handle_connections_namedpipes,arg);
#endif
extern pthread_handler_decl(handle_slave,arg);
#ifdef SET_RLIMIT_NOFILE
static uint set_maximum_open_files(uint max_file_limit);
#endif
413
static ulong find_bit_type(const char *x, TYPELIB *bit_lib);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

/****************************************************************************
** Code to end mysqld
****************************************************************************/

static void close_connections(void)
{
#ifdef EXTRA_DEBUG
  int count=0;
#endif
  NET net;
  DBUG_ENTER("close_connections");

  /* Clear thread cache */
  kill_cached_threads++;
  flush_thread_cache();

  /* kill flush thread */
432 433
  (void) pthread_mutex_lock(&LOCK_manager);
  if (manager_thread_in_use)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
434
  {
435 436
    DBUG_PRINT("quit",("killing manager thread: %lx",manager_thread));
   (void) pthread_cond_signal(&COND_manager);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
437
  }
438
  (void) pthread_mutex_unlock(&LOCK_manager);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
439 440

  /* kill connection thread */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
441
#if !defined(__WIN__) && !defined(__EMX__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
442 443 444 445 446 447 448 449 450 451 452 453
  DBUG_PRINT("quit",("waiting for select thread: %lx",select_thread));
  (void) pthread_mutex_lock(&LOCK_thread_count);

  while (select_thread_in_use)
  {
    struct timespec abstime;
    int error;
    LINT_INIT(error);
#ifndef DONT_USE_THR_ALARM
    if (pthread_kill(select_thread,THR_CLIENT_ALARM))
      break;					// allready dead
#endif
454
    set_timespec(abstime, 2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
455 456 457 458 459 460 461 462 463 464 465
    for (uint tmp=0 ; tmp < 10 ; tmp++)
    {
      error=pthread_cond_timedwait(&COND_thread_count,&LOCK_thread_count,
				   &abstime);
      if (error != EINTR)
	break;
    }
#ifdef EXTRA_DEBUG
    if (error != 0 && !count++)
      sql_print_error("Got error %d from pthread_cond_timedwait",error);
#endif
466
    close_server_sock();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  }
  (void) pthread_mutex_unlock(&LOCK_thread_count);
#endif /* __WIN__ */


  /* Abort listening to new connections */
  DBUG_PRINT("quit",("Closing sockets"));
  if ( !opt_disable_networking )
  {
    if (ip_sock != INVALID_SOCKET)
    {
      (void) shutdown(ip_sock,2);
      (void) closesocket(ip_sock);
      ip_sock= INVALID_SOCKET;
    }
  }
#ifdef __NT__
  if ( hPipe != INVALID_HANDLE_VALUE )
  {
486
    HANDLE hTempPipe = &hPipe;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    DBUG_PRINT( "quit", ("Closing named pipes") );
    hPipe = INVALID_HANDLE_VALUE;
    DisconnectNamedPipe( hTempPipe );
    CloseHandle( hTempPipe );
  }
#endif
#ifdef HAVE_SYS_UN_H
  if (unix_sock != INVALID_SOCKET)
  {
    (void) shutdown(unix_sock,2);
    (void) closesocket(unix_sock);
    (void) unlink(mysql_unix_port);
    unix_sock= INVALID_SOCKET;
  }
#endif
  end_thr_alarm();			 // Don't allow alarms
503
  end_slave();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

  /* First signal all threads that it's time to die */

  THD *tmp;
  (void) pthread_mutex_lock(&LOCK_thread_count); // For unlink from list

  I_List_iterator<THD> it(threads);
  while ((tmp=it++))
  {
    DBUG_PRINT("quit",("Informing thread %ld that it's time to die",
		       tmp->thread_id));
    tmp->killed=1;
    if (tmp->mysys_var)
    {
      tmp->mysys_var->abort=1;
519 520
      pthread_mutex_lock(&tmp->mysys_var->mutex);
      if (tmp->mysys_var->current_cond)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
521 522 523 524 525
      {
	pthread_mutex_lock(tmp->mysys_var->current_mutex);
	pthread_cond_broadcast(tmp->mysys_var->current_cond);
	pthread_mutex_unlock(tmp->mysys_var->current_mutex);
      }
526
      pthread_mutex_unlock(&tmp->mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
527 528 529 530 531 532 533 534 535 536 537
    }
  }
  (void) pthread_mutex_unlock(&LOCK_thread_count); // For unlink from list

  if (thread_count)
  {
    sleep(1);					// Give threads time to die
  }

  /* Force remaining threads to die by closing the connection to the client */

538
  (void) my_net_init(&net, (st_vio*) 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  for (;;)
  {
    DBUG_PRINT("quit",("Locking LOCK_thread_count"));
    (void) pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
    if (!(tmp=threads.get()))
    {
      DBUG_PRINT("quit",("Unlocking LOCK_thread_count"));
      (void) pthread_mutex_unlock(&LOCK_thread_count);
      break;
    }
#ifndef __bsdi__				// Bug in BSDI kernel
    if ((net.vio=tmp->net.vio) != 0)
    {
      sql_print_error(ER(ER_FORCING_CLOSE),my_progname,
		      tmp->thread_id,tmp->user ? tmp->user : "");
      close_connection(&net,0,0);
    }
#endif
    DBUG_PRINT("quit",("Unlocking LOCK_thread_count"));
    (void) pthread_mutex_unlock(&LOCK_thread_count);
  }
  net_end(&net);
  /* All threads has now been aborted */
  DBUG_PRINT("quit",("Waiting for threads to die (count=%u)",thread_count));
  (void) pthread_mutex_lock(&LOCK_thread_count);
  while (thread_count)
  {
    (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
    DBUG_PRINT("quit",("One thread died (count=%u)",thread_count));
  }
  (void) pthread_mutex_unlock(&LOCK_thread_count);

571
  mysql_log.close(1);
572
  mysql_slow_log.close(1);
573 574
  mysql_update_log.close(1);
  mysql_bin_log.close(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
575 576 577 578
  DBUG_PRINT("quit",("close_connections thread"));
  DBUG_VOID_RETURN;
}

579
static void close_server_sock()
580
{
581
#ifdef HAVE_CLOSE_SERVER_SOCK
582
  DBUG_ENTER("close_server_sock");
583 584 585
  my_socket tmp_sock;
  tmp_sock=ip_sock;
  if (tmp_sock != INVALID_SOCKET)
586 587
  {
    ip_sock=INVALID_SOCKET;
588 589 590
    DBUG_PRINT("info",("closing TCP/IP socket"));
    VOID(shutdown(tmp_sock,2));
    VOID(closesocket(tmp_sock));
591
  }
592 593
  tmp_sock=unix_sock;
  if (tmp_sock != INVALID_SOCKET)
594
  {
595
    unix_sock=INVALID_SOCKET;
596
    DBUG_PRINT("info",("closing Unix socket"));
597 598
    VOID(shutdown(tmp_sock,2));
    VOID(closesocket(tmp_sock));
599 600 601 602
    VOID(unlink(mysql_unix_port));
  }
  DBUG_VOID_RETURN;
#endif
603
}
604

bk@work.mysql.com's avatar
bk@work.mysql.com committed
605 606 607 608
void kill_mysql(void)
{
  DBUG_ENTER("kill_mysql");

609 610 611
#ifdef SIGNALS_DONT_BREAK_READ
  close_server_sock(); /* force accept to wake up */
#endif  
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
612

bk@work.mysql.com's avatar
bk@work.mysql.com committed
613
#if defined(__WIN__)
614
#if !defined(EMBEDDED_LIBRARY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
615 616 617 618 619
  {
    if (!SetEvent(hEventShutdown))
    {
      DBUG_PRINT("error",("Got error: %ld from SetEvent",GetLastError()));
    }
620 621 622 623 624 625
    /*
      or:
      HANDLE hEvent=OpenEvent(0, FALSE, "MySqlShutdown");
      SetEvent(hEventShutdown);
      CloseHandle(hEvent);
    */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
626
  }
627
#endif
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
628 629
#elif defined(OS2)
  pthread_cond_signal( &eventShutdown);		// post semaphore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
630
#elif defined(HAVE_PTHREAD_KILL)
631 632 633 634 635 636
  if (pthread_kill(signal_thread,SIGTERM))	/* End everything nicely */
  {
    DBUG_PRINT("error",("Got error %d from pthread_kill",errno)); /* purecov: inspected */
  }
#elif !defined(SIGNALS_DONT_BREAK_READ)
  kill(current_pid,SIGTERM);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
637
#endif
638 639 640 641 642 643
  DBUG_PRINT("quit",("After pthread_kill"));
  shutdown_in_progress=1;			// Safety if kill didn't work
#ifdef SIGNALS_DONT_BREAK_READ
  if (!abort_loop)
  {
    pthread_t tmp;
644
    abort_loop=1;
645 646 647 648
    if (pthread_create(&tmp,&connection_attrib, kill_server_thread,
			   (void*) 0))
      sql_print_error("Error: Can't create thread to kill server");
  }
649
#endif    
650
  DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
651 652 653 654 655
}


	/* Force server down. kill all connections and threads and exit */

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
656 657 658 659
#if defined(OS2)
extern "C" void kill_server(int sig_ptr)
#define RETURN_FROM_KILL_SERVER return
#elif !defined(__WIN__)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
660 661 662 663 664 665 666 667 668 669
static void *kill_server(void *sig_ptr)
#define RETURN_FROM_KILL_SERVER return 0
#else
static void __cdecl kill_server(int sig_ptr)
#define RETURN_FROM_KILL_SERVER return
#endif
{
  int sig=(int) (long) sig_ptr;			// This is passed a int
  DBUG_ENTER("kill_server");

670
  // if there is a signal during the kill in progress, ignore the other
bk@work.mysql.com's avatar
bk@work.mysql.com committed
671 672 673 674 675 676 677 678 679 680
  if (kill_in_progress)				// Safety
    RETURN_FROM_KILL_SERVER;
  kill_in_progress=TRUE;
  abort_loop=1;					// This should be set
  signal(sig,SIG_IGN);
  if (sig == MYSQL_KILL_SIGNAL || sig == 0)
    sql_print_error(ER(ER_NORMAL_SHUTDOWN),my_progname);
  else
    sql_print_error(ER(ER_GOT_SIGNAL),my_progname,sig); /* purecov: inspected */

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
681
#if defined(USE_ONE_SIGNAL_HAND) && !defined(__WIN__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
682 683 684 685 686 687 688 689 690 691 692 693 694
  my_thread_init();				// If this is a new thread
#endif
  close_connections();
  if (sig != MYSQL_KILL_SIGNAL && sig != 0)
    unireg_abort(1);				/* purecov: inspected */
  else
    unireg_end(0);
  pthread_exit(0);				/* purecov: deadcode */
  RETURN_FROM_KILL_SERVER;
}


#ifdef USE_ONE_SIGNAL_HAND
695
static pthread_handler_decl(kill_server_thread,arg __attribute__((unused)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
696 697 698 699 700 701 702 703
{
  my_thread_init();				// Initialize new thread
  kill_server(0);
  my_thread_end();				// Normally never reached
  return 0;
}
#endif

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
704 705 706 707 708
#if defined(__amiga__)
#undef sigset
#define sigset signal
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
709 710 711 712 713 714 715
static sig_handler print_signal_warning(int sig)
{
  sql_print_error("Warning: Got signal %d from thread %d",
		  sig,my_thread_id());
#ifdef DONT_REMEMBER_SIGNAL
  sigset(sig,print_signal_warning);		/* int. thread system calls */
#endif
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
716
#if !defined(__WIN__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
717 718 719 720 721 722 723 724 725
  if (sig == SIGALRM)
    alarm(2);					/* reschedule alarm */
#endif
}


void unireg_end(int signal_number __attribute__((unused)))
{
  clean_up();
726
  my_thread_end();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
727 728 729 730 731 732
  pthread_exit(0);				// Exit is in main thread
}


void unireg_abort(int exit_code)
{
733
  DBUG_ENTER("unireg_abort");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
734 735 736
  if (exit_code)
    sql_print_error("Aborting\n");
  clean_up(); /* purecov: inspected */
737
  DBUG_PRINT("quit",("done with cleanup in unireg_abort"));
738
  my_thread_end();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
739 740 741 742
  exit(exit_code); /* purecov: inspected */
}


743
void clean_up(bool print_message)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
744 745 746 747
{
  DBUG_PRINT("exit",("clean_up"));
  if (cleanup_done++)
    return; /* purecov: inspected */
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
748 749
  if (use_slave_mask)
    bitmap_free(&slave_error_mask);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750 751
  acl_free(1);
  grant_free();
752
  query_cache.destroy();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
753 754 755 756 757 758 759 760
  table_cache_free();
  hostname_cache_free();
  item_user_lock_free();
  lex_free();				/* Free some memory */
#ifdef HAVE_DLOPEN
  if (!opt_noacl)
    udf_free();
#endif
761
  (void) ha_panic(HA_PANIC_CLOSE);	/* close all tables and logs */
762
  end_key_cache();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
763 764 765
#ifdef USE_RAID
  end_raid();
#endif
766
#ifdef HAVE_OPENSSL
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
767 768 769 770
  my_free(opt_ssl_key,MYF(MY_ALLOW_ZERO_PTR));
  my_free(opt_ssl_cert,MYF(MY_ALLOW_ZERO_PTR));
  my_free(opt_ssl_ca,MYF(MY_ALLOW_ZERO_PTR));
  my_free(opt_ssl_capath,MYF(MY_ALLOW_ZERO_PTR));
771
  my_free(opt_ssl_cipher,MYF(MY_ALLOW_ZERO_PTR));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
772
  opt_ssl_key=opt_ssl_cert=opt_ssl_ca=opt_ssl_capath=0;
773
#endif /* HAVE_OPENSSL */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
774
  free_defaults(defaults_argv);
775
  my_free(charsets_list, MYF(MY_ALLOW_ZERO_PTR));
776 777
  my_free(allocated_mysql_tmpdir,MYF(MY_ALLOW_ZERO_PTR));
  my_free(slave_load_tmpdir,MYF(MY_ALLOW_ZERO_PTR));
778
  x_free(opt_bin_logname);
779
  x_free(opt_relay_logname);
780
  bitmap_free(&temp_pool);
781
  free_max_user_conn();
782
  end_slave_list();
783

784
#if !defined(__WIN__) && !defined(EMBEDDED_LIBRARY)
785 786
  if (!opt_bootstrap)
    (void) my_delete(pidfile_name,MYF(0));	// This may not always exist
787
#endif
788
  if (print_message && errmesg)
789 790
    sql_print_error(ER(ER_SHUTDOWN_COMPLETE),my_progname);
  x_free((gptr) my_errmsg[ERRMAPP]);	/* Free messages */
791
  DBUG_PRINT("quit", ("Error messages freed"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
792 793
  /* Tell main we are ready */
  (void) pthread_mutex_lock(&LOCK_thread_count);
794
  DBUG_PRINT("quit", ("got thread count lock"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
795
  ready_to_exit=1;
796
  /* do the broadcast inside the lock to ensure that my_end() is not called */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
797 798
  (void) pthread_cond_broadcast(&COND_thread_count);
  (void) pthread_mutex_unlock(&LOCK_thread_count);
799
  DBUG_PRINT("quit", ("done with cleanup"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
800 801 802 803 804 805 806 807 808 809 810
} /* clean_up */



/****************************************************************************
** Init IP and UNIX socket
****************************************************************************/

static void set_ports()
{
  char	*env;
811
  if (!mysql_port && !opt_disable_networking)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  {					// Get port if not from commandline
    struct  servent *serv_ptr;
    mysql_port = MYSQL_PORT;
    if ((serv_ptr = getservbyname("mysql", "tcp")))
      mysql_port = ntohs((u_short) serv_ptr->s_port); /* purecov: inspected */
    if ((env = getenv("MYSQL_TCP_PORT")))
      mysql_port = (uint) atoi(env);		/* purecov: inspected */
  }
  if (!mysql_unix_port)
  {
#ifdef __WIN__
    mysql_unix_port = (char*) MYSQL_NAMEDPIPE;
#else
    mysql_unix_port = (char*) MYSQL_UNIX_ADDR;
#endif
    if ((env = getenv("MYSQL_UNIX_PORT")))
      mysql_unix_port = env;			/* purecov: inspected */
  }
}

/* Change to run as another user if started with --user */

static void set_user(const char *user)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
836
#if !defined(__WIN__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    struct passwd *ent;

  // don't bother if we aren't superuser
  if (geteuid())
  {
    if (user)
      fprintf(stderr,
	      "Warning: One can only use the --user switch if running as root\n");
    return;
  }
  else if (!user)
  {
    if (!opt_bootstrap)
    {
      fprintf(stderr,"Fatal error: Please read \"Security\" section of the manual to find out how to run mysqld as root!\n");
      unireg_abort(1);
    }
    return;
  }
  if (!strcmp(user,"root"))
    return;				// Avoid problem with dynamic libraries

  if (!(ent = getpwnam(user)))
  {
    fprintf(stderr,"Fatal error: Can't change to run as user '%s' ;  Please check that the user exists!\n",user);
    unireg_abort(1);
  }
#ifdef HAVE_INITGROUPS
865
  initgroups((char*) user,ent->pw_gid);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
#endif
  if (setgid(ent->pw_gid) == -1)
  {
    sql_perror("setgid");
    unireg_abort(1);
  }
  if (setuid(ent->pw_uid) == -1)
  {
    sql_perror("setuid");
    unireg_abort(1);
  }
#endif
}

/* Change root user if started with  --chroot */

static void set_root(const char *path)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
884
#if !defined(__WIN__) && !defined(__EMX__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  if (chroot(path) == -1)
  {
    sql_perror("chroot");
    unireg_abort(1);
  }
#endif
}

static void server_init(void)
{
  struct sockaddr_in	IPaddr;
#ifdef HAVE_SYS_UN_H
  struct sockaddr_un	UNIXaddr;
#endif
  int	arg=1;
  DBUG_ENTER("server_init");

#ifdef	__WIN__
  if ( !opt_disable_networking )
  {
    WSADATA WsaData;
    if (SOCKET_ERROR == WSAStartup (0x0101, &WsaData))
    {
      my_message(0,"WSAStartup Failed\n",MYF(0));
      unireg_abort(1);
    }
  }
#endif /* __WIN__ */

  set_ports();

  if (mysql_port != 0 && !opt_disable_networking && !opt_bootstrap)
  {
    DBUG_PRINT("general",("IP Socket is %d",mysql_port));
    ip_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (ip_sock == INVALID_SOCKET)
    {
      DBUG_PRINT("error",("Got error: %d from socket()",socket_errno));
      sql_perror(ER(ER_IPSOCK_ERROR));		/* purecov: tested */
      unireg_abort(1);				/* purecov: tested */
    }
    bzero((char*) &IPaddr, sizeof(IPaddr));
    IPaddr.sin_family = AF_INET;
    IPaddr.sin_addr.s_addr = my_bind_addr;
    IPaddr.sin_port = (unsigned short) htons((unsigned short) mysql_port);
    (void) setsockopt(ip_sock,SOL_SOCKET,SO_REUSEADDR,(char*)&arg,sizeof(arg));
    for(;;)
    {
      if (bind(ip_sock, my_reinterpret_cast(struct sockaddr *) (&IPaddr),
	       sizeof(IPaddr)) >= 0)
	break;
      DBUG_PRINT("error",("Got error: %d from bind",socket_errno));
      sql_perror("Can't start server: Bind on TCP/IP port");/* Had a loop here */
      sql_print_error("Do you already have another mysqld server running on port: %d ?",mysql_port);
      unireg_abort(1);
    }
941 942
    if (listen(ip_sock,(int) back_log) < 0)
      sql_print_error("Warning:  listen() on TCP/IP failed with error %d",
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
943
		      socket_errno);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
  }

  if (mysqld_chroot)
    set_root(mysqld_chroot);

  set_user(mysqld_user); // set_user now takes care of mysqld_user==NULL

#ifdef __NT__
  /* create named pipe */
  if (Service.IsNT() && mysql_unix_port[0] && !opt_bootstrap)
  {
    sprintf( szPipeName, "\\\\.\\pipe\\%s", mysql_unix_port );
    ZeroMemory( &saPipeSecurity, sizeof(saPipeSecurity) );
    ZeroMemory( &sdPipeDescriptor, sizeof(sdPipeDescriptor) );
    if ( !InitializeSecurityDescriptor(&sdPipeDescriptor,
				       SECURITY_DESCRIPTOR_REVISION) )
    {
      sql_perror("Can't start server : Initialize security descriptor");
      unireg_abort(1);
    }
    if (!SetSecurityDescriptorDacl(&sdPipeDescriptor, TRUE, NULL, FALSE))
    {
      sql_perror("Can't start server : Set security descriptor");
      unireg_abort(1);
    }
    saPipeSecurity.nLength = sizeof( SECURITY_ATTRIBUTES );
    saPipeSecurity.lpSecurityDescriptor = &sdPipeDescriptor;
    saPipeSecurity.bInheritHandle = FALSE;
    if ((hPipe = CreateNamedPipe(szPipeName,
				 PIPE_ACCESS_DUPLEX,
				 PIPE_TYPE_BYTE |
				 PIPE_READMODE_BYTE |
				 PIPE_WAIT,
				 PIPE_UNLIMITED_INSTANCES,
				 (int) net_buffer_length,
				 (int) net_buffer_length,
				 NMPWAIT_USE_DEFAULT_WAIT,
				 &saPipeSecurity )) == INVALID_HANDLE_VALUE)
      {
	LPVOID lpMsgBuf;
	int error=GetLastError();
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
		      FORMAT_MESSAGE_FROM_SYSTEM,
		      NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		      (LPTSTR) &lpMsgBuf, 0, NULL );
	MessageBox( NULL, (LPTSTR) lpMsgBuf, "Error from CreateNamedPipe",
		    MB_OK|MB_ICONINFORMATION );
	LocalFree( lpMsgBuf );
	unireg_abort(1);
      }
  }
#endif

997
#if defined(HAVE_SYS_UN_H)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  /*
  ** Create the UNIX socket
  */
  if (mysql_unix_port[0] && !opt_bootstrap)
  {
    DBUG_PRINT("general",("UNIX Socket is %s",mysql_unix_port));

    if ((unix_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    {
      sql_perror("Can't start server : UNIX Socket "); /* purecov: inspected */
      unireg_abort(1);				/* purecov: inspected */
    }
    bzero((char*) &UNIXaddr, sizeof(UNIXaddr));
    UNIXaddr.sun_family = AF_UNIX;
    strmov(UNIXaddr.sun_path, mysql_unix_port);
    (void) unlink(mysql_unix_port);
    (void) setsockopt(unix_sock,SOL_SOCKET,SO_REUSEADDR,(char*)&arg,
		      sizeof(arg));
    umask(0);
    if (bind(unix_sock, my_reinterpret_cast(struct sockaddr *) (&UNIXaddr),
	     sizeof(UNIXaddr)) < 0)
    {
      sql_perror("Can't start server : Bind on unix socket"); /* purecov: tested */
      sql_print_error("Do you already have another mysqld server running on socket: %s ?",mysql_unix_port);
      unireg_abort(1);					/* purecov: tested */
    }
    umask(((~my_umask) & 0666));
#if defined(S_IFSOCK) && defined(SECURE_SOCKETS)
    (void) chmod(mysql_unix_port,S_IFSOCK);	/* Fix solaris 2.6 bug */
#endif
1028 1029
    if (listen(unix_sock,(int) back_log) < 0)
      sql_print_error("Warning:  listen() on Unix socket failed with error %d",
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1030
		      socket_errno);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  }
#endif
  DBUG_PRINT("info",("server started"));
  DBUG_VOID_RETURN;
}


void yyerror(const char *s)
{
  NET *net=my_pthread_getspecific_ptr(NET*,THR_NET);
  char *yytext=(char*) current_lex->tok_start;
  if (!strcmp(s,"parse error"))
    s=ER(ER_SYNTAX_ERROR);
  net_printf(net,ER_PARSE_ERROR, s, yytext ? (char*) yytext : "",
	     current_lex->yylineno);
}


void close_connection(NET *net,uint errcode,bool lock)
{
1051
  st_vio* vio;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
  DBUG_ENTER("close_connection");
  DBUG_PRINT("enter",("fd: %s  error: '%s'",
                    net->vio? vio_description(net->vio):"(not connected)",
                    errcode ? ER(errcode) : ""));
  if (lock)
    (void) pthread_mutex_lock(&LOCK_thread_count);
  if ((vio=net->vio) != 0)
  {
    if (errcode)
      send_error(net,errcode,ER(errcode));	/* purecov: inspected */
    vio_close(vio);			/* vio is freed in delete thd */
  }
  if (lock)
    (void) pthread_mutex_unlock(&LOCK_thread_count);
  DBUG_VOID_RETURN;
}

	/* Called when a thread is aborted */
	/* ARGSUSED */

sig_handler end_thread_signal(int sig __attribute__((unused)))
{
  THD *thd=current_thd;
  DBUG_ENTER("end_thread_signal");
  if (thd)
    end_thread(thd,0);
  DBUG_VOID_RETURN;				/* purecov: deadcode */
}


void end_thread(THD *thd, bool put_in_cache)
{
  DBUG_ENTER("end_thread");
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1085
  thd->cleanup();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1086 1087 1088
  (void) pthread_mutex_lock(&LOCK_thread_count);
  thread_count--;
  delete thd;
1089

1090 1091
  if (put_in_cache && cached_thread_count < thread_cache_size &&
      ! abort_loop && !kill_cached_threads)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  {
    /* Don't kill the thread, just put it in cache for reuse */
    DBUG_PRINT("info", ("Adding thread to cache"))
    cached_thread_count++;
    while (!abort_loop && ! wake_thread && ! kill_cached_threads)
      (void) pthread_cond_wait(&COND_thread_cache, &LOCK_thread_count);
    cached_thread_count--;
    if (kill_cached_threads)
      pthread_cond_signal(&COND_flush_thread_cache);
    if (wake_thread)
    {
      wake_thread--;
      thd=thread_cache.get();
1105
      thd->real_id=pthread_self();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1106
      (void) thd->store_globals();
1107
      threads.append(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1108 1109 1110 1111 1112 1113 1114 1115
      pthread_mutex_unlock(&LOCK_thread_count);
      DBUG_VOID_RETURN;
    }
  }

  DBUG_PRINT("info", ("sending a broadcast"))

  /* Tell main we are ready */
1116 1117
    // TODO: explain why we broadcast outside of the lock or
    // fix the bug - Sasha
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1118
  (void) pthread_mutex_unlock(&LOCK_thread_count);
1119
  (void) pthread_cond_broadcast(&COND_thread_count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1120
  DBUG_PRINT("info", ("unlocked thread_count mutex"))
1121
#ifdef ONE_THREAD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  if (!(test_flags & TEST_NO_THREADS))	// For debugging under Linux
#endif
  {
    my_thread_end();
    pthread_exit(0);
  }
  DBUG_VOID_RETURN;
}


/* Start a cached thread. LOCK_thread_count is locked on entry */

static void start_cached_thread(THD *thd)
{
  thread_cache.append(thd);
  wake_thread++;
  thread_count++;
  pthread_cond_signal(&COND_thread_cache);
}


void flush_thread_cache()
{
  (void) pthread_mutex_lock(&LOCK_thread_count);
  kill_cached_threads++;
  while (cached_thread_count)
  {
    pthread_cond_broadcast(&COND_thread_cache);
    pthread_cond_wait(&COND_flush_thread_cache,&LOCK_thread_count);
  }
  kill_cached_threads--;
  (void) pthread_mutex_unlock(&LOCK_thread_count);
}


	/*
	** Aborts a thread nicely. Commes here on SIGPIPE
	** TODO: One should have to fix that thr_alarm know about this
	** thread too
	*/

#ifdef THREAD_SPECIFIC_SIGPIPE
static sig_handler abort_thread(int sig __attribute__((unused)))
{
  THD *thd=current_thd;
  DBUG_ENTER("abort_thread");
  if (thd)
    thd->killed=1;
  DBUG_VOID_RETURN;
}
#endif

/******************************************************************************
** Setup a signal thread with handles all signals
** Because linux doesn't support scemas use a mutex to check that
** the signal thread is ready before continuing
******************************************************************************/

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1180
#if defined(__WIN__) || defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1181 1182 1183 1184 1185
static void init_signals(void)
{
  int signals[] = {SIGINT,SIGILL,SIGFPE,SIGSEGV,SIGTERM,SIGABRT } ;
  for (uint i=0 ; i < sizeof(signals)/sizeof(int) ; i++)
    signal( signals[i], kill_server) ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1186
#if defined(__WIN__)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1187
  signal(SIGBREAK,SIG_IGN);	//ignore SIGBREAK for NT
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1188 1189 1190
#else
  signal(SIGBREAK, kill_server);
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1191 1192
}

1193 1194 1195 1196
static void start_signal_handler(void)
{
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1197 1198 1199
#elif defined(__EMX__)
static void sig_reload(int signo)
{
1200
  reload_acl_and_cache((THD*) 0,REFRESH_LOG, (TABLE_LIST*) 0); // Flush everything
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
  signal(signo, SIG_ACK);
}

static void sig_kill(int signo)
{
  if (!abort_loop)
  {
    abort_loop=1;				// mark abort for threads
    kill_server((void*) signo);
  }
  signal(signo, SIG_ACK);
}

static void init_signals(void)
{
  signal(SIGQUIT, sig_kill);
  signal(SIGKILL, sig_kill);
  signal(SIGTERM, sig_kill);
  signal(SIGINT,  sig_kill);
  signal(SIGHUP,  sig_reload);	// Flush everything
  signal(SIGALRM, SIG_IGN);
  signal(SIGBREAK,SIG_IGN);
  signal_thread = pthread_self();
}
1225 1226 1227 1228 1229

static void start_signal_handler(void)
{
}

1230
#else /* if ! __WIN__ && ! __EMX__ */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1231

1232 1233
#ifdef HAVE_LINUXTHREADS
#define UNSAFE_DEFAULT_LINUX_THREADS 200
1234
#endif
1235 1236

static sig_handler handle_segfault(int sig)
1237
{
1238
  THD *thd=current_thd;
1239 1240 1241 1242 1243 1244
  /*
    Strictly speaking, one needs a mutex here
    but since we have got SIGSEGV already, things are a mess
    so not having the mutex is not as bad as possibly using a buggy
    mutex - so we keep things simple
  */
1245
  if (segfaulted)
1246 1247 1248 1249
  {
    fprintf(stderr, "Fatal signal %d while backtracing\n", sig);
    exit(1);
  }
1250
  
1251
  segfaulted = 1;
1252
  fprintf(stderr,"\
1253
mysqld got signal %d;\n\
1254
This could be because you hit a bug. It is also possible that this binary\n\
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1255
or one of the libraries it was linked against is corrupt, improperly built,\n\
1256 1257 1258 1259 1260
or misconfigured. This error can also be caused by malfunctioning hardware.\n",
	  sig);
  fprintf(stderr, "\
We will try our best to scrape up some info that will hopefully help diagnose\n\
the problem, but since we have already crashed, something is definitely wrong\n\
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1261
and this may fail.\n\n");
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  fprintf(stderr, "key_buffer_size=%ld\n", keybuff_size);
  fprintf(stderr, "record_buffer=%ld\n", my_default_record_cache_size);
  fprintf(stderr, "sort_buffer=%ld\n", sortbuff_size);
  fprintf(stderr, "max_used_connections=%ld\n", max_used_connections);
  fprintf(stderr, "max_connections=%ld\n", max_connections);
  fprintf(stderr, "threads_connected=%d\n", thread_count);
  fprintf(stderr, "It is possible that mysqld could use up to \n\
key_buffer_size + (record_buffer + sort_buffer)*max_connections = %ld K\n\
bytes of memory\n", (keybuff_size + (my_default_record_cache_size +
			     sortbuff_size) * max_connections)/ 1024);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1272
  fprintf(stderr, "Hope that's ok; if not, decrease some variables in the equation.\n\n");
1273
  
1274
#if defined(HAVE_LINUXTHREADS)
1275 1276 1277 1278
  if (sizeof(char*) == 4 && thread_count > UNSAFE_DEFAULT_LINUX_THREADS)
  {
    fprintf(stderr, "\
You seem to be running 32-bit Linux and have %d concurrent connections.\n\
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1279 1280
If you have not changed STACK_SIZE in LinuxThreads and built the binary \n\
yourself, LinuxThreads is quite likely to steal a part of the global heap for\n\
1281 1282 1283 1284
the thread stack. Please read http://www.mysql.com/doc/L/i/Linux.html\n\n",
	    thread_count);
  }
#endif /* HAVE_LINUXTHREADS */
1285

1286
#ifdef HAVE_STACKTRACE
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
1287
  if(!(test_flags & TEST_NO_STACKTRACE))
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1288
  {
1289
    fprintf(stderr,"thd=%p\n",thd);
1290 1291
    print_stacktrace(thd ? (gptr) thd->thread_stack : (gptr) 0,
		     thread_stack);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1292
  }
1293 1294 1295 1296 1297 1298
  if (thd)
  {
    fprintf(stderr, "Trying to get some variables.\n\
Some pointers may be invalid and cause the dump to abort...\n");
    safe_print_str("thd->query", thd->query, 1024);
    fprintf(stderr, "thd->thread_id=%ld\n", thd->thread_id);
1299
    fprintf(stderr, "\n\
1300 1301
Successfully dumped variables, if you ran with --log, take a look at the\n\
details of what thread %ld did to cause the crash.  In some cases of really\n\
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1302
bad corruption, the values shown above may be invalid.\n\n",
1303 1304 1305
	  thd->thread_id);
  }
  fprintf(stderr, "\
1306
The manual page at http://www.mysql.com/doc/C/r/Crashing.html contains\n\
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1307
information that should help you find out what is causing the crash.\n");
1308
  fflush(stderr);
1309 1310
#endif /* HAVE_STACKTRACE */

1311 1312
 if (test_flags & TEST_CORE_ON_SIGNAL)
   write_core(sig);
1313
 exit(1);
1314 1315
}

1316 1317 1318 1319 1320 1321
#ifndef SA_RESETHAND
#define SA_RESETHAND 0
#endif
#ifndef SA_NODEFER
#define SA_NODEFER 0
#endif
1322

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1323 1324 1325 1326 1327 1328 1329
static void init_signals(void)
{
  sigset_t set;
  DBUG_ENTER("init_signals");

  sigset(THR_KILL_SIGNAL,end_thread_signal);
  sigset(THR_SERVER_ALARM,print_signal_warning); // Should never be called!
1330

sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
1331
  if (!(test_flags & TEST_NO_STACKTRACE) || (test_flags & TEST_CORE_ON_SIGNAL))
1332
  {
1333 1334 1335 1336 1337
    struct sigaction sa;
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
    sigemptyset(&sa.sa_mask);
    sigprocmask(SIG_SETMASK,&sa.sa_mask,NULL);

1338
    init_stacktrace();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1339 1340 1341
#if defined(__amiga__)
    sa.sa_handler=(void(*)())handle_segfault;
#else
1342
    sa.sa_handler=handle_segfault;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1343
#endif
1344
    sigaction(SIGSEGV, &sa, NULL);
1345
#ifdef SIGBUS
1346
    sigaction(SIGBUS, &sa, NULL);
1347
#endif
1348 1349
    sigaction(SIGILL, &sa, NULL);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
  (void) sigemptyset(&set);
#ifdef THREAD_SPECIFIC_SIGPIPE
  sigset(SIGPIPE,abort_thread);
  sigaddset(&set,SIGPIPE);
#else
  (void) signal(SIGPIPE,SIG_IGN);		// Can't know which thread
  sigaddset(&set,SIGPIPE);
#endif
  sigaddset(&set,SIGINT);
  sigaddset(&set,SIGQUIT);
  sigaddset(&set,SIGTERM);
  sigaddset(&set,SIGHUP);
  signal(SIGTERM,SIG_DFL);			// If it's blocked by parent
1363
  signal(SIGHUP,SIG_DFL);			// If it's blocked by parent
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1364 1365 1366 1367 1368 1369 1370
#ifdef SIGTSTP
  sigaddset(&set,SIGTSTP);
#endif
  sigaddset(&set,THR_SERVER_ALARM);
  sigdelset(&set,THR_KILL_SIGNAL);		// May be SIGINT
  sigdelset(&set,THR_CLIENT_ALARM);		// For alarms
  (void) pthread_sigmask(SIG_SETMASK,&set,NULL);
1371 1372 1373 1374 1375 1376 1377 1378 1379
  DBUG_VOID_RETURN;
}


static void start_signal_handler(void)
{
  int error;
  pthread_attr_t thr_attr;
  DBUG_ENTER("start_signal_handler");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

  (void) pthread_attr_init(&thr_attr);
#if !defined(HAVE_DEC_3_2_THREADS)
  pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_SYSTEM);
  (void) pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
  if (!(opt_specialflag & SPECIAL_NO_PRIOR))
    my_pthread_attr_setprio(&thr_attr,INTERRUPT_PRIOR);
  pthread_attr_setstacksize(&thr_attr,32768);
#endif

  (void) pthread_mutex_lock(&LOCK_thread_count);
  if ((error=pthread_create(&signal_thread,&thr_attr,signal_hand,0)))
  {
    sql_print_error("Can't create interrupt-thread (error %d, errno: %d)",
		    error,errno);
    exit(1);
  }
  (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
  pthread_mutex_unlock(&LOCK_thread_count);

  (void) pthread_attr_destroy(&thr_attr);
  DBUG_VOID_RETURN;
}


/*
** This threads handles all signals and alarms
*/

/* ARGSUSED */
static void *signal_hand(void *arg __attribute__((unused)))
{
  sigset_t set;
  int sig;
  my_thread_init();				// Init new thread
  DBUG_ENTER("signal_hand");

  /* Setup alarm handler */
  init_thr_alarm(max_connections+max_insert_delayed_threads);
#if SIGINT != THR_KILL_SIGNAL
  (void) sigemptyset(&set);			// Setup up SIGINT for debug
  (void) sigaddset(&set,SIGINT);		// For debugging
  (void) pthread_sigmask(SIG_UNBLOCK,&set,NULL);
#endif
  (void) sigemptyset(&set);			// Setup up SIGINT for debug
#ifdef USE_ONE_SIGNAL_HAND
  (void) sigaddset(&set,THR_SERVER_ALARM);	// For alarms
#endif
  (void) sigaddset(&set,SIGQUIT);
  (void) sigaddset(&set,SIGTERM);
#if THR_CLIENT_ALARM != SIGHUP
  (void) sigaddset(&set,SIGHUP);
#endif
  (void) sigaddset(&set,SIGTSTP);

  /* Save pid to this process (or thread on Linux) */
1436
  if (!opt_bootstrap)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1437
  {
1438 1439
    File pidFile;
    if ((pidFile = my_create(pidfile_name,0664, O_WRONLY, MYF(MY_WME))) >= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1440
    {
1441 1442
      char buff[21];
      sprintf(buff,"%lu",(ulong) getpid());
1443
      (void) my_write(pidFile, buff,strlen(buff),MYF(MY_WME));
1444
      (void) my_close(pidFile,MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1445 1446
    }
  }
1447 1448 1449 1450 1451 1452 1453
#ifdef HAVE_STACK_TRACE_ON_SEGV
  if (opt_do_pstack)
  {
    sprintf(pstack_file_name,"mysqld-%lu-%%d-%%d.backtrace", (ulong)getpid());
    pstack_install_segv_action(pstack_file_name);
  }
#endif /* HAVE_STACK_TRACE_ON_SEGV */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1454

1455
  // signal to start_signal_handler that we are ready
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1456
  (void) pthread_mutex_lock(&LOCK_thread_count);
1457
  (void) pthread_cond_signal(&COND_thread_count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
  (void) pthread_mutex_unlock(&LOCK_thread_count);

  for (;;)
  {
    int error;					// Used when debugging
    if (shutdown_in_progress && !abort_loop)
    {
      sig=SIGTERM;
      error=0;
    }
    else
      while ((error=my_sigwait(&set,&sig)) == EINTR) ;
    if (cleanup_done)
      pthread_exit(0);				// Safety
    switch (sig) {
    case SIGTERM:
    case SIGQUIT:
    case SIGKILL:
#ifdef EXTRA_DEBUG
      sql_print_error("Got signal %d to shutdown mysqld",sig);
#endif
      DBUG_PRINT("info",("Got signal: %d  abort_loop: %d",sig,abort_loop));
      if (!abort_loop)
      {
	abort_loop=1;				// mark abort for threads
#ifdef USE_ONE_SIGNAL_HAND
	pthread_t tmp;
	if (!(opt_specialflag & SPECIAL_NO_PRIOR))
	  my_pthread_attr_setprio(&connection_attrib,INTERRUPT_PRIOR);
	if (pthread_create(&tmp,&connection_attrib, kill_server_thread,
			   (void*) sig))
	  sql_print_error("Error: Can't create thread to kill server");
#else
1491
	kill_server((void*) sig);	// MIT THREAD has a alarm thread
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1492 1493 1494 1495
#endif
      }
      break;
    case SIGHUP:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1496 1497 1498 1499
      reload_acl_and_cache((THD*) 0,
			   (REFRESH_LOG | REFRESH_TABLES | REFRESH_FAST |
			    REFRESH_STATUS | REFRESH_GRANT | REFRESH_THREADS |
			    REFRESH_HOSTS),
1500
			   (TABLE_LIST*) 0); // Flush logs
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
      mysql_print_status((THD*) 0);		// Send debug some info
      break;
#ifdef USE_ONE_SIGNAL_HAND
    case THR_SERVER_ALARM:
      process_alarm(sig);			// Trigger alarms.
      break;
#endif
    default:
#ifdef EXTRA_DEBUG
      sql_print_error("Warning: Got signal: %d, error: %d",sig,error); /* purecov: tested */
#endif
      break;					/* purecov: tested */
    }
  }
  return(0);					/* purecov: deadcode */
}

#endif	/* __WIN__*/


/*
** All global error messages are sent here where the first one is stored for
** the client
*/


/* ARGSUSED */
static int my_message_sql(uint error, const char *str,
			  myf MyFlags __attribute__((unused)))
{
  NET *net;
  DBUG_ENTER("my_message_sql");
  DBUG_PRINT("error",("Message: '%s'",str));
  if ((net=my_pthread_getspecific_ptr(NET*,THR_NET)))
  {
    if (!net->last_error[0])			// Return only first message
    {
      strmake(net->last_error,str,sizeof(net->last_error)-1);
      net->last_errno=error ? error : ER_UNKNOWN_ERROR;
    }
  }
  else
    sql_print_error("%s: %s",my_progname,str); /* purecov: inspected */
  DBUG_RETURN(0);
}

#ifdef __WIN__

struct utsname
{
  char nodename[FN_REFLEN];
};

int uname(struct utsname *a)
{
  return -1;
}
#endif


#ifdef __WIN__
pthread_handler_decl(handle_shutdown,arg)
{
  MSG msg;
  my_thread_init();

  /* this call should create the message queue for this thread */
  PeekMessage(&msg, NULL, 1, 65534,PM_NOREMOVE);
1569
#if !defined(EMBEDDED_LIBRARY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1570
  if (WaitForSingleObject(hEventShutdown,INFINITE)==WAIT_OBJECT_0)
1571
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
     kill_server(MYSQL_KILL_SIGNAL);
  return 0;
}

int __stdcall handle_kill(ulong ctrl_type)
{
  if (ctrl_type == CTRL_CLOSE_EVENT ||
      ctrl_type == CTRL_SHUTDOWN_EVENT)
  {
    kill_server(MYSQL_KILL_SIGNAL);
    return TRUE;
  }
  return FALSE;
}
#endif

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
#ifdef OS2
pthread_handler_decl(handle_shutdown,arg)
{
  my_thread_init();

  // wait semaphore
  pthread_cond_wait( &eventShutdown, NULL);

  // close semaphore and kill server
  pthread_cond_destroy( &eventShutdown);

1599 1600 1601 1602
  /*
    Exit main loop on main thread, so kill will be done from
    main thread (this is thread 2)
  */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
  abort_loop = 1;

  // unblock select()
  so_cancel( ip_sock);
  so_cancel( unix_sock);

  return 0;
}
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1613 1614 1615 1616 1617 1618
const char *load_default_groups[]= { "mysqld","server",0 };

#ifdef HAVE_LIBWRAP
char *libwrapName=NULL;
#endif

1619
void open_log(MYSQL_LOG *log, const char *hostname,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1620
		     const char *opt_name, const char *extension,
1621 1622
		     enum_log_type type, bool read_append,
	             bool no_auto_events)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1623 1624 1625 1626
{
  char tmp[FN_REFLEN];
  if (!opt_name || !opt_name[0])
  {
1627 1628 1629
    /*
      TODO: The following should be using fn_format();  We just need to
      first change fn_format() to cut the file name if it's too long.
1630 1631
    */
    strmake(tmp,hostname,FN_REFLEN-5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1632 1633 1634
    strmov(strcend(tmp,'.'),extension);
    opt_name=tmp;
  }
1635 1636 1637
  // get rid of extention if the log is binary to avoid problems
  if (type == LOG_BIN)
  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1638
    char *p = fn_ext(opt_name);
1639
    if (p)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1640 1641 1642 1643 1644
    {
      uint length=(uint) (p-opt_name);
      strmake(tmp,opt_name,min(length,FN_REFLEN));
      opt_name=tmp;
    }
1645
  }
1646 1647
  log->open(opt_name,type,0,(read_append) ? SEQ_READ_APPEND : WRITE_CACHE,
	    no_auto_events);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
}



#ifdef __WIN__
int win_main(int argc, char **argv)
#else
int main(int argc, char **argv)
#endif
{
  DEBUGGER_OFF;

  my_umask=0660;		// Default umask for new files
  my_umask_dir=0700;		// Default umask for new directories
  MY_INIT(argv[0]);		// init my_sys library & pthreads
  tzset();			// Set tzname

  start_time=time((time_t*) 0);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1666 1667 1668 1669 1670 1671 1672 1673
#ifdef OS2
  {
    // fix timezone for daylight saving
    struct tm *ts = localtime(&start_time);
    if (ts->tm_isdst > 0)
      _timezone -= 3600;
  }
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1674 1675 1676 1677 1678
#ifdef HAVE_TZNAME
#if defined(HAVE_LOCALTIME_R) && defined(_REENTRANT)
  {
    struct tm tm_tmp;
    localtime_r(&start_time,&tm_tmp);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1679
    strmov(time_zone,tzname[tm_tmp.tm_isdst != 0 ? 1 : 0]);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1680 1681 1682 1683 1684
  }
#else
  {
    struct tm *start_tm;
    start_tm=localtime(&start_time);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1685
    strmov(time_zone,tzname[start_tm->tm_isdst != 0 ? 1 : 0]);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1686 1687 1688 1689
  }
#endif
#endif

1690 1691 1692
  if (gethostname(glob_hostname,sizeof(glob_hostname)-4) < 0)
    strmov(glob_hostname,"mysql");
  strmov(pidfile_name,glob_hostname);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1693 1694
  strmov(strcend(pidfile_name,'.'),".pid");	// Add extension
#ifndef DBUG_OFF
1695 1696 1697
  strxmov(strend(server_version),MYSQL_SERVER_SUFFIX,"-debug",NullS);
#else
  strmov(strend(server_version),MYSQL_SERVER_SUFFIX);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
#endif
#ifdef _CUSTOMSTARTUPCONFIG_
  if (_cust_check_startup())
  {
    /* _cust_check_startup will report startup failure error */
    exit( 1 );
  }
#endif
  load_defaults("my",load_default_groups,&argc,&argv);
  defaults_argv=argv;
  mysql_tmpdir=getenv("TMPDIR");	/* Use this if possible */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1709
#if defined( __WIN__) || defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
  if (!mysql_tmpdir)
    mysql_tmpdir=getenv("TEMP");
  if (!mysql_tmpdir)
    mysql_tmpdir=getenv("TMP");
#endif
  if (!mysql_tmpdir || !mysql_tmpdir[0])
    mysql_tmpdir=(char*) P_tmpdir;		/* purecov: inspected */

  set_options();
  get_options(argc,argv);
  if (opt_log || opt_update_log || opt_slow_log || opt_bin_log)
    strcat(server_version,"-log");
  DBUG_PRINT("info",("%s  Ver %s for %s on %s\n",my_progname,
		     server_version, SYSTEM_TYPE,MACHINE_TYPE));

  /* These must be set early */

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
  (void) pthread_mutex_init(&LOCK_mysql_create_db,MY_MUTEX_INIT_SLOW);
  (void) pthread_mutex_init(&LOCK_Acl,MY_MUTEX_INIT_SLOW);
  (void) pthread_mutex_init(&LOCK_grant,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_open,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_mapped_file,MY_MUTEX_INIT_SLOW);
  (void) pthread_mutex_init(&LOCK_status,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_error_log,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_delayed_insert,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_delayed_status,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_delayed_create,MY_MUTEX_INIT_SLOW);
  (void) pthread_mutex_init(&LOCK_manager,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_crypt,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_bytes_sent,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_bytes_received,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_timezone,MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_server_id, MY_MUTEX_INIT_FAST);
  (void) pthread_mutex_init(&LOCK_user_conn, MY_MUTEX_INIT_FAST);
1745
  (void) pthread_mutex_init(&LOCK_rpl_status, MY_MUTEX_INIT_FAST);
1746
  (void) pthread_mutex_init(&LOCK_active_mi, MY_MUTEX_INIT_FAST);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1747 1748 1749 1750
  (void) pthread_cond_init(&COND_thread_count,NULL);
  (void) pthread_cond_init(&COND_refresh,NULL);
  (void) pthread_cond_init(&COND_thread_cache,NULL);
  (void) pthread_cond_init(&COND_flush_thread_cache,NULL);
1751
  (void) pthread_cond_init(&COND_manager,NULL);
1752
  (void) pthread_cond_init(&COND_rpl_status, NULL);
1753
  init_signals();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1754 1755

  if (set_default_charset_by_name(default_charset, MYF(MY_WME)))
1756
    exit( 1 );
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1757 1758 1759 1760 1761
  charsets_list = list_charsets(MYF(MY_COMPILED_SETS|MY_CONFIG_SETS));

#ifdef HAVE_OPENSSL
  if (opt_use_ssl)
  {
1762
    ssl_acceptor_fd = new_VioSSLAcceptorFd(opt_ssl_key, opt_ssl_cert,
1763
			   opt_ssl_ca, opt_ssl_capath, opt_ssl_cipher);
1764
    DBUG_PRINT("info",("ssl_acceptor_fd: %p",ssl_acceptor_fd));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1765
    if (!ssl_acceptor_fd)
1766 1767
      opt_use_ssl = 0;
    /* having ssl_acceptor_fd != 0 signals the use of SSL */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1768
  }
1769 1770
  if (des_key_file)
    load_des_key_file(des_key_file);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
#endif /* HAVE_OPENSSL */

#ifdef HAVE_LIBWRAP
  libwrapName= my_progname+dirname_length(my_progname);
  openlog(libwrapName, LOG_PID, LOG_AUTH);
#endif

  if (!(opt_specialflag & SPECIAL_NO_PRIOR))
    my_pthread_setprio(pthread_self(),CONNECT_PRIOR);
  /* Parameter for threads created for connections */
  (void) pthread_attr_init(&connection_attrib);
  (void) pthread_attr_setdetachstate(&connection_attrib,
				     PTHREAD_CREATE_DETACHED);
  pthread_attr_setstacksize(&connection_attrib,thread_stack);

  if (!(opt_specialflag & SPECIAL_NO_PRIOR))
    my_pthread_attr_setprio(&connection_attrib,WAIT_PRIOR);
  pthread_attr_setscope(&connection_attrib, PTHREAD_SCOPE_SYSTEM);

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1790
#if defined( SET_RLIMIT_NOFILE) || defined( OS2)
1791
  /* connections and databases needs lots of files */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1792 1793 1794
  {
    uint wanted_files=10+(uint) max(max_connections*5,
				    max_connections+table_cache_size*2);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1795 1796
    set_if_bigger(wanted_files, open_files_limit);
    // Note that some system returns 0 if we succeed here:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1797
    uint files=set_maximum_open_files(wanted_files);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1798
    if (files && files < wanted_files && ! open_files_limit)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1799
    {
1800 1801
      max_connections=	(ulong) min((files-10),max_connections);
      table_cache_size= (ulong) max((files-10-max_connections)/2,64);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
      DBUG_PRINT("warning",
		 ("Changed limits: max_connections: %ld  table_cache: %ld",
		  max_connections,table_cache_size));
      sql_print_error("Warning: Changed limits: max_connections: %ld  table_cache: %ld",max_connections,table_cache_size);
    }
  }
#endif
  unireg_init(opt_specialflag); /* Set up extern variabels */
  init_errmessage();		/* Read error messages from file */
  lex_init();
  item_init();
  mysys_uses_curses=0;
#ifdef USE_REGEX
  regex_init();
#endif
  select_thread=pthread_self();
  select_thread_in_use=1;
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
1819
  if (use_temp_pool && bitmap_init(&temp_pool,1024,1))
1820
    unireg_abort(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834

  /*
  ** We have enough space for fiddling with the argv, continue
  */
  umask(((~my_umask) & 0666));
  if (my_setwd(mysql_real_data_home,MYF(MY_WME)))
  {
    unireg_abort(1);				/* purecov: inspected */
  }
  mysql_data_home[0]=FN_CURLIB;		// all paths are relative from here
  mysql_data_home[1]=0;
  server_init();
  table_cache_init();
  hostname_cache_init();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1835 1836
  query_cache.result_size_limit(query_cache_limit);
  query_cache.resize(query_cache_size);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1837 1838 1839
  randominit(&sql_rand,(ulong) start_time,(ulong) start_time/2);
  reset_floating_point_exceptions();
  init_thr_lock();
1840
  init_slave_list();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1841

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1842 1843
  /* Setup log files */
  if (opt_log)
1844
    open_log(&mysql_log, glob_hostname, opt_logname, ".log", LOG_NORMAL);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1845
  if (opt_update_log)
1846
  {
1847
    open_log(&mysql_update_log, glob_hostname, opt_update_logname, "",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1848
	     LOG_NEW);
1849 1850
    using_update_log=1;
  }
1851 1852 1853
 
  init_slave();
  
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
1854
  if (opt_bin_log && !server_id)
1855 1856 1857
  {
    server_id= !master_host ? 1 : 2;
    switch (server_id) {
1858
#ifdef EXTRA_DEBUG
1859 1860
    case 1:
      sql_print_error("\
1861 1862 1863
Warning: You have enabled the binary log, but you haven't set server-id:\n\
Updates will be logged to the binary log, but connections to slaves will\n\
not be accepted.");
1864
      break;
1865 1866
#endif
    case 2:
1867
      sql_print_error("\
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
1868
Warning: You should set server-id to a non-0 value if master_host is set.\n\
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1869
The server will not act as a slave.");
1870
      break;
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
1871
    }
1872
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1873 1874
  if (opt_bin_log)
  {
1875 1876 1877
    if (!opt_bin_logname)
    {
      char tmp[FN_REFLEN];
1878 1879 1880 1881
      /* TODO: The following should be using fn_format();  We just need to
	 first change fn_format() to cut the file name if it's too long.
      */
      strmake(tmp,glob_hostname,FN_REFLEN-5);
1882 1883 1884 1885 1886 1887
      strmov(strcend(tmp,'.'),"-bin");
      opt_bin_logname=my_strdup(tmp,MYF(MY_WME));
    }
    mysql_bin_log.set_index_file_name(opt_binlog_index_name);
    open_log(&mysql_bin_log, glob_hostname, opt_bin_logname, "-bin",
	     LOG_BIN);
1888
    using_update_log=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1889
  }
1890

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1891
  if (opt_slow_log)
1892
    open_log(&mysql_slow_log, glob_hostname, opt_slow_logname, "-slow.log",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1893 1894 1895 1896 1897 1898
	     LOG_NORMAL);
  if (ha_init())
  {
    sql_print_error("Can't init databases");
    exit(1);
  }
1899
  ha_key_cache();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1900
#if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT)
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
  if (locked_in_memory && !geteuid())
  {
    if (mlockall(MCL_CURRENT))
    {
      sql_print_error("Warning: Failed to lock memory. Errno: %d\n",errno);
    }
    else
      locked_in_memory=1;
  }
#else
  locked_in_memory=0;
1912
#endif
1913

1914 1915
  if (opt_myisam_log)
    (void) mi_log( 1 );
1916
  ft_init_stopwords(ft_precompiled_stopwords);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938

#ifdef __WIN__
#define MYSQL_ERR_FILE "mysql.err"
  if (!opt_console)
  {
    freopen(MYSQL_ERR_FILE,"a+",stdout);
    freopen(MYSQL_ERR_FILE,"a+",stderr);
    FreeConsole();				// Remove window
  }
#endif

  /*
    init signals & alarm
    After this we can't quit by a simple unireg_abort
  */
  error_handler_hook = my_message_sql;
  if (pthread_key_create(&THR_THD,NULL) || pthread_key_create(&THR_NET,NULL) ||
      pthread_key_create(&THR_MALLOC,NULL))
  {
    sql_print_error("Can't create thread-keys");
    exit(1);
  }
1939
  start_signal_handler();				// Creates pidfile
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1940 1941 1942 1943
  if (acl_init(opt_noacl))
  {
    select_thread_in_use=0;
    (void) pthread_kill(signal_thread,MYSQL_KILL_SIGNAL);
1944
#ifndef __WIN__
1945
    if (!opt_bootstrap)
1946
      (void) my_delete(pidfile_name,MYF(MY_WME));	// Not needed anymore
1947
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1948 1949 1950 1951
    exit(1);
  }
  if (!opt_noacl)
    (void) grant_init();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1952
  init_max_user_conn();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

#ifdef HAVE_DLOPEN
  if (!opt_noacl)
    udf_init();
#endif

  if (opt_bootstrap)
  {
    int error=bootstrap(stdin);
    end_thr_alarm();				// Don't allow alarms
    unireg_abort(error ? 1 : 0);
  }
  if (opt_init_file)
  {
    if (read_init_file(opt_init_file))
    {
      end_thr_alarm();				// Don't allow alarms
      unireg_abort(1);
    }
  }
  (void) thr_setconcurrency(concurrency);	// 10 by default
1974
#if defined(__WIN__) && !defined(EMBEDDED_LIBRARY)	  //IRENA
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
  {
    hEventShutdown=CreateEvent(0, FALSE, FALSE, "MySqlShutdown");
    pthread_t hThread;
    if (pthread_create(&hThread,&connection_attrib,handle_shutdown,0))
      sql_print_error("Warning: Can't create thread to handle shutdown requests");

    // On "Stop Service" we have to do regular shutdown
    Service.SetShutdownEvent(hEventShutdown);
  }
#endif
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1985 1986 1987 1988 1989 1990 1991 1992
#ifdef OS2
  {
    pthread_cond_init( &eventShutdown, NULL);
    pthread_t hThread;
    if (pthread_create(&hThread,&connection_attrib,handle_shutdown,0))
      sql_print_error("Warning: Can't create thread to handle shutdown requests");
  }
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1993

1994
  if (
1995
#ifdef HAVE_BERKELEY_DB
1996
      !berkeley_skip ||
1997
#endif
1998
      (flush_time && flush_time != ~(ulong) 0L))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1999 2000
  {
    pthread_t hThread;
2001 2002
    if (pthread_create(&hThread,&connection_attrib,handle_manager,0))
      sql_print_error("Warning: Can't create thread to manage maintenance");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
  }

  printf(ER(ER_READY),my_progname,server_version,"");
  fflush(stdout);

#ifdef __NT__
  if (hPipe == INVALID_HANDLE_VALUE && !have_tcpip)
  {
    sql_print_error("TCP/IP must be installed on Win98 platforms");
  }
  else
  {
    pthread_mutex_lock(&LOCK_thread_count);
    (void) pthread_cond_init(&COND_handler_count,NULL);
    {
      pthread_t hThread;
      handler_count=0;
      if ( hPipe != INVALID_HANDLE_VALUE )
      {
	handler_count++;
	if (pthread_create(&hThread,&connection_attrib,
			   handle_connections_namedpipes, 0))
	{
	  sql_print_error("Warning: Can't create thread to handle named pipes");
	  handler_count--;
	}
      }
2030
      if (have_tcpip && !opt_disable_networking)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
      {
	handler_count++;
	if (pthread_create(&hThread,&connection_attrib,
			   handle_connections_sockets, 0))
	{
	  sql_print_error("Warning: Can't create thread to handle named pipes");
	  handler_count--;
	}
      }
      while (handler_count > 0)
      {
	pthread_cond_wait(&COND_handler_count,&LOCK_thread_count);
      }
    }
    pthread_mutex_unlock(&LOCK_thread_count);
  }
#else
  handle_connections_sockets(0);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
2049
#ifdef EXTRA_DEBUG2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2050 2051 2052 2053 2054 2055 2056 2057 2058
  sql_print_error("Exiting main thread");
#endif
#endif /* __NT__ */

  /* (void) pthread_attr_destroy(&connection_attrib); */

  DBUG_PRINT("quit",("Exiting main thread"));

#ifndef __WIN__
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
2059
#ifdef EXTRA_DEBUG2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2060 2061 2062
  sql_print_error("Before Lock_thread_count");
#endif
  (void) pthread_mutex_lock(&LOCK_thread_count);
2063
  DBUG_PRINT("quit", ("Got thread_count mutex"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2064 2065 2066
  select_thread_in_use=0;			// For close_connections
  (void) pthread_cond_broadcast(&COND_thread_count);
  (void) pthread_mutex_unlock(&LOCK_thread_count);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
2067
#ifdef EXTRA_DEBUG2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2068 2069 2070
  sql_print_error("After lock_thread_count");
#endif
#else
2071
#if !defined(EMBEDDED_LIBRARY)
monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
2072 2073 2074 2075
  if (Service.IsNT())
  {
    if(start_mode)
    {
2076
      if (WaitForSingleObject(hEventShutdown,1000)==WAIT_TIMEOUT)
monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
        Service.Stop();
    }
    else
    {
      Service.SetShutdownEvent(0);
      if(hEventShutdown) CloseHandle(hEventShutdown);
    }
  }
  else
  {
    Service.SetShutdownEvent(0);
    if(hEventShutdown) CloseHandle(hEventShutdown);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2090
#endif
2091
#endif
2092
#ifdef HAVE_OPENSSL
2093
  my_free((gptr)ssl_acceptor_fd,MYF(MY_ALLOW_ZERO_PTR));
2094
#endif /* HAVE_OPENSSL */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2095 2096
  /* Wait until cleanup is done */
  (void) pthread_mutex_lock(&LOCK_thread_count);
2097
  DBUG_PRINT("quit", ("Got thread_count mutex for clean up wait"));
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2098

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2099 2100
  while (!ready_to_exit)
  {
2101
    DBUG_PRINT("quit", ("not yet ready to exit"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2102 2103
    pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
  }
2104
  DBUG_PRINT("quit", ("ready to exit"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2105
  (void) pthread_mutex_unlock(&LOCK_thread_count);
2106
  my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2107 2108 2109 2110 2111
  exit(0);
  return(0);					/* purecov: deadcode */
}


2112
#if defined(__WIN__) && !defined(EMBEDDED_LIBRARY)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
/* ------------------------------------------------------------------------
   main and thread entry function for Win32
   (all this is needed only to run mysqld as a service on WinNT)
 -------------------------------------------------------------------------- */
int mysql_service(void *p)
{
  win_main(Service.my_argc, Service.my_argv);
  return 0;
}

int main(int argc, char **argv)
{
  // check  environment variable OS
  if (Service.GetOS())	// "OS" defined; Should be NT
  {
    if (argc == 2)
    {
2130 2131 2132 2133
      char path[FN_REFLEN];
      my_path(path, argv[0], "");		   // Find name in path
      fn_format(path,argv[0],path,"",1+4+16);    // Force use of full path

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2134 2135
      if (!strcmp(argv[1],"-install") || !strcmp(argv[1],"--install"))
      {
2136 2137 2138 2139 2140 2141
	Service.Install(1,MYSQL_SERVICENAME,MYSQL_SERVICENAME,path);
	return 0;
      }
      else if (!strcmp(argv[1],"-install-manual") || !strcmp(argv[1],"--install-manual"))
      {
        Service.Install(0,MYSQL_SERVICENAME,MYSQL_SERVICENAME,path);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
	return 0;
      }
      else if (!strcmp(argv[1],"-remove") || !strcmp(argv[1],"--remove"))
      {
	Service.Remove(MYSQL_SERVICENAME);
	return 0;
      }
    }
    else if (argc == 1)		   // No arguments; start as a service
    {
      // init service
monty@donna.mysql.fi's avatar
monty@donna.mysql.fi committed
2153
      start_mode = 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2154 2155 2156 2157 2158
      long tmp=Service.Init(MYSQL_SERVICENAME,mysql_service);
      return 0;
    }
  }

2159 2160 2161 2162 2163
  /*
    This is a WIN95 machine or a start of mysqld as a standalone program
    we have to pass the arguments, in case of NT-service this will be done
    by ServiceMain()
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179

  Service.my_argc=argc;
  Service.my_argv=argv;
  mysql_service(NULL);
  return 0;
}
/* ------------------------------------------------------------------------ */
#endif


static int bootstrap(FILE *file)
{
  THD *thd= new THD;
  int error;
  thd->bootstrap=1;
  thd->client_capabilities=0;
2180
  my_net_init(&thd->net,(st_vio*) 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2181 2182
  thd->max_packet_length=thd->net.max_packet;
  thd->master_access= ~0;
2183
  thd->thread_id=thread_id++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2184
  thread_count++;
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201

  bootstrap_file=file;
  if (pthread_create(&thd->real_id,&connection_attrib,handle_bootstrap,
		     (void*) thd))
  {
    sql_print_error("Warning: Can't create thread to handle bootstrap");    
    return -1;
  }
  /* Wait for thread to die */
  (void) pthread_mutex_lock(&LOCK_thread_count);
  while (thread_count)
  {
    (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
    DBUG_PRINT("quit",("One thread died (count=%u)",thread_count));
  }
  (void) pthread_mutex_unlock(&LOCK_thread_count);
  error= thd->fatal_error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2202
  net_end(&thd->net);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2203
  thd->cleanup();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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 2231 2232
  delete thd;
  return error;
}

static bool read_init_file(char *file_name)
{
  FILE *file;
  DBUG_ENTER("read_init_file");
  DBUG_PRINT("enter",("name: %s",file_name));
  if (!(file=my_fopen(file_name,O_RDONLY,MYF(MY_WME))))
    return(1);
  bootstrap(file);				/* Ignore errors from this */
  (void) my_fclose(file,MYF(MY_WME));
  return 0;
}


static void create_new_thread(THD *thd)
{
  DBUG_ENTER("create_new_thread");

  NET *net=&thd->net;				// For easy ref
  net->timeout = (uint) connect_timeout;	// Timeout for read
  if (protocol_version > 9)
    net->return_errno=1;

  /* don't allow too many connections */
  if (thread_count - delayed_insert_threads >= max_connections+1 || abort_loop)
  {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2233
    DBUG_PRINT("error",("Too many connections"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
    close_connection(net,ER_CON_COUNT_ERROR);
    delete thd;
    DBUG_VOID_RETURN;
  }
  if (pthread_mutex_lock(&LOCK_thread_count))
  {
    DBUG_PRINT("error",("Can't lock LOCK_thread_count"));
    close_connection(net,ER_OUT_OF_RESOURCES);
    delete thd;
    DBUG_VOID_RETURN;
  }
  if (thread_count-delayed_insert_threads > max_used_connections)
    max_used_connections=thread_count-delayed_insert_threads;
  thd->thread_id=thread_id++;
  for (uint i=0; i < 8 ; i++)			// Generate password teststring
    thd->scramble[i]= (char) (rnd(&sql_rand)*94+33);
  thd->scramble[8]=0;
  thd->rand=sql_rand;
  thd->real_id=pthread_self();			// Keep purify happy

  /* Start a new thread to handle connection */
2255
#ifdef ONE_THREAD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
  if (test_flags & TEST_NO_THREADS)		// For debugging under Linux
  {
    thread_cache_size=0;			// Safety
    thread_count++;
    threads.append(thd);
    thd->real_id=pthread_self();
    (void) pthread_mutex_unlock(&LOCK_thread_count);
    handle_one_connection((void*) thd);
  }
  else
#endif
  {
    if (cached_thread_count > wake_thread)
    {
      start_cached_thread(thd);
      (void) pthread_mutex_unlock(&LOCK_thread_count);
    }
    else
    {
      int error;
      thread_count++;
2277
      thread_created++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
      threads.append(thd);
      DBUG_PRINT("info",(("creating thread %d"), thd->thread_id));
      thd->connect_time = time(NULL);
      if ((error=pthread_create(&thd->real_id,&connection_attrib,
				handle_one_connection,
				(void*) thd)))
      {
	DBUG_PRINT("error",
		   ("Can't create thread to handle request (error %d)",
		    error));
	thread_count--;
	thd->killed=1;				// Safety
	(void) pthread_mutex_unlock(&LOCK_thread_count);
	net_printf(net,ER_CANT_CREATE_THREAD,error);
	(void) pthread_mutex_lock(&LOCK_thread_count);
	close_connection(net,0,0);
	delete thd;
	(void) pthread_mutex_unlock(&LOCK_thread_count);
	DBUG_VOID_RETURN;
      }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2298

sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
2299
      (void) pthread_mutex_unlock(&LOCK_thread_count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2300 2301
    }
  }
2302
  DBUG_PRINT("info",("Thread created"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2303 2304 2305
  DBUG_VOID_RETURN;
}

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
#ifdef SIGNALS_DONT_BREAK_READ
inline void kill_broken_server()
{
  /* hack to get around signals ignored in syscalls for problem OS's */
  if (unix_sock == INVALID_SOCKET || ip_sock ==INVALID_SOCKET)
  {
    select_thread_in_use = 0;
    kill_server((void*)MYSQL_KILL_SIGNAL); /* never returns */
  }
}
#define MAYBE_BROKEN_SYSCALL kill_broken_server();
#else
#define MAYBE_BROKEN_SYSCALL
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331

	/* Handle new connections and spawn new process to handle them */

pthread_handler_decl(handle_connections_sockets,arg __attribute__((unused)))
{
  my_socket sock,new_sock;
  uint error_count=0;
  uint max_used_connection= (uint) (max(ip_sock,unix_sock)+1);
  fd_set readFDs,clientFDs;
  THD *thd;
  struct sockaddr_in cAddr;
  int ip_flags=0,socket_flags=0,flags;
2332
  st_vio *vio_tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
  DBUG_ENTER("handle_connections_sockets");

  LINT_INIT(new_sock);

  (void) my_pthread_getprio(pthread_self());		// For debugging

  FD_ZERO(&clientFDs);
  if (ip_sock != INVALID_SOCKET)
  {
    FD_SET(ip_sock,&clientFDs);
#ifdef HAVE_FCNTL
    ip_flags = fcntl(ip_sock, F_GETFL, 0);
#endif
  }
#ifdef HAVE_SYS_UN_H
  FD_SET(unix_sock,&clientFDs);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2349
#ifdef HAVE_FCNTL
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2350
  socket_flags=fcntl(unix_sock, F_GETFL, 0);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2351
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2352 2353 2354
#endif

  DBUG_PRINT("general",("Waiting for connections."));
2355
  MAYBE_BROKEN_SYSCALL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2356 2357 2358 2359 2360 2361 2362 2363 2364
  while (!abort_loop)
  {
    readFDs=clientFDs;
#ifdef HPUX
    if (select(max_used_connection,(int*) &readFDs,0,0,0) < 0)
      continue;
#else
    if (select((int) max_used_connection,&readFDs,0,0,0) < 0)
    {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2365
      if (socket_errno != SOCKET_EINTR)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2366 2367
      {
	if (!select_errors++ && !abort_loop)	/* purecov: inspected */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2368
	  sql_print_error("mysqld: Got error %d from select",socket_errno); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2369
      }
2370
      MAYBE_BROKEN_SYSCALL
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2371 2372 2373 2374
      continue;
    }
#endif	/* HPUX */
    if (abort_loop)
2375 2376
    {
      MAYBE_BROKEN_SYSCALL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2377
      break;
2378
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
    /*
    ** Is this a new connection request
    */

#ifdef HAVE_SYS_UN_H
    if (FD_ISSET(unix_sock,&readFDs))
    {
      sock = unix_sock;
      flags= socket_flags;
    }
    else
#endif
    {
      sock = ip_sock;
      flags= ip_flags;
    }

#if !defined(NO_FCNTL_NONBLOCK)
    if (!(test_flags & TEST_BLOCKING))
    {
#if defined(O_NONBLOCK)
      fcntl(sock, F_SETFL, flags | O_NONBLOCK);
#elif defined(O_NDELAY)
      fcntl(sock, F_SETFL, flags | O_NDELAY);
#endif
    }
#endif /* NO_FCNTL_NONBLOCK */
    for (uint retry=0; retry < MAX_ACCEPT_RETRY; retry++)
    {
      size_socket length=sizeof(struct sockaddr_in);
      new_sock = accept(sock, my_reinterpret_cast(struct sockaddr *) (&cAddr),
			&length);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2411 2412
      if (new_sock != INVALID_SOCKET ||
	  (socket_errno != SOCKET_EINTR && socket_errno != SOCKET_EAGAIN))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2413
	break;
2414
      MAYBE_BROKEN_SYSCALL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
#if !defined(NO_FCNTL_NONBLOCK)
      if (!(test_flags & TEST_BLOCKING))
      {
	if (retry == MAX_ACCEPT_RETRY - 1)
	  fcntl(sock, F_SETFL, flags);		// Try without O_NONBLOCK
      }
#endif
    }
#if !defined(NO_FCNTL_NONBLOCK)
    if (!(test_flags & TEST_BLOCKING))
      fcntl(sock, F_SETFL, flags);
#endif
2427
    if (new_sock == INVALID_SOCKET)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2428 2429 2430
    {
      if ((error_count++ & 255) == 0)		// This can happen often
	sql_perror("Error in accept");
2431
      MAYBE_BROKEN_SYSCALL;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2432
      if (socket_errno == SOCKET_ENFILE || socket_errno == SOCKET_EMFILE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
	sleep(1);				// Give other threads some time
      continue;
    }

#ifdef HAVE_LIBWRAP
    {
      if (sock == ip_sock)
      {
	struct request_info req;
	signal(SIGCHLD, SIG_DFL);
	request_init(&req, RQ_DAEMON, libwrapName, RQ_FILE, new_sock, NULL);
	fromhost(&req);
	if (!hosts_access(&req))
	{
2447 2448 2449 2450 2451
	  /*
	    This may be stupid but refuse() includes an exit(0)
	    which we surely don't want...
	    clean_exit() - same stupid thing ...
	  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2452 2453 2454 2455
	  syslog(deny_severity, "refused connect from %s", eval_client(&req));
	  if (req.sink)
	    ((void (*)(int))req.sink)(req.fd);

2456 2457 2458 2459 2460 2461
	  /*
	    C++ sucks (the gibberish in front just translates the supplied
	    sink function pointer in the req structure from a void (*sink)();
	    to a void(*sink)(int) if you omit the cast, the C++ compiler
	    will cry...
	  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
	  (void) shutdown(new_sock,2);  // This looks fine to me...
	  (void) closesocket(new_sock);
	  continue;
	}
      }
    }
#endif /* HAVE_LIBWRAP */

    {
      size_socket dummyLen;
      struct sockaddr dummy;
      dummyLen = sizeof(struct sockaddr);
      if (getsockname(new_sock,&dummy, &dummyLen) < 0)
      {
	sql_perror("Error on new connection socket");
	(void) shutdown(new_sock,2);
	(void) closesocket(new_sock);
	continue;
      }
    }

    /*
    ** Don't allow too many connections
    */

    if (!(thd= new THD))
    {
2489 2490
      (void) shutdown(new_sock,2);
      VOID(closesocket(new_sock));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2491 2492 2493
      continue;
    }
    if (!(vio_tmp=vio_new(new_sock,
2494
			  sock == unix_sock ? VIO_TYPE_SOCKET :
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2495
			  VIO_TYPE_TCPIP,
2496
			  sock == unix_sock)) ||
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
	my_net_init(&thd->net,vio_tmp))
    {
      if (vio_tmp)
	vio_delete(vio_tmp);
      else
      {
	(void) shutdown(new_sock,2);
	(void) closesocket(new_sock);
      }
      delete thd;
      continue;
    }
    if (sock == unix_sock)
2510
      thd->host=(char*) localhost;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2511 2512 2513
    create_new_thread(thd);
  }

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2514 2515 2516 2517 2518
#ifdef OS2
  // kill server must be invoked from thread 1!
  kill_server(MYSQL_KILL_SIGNAL);
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2519 2520 2521 2522
#ifdef __NT__
  pthread_mutex_lock(&LOCK_thread_count);
  handler_count--;
  pthread_mutex_unlock(&LOCK_thread_count);
2523
  pthread_cond_signal(&COND_handler_count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599
#endif
  DBUG_RETURN(0);
}


#ifdef __NT__
pthread_handler_decl(handle_connections_namedpipes,arg)
{
  HANDLE hConnectedPipe;
  BOOL fConnected;
  THD *thd;
  my_thread_init();
  DBUG_ENTER("handle_connections_namedpipes");
  (void) my_pthread_getprio(pthread_self());		// For debugging

  DBUG_PRINT("general",("Waiting for named pipe connections."));
  while (!abort_loop)
  {
    /* wait for named pipe connection */
    fConnected = ConnectNamedPipe( hPipe, NULL );
    if (abort_loop)
      break;
    if ( !fConnected )
      fConnected = GetLastError() == ERROR_PIPE_CONNECTED;
    if ( !fConnected )
    {
      CloseHandle( hPipe );
      if ((hPipe = CreateNamedPipe(szPipeName,
				   PIPE_ACCESS_DUPLEX,
				   PIPE_TYPE_BYTE |
				   PIPE_READMODE_BYTE |
				   PIPE_WAIT,
				   PIPE_UNLIMITED_INSTANCES,
				   (int) net_buffer_length,
				   (int) net_buffer_length,
				   NMPWAIT_USE_DEFAULT_WAIT,
				   &saPipeSecurity )) ==
	  INVALID_HANDLE_VALUE )
      {
	sql_perror("Can't create new named pipe!");
	break;					// Abort
      }
    }
    hConnectedPipe = hPipe;
    /* create new pipe for new connection */
    if ((hPipe = CreateNamedPipe(szPipeName,
				 PIPE_ACCESS_DUPLEX,
				 PIPE_TYPE_BYTE |
				 PIPE_READMODE_BYTE |
				 PIPE_WAIT,
				 PIPE_UNLIMITED_INSTANCES,
				 (int) net_buffer_length,
				 (int) net_buffer_length,
				 NMPWAIT_USE_DEFAULT_WAIT,
				 &saPipeSecurity)) ==
	INVALID_HANDLE_VALUE)
    {
      sql_perror("Can't create new named pipe!");
      hPipe=hConnectedPipe;
      continue;					// We have to try again
    }

    if ( !(thd = new THD))
    {
      DisconnectNamedPipe( hConnectedPipe );
      CloseHandle( hConnectedPipe );
      continue;
    }
    if (!(thd->net.vio = vio_new_win32pipe(hConnectedPipe)) ||
	my_net_init(&thd->net, thd->net.vio))
    {
      close_connection(&thd->net,ER_OUT_OF_RESOURCES);
      delete thd;
      continue;
    }
    /* host name is unknown */
2600
    thd->host = my_strdup(localhost,MYF(0)); /* Host is unknown */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
    create_new_thread(thd);
  }

  pthread_mutex_lock(&LOCK_thread_count);
  handler_count--;
  pthread_cond_signal(&COND_handler_count);
  pthread_mutex_unlock(&LOCK_thread_count);
  DBUG_RETURN(0);
}
#endif /* __NT__ */


/******************************************************************************
** handle start options
******************************************************************************/

2617
enum options {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
               OPT_ISAM_LOG=256,            OPT_SKIP_NEW, 
               OPT_SKIP_GRANT,              OPT_SKIP_LOCK, 
               OPT_ENABLE_LOCK,             OPT_USE_LOCKING,
               OPT_SOCKET,                  OPT_UPDATE_LOG, 
               OPT_BIN_LOG,                 OPT_SKIP_RESOLVE, 
               OPT_SKIP_NETWORKING,         OPT_BIN_LOG_INDEX,
               OPT_BIND_ADDRESS,            OPT_PID_FILE,
               OPT_SKIP_PRIOR,              OPT_BIG_TABLES,    
               OPT_STANDALONE,              OPT_ONE_THREAD,
               OPT_CONSOLE,                 OPT_LOW_PRIORITY_UPDATES,
               OPT_SKIP_HOST_CACHE,         OPT_LONG_FORMAT,   
               OPT_FLUSH,                   OPT_SAFE, 
               OPT_BOOTSTRAP,               OPT_SKIP_SHOW_DB,
               OPT_TABLE_TYPE,              OPT_INIT_FILE,   
               OPT_DELAY_KEY_WRITE,         OPT_SLOW_QUERY_LOG, 
               OPT_SKIP_DELAY_KEY_WRITE,    OPT_CHARSETS_DIR,
               OPT_BDB_HOME,                OPT_BDB_LOG,  
               OPT_BDB_TMP,                 OPT_BDB_NOSYNC,
               OPT_BDB_LOCK,                OPT_BDB_SKIP, 
               OPT_BDB_NO_RECOVER,	    OPT_BDB_SHARED,
	       OPT_MASTER_HOST,             OPT_MASTER_USER,
               OPT_MASTER_PASSWORD,         OPT_MASTER_PORT,
               OPT_MASTER_INFO_FILE,        OPT_MASTER_CONNECT_RETRY,
2641
	       OPT_MASTER_RETRY_COUNT,
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
2642 2643
	       OPT_MASTER_SSL,             OPT_MASTER_SSL_KEY,
	       OPT_MASTER_SSL_CERT,            
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
               OPT_SQL_BIN_UPDATE_SAME,     OPT_REPLICATE_DO_DB,      
               OPT_REPLICATE_IGNORE_DB,     OPT_LOG_SLAVE_UPDATES,
               OPT_BINLOG_DO_DB,            OPT_BINLOG_IGNORE_DB,
               OPT_WANT_CORE,               OPT_SKIP_CONCURRENT_INSERT,
               OPT_MEMLOCK,                 OPT_MYISAM_RECOVER,
               OPT_REPLICATE_REWRITE_DB,    OPT_SERVER_ID, 
               OPT_SKIP_SLAVE_START,        OPT_SKIP_INNOBASE,
               OPT_SAFEMALLOC_MEM_LIMIT,    OPT_REPLICATE_DO_TABLE, 
               OPT_REPLICATE_IGNORE_TABLE,  OPT_REPLICATE_WILD_DO_TABLE, 
               OPT_REPLICATE_WILD_IGNORE_TABLE, 
               OPT_DISCONNECT_SLAVE_EVENT_COUNT, 
               OPT_ABORT_SLAVE_EVENT_COUNT,
2656 2657 2658 2659 2660 2661
	       OPT_INNODB_DATA_HOME_DIR,
               OPT_INNODB_DATA_FILE_PATH,
	       OPT_INNODB_LOG_GROUP_HOME_DIR, 
               OPT_INNODB_LOG_ARCH_DIR, 
               OPT_INNODB_LOG_ARCHIVE, 
               OPT_INNODB_FLUSH_LOG_AT_TRX_COMMIT, 
2662
               OPT_INNODB_FLUSH_METHOD, 
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
2663
               OPT_INNODB_FAST_SHUTDOWN, 
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2664
               OPT_SAFE_SHOW_DB,
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2665
	       OPT_INNODB_SKIP, OPT_SKIP_SAFEMALLOC,
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2666 2667 2668 2669 2670
               OPT_TEMP_POOL, OPT_TX_ISOLATION,
	       OPT_SKIP_STACK_TRACE, OPT_SKIP_SYMLINKS,
	       OPT_MAX_BINLOG_DUMP_EVENTS, OPT_SPORADIC_BINLOG_DUMP_FAIL,
	       OPT_SAFE_USER_CREATE, OPT_SQL_MODE,
               OPT_DO_PSTACK, OPT_REPORT_HOST,
2671
	       OPT_REPORT_USER, OPT_REPORT_PASSWORD, OPT_REPORT_PORT,
2672
               OPT_SHOW_SLAVE_AUTH_INFO, OPT_OLD_RPL_COMPAT,
2673
               OPT_SLAVE_LOAD_TMPDIR, OPT_NO_MIX_TYPE,
2674
	       OPT_RPL_RECOVERY_RANK,OPT_INIT_RPL_ROLE,
2675 2676
	       OPT_RELAY_LOG, OPT_RELAY_LOG_INDEX, OPT_RELAY_LOG_INFO_FILE,
               OPT_SLAVE_SKIP_ERRORS, OPT_DES_KEY_FILE
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2677
};
2678 2679 2680 2681

static struct option long_options[] = {
  {"ansi",                  no_argument,       0, 'a'},
  {"basedir",               required_argument, 0, 'b'},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2682
#ifdef HAVE_BERKELEY_DB
2683 2684 2685
  {"bdb-home",              required_argument, 0, (int) OPT_BDB_HOME},
  {"bdb-lock-detect",       required_argument, 0, (int) OPT_BDB_LOCK},
  {"bdb-logdir",            required_argument, 0, (int) OPT_BDB_LOG},
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2686
  {"bdb-no-recover",        no_argument,       0, (int) OPT_BDB_NO_RECOVER},
2687
  {"bdb-no-sync",           no_argument,       0, (int) OPT_BDB_NOSYNC},
2688
  {"bdb-shared-data",       no_argument,       0, (int) OPT_BDB_SHARED},
2689 2690 2691 2692 2693 2694 2695
  {"bdb-tmpdir",            required_argument, 0, (int) OPT_BDB_TMP},
#endif
  {"big-tables",            no_argument,       0, (int) OPT_BIG_TABLES},
  {"binlog-do-db",          required_argument, 0, (int) OPT_BINLOG_DO_DB},
  {"binlog-ignore-db",      required_argument, 0, (int) OPT_BINLOG_IGNORE_DB},
  {"bind-address",          required_argument, 0, (int) OPT_BIND_ADDRESS},
  {"bootstrap",             no_argument,       0, (int) OPT_BOOTSTRAP},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2696
#ifdef __WIN__
2697
  {"console",               no_argument,       0, (int) OPT_CONSOLE},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2698
#endif
2699 2700 2701 2702 2703
  {"core-file",             no_argument,       0, (int) OPT_WANT_CORE},
  {"chroot",                required_argument, 0, 'r'},
  {"character-sets-dir",    required_argument, 0, (int) OPT_CHARSETS_DIR},
  {"datadir",               required_argument, 0, 'h'},
  {"debug",                 optional_argument, 0, '#'},
2704
  {"des-key-file",          required_argument, 0, (int) OPT_DES_KEY_FILE},
2705 2706
  {"default-character-set", required_argument, 0, 'C'},
  {"default-table-type",    required_argument, 0, (int) OPT_TABLE_TYPE},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2707
  {"delay-key-write-for-all-tables",
2708 2709
                            no_argument,       0, (int) OPT_DELAY_KEY_WRITE},
  {"enable-locking",        no_argument,       0, (int) OPT_ENABLE_LOCK},
2710
  {"enable-pstack",         no_argument,       0, (int) OPT_DO_PSTACK},
2711 2712
  {"exit-info",             optional_argument, 0, 'T'},
  {"flush",                 no_argument,       0, (int) OPT_FLUSH},
2713
  {"init-rpl-role",         required_argument, 0, (int) OPT_INIT_RPL_ROLE},
2714 2715
  /* We must always support this option to make scripts like mysqltest easier
     to do */
2716 2717
  {"innodb_data_file_path", required_argument, 0,
     OPT_INNODB_DATA_FILE_PATH},
2718
#ifdef HAVE_INNOBASE_DB
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
  {"innodb_data_home_dir", required_argument, 0,
     OPT_INNODB_DATA_HOME_DIR},
  {"innodb_log_group_home_dir", required_argument, 0,
    OPT_INNODB_LOG_GROUP_HOME_DIR},
  {"innodb_log_arch_dir", required_argument, 0,
    OPT_INNODB_LOG_ARCH_DIR},
  {"innodb_log_archive", optional_argument, 0,
     OPT_INNODB_LOG_ARCHIVE},
  {"innodb_flush_log_at_trx_commit", optional_argument, 0,
     OPT_INNODB_FLUSH_LOG_AT_TRX_COMMIT},
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
2729 2730
  {"innodb_fast_shutdown", optional_argument, 0,
     OPT_INNODB_FAST_SHUTDOWN},
2731
  {"innodb_flush_method", required_argument, 0,
2732
    OPT_INNODB_FLUSH_METHOD},
2733
#endif
2734 2735 2736 2737 2738
  {"help",                  no_argument,       0, '?'},
  {"init-file",             required_argument, 0, (int) OPT_INIT_FILE},
  {"log",                   optional_argument, 0, 'l'},
  {"language",              required_argument, 0, 'L'},
  {"log-bin",               optional_argument, 0, (int) OPT_BIN_LOG},
2739
  {"log-bin-index",         required_argument, 0, (int) OPT_BIN_LOG_INDEX},
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
  {"log-isam",              optional_argument, 0, (int) OPT_ISAM_LOG},
  {"log-update",            optional_argument, 0, (int) OPT_UPDATE_LOG},
  {"log-slow-queries",      optional_argument, 0, (int) OPT_SLOW_QUERY_LOG},
  {"log-long-format",       no_argument,       0, (int) OPT_LONG_FORMAT},
  {"log-slave-updates",     no_argument,       0, (int) OPT_LOG_SLAVE_UPDATES},
  {"low-priority-updates",  no_argument,       0, (int) OPT_LOW_PRIORITY_UPDATES},
  {"master-host",           required_argument, 0, (int) OPT_MASTER_HOST},
  {"master-user",           required_argument, 0, (int) OPT_MASTER_USER},
  {"master-password",       required_argument, 0, (int) OPT_MASTER_PASSWORD},
  {"master-port",           required_argument, 0, (int) OPT_MASTER_PORT},
  {"master-connect-retry",  required_argument, 0, (int) OPT_MASTER_CONNECT_RETRY},
2751
  {"master-retry-count",    required_argument, 0, (int) OPT_MASTER_RETRY_COUNT},
2752
  {"master-info-file",      required_argument, 0, (int) OPT_MASTER_INFO_FILE},
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
2753 2754 2755
  {"master-ssl",      	    optional_argument, 0, (int) OPT_MASTER_SSL},
  {"master-ssl-key",        optional_argument, 0, (int) OPT_MASTER_SSL_KEY},
  {"master-ssl-cert",       optional_argument, 0, (int) OPT_MASTER_SSL_CERT},
2756
  {"myisam-recover",	    optional_argument, 0, (int) OPT_MYISAM_RECOVER},
2757
  {"memlock",		    no_argument,       0, (int) OPT_MEMLOCK},
2758 2759 2760 2761
  /*
    Option needs to be available for the test case to pass in non-debugging
    mode. is a no-op.
  */
2762 2763
  {"disconnect-slave-event-count",      required_argument, 0,
     (int) OPT_DISCONNECT_SLAVE_EVENT_COUNT},
2764 2765
  {"abort-slave-event-count",      required_argument, 0,
     (int) OPT_ABORT_SLAVE_EVENT_COUNT},
2766 2767 2768 2769
  {"max-binlog-dump-events",      required_argument, 0,
     (int) OPT_MAX_BINLOG_DUMP_EVENTS},
  {"sporadic-binlog-dump-fail", no_argument, 0,
     (int) OPT_SPORADIC_BINLOG_DUMP_FAIL},
2770 2771
  {"safemalloc-mem-limit",  required_argument, 0, (int)
     OPT_SAFEMALLOC_MEM_LIMIT},
2772
  {"new",                   no_argument,       0, 'n'},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2773
#ifdef NOT_YET
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2774
  {"no-mix-table-types",    no_argument,       0, (int) OPT_NO_MIX_TYPE},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2775
#endif
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2776 2777
  {"old-protocol",	    no_argument,       0, 'o'},
  {"old-rpl-compat",        no_argument,       0, (int) OPT_OLD_RPL_COMPAT},
2778
#ifdef ONE_THREAD
2779 2780 2781 2782 2783
  {"one-thread",            no_argument,       0, (int) OPT_ONE_THREAD},
#endif
  {"pid-file",              required_argument, 0, (int) OPT_PID_FILE},
  {"port",                  required_argument, 0, 'P'},
  {"replicate-do-db",       required_argument, 0, (int) OPT_REPLICATE_DO_DB},
2784 2785
  {"replicate-do-table",       required_argument, 0,
   (int) OPT_REPLICATE_DO_TABLE},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2786
  {"replicate-wild-do-table",  required_argument, 0,
2787
   (int) OPT_REPLICATE_WILD_DO_TABLE},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2788
  {"replicate-ignore-db",      required_argument, 0,
2789 2790 2791
   (int) OPT_REPLICATE_IGNORE_DB},
  {"replicate-ignore-table",   required_argument, 0,
   (int) OPT_REPLICATE_IGNORE_TABLE},
2792
  {"replicate-wild-ignore-table",   required_argument, 0,
2793
   (int) OPT_REPLICATE_WILD_IGNORE_TABLE},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2794
  {"replicate-rewrite-db",     required_argument, 0,
2795
     (int) OPT_REPLICATE_REWRITE_DB},
2796
  // In replication, we may need to tell the other servers how to connect
2797 2798 2799 2800
  {"report-host",           required_argument, 0, (int) OPT_REPORT_HOST},
  {"report-user",           required_argument, 0, (int) OPT_REPORT_USER},
  {"report-password",       required_argument, 0, (int) OPT_REPORT_PASSWORD},
  {"report-port",           required_argument, 0, (int) OPT_REPORT_PORT},
2801
  {"rpl-recovery-rank",     required_argument, 0, (int) OPT_RPL_RECOVERY_RANK},
2802 2803
  {"relay-log",             required_argument, 0, (int) OPT_RELAY_LOG},
  {"relay-log-index",       required_argument, 0, (int) OPT_RELAY_LOG_INDEX},
2804
  {"safe-mode",             no_argument,       0, (int) OPT_SAFE},
2805
  {"safe-show-database",    no_argument,       0, (int) OPT_SAFE_SHOW_DB},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2806
  {"safe-user-create",	    no_argument,       0, (int) OPT_SAFE_USER_CREATE},
2807
  {"server-id",		    required_argument, 0, (int) OPT_SERVER_ID},
2808
  {"set-variable",          required_argument, 0, 'O'},
2809 2810
  {"show-slave-auth-info",  no_argument,       0,
     (int) OPT_SHOW_SLAVE_AUTH_INFO},
2811
  {"skip-bdb",              no_argument,       0, (int) OPT_BDB_SKIP},
2812
  {"skip-innodb",           no_argument,       0, (int) OPT_INNODB_SKIP},
2813
  {"skip-concurrent-insert", no_argument,      0, (int) OPT_SKIP_CONCURRENT_INSERT},
2814 2815 2816 2817 2818
  {"skip-delay-key-write",  no_argument,       0, (int) OPT_SKIP_DELAY_KEY_WRITE},
  {"skip-grant-tables",     no_argument,       0, (int) OPT_SKIP_GRANT},
  {"skip-locking",          no_argument,       0, (int) OPT_SKIP_LOCK},
  {"skip-host-cache",       no_argument,       0, (int) OPT_SKIP_HOST_CACHE},
  {"skip-name-resolve",     no_argument,       0, (int) OPT_SKIP_RESOLVE},
2819
  {"skip-networking",       no_argument,       0, (int) OPT_SKIP_NETWORKING},
2820
  {"skip-new",              no_argument,       0, (int) OPT_SKIP_NEW},
2821
  {"skip-safemalloc",	    no_argument,       0, (int) OPT_SKIP_SAFEMALLOC},
2822
  {"skip-show-database",    no_argument,       0, (int) OPT_SKIP_SHOW_DB},
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2823
  {"skip-slave-start",      no_argument,       0, (int) OPT_SKIP_SLAVE_START},
2824
  {"skip-stack-trace",	    no_argument,       0, (int) OPT_SKIP_STACK_TRACE},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2825
  {"skip-symlink",	    no_argument,       0, (int) OPT_SKIP_SYMLINKS},
2826
  {"skip-thread-priority",  no_argument,       0, (int) OPT_SKIP_PRIOR},
2827 2828
  {"relay-log-info-file",      required_argument, 0,
   (int) OPT_RELAY_LOG_INFO_FILE},
2829
  {"slave-load-tmpdir", required_argument, 0, (int) OPT_SLAVE_LOAD_TMPDIR},  
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2830
  {"slave-skip-errors", required_argument, 0,
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
2831
   (int) OPT_SLAVE_SKIP_ERRORS},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2832
  {"socket",                required_argument, 0, (int) OPT_SOCKET},
2833
  {"sql-bin-update-same",   no_argument,       0, (int) OPT_SQL_BIN_UPDATE_SAME},
2834
  {"sql-mode",              required_argument, 0, (int) OPT_SQL_MODE},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2835 2836
#include "sslopt-longopts.h"
#ifdef __WIN__
2837
  {"standalone",            no_argument,       0, (int) OPT_STANDALONE},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2838
#endif
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
2839
  {"transaction-isolation", required_argument, 0, (int) OPT_TX_ISOLATION},
2840
  {"temp-pool",             no_argument,       0, (int) OPT_TEMP_POOL},
2841 2842
  {"tmpdir",                required_argument, 0, 't'},
  {"use-locking",           no_argument,       0, (int) OPT_USE_LOCKING},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2843
#ifdef USE_SYMDIR
2844
  {"use-symbolic-links",    no_argument,       0, 's'},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2845
#endif
2846 2847
  {"user",                  required_argument, 0, 'u'},
  {"version",               no_argument,       0, 'V'},
2848
  {"warnings",		    no_argument,       0, 'W'},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2849 2850 2851
  {0, 0, 0, 0}
};

2852 2853
#define LONG_TIMEOUT ((ulong) 3600L*24L*365L)

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2854
CHANGEABLE_VAR changeable_vars[] = {
2855 2856
  { "back_log",                (long*) &back_log, 
      50, 1, 65535, 0, 1 },
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2857
#ifdef HAVE_BERKELEY_DB
2858 2859
  { "bdb_cache_size",          (long*) &berkeley_cache_size, 
      KEY_CACHE_SIZE, 20*1024, (long) ~0, 0, IO_SIZE },
2860 2861
  {"bdb_log_buffer_size",      (long*) &berkeley_log_buffer_size, 0, 256*1024L,
     ~0L, 0, 1024},
2862
  { "bdb_max_lock",            (long*) &berkeley_max_lock, 
2863
      10000, 0, (long) ~0, 0, 1 },
2864 2865
    /* QQ: The following should be removed soon! */
  { "bdb_lock_max",            (long*) &berkeley_max_lock, 
2866
      10000, 0, (long) ~0, 0, 1 },
2867
#endif
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2868
  { "binlog_cache_size",       (long*) &binlog_cache_size,
2869
      32*1024L, IO_SIZE, ~0L, 0, IO_SIZE },
2870
  { "connect_timeout",         (long*) &connect_timeout,
2871
      CONNECT_TIMEOUT, 2, LONG_TIMEOUT, 0, 1 },
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2872
  { "delayed_insert_timeout",  (long*) &delayed_insert_timeout, 
2873
      DELAYED_WAIT_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
2874 2875 2876 2877 2878
  { "delayed_insert_limit",    (long*) &delayed_insert_limit, 
      DELAYED_LIMIT, 1, ~0L, 0, 1 },
  { "delayed_queue_size",      (long*) &delayed_queue_size,
      DELAYED_QUEUE_SIZE, 1, ~0L, 0, 1 },
  { "flush_time",              (long*) &flush_time,
2879
      FLUSH_TIME, 0, LONG_TIMEOUT, 0, 1 },
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2880 2881 2882 2883 2884 2885
  { "ft_min_word_len",         (long*) &ft_min_word_len,
      4, 1, HA_FT_MAXLEN, 0, 1 },
  { "ft_max_word_len",         (long*) &ft_max_word_len,
      HA_FT_MAXLEN, 10, HA_FT_MAXLEN, 0, 1 },
  { "ft_max_word_len_for_sort",(long*) &ft_max_word_len_for_sort,
      20, 4, HA_FT_MAXLEN, 0, 1 },
2886
#ifdef HAVE_INNOBASE_DB
2887
  {"innodb_mirrored_log_groups",
2888
   (long*) &innobase_mirrored_log_groups, 1, 1, 10, 0, 1},
2889
  {"innodb_log_files_in_group",
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2890
     (long*) &innobase_log_files_in_group, 2, 2, 100, 0, 1},
2891
  {"innodb_log_file_size",
2892 2893
     (long*) &innobase_log_file_size, 5*1024*1024L, 1*1024*1024L,
     ~0L, 0, 1024*1024L},
2894
  {"innodb_log_buffer_size",
2895 2896
     (long*) &innobase_log_buffer_size, 1024*1024L, 256*1024L,
     ~0L, 0, 1024},
2897
  {"innodb_buffer_pool_size",
2898 2899
     (long*) &innobase_buffer_pool_size, 8*1024*1024L, 1024*1024L,
     ~0L, 0, 1024*1024L},
2900
  {"innodb_additional_mem_pool_size",
2901 2902
   (long*) &innobase_additional_mem_pool_size, 1*1024*1024L, 512*1024L,
     ~0L, 0, 1024},
2903
  {"innodb_file_io_threads",
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
2904
     (long*) &innobase_file_io_threads, 4, 4, 64, 0, 1},
2905
  {"innodb_lock_wait_timeout",
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
2906
     (long*) &innobase_lock_wait_timeout, 50, 1,
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2907
						1024 * 1024 * 1024, 0, 1},
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
2908 2909 2910 2911
  {"innodb_thread_concurrency",
     (long*) &innobase_thread_concurrency, 8, 1, 1000, 0, 1},
  {"innodb_force_recovery",
     (long*) &innobase_force_recovery, 0, 0, 6, 0, 1},
2912
#endif
2913
  { "interactive_timeout",     (long*) &net_interactive_timeout,
2914
      NET_WAIT_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
2915 2916 2917 2918 2919
  { "join_buffer_size",        (long*) &join_buff_size,
      128*1024L, IO_SIZE*2+MALLOC_OVERHEAD, ~0L, MALLOC_OVERHEAD, IO_SIZE },
  { "key_buffer_size",         (long*) &keybuff_size,
      KEY_CACHE_SIZE, MALLOC_OVERHEAD, (long) ~0, MALLOC_OVERHEAD, IO_SIZE },
  { "long_query_time",         (long*) &long_query_time,
2920
      10, 1, LONG_TIMEOUT, 0, 1 },
2921 2922 2923
  { "lower_case_table_names",  (long*) &lower_case_table_names,
      IF_WIN(1,0), 0, 1, 0, 1 },
  { "max_allowed_packet",      (long*) &max_allowed_packet,
2924
      1024*1024L, 80, 64*1024*1024L, MALLOC_OVERHEAD, 1024 },
2925 2926
  { "max_binlog_cache_size",   (long*) &max_binlog_cache_size,
      ~0L, IO_SIZE, ~0L, 0, IO_SIZE },
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
2927
  { "max_binlog_size",         (long*) &max_binlog_size,
2928
      1024*1024L*1024L, 1024, 1024*1024L*1024L, 0, 1 },
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
  { "max_connections",         (long*) &max_connections,
      100, 1, 16384, 0, 1 },
  { "max_connect_errors",      (long*) &max_connect_errors,
      MAX_CONNECT_ERRORS, 1, ~0L, 0, 1 },
  { "max_delayed_threads",     (long*) &max_insert_delayed_threads,
      20, 1, 16384, 0, 1 },
  { "max_heap_table_size",     (long*) &max_heap_table_size,
      16*1024*1024L, 16384, ~0L, MALLOC_OVERHEAD, 1024 },
  { "max_join_size",           (long*) &max_join_size,
      ~0L, 1, ~0L, 0, 1 },
  { "max_sort_length",         (long*) &max_item_sort_length,
      1024, 4, 8192*1024L, 0, 1 },
  { "max_tmp_tables",          (long*) &max_tmp_tables,
      32, 1, ~0L, 0, 1 },
2943
  { "max_user_connections",    (long*) &max_user_connections,
2944
      0, 1, ~0L, 0, 1 },
2945 2946
  { "max_write_lock_count",    (long*) &max_write_lock_count,
      ~0L, 1, ~0L, 0, 1 },
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2947
  { "myisam_bulk_insert_tree_size", (long*) &myisam_bulk_insert_tree_size,
2948
      8192*1024, 0, ~0L, 0, 1 },
2949 2950 2951
  { "myisam_block_size", 	(long*) &opt_myisam_block_size,
      MI_KEY_BLOCK_LENGTH, MI_MIN_KEY_BLOCK_LENGTH, MI_MAX_KEY_BLOCK_LENGTH,
    0, MI_MIN_KEY_BLOCK_LENGTH },
2952 2953 2954 2955 2956
  { "myisam_max_extra_sort_file_size",
    (long*) &myisam_max_extra_sort_file_size,
    (long) (MI_MAX_TEMP_LENGTH/(1024L*1024L)), 0, ~0L, 0, 1 },
  { "myisam_max_sort_file_size", (long*) &myisam_max_sort_file_size,
    (long) (LONG_MAX/(1024L*1024L)), 0, ~0L, 0, 1 },
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2957 2958
  { "myisam_sort_buffer_size", (long*) &myisam_sort_buffer_size,
      8192*1024, 4, ~0L, 0, 1 },
2959 2960 2961 2962 2963
  { "net_buffer_length",       (long*) &net_buffer_length,
      16384, 1024, 1024*1024L, MALLOC_OVERHEAD, 1024 },
  { "net_retry_count",         (long*) &mysqld_net_retry_count,
      MYSQLD_NET_RETRY_COUNT, 1, ~0L, 0, 1 },
  { "net_read_timeout",        (long*) &net_read_timeout, 
2964
      NET_READ_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
2965
  { "net_write_timeout",       (long*) &net_write_timeout,
2966
      NET_WRITE_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2967 2968
  { "open_files_limit",        (long*) &open_files_limit,
      0, 0, 65535, 0, 1},
2969 2970
  { "query_buffer_size",       (long*) &query_buff_size,
      0, MALLOC_OVERHEAD, (long) ~0, MALLOC_OVERHEAD, IO_SIZE },
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2971 2972 2973 2974 2975 2976
  { "query_cache_limit",       (long*) &query_cache_limit,
     1024*1024L, 0, ULONG_MAX, 0, 1},
  { "query_cache_size",        (long*) &query_cache_size,
      0, 0, ULONG_MAX, 0, 1},
  { "query_cache_startup_type",(long*) &query_cache_startup_type,
      1, 0, 2, 0, 1},
2977 2978
  { "record_buffer",           (long*) &my_default_record_cache_size,
      128*1024L, IO_SIZE*2+MALLOC_OVERHEAD, ~0L, MALLOC_OVERHEAD, IO_SIZE },
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2979 2980
  { "record_rnd_buffer",           (long*) &record_rnd_cache_size,
      0, IO_SIZE*2+MALLOC_OVERHEAD, ~0L, MALLOC_OVERHEAD, IO_SIZE },
2981
  { "slave_net_timeout",        (long*) &slave_net_timeout, 
2982
      SLAVE_NET_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
2983
  { "slow_launch_time",        (long*) &slow_launch_time, 
2984
      2L, 0L, LONG_TIMEOUT, 0, 1 },
2985 2986 2987 2988 2989 2990 2991
  { "sort_buffer",             (long*) &sortbuff_size,
      MAX_SORT_MEMORY, MIN_SORT_MEMORY+MALLOC_OVERHEAD*2, ~0L, MALLOC_OVERHEAD, 1 },
  { "table_cache",             (long*) &table_cache_size,
      64, 1, 16384, 0, 1 },
  { "thread_concurrency",      (long*) &concurrency,
      DEFAULT_CONCURRENCY, 1, 512, 0, 1 },
  { "thread_cache_size",       (long*) &thread_cache_size,
2992
      0, 0, 16384, 0, 1 },
2993
  { "tmp_table_size",          (long*) &tmp_table_size,
2994
    32*1024*1024L, 1024, ~0L, 0, 1 },
2995
  { "thread_stack",            (long*) &thread_stack,
monty@donna.mysql.com's avatar
Merge  
monty@donna.mysql.com committed
2996
      DEFAULT_THREAD_STACK, 1024*32, ~0L, 0, 1024 },
2997
  { "wait_timeout",            (long*) &net_wait_timeout,
2998
      NET_WAIT_TIMEOUT, 1, LONG_TIMEOUT, 0, 1 },
2999 3000
  { NullS, (long*) 0, 0, 0, 0, 0, 0}
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3001 3002 3003


struct show_var_st init_vars[]= {
3004 3005
  {"back_log",                (char*) &back_log,                    SHOW_LONG},
  {"basedir",                 mysql_home,                           SHOW_CHAR},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3006
#ifdef HAVE_BERKELEY_DB
3007
  {"bdb_cache_size",          (char*) &berkeley_cache_size,         SHOW_LONG},
3008
  {"bdb_log_buffer_size",     (char*) &berkeley_log_buffer_size,    SHOW_LONG},
3009
  {"bdb_home",                (char*) &berkeley_home,               SHOW_CHAR_PTR},
3010
  {"bdb_max_lock",            (char*) &berkeley_max_lock,	    SHOW_LONG},
3011
  {"bdb_logdir",              (char*) &berkeley_logdir,             SHOW_CHAR_PTR},
3012
  {"bdb_shared_data",	      (char*) &berkeley_shared_data,	    SHOW_BOOL},
3013
  {"bdb_tmpdir",              (char*) &berkeley_tmpdir,             SHOW_CHAR_PTR},
3014
  {"bdb_version",             (char*) DB_VERSION_STRING,            SHOW_CHAR},
3015
#endif
3016
  {"binlog_cache_size",       (char*) &binlog_cache_size,	    SHOW_LONG},
3017 3018 3019
  {"character_set",           default_charset,                      SHOW_CHAR},
  {"character_sets",          (char*) &charsets_list,               SHOW_CHAR_PTR},
  {"concurrent_insert",       (char*) &myisam_concurrent_insert,    SHOW_MY_BOOL},
3020
  {"connect_timeout",         (char*) &connect_timeout,             SHOW_LONG},
3021 3022 3023 3024 3025 3026 3027
  {"datadir",                 mysql_real_data_home,                 SHOW_CHAR},
  {"delay_key_write",         (char*) &myisam_delay_key_write,      SHOW_MY_BOOL},
  {"delayed_insert_limit",    (char*) &delayed_insert_limit,        SHOW_LONG},
  {"delayed_insert_timeout",  (char*) &delayed_insert_timeout,      SHOW_LONG},
  {"delayed_queue_size",      (char*) &delayed_queue_size,          SHOW_LONG},
  {"flush",                   (char*) &myisam_flush,                SHOW_MY_BOOL},
  {"flush_time",              (char*) &flush_time,                  SHOW_LONG},
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3028 3029 3030
  {"ft_min_word_len",         (char*) &ft_min_word_len,             SHOW_LONG},
  {"ft_max_word_len",         (char*) &ft_max_word_len,             SHOW_LONG},
  {"ft_max_word_len_for_sort",(char*) &ft_max_word_len_for_sort,    SHOW_LONG},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3031
  {"ft_boolean_syntax",       (char*) ft_boolean_syntax,	    SHOW_CHAR},
3032
  {"have_bdb",		      (char*) &have_berkeley_db,	    SHOW_HAVE},
3033
  {"have_innodb",	      (char*) &have_innodb,		    SHOW_HAVE},
3034
  {"have_isam",	      	      (char*) &have_isam,		    SHOW_HAVE},
3035
  {"have_raid",		      (char*) &have_raid,		    SHOW_HAVE},
3036
  {"have_symlink",            (char*) &have_symlink,         	    SHOW_HAVE},
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
3037
  {"have_openssl",	      (char*) &have_openssl,		    SHOW_HAVE},
3038
  {"init_file",               (char*) &opt_init_file,               SHOW_CHAR_PTR},
3039
#ifdef HAVE_INNOBASE_DB
3040 3041
  {"innodb_additional_mem_pool_size", (char*) &innobase_additional_mem_pool_size, SHOW_LONG },
  {"innodb_buffer_pool_size", (char*) &innobase_buffer_pool_size, SHOW_LONG },
3042 3043
  {"innodb_data_file_path", (char*) &innobase_data_file_path,	    SHOW_CHAR_PTR},
  {"innodb_data_home_dir",  (char*) &innobase_data_home_dir,	    SHOW_CHAR_PTR},
3044
  {"innodb_file_io_threads", (char*) &innobase_file_io_threads, SHOW_LONG },
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
3045 3046
  {"innodb_force_recovery", (char*) &innobase_force_recovery, SHOW_LONG },
  {"innodb_thread_concurrency", (char*) &innobase_thread_concurrency, SHOW_LONG },
3047
  {"innodb_flush_log_at_trx_commit", (char*) &innobase_flush_log_at_trx_commit, SHOW_MY_BOOL},
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
3048
  {"innodb_fast_shutdown", (char*) &innobase_fast_shutdown, SHOW_MY_BOOL},
3049 3050
  {"innodb_flush_method",    (char*) &innobase_unix_file_flush_method, SHOW_CHAR_PTR},
  {"innodb_lock_wait_timeout", (char*) &innobase_lock_wait_timeout, SHOW_LONG },
3051 3052
  {"innodb_log_arch_dir",   (char*) &innobase_log_arch_dir, 	    SHOW_CHAR_PTR},
  {"innodb_log_archive",    (char*) &innobase_log_archive, 	    SHOW_MY_BOOL},
3053 3054 3055
  {"innodb_log_buffer_size", (char*) &innobase_log_buffer_size, SHOW_LONG },
  {"innodb_log_file_size", (char*) &innobase_log_file_size, SHOW_LONG},
  {"innodb_log_files_in_group", (char*) &innobase_log_files_in_group,	SHOW_LONG},
3056
  {"innodb_log_group_home_dir", (char*) &innobase_log_group_home_dir, SHOW_CHAR_PTR},
3057
  {"innodb_mirrored_log_groups", (char*) &innobase_mirrored_log_groups, SHOW_LONG},
3058
#endif
3059
  {"interactive_timeout",     (char*) &net_interactive_timeout,     SHOW_LONG},
3060
  {"join_buffer_size",        (char*) &join_buff_size,              SHOW_LONG},
3061 3062
  {"key_buffer_size",         (char*) &keybuff_size,                SHOW_LONG},
  {"language",                language,                             SHOW_CHAR},
3063
  {"large_files_support",     (char*) &opt_large_files,             SHOW_BOOL},	
3064
#ifdef HAVE_MLOCKALL
3065 3066
  {"locked_in_memory",	      (char*) &locked_in_memory,	    SHOW_BOOL},
#endif
3067 3068 3069 3070
  {"log",                     (char*) &opt_log,                     SHOW_BOOL},
  {"log_update",              (char*) &opt_update_log,              SHOW_BOOL},
  {"log_bin",                 (char*) &opt_bin_log,                 SHOW_BOOL},
  {"log_slave_updates",       (char*) &opt_log_slave_updates,       SHOW_BOOL},
3071
  {"log_long_queries",        (char*) &opt_slow_log,                SHOW_BOOL},
3072 3073 3074 3075
  {"long_query_time",         (char*) &long_query_time,             SHOW_LONG},
  {"low_priority_updates",    (char*) &low_priority_updates,        SHOW_BOOL},
  {"lower_case_table_names",  (char*) &lower_case_table_names,      SHOW_LONG},
  {"max_allowed_packet",      (char*) &max_allowed_packet,          SHOW_LONG},
3076 3077
  {"max_binlog_cache_size",   (char*) &max_binlog_cache_size,	    SHOW_LONG},
  {"max_binlog_size",         (char*) &max_binlog_size,	            SHOW_LONG},
3078 3079 3080 3081 3082 3083
  {"max_connections",         (char*) &max_connections,             SHOW_LONG},
  {"max_connect_errors",      (char*) &max_connect_errors,          SHOW_LONG},
  {"max_delayed_threads",     (char*) &max_insert_delayed_threads,  SHOW_LONG},
  {"max_heap_table_size",     (char*) &max_heap_table_size,         SHOW_LONG},
  {"max_join_size",           (char*) &max_join_size,               SHOW_LONG},
  {"max_sort_length",         (char*) &max_item_sort_length,        SHOW_LONG},
3084
  {"max_user_connections",    (char*) &max_user_connections,        SHOW_LONG},
3085 3086
  {"max_tmp_tables",          (char*) &max_tmp_tables,              SHOW_LONG},
  {"max_write_lock_count",    (char*) &max_write_lock_count,        SHOW_LONG},
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3087
  {"myisam_bulk_insert_tree_size", (char*) &myisam_bulk_insert_tree_size, SHOW_INT},
3088 3089 3090
  {"myisam_max_extra_sort_file_size", (char*) &myisam_max_extra_sort_file_size,
   SHOW_LONG},
  {"myisam_max_sort_file_size",(char*) &myisam_max_sort_file_size,  SHOW_LONG},
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3091
  {"myisam_recover_options",  (char*) &myisam_recover_options_str,  SHOW_CHAR_PTR},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3092
  {"myisam_sort_buffer_size", (char*) &myisam_sort_buffer_size,     SHOW_LONG},
3093
  {"net_buffer_length",       (char*) &net_buffer_length,           SHOW_LONG},
3094
  {"net_read_timeout",        (char*) &net_read_timeout,	    SHOW_LONG},
3095
  {"net_retry_count",         (char*) &mysqld_net_retry_count,      SHOW_LONG},
3096
  {"net_write_timeout",       (char*) &net_write_timeout,	    SHOW_LONG},
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3097
  {"open_files_limit",	      (char*) &open_files_limit,	    SHOW_LONG},
3098 3099 3100 3101
  {"pid_file",                (char*) pidfile_name,                 SHOW_CHAR},
  {"port",                    (char*) &mysql_port,                  SHOW_INT},
  {"protocol_version",        (char*) &protocol_version,            SHOW_INT},
  {"record_buffer",           (char*) &my_default_record_cache_size,SHOW_LONG},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3102
  {"record_rnd_buffer",       (char*) &record_rnd_cache_size,	    SHOW_LONG},
3103
  {"rpl_recovery_rank",       (char*) &rpl_recovery_rank,           SHOW_LONG},
3104
  {"query_buffer_size",       (char*) &query_buff_size,		    SHOW_LONG},
3105 3106
  {"query_cache_limit",       (char*) &query_cache.query_cache_limit, SHOW_LONG},
  {"query_cache_size",        (char*) &query_cache.query_cache_size, SHOW_LONG},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3107
  {"query_cache_startup_type",(char*) &query_cache_startup_type,    SHOW_LONG},
3108
  {"safe_show_database",      (char*) &opt_safe_show_db,            SHOW_BOOL},
3109
  {"server_id",               (char*) &server_id,		    SHOW_LONG},
3110
  {"slave_net_timeout",       (char*) &slave_net_timeout,	    SHOW_LONG},
3111 3112 3113 3114 3115 3116
  {"skip_locking",            (char*) &my_disable_locking,          SHOW_MY_BOOL},
  {"skip_networking",         (char*) &opt_disable_networking,      SHOW_BOOL},
  {"skip_show_database",      (char*) &opt_skip_show_db,            SHOW_BOOL},
  {"slow_launch_time",        (char*) &slow_launch_time,            SHOW_LONG},
  {"socket",                  (char*) &mysql_unix_port,             SHOW_CHAR_PTR},
  {"sort_buffer",             (char*) &sortbuff_size,               SHOW_LONG},
3117
  {"sql_mode",                (char*) &opt_sql_mode,                SHOW_LONG},
3118 3119
  {"table_cache",             (char*) &table_cache_size,            SHOW_LONG},
  {"table_type",              (char*) &default_table_type_name,     SHOW_CHAR_PTR},
3120
  {"thread_cache_size",       (char*) &thread_cache_size,           SHOW_LONG},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3121
#ifdef HAVE_THR_SETCONCURRENCY
3122
  {"thread_concurrency",      (char*) &concurrency,                 SHOW_LONG},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3123
#endif
3124
  {"thread_stack",            (char*) &thread_stack,                SHOW_LONG},
3125
  {"transaction_isolation",   (char*) &default_tx_isolation_name,   SHOW_CHAR_PTR},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3126
#ifdef HAVE_TZNAME
3127
  {"timezone",                time_zone,                            SHOW_CHAR},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3128
#endif
3129 3130 3131 3132
  {"tmp_table_size",          (char*) &tmp_table_size,              SHOW_LONG},
  {"tmpdir",                  (char*) &mysql_tmpdir,                SHOW_CHAR_PTR},
  {"version",                 server_version,                       SHOW_CHAR},
  {"wait_timeout",            (char*) &net_wait_timeout,            SHOW_LONG},
3133
  {NullS, NullS, SHOW_LONG}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3134 3135 3136
};

struct show_var_st status_vars[]= {
3137 3138 3139 3140
  {"Aborted_clients",          (char*) &aborted_threads,        SHOW_LONG},
  {"Aborted_connects",         (char*) &aborted_connects,       SHOW_LONG},
  {"Bytes_received",           (char*) &bytes_received,         SHOW_LONG},
  {"Bytes_sent",               (char*) &bytes_sent,             SHOW_LONG},
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
  {"Com_admin_commands",       (char*) &com_other,		SHOW_LONG},
  {"Com_alter_table",	       (char*) (com_stat+(uint) SQLCOM_ALTER_TABLE),SHOW_LONG},
  {"Com_analyze",	       (char*) (com_stat+(uint) SQLCOM_ANALYZE),SHOW_LONG},
  {"Com_backup_table",	       (char*) (com_stat+(uint) SQLCOM_BACKUP_TABLE),SHOW_LONG},
  {"Com_begin",		       (char*) (com_stat+(uint) SQLCOM_BEGIN),SHOW_LONG},
  {"Com_change_db",	       (char*) (com_stat+(uint) SQLCOM_CHANGE_DB),SHOW_LONG},
  {"Com_change_master",	       (char*) (com_stat+(uint) SQLCOM_CHANGE_MASTER),SHOW_LONG},
  {"Com_check",		       (char*) (com_stat+(uint) SQLCOM_CHECK),SHOW_LONG},
  {"Com_commit",	       (char*) (com_stat+(uint) SQLCOM_COMMIT),SHOW_LONG},
  {"Com_create_db",	       (char*) (com_stat+(uint) SQLCOM_CREATE_DB),SHOW_LONG},
  {"Com_create_function",      (char*) (com_stat+(uint) SQLCOM_CREATE_FUNCTION),SHOW_LONG},
  {"Com_create_index",	       (char*) (com_stat+(uint) SQLCOM_CREATE_INDEX),SHOW_LONG},
  {"Com_create_table",	       (char*) (com_stat+(uint) SQLCOM_CREATE_TABLE),SHOW_LONG},
  {"Com_delete",	       (char*) (com_stat+(uint) SQLCOM_DELETE),SHOW_LONG},
3155
  {"Com_delete_multi",	       (char*) (com_stat+(uint) SQLCOM_DELETE_MULTI),SHOW_LONG},
3156 3157 3158 3159 3160 3161
  {"Com_drop_db",	       (char*) (com_stat+(uint) SQLCOM_DROP_DB),SHOW_LONG},
  {"Com_drop_function",	       (char*) (com_stat+(uint) SQLCOM_DROP_FUNCTION),SHOW_LONG},
  {"Com_drop_index",	       (char*) (com_stat+(uint) SQLCOM_DROP_INDEX),SHOW_LONG},
  {"Com_drop_table",	       (char*) (com_stat+(uint) SQLCOM_DROP_TABLE),SHOW_LONG},
  {"Com_flush",		       (char*) (com_stat+(uint) SQLCOM_FLUSH),SHOW_LONG},
  {"Com_grant",		       (char*) (com_stat+(uint) SQLCOM_GRANT),SHOW_LONG},
3162
  {"Com_ha_close",	       (char*) (com_stat+(uint) SQLCOM_HA_CLOSE),SHOW_LONG},
3163 3164
  {"Com_ha_open",	       (char*) (com_stat+(uint) SQLCOM_HA_OPEN),SHOW_LONG},
  {"Com_ha_read",	       (char*) (com_stat+(uint) SQLCOM_HA_READ),SHOW_LONG},
3165 3166 3167 3168
  {"Com_insert",	       (char*) (com_stat+(uint) SQLCOM_INSERT),SHOW_LONG},
  {"Com_insert_select",	       (char*) (com_stat+(uint) SQLCOM_INSERT_SELECT),SHOW_LONG},
  {"Com_kill",		       (char*) (com_stat+(uint) SQLCOM_KILL),SHOW_LONG},
  {"Com_load",		       (char*) (com_stat+(uint) SQLCOM_LOAD),SHOW_LONG},
3169
  {"Com_load_master_data",    (char*) (com_stat+(uint) SQLCOM_LOAD_MASTER_DATA),SHOW_LONG},
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
  {"Com_load_master_table",    (char*) (com_stat+(uint) SQLCOM_LOAD_MASTER_TABLE),SHOW_LONG},
  {"Com_lock_tables",	       (char*) (com_stat+(uint) SQLCOM_LOCK_TABLES),SHOW_LONG},
  {"Com_optimize",	       (char*) (com_stat+(uint) SQLCOM_OPTIMIZE),SHOW_LONG},
  {"Com_purge",		       (char*) (com_stat+(uint) SQLCOM_PURGE),SHOW_LONG},
  {"Com_rename_table",	       (char*) (com_stat+(uint) SQLCOM_RENAME_TABLE),SHOW_LONG},
  {"Com_repair",	       (char*) (com_stat+(uint) SQLCOM_REPAIR),SHOW_LONG},
  {"Com_replace",	       (char*) (com_stat+(uint) SQLCOM_REPLACE),SHOW_LONG},
  {"Com_replace_select",       (char*) (com_stat+(uint) SQLCOM_REPLACE_SELECT),SHOW_LONG},
  {"Com_reset",		       (char*) (com_stat+(uint) SQLCOM_RESET),SHOW_LONG},
  {"Com_restore_table",	       (char*) (com_stat+(uint) SQLCOM_RESTORE_TABLE),SHOW_LONG},
  {"Com_revoke",	       (char*) (com_stat+(uint) SQLCOM_REVOKE),SHOW_LONG},
  {"Com_rollback",	       (char*) (com_stat+(uint) SQLCOM_ROLLBACK),SHOW_LONG},
  {"Com_select",	       (char*) (com_stat+(uint) SQLCOM_SELECT),SHOW_LONG},
  {"Com_set_option",	       (char*) (com_stat+(uint) SQLCOM_SET_OPTION),SHOW_LONG},
3184
  {"Com_show_binlog_events",   (char*) (com_stat+(uint) SQLCOM_SHOW_BINLOG_EVENTS),SHOW_LONG},
3185 3186 3187 3188 3189 3190 3191
  {"Com_show_binlogs",	       (char*) (com_stat+(uint) SQLCOM_SHOW_BINLOGS),SHOW_LONG},
  {"Com_show_create",	       (char*) (com_stat+(uint) SQLCOM_SHOW_CREATE),SHOW_LONG},
  {"Com_show_databases",       (char*) (com_stat+(uint) SQLCOM_SHOW_DATABASES),SHOW_LONG},
  {"Com_show_fields",	       (char*) (com_stat+(uint) SQLCOM_SHOW_FIELDS),SHOW_LONG},
  {"Com_show_grants",	       (char*) (com_stat+(uint) SQLCOM_SHOW_GRANTS),SHOW_LONG},
  {"Com_show_keys",	       (char*) (com_stat+(uint) SQLCOM_SHOW_KEYS),SHOW_LONG},
  {"Com_show_logs",	       (char*) (com_stat+(uint) SQLCOM_SHOW_LOGS),SHOW_LONG},
3192 3193
  {"Com_show_master_stat",     (char*) (com_stat+(uint) SQLCOM_SHOW_MASTER_STAT),SHOW_LONG},
  {"Com_show_new_master",      (char*) (com_stat+(uint) SQLCOM_SHOW_NEW_MASTER),SHOW_LONG},
3194 3195
  {"Com_show_open_tables",     (char*) (com_stat+(uint) SQLCOM_SHOW_OPEN_TABLES),SHOW_LONG},
  {"Com_show_processlist",     (char*) (com_stat+(uint) SQLCOM_SHOW_PROCESSLIST),SHOW_LONG},
3196
  {"Com_show_slave_hosts",     (char*) (com_stat+(uint) SQLCOM_SHOW_SLAVE_HOSTS),SHOW_LONG},
3197 3198 3199 3200 3201 3202 3203 3204 3205
  {"Com_show_slave_stat",      (char*) (com_stat+(uint) SQLCOM_SHOW_SLAVE_STAT),SHOW_LONG},
  {"Com_show_status",	       (char*) (com_stat+(uint) SQLCOM_SHOW_STATUS),SHOW_LONG},
  {"Com_show_tables",	       (char*) (com_stat+(uint) SQLCOM_SHOW_TABLES),SHOW_LONG},
  {"Com_show_variables",       (char*) (com_stat+(uint) SQLCOM_SHOW_VARIABLES),SHOW_LONG},
  {"Com_slave_start",	       (char*) (com_stat+(uint) SQLCOM_SLAVE_START),SHOW_LONG},
  {"Com_slave_stop",	       (char*) (com_stat+(uint) SQLCOM_SLAVE_STOP),SHOW_LONG},
  {"Com_truncate",	       (char*) (com_stat+(uint) SQLCOM_TRUNCATE),SHOW_LONG},
  {"Com_unlock_tables",	       (char*) (com_stat+(uint) SQLCOM_UNLOCK_TABLES),SHOW_LONG},
  {"Com_update",	       (char*) (com_stat+(uint) SQLCOM_UPDATE),SHOW_LONG},
3206
  {"Connections",              (char*) &thread_id,              SHOW_LONG_CONST},
3207
  {"Created_tmp_disk_tables",  (char*) &created_tmp_disk_tables,SHOW_LONG},
3208
  {"Created_tmp_tables",       (char*) &created_tmp_tables,     SHOW_LONG},
3209
  {"Created_tmp_files",	       (char*) &my_tmp_file_created,	SHOW_LONG},
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
  {"Delayed_insert_threads",   (char*) &delayed_insert_threads, SHOW_LONG},
  {"Delayed_writes",           (char*) &delayed_insert_writes,  SHOW_LONG},
  {"Delayed_errors",           (char*) &delayed_insert_errors,  SHOW_LONG},
  {"Flush_commands",           (char*) &refresh_version,        SHOW_LONG_CONST},
  {"Handler_delete",           (char*) &ha_delete_count,        SHOW_LONG},
  {"Handler_read_first",       (char*) &ha_read_first_count,    SHOW_LONG},
  {"Handler_read_key",         (char*) &ha_read_key_count,      SHOW_LONG},
  {"Handler_read_next",        (char*) &ha_read_next_count,     SHOW_LONG},
  {"Handler_read_prev",        (char*) &ha_read_prev_count,     SHOW_LONG},
  {"Handler_read_rnd",         (char*) &ha_read_rnd_count,      SHOW_LONG},
  {"Handler_read_rnd_next",    (char*) &ha_read_rnd_next_count, SHOW_LONG},
  {"Handler_update",           (char*) &ha_update_count,        SHOW_LONG},
  {"Handler_write",            (char*) &ha_write_count,         SHOW_LONG},
  {"Key_blocks_used",          (char*) &_my_blocks_used,        SHOW_LONG_CONST},
  {"Key_read_requests",        (char*) &_my_cache_r_requests,   SHOW_LONG},
  {"Key_reads",                (char*) &_my_cache_read,         SHOW_LONG},
  {"Key_write_requests",       (char*) &_my_cache_w_requests,   SHOW_LONG},
  {"Key_writes",               (char*) &_my_cache_write,        SHOW_LONG},
  {"Max_used_connections",     (char*) &max_used_connections,   SHOW_LONG},
  {"Not_flushed_key_blocks",   (char*) &_my_blocks_changed,     SHOW_LONG_CONST},
  {"Not_flushed_delayed_rows", (char*) &delayed_rows_in_use,    SHOW_LONG_CONST},
  {"Open_tables",              (char*) 0,                       SHOW_OPENTABLES},
  {"Open_files",               (char*) &my_file_opened,         SHOW_INT_CONST},
  {"Open_streams",             (char*) &my_stream_opened,       SHOW_INT_CONST},
  {"Opened_tables",            (char*) &opened_tables,          SHOW_LONG},
  {"Questions",                (char*) 0,                       SHOW_QUESTION},
3236
  {"Qcache_queries_in_cache",  (char*) &query_cache.queries_in_cache, SHOW_LONG_CONST},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3237 3238
  {"Qcache_inserts",           (char*) &query_cache.inserts,    SHOW_LONG},
  {"Qcache_hits",              (char*) &query_cache.hits,       SHOW_LONG},
3239
  {"Qcache_not_cached",        (char*) &query_cache.refused,    SHOW_LONG},
3240 3241 3242 3243 3244 3245
  {"Qcache_free_memory",       (char*) &query_cache.free_memory, 
   SHOW_LONG_CONST},
  {"Qcache_free_blocks",       (char*) &query_cache.free_memory_blocks,
   SHOW_LONG_CONST},
  {"Qcache_total_blocks",      (char*) &query_cache.total_blocks,
   SHOW_LONG_CONST},
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3246
  {"Rpl_status",               (char*) 0,                 SHOW_RPL_STATUS},
3247 3248 3249 3250 3251
  {"Select_full_join",         (char*) &select_full_join_count, SHOW_LONG},
  {"Select_full_range_join",   (char*) &select_full_range_join_count, SHOW_LONG},
  {"Select_range",             (char*) &select_range_count, 	SHOW_LONG},
  {"Select_range_check",       (char*) &select_range_check_count, SHOW_LONG},
  {"Select_scan",	       (char*) &select_scan_count,	SHOW_LONG},
3252
  {"Slave_open_temp_tables",   (char*) &slave_open_temp_tables, SHOW_LONG},
3253
  {"Slave_running",            (char*) 0, SHOW_SLAVE_RUNNING},
3254 3255
  {"Slow_launch_threads",      (char*) &slow_launch_threads,    SHOW_LONG},
  {"Slow_queries",             (char*) &long_query_count,       SHOW_LONG},
3256
  {"Sort_merge_passes",	       (char*) &filesort_merge_passes,  SHOW_LONG},
3257 3258 3259
  {"Sort_range",	       (char*) &filesort_range_count,   SHOW_LONG},
  {"Sort_rows",		       (char*) &filesort_rows,	        SHOW_LONG},
  {"Sort_scan",		       (char*) &filesort_scan_count,    SHOW_LONG},
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
3260
#ifdef HAVE_OPENSSL
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
  {"ssl_accepts",              (char*) 0,  	SHOW_SSL_CTX_SESS_ACCEPT},
  {"ssl_finished_accepts",     (char*) 0,  	SHOW_SSL_CTX_SESS_ACCEPT_GOOD},
  {"ssl_finished_connects",    (char*) 0,  	SHOW_SSL_CTX_SESS_CONNECT_GOOD},
  {"ssl_accept_renegotiates",  (char*) 0, 	SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE},
  {"ssl_connect_renegotiates", (char*) 0, 	SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE},
  {"ssl_callback_cache_hits",  (char*) 0,	SHOW_SSL_CTX_SESS_CB_HITS},
  {"ssl_session_cache_hits",   (char*) 0,	SHOW_SSL_CTX_SESS_HITS},
  {"ssl_session_cache_misses", (char*) 0,	SHOW_SSL_CTX_SESS_MISSES},
  {"ssl_session_cache_timeouts", (char*) 0,	SHOW_SSL_CTX_SESS_TIMEOUTS},
  {"ssl_used_session_cache_entries",(char*) 0,	SHOW_SSL_CTX_SESS_NUMBER},
  {"ssl_client_connects",      (char*) 0,	SHOW_SSL_CTX_SESS_CONNECT},
  {"ssl_session_cache_overflows", (char*) 0,	SHOW_SSL_CTX_SESS_CACHE_FULL},
  {"ssl_session_cache_size",   (char*) 0,	SHOW_SSL_CTX_SESS_GET_CACHE_SIZE},
  {"ssl_session_cache_mode",   (char*) 0,	SHOW_SSL_CTX_GET_SESSION_CACHE_MODE},
  {"ssl_sessions_reused",      (char*) 0,	SHOW_SSL_SESSION_REUSED},
  {"ssl_ctx_verify_mode",      (char*) 0,	SHOW_SSL_CTX_GET_VERIFY_MODE},
  {"ssl_ctx_verify_depth",     (char*) 0,	SHOW_SSL_CTX_GET_VERIFY_DEPTH},
  {"ssl_verify_mode",          (char*) 0,	SHOW_SSL_GET_VERIFY_MODE},
  {"ssl_verify_depth",         (char*) 0,	SHOW_SSL_GET_VERIFY_DEPTH},
  {"ssl_version",   	       (char*) 0,  	SHOW_SSL_GET_VERSION},
  {"ssl_cipher",               (char*) 0,  	SHOW_SSL_GET_CIPHER},
  {"ssl_cipher_list",          (char*) 0,  	SHOW_SSL_GET_CIPHER_LIST},
  {"ssl_default_timeout",      (char*) 0,  	SHOW_SSL_GET_DEFAULT_TIMEOUT},
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
3284
#endif /* HAVE_OPENSSL */
3285 3286
  {"Table_locks_immediate",    (char*) &locks_immediate,        SHOW_LONG},
  {"Table_locks_waited",       (char*) &locks_waited,           SHOW_LONG},
3287
  {"Threads_cached",           (char*) &cached_thread_count,    SHOW_LONG_CONST},
3288
  {"Threads_created",	       (char*) &thread_created,		SHOW_LONG_CONST},
3289 3290 3291 3292
  {"Threads_connected",        (char*) &thread_count,           SHOW_INT_CONST},
  {"Threads_running",          (char*) &thread_running,         SHOW_INT_CONST},
  {"Uptime",                   (char*) 0,                       SHOW_STARTTIME},
  {NullS, NullS, SHOW_LONG}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3293 3294 3295 3296 3297 3298 3299 3300
};

static void print_version(void)
{
  printf("%s  Ver %s for %s on %s\n",my_progname,
	 server_version,SYSTEM_TYPE,MACHINE_TYPE);
}

3301 3302 3303
static void use_help(void)
{
  print_version();
3304
  printf("Use '--help' or '--no-defaults --help' for a list of available options\n");
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3305
}
3306

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3307 3308 3309 3310
static void usage(void)
{
  print_version();
  puts("Copyright (C) 2000 MySQL AB & MySQL Finland AB, by Monty and others");
3311 3312
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,");
  puts("and you are welcome to modify and redistribute it under the GPL license\n");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
  puts("Starts the MySQL server\n");

  printf("Usage: %s [OPTIONS]\n", my_progname);
  puts("\n\
  --ansi		Use ANSI SQL syntax instead of MySQL syntax\n\
  -b, --basedir=path	Path to installation directory. All paths are\n\
			usually resolved relative to this\n\
  --big-tables		Allow big result sets by saving all temporary sets\n\
			on file (Solves most 'table full' errors)\n\
  --bind-address=IP	Ip address to bind to\n\
  --bootstrap		Used by mysql installation scripts\n\
  --character-sets-dir=...\n\
                        Directory where character sets are\n\
  --chroot=path		Chroot mysqld daemon during startup\n\
3327
  --core-file		Write core on errors\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3328 3329 3330 3331
  -h, --datadir=path	Path to the database root");
#ifndef DBUG_OFF
  printf("\
  -#, --debug[=...]     Debug log. Default is '%s'\n",default_dbug_option);
3332 3333 3334 3335
#ifdef SAFEMALLOC
  puts("\
  --skip-safemalloc     Don't use the memory allocation checking");
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3336 3337 3338 3339 3340 3341 3342 3343
#endif
  puts("\
  --default-character-set=charset\n\
			Set the default character set\n\
  --default-table-type=type\n\
			Set the default table type for tables\n\
  --delay-key-write-for-all-tables\n\
			Don't flush key buffers between writes for any MyISAM\n\
3344 3345 3346 3347 3348 3349 3350
			table\n");
#ifdef HAVE_OPENSSL
  puts("\
  --des-key-file        Load keys for des_encrypt() and des_encrypt\n\
                        from given file");
#endif /* HAVE_OPENSSL */
  puts("\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3351
  --enable-locking	Enable system locking\n\
3352
  --enable-pstack	Print a symbolic stack trace on failure\n\
3353
  -T, --exit-info	Used for debugging;  Use at your own risk!\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
  --flush		Flush tables to disk between SQL commands\n\
  -?, --help		Display this help and exit\n\
  --init-file=file	Read SQL commands from this file at startup\n\
  -L, --language=...	Client error messages in given language. May be\n\
			given as a full path\n\
  -l, --log[=file]	Log connections and queries to file\n\
  --log-bin[=file]      Log queries in new binary format (for replication)\n\
  --log-bin-index=file  File that holds the names for last binary log files\n\
  --log-update[=file]	Log updates to file.# where # is a unique number\n\
			if not given.\n\
3364
  --log-isam[=file]	Log all MyISAM changes to file\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3365 3366
  --log-long-format	Log some extra information to update log\n\
  --low-priority-updates INSERT/DELETE/UPDATE has lower priority than selects\n\
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3367
  --log-slow-queries=[file]\n\
3368 3369
			Log slow queries to this log file.  Defaults logging\n\
                        to hostname-slow.log\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3370
  --pid-file=path	Pid file used by safe_mysqld\n\
3371 3372
  --myisam-recover[=option[,option...]] where options is one of DEAULT,\n\
			BACKUP or FORCE.\n\
3373
  --memlock		Lock mysqld in memory\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3374
  -n, --new		Use very new possible 'unsafe' functions\n\
3375 3376
  -o, --old-protocol	Use the old (3.20) protocol\n\
  -P, --port=...	Port number to use for connection\n");
3377
#ifdef ONE_THREAD
3378
  puts("\
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3379
  --one-thread		Only use one thread (for debugging under Linux)\n");
3380
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3381
  puts("\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3382 3383 3384
  -O, --set-variable var=option\n\
			Give a variable an value. --help lists variables\n\
  --safe-mode		Skip some optimize stages (for testing)\n\
3385 3386
  --safe-show-database  Don't show databases for which the user has no\n\
                        privileges\n\
3387 3388
  --safe-user-create	Don't allow new user creation by the user who has\n\
                        no write privileges to the mysql.user table\n\
3389 3390
  --skip-concurrent-insert\n\
		        Don't use concurrent insert with MyISAM\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3391 3392
  --skip-delay-key-write\n\
			Ignore the delay_key_write option for all tables\n\
3393 3394
  --skip-grant-tables	Start without grant tables. This gives all users\n\
			FULL ACCESS to all tables!\n\
3395
  --skip-host-cache	Don't cache host names\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3396 3397 3398 3399 3400
  --skip-locking	Don't use system locking. To use isamchk one has\n\
			to shut down the server.\n\
  --skip-name-resolve	Don't resolve hostnames.\n\
			All hostnames are IP's or 'localhost'\n\
  --skip-networking	Don't allow connection with TCP/IP.\n\
3401
  --skip-new		Don't use new, possible wrong routines.\n");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3402 3403
  /* We have to break the string here because of VC++ limits */
  puts("\
3404
  --skip-stack-trace    Don't print a stack trace on failure\n\
3405
  --skip-symlink	Don't allow symlinking of tables\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3406 3407 3408 3409 3410
  --skip-show-database  Don't allow 'SHOW DATABASE' commands\n\
  --skip-thread-priority\n\
			Don't give threads different priorities.\n\
  --socket=...		Socket file to use for connection\n\
  -t, --tmpdir=path	Path for temporary files\n\
3411 3412
  --sql-mode=option[,option[,option...]] where option can be one of:\n\
                        REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES,\n\
3413 3414
                        IGNORE_SPACE, SERIALIZE, ONLY_FULL_GROUP_BY,\n\
			NO_UNSIGNED_SUBTRACTION.\n\
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3415 3416
  --transaction-isolation\n\
		        Default transaction isolation level\n\
3417
  --temp-pool           Use a pool of temporary files\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3418
  -u, --user=user_name	Run mysqld daemon as user\n\
3419 3420
  -V, --version		output version information and exit\n\
  -W, --warnings        Log some not critical warnings to the log file\n");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3421 3422 3423 3424 3425
#ifdef __WIN__
  puts("NT and Win32 specific options:\n\
  --console		Don't remove the console window\n\
  --install		Install mysqld as a service (NT)\n\
  --remove		Remove mysqld from the service list (NT)\n\
3426
  --standalone		Dummy option to start as a standalone program (NT)\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3427
");
3428 3429 3430 3431
#ifdef USE_SYMDIR
  puts("--use-symbolic-links	Enable symbolic link support");
#endif
  puts("");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3432 3433 3434 3435 3436 3437 3438
#endif
#ifdef HAVE_BERKELEY_DB
  puts("\
  --bdb-home=  directory  Berkeley home direcory\n\
  --bdb-lock-detect=#	  Berkeley lock detect\n\
                          (DEFAULT, OLDEST, RANDOM or YOUNGEST, # sec)\n\
  --bdb-logdir=directory  Berkeley DB log file directory\n\
3439
  --bdb-no-sync		  Don't synchronously flush logs\n\
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3440
  --bdb-no-recover	  Don't try to recover Berkeley DB tables on start\n\
3441
  --bdb-shared-data	  Start Berkeley DB in multi-process mode\n\
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3442 3443 3444
  --bdb-tmpdir=directory  Berkeley DB tempfile name\n\
  --skip-bdb		  Don't use berkeley db (will save memory)\n\
");
3445
#endif /* HAVE_BERKELEY_DB */
3446 3447
#ifdef HAVE_INNOBASE_DB
  puts("\
3448 3449
  --innodb_data_home_dir=dir   The common part for Innodb table spaces\n\
  --innodb_data_file_path=dir  Path to individual files and their sizes\n\
3450
  --innodb_flush_method=#  With which method to flush data\n\
3451 3452 3453 3454 3455 3456
  --innodb_flush_log_at_trx_commit[=#]\n\
			       Set to 0 if you don't want to flush logs\n\
  --innodb_log_arch_dir=dir    Where full logs should be archived\n\
  --innodb_log_archive[=#]     Set to 1 if you want to have logs archived\n\
  --innodb_log_group_home_dir=dir  Path to innodb log files.\n\
  --skip-innodb		       Don't use Innodb (will save memory)\n\
3457
");
3458
#endif /* HAVE_INNOBASE_DB */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474
  print_defaults("my",load_default_groups);
  puts("");

#include "sslopt-usage.h"

  fix_paths();
  set_ports();
  printf("\
To see what values a running MySQL server is using, type\n\
'mysqladmin variables' instead of 'mysqld --help'.\n\
The default values (after parsing the command line arguments) are:\n\n");

  printf("basedir:     %s\n",mysql_home);
  printf("datadir:     %s\n",mysql_real_data_home);
  printf("tmpdir:      %s\n",mysql_tmpdir);
  printf("language:    %s\n",language);
3475
#ifndef __WIN__
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3476
  printf("pid file:    %s\n",pidfile_name);
3477
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3478 3479 3480 3481
  if (opt_logname)
    printf("logfile:     %s\n",opt_logname);
  if (opt_update_logname)
    printf("update log:  %s\n",opt_update_logname);
3482 3483 3484 3485 3486 3487
  if (opt_bin_log)
  {
    printf("binary log:  %s\n",opt_bin_logname ? opt_bin_logname : "");
    printf("binary log index:  %s\n",
	   opt_binlog_index_name ? opt_binlog_index_name : "");
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3488 3489 3490
  if (opt_slow_logname)
    printf("update log:  %s\n",opt_slow_logname);
  printf("TCP port:    %d\n",mysql_port);
3491
#if defined(HAVE_SYS_UN_H)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531
  printf("Unix socket: %s\n",mysql_unix_port);
#endif
  if (my_disable_locking)
    puts("\nsystem locking is not in use");
  if (opt_noacl)
    puts("\nGrant tables are not used. All users have full access rights");
  printf("\nPossible variables for option --set-variable (-O) are:\n");
  for (uint i=0 ; changeable_vars[i].name ; i++)
    printf("%-20s  current value: %lu\n",
	   changeable_vars[i].name,
	   (ulong) *changeable_vars[i].varptr);
}


static void set_options(void)
{
  set_all_changeable_vars( changeable_vars );
#if !defined( my_pthread_setprio ) && !defined( HAVE_PTHREAD_SETSCHEDPARAM )
  opt_specialflag |= SPECIAL_NO_PRIOR;
#endif

  (void) strmov( default_charset, MYSQL_CHARSET);
  (void) strmov( language, LANGUAGE);
  (void) strmov( mysql_real_data_home, get_relative_path(DATADIR));
#ifdef __WIN__
  /* Allow Win32 users to move MySQL anywhere */
  {
    char prg_dev[LIBLEN];
    my_path(prg_dev,my_progname,"mysql/bin");
    strcat(prg_dev,"/../");			// Remove 'bin' to get base dir
    cleanup_dirname(mysql_home,prg_dev);
  }
#else
  const char *tmpenv;
  if ( !(tmpenv = getenv("MY_BASEDIR_VERSION")))
    tmpenv = DEFAULT_MYSQL_HOME;
  (void) strmov( mysql_home, tmpenv );
#endif

#if defined( HAVE_mit_thread ) || defined( __WIN__ ) || defined( HAVE_LINUXTHREADS )
3532
  my_disable_locking=myisam_single_user= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
#endif
  my_bind_addr = htonl( INADDR_ANY );
}

	/* Initiates DEBUG - but no debugging here ! */

static void get_options(int argc,char **argv)
{
  int c,option_index=0;

  myisam_delay_key_write=1;			// Allow use of this
3544
#ifndef HAVE_purify
3545
  my_use_symdir=1;				// Use internal symbolic links
3546 3547 3548 3549 3550 3551
#else
  /* Symlinks gives too many warnings with purify */
  my_disable_symlinks=1;
  my_use_symdir=0;
  have_symlink=SHOW_OPTION_DISABLED;
#endif
3552

tim@black.box's avatar
tim@black.box committed
3553
  optind = 0;   // setup in case getopt() was called previously
3554
  while ((c=getopt_long(argc,argv,"ab:C:h:#::T::?l::L:O:P:sS::t:u:noVvWI?",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3555 3556 3557 3558
			long_options, &option_index)) != EOF)
  {
    switch(c) {
    case '#':
3559
#ifndef DBUG_OFF
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3560
      DBUG_PUSH(optarg ? optarg : default_dbug_option);
3561
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3562 3563
      opt_endinfo=1;				/* unireg: memory allocation */
      break;
3564 3565 3566
    case 'W':
      opt_warnings=1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3567
    case 'a':
3568 3569 3570
      opt_sql_mode = (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT |
		      MODE_ANSI_QUOTES | MODE_IGNORE_SPACE | MODE_SERIALIZABLE
		      | MODE_ONLY_FULL_GROUP_BY);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3571
      default_tx_isolation= ISO_SERIALIZABLE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
      break;
    case 'b':
      strmov(mysql_home,optarg);
      break;
    case 'l':
      opt_log=1;
      opt_logname=optarg;			// Use hostname.log if null
      break;
    case 'h':
      strmov(mysql_real_data_home,optarg);
      break;
    case 'L':
      strmov(language,optarg);
      break;
    case 'n':
      opt_specialflag|= SPECIAL_NEW_FUNC;
      break;
    case 'o':
      protocol_version=PROTOCOL_VERSION-1;
      break;
    case 'O':
      if (set_changeable_var(optarg, changeable_vars))
      {
3595
	use_help();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3596 3597 3598 3599 3600 3601
	exit(1);
      }
      break;
    case 'P':
      mysql_port= (unsigned int) atoi(optarg);
      break;
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3602 3603 3604
    case OPT_SLAVE_SKIP_ERRORS:
      init_slave_skip_errors(optarg);
      break;
3605
    case OPT_SAFEMALLOC_MEM_LIMIT:
3606
#if !defined(DBUG_OFF) && defined(SAFEMALLOC)      
3607 3608
      safemalloc_mem_limit = atoi(optarg);
#endif      
3609
      break;
3610 3611 3612
    case OPT_RPL_RECOVERY_RANK:
      rpl_recovery_rank=atoi(optarg);
      break;
3613 3614 3615 3616 3617 3618
    case OPT_SLAVE_LOAD_TMPDIR:
      slave_load_tmpdir = my_strdup(optarg, MYF(MY_FAE));
      break;
    case OPT_OLD_RPL_COMPAT:
      opt_old_rpl_compat = 1;
      break;
3619 3620 3621
    case OPT_SHOW_SLAVE_AUTH_INFO:
      opt_show_slave_auth_info = 1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
    case OPT_SOCKET:
      mysql_unix_port= optarg;
      break;
    case 'r':
      mysqld_chroot=optarg;
      break;
#ifdef USE_SYMDIR
    case 's':
      my_use_symdir=1;			/* Use internal symbolic links */
      break;
#endif
    case 't':
      mysql_tmpdir=optarg;
      break;
3636 3637 3638
    case OPT_TEMP_POOL:
      use_temp_pool=1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650
    case 'u':
      mysqld_user=optarg;
      break;
    case 'v':
    case 'V':
      print_version();
      exit(0);
    case 'I':
    case '?':
      usage();
      exit(0);
    case 'T':
3651
      test_flags= optarg ? (uint) atoi(optarg) : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3652 3653 3654 3655 3656 3657
      opt_endinfo=1;
      break;
    case (int) OPT_BIG_TABLES:
      thd_startup_options|=OPTION_BIG_TABLES;
      break;
    case (int) OPT_ISAM_LOG:
3658
      opt_myisam_log=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3659
      if (optarg)
3660
	myisam_log_filename=optarg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3661 3662 3663 3664 3665
      break;
    case (int) OPT_UPDATE_LOG:
      opt_update_log=1;
      opt_update_logname=optarg;		// Use hostname.# if null
      break;
3666 3667 3668 3669 3670 3671 3672 3673
   case (int) OPT_RELAY_LOG_INDEX:
      opt_relaylog_index_name = optarg;
      break;
    case (int) OPT_RELAY_LOG:
      x_free(opt_relay_logname);
      if (optarg && optarg[0])
	opt_relay_logname=my_strdup(optarg,MYF(0));
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3674 3675 3676 3677 3678
    case (int) OPT_BIN_LOG_INDEX:
      opt_binlog_index_name = optarg;
      break;
    case (int) OPT_BIN_LOG:
      opt_bin_log=1;
3679 3680 3681
      x_free(opt_bin_logname);
      if (optarg && optarg[0])
	opt_bin_logname=my_strdup(optarg,MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3682
      break;
3683 3684 3685 3686
      // needs to be handled (as no-op) in non-debugging mode for test suite
    case (int)OPT_DISCONNECT_SLAVE_EVENT_COUNT:
#ifndef DBUG_OFF      
      disconnect_slave_event_count = atoi(optarg);
3687 3688 3689 3690 3691
#endif      
      break;
    case (int)OPT_ABORT_SLAVE_EVENT_COUNT:
#ifndef DBUG_OFF      
      abort_slave_event_count = atoi(optarg);
3692 3693
#endif      
      break;
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704
    case (int)OPT_SPORADIC_BINLOG_DUMP_FAIL:
#ifndef DBUG_OFF      
      opt_sporadic_binlog_dump_fail = 1;
#endif      
      break;
     case (int)OPT_MAX_BINLOG_DUMP_EVENTS:
#ifndef DBUG_OFF      
      max_binlog_dump_events = atoi(optarg);
#endif      
      break;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3705 3706 3707
    case (int) OPT_LOG_SLAVE_UPDATES:
      opt_log_slave_updates = 1;
      break;
3708

3709 3710 3711 3712 3713 3714 3715 3716
    case (int) OPT_INIT_RPL_ROLE:
    {
      int role;
      if ((role=find_type(optarg, &rpl_role_typelib, 2)) <= 0)
      {
	fprintf(stderr, "Unknown replication role: %s\n", optarg);
	exit(1);
      }
3717
      rpl_status = (role == 1) ?  RPL_AUTH_MASTER : RPL_IDLE_SLAVE;
3718 3719
      break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3720 3721
    case (int)OPT_REPLICATE_IGNORE_DB:
      {
3722
	i_string *db = new i_string(optarg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3723 3724 3725 3726 3727
	replicate_ignore_db.push_back(db);
        break;
      }
    case (int)OPT_REPLICATE_DO_DB:
      {
3728
	i_string *db = new i_string(optarg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3729 3730 3731
	replicate_do_db.push_back(db);
        break;
      }
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3732 3733 3734 3735
    case (int)OPT_REPLICATE_REWRITE_DB:
      {
	char* key = optarg,*p, *val;
	p = strstr(optarg, "->");
3736
	if (!p)
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3737 3738
	  {
	    fprintf(stderr,
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3739
		    "Bad syntax in replicate-rewrite-db - missing '->'!\n");
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3740 3741 3742 3743 3744 3745 3746
	    exit(1);
	  }
	val = p--;
	while(isspace(*p) && p > optarg) *p-- = 0;
	if(p == optarg)
	  {
	    fprintf(stderr,
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3747
		    "Bad syntax in replicate-rewrite-db - empty FROM db!\n");
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3748 3749 3750 3751 3752
	    exit(1);
	  }
	*val = 0;
	val += 2;
	while(*val && isspace(*val)) *val++;
3753
	if (!*val)
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3754 3755
	  {
	    fprintf(stderr,
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3756
		    "Bad syntax in replicate-rewrite-db - empty TO db!\n");
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3757 3758 3759 3760 3761 3762 3763
	    exit(1);
	  }

	i_string_pair* db_pair = new i_string_pair(key, val);
	replicate_rewrite_db.push_back(db_pair);
	break;
      }
3764

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3765 3766
    case (int)OPT_BINLOG_IGNORE_DB:
      {
3767
	i_string *db = new i_string(optarg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3768 3769 3770 3771 3772
	binlog_ignore_db.push_back(db);
        break;
      }
    case (int)OPT_BINLOG_DO_DB:
      {
3773
	i_string *db = new i_string(optarg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3774 3775 3776
	binlog_do_db.push_back(db);
        break;
      }
3777 3778
    case (int)OPT_REPLICATE_DO_TABLE:
      {
3779
	if (!do_table_inited)
3780 3781 3782
	  init_table_rule_hash(&replicate_do_table, &do_table_inited);
	if(add_table_rule(&replicate_do_table, optarg))
	  {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3783
	    fprintf(stderr, "Could not add do table rule '%s'!\n", optarg);
3784 3785 3786 3787 3788
	    exit(1);
	  }
	table_rules_on = 1;
	break;
      }
3789 3790
    case (int)OPT_REPLICATE_WILD_DO_TABLE:
      {
3791
	if (!wild_do_table_inited)
3792 3793 3794 3795
	  init_table_rule_array(&replicate_wild_do_table,
				&wild_do_table_inited);
	if(add_wild_table_rule(&replicate_wild_do_table, optarg))
	  {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3796
	    fprintf(stderr, "Could not add do table rule '%s'!\n", optarg);
3797 3798 3799 3800 3801 3802 3803
	    exit(1);
	  }
	table_rules_on = 1;
	break;
      }
    case (int)OPT_REPLICATE_WILD_IGNORE_TABLE:
      {
3804
	if (!wild_ignore_table_inited)
3805 3806 3807 3808
	  init_table_rule_array(&replicate_wild_ignore_table,
				&wild_ignore_table_inited);
	if(add_wild_table_rule(&replicate_wild_ignore_table, optarg))
	  {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3809
	    fprintf(stderr, "Could not add ignore table rule '%s'!\n", optarg);
3810 3811 3812 3813 3814
	    exit(1);
	  }
	table_rules_on = 1;
	break;
      }
3815 3816
    case (int)OPT_REPLICATE_IGNORE_TABLE:
      {
3817
	if (!ignore_table_inited)
3818 3819 3820
	  init_table_rule_hash(&replicate_ignore_table, &ignore_table_inited);
	if(add_table_rule(&replicate_ignore_table, optarg))
	  {
jcole@tetra.spaceapes.com's avatar
jcole@tetra.spaceapes.com committed
3821
	    fprintf(stderr, "Could not add ignore table rule '%s'!\n", optarg);
3822 3823 3824 3825 3826
	    exit(1);
	  }
	table_rules_on = 1;
	break;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3827 3828 3829 3830 3831 3832 3833
    case (int) OPT_SQL_BIN_UPDATE_SAME:
      opt_sql_bin_update  = 1;
      break;
    case (int) OPT_SLOW_QUERY_LOG:
      opt_slow_log=1;
      opt_slow_logname=optarg;
      break;
3834 3835 3836
    case (int)OPT_SKIP_SLAVE_START:
      opt_skip_slave_start = 1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3837 3838 3839 3840
    case (int) OPT_SKIP_NEW:
      opt_specialflag|= SPECIAL_NO_NEW_FUNC;
      myisam_delay_key_write=0;
      myisam_concurrent_insert=0;
3841
      myisam_recover_options= HA_RECOVER_NONE;
3842
      my_disable_symlinks=1;
3843
      my_use_symdir=0;
3844
      have_symlink=SHOW_OPTION_DISABLED;
3845
      ha_open_options&= ~HA_OPEN_ABORT_IF_CRASHED;
3846
      query_cache_size=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3847 3848 3849 3850
      break;
    case (int) OPT_SAFE:
      opt_specialflag|= SPECIAL_SAFE_MODE;
      myisam_delay_key_write=0;
3851
      myisam_recover_options= HA_RECOVER_NONE;	// To be changed
3852
      ha_open_options&= ~HA_OPEN_ABORT_IF_CRASHED;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3853
      break;
3854 3855 3856
    case (int) OPT_SKIP_CONCURRENT_INSERT:
      myisam_concurrent_insert=0;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3857 3858 3859 3860 3861 3862 3863
    case (int) OPT_SKIP_PRIOR:
      opt_specialflag|= SPECIAL_NO_PRIOR;
      break;
    case (int) OPT_SKIP_GRANT:
      opt_noacl=1;
      break;
    case (int) OPT_SKIP_LOCK:
3864
      my_disable_locking=myisam_single_user= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3865 3866 3867 3868 3869
      break;
    case (int) OPT_SKIP_HOST_CACHE:
      opt_specialflag|= SPECIAL_NO_HOST_CACHE;
      break;
    case (int) OPT_ENABLE_LOCK:
3870
      my_disable_locking=myisam_single_user=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880
      break;
    case (int) OPT_USE_LOCKING:
      my_disable_locking=0;
      break;
    case (int) OPT_SKIP_RESOLVE:
      opt_specialflag|=SPECIAL_NO_RESOLVE;
      break;
    case (int) OPT_LONG_FORMAT:
      opt_specialflag|=SPECIAL_LONG_LOG_FORMAT;
      break;
3881 3882 3883
    case (int) OPT_NO_MIX_TYPE:
      opt_no_mix_types = 1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3884 3885 3886 3887 3888 3889 3890 3891 3892
    case (int) OPT_SKIP_NETWORKING:
      opt_disable_networking=1;
      mysql_port=0;
      break;
    case (int) OPT_SKIP_SHOW_DB:
      opt_skip_show_db=1;
      opt_specialflag|=SPECIAL_SKIP_SHOW_DB;
      mysql_port=0;
      break;
3893 3894 3895
    case (int) OPT_MEMLOCK:
      locked_in_memory=1;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3896 3897 3898
    case (int) OPT_ONE_THREAD:
      test_flags |= TEST_NO_THREADS;
      break;
3899 3900 3901
    case (int) OPT_WANT_CORE:
      test_flags |= TEST_CORE_ON_SIGNAL;
      break;
3902 3903 3904
    case (int) OPT_SKIP_STACK_TRACE:
      test_flags|=TEST_NO_STACKTRACE;
      break;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3905
    case (int) OPT_SKIP_SYMLINKS:
3906
      my_disable_symlinks=1;
3907
      my_use_symdir=0;
3908
      have_symlink=SHOW_OPTION_DISABLED;
3909
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951
    case (int) OPT_BIND_ADDRESS:
      if (optarg && isdigit(optarg[0]))
      {
	my_bind_addr = (ulong) inet_addr(optarg);
      }
      else
      {
	struct hostent *ent;
	if (!optarg || !optarg[0])
	  ent=gethostbyname(optarg);
	else
	{
	  char myhostname[255];
	  if (gethostname(myhostname,sizeof(myhostname)) < 0)
	  {
	    sql_perror("Can't start server: cannot get my own hostname!");
	    exit(1);
	  }
	  ent=gethostbyname(myhostname);
	}
	if (!ent)
	{
	  sql_perror("Can't start server: cannot resolve hostname!");
	  exit(1);
	}
	my_bind_addr = (ulong) ((in_addr*)ent->h_addr_list[0])->s_addr;
      }
      break;
    case (int) OPT_PID_FILE:
      strmov(pidfile_name,optarg);
      break;
    case (int) OPT_INIT_FILE:
      opt_init_file=optarg;
      break;
#ifdef __WIN__
    case (int) OPT_STANDALONE:		/* Dummy option for NT */
      break;
    case (int) OPT_CONSOLE:
      opt_console=1;
      break;
#endif
    case (int) OPT_FLUSH:
3952 3953 3954 3955
#ifdef HAVE_ISAM
      nisam_flush=1;
#endif
      myisam_flush=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3956 3957 3958 3959
      flush_time=0;			// No auto flush
      break;
    case OPT_LOW_PRIORITY_UPDATES:
      thd_startup_options|=OPTION_LOW_PRIORITY_UPDATES;
3960
      thr_upgraded_concurrent_insert_lock= TL_WRITE_LOW_PRIORITY;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
      low_priority_updates=1;
      break;
    case OPT_BOOTSTRAP:
      opt_noacl=opt_bootstrap=1;
      break;
    case OPT_TABLE_TYPE:
    {
      int type;
      if ((type=find_type(optarg, &ha_table_typelib, 2)) <= 0)
      {
	fprintf(stderr,"Unknown table type: %s\n",optarg);
	exit(1);
      }
      default_table_type= (enum db_type) type;
      break;
    }
3977 3978
    case OPT_SERVER_ID:
      server_id = atoi(optarg);
sasha@mysql.sashanet.com's avatar
sasha@mysql.sashanet.com committed
3979
      server_id_supplied = 1;
3980
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
    case OPT_DELAY_KEY_WRITE:
      ha_open_options|=HA_OPEN_DELAY_KEY_WRITE;
      myisam_delay_key_write=1;
      break;
    case OPT_SKIP_DELAY_KEY_WRITE:
      myisam_delay_key_write=0;
      break;
    case 'C':
      strmov(default_charset,optarg);
      break;
    case OPT_CHARSETS_DIR:
      strmov(mysql_charsets_dir, optarg);
      charsets_dir = mysql_charsets_dir;
      break;
#include "sslopt-case.h"
3996
    case OPT_DES_KEY_FILE:
3997
#ifdef HAVE_OPENSSL
3998 3999
      des_key_file=optarg;
#endif
4000
      break;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011
    case OPT_TX_ISOLATION:
    {
      int type;
      if ((type=find_type(optarg, &tx_isolation_typelib, 2)) <= 0)
      {
	fprintf(stderr,"Unknown transaction isolation type: %s\n",optarg);
	exit(1);
      }
      default_tx_isolation= (enum_tx_isolation) (type-1);
      break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4012 4013 4014 4015 4016 4017 4018 4019
#ifdef HAVE_BERKELEY_DB
    case OPT_BDB_LOG:
      berkeley_logdir=optarg;
      break;
    case OPT_BDB_HOME:
      berkeley_home=optarg;
      break;
    case OPT_BDB_NOSYNC:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4020
      berkeley_env_flags|=DB_TXN_NOSYNC;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4021
      break;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4022 4023
    case OPT_BDB_NO_RECOVER:
      berkeley_init_flags&= ~(DB_RECOVER);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
      break;
    case OPT_BDB_TMP:
      berkeley_tmpdir=optarg;
      break;
    case OPT_BDB_LOCK:
    {
      int type;
      if ((type=find_type(optarg, &berkeley_lock_typelib, 2)) > 0)
	berkeley_lock_type=berkeley_lock_types[type-1];
      else
      {
4035
	if (test_if_int(optarg,(uint) strlen(optarg)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4036 4037
	  berkeley_lock_scan_time=atoi(optarg);
	else
4038
	{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4039 4040 4041 4042 4043 4044
	  fprintf(stderr,"Unknown lock type: %s\n",optarg);
	  exit(1);
	}
      }
      break;
    }
4045 4046 4047 4048
    case OPT_BDB_SHARED:
      berkeley_init_flags&= ~(DB_PRIVATE);
      berkeley_shared_data=1;
      break;
4049
#endif /* HAVE_BERKELEY_DB */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4050
    case OPT_BDB_SKIP:
4051
#ifdef HAVE_BERKELEY_DB
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4052
      berkeley_skip=1;
4053
      have_berkeley_db=SHOW_OPTION_DISABLED;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4054
#endif
4055
      break;
4056
    case OPT_INNODB_SKIP:
4057
#ifdef HAVE_INNOBASE_DB
4058 4059
      innodb_skip=1;
      have_innodb=SHOW_OPTION_DISABLED;
4060 4061
#endif
      break;
4062
    case OPT_INNODB_DATA_FILE_PATH:
4063 4064
#ifdef HAVE_INNOBASE_DB
      innobase_data_file_path=optarg;
4065
#endif
4066
      break;
4067
#ifdef HAVE_INNOBASE_DB
4068
    case OPT_INNODB_DATA_HOME_DIR:
4069 4070
      innobase_data_home_dir=optarg;
      break;
4071
    case OPT_INNODB_LOG_GROUP_HOME_DIR:
4072 4073
      innobase_log_group_home_dir=optarg;
      break;
4074
    case OPT_INNODB_LOG_ARCH_DIR:
4075 4076
      innobase_log_arch_dir=optarg;
      break;
4077
    case OPT_INNODB_LOG_ARCHIVE:
4078 4079
      innobase_log_archive= optarg ? test(atoi(optarg)) : 1;
      break;
4080
    case OPT_INNODB_FLUSH_LOG_AT_TRX_COMMIT:
4081
      innobase_flush_log_at_trx_commit= optarg ? test(atoi(optarg)) : 1;
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
4082
      break;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
4083 4084
    case OPT_INNODB_FAST_SHUTDOWN:
      innobase_fast_shutdown= optarg ? test(atoi(optarg)) : 1;
4085
      break;
4086
    case OPT_INNODB_FLUSH_METHOD:
4087 4088
      innobase_unix_file_flush_method=optarg;
      break;
4089
#endif /* HAVE_INNOBASE_DB */
4090 4091 4092
    case OPT_DO_PSTACK:
      opt_do_pstack = 1;
      break;
4093 4094
    case OPT_MYISAM_RECOVER:
    {
4095
      if (!optarg || !optarg[0])
4096
      {
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
	myisam_recover_options=    HA_RECOVER_DEFAULT;
	myisam_recover_options_str= myisam_recover_typelib.type_names[0];
      }
      else
      {
	myisam_recover_options_str=optarg;
	if ((myisam_recover_options=
		find_bit_type(optarg, &myisam_recover_typelib)) == ~(ulong) 0)
	{
	  fprintf(stderr, "Unknown option to myisam-recover: %s\n",optarg);
	  exit(1);
	}
4109
      }
4110
      ha_open_options|=HA_OPEN_ABORT_IF_CRASHED;
4111 4112
      break;
    }
4113 4114 4115 4116 4117 4118 4119 4120 4121
    case OPT_SQL_MODE:
    {
      sql_mode_str = optarg;
      if ((opt_sql_mode =
	   find_bit_type(optarg, &sql_mode_typelib)) == ~(ulong) 0)
      {
	fprintf(stderr, "Unknown option to sql-mode: %s\n", optarg);
	exit(1);
      }
4122 4123 4124
      default_tx_isolation= ((opt_sql_mode & MODE_SERIALIZABLE) ?
			     ISO_SERIALIZABLE :
			     ISO_READ_COMMITTED);
4125 4126
      break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
    case OPT_MASTER_HOST:
      master_host=optarg;
      break;
    case OPT_MASTER_USER:
      master_user=optarg;
      break;
    case OPT_MASTER_PASSWORD:
      master_password=optarg;
      break;
    case OPT_MASTER_INFO_FILE:
      master_info_file=optarg;
      break;
4139 4140 4141
    case OPT_RELAY_LOG_INFO_FILE:
      relay_log_info_file=optarg;
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4142 4143 4144
    case OPT_MASTER_PORT:
      master_port= atoi(optarg);
      break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
4145 4146 4147 4148 4149 4150 4151 4152 4153
    case OPT_MASTER_SSL:
      master_ssl=atoi(optarg);
      break;
    case OPT_MASTER_SSL_KEY:
      master_ssl_key=optarg;
      break;
    case OPT_MASTER_SSL_CERT:
      master_ssl_cert=optarg;
      break;
4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    case OPT_REPORT_HOST:
      report_host=optarg;
      break;
    case OPT_REPORT_USER:
      report_user=optarg;
      break;
    case OPT_REPORT_PASSWORD:
      report_password=optarg;
      break;
    case OPT_REPORT_PORT:
      report_port= atoi(optarg);
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4166 4167 4168
    case OPT_MASTER_CONNECT_RETRY:
      master_connect_retry= atoi(optarg);
      break;
4169 4170 4171
    case OPT_MASTER_RETRY_COUNT:
      master_retry_count= atoi(optarg);
      break;
4172
    case OPT_SAFE_SHOW_DB:
4173 4174
      opt_safe_show_db=1;
      break;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4175 4176 4177
    case OPT_SAFE_USER_CREATE:
      opt_safe_user_create=1;
      break;
4178 4179 4180 4181 4182
    case OPT_SKIP_SAFEMALLOC:
#ifdef SAFEMALLOC
      sf_malloc_quick=1;
#endif
      break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4183 4184
    default:
      fprintf(stderr,"%s: Unrecognized option: %c\n",my_progname,c);
4185
      use_help();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4186 4187 4188
      exit(1);
    }
  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4189
  // Skip empty arguments (from shell)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4190 4191 4192 4193 4194
  while (argc != optind && !argv[optind][0])
    optind++;
  if (argc != optind)
  {
    fprintf(stderr,"%s: Too many parameters\n",my_progname);
4195
    use_help();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4196 4197
    exit(1);
  }
tim@black.box's avatar
tim@black.box committed
4198
  optind = 0;   // setup so that getopt_long() can be called again
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4199 4200
  fix_paths();
  default_table_type_name=ha_table_typelib.type_names[default_table_type-1];
4201
  default_tx_isolation_name=tx_isolation_typelib.type_names[default_tx_isolation];
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4202 4203 4204
  /* To be deleted in MySQL 4.0 */
  if (!record_rnd_cache_size)
    record_rnd_cache_size=my_default_record_cache_size;
4205 4206 4207 4208 4209 4210

  /* Fix variables that are base 1024*1024 */
  myisam_max_temp_length= (my_off_t) min(((ulonglong) myisam_max_sort_file_size)*1024*1024, (ulonglong) MAX_FILE_SIZE);
  myisam_max_extra_temp_length= (my_off_t) min(((ulonglong) myisam_max_extra_sort_file_size)*1024*1024, (ulonglong) MAX_FILE_SIZE);

  myisam_block_size=(uint) 1 << my_bit_log2(opt_myisam_block_size);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4211 4212 4213 4214 4215 4216
}


static char *get_relative_path(const char *path)
{
  if (test_if_hard_path(path) &&
4217
      is_prefix(path,DEFAULT_MYSQL_HOME) &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4218 4219
      strcmp(DEFAULT_MYSQL_HOME,FN_ROOTDIR))
  {
4220
    path+=(uint) strlen(DEFAULT_MYSQL_HOME);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4221 4222 4223 4224 4225 4226 4227
    while (*path == FN_LIBCHAR)
      path++;
  }
  return (char*) path;
}


4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249
/*
  Fix filename and replace extension where 'dir' is relative to
  mysql_real_data_home.
  Return 1 if len(path) > FN_REFLEN
*/

bool
fn_format_relative_to_data_home(my_string to, const char *name,
				const char *dir, const char *extension)
{
  char tmp_path[FN_REFLEN];
  if (!test_if_hard_path(dir))
  {
    strxnmov(tmp_path,sizeof(tmp_path)-1, mysql_real_data_home,
	     dir, NullS);
    dir=tmp_path;
  }
  return !fn_format(to, name, dir, extension,
		    MY_REPLACE_EXT | MY_UNPACK_FILENAME | MY_SAFE_PATH);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4250 4251 4252
static void fix_paths(void)
{
  (void) fn_format(mysql_home,mysql_home,"","",16); // Remove symlinks
4253 4254 4255
  convert_dirname(mysql_home,mysql_home,NullS);
  convert_dirname(mysql_real_data_home,mysql_real_data_home,NullS);
  convert_dirname(language,language,NullS);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4256 4257 4258 4259 4260 4261 4262 4263 4264
  (void) my_load_path(mysql_home,mysql_home,""); // Resolve current dir
  (void) my_load_path(mysql_real_data_home,mysql_real_data_home,mysql_home);
  (void) my_load_path(pidfile_name,pidfile_name,mysql_real_data_home);

  char buff[FN_REFLEN],*sharedir=get_relative_path(SHAREDIR);
  if (test_if_hard_path(sharedir))
    strmov(buff,sharedir);			/* purecov: tested */
  else
    strxmov(buff,mysql_home,sharedir,NullS);
4265
  convert_dirname(buff,buff,NullS);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
  (void) my_load_path(language,language,buff);

  /* If --character-sets-dir isn't given, use shared library dir */
  if (charsets_dir != mysql_charsets_dir)
  {
    strmov(strmov(mysql_charsets_dir,buff),CHARSET_DIR);
    charsets_dir=mysql_charsets_dir;
  }

  /* Add '/' to TMPDIR if needed */
  char *tmp= (char*) my_malloc(FN_REFLEN,MYF(MY_FAE));
  if (tmp)
  {
4279 4280 4281
    char *end=convert_dirname(tmp, mysql_tmpdir, NullS);

    mysql_tmpdir=(char*) my_realloc(tmp,(uint) (end-tmp)+1,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4282
				    MYF(MY_HOLD_ON_ERROR));
4283
    allocated_mysql_tmpdir=mysql_tmpdir;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4284
  }
4285 4286 4287
  if (!slave_load_tmpdir)
  {
    // no need to check return value, if we fail, my_malloc() never returns
4288
    slave_load_tmpdir = (char*) my_strdup(mysql_tmpdir, MYF(MY_FAE));
4289
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306
}


#ifdef SET_RLIMIT_NOFILE
static uint set_maximum_open_files(uint max_file_limit)
{
  struct rlimit rlimit;
  ulong old_cur;

  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
  {
    old_cur=rlimit.rlim_cur;
    if (rlimit.rlim_cur >= max_file_limit)	// Nothing to do
      return rlimit.rlim_cur;			/* purecov: inspected */
    rlimit.rlim_cur=rlimit.rlim_max=max_file_limit;
    if (setrlimit(RLIMIT_NOFILE,&rlimit))
    {
4307 4308
      sql_print_error("Warning: setrlimit couldn't increase number of open files to more than %lu (request: %u)",
		      old_cur, max_file_limit);	/* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4309 4310 4311 4312 4313 4314
      max_file_limit=old_cur;
    }
    else
    {
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
      if ((uint) rlimit.rlim_cur != max_file_limit)
4315 4316 4317
	sql_print_error("Warning: setrlimit returned ok, but didn't change limits. Max open files is %ld (request: %u)",
			(ulong) rlimit.rlim_cur,
			max_file_limit); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4318 4319 4320 4321 4322 4323 4324
      max_file_limit=rlimit.rlim_cur;
    }
  }
  return max_file_limit;
}
#endif

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347
#ifdef OS2
static uint set_maximum_open_files(uint max_file_limit)
{
   LONG     cbReqCount;
   ULONG    cbCurMaxFH, cbCurMaxFH0;
   APIRET   ulrc;

   // get current limit
   cbReqCount = 0;
   DosSetRelMaxFH( &cbReqCount, &cbCurMaxFH0);

   // set new limit
   cbReqCount = max_file_limit - cbCurMaxFH0;
   ulrc = DosSetRelMaxFH( &cbReqCount, &cbCurMaxFH);
   if (ulrc) {
      sql_print_error("Warning: DosSetRelMaxFH couldn't increase number of open files to more than %d",
         cbCurMaxFH0);
      cbCurMaxFH = cbCurMaxFH0;
   }

   return cbCurMaxFH;
}
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4348

4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366
	/*
	  Return a bitfield from a string of substrings separated by ','
	  returns ~(ulong) 0 on error.
	*/

static ulong find_bit_type(const char *x, TYPELIB *bit_lib)
{
  bool found_end;
  int  found_count;
  const char *end,*i,*j;
  const char **array, *pos;
  ulong found,found_int,bit;
  DBUG_ENTER("find_bit_type");
  DBUG_PRINT("enter",("x: '%s'",x));

  found=0;
  found_end= 0;
  pos=(my_string) x;
4367 4368 4369
  while (*pos == ' ') pos++;
  found_end= *pos == 0;
  while (!found_end)
4370 4371 4372 4373
  {
    if (!*(end=strcend(pos,',')))		/* Let end point at fieldend */
    {
      while (end > pos && end[-1] == ' ')
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4374
	end--;					/* Skip end-space */
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401
      found_end=1;
    }
    found_int=0; found_count=0;
    for (array=bit_lib->type_names, bit=1 ; (i= *array++) ; bit<<=1)
    {
      j=pos;
      while (j != end)
      {
	if (toupper(*i++) != toupper(*j++))
	  goto skipp;
      }
      found_int=bit;
      if (! *i)
      {
	found_count=1;
	break;
      }
      else if (j != pos)			// Half field found
      {
	found_count++;				// Could be one of two values
      }
skipp: ;
    }
    if (found_count != 1)
      DBUG_RETURN(~(ulong) 0);				// No unique value
    found|=found_int;
    pos=end+1;
4402
  }
4403 4404 4405 4406 4407 4408

  DBUG_PRINT("exit",("bit-field: %ld",(ulong) found));
  DBUG_RETURN(found);
} /* find_bit_type */


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4409 4410 4411 4412 4413 4414 4415 4416 4417
/*****************************************************************************
** Instantiate templates
*****************************************************************************/

#ifdef __GNUC__
/* Used templates */
template class I_List<THD>;
template class I_List_iterator<THD>;
template class I_List<i_string>;
4418
template class I_List<i_string_pair>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4419
#endif