From 2a41f169c55f53dbaefb1f6653b2ac542ac57d83 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Tue, 15 May 2018 16:20:00 +0200 Subject: [PATCH] Add -c option to cast the return value of shutdown(2) to int explicitly on broken machines --- kill-wpull-connections | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/kill-wpull-connections b/kill-wpull-connections index 9a4c245..daf6f98 100755 --- a/kill-wpull-connections +++ b/kill-wpull-connections @@ -1,14 +1,25 @@ #!/bin/bash -if [[ "$1" == '-h' || $# -ne 2 ]] -then - echo 'Usage: kill-wpull-connections (-p PID | -j JOBID | -h)' +function usage_exit { + # usage E -- print usage; exit with status code E + echo 'Usage: kill-wpull-connections (-h | [-c] (-p PID | -j JOBID))' echo echo ' -h: Display this message and exit' - echo ' -j JOBID: Kill connections of the wpull process for ArchiveBot job JOBID' + echo ' -c: Cast return value of shutdown(2) to int explicitly (only necessary on broken machines)' echo ' -p PID: Kill connections of wpull process PID' - [[ "$1" == '-h' && $# -eq 1 ]] && exit 0 || exit 1 -fi + echo ' -j JOBID: Kill connections of the wpull process for ArchiveBot job JOBID' + exit $1 +} + +if [[ $# -eq 0 || $# -gt 3 ]]; then usage_exit 1; fi +if [[ $# -eq 1 && "$1" == '-h' ]]; then usage_exit 0; fi +if [[ $# -ne 2 && $# -ne 3 ]]; then usage_exit 1; fi + +cast= +if [[ "$1" == '-c' ]]; then cast='(int)'; shift; fi + +if [[ "$1" != -[pj] ]]; then usage_exit 1; fi +if [[ $# -ne 2 ]]; then usage_exit 1; fi if [[ "$1" == '-p' ]] then @@ -56,6 +67,6 @@ gdb -batch -batch-silent \ -ex "attach ${wpullPid}" \ -ex 'python import subprocess' \ -ex 'python def call(s): return subprocess.call(s, shell = True) == 0' \ - -ex 'python call("echo '\''FDs before forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1") and ([gdb.execute("p shutdown(" + fd + ", 2)") for fd in subprocess.check_output("lsof -an -p '${wpullPid}' -i TCP -F pfn | awk '\''NR%2==0{fd=substr($0,2)}NR%2==1&&NR>1&&!/127\.0\.0\.1/{print fd}'\''", shell = True).decode("ascii").strip().split("\n")] or True) and call("echo '\''FDs after forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1")' \ + -ex 'python call("echo '\''FDs before forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1") and ([gdb.execute("p '${cast}'shutdown(" + fd + ", 2)") for fd in subprocess.check_output("lsof -an -p '${wpullPid}' -i TCP -F pfn | awk '\''NR%2==0{fd=substr($0,2)}NR%2==1&&NR>1&&!/127\.0\.0\.1/{print fd}'\''", shell = True).decode("ascii").strip().split("\n")] or True) and call("echo '\''FDs after forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1")' \ -ex detach \ -ex quit