~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzled_safe.in

  • Committer: Brian Aker
  • Date: 2008-08-16 15:41:14 UTC
  • mto: This revision was merged to the branch mainline in revision 346.
  • Revision ID: brian@tangent.org-20080816154114-eufmwf31p6ie1nd6
Cleaned up depend in Proto utils. Modified int to bool. Put TmpTable class
into play.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
 
3
# This file is public domain and comes with NO WARRANTY of any kind
 
4
#
 
5
# Script to start the Drizzle daemon and restart it if it dies unexpectedly
 
6
#
 
7
# This should be executed in the Drizzle base directory if you are using a
 
8
# binary installation that is not installed in its compile-time default
 
9
# location
 
10
#
 
11
# mysql.server works by first doing a cd to the base directory and from there
 
12
# executing drizzled_safe
 
13
 
 
14
KILL_DRIZZLED=1;
 
15
DRIZZLED=
 
16
niceness=0
 
17
# Initial logging status: error log is not open, and not using syslog
 
18
logging=init
 
19
want_syslog=0
 
20
syslog_tag=
 
21
user=@DRIZZLED_USER@
 
22
pid_file=
 
23
err_log=
 
24
 
 
25
prefix=@prefix@
 
26
exec_prefix=@exec_prefix@
 
27
sbindir=@sbindir@
 
28
libdir=@libdir@
 
29
includedir=@includedir@
 
30
 
 
31
 
 
32
syslog_tag_drizzled=drizzled
 
33
syslog_tag_drizzled_safe=drizzled_safe
 
34
 
 
35
trap '' 1 2 3 15                        # we shouldn't let anyone kill us
 
36
 
 
37
umask 007
 
38
 
 
39
defaults=
 
40
case "$1" in
 
41
    --no-defaults|--defaults-file=*|--defaults-extra-file=*)
 
42
      defaults="$1"; shift
 
43
      ;;
 
44
esac
 
45
 
 
46
usage () {
 
47
        cat <<EOF
 
48
Usage: $0 [OPTIONS]
 
49
  --no-defaults              Don't read the system defaults file
 
50
  --defaults-file=FILE       Use the specified defaults file
 
51
  --defaults-extra-file=FILE Also use defaults from the specified file
 
52
  --open-files-limit=LIMIT   Limit the number of open files
 
53
  --core-file-size=LIMIT     Limit core files to the specified size
 
54
  --timezone=TZ              Set the system timezone
 
55
  --drizzled=FILE              Use the specified file as drizzled
 
56
  --drizzled-version=VERSION   Use "drizzled-VERSION" as drizzled
 
57
  --nice=NICE                Set the scheduling priority of drizzled
 
58
  --skip-kill-drizzled         Don't try to kill stray drizzled processes
 
59
  --syslog                   Log messages to syslog with 'logger'
 
60
  --skip-syslog              Log messages to error log (default)
 
61
  --syslog-tag=TAG           Pass -t "drizzled-TAG" to 'logger'
 
62
 
 
63
All other options are passed to the drizzled program.
 
64
 
 
65
EOF
 
66
        exit 1
 
67
}
 
68
 
 
69
my_which ()
 
70
{
 
71
  save_ifs="${IFS-UNSET}"
 
72
  IFS=:
 
73
  for file
 
74
  do
 
75
    for dir in $PATH
 
76
    do
 
77
      if [ -f "$dir/$file" ]
 
78
      then
 
79
        echo "$dir/$file"
 
80
        continue 2
 
81
      fi
 
82
    done
 
83
    return 1  # Failure, didn't find file in path
 
84
  done
 
85
  if [ "$save_ifs" = UNSET ]
 
86
  then
 
87
    unset IFS
 
88
  else
 
89
    IFS="$save_ifs"
 
90
  fi
 
91
  return 0  # Success
 
92
}
 
