rc.ewft.minidlna 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/bin/sh
  2. # Read our configuration file and set variables
  3. APPNAME="ewft.minidlna"
  4. DISPLAYNAME="MiniDLNA"
  5. PLGCONFFILE="/boot/config/plugins/ewft.minidlna/${APPNAME}.cfg"
  6. APPCONFFILE="settings.json"
  7. APPEXECUTABLE="/usr/sbin/minidlnad"
  8. PIDFILE="/var/run/minidlna/minidlna.pid"
  9. PLGLOGFILE="/var/log/minidlna/${APPNAME}.log"
  10. ARRAYSTATE=`grep fsState /var/local/emhttp/var.ini | sed -n 's!fsState="\(.*\)"!\1!p'`
  11. source "$PLGCONFFILE"
  12. # This needs to be near the top so the dependency common directory can be set
  13. app_check_cache() {
  14. local COMMONVAR="false"
  15. if [ -e "/mnt/cache" ] && [ -w "/mnt/cache" ]; then
  16. local CDRIVE=$( stat -f -c '%T' "/mnt/cache" )
  17. if [ "$CDRIVE" != "ramfs" ] && [ "$CDRIVE" != "tmpfs" ] && [ "$CDRIVE" != "proc" ] && [ "$CDRIVE" != "sysfs" ] && [ "$CDRIVE" != "msdos" ]; then
  18. COMMONVAR="true"
  19. fi
  20. fi
  21. echo "$COMMONVAR"
  22. }
  23. # Check for cache drive and make sure its an actual disk drive
  24. PHAZECOMMON="/usr/local/PhAzE-Common"
  25. COMMONCHECK=`app_check_cache`
  26. [ "$COMMONCHECK" == "true" ] && PHAZECOMMON="/mnt/cache/.PhAzE-Common"
  27. STARTFILE="$PHAZECOMMON/${APPNAME}/startcfg.sh"
  28. app_start() {
  29. # Get array status and exit if not started
  30. if [ "$ARRAYSTATE" != "Started" ]; then
  31. app_logger "Starting $DISPLAYNAME..." info all
  32. app_logger "Start failed: Array stopped " warn all
  33. return
  34. fi
  35. # Set service to enabled
  36. [ "$SERVICE" != "enable" ] && sed -i 's/disable/enable/' "$PLGCONFFILE"
  37. # No-op if already running and install if not installed
  38. local STATUS=`app_status`
  39. if [ "$STATUS" == "RUNNING" ]; then
  40. app_logger "Starting $DISPLAYNAME..." info all
  41. app_logger "Start OK: Already running " info all
  42. return
  43. else
  44. app_logger "Starting $DISPLAYNAME..." info all
  45. # Create export file and check dependencies
  46. # write_export_config
  47. if [ "$EXPORTOK" == "no" ]; then
  48. app_logger "Start failed: Unable to update export file " error all
  49. return
  50. fi
  51. fi
  52. # Make working directories and write the app config file
  53. #app_make_dirs
  54. #write_app_config
  55. # Start the app
  56. sudo -H -u root /bin/bash -c 'nohup '$APPEXECUTABLE' > /dev/null 2>&1 &'
  57. # Checks if the app started properly or not, giving it 10 seconds to create the PID file
  58. local TIMER=0
  59. while [ ! -e "$PIDFILE" ]; do
  60. let TIMER=$TIMER+1
  61. sleep 1
  62. if [ $TIMER -gt 10 ]; then
  63. app_logger "Start failed: No PID created " error all
  64. sleep 3
  65. return
  66. fi
  67. done
  68. # Check if process specified in the PID is actually running, as a double check
  69. TIMER=0
  70. while [[ -e "$PIDFILE" && ! -f /proc/`cat "$PIDFILE" 2> /dev/null`/exe ]]; do
  71. let TIMER=$TIMER+1
  72. sleep 1
  73. if [ $TIMER -gt 5 ]; then
  74. app_logger "Start failed: PID created but no process exists " error all
  75. sleep 3
  76. return
  77. fi
  78. done
  79. # Notify if start was successful or failed
  80. app_logger "Start OK! " info all
  81. sleep 1
  82. }
  83. app_stop() {
  84. # No-op if already running
  85. local STATUS=`app_status`
  86. app_logger "Stopping $DISPLAYNAME..." info all
  87. if [ "$STATUS" == "STOPPED" ]; then
  88. app_logger "Stop OK: Already stopped " info all
  89. sleep 1
  90. return
  91. fi
  92. # Send the kill command to gracefully shutdown
  93. kill $(cat "$PIDFILE" ) 2> /dev/null
  94. # Begin a timer for shutdown, force kill process if not shut down by end of timer
  95. let TIMER=0
  96. while [ -e "$PIDFILE" ]; do
  97. let TIMER=$TIMER+1
  98. sleep 1
  99. # Process has ended but PID file remains, so remove it
  100. if [ ! -f /proc/`cat "$PIDFILE" 2> /dev/null`/exe ]; then
  101. rm "$PIDFILE" 2> /dev/null
  102. fi
  103. if [ $TIMER -gt 30 ]; then
  104. app_logger "Taking too long: Force killing process" warn log
  105. kill -9 $(cat "$PIDFILE" ) 2> /dev/null
  106. rm "$PIDFILE"
  107. break
  108. fi
  109. done
  110. app_logger "Stop OK! " info all
  111. sleep 1
  112. }
  113. app_restart() {
  114. app_stop
  115. app_start
  116. }
  117. app_status() {
  118. # Get running status
  119. local STATUS=""
  120. if [ ! -f "$APPEXECUTABLE" ]; then
  121. STATUS="NOT INSTALLED"
  122. elif [ -f "$PIDFILE" ] && [ -f /proc/`cat "$PIDFILE" 2> /dev/null`/exe ]; then
  123. STATUS="RUNNING"
  124. else
  125. STATUS="STOPPED"
  126. # Remove stale PID file that shouldn't exist anymore
  127. [ -f "$PIDFILE" ] && rm -f "$PIDFILE"
  128. fi
  129. echo "$STATUS"
  130. }
  131. app_buttonstart() {
  132. if [ -f "$PLGCONFFILE" ]; then
  133. sync_app_config
  134. app_start
  135. else
  136. app_logger "Start failed: Config file missing - Reinstall plugin " error all
  137. sleep 3
  138. fi
  139. }
  140. app_logger() {
  141. # First passed item is the text ($1), secone item is severity ($2 - info, warn, error), third is where to log ($3 - all, log)
  142. local LOGTEXT=$1
  143. local LOGTYPE=$2
  144. local LOGVAR=$3
  145. local TIMESTAMP=`date +"%Y-%m-%d %T"`
  146. # Set the type of log
  147. if [ "$LOGTYPE" == "info" ]; then
  148. TYPE="[INFO]"
  149. elif [ "$LOGTYPE" == "warn" ]; then
  150. TYPE="[WARNING]"
  151. elif [ "$LOGTYPE" == "error" ]; then
  152. TYPE="[ERROR]"
  153. fi
  154. # If log file doesn't exist, make it
  155. if [ ! -f "$PLGLOGFILE" ]; then
  156. mkdir -p "/var/log/minidlna"
  157. touch "$PLGLOGFILE"
  158. fi
  159. # Echo the text to display and log, or just log file
  160. if [ "$LOGVAR" == "all" ]; then
  161. echo -e "${LOGTEXT}"
  162. echo -e "${TIMESTAMP} ${TYPE}\t${LOGTEXT}" >> "$PLGLOGFILE"
  163. elif [ "$LOGVAR" == "log" ]; then
  164. echo -e "${TIMESTAMP} ${TYPE}\t${LOGTEXT}" >> "$PLGLOGFILE"
  165. fi
  166. }
  167. app_enable() {
  168. app_stop
  169. SERVICE=enable
  170. app_start
  171. }
  172. app_disable() {
  173. app_stop
  174. SERVICE=disable
  175. }
  176. app_curver() {
  177. # This gets the current version installed
  178. local CURVER=""
  179. if [ "$ARRAYSTATE" == "Started" ]; then
  180. [ -e "$APPEXECUTABLE" ] && CURVER=`sudo -H -u root /bin/bash -c ''$APPEXECUTABLE' -V 2>&1' | cut -f1 | awk '{print $2}'` || CURVER="NOT INSTALLED"
  181. fi
  182. echo "$CURVER"
  183. }
  184. case "$1" in
  185. 'start')
  186. app_logger "*** Start Initiated ***" info log
  187. [ "$2" == "force" ] && ARRAYSTATE="Started"
  188. app_start
  189. ;;
  190. 'stop')
  191. app_logger "*** Stop Initiated ***" info log
  192. app_stop
  193. ;;
  194. 'restart')
  195. app_logger "*** Restart Initiated ***" info log
  196. app_restart
  197. ;;
  198. 'status')
  199. app_status
  200. ;;
  201. 'currentversion')
  202. app_curver
  203. ;;
  204. 'enable')
  205. app_logger "*** Apply - Enable Initiated ***" info log
  206. VAR="$2"
  207. VAR=`echo "$VAR" | sed 's!@20! !g'`
  208. VAR=`echo "$VAR" | sed 's!@@2200!@20!g'`
  209. VARS=( $VAR )
  210. app_enable ${VARS[0]} ${VARS[1]} ${VARS[2]} ${VARS[3]} ${VARS[4]} ${VARS[5]} ${VARS[6]} ${VARS[7]} ${VARS[8]} ${VARS[9]} ${VARS[10]} ${VARS[11]} ${VARS[12]} ${VARS[13]}
  211. ;;
  212. 'disable')
  213. app_logger "*** Apply - Disable Initiated ***" info log
  214. VAR="$2"
  215. VAR=`echo "$VAR" | sed 's!@20! !g'`
  216. VAR=`echo "$VAR" | sed 's!@@2200!@20!g'`
  217. VARS=( $VAR )
  218. app_disable ${VARS[0]} ${VARS[1]} ${VARS[2]} ${VARS[3]} ${VARS[4]} ${VARS[5]} ${VARS[6]} ${VARS[7]} ${VARS[8]} ${VARS[9]} ${VARS[10]} ${VARS[11]} ${VARS[12]} ${VARS[13]}
  219. ;;
  220. *)
  221. echo "usage $0 [start | stop | restart | status | install | update | updateplg | downgradeplg | removeplg | latestversion | currentversion]"
  222. esac