You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2014 OpenWrt.org
  3. # smsd initscript openwrt mod
  4. START=99
  5. STOP=99
  6. EXTRA_COMMANDS="status"
  7. EXTRA_HELP=" status View pid and service status "
  8. # Set USER and GROUP, if necessary:
  9. USER=""
  10. GROUP=""
  11. # If an unpriviledged user is selected, make sure that next two
  12. # files are writable by that user:
  13. PIDFILE="/var/run/smsd.pid"
  14. INFOFILE="/var/run/smsd.working"
  15. # Logfile can also be defined in here:
  16. LOGFILE="/var/log/smsd.log"
  17. DAEMON=/usr/bin/smsd
  18. # A program which turns power off for couple of seconds:
  19. RESETMODEMS=/usr/bin/smsd_resetmodems
  20. NAME=smsd
  21. PSOPT=""
  22. # Set/edit this before starting service !!!!!
  23. WRT_SPOOL=/var/spool
  24. # Maximum time to stop smsd, after that it gets killed hardly:
  25. MAXWAIT=45
  26. boot() {
  27. start
  28. }
  29. start() {
  30. test -x $DAEMON || exit 0
  31. echo "Creating minimum spool directories"
  32. mkdir -p $WRT_SPOOL
  33. mkdir -p $WRT_SPOOL/sms
  34. mkdir -p $WRT_SPOOL/sms/incoming
  35. mkdir -p $WRT_SPOOL/sms/outgoing
  36. mkdir -p $WRT_SPOOL/sms/checked
  37. mkdir -p $WRT_SPOOL/sms/failed
  38. mkdir -p $WRT_SPOOL/sms/sent
  39. echo -n "Starting SMS Daemon: "
  40. MSG="."
  41. ARGS="-n MAINPROCESS -p$PIDFILE -i$INFOFILE"
  42. [ "x$USER" != x ] && ARGS="$ARGS -u$USER"
  43. [ "x$GROUP" != x ] && ARGS="$ARGS -g$GROUP"
  44. [ "x$LOGFILE" != x ] && ARGS="$ARGS -l$LOGFILE"
  45. PID=`cat $PIDFILE 2>/dev/null`
  46. if [ "x$PID" != x ]; then
  47. if kill -0 $PID 2>/dev/null; then
  48. MSG=" already running ($PID)."
  49. else
  50. PID=""
  51. fi
  52. fi
  53. if [ "x$PID" = x ]; then
  54. if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
  55. MSG=" already running."
  56. else
  57. $DAEMON $ARGS
  58. sleep 1
  59. PIDS=`ps $PSOPT | grep $NAME | grep -v grep`
  60. [ "x$PIDS" = x ] && MSG=" failed."
  61. fi
  62. fi
  63. echo "$NAME$MSG"
  64. }
  65. stop() {
  66. if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
  67. PID=`cat $PIDFILE 2>/dev/null`
  68. if [ "x$PID" != x ]; then
  69. P=`kill -0 $PID 2>/dev/null`
  70. [ "x$P" != x ] && PID=""
  71. fi
  72. if [ "x$PID" != x ]; then
  73. kill $PID
  74. else
  75. kill `ps $PSOPT | grep $NAME | grep -v grep | awk '{print $1}'` >/dev/null 2>&1
  76. fi
  77. sleep 1
  78. if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
  79. echo "Allowing $NAME to terminate gracefully within $MAXWAIT seconds"
  80. infofound=0
  81. dots=0
  82. seconds=0
  83. while ps $PSOPT | grep $NAME | grep -v grep >/dev/null; do
  84. if [ $infofound -lt 1 ]; then
  85. if [ -f $INFOFILE ]; then
  86. infofound=1
  87. if [ $dots -gt 0 ]; then
  88. echo ""
  89. dots=0
  90. fi
  91. $ECHO -n "$NAME is currently "
  92. cat $INFOFILE
  93. echo "Time counting is now disabled and we will wait until this job is complete."
  94. fi
  95. fi
  96. [ $infofound -lt 1 ] && seconds=`expr $seconds + 1`
  97. echo -n "."
  98. dots=`expr $dots + 1`
  99. if [ "$seconds" -ge $MAXWAIT ]; then
  100. if [ $dots -gt 0 ]; then
  101. echo ""
  102. dots=0
  103. fi
  104. echo "Timeout occurred, killing $NAME hardly."
  105. kill -9 `ps $PSOPT | grep $NAME | grep -v grep | awk '{print $1}'` >/dev/null 2>&1
  106. [ -f $PIDFILE ] && rm $PIDFILE
  107. seconds=0
  108. fi
  109. sleep 1
  110. done
  111. [ $dots -gt 0 ] && echo ""
  112. #echo "$NAME is stopped."
  113. fi
  114. fi
  115. }
  116. restart() {
  117. stop
  118. start
  119. }
  120. status() {
  121. PID=$(cat $PIDFILE)
  122. test -e $PIDFILE
  123. if [ $? == 0 ]; then
  124. echo $NAME " running! pid $PID"
  125. else
  126. echo $NAME " not running !!!"
  127. fi
  128. }
  129. reset() {
  130. $0 stop
  131. [ -f "$RESETMODEMS" ] && "$RESETMODEMS"
  132. sleep 30
  133. $0 start
  134. }