93
 
 
94
log_generic () {
 
95
  priority="$1"
 
96
  shift
 
97
 
 
98
  msg="`date +'%y%m%d %H:%M:%S'` drizzled_safe $*"
 
99
  echo "$msg"
 
100
  case $logging in
 
101
    init) ;;  # Just echo the message, don't save it anywhere
 
102
    file) echo "$msg" >> "$err_log" ;;
 
103
    syslog) logger -t "$syslog_tag_drizzled_safe" -p "$priority" "$*" ;;
 
104
    *)
 
105
      echo "Internal program error (non-fatal):" \
 
106
           " unknown logging method '$logging'" >&2
 
107
      ;;
 
108
  esac
 
109
}
 
110
 
 
111
log_error () {
 
112
  log_generic daemon.error "$@" >&2
 
113
}
 
114
 
 
115
log_notice () {
 
116
  log_generic daemon.notice "$@"
 
117
}
 
118
 
 
119
eval_log_error () {
 
120
  cmd="$1"
 
121
  case $logging in
 
122
    file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;;
 
123
    syslog)
 
124
      # drizzled often prefixes its messages with a timestamp, which is
 
125
      # redundant when logging to syslog (which adds its own timestamp)
 
126
      # However, we don't strip the timestamp with sed here, because
 
127
      # sed buffers output (only GNU sed supports a -u (unbuffered) option)
 
128
      # which means that messages may not get sent to syslog until the
 
129
      # drizzled process quits.
 
130
      cmd="$cmd 2>&1 | logger -t '$syslog_tag_drizzled' -p daemon.error"
 
131
      ;;
 
132
    *)
 
133
      echo "Internal program error (non-fatal):" \
 
134
           " unknown logging method '$logging'" >&2
 
135
      ;;
 
136
  esac
 
137
 
 
138
  #echo "Running drizzled: [$cmd]"
 
139
  eval "$cmd"
 
140
}
 
141
 
 
142
shell_quote_string() {
 
143
  # This sed command makes sure that any special chars are quoted,
 
144
  # so the arg gets passed exactly to the server.
 
145
  echo "$1" | sed -e 's,\([^a-zA-Z0-9/_.=-]\),\\\1,g'
 
146
}
 
147
 
 
148
parse_arguments() {
 
149
  # We only need to pass arguments through to the server if we don't
 
150
  # handle them here.  So, we collect unrecognized options (passed on
 
151
  # the command line) into the args variable.
 
152
  pick_args=
 
153
  if test "$1" = PICK-ARGS-FROM-ARGV
 
154
  then
 
155
    pick_args=1
 
156
    shift
 
157
  fi
 
158
 
 
159
  for arg do
 
160
    val=`echo "$arg" | sed -e "s;--[^=]*=;;"`
 
161
    case "$arg" in
 
162
      # these get passed explicitly to drizzled
 
163
      --basedir=*) MY_BASEDIR_VERSION="$val" ;;
 
164
      --datadir=*) DATADIR="$val" ;;
 
165
      --pid-file=*) pid_file="$val" ;;
 
166
      --user=*) user="$val"; SET_USER=1 ;;
 
167
 
 
168
      # these might have been set in a [drizzled_safe] section of my.cnf
 
169
      # they are added to drizzled command line to override settings from my.cnf
 
170
      --log-error=*) err_log="$val" ;;
 
171
      --port=*) mysql_tcp_port="$val" ;;
 
172
      --socket=*) mysql_unix_port="$val" ;;
 
173
 
 
174
      # drizzled_safe-specific options - must be set in my.cnf ([drizzled_safe])!
 
175
      --core-file-size=*) core_file_size="$val" ;;
 
176
      --drizzled=*) DRIZZLED="$val" ;;
 
177
      --drizzled-version=*)
 
178
        if test -n "$val"
 
179
        then
 
180
          DRIZZLED="drizzled-$val"
 
181
        else
 
182
          DRIZZLED="drizzled"
 
183
        fi
 
184
        ;;
 
185
      --nice=*) niceness="$val" ;;
 
186
      --open-files-limit=*) open_files="$val" ;;
 
