mysqld_safe.c 16 KB
Newer Older
unknown's avatar
unknown committed
1
/*
unknown's avatar
unknown committed
2
  Copyright (c) 2003 Novell, Inc. All Rights Reserved.
unknown's avatar
unknown committed
3

unknown's avatar
unknown committed
4 5 6 7 8 9 10 11 12
  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.

  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.
unknown's avatar
unknown committed
13

unknown's avatar
unknown committed
14 15
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
unknown's avatar
unknown committed
16
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
unknown's avatar
unknown committed
17
*/
unknown's avatar
unknown committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/stat.h>
#include <monitor.h>
#include <strings.h>
#include <getopt.h>
#include <screen.h>
#include <dirent.h>

#include "my_config.h"
#include "my_manage.h"

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

	global variables
unknown's avatar
unknown committed
35

unknown's avatar
unknown committed
36 37 38
******************************************************************************/
char autoclose;
char basedir[PATH_MAX];
unknown's avatar
unknown committed
39
char checktables;
unknown's avatar
unknown committed
40 41 42 43 44 45 46 47 48 49
char datadir[PATH_MAX];
char pid_file[PATH_MAX];
char address[PATH_MAX];
char port[PATH_MAX];
char err_log[PATH_MAX];
char safe_log[PATH_MAX];
char mysqld[PATH_MAX];
char hostname[PATH_MAX];
char default_option[PATH_MAX];

unknown's avatar
unknown committed
50
FILE *log_fd= NULL;
unknown's avatar
unknown committed
51 52 53 54

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

	prototypes
unknown's avatar
unknown committed
55

unknown's avatar
unknown committed
56 57
******************************************************************************/

unknown's avatar
unknown committed
58
void usage(void);
unknown's avatar
unknown committed
59 60
void vlog(char *, va_list);
void log(char *, ...);
unknown's avatar
unknown committed
61
void start_defaults(int, char *[]);
unknown's avatar
unknown committed
62
void finish_defaults();
63
void read_defaults(arg_list_t *);
unknown's avatar
unknown committed
64 65
void parse_args(int, char *[]);
void get_options(int, char *[]);
unknown's avatar
unknown committed
66 67 68
void check_data_vol();
void check_setup();
void check_tables();
unknown's avatar
unknown committed
69
void mysql_start(int, char *[]);
70
void parse_setvar(char *arg);
unknown's avatar
unknown committed
71 72 73 74

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

	functions
unknown's avatar
unknown committed
75

unknown's avatar
unknown committed
76 77 78 79
******************************************************************************/

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

unknown's avatar
unknown committed
80
  usage()
unknown's avatar
unknown committed
81

unknown's avatar
unknown committed
82 83 84 85 86 87 88
  Show usage.

******************************************************************************/
void usage(void)
{
  // keep the screen up
  setscreenmode(SCR_NO_MODE);
unknown's avatar
unknown committed
89

unknown's avatar
unknown committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  puts("\
\n\
usage: mysqld_safe [options]\n\
\n\
Program to start the MySQL daemon and restart it if it dies unexpectedly.\n\
All options, besides those listed below, are passed on to the MySQL daemon.\n\
\n\
options:\n\
\n\
--autoclose                 Automatically close the mysqld_safe screen.\n\
\n\
--check-tables              Check the tables before starting the MySQL daemon.\n\
\n\
--err-log=<file>            Send the MySQL daemon error output to <file>.\n\
\n\
--help                      Show this help information.\n\
\n\
--mysqld=<file>             Use the <file> MySQL daemon.\n\
\n\
  ");
unknown's avatar
unknown committed
110

unknown's avatar
unknown committed
111 112 113 114 115
  exit(-1);
}

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

unknown's avatar
unknown committed
116
  vlog()
unknown's avatar
unknown committed
117

unknown's avatar
unknown committed
118 119 120 121 122 123 124
  Log the message.

