Reorder need_bind in struct configuration_spec to save memory
[privoxy.git] / debian / init.d
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          privoxy
4 # Required-Start:    $local_fs $remote_fs $named $network $time
5 # Required-Stop:     $local_fs $remote_fs $named $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=/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 # Define LSB log_* functions.
46 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
47 . /lib/lsb/init-functions
48
49 #
50 # Function that starts the daemon/service
51 #
52 do_start()
53 {
54         # Return
55         #   0 if daemon has been started
56         #   1 if daemon was already running
57         #   2 if daemon could not be started
58         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
59                 || return 1
60         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
61                 $DAEMON_ARGS \
62                 || return 2
63         # Add code here, if necessary, that waits for the process to be ready
64         # to handle requests from services started subsequently which depend
65         # on this one.  As a last resort, sleep for some time.
66 }
67
68 #
69 # Function that stops the daemon/service
70 #
71 do_stop()
72 {
73         # Return
74         #   0 if daemon has been stopped
75         #   1 if daemon was already stopped
76         #   2 if daemon could not be stopped
77         #   other if a failure occurred
78         start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
79         RETVAL="$?"
80         [ "$RETVAL" = 2 ] && return 2
81         # Wait for children to finish too if this is a daemon that forks
82         # and if the daemon is only ever run from this initscript.
83         # If the above conditions are not satisfied then add some other code
84         # that waits for the process to drop all resources that could be
85         # needed by services started subsequently.  A last resort is to
86         # sleep for some time.
87         start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
88         [ "$?" = 2 ] && return 2
89         # Many daemons don't delete their pidfiles when they exit.
90         rm -f $PIDFILE
91         return "$RETVAL"
92 }
93
94 #
95 # Function that sends a SIGHUP to the daemon/service
96 #
97 do_rotate() {
98         #
99         # If the daemon can reload its configuration without
100         # restarting (for example, when it is sent a SIGHUP),
101         # then implement that here.
102         #
103         start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE --name $NAME
104         return 0
105 }
106
107
108 case "$1" in
109   start)
110         if [ "$RUN_DAEMON" = "no" ]; then
111             log_warning_msg "Not starting $DESC (disabled in $DEFAULTSFILE)."
112             exit 0
113         fi
114
115         log_daemon_msg "Starting $DESC" "$NAME"
116         do_start
117         case "$?" in
118                 0|1) log_end_msg 0 ;;
119                 2) log_end_msg 1 ;;
120         esac
121         ;;
122   stop)
123         log_daemon_msg "Stopping $DESC" "$NAME"
124         do_stop
125         case "$?" in
126                 0|1) log_end_msg 0 ;;
127                 2) log_end_msg 1 ;;
128         esac
129         ;;
130   #reload|force-reload)
131         #
132         # If do_reload() is not implemented then leave this commented out
133         # and leave 'force-reload' as an alias for 'restart'.
134         #
135         #log_daemon_msg "Reloading $DESC" "$NAME"
136         #do_reload
137         #log_end_msg $?
138         #;;
139   rotate)
140         log_daemon_msg "Closing open files" "$NAME"
141         do_rotate
142         log_end_msg $?
143         ;;
144   restart|force-reload)
145         #
146         # If the "reload" option is implemented then remove the
147         # 'force-reload' alias
148         #
149         if [ "$RUN_DAEMON" = "no" ]; then
150             log_warning_msg "Not restarting $DESC (disabled in $DEFAULTSFILE)."
151             exit 0
152         fi
153
154         log_daemon_msg "Restarting $DESC" "$NAME"
155         do_stop
156         case "$?" in
157           0|1)
158                 do_start
159                 case "$?" in
160                         0) log_end_msg 0 ;;
161                         1) log_end_msg 1 ;; # Old process is still running
162                         *) log_end_msg 1 ;; # Failed to start
163                 esac
164                 ;;
165           *)
166                 # Failed to stop
167                 log_end_msg 1
168                 ;;
169         esac
170         ;;
171   status)
172         status_of_proc "$DAEMON" "$NAME"
173         exit $?
174         ;;
175
176   *)
177         #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
178         echo "Usage: $SCRIPTNAME {start|stop|rotate|restart|force-reload|status}" >&2
179         exit 3
180         ;;
181 esac
182
183 :