Merge branch 'bug-1339' into release
[cacert-devel.git] / CommModule / commmodule
1 #! /bin/sh
2 # @(#)(CAcert) $Id: commmodule,v 1.1 2009-12-28 15:09:24 wytze Exp $
3 ### BEGIN INIT INFO
4 # Provides: commmodule
5 # Required-Start: $local_fs $remote_fs $syslog mysql
6 # Required-Stop: $local_fs $remote_fs $syslog
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: Start and stop the CAcert CommModule processes
10 # Description: This file should be used to start and stop the CAcert
11 # CommModule processes from /etc/init.d.
12 ### END INIT INFO
13
14 # Author: Wytze van der Raay <wytze@cacert.org>
15 #
16 # Please remove the "Author" lines above and replace them
17 # with your own name if you copy and modify this script.
18
19 # Do NOT "set -e"
20
21 # PATH should only include /usr/* if it runs after the mountnfs.sh script
22 PATH=/sbin:/usr/sbin:/bin:/usr/bin
23 DESC="CAcert CommModule processes start/stop"
24 NAME=commmodule
25 PIDFILE=/var/run/$NAME.pid
26 SCRIPTNAME=/etc/init.d/$NAME
27
28 # Read configuration variable file if it is present
29 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
30
31 # Load the VERBOSE setting and other rcS variables
32 . /lib/init/vars.sh
33
34 # Define LSB log_* functions.
35 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
36 . /lib/lsb/init-functions
37
38 if [ "$1" != "setup" ]
39 then
40 if [ "${COMMROOT}" = "" ]
41 then
42 echo "$0: COMMROOT not set"
43 exit 1
44 fi
45 if [ "${COMMROLE}" = "" ]
46 then
47 echo "$0: COMMROLE not set"
48 exit 1
49 fi
50 if [ ! -d ${COMMROOT} ]
51 then
52 echo "$0: ${COMMROOT} is not a directory"
53 exit 1
54 fi
55 if [ ! -x ${COMMROOT}/commdaemon ]
56 then
57 echo "$0: ${COMMROOT}/commdaemon not present or non-executable"
58 exit 1
59 fi
60 if [ ! -x ${COMMROOT}/${COMMROLE}.pl ]
61 then
62 echo "$0: ${COMMROOT}/${COMMROLE}.pl not present or non executable"
63 exit 1
64 fi
65
66 DAEMON=${COMMROOT}/commdaemon
67 DAEMON_ARGS="${COMMROOT}/${COMMROLE}.pl"
68 ACTIVE=${COMMROOT}/${COMMROLE}.pl-active
69 fi
70
71 # Load usbserial module with proper parameters
72 /sbin/modprobe usbserial vendor=0x067b product=0x2501
73
74 #
75 # Function that starts the daemon/service
76 #
77 do_start()
78 {
79 # Return
80 # 0 if daemon has been started
81 # 1 if daemon was already running
82 # 2 if daemon could not be started
83 start-stop-daemon --start --quiet --pidfile $PIDFILE \
84 --chdir ${COMMROOT} --background --exec $DAEMON --test \
85 > /dev/null \
86 || return 1
87 start-stop-daemon --start --quiet --pidfile $PIDFILE \
88 --chdir ${COMMROOT} --background --exec $DAEMON -- \
89 $DAEMON_ARGS \
90 || return 2
91 # Add code here, if necessary, that waits for the process to be ready
92 # to handle requests from services started subsequently which depend
93 # on this one. As a last resort, sleep for some time.
94 }
95
96 #
97 # Function that stops the daemon/service
98 #
99 do_stop()
100 {
101 # Return
102 # 0 if daemon has been stopped
103 # 1 if daemon was already stopped
104 # 2 if daemon could not be stopped
105 # other if a failure occurred
106 rm -f ${ACTIVE}
107 sleep 5
108 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
109 RETVAL="$?"
110 [ "$RETVAL" = 2 ] && return 2
111 # Many daemons don't delete their pidfiles when they exit.
112 rm -f $PIDFILE
113 return "$RETVAL"
114 }
115
116 case "$1" in
117 start)
118 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
119 do_start
120 case "$?" in
121 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
122 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
123 esac
124 ;;
125 stop)
126 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
127 do_stop
128 case "$?" in
129 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
130 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
131 esac
132 ;;
133 restart|force-reload)
134 log_daemon_msg "Restarting $DESC" "$NAME"
135 do_stop
136 case "$?" in
137 0|1)
138 do_start
139 case "$?" in
140 0) log_end_msg 0 ;;
141 1) log_end_msg 1 ;; # Old process is still running
142 *) log_end_msg 1 ;; # Failed to start
143 esac
144 ;;
145 *)
146 # Failed to stop
147 log_end_msg 1
148 ;;
149 esac
150 ;;
151 setup)
152 # setup default installation of commmodule scripts
153 cp ${NAME} /etc/init.d
154 chown root.root /etc/init.d/${NAME}
155 update-rc.d -f ${NAME} remove
156 update-rc.d ${NAME} defaults 90 20
157 (echo "COMMROOT=/home/cacert/www/CommModule";
158 echo "COMMROLE=client") >/etc/default/${NAME}
159 ;;
160 *)
161 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|setup}" >&2
162 exit 3
163 ;;
164 esac
165
166 :