******************************************************************************/
void vlog(char *format, va_list ap)
{
  vfprintf(stdout, format, ap);
  fflush(stdout);
unknown's avatar
unknown committed
125

unknown's avatar
unknown committed
126 127 128 129 130 131 132 133 134 135
  if (log_fd)
  {
    vfprintf(log_fd, format, ap);
    fflush(log_fd);
  }
}

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

  log()
unknown's avatar
unknown committed
136

unknown's avatar
unknown committed
137 138 139 140 141 142
  Log the message.

******************************************************************************/
void log(char *format, ...)
{
  va_list ap;
unknown's avatar
unknown committed
143

unknown's avatar
unknown committed
144 145 146
  va_start(ap, format);

  vlog(format, ap);
unknown's avatar
unknown committed
147

unknown's avatar
unknown committed
148 149 150 151 152 153
  va_end(ap);
}

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

	start_defaults()
unknown's avatar
unknown committed
154

unknown's avatar
unknown committed
155 156 157 158 159 160 161
	Start setting the defaults.

******************************************************************************/
void start_defaults(int argc, char *argv[])
{
  struct stat buf;
  int i;
unknown's avatar
unknown committed
162

unknown's avatar
unknown committed
163
  // default options
unknown's avatar
unknown committed
164
  static char *default_options[]=
unknown's avatar
unknown committed
165
  {
unknown's avatar
unknown committed
166 167 168 169
    "--no-defaults",
    "--defaults-file=",
    "--defaults-extra-file=",
    NULL
unknown's avatar
unknown committed
170
  };
unknown's avatar
unknown committed
171

unknown's avatar
unknown committed
172
  // autoclose
unknown's avatar
unknown committed
173 174
  autoclose= FALSE;

unknown's avatar
unknown committed
175 176
  // basedir
  get_basedir(argv[0], basedir);
unknown's avatar
unknown committed
177

unknown's avatar
unknown committed
178
  // check-tables
unknown's avatar
unknown committed
179 180
  checktables= FALSE;

unknown's avatar
unknown committed
181
  // hostname
unknown's avatar
unknown committed
182
  if (gethostname(hostname, PATH_MAX) < 0)
unknown's avatar
unknown committed
183 184
  {
    // default
unknown's avatar
unknown committed
185
    strcpy(hostname, "mysql");
unknown's avatar
unknown committed
186 187 188 189 190 191
  }

  // address
  snprintf(address, PATH_MAX, "0.0.0.0");

  // port
192
  snprintf(port, PATH_MAX, "%d", MYSQL_PORT);
unknown's avatar
unknown committed
193

unknown's avatar
unknown committed
194
  // default option
unknown's avatar
unknown committed
195 196 197 198 199 200 201 202 203 204
  default_option[0]= NULL;
  for (i= 0; (argc > 1) && default_options[i]; i++)
  {
    if (!strnicmp(argv[1], default_options[i], strlen(default_options[i])))
    {
      strncpy(default_option, argv[1], PATH_MAX);
      break;
    }
  }

unknown's avatar
unknown committed
205
  // set after basedir is established
unknown's avatar
unknown committed
206 207 208 209 210
  datadir[0]= NULL;
  pid_file[0]= NULL;
  err_log[0]= NULL;
  safe_log[0]= NULL;
  mysqld[0]= NULL;
unknown's avatar
unknown committed
211 212 213 214 215
}

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

	finish_defaults()
unknown's avatar
unknown committed
216

unknown's avatar
unknown committed
217 218 219 220 221 222 223
	Finish settig the defaults.

