4e96a1a6fd921d19109f2903ac433863b5b22c2d
[privoxy.git] / debian / init.d
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          privoxy
4 # Required-Start:    $local_fs $remote_fs $network $time
5 # Required-Stop:     $local_fs $remote_fs $network $time
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: Privacy enhancing HTTP Proxy
9 # Description:       Privoxy is a web proxy with advanced filtering
10 #                    capabilities for protecting privacy, filtering
11 #                    web page content, managing cookies, controlling
12 #                    access, and removing ads, banners, pop-ups and
13 #                    other obnoxious Internet junk.
14 ### END INIT INFO
15
16 # Author: Roland Rosenfeld <roland@debian.org>
17
18 # Do NOT "set -e"
19
20 # PATH should only include /usr/* if it runs after the mountnfs.sh script
21 PATH=/sbin:/usr/sbin:/bin:/usr/bin
22 DESC="filtering proxy server"
23 NAME=privoxy
24 DAEMON=/usr/sbin/$NAME
25 PIDFILE=/var/run/$NAME.pid
26 OWNER=privoxy
27 CONFIGFILE=/etc/privoxy/config
28 DAEMON_ARGS="--pidfile $PIDFILE --user $OWNER $CONFIGFILE"
29 SCRIPTNAME=/etc/init.d/$NAME
30 LOGDIR=/var/log/privoxy
31 DEFAULTSFILE=/etc/default/$NAME
32
33 # Exit if the package is not installed
34 [ -x "$DAEMON" ] || exit 0
35
36 # Read configuration variable file if it is present
37 [ -r $DEFAULTSFILE ] && . $DEFAULTSFILE
38
39 # Create log directory if it does not exist
40 if [ ! -d "$LOGDIR" ]; then
41     mkdir -m 750 $LOGDIR
42     chown $OWNER:adm $LOGDIR
43 fi
44
45 # Load the VERBOSE setting and other rcS variables
46 . /lib/init/vars.sh
47
48 # Define LSB log_* functions.
49 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
50 . /lib/lsb/init-functions
51
52 #
53 # Function that starts the daemon/service
54 #
55 do_start()
56 {
57         # Return
58         #   0 if daemon has been started
59         #   1 if daemon was already running
60         #   2 if daemon could not be started
61         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
62                 || return 1
63         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
64                 $DAEMON_ARGS \
65                 || return 2
66         # Add code here, if necessary, that waits for the process to be ready
67         # to handle requests from services started subsequently which depend
68         # on this one.  As a last resort, sleep for some time.
69 }
70
71 #
72 # Function that stops the daemon/service
73 #
74 do_stop()
75 {
76         # Return
77         #   0 if daemon has been stopped
78         #   1 if daemon was already stopped
79         #   2 if daemon could not be stopped
80         #   other if a failure occurred
81         start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
82         RETVAL="$?"
83         [ "$RETVAL" = 2 ] && return 2
84         # Wait for children to finish too if this is a daemon that forks
85         # and if the daemon is only ever run from this initscript.
86         # If the above conditions are not satisfied then add some other code
87         # that waits for the process to drop all resources that could be
88         # needed by services started subsequently.  A last resort is to
89         # sleep for some time.
90         start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
91         [ "$?" = 2 ] && return 2
92         # Many daemons don't delete their pidfiles when they exit.
93         rm -f $PIDFILE
94         return "$RETVAL"
95 }
96
97 #
98 # Function that sends a SIGHUP to the daemon/service
99 #
100 do_reload() {
101         #
102         # If the daemon can reload its configuration without
103         # restarting (for example, when it is sent a SIGHUP),
104         # then implement that here.
105         #
106         start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
107         return 0
108 }
109
110
111 case "$1" in
112   start)
113         if [ "$RUN_DAEMON" = "no" ]; then
114             [ "$VERBOSE" != no ] && log_warning_msg "Not starting $DESC (disabled in $DEFAULTSFILE)."
115             exit 0
116         fi
117
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   #reload|force-reload)
134         #
135         # If do_reload() is not implemented then leave this commented out
136         # and leave 'force-reload' as an alias for 'restart'.
137         #
138         #log_daemon_msg "Reloading $DESC" "$NAME"
139         #do_reload
140         #log_end_msg $?
141         #;;
142   restart|force-reload)
143         #
144         # If the "reload" option is implemented then remove the
145         # 'force-reload' alias
146         #
147         if [ "$RUN_DAEMON" = "no" ]; then
148             [ "$VERBOSE" != no ] && log_warning_msg "Not restarting $DESC (disabled in $DEFAULTSFILE)."
149             exit 0
150         fi
151
152         log_daemon_msg "Restarting $DESC" "$NAME"
153         do_stop
154         case "$?" in
155           0|1)
156                 do_start
157                 case "$?" in
158                         0) log_end_msg 0 ;;
159                         1) log_end_msg 1 ;; # Old process is still running
160                         *) log_end_msg 1 ;; # Failed to start
161                 esac
162                 ;;
163           *)
164                 # Failed to stop
165                 log_end_msg 1
166                 ;;
167         esac
168         ;;
169   status)
170         status_of_proc "$DAEMON" "$NAME"
171         exit $?
172         ;;
173
174   *)
175         #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
176         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
177         exit 3
178         ;;
179 esac
180
181 :