cjdaemon 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/system/bin/sh
  2. # Set the main directory for things to run (if changed, also change in cjdctl and 99cjdroute)
  3. CJDPATH="/sdcard/cjdns"
  4. # Function to output status messages and log if configured to do so
  5. logmessage() {
  6. if [ -n "$CJDLOG" ]; then
  7. echo "`date` - $1" >> "$CJDPATH"/cjdaemon.log
  8. fi
  9. }
  10. # Create the daemon folder if it doesn't exist
  11. if [ ! -e "$CJDPATH" ]; then
  12. install -d "$CJDPATH" \
  13. || exit 1
  14. fi
  15. # Fail if the daemon is already running
  16. if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
  17. if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
  18. logmessage "Error! Daemon already running"
  19. exit 1
  20. fi
  21. fi
  22. # Set a pid file for the daemon so we know what to kill if it starts causing issues
  23. echo "$$" > "$CJDPATH"/.cjdaemon.pid
  24. # Create cjdaemon.conf if it's missing
  25. if [ ! -f "$CJDPATH"/cjdaemon.conf ]; then
  26. touch "$CJDPATH"/cjdaemon.conf \
  27. || exit 1
  28. echo -e 'CJDCFG="cjdroute.conf"\nCJDLOG=0' \
  29. > "$CJDPATH"/cjdaemon.conf
  30. fi
  31. # Source cjdaemon.conf to load user settings
  32. source "$CJDPATH"/cjdaemon.conf
  33. # Set $CJDCFG to the default if it wasn't set by cjdaemon.conf
  34. if [ -z "$CJDCFG" ]; then
  35. CJDCFG="cjdroute.conf"
  36. fi
  37. # Create the log if it doesn't exist, and if it can't be created disable logging
  38. if [ -n "$CJDLOG" ]; then
  39. if [ ! -f "$CJDPATH"/cjdaemon.log ]; then
  40. touch "$CJDPATH"/cjdaemon.log \
  41. || unset CJDLOG
  42. fi
  43. fi
  44. # Function used to start cjdroute
  45. cjdstart() {
  46. # Exit if the lock file is missing
  47. if [ ! -f "$CJDPATH"/.lock ]; then
  48. exit 0
  49. fi
  50. # Start cjdroute and output/log results
  51. if [ -f "${CJDPATH}/${CJDCFG}" ]; then
  52. if [ `pgrep cjdroute | wc -l` -eq 0 ]; then
  53. LOGMSG="Running: cjdroute < ${CJDPATH}/${CJDCFG}..."
  54. cjdroute < "${CJDPATH}/${CJDCFG}" &> /dev/null 2>&1
  55. sleep 1
  56. if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
  57. LOGMSG="${LOGMSG} Success!"
  58. else
  59. LOGMSG="${LOGMSG} Error! Failed to start cjdroute"
  60. fi
  61. else
  62. LOGMSG="${LOGMSG} Error! Failed to start cjdroute, already running"
  63. fi
  64. else
  65. LOGMSG="${LOGMSG} Error! Failed to start cjdroute, config missing @ ${CJDPATH}/${CJDCFG}"
  66. fi
  67. logmessage "$LOGMSG"
  68. unset LOGMSG
  69. }
  70. # Function used to stop cjdroute
  71. cjdstop() {
  72. # Exit if the lock file is missing
  73. if [ ! -f "$CJDPATH"/.lock ]; then
  74. exit 0
  75. fi
  76. # Kill cjdroute and output/log results
  77. if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
  78. LOGMSG="Running: killall cjdroute..."
  79. killall cjdroute
  80. sleep 1
  81. if [ `pgrep cjdroute | wc -l` -eq 0 ]; then
  82. LOGMSG="${LOGMSG} Success!"
  83. else
  84. LOGMSG="${LOGMSG} Error! Failed to stop cjdroute"
  85. fi
  86. else
  87. LOGMSG="${LOGMSG} Error! Failed to stop cjdroute, already stopped"
  88. fi
  89. logmessage "$LOGMSG"
  90. unset LOGMSG
  91. }
  92. # Run cjdstart when the phone wakes and cjdstop when it sleeps until the phone powers down
  93. while :; do
  94. # Wait until the phone is awake, then start cjdroute
  95. cat /sys/power/wait_for_fb_wake > /dev/null
  96. cjdstart
  97. sleep 1
  98. # Wait until the phone sleeps, then stop cjdns
  99. cat /sys/power/wait_for_fb_sleep > /dev/null
  100. cjdstop
  101. done