******************************************************************************/
void finish_defaults()
{
  struct stat buf;
  int i;
unknown's avatar
unknown committed
224

unknown's avatar
unknown committed
225
  // datadir
unknown's avatar
unknown committed
226 227 228
  if (!datadir[0])
    snprintf(datadir, PATH_MAX, "%s/data", basedir);

unknown's avatar
unknown committed
229
  // pid-file
unknown's avatar
unknown committed
230 231 232
  if (!pid_file[0])
    snprintf(pid_file, PATH_MAX, "%s/%s.pid", datadir, hostname);

unknown's avatar
unknown committed
233
  // err-log
unknown's avatar
unknown committed
234 235
  if (!err_log[0])
    snprintf(err_log, PATH_MAX, "%s/%s.err", datadir, hostname);
unknown's avatar
unknown committed
236 237

  // safe-log
unknown's avatar
unknown committed
238 239
  if (!safe_log[0])
    snprintf(safe_log, PATH_MAX, "%s/%s.safe", datadir, hostname);
unknown's avatar
unknown committed
240 241

  // mysqld
unknown's avatar
unknown committed
242 243 244
  if (!mysqld[0])
    snprintf(mysqld, PATH_MAX, "%s/bin/mysqld-max", basedir);

unknown's avatar
unknown committed
245 246 247 248 249 250 251 252 253
  if (stat(mysqld, &buf))
  {
    snprintf(mysqld, PATH_MAX, "%s/bin/mysqld", basedir);
  }
}

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

	read_defaults()
unknown's avatar
unknown committed
254

unknown's avatar
unknown committed
255 256 257
	Read the defaults.

******************************************************************************/
258
void read_defaults(arg_list_t *pal)
unknown's avatar
unknown committed
259
{
260
  arg_list_t al;
261
  char defaults_file[PATH_MAX];
unknown's avatar
unknown committed
262 263 264
  char mydefaults[PATH_MAX];
  char line[PATH_MAX];
  FILE *fp;
265

unknown's avatar
unknown committed
266 267 268 269 270
  // defaults output file
  snprintf(defaults_file, PATH_MAX, "%s/bin/defaults.out", basedir);
  remove(defaults_file);

  // mysqladmin file
unknown's avatar
unknown committed
271
  snprintf(mydefaults, PATH_MAX, "%s/bin/my_print_defaults", basedir);
unknown's avatar
unknown committed
272

273
  // args
274 275
  init_args(&al);
  add_arg(&al, mydefaults);
unknown's avatar
unknown committed
276 277
  if (default_option[0])
    add_arg(&al, default_option);
278 279 280 281
  add_arg(&al, "mysqld");
  add_arg(&al, "server");
  add_arg(&al, "mysqld_safe");
  add_arg(&al, "safe_mysqld");
282

unknown's avatar
unknown committed
283
  spawn(mydefaults, &al, TRUE, NULL, defaults_file, NULL);
284

285
  free_args(&al);
286

unknown's avatar
unknown committed
287 288 289 290 291
  // gather defaults
  if ((fp= fopen(defaults_file, "r")) != NULL)
  {
    while (fgets(line, PATH_MAX, fp))
    {
unknown's avatar
unknown committed
292
      char *p;
unknown's avatar
unknown committed
293

unknown's avatar
unknown committed
294
      // remove end-of-line character
unknown's avatar
unknown committed
295 296 297
      if ((p= strrchr(line, '\n')) != NULL)
	*p= '\0';

unknown's avatar
unknown committed
298
      // add the option as an argument
unknown's avatar
unknown committed
299 300 301 302 303 304 305 306
      add_arg(pal, line);
    }

    fclose(fp);
  }

  // remove file
  remove(defaults_file);
unknown's avatar
unknown committed
307 308 309 310 311
}

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

	parse_args()
unknown's avatar
unknown committed
312

unknown's avatar
unknown committed
313 314 315 316 317
	Get the options.

