The little things give you away... A collection of various small helper stuff
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

73 satır
2.3 KiB

  1. #!/bin/bash
  2. function usage_exit {
  3. # usage E -- print usage; exit with status code E
  4. echo 'Usage: kill-wpull-connections (-h | [-c] (-p PID | -j JOBID))'
  5. echo
  6. echo ' -h: Display this message and exit'
  7. echo ' -c: Cast return value of shutdown(2) to int explicitly (only necessary on broken machines)'
  8. echo ' -p PID: Kill connections of wpull process PID'
  9. echo ' -j JOBID: Kill connections of the wpull process for ArchiveBot job JOBID'
  10. exit $1
  11. }
  12. if [[ $# -eq 0 || $# -gt 3 ]]; then usage_exit 1; fi
  13. if [[ $# -eq 1 && "$1" == '-h' ]]; then usage_exit 0; fi
  14. if [[ $# -ne 2 && $# -ne 3 ]]; then usage_exit 1; fi
  15. cast=
  16. if [[ "$1" == '-c' ]]; then cast='(int)'; shift; fi
  17. if [[ "$1" != -[pj] ]]; then usage_exit 1; fi
  18. if [[ $# -ne 2 ]]; then usage_exit 1; fi
  19. if [[ "$1" == '-p' ]]
  20. then
  21. wpullPid=$2
  22. if [[ "${wpullPid}" == *[^0-9]* ]]
  23. then
  24. echo "Error: '${wpullPid}' is not a valid PID"
  25. exit 1
  26. fi
  27. elif [[ "$1" == '-j' ]]
  28. then
  29. pids=($(pgrep --full "wpull.*/$2/"))
  30. if [[ ${#pids[@]} -ne 1 ]]
  31. then
  32. echo "Error: not exactly one process found for '$2'"
  33. exit 1
  34. fi
  35. wpullPid=${pids[0]}
  36. fi
  37. if ! command -v lsof >/dev/null 2>&1
  38. then
  39. echo "Error: could not find lsof"
  40. exit 1
  41. fi
  42. if ! command -v gdb >/dev/null 2>&1
  43. then
  44. echo "Error: could not find gdb"
  45. exit 1
  46. fi
  47. if ! ps -p ${wpullPid} >/dev/null 2>&1
  48. then
  49. echo "Error: no process with PID ${wpullPid}"
  50. exit 1
  51. fi
  52. if [[ -e /proc/sys/kernel/yama/ptrace_scope && "$(< /proc/sys/kernel/yama/ptrace_scope)" != "0" && $EUID -ne 0 ]]
  53. then
  54. echo "Warning: /proc/sys/kernel/yama/ptrace_scope is not zero. You likely need to run this script as root."
  55. fi
  56. gdb -batch -batch-silent \
  57. -ex "attach ${wpullPid}" \
  58. -ex 'python import subprocess' \
  59. -ex 'python def call(s): return subprocess.call(s, shell = True) == 0' \
  60. -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")' \
  61. -ex detach \
  62. -ex quit