powerdown 1.6 KB

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