******************************************************************************/
void parse_args(int argc, char *argv[])
{
unknown's avatar
unknown committed
318
  int index= 0;
unknown's avatar
unknown committed
319
  int c;
unknown's avatar
unknown committed
320

unknown's avatar
unknown committed
321 322 323
  // parse options
  enum opts
  {
unknown's avatar
unknown committed
324
    OPT_BASEDIR= 0xFF,
unknown's avatar
unknown committed
325 326 327 328 329 330
    OPT_DATADIR,
    OPT_PID_FILE,
    OPT_BIND_ADDRESS,
    OPT_PORT,
    OPT_ERR_LOG,
    OPT_SAFE_LOG,
unknown's avatar
unknown committed
331
    OPT_MYSQLD,
332 333
    OPT_HELP,
    OPT_SETVAR
unknown's avatar
unknown committed
334
  };
unknown's avatar
unknown committed
335 336

  static struct option options[]=
unknown's avatar
unknown committed
337
  {
unknown's avatar
unknown committed
338 339 340 341 342 343 344 345 346 347 348 349 350
    {"autoclose", no_argument, &autoclose, TRUE},
    {"basedir", required_argument, 0, OPT_BASEDIR},
    {"check-tables", no_argument, &checktables, TRUE},
    {"datadir", required_argument, 0, OPT_DATADIR},
    {"pid-file", required_argument, 0, OPT_PID_FILE},
    {"bind-address", required_argument, 0, OPT_BIND_ADDRESS},
    {"port", required_argument, 0, OPT_PORT},
    {"err-log", required_argument, 0, OPT_ERR_LOG},
    {"safe-log", required_argument, 0, OPT_SAFE_LOG},
    {"mysqld", required_argument, 0, OPT_MYSQLD},
    {"help", no_argument, 0, OPT_HELP},
    {"set-variable", required_argument, 0, OPT_SETVAR},
    {0, 0, 0, 0}
unknown's avatar
unknown committed
351
  };
unknown's avatar
unknown committed
352

unknown's avatar
unknown committed
353
  // we have to reset getopt_long because we use it multiple times
unknown's avatar
unknown committed
354 355
  optind= 1;

unknown's avatar
unknown committed
356
  // turn off error reporting
unknown's avatar
unknown committed
357 358 359
  opterr= 0;

  while ((c= getopt_long(argc, argv, "b:h:P:", options, &index)) >= 0)
unknown's avatar
unknown committed
360
  {
unknown's avatar
unknown committed
361
    switch (c) {
unknown's avatar
unknown committed
362 363 364 365
    case OPT_BASEDIR:
    case 'b':
      strcpy(basedir, optarg);
      break;
unknown's avatar
unknown committed
366

unknown's avatar
unknown committed
367 368 369 370
    case OPT_DATADIR:
    case 'h':
      strcpy(datadir, optarg);
      break;
unknown's avatar
unknown committed
371

unknown's avatar
unknown committed
372 373 374
    case OPT_PID_FILE:
      strcpy(pid_file, optarg);
      break;
unknown's avatar
unknown committed
375

unknown's avatar
unknown committed
376 377 378
    case OPT_BIND_ADDRESS:
      strcpy(address, optarg);
      break;
unknown's avatar
unknown committed
379

unknown's avatar
unknown committed
380 381 382 383
    case OPT_PORT:
    case 'P':
      strcpy(port, optarg);
      break;
unknown's avatar
unknown committed
384

unknown's avatar
unknown committed
385 386 387
    case OPT_ERR_LOG:
      strcpy(err_log, optarg);
      break;
unknown's avatar
unknown committed
388

unknown's avatar
unknown committed
389 390 391
    case OPT_SAFE_LOG:
      strcpy(safe_log, optarg);
      break;
unknown's avatar
unknown committed
392

unknown's avatar
unknown committed
393 394 395
    case OPT_MYSQLD:
      strcpy(mysqld, optarg);
      break;
396 397 398 399

    case OPT_SETVAR:
      parse_setvar(optarg);
      break;
unknown's avatar
unknown committed
400

unknown's avatar
unknown committed
401 402 403
    case OPT_HELP:
      usage();
      break;
unknown's avatar
unknown committed
404

unknown's avatar
unknown committed
405 406 407 408 409 410 411
    default:
      // ignore
      break;
    }
  }
}

412 413 414 415 416 417 418
/*
  parse_setvar(char *arg)
  Pasrsing for port just to display the port num on the mysqld_safe screen
*/
void parse_setvar(char *arg)
{
  char *pos;
unknown's avatar
unknown committed
419

420 421
  if ((pos= strindex(arg, "port")))
  {
unknown's avatar
unknown committed
422
    for (; *pos && *pos != '='; pos++);
423 424 425 426
    if (*pos)
      strcpy(port, pos + 1);
  }
}
unknown's avatar
unknown committed
427

