~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzled_safe.in

  • Committer: Brian Aker
  • Date: 2009-05-26 02:54:31 UTC
  • mfrom: (1022.2.34 mordred)
  • Revision ID: brian@gaz-20090526025431-hpjmsrr4j6nftuic
Merge Monty

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