squid3 3.5 KB

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