summaryrefslogtreecommitdiff
path: root/netware/mysqld_safe.c
blob: 9db8a441ca33692b0b5cc926f6cd99fbbae43355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
571
572
573
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
  Copyright (c) 2003 Novell, Inc. All Rights Reserved.

  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.

  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 <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

******************************************************************************/
char autoclose;
char basedir[PATH_MAX];
char checktables;
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];

FILE *log_fd= NULL;

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

	prototypes

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

void usage(void);
void vlog(char *, va_list);
void log(char *, ...);
void start_defaults(int, char *[]);
void finish_defaults();
void read_defaults(arg_list_t *);
void parse_args(int, char *[]);
void get_options(int, char *[]);
void check_data_vol();
void check_setup();
void check_tables();
void mysql_start(int, char *[]);
void parse_setvar(char *arg);

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

	functions

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

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

  usage()

  Show usage.

******************************************************************************/
void usage(void)
{
  // keep the screen up
  setscreenmode(SCR_NO_MODE);

  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\
  ");

  exit(-1);
}

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

  vlog()

  Log the message.

******************************************************************************/
void vlog(char *format, va_list ap)
{
  vfprintf(stdout, format, ap);
  fflush(stdout);

  if (log_fd)
  {
    vfprintf(log_fd, format, ap);
    fflush(log_fd);
  }
}

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

  log()

  Log the message.

******************************************************************************/
void log(char *format, ...)
{
  va_list ap;

  va_start(ap, format);

  vlog(format, ap);

  va_end(ap);
}

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

	start_defaults()

	Start setting the defaults.

******************************************************************************/
void start_defaults(int argc, char *argv[])
{
  struct stat buf;
  int i;

  // default options
  static char *default_options[]=
  {
    "--no-defaults",
    "--defaults-file=",
    "--defaults-extra-file=",
    NULL
  };

  // autoclose
  autoclose= FALSE;

  // basedir
  get_basedir(argv[0], basedir);

  // check-tables
  checktables= FALSE;

  // hostname
  if (gethostname(hostname, PATH_MAX) < 0)
  {
    // default
    strcpy(hostname, "mysql");
  }

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

  // port
  snprintf(port, PATH_MAX, "3306");

  // default option
  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;
    }
  }

  // set after basedir is established
  datadir[0]= NULL;
  pid_file[0]= NULL;
  err_log[0]= NULL;
  safe_log[0]= NULL;
  mysqld[0]= NULL;
}

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

	finish_defaults()

	Finish settig the defaults.

******************************************************************************/
void finish_defaults()
{
  struct stat buf;
  int i;

  // datadir
  if (!datadir[0])
    snprintf(datadir, PATH_MAX, "%s/data", basedir);

  // pid-file
  if (!pid_file[0])
    snprintf(pid_file, PATH_MAX, "%s/%s.pid", datadir, hostname);

  // err-log
  if (!err_log[0])
    snprintf(err_log, PATH_MAX, "%s/%s.err", datadir, hostname);

  // safe-log
  if (!safe_log[0])
    snprintf(safe_log, PATH_MAX, "%s/%s.safe", datadir, hostname);

  // mysqld
  if (!mysqld[0])
    snprintf(mysqld, PATH_MAX, "%s/bin/mysqld-max", basedir);

  if (stat(mysqld, &buf))
  {
    snprintf(mysqld, PATH_MAX, "%s/bin/mysqld", basedir);
  }
}

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

	read_defaults()

	Read the defaults.

******************************************************************************/
void read_defaults(arg_list_t *pal)
{
  arg_list_t al;
  char defaults_file[PATH_MAX];
  char mydefaults[PATH_MAX];
  char line[PATH_MAX];
  FILE *fp;

  // defaults output file
  snprintf(defaults_file, PATH_MAX, "%s/bin/defaults.out", basedir);
  remove(defaults_file);

  // mysqladmin file
  snprintf(mydefaults, PATH_MAX, "%s/bin/my_print_defaults", basedir);

  // args
  init_args(&al);
  add_arg(&al, mydefaults);
  if (default_option[0])
    add_arg(&al, default_option);
  add_arg(&al, "mysqld");
  add_arg(&al, "server");
  add_arg(&al, "mysqld_safe");
  add_arg(&al, "safe_mysqld");

  spawn(mydefaults, &al, TRUE, NULL, defaults_file, NULL);

  free_args(&al);

  // gather defaults
  if ((fp= fopen(defaults_file, "r")) != NULL)
  {
    while (fgets(line, PATH_MAX, fp))
    {
      char *p;

      // remove end-of-line character
      if ((p= strrchr(line, '\n')) != NULL)
	*p= '\0';

      // add the option as an argument
      add_arg(pal, line);
    }

    fclose(fp);
  }

  // remove file
  remove(defaults_file);
}

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

	parse_args()

	Get the options.