428 429 430 431
/******************************************************************************



unknown's avatar
unknown committed
432 433 434
/******************************************************************************

	get_options()
unknown's avatar
unknown committed
435

unknown's avatar
unknown committed
436 437 438 439 440
	Get the options.

******************************************************************************/
void get_options(int argc, char *argv[])
{
441
  arg_list_t al;
unknown's avatar
unknown committed
442

unknown's avatar
unknown committed
443 444 445 446
  // start defaults
  start_defaults(argc, argv);

  // default file arguments
447 448 449 450 451
  init_args(&al);
  add_arg(&al, "ignore");
  read_defaults(&al);
  parse_args(al.argc, al.argv);
  free_args(&al);
unknown's avatar
unknown committed
452

unknown's avatar
unknown committed
453 454
  // command-line arguments
  parse_args(argc, argv);
unknown's avatar
unknown committed
455

unknown's avatar
unknown committed
456 457 458 459 460 461 462
  // finish defaults
  finish_defaults();
}

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

	check_data_vol()
unknown's avatar
unknown committed
463

unknown's avatar
unknown committed
464 465 466 467 468 469 470 471 472
	Check the database volume.

******************************************************************************/
void check_data_vol()
{
  // warn if the data is on a Traditional volume
  struct volume_info vol;
  char buff[PATH_MAX];
  char *p;
unknown's avatar
unknown committed
473

unknown's avatar
unknown committed
474 475
  // clear struct
  memset(&vol, 0, sizeof(vol));
unknown's avatar
unknown committed
476

unknown's avatar
unknown committed
477 478
  // find volume name
  strcpy(buff, datadir);
unknown's avatar
unknown committed
479
  if (p= strchr(buff, ':'))
unknown's avatar
unknown committed
480 481
  {
    // terminate after volume name
unknown's avatar
unknown committed
482
    *p= 0;
unknown's avatar
unknown committed
483 484 485 486 487 488
  }
  else
  {
    // assume SYS volume
    strcpy(buff, "SYS");
  }
unknown's avatar
unknown committed
489

unknown's avatar
unknown committed
490 491
  // retrieve information
  netware_vol_info_from_name(&vol, buff);
492

unknown's avatar
unknown committed
493 494
  if ((vol.flags & VOL_NSS_PRESENT) == 0)
  {
495
    log("Error: Either the data directory does not exist or is not on an NSS volume!\n\n");
unknown's avatar
unknown committed
496 497 498 499 500 501 502
    exit(-1);
  }
}

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

	check_setup()
unknown's avatar
unknown committed
503

unknown's avatar
unknown committed
504 505 506 507 508
	Check the current setup.

******************************************************************************/
void check_setup()
{
unknown's avatar
unknown committed
509
  struct stat info;
unknown's avatar
unknown committed
510
  char temp[PATH_MAX];
unknown's avatar
unknown committed
511

unknown's avatar
unknown committed
512
  // remove any current pid_file
unknown's avatar
unknown committed
513 514
  if (!stat(pid_file, &info) && (remove(pid_file) < 0))
  {
unknown's avatar
unknown committed
515 516
    log("ERROR: Unable to remove current pid file!\n\n");
    exit(-1);
unknown's avatar
unknown committed
517 518 519
  }

  // check the data volume
unknown's avatar
unknown committed
520
  check_data_vol();
unknown's avatar
unknown committed
521

unknown's avatar
unknown committed
522 523 524 525 526 527 528 529 530 531 532 533
  // check for a database
  snprintf(temp, PATH_MAX, "%s/mysql/host.frm", datadir);
  if (stat(temp, &info))
  {
    log("ERROR: No database found in the data directory!\n\n");
    exit(-1);
  }
}

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

	check_tables()
unknown's avatar
unknown committed
534

unknown's avatar
unknown committed
535 536 537 538 539
	Check the database tables.