187
      --skip-kill-drizzled*) KILL_DRIZZLED=0 ;;
 
188
      --syslog) want_syslog=1 ;;
 
189
      --skip-syslog) want_syslog=0 ;;
 
190
      --syslog-tag=*) syslog_tag="$val" ;;
 
191
      --timezone=*) TZ="$val"; export TZ; ;;
 
192
 
 
193
      --help) usage ;;
 
194
 
 
195
      *)
 
196
        if test -n "$pick_args"
 
197
        then
 
198
          append_arg_to_args "$arg"
 
199
        fi
 
200
        ;;
 
201
    esac
 
202
  done
 
203
}
 
204
 
 
205
 
 
206
#
 
207
# First, try to find BASEDIR (where drizzled is)
 
208
#
 
209
 
 
210
if echo '@pkgdatadir@' | grep '^@prefix@' > /dev/null
 
211
then
 
212
  relpkgdata=`echo '@pkgdatadir@' | sed -e 's,^@prefix@,,' -e 's,^/,,' -e 's,^,./,'`
 
213
else
 
214
  # pkgdatadir is not relative to prefix
 
215
  relpkgdata='@pkgdatadir@'
 
216
fi
 
217
 
 
218
MY_PWD=`pwd`
 
219
# Check for the directories we would expect from a source install
 
220
if test -f "$prefix"/include/drizzled/error.h -a -x "${sbindir}"/drizzled
 
221
then
 
222
  MY_BASEDIR_VERSION=$MY_PWD            # Where libexec, share and var are
 
223
# Since we didn't find anything, used the compiled-in defaults
 
224
else
 
225
  MY_BASEDIR_VERSION=@prefix@
 
226
fi
 
227
 
 
228
 
 
229
#
 
230
# Second, try to find the data directory
 
231
#
 
232
 
 
233
if test -d $MY_BASEDIR_VERSION/var
 
234
then
 
235
  DATADIR=$MY_BASEDIR_VERSION/var
 
236
# Or just give up and use our compiled-in default
 
237
else
 
238
  DATADIR=@localstatedir@
 
239
fi
 
240
 
 
241
if test -z "$DRIZZLE_HOME"
 
242
then 
 
243
  if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
 
244
  then
 
245
    log_error "WARNING: Found two instances of my.cnf -
 
246
$MY_BASEDIR_VERSION/my.cnf and
 
247
$DATADIR/my.cnf
 
248
IGNORING $DATADIR/my.cnf"
 
249
 
 
250
    DRIZZLE_HOME=$MY_BASEDIR_VERSION
 
251
  elif test -r "$DATADIR/my.cnf"
 
252
  then
 
253
    log_error "WARNING: Found $DATADIR/my.cnf
 
254
The data directory is a deprecated location for my.cnf, please move it to
 
255
$MY_BASEDIR_VERSION/my.cnf"
 
256
    DRIZZLE_HOME=$DATADIR
 
257
  else
 
258
    DRIZZLE_HOME=$MY_BASEDIR_VERSION
 
259
  fi
 
260
fi
 
261
export DRIZZLE_HOME
 
262
 
 
263
 
 
264
# Get first arguments from the my.cnf file, groups [drizzled] and [drizzled_safe]
 
265
# and then merge with the command line arguments
 
266
if test -x ./bin/my_print_defaults
 
267
then
 
268
  print_defaults="./bin/my_print_defaults"
 
269
elif test -x @bindir@/my_print_defaults
 
270
then
 
271
  print_defaults="@bindir@/my_print_defaults"
 
272
elif test -x @bindir@/mysql_print_defaults
 
273
then
 
274
  print_defaults="@bindir@/mysql_print_defaults"
 
275
else
 
276
  print_defaults="my_print_defaults"
 
277
fi
 
278
 
 
279
append_arg_to_args () {
 
280
  args="$args "`shell_quote_string "$1"`
 
281
}
 
282
 
 
283
args=
 
284
 
 
285
SET_USER=2
 