******************************************************************************/
void parse_args(int argc, char *argv[])
{
  int index= 0;
  int c;

  // parse options
  enum opts
  {
    OPT_BASEDIR= 0xFF,
    OPT_DATADIR,
    OPT_PID_FILE,
    OPT_BIND_ADDRESS,
    OPT_PORT,
    OPT_ERR_LOG,
    OPT_SAFE_LOG,
    OPT_MYSQLD,
    OPT_HELP,
    OPT_SETVAR
  };

  static struct option options[]=
  {
    {"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}
  };

  // we have to reset getopt_long because we use it multiple times
  optind= 1;

  // turn off error reporting
  opterr= 0;

  while ((c= getopt_long(argc, argv, "b:h:P:", options, &index)) >= 0)
  {
    switch (c) {
    case OPT_BASEDIR:
    case 'b':
      strcpy(basedir, optarg);
      break;

    case OPT_DATADIR:
    case 'h':
      strcpy(datadir, optarg);
      break;

    case OPT_PID_FILE:
      strcpy(pid_file, optarg);
      break;

    case OPT_BIND_ADDRESS:
      strcpy(address, optarg);
      break;

    case OPT_PORT:
    case 'P':
      strcpy(port, optarg);
      break;

    case OPT_ERR_LOG:
      strcpy(err_log, optarg);
      break;

    case OPT_SAFE_LOG:
      strcpy(safe_log, optarg);
      break;

    case OPT_MYSQLD:
      strcpy(mysqld, optarg);
      break;

    case OPT_SETVAR:
      parse_setvar(optarg);
      break;

    case OPT_HELP:
      usage();
      break;

    default:
      // ignore
      break;
    }
  }
}

/*
  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;

  if ((pos= strindex(arg, "port")))
  {
    for (; *pos && *pos != '='; pos++);
    if (*pos)
      strcpy(port, pos + 1);
  }
}

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



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

	get_options()

	Get the options.

******************************************************************************/
void get_options(int argc, char *argv[])
{
  arg_list_t al;

  // start defaults
  start_defaults(argc, argv);

  // default file arguments
  init_args(&al);
  add_arg(&al, "ignore");
  read_defaults(&al);
  parse_args(al.argc, al.argv);
  free_args(&al);

  // command-line arguments
  parse_args(argc, argv);

  // finish defaults
  finish_defaults();
}

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

	check_data_vol()

	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;

  // clear struct
  memset(&vol, 0, sizeof(vol));

  // find volume name
  strcpy(buff, datadir);
  if (p= strchr(buff, ':'))
  {
    // terminate after volume name
    *p= 0;
  }
  else
  {
    // assume SYS volume
    strcpy(buff, "SYS");
  }

  // retrieve information
  netware_vol_info_from_name(&vol, buff);

  if ((vol.flags & VOL_NSS_PRESENT) == 0)
  {
    log("Error: Either the data directory does not exist or is not on an NSS volume!\n\n");
    exit(-1);
  }
}

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

	check_setup()

	Check the current setup.

******************************************************************************/
void check_setup()
{
  struct stat info;
  char temp[PATH_MAX];

  // remove any current pid_file
  if (!stat(pid_file, &info) && (remove(pid_file) < 0))
  {
    log("ERROR: Unable to remove current pid file!\n\n");
    exit(-1);
  }

  // check the data volume
  check_data_vol();

  // 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()

	Check the database tables.

******************************************************************************/
void check_tables()
{
  arg_list_t al;
  char mycheck[PATH_MAX];
  char table[PATH_MAX];
  char db[PATH_MAX];
  DIR *datadir_entry, *db_entry, *table_entry;

  // status
  log("checking tables...\n");

  // list databases
  if ((datadir_entry= opendir(datadir)) == NULL)
  {
    return;
  }

  while ((db_entry= readdir(datadir_entry)) != NULL)
  {
    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);

      // list tables
      if ((db_entry= opendir(db)) == NULL)
      {
	continue;
      }

      while ((table_entry= readdir(db_entry)) != NULL)
      {
	// 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);
	}
      }
    }
  }
}

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

	mysql_start()

	Start the mysql server.

******************************************************************************/
void mysql_start(int argc, char *argv[])
{
  arg_list_t al;
  int i, j, err;
  struct stat info;
  time_t cal;
  struct tm lt;
  char stamp[PATH_MAX];
  char skip;

  // private options
  static char *private_options[]=
  {
    "--autoclose",
    "--check-tables",
    "--help",
    "--err-log=",
    "--mysqld=",
    NULL
  };

  // args
  init_args(&al);
  add_arg(&al, "%s", mysqld);

  // parent args
  for (i= 1; i < argc; i++)
  {
    skip= FALSE;

    // skip private arguments
    for (j= 0; private_options[j]; j++)
    {
      if (!strnicmp(argv[i], private_options[j], strlen(private_options[j])))
      {
	skip= TRUE;
	break;
      }
    }

    if (!skip)
    {
      add_arg(&al, "%s", argv[i]);
    }
  }
  // spawn
  do
  {
    // check the database tables
    if (checktables)
      check_tables();

    // status
    time(&cal);
    localtime_r(&cal, &lt);
    strftime(stamp, PATH_MAX, "%d %b %Y %H:%M:%S", &lt);
    log("mysql started    : %s\n", stamp);

    // spawn mysqld
    spawn(mysqld, &al, TRUE, NULL, NULL, err_log);
  }
  while (!stat(pid_file, &info));

  // status
  time(&cal);
  localtime_r(&cal, &lt);
  strftime(stamp, PATH_MAX, "%d %b %Y %H:%M:%S", &lt);
  log("mysql stopped    : %s\n\n", stamp);

  // free args
  free_args(&al);
}

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

	main()

******************************************************************************/
int main(int argc, char **argv)
{
  char temp[PATH_MAX];

  // get the options
  get_options(argc, argv);

  // keep the screen up
  if (!autoclose)
    setscreenmode(SCR_NO_MODE);

  // create log file
  log_fd= fopen(safe_log, "w+");

  // header
  log("MySQL Server %s, for %s (%s)\n\n", VERSION, SYSTEM_TYPE, MACHINE_TYPE);

  // status
  log("address          : %s\n", address);
  log("port             : %s\n", port);
  log("daemon           : %s\n", mysqld);
  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);
  log("\n");

  // check setup
  check_setup();

  // start the MySQL server
  mysql_start(argc, argv);

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

  return 0;
}