******************************************************************************/
void check_tables()
{
540
  arg_list_t al;
unknown's avatar
unknown committed
541
  char mycheck[PATH_MAX];
unknown's avatar
unknown committed
542 543 544 545 546
  char table[PATH_MAX];
  char db[PATH_MAX];
  DIR *datadir_entry, *db_entry, *table_entry;

  // status
unknown's avatar
unknown committed
547
  log("checking tables...\n");
unknown's avatar
unknown committed
548

unknown's avatar
unknown committed
549
  // list databases
unknown's avatar
unknown committed
550 551 552 553
  if ((datadir_entry= opendir(datadir)) == NULL)
  {
    return;
  }
unknown's avatar
unknown committed
554

unknown's avatar
unknown committed
555 556
  while ((db_entry= readdir(datadir_entry)) != NULL)
  {
unknown's avatar
unknown committed
557 558 559 560 561 562 563 564
    if (db_entry->d_name[0] == '.')
    {
      // Skip
    }
    else if (S_ISDIR(db_entry->d_type))
    {
      // create long db name
      snprintf(db, PATH_MAX, "%s/%s", datadir, db_entry->d_name);
unknown's avatar
unknown committed
565

unknown's avatar
unknown committed
566
      // list tables
unknown's avatar
unknown committed
567
      if ((db_entry= opendir(db)) == NULL)
unknown's avatar
unknown committed
568
      {
unknown's avatar
unknown committed
569
	continue;
unknown's avatar
unknown committed
570
      }
unknown's avatar
unknown committed
571 572

      while ((table_entry= readdir(db_entry)) != NULL)
unknown's avatar
unknown committed
573
      {
unknown's avatar
unknown committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	// create long table name
	snprintf(table, PATH_MAX, "%s/%s", db, strlwr(table_entry->d_name));

	if (strindex(table, ".myi"))
	{
	  // ** myisamchk

	  // mysqladmin file
	  snprintf(mycheck, PATH_MAX, "%s/bin/myisamchk", basedir);

	  // args
	  init_args(&al);
	  add_arg(&al, mycheck);
	  add_arg(&al, "--silent");
	  add_arg(&al, "--force");
	  add_arg(&al, "--fast");
	  add_arg(&al, "--medium-check");
	  add_arg(&al, "-O");
	  add_arg(&al, "key_buffer=64M");
	  add_arg(&al, "-O");
	  add_arg(&al, "sort_buffer=64M");
	  add_arg(&al, table);

	  spawn(mycheck, &al, TRUE, NULL, NULL, NULL);

	  free_args(&al);
	}
	else if (strindex(table, ".ism"))
	{
	  // ** isamchk

	  // mysqladmin file
	  snprintf(mycheck, PATH_MAX, "%s/bin/isamchk", basedir);

	  // args
	  init_args(&al);
	  add_arg(&al, mycheck);
	  add_arg(&al, "--silent");
	  add_arg(&al, "--force");
	  add_arg(&al, "-O");
	  add_arg(&al, "sort_buffer=64M");
	  add_arg(&al, table);

	  spawn(mycheck, &al, TRUE, NULL, NULL, NULL);

	  free_args(&al);
	}
unknown's avatar
unknown committed
621 622
      }
    }
unknown's avatar
unknown committed
623
  }
unknown's avatar
unknown committed
624 625 626 627 628
}

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

	mysql_start()
unknown's avatar
unknown committed
629

unknown's avatar
unknown committed
630 631 632 633 634
	Start the mysql server.

