squid3 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #! /bin/sh
  2. NAME=squid3
  3. DESC="Squid HTTP Proxy"
  4. DAEMON=/usr/sbin/squid3
  5. PIDFILE=/var/run/$NAME.pid
  6. CONFIG=/etc/squid3/squid.conf
  7. SQUID_ARGS="-YC -f $CONFIG"
  8. [ ! -f /etc/default/squid ] || . /etc/default/squid
  9. . /lib/lsb/init-functions
  10. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  11. [ -x $DAEMON ] || exit 0
  12. ulimit -n 65535
  13. find_cache_dir () {
  14. w=" "
  15. res=`$DAEMON -k parse -f $CONFIG 2>&1 |
  16. grep "Processing:" |
  17. sed s/.*Processing:\ // |
  18. sed -ne '
  19. s/^['"$w"']*'$1'['"$w"']\+[^'"$w"']\+['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
  20. t end;
  21. d;
  22. :end q'`
  23. [ -n "$res" ] || res=$2
  24. echo "$res"
  25. }
  26. grepconf () {
  27. w=" "
  28. res=`$DAEMON -k parse -f $CONFIG 2>&1 |
  29. grep "Processing:" |
  30. sed s/.*Processing:\ // |
  31. sed -ne '
  32. s/^['"$w"']*'$1'['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
  33. t end;
  34. d;
  35. :end q'`
  36. [ -n "$res" ] || res=$2
  37. echo "$res"
  38. }
  39. create_run_dir () {
  40. run_dir=/var/run/squid3
  41. usr=`grepconf cache_effective_user proxy`
  42. grp=`grepconf cache_effective_group proxy`
  43. if [ "$(dpkg-statoverride --list $run_dir)" = "" ] &&
  44. [ ! -e $run_dir ] ; then
  45. mkdir -p $run_dir
  46. chown $usr:$grp $run_dir
  47. [ -x /sbin/restorecon ] && restorecon $run_dir
  48. fi
  49. }
  50. start () {
  51. cache_dir=`find_cache_dir cache_dir`
  52. cache_type=`grepconf cache_dir`
  53. run_dir=/var/run/squid3
  54. create_run_dir
  55. if test -d "$cache_dir" -a ! -d "$cache_dir/00"
  56. then
  57. log_warning_msg "Creating $DESC cache structure"
  58. $DAEMON -z -f $CONFIG
  59. [ -x /sbin/restorecon ] && restorecon -R $cache_dir
  60. fi
  61. umask 027
  62. ulimit -n 65535
  63. cd $run_dir
  64. start-stop-daemon --quiet --start \
  65. --pidfile $PIDFILE \
  66. --exec $DAEMON -- $SQUID_ARGS < /dev/null
  67. return $?
  68. }
  69. stop () {
  70. PID=`cat $PIDFILE 2>/dev/null`
  71. start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
  72. sleep 2
  73. if test -n "$PID" && kill -0 $PID 2>/dev/null
  74. then
  75. log_action_begin_msg " Waiting"
  76. cnt=0
  77. while kill -0 $PID 2>/dev/null
  78. do
  79. cnt=`expr $cnt + 1`
  80. if [ $cnt -gt 24 ]
  81. then
  82. log_action_end_msg 1
  83. return 1
  84. fi
  85. sleep 5
  86. log_action_cont_msg ""
  87. done
  88. log_action_end_msg 0
  89. return 0
  90. else
  91. return 0
  92. fi
  93. }
  94. cfg_pidfile=`grepconf pid_filename`
  95. if test "${cfg_pidfile:-none}" != "none" -a "$cfg_pidfile" != "$PIDFILE"
  96. then
  97. log_warning_msg "squid.conf pid_filename overrides init script"
  98. PIDFILE="$cfg_pidfile"
  99. fi
  100. case "$1" in
  101. start)
  102. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  103. if test -n "$res";
  104. then
  105. log_failure_msg "$res"
  106. exit 3
  107. else
  108. log_daemon_msg "Starting $DESC" "$NAME"
  109. if start ; then
  110. log_end_msg $?
  111. else
  112. log_end_msg $?
  113. fi
  114. fi
  115. ;;
  116. stop)
  117. log_daemon_msg "Stopping $DESC" "$NAME"
  118. if stop ; then
  119. log_end_msg $?
  120. else
  121. log_end_msg $?
  122. fi
  123. ;;
  124. reload|force-reload)
  125. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  126. if test -n "$res";
  127. then
  128. log_failure_msg "$res"
  129. exit 3
  130. else
  131. log_action_msg "Reloading $DESC configuration files"
  132. start-stop-daemon --stop --signal 1 \
  133. --pidfile $PIDFILE --quiet --exec $DAEMON
  134. log_action_end_msg 0
  135. fi
  136. ;;
  137. restart)
  138. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  139. if test -n "$res";
  140. then
  141. log_failure_msg "$res"
  142. exit 3
  143. else
  144. log_daemon_msg "Restarting $DESC" "$NAME"
  145. stop
  146. if start ; then
  147. log_end_msg $?
  148. else
  149. log_end_msg $?
  150. fi
  151. fi
  152. ;;
  153. status)
  154. status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit 3
  155. ;;
  156. *)
  157. echo "Utilize: /etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}"
  158. exit 3
  159. ;;
  160. esac
  161. exit 0