Die if --log, --pid, or --dest aren't accessible. Also die if po dir isn't accessible.

This commit is contained in:
Daniel Nichter
2012-01-17 13:12:08 -07:00
parent b2bac5c765
commit 6e2b670af6
5 changed files with 76 additions and 10 deletions

View File

@@ -102,8 +102,20 @@ parse_options() {
local file="$1" local file="$1"
shift shift
mkdir "$TMPDIR/po/" 2>/dev/null if [ ! -d "$TMPDIR/po/" ]; then
mkdir "$TMPDIR/po/"
if [ $? -ne 0 ]; then
echo "Cannot mkdir $TMPDIR/po/" >&2
exit 1
fi
fi
rm -rf "$TMPDIR"/po/* rm -rf "$TMPDIR"/po/*
if [ $? -ne 0 ]; then
echo "Cannot rm -rf $TMPDIR/po/*" >&2
exit 1
fi
( (
export PO_DIR="$TMPDIR/po" export PO_DIR="$TMPDIR/po"
cat "$file" | perl -ne ' cat "$file" | perl -ne '
@@ -399,6 +411,9 @@ make_pid_file() {
fi fi
echo "$pid" > "$file" echo "$pid" > "$file"
if [ $? -ne 0 ]; then
die "Cannot create or write PID file $file"
fi
} }
remove_pid_file() { remove_pid_file() {
@@ -890,19 +905,30 @@ main() {
# we don't know our own PID. See the usage of $! below. # we don't know our own PID. See the usage of $! below.
log "$0 started" log "$0 started"
# Make a secure tmpdir.
mk_tmpdir
# Make the collection dir exists. # Make the collection dir exists.
mkdir -p "$OPT_DEST" || die "Can't make the destination directory" if [ ! -d "$OPT_DEST" ]; then
test -d "$OPT_DEST" || die "$OPT_DEST isn't a directory" mkdir -p "$OPT_DEST" || die "Cannot make --dest $OPT_DEST"
test -w "$OPT_DEST" || die "$OPT_DEST isn't writable" fi
# Check access to the --dest dir. By setting -x in the subshell,
# if either command fails, the subshell will exit immediately and
# $? will be non-zero.
(
set -e
touch "$OPT_DEST/test"
rm "$OPT_DEST/test"
)
if [ $? -ne 0 ]; then
die "Cannot read and write files to --dest $OPT_DEST"
fi
# Test if we have root; warn if not, but it isn't critical. # Test if we have root; warn if not, but it isn't critical.
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
log 'Not running with root privileges!'; log 'Not running with root privileges!';
fi fi
# Make a secure tmpdir.
mk_tmpdir
# Set TRIGGER_FUNCTION based on --function. # Set TRIGGER_FUNCTION based on --function.
set_trg_func set_trg_func
@@ -948,6 +974,15 @@ if [ "$(basename "$0")" = "pt-stalk" ] \
|| die "Cannot connect to MySQL. Check that MySQL is running and that the options after -- are correct." || die "Cannot connect to MySQL. Check that MySQL is running and that the options after -- are correct."
if [ "$OPT_DAEMONIZE" = "yes" ]; then if [ "$OPT_DAEMONIZE" = "yes" ]; then
# Check access to the --log file.
(
set -e
touch "$OPT_LOG"
)
if [ $? -ne 0 ]; then
die "Cannot write to --log $OPT_LOG"
fi
# The PID file will at first have our (parent) PID. # The PID file will at first have our (parent) PID.
# This is fine for ensuring that only one of us is # This is fine for ensuring that only one of us is
# running, but it's not fine if the user wants to use # running, but it's not fine if the user wants to use

View File

@@ -57,6 +57,9 @@ make_pid_file() {
# PID file doesn't exist, or it does but its pid is stale. # PID file doesn't exist, or it does but its pid is stale.
echo "$pid" > "$file" echo "$pid" > "$file"
if [ $? -ne 0 ]; then
die "Cannot create or write PID file $file"
fi
} }
remove_pid_file() { remove_pid_file() {

View File

@@ -111,8 +111,20 @@ parse_options() {
# default=foo # default=foo
# That's the spec for --string-opt2. Each line is a key:value pair # That's the spec for --string-opt2. Each line is a key:value pair
# from the option's POD line like "type: string; default: foo". # from the option's POD line like "type: string; default: foo".
mkdir "$TMPDIR/po/" 2>/dev/null if [ ! -d "$TMPDIR/po/" ]; then
mkdir "$TMPDIR/po/"
if [ $? -ne 0 ]; then
echo "Cannot mkdir $TMPDIR/po/" >&2
exit 1
fi
fi
rm -rf "$TMPDIR"/po/* rm -rf "$TMPDIR"/po/*
if [ $? -ne 0 ]; then
echo "Cannot rm -rf $TMPDIR/po/*" >&2
exit 1
fi
( (
export PO_DIR="$TMPDIR/po" export PO_DIR="$TMPDIR/po"
cat "$file" | perl -ne ' cat "$file" | perl -ne '

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TESTS=7 TESTS=9
TMPDIR="$TEST_TMPDIR" TMPDIR="$TEST_TMPDIR"
local file="$TMPDIR/pid-file" local file="$TMPDIR/pid-file"
@@ -60,6 +60,22 @@ is \
rm $file rm $file
rm $TMPDIR/output rm $TMPDIR/output
# ###########################################################################
# Die if pid file can't be created.
# ###########################################################################
(
make_pid_file "/root/pid" $$ >$TMPDIR/output 2>&1
)
is \
"$?" \
"1" \
"Exit 1 if PID file can't be created"
cmd_ok \
"grep -q 'Cannot create or write PID file /root/pid' $TMPDIR/output" \
"Error that PID file can't be created"
# ########################################################################### # ###########################################################################
# Done. # Done.
# ########################################################################### # ###########################################################################

View File

@@ -136,7 +136,7 @@ diag(`rm $dest/* 2>/dev/null`);
my (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'"); my (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'");
my $threshold = $uptime + 2; my $threshold = $uptime + 2;
$retval = system("$trunk/bin/pt-stalk --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 2 --run-time 2 -- --defaults-file=$cnf >$log_file 2>&1"); $retval = system("$trunk/bin/pt-stalk --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 2 --run-time 2 --pid $pid_file -- --defaults-file=$cnf >$log_file 2>&1");
sleep 3; sleep 3;