diff --git a/bin/pt-stalk b/bin/pt-stalk index 3de794d7..312858bd 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -102,8 +102,20 @@ parse_options() { local file="$1" 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/* + if [ $? -ne 0 ]; then + echo "Cannot rm -rf $TMPDIR/po/*" >&2 + exit 1 + fi + ( export PO_DIR="$TMPDIR/po" cat "$file" | perl -ne ' @@ -399,6 +411,9 @@ make_pid_file() { fi echo "$pid" > "$file" + if [ $? -ne 0 ]; then + die "Cannot create or write PID file $file" + fi } remove_pid_file() { @@ -890,19 +905,30 @@ main() { # we don't know our own PID. See the usage of $! below. log "$0 started" - # Make a secure tmpdir. - mk_tmpdir - # Make the collection dir exists. - mkdir -p "$OPT_DEST" || die "Can't make the destination directory" - test -d "$OPT_DEST" || die "$OPT_DEST isn't a directory" - test -w "$OPT_DEST" || die "$OPT_DEST isn't writable" + if [ ! -d "$OPT_DEST" ]; then + mkdir -p "$OPT_DEST" || die "Cannot make --dest $OPT_DEST" + 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. if [ "$(id -u)" != "0" ]; then log 'Not running with root privileges!'; fi + # Make a secure tmpdir. + mk_tmpdir + # Set TRIGGER_FUNCTION based on --function. 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." 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. # This is fine for ensuring that only one of us is # running, but it's not fine if the user wants to use diff --git a/lib/bash/daemon.sh b/lib/bash/daemon.sh index 9abe8b3e..663cb8b3 100644 --- a/lib/bash/daemon.sh +++ b/lib/bash/daemon.sh @@ -57,6 +57,9 @@ make_pid_file() { # PID file doesn't exist, or it does but its pid is stale. echo "$pid" > "$file" + if [ $? -ne 0 ]; then + die "Cannot create or write PID file $file" + fi } remove_pid_file() { diff --git a/lib/bash/parse_options.sh b/lib/bash/parse_options.sh index 4fde19c1..21d580d2 100644 --- a/lib/bash/parse_options.sh +++ b/lib/bash/parse_options.sh @@ -111,8 +111,20 @@ parse_options() { # default=foo # 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". - 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/* + if [ $? -ne 0 ]; then + echo "Cannot rm -rf $TMPDIR/po/*" >&2 + exit 1 + fi + ( export PO_DIR="$TMPDIR/po" cat "$file" | perl -ne ' diff --git a/t/lib/bash/daemon.sh b/t/lib/bash/daemon.sh index 76ec4d0b..0ffad457 100644 --- a/t/lib/bash/daemon.sh +++ b/t/lib/bash/daemon.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=7 +TESTS=9 TMPDIR="$TEST_TMPDIR" local file="$TMPDIR/pid-file" @@ -60,6 +60,22 @@ is \ rm $file 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. # ########################################################################### diff --git a/t/pt-stalk/pt-stalk.t b/t/pt-stalk/pt-stalk.t index eb57d5c5..a8e7da87 100644 --- a/t/pt-stalk/pt-stalk.t +++ b/t/pt-stalk/pt-stalk.t @@ -136,7 +136,7 @@ diag(`rm $dest/* 2>/dev/null`); my (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'"); 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;