286
parse_arguments `$print_defaults $defaults --loose-verbose drizzled server`
 
287
if test $SET_USER -eq 2
 
288
then
 
289
  SET_USER=0
 
290
fi
 
291
 
 
292
parse_arguments `$print_defaults $defaults --loose-verbose drizzled_safe safe_drizzled`
 
293
parse_arguments PICK-ARGS-FROM-ARGV "$@"
 
294
 
 
295
# Determine what logging facility to use
 
296
 
 
297
# Ensure that 'logger' exists, if it's requested
 
298
if [ $want_syslog -eq 1 ]
 
299
then
 
300
  my_which logger > /dev/null 2>&1
 
301
  if [ $? -ne 0 ]
 
302
  then
 
303
    log_error "--syslog requested, but no 'logger' program found.  Please ensure that 'logger' is in your PATH, or do not specify the --syslog option to drizzled_safe."
 
304
    exit 1
 
305
  fi
 
306
fi
 
307
 
 
308
if [ -n "$err_log" -o $want_syslog -eq 0 ]
 
309
then
 
310
  if [ -n "$err_log" ]
 
311
  then
 
312
    # drizzled adds ".err" if there is no extension on the --log-error
 
313
    # argument; must match that here, or drizzled_safe will write to a
 
314
    # different log file than drizzled
 
315
 
 
316
    # drizzled does not add ".err" to "--log-error=foo."; it considers a
 
317
    # trailing "." as an extension
 
318
    if expr "$err_log" : '.*\.[^/]*$' > /dev/null
 
319
    then
 
320
        :
 
321
    else
 
322
      err_log="$err_log".err
 
323
    fi
 
324
 
 
325
    case "$err_log" in
 
