Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-connectivity / openssh / files / stx / sshd.init
1 #!/bin/bash
2 #
3 # sshd          Start up the OpenSSH server daemon
4 #
5 # chkconfig: 2345 55 25
6 # description: SSH is a protocol for secure remote shell access. \
7 #              This service starts up the OpenSSH server daemon.
8 #
9 # processname: sshd
10 # config: /etc/ssh/ssh_host_key
11 # config: /etc/ssh/ssh_host_key.pub
12 # config: /etc/ssh/ssh_random_seed
13 # config: /etc/ssh/sshd_config
14 # pidfile: /var/run/sshd.pid
15
16 ### BEGIN INIT INFO
17 # Provides: sshd
18 # Required-Start: $local_fs $network $syslog
19 # Required-Stop: $local_fs $syslog
20 # Should-Start: $syslog
21 # Should-Stop: $network $syslog
22 # Default-Start: 2 3 4 5
23 # Default-Stop: 0 1 6
24 # Short-Description: Start up the OpenSSH server daemon
25 # Description:       SSH is a protocol for secure remote shell access.
26 #                    This service starts up the OpenSSH server daemon.
27 ### END INIT INFO
28
29 # source function library
30 . /etc/init.d/functions
31
32 # pull in sysconfig settings
33 [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
34
35 RETVAL=0
36 prog="sshd"
37 lockfile=/var/lock/subsys/$prog
38
39 # Some functions to make the below more readable
40 SSHD=/usr/sbin/sshd
41 XPID_FILE=/var/run/sshd.pid
42 PID_FILE=/var/run/sshd-s.pid
43
44 runlevel=$(set -- $(runlevel); eval "echo \$$#" )
45
46 do_restart_sanity_check()
47 {
48         $SSHD -t
49         RETVAL=$?
50         if [ $RETVAL -ne  0 ]; then
51                 failure $"Configuration file or keys are invalid"
52                 echo
53         fi
54 }
55
56 start()
57 {
58         [ -x $SSHD ] || exit 5
59         [ -f /etc/ssh/sshd_config ] || exit 6
60         # Create keys if necessary
61         /usr/sbin/sshd-keygen
62
63         # In rare instances, sshd may be started but a pid
64         # file is not generated. This is seen usually during
65         # system reboot scenarios. A subsequent attempt to start
66         # sshd here will trigger a port bind error. As a sanity
67         # check attempt a kill call (mostly redundant) to any
68         # previously running sshd instance
69         /bin/kill `/bin/pidof "$SSHD"` >/dev/null 2>&1
70
71         echo -n $"Starting $prog: "
72         $SSHD $OPTIONS && success || failure
73         RETVAL=$?
74         [ $RETVAL -eq 0 ] && touch $lockfile
75         [ $RETVAL -eq 0 ] && cp -f $XPID_FILE $PID_FILE
76         echo
77         return $RETVAL
78 }
79
80 stop()
81 {
82
83         echo -n $"Stopping $prog: "
84         if [ ! -f "$PID_FILE" ]; then
85                 # not running; per LSB standards this is "ok"
86                 action $"Stopping $prog: " /bin/true
87                 return 0
88         fi
89         PID=`cat "$PID_FILE"`
90         if [ -n "$PID" ]; then
91                 /bin/kill "$PID" >/dev/null 2>&1
92                 RETVAL=$?
93                 if [ $RETVAL -eq 0 ]; then
94                         RETVAL=1
95                         action $"Stopping $prog: " /bin/false
96                 else
97                         action $"Stopping $prog: " /bin/true
98                 fi
99         else
100                  # failed to read pidfile
101                 action $"Stopping $prog: " /bin/false
102                 RETVAL=4
103         fi
104         # if we are in halt or reboot runlevel kill all running sessions
105         # so the TCP connections are closed cleanly
106         if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
107             trap '' TERM
108             killall $prog 2>/dev/null
109             trap TERM
110         fi
111         [ $RETVAL -eq 0 ] && rm -f $lockfile
112         rm -f "$PID_FILE"
113         return $RETVAL
114 }
115
116 reload()
117 {
118         echo -n $"Reloading $prog: "
119         if [ -n "`pidfileofproc $SSHD`" ] ; then
120             killproc $SSHD -HUP
121         else
122             failure $"Reloading $prog"
123         fi
124         RETVAL=$?
125         echo
126 }
127
128 restart() {
129         stop
130         start
131 }
132
133 force_reload() {
134         restart
135 }
136
137 rh_status() {
138         status -p $PID_FILE openssh-daemon
139 }
140
141 rh_status_q() {
142         rh_status >/dev/null 2>&1
143 }
144
145 case "$1" in
146         start)
147                 rh_status_q && exit 0
148                 start
149                 ;;
150         stop)
151                 if ! rh_status_q; then
152                         rm -f $lockfile
153                         exit 0
154                 fi
155                 stop
156                 ;;
157         restart)
158                 restart
159                 ;;
160         reload)
161                 rh_status_q || exit 7
162                 reload
163                 ;;
164         force-reload)
165                 force_reload
166                 ;;
167         condrestart|try-restart)
168                 rh_status_q || exit 0
169                 if [ -f $lockfile ] ; then
170                         do_restart_sanity_check
171                         if [ $RETVAL -eq 0 ] ; then
172                                 stop
173                                 # avoid race
174                                 sleep 3
175                                 start
176                         else
177                                 RETVAL=6
178                         fi
179                 fi
180                 ;;
181         status)
182                 rh_status
183                 RETVAL=$?
184                 if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
185                         RETVAL=2
186                 fi
187                 ;;
188         *)
189                 echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
190                 RETVAL=2
191 esac
192 exit $RETVAL