Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Init script for vblade (ATA over Ethernet daemon) |
| 4 | # |
| 5 | # chkconfig: - 30 70 |
| 6 | # description: vblade AoE daemon |
| 7 | # |
| 8 | # processname: vblade |
| 9 | # config: /etc/vblade.conf |
| 10 | # |
| 11 | # Shamelessly hacked together from other init scripts (sshd, mostly) |
| 12 | # integrate vblade.init from Fedora's vblade-14-6.fc12.src.rpm |
| 13 | # |
| 14 | |
| 15 | RETVAL=0 |
| 16 | prog=vblade |
| 17 | |
| 18 | spawn_vblade() { |
| 19 | ALLOWMACS="" |
| 20 | [ -n "$5" ] && ALLOWMACS="-m $5" |
| 21 | ID="$1-e$2.$3" |
| 22 | if [ ! -d "/var/run/$prog" ]; then |
| 23 | mkdir /var/run/$prog |
| 24 | fi |
| 25 | PID_FILE=/var/run/$prog/${ID}.pid |
| 26 | $prog $ALLOWMACS $2 $3 $1 $4 >> /var/log/$prog.log 2>&1 & |
| 27 | pid=$! |
| 28 | RETVAL=$? |
| 29 | echo $pid > $PID_FILE |
| 30 | echo -n $"$4 (e$2.$3@$1) [pid $pid]" |
| 31 | [ "$RETVAL" = 0 ] && echo "success" || echo "failure" |
| 32 | echo |
| 33 | } |
| 34 | |
| 35 | start() { |
| 36 | local ret |
| 37 | |
| 38 | echo $"Starting up $prog: " |
| 39 | |
| 40 | #/var/lock/subsys/$prog exists? |
| 41 | status $prog 2>&1 > /dev/null |
| 42 | ret=$? |
| 43 | |
| 44 | if [ "$ret" = "2" ]; then |
| 45 | echo "$prog dead but subsys locked" |
| 46 | echo |
| 47 | return 2 |
| 48 | else |
| 49 | if [ "$ret" = "0" ]; then |
| 50 | #is running |
| 51 | echo "already running" |
| 52 | return 0 |
| 53 | fi |
| 54 | fi |
| 55 | |
| 56 | if [ 0 -ne `grep -vc '^#\|^$' /etc/$prog.conf` ] |
| 57 | then |
| 58 | grep -v '^#' /etc/$prog.conf | sed -e 's/ / /g' -e 's/ / /g' | while read line |
| 59 | do |
| 60 | spawn_vblade $line |
| 61 | done |
| 62 | touch /var/lock/subsys/$prog |
| 63 | else |
| 64 | echo -n "empty $prog.conf?" |
| 65 | echo " passed" |
| 66 | echo |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | stop() { |
| 71 | echo -n $"Shutting down $prog: " |
| 72 | for pidfile in `ls /var/run/$prog/*.pid 2>/dev/null` |
| 73 | do |
| 74 | kill -TERM `cat $pidfile` |
| 75 | rm -f $pidfile |
| 76 | done |
| 77 | echo "success" |
| 78 | echo |
| 79 | rm -f /var/lock/subsys/$prog |
| 80 | } |
| 81 | |
| 82 | __pids_var_run() { |
| 83 | local base=${1##*/} |
| 84 | local pid_file=${2:-/var/run/$base.pid} |
| 85 | |
| 86 | pid= |
| 87 | if [ -f "$pid_file" ] ; then |
| 88 | local line p |
| 89 | |
| 90 | while : ; do |
| 91 | read line |
| 92 | [ -z "$line" ] && break |
| 93 | for p in $line ; do |
| 94 | [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" |
| 95 | done |
| 96 | done < "$pid_file" |
| 97 | |
| 98 | if [ -n "$pid" ]; then |
| 99 | return 0 |
| 100 | fi |
| 101 | return 1 # "Program is dead and /var/run pid file exists" |
| 102 | fi |
| 103 | return 3 # "Program is not running" |
| 104 | } |
| 105 | |
| 106 | __pids_pidof() { |
| 107 | pidof "$1" || pidof "${1##*/}" |
| 108 | } |
| 109 | |
| 110 | status() { |
| 111 | local base pid lock_file= pid_file= |
| 112 | |
| 113 | # Test syntax. |
| 114 | if [ "$#" = 0 ] ; then |
| 115 | echo $"Usage: status [-p pidfile] {program}" |
| 116 | return 1 |
| 117 | fi |
| 118 | if [ "$1" = "-p" ]; then |
| 119 | pid_file=$2 |
| 120 | shift 2 |
| 121 | fi |
| 122 | if [ "$1" = "-l" ]; then |
| 123 | lock_file=$2 |
| 124 | shift 2 |
| 125 | fi |
| 126 | base=${1##*/} |
| 127 | |
| 128 | # First try "pidof" |
| 129 | __pids_var_run "$1" "$pid_file" |
| 130 | RC=$? |
| 131 | if [ -z "$pid_file" -a -z "$pid" ]; then |
| 132 | pid="$(__pids_pidof "$1")" |
| 133 | fi |
| 134 | if [ -n "$pid" ]; then |
| 135 | echo $"${base} (pid $pid) is running..." |
| 136 | return 0 |
| 137 | fi |
| 138 | |
| 139 | case "$RC" in |
| 140 | 0) |
| 141 | echo $"${base} (pid $pid) is running..." |
| 142 | return 0 |
| 143 | ;; |
| 144 | 1) |
| 145 | echo $"${base} dead but pid file exists" |
| 146 | return 1 |
| 147 | ;; |
| 148 | esac |
| 149 | if [ -z "${lock_file}" ]; then |
| 150 | lock_file=${base} |
| 151 | fi |
| 152 | # See if /var/lock/subsys/${lock_file} exists |
| 153 | if [ -f /var/lock/subsys/${lock_file} ]; then |
| 154 | echo $"${base} dead but subsys locked" |
| 155 | return 2 |
| 156 | fi |
| 157 | echo $"${base} is stopped" |
| 158 | return 3 |
| 159 | } |
| 160 | |
| 161 | case "$1" in |
| 162 | start) |
| 163 | start |
| 164 | ;; |
| 165 | stop) |
| 166 | stop |
| 167 | ;; |
| 168 | restart) |
| 169 | stop |
| 170 | start |
| 171 | ;; |
| 172 | reload) |
| 173 | # yes, this sucks, but the vblade processes die on SIGHUP |
| 174 | stop |
| 175 | start |
| 176 | ;; |
| 177 | condrestart) |
| 178 | if [ -f /var/lock/subsys/$prog ]; then |
| 179 | stop |
| 180 | # avoid race |
| 181 | sleep 3 |
| 182 | start |
| 183 | fi |
| 184 | ;; |
| 185 | status) |
| 186 | status $prog |
| 187 | RETVAL=$? |
| 188 | ;; |
| 189 | *) |
| 190 | echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}" |
| 191 | RETVAL=1 |
| 192 | esac |
| 193 | exit $RETVAL |