326
      /* ) ;;
 
327
      * ) err_log="$DATADIR/$err_log" ;;
 
328
    esac
 
329
  else
 
330
    err_log=$DATADIR/`@HOSTNAME@`.err
 
331
  fi
 
332
 
 
333
  append_arg_to_args "--log-error=$err_log"
 
334
 
 
335
  if [ $want_syslog -eq 1 ]
 
336
  then
 
337
    # User explicitly asked for syslog, so warn that it isn't used
 
338
    log_error "Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect."
 
339
  fi
 
340
 
 
341
  # Log to err_log file
 
342
  log_notice "Logging to '$err_log'."
 
343
  logging=file
 
344
else
 
345
  if [ -n "$syslog_tag" ]
 
346
  then
 
347
    # Sanitize the syslog tag
 
348
    syslog_tag=`echo "$syslog_tag" | sed -e 's/[^a-zA-Z0-9_-]/_/g'`
 
349
    syslog_tag_drizzled_safe="${syslog_tag_drizzled_safe}-$syslog_tag"
 
350
    syslog_tag_drizzled="${syslog_tag_drizzled}-$syslog_tag"
 
351
  fi
 
352
  log_notice "Logging to syslog."
 
353
  logging=syslog
 
354
fi
 
355
 
 
356
USER_OPTION=""
 
357
if test -w / -o "$USER" = "root"
 
358
then
 
359
  if test "$user" != "root" -o $SET_USER = 1
 
360
  then
 
361
    USER_OPTION="--user=$user"
 
362
  fi
 
363
  # Change the err log to the right user, if it is in use
 
364
  if [ $want_syslog -eq 0 ]; then
 
365
    touch $err_log
 
366
    chown $user $err_log
 
367
  fi
 
368
  if test -n "$open_files"
 
369
  then
 
370
    ulimit -n $open_files
 
371
    append_arg_to_args "--open-files-limit=$open_files"
 
372
  fi
 
373
fi
 
374
 
 
375
safe_mysql_unix_port=${mysql_unix_port:-${DRIZZLE_UNIX_PORT:-@DRIZZLE_UNIX_ADDR@}}
 
376
# Make sure that directory for $safe_mysql_unix_port exists
 
377
mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
 
378
if [ ! -d $mysql_unix_port_dir ]
 
379
then
 
380
  mkdir $mysql_unix_port_dir
 
381
  chown $user $mysql_unix_port_dir
 
382
  chmod 755 $mysql_unix_port_dir
 
383
fi
 
384
 
 
385
# If the user doesn't specify a binary, we assume name "drizzled"
 
386
if test -z "$DRIZZLED"
 
387
then
 
388
  DRIZZLED=drizzled
 
389
fi
 
390
 
 
391
if test ! -x $sbindir/$DRIZZLED
 
392
then
 
393
  log_error "The file $sbindir/$DRIZZLED
 
394
does not exist or is not executable. Please cd to the mysql installation
 
395
directory and restart this script from there as follows:
 
396
./bin/drizzled_safe&
 
397
See http://dev.mysql.com/doc/mysql/en/drizzled-safe.html for more information"
 
398
  exit 1
 
399
fi
 
400
 
 
401
if test -z "$pid_file"
 
402
then
 
403
  pid_file=$DATADIR/`@HOSTNAME@`.pid
 
404
else
 
405
  case "$pid_file" in
 
406
    /* ) ;;
 
407
    * )  pid_file="$DATADIR/$pid_file" ;;
 
408
  esac
 
409
fi
 
410
append_arg_to_args "--pid-file=$pid_file"
 
411
 
 
412
if test -n "$mysql_unix_port"
 
413
then
 
414
  append_arg_to_args "--socket=$mysql_unix_port"
 
415
fi
 
416
if test -n "$mysql_tcp_port"
 
417
then
 
418
  append_arg_to_args "--port=$mysql_tcp_port"
 
419
fi
 
420
 
 
421
if test $niceness -eq 0
 
422
then
 
423
  NOHUP_NICENESS="nohup"
 
424
else
 
425
  NOHUP_NICENESS="nohup nice -$niceness"
 
426
fi
 
427
 
 
428
# Using nice with no args to get the niceness level is GNU-specific.
 
429
# This check could be extended for other operating systems (e.g.,
 
430
# BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
 
431
# But, it also seems that GNU nohup is the only one which messes
 
432
# with the priority, so this is okay.
 
433
if nohup nice > /dev/null 2>&1
 
434
then
 
435
    normal_niceness=`nice`
 
436
    nohup_niceness=`nohup nice 2>/dev/null`
 
437
 
 
438
    numeric_nice_values=1
 
439
    for val in $normal_niceness $nohup_niceness
 
440
    do
 
441
        case "$val" in
 
442
            -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
 
443
             [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
 
444
                ;;
 
445
            * )
 
446
                numeric_nice_values=0 ;;
 
447
        esac
 
448
    done
 
449
 
 
450
    if test $numeric_nice_values -eq 1
 
451
    then
 
452
        nice_value_diff=`expr $nohup_niceness - $normal_niceness`
 
453
        if test $? -eq 0 && test $nice_value_diff -gt 0 && \
 
454
            nice --$nice_value_diff echo testing > /dev/null 2>&1
 
455
        then
 
456
            # nohup increases the priority (bad), and we are permitted
 
457
            # to lower the priority with respect to the value the user
 
458
            # might have been given
 
459
            niceness=`expr $niceness - $nice_value_diff`
 
460
            NOHUP_NICENESS="nice -$niceness nohup"
 
461
        fi
 
462
    fi
 
463
else
 
464
    if nohup echo testing > /dev/null 2>&1
 
465
    then
 
466
        :
 
467
    else
 
468
        # nohup doesn't work on this system
 
469
        NOHUP_NICENESS=""
 
470
    fi
 
471
fi
 
472
 
 
473
# Try to set the core file size (even if we aren't root) because many systems
 
474
# don't specify a hard limit on core file size.
 
475
if test -n "$core_file_size"
 
476
then
 
477
  ulimit -c $core_file_size
 
478
fi
 
479
 
 
480
#
 
481
# If there exists an old pid file, check if the daemon is already running
 
482
# Note: The switches to 'ps' may depend on your operating system
 
483
if test -f $pid_file
 
484
then
 
485
  PID=`cat $pid_file`
 
486
  if @CHECK_PID@
 
487
  then
 
488
    if @FIND_PROC@
 
489
    then    # The pid contains a drizzled process
 
490
      log_error "A drizzled process already exists"
 
491
      exit 1
 
492
    fi
 
493
  fi
 
494
  rm -f $pid_file
 
495
  if test -f $pid_file
 
496
  then
 
497
    log_error "Fatal error: Can't remove the pid file:
 
498
$pid_file
 
499
Please remove it manually and start $0 again;
 
500
drizzled daemon not started"
 
501
    exit 1
 
502
  fi
 
503
fi
 
504
 
 
505
#
 
506
# Uncomment the following lines if you want all tables to be automatically
 
507
# checked and repaired during startup. You should add sensible key_buffer
 
508
# and sort_buffer values to my.cnf to improve check performance or require
 
509
# less disk space.
 
510
# Alternatively, you can start drizzled with the "myisam-recover" option. See
 
511
# the manual for details.
 
512
#
 
513
# echo "Checking tables in $DATADIR"
 
514
# $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
 
515
# $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
 
516
 
 
517
# Does this work on all systems?
 
518
#if type ulimit | grep "shell builtin" > /dev/null
 
519
#then
 
520
#  ulimit -n 256 > /dev/null 2>&1               # Fix for BSD and FreeBSD systems
 
521
#fi
 
522
 
 
523
cmd="$NOHUP_NICENESS"
 
524
 
 
525
for i in  "$sbindir/$DRIZZLED" "$defaults" "--basedir=$MY_BASEDIR_VERSION" \
 
526
  "--datadir=$DATADIR" "$USER_OPTION"
 
527
do
 
528
  cmd="$cmd "`shell_quote_string "$i"`
 
529
done
 
530
cmd="$cmd $args"
 
531
# Avoid 'nohup: ignoring input' warning
 
532
test -n "$NOHUP_NICENESS" && cmd="$cmd < /dev/null"
 
533
 
 
534
log_notice "Starting $DRIZZLED daemon with databases from $DATADIR"
 
535
while true
 
536
do
 
537
  rm -f $safe_mysql_unix_port $pid_file # Some extra safety
 
538
 
 
539
  eval_log_error "$cmd"
 
540
 
 
541
  if test ! -f $pid_file                # This is removed if normal shutdown
 
542
  then
 
543
    break
 
544
  fi
 
545
 
 
546
  if @TARGET_LINUX@ && test $KILL_DRIZZLED -eq 1
 
547
  then
 
548
    # Test if one process was hanging.
 
549
    # This is only a fix for Linux (running as base 3 drizzled processes)
 
550
    # but should work for the rest of the servers.
 
551
    # The only thing is ps x => redhat 5 gives warnings when using ps -x.
 
552
    # kill -9 is used or the process won't react on the kill.
 
553
    numofproces=`ps xaww | grep -v "grep" | grep "$sbindir/$DRIZZLED\>" | grep -c "pid-file=$pid_file"`
 
554
 
 
555
    log_notice "Number of processes running now: $numofproces"
 
556
    I=1
 
557
    while test "$I" -le "$numofproces"
 
558
    do 
 
559
      PROC=`ps xaww | grep "$sbindir/$DRIZZLED\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
 
560
 
 
561
      for T in $PROC
 
562
      do
 
563
        break
 
564
      done
 
565
      #    echo "TEST $I - $T **"
 
566
      if kill -9 $T
 
567
      then
 
568
        log_error "$DRIZZLED process hanging, pid $T - killed"
 
569
      else
 
570
        break
 
571
      fi
 
572
      I=`expr $I + 1`
 
573
    done
 
574
  fi
 
575
  log_notice "drizzled restarted"
 
576
done
 
577
 
 
578
log_notice "drizzled from pid file $pid_file ended"
 
579