******************************************************************************/
void mysql_start(int argc, char *argv[])
{
635 636 637 638 639 640 641
  arg_list_t al;
  int i, j, err;
  struct stat info;
  time_t cal;
  struct tm lt;
  char stamp[PATH_MAX];
  char skip;
unknown's avatar
unknown committed
642

unknown's avatar
unknown committed
643
  // private options
unknown's avatar
unknown committed
644
  static char *private_options[]=
unknown's avatar
unknown committed
645
  {
646
    "--autoclose",
unknown's avatar
unknown committed
647 648
    "--check-tables",
    "--help",
649 650 651
    "--err-log=",
    "--mysqld=",
    NULL
unknown's avatar
unknown committed
652
  };
unknown's avatar
unknown committed
653

654 655 656
  // args
  init_args(&al);
  add_arg(&al, "%s", mysqld);
unknown's avatar
unknown committed
657

658
  // parent args
unknown's avatar
unknown committed
659
  for (i= 1; i < argc; i++)
660
  {
unknown's avatar
unknown committed
661 662
    skip= FALSE;

unknown's avatar
unknown committed
663
    // skip private arguments
unknown's avatar
unknown committed
664
    for (j= 0; private_options[j]; j++)
unknown's avatar
unknown committed
665
    {
unknown's avatar
unknown committed
666
      if (!strnicmp(argv[i], private_options[j], strlen(private_options[j])))
unknown's avatar
unknown committed
667
      {
unknown's avatar
unknown committed
668 669
	skip= TRUE;
	break;
unknown's avatar
unknown committed
670 671
      }
    }
unknown's avatar
unknown committed
672

673 674 675 676 677
    if (!skip)
    {
      add_arg(&al, "%s", argv[i]);
    }
  }
unknown's avatar
unknown committed
678
  // spawn
679 680 681
  do
  {
    // check the database tables
unknown's avatar
unknown committed
682 683 684
    if (checktables)
      check_tables();

685
    // status
unknown's avatar
unknown committed
686 687 688 689
    time(&cal);
    localtime_r(&cal, &lt);
    strftime(stamp, PATH_MAX, "%d %b %Y %H:%M:%S", &lt);
    log("mysql started    : %s\n", stamp);
unknown's avatar
unknown committed
690

691 692 693 694
    // spawn mysqld
    spawn(mysqld, &al, TRUE, NULL, NULL, err_log);
  }
  while (!stat(pid_file, &info));
unknown's avatar
unknown committed
695

696
  // status
unknown's avatar
unknown committed
697 698 699 700
  time(&cal);
  localtime_r(&cal, &lt);
  strftime(stamp, PATH_MAX, "%d %b %Y %H:%M:%S", &lt);
  log("mysql stopped    : %s\n\n", stamp);
unknown's avatar
unknown committed
701

702 703
  // free args
  free_args(&al);
unknown's avatar
unknown committed
704 705 706 707 708
}

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

	main()
unknown's avatar
unknown committed
709

unknown's avatar
unknown committed
710 711 712
******************************************************************************/
int main(int argc, char **argv)
{
unknown's avatar
unknown committed
713 714
  char temp[PATH_MAX];

unknown's avatar
unknown committed
715
  // get the options
unknown's avatar
unknown committed
716
  get_options(argc, argv);
unknown's avatar
unknown committed
717 718

  // keep the screen up
unknown's avatar
unknown committed
719 720 721
  if (!autoclose)
    setscreenmode(SCR_NO_MODE);

unknown's avatar
unknown committed
722
  // create log file
unknown's avatar
unknown committed
723 724
  log_fd= fopen(safe_log, "w+");

unknown's avatar
unknown committed
725 726
  // header
  log("MySQL Server %s, for %s (%s)\n\n", VERSION, SYSTEM_TYPE, MACHINE_TYPE);
unknown's avatar
unknown committed
727 728 729 730

  // status
  log("address          : %s\n", address);
  log("port             : %s\n", port);
unknown's avatar
unknown committed
731
  log("daemon           : %s\n", mysqld);
unknown's avatar
unknown committed
732 733 734 735 736
  log("base directory   : %s\n", basedir);
  log("data directory   : %s\n", datadir);
  log("pid file         : %s\n", pid_file);
  log("error file       : %s\n", err_log);
  log("log file         : %s\n", safe_log);
unknown's avatar
unknown committed
737
  log("\n");
unknown's avatar
unknown committed
738

unknown's avatar
unknown committed
739 740
  // check setup
  check_setup();
unknown's avatar
unknown committed
741

unknown's avatar
unknown committed
742
  // start the MySQL server
unknown's avatar
unknown committed
743 744 745 746 747 748
  mysql_start(argc, argv);

  // close log file
  if (log_fd)
    fclose(log_fd);

unknown's avatar
unknown committed
749 750
  return 0;
}