powerdown 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. PID_PATH="/var/run/pid_path"
  6. REBOOTING_FILE="/run/rebooting"
  7. usage() {
  8. >&2 echo "Usage: ${SELF} schedule delay"
  9. >&2 echo " ${SELF} handle"
  10. >&2 echo " ${SELF} now"
  11. exit 1
  12. }
  13. schedule_powerdown() {
  14. # Save current pid path before closing bin
  15. # (won't work if bin is already closed)
  16. local pid_path=$(dirname $(readlink /proc/$(pid print)/exe))
  17. echo -n "${pid_path}" > "${PID_PATH}"
  18. # Send USR1 signal to the running FunKey process to warn about
  19. # impending shutdown
  20. pkill -USR1 "$(pid print)" > /dev/null 2>&1
  21. # Delay for the given grace period seconds to catch signal USR2.
  22. # If the signal is caught, then it means the running FunKey
  23. # process canceled this shutdown and will handle it by itself.
  24. sleep ${1}
  25. # Delay expired, initiate final powerdown
  26. powerdown_now
  27. }
  28. handle_powerdown() {
  29. pkill -f "powerdown schedule"
  30. }
  31. powerdown_now() {
  32. # Sync before all else
  33. sync
  34. # Notif fullscreen "Shutting down"
  35. notif set 0 "^^^^^^^^ SHUTTING DOWN...^^^^^^^^"
  36. # Notify system, reboot in progress
  37. touch "${REBOOTING_FILE}"
  38. # Shutdown amp
  39. audio_amp off >/dev/null 2>&1
  40. # Force Read Only
  41. ro
  42. # Poweroff
  43. poweroff
  44. }
  45. action="${1:-now}"
  46. case "${action}" in
  47. schedule)
  48. if [ ${#} != 2 -o "${2}" == "0" ]; then
  49. usage
  50. fi
  51. schedule_powerdown ${2}
  52. ;;
  53. handle)
  54. if [ ${#} -ne 1 ]; then
  55. usage
  56. fi
  57. handle_powerdown
  58. ;;
  59. now)
  60. if [ ${#} -gt 1 ]; then
  61. usage
  62. fi
  63. powerdown_now
  64. ;;
  65. *)
  66. usage
  67. ;;
  68. esac
  69. exit 0