cjdctl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/system/bin/sh
  2. # Config and runtime directory (if changed, also change in cjdaemon and 99cjdroute)
  3. CJDPATH="/sdcard/cjdns"
  4. # Set the name of this script
  5. APPNAME="${0##*/}"
  6. # Exit with an error if the user isn't root
  7. if [ ! `whoami` = "root" ]; then
  8. echo "Error: ${APPNAME} must be run as root"
  9. exit 1
  10. fi
  11. # Create the daemon folder if it doesn't exist
  12. if [ ! -e "$CJDPATH" ]; then
  13. install -d "$CJDPATH" || exit 1
  14. fi
  15. # Source cjdaemon.conf to load user settings if it exists
  16. if [ -f "$CJDPATH"/cjdaemon.conf ]; then
  17. source "$CJDPATH"/cjdaemon.conf
  18. fi
  19. # Set $CJDCFG to the default if it wasn't set by cjdaemon.conf
  20. if [ -z "$CJDCFG" ]; then
  21. CJDCFG="cjdroute.conf"
  22. fi
  23. # Create the lock file and start (or if running, restart) cjdaemon
  24. enable_cjdaemon() {
  25. echo -n "${APPNAME}: Enabling cjdns... "
  26. # Create the lock file, enabling cjdaemon
  27. if [ ! -f "$CJDPATH"/.lock ]; then
  28. touch "$CJDPATH"/.lock
  29. fi
  30. # If running, kill cjdaemon (it'll start cjdroute when restarted)
  31. if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
  32. if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
  33. kill `cat "$CJDPATH"/.cjdaemon.pid`
  34. sleep 1
  35. fi
  36. rm "$CJDPATH"/.cjdaemon.pid
  37. fi
  38. # Start cjdaemon (which will start cjdroute if the phone is awake)
  39. cjdaemon &
  40. sleep 1
  41. # Exit successfully if cjdaemon started, otherwise exit with an error
  42. if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
  43. if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
  44. echo "Done!"
  45. exit 0
  46. fi
  47. fi
  48. echo "Error! Couldn't start cjdaemon."
  49. exit 1
  50. }
  51. # Remove the lock file then kill cjdaemon and cjdroute if either is running
  52. disable_cjdaemon() {
  53. echo -n "${APPNAME}: Disabling cjdns... "
  54. # Remove the lock file, disabling cjdaemon
  55. if [ -f "$CJDPATH"/.lock ]; then
  56. rm "$CJDPATH"/.lock
  57. fi
  58. # If cjdaemon is running, kill it
  59. if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
  60. if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
  61. kill `cat "$CJDPATH"/.cjdaemon.pid`
  62. sleep 1
  63. fi
  64. rm "$CJDPATH"/.cjdaemon.pid
  65. fi
  66. # If cjdroute is running, kill it
  67. if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
  68. killall cjdroute
  69. sleep 1
  70. if [ ! `pgrep cjdroute | wc -l` -eq 0 ]; then
  71. echo "Error! cjdroute still running"
  72. exit 1
  73. fi
  74. fi
  75. echo "Done!"
  76. }
  77. # Parse commandline arguments and behave accordingly
  78. case "$1" in
  79. e|-e|enable|--enable)
  80. enable_cjdaemon
  81. ;;
  82. d|-d|disable|--disable)
  83. disable_cjdaemon
  84. ;;
  85. r|-r|restart|--restart)
  86. disable_cjdaemon
  87. enable_cjdaemon
  88. ;;
  89. *)
  90. echo -e "Usage:\n\t${APPNAME} [option]\n"
  91. echo -e "Options:"
  92. echo -e "\te|enable: start cjdns and enable at boot"
  93. echo -e "\td|disable: stop cjdns and disable at boot"
  94. echo -e "\tr|restart: stop+disable, then start+enable cjdns"
  95. ;;
  96. esac