You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=80
  3. STOP=20
  4. PIDFILE=/var/run/privoxy.pid
  5. CFGFILE=/var/etc/privoxy.conf
  6. CFGTEMP=/var/etc/privoxy.conf.tmp
  7. _uci2conf() {
  8. local _LOGDIR="/var/log" # set default
  9. local _LOGFILE="privoxy.log" # set default
  10. # redefined callback for options when calling config_load
  11. option_cb()
  12. {
  13. # $1 name of variable
  14. # $2 value
  15. local __OPT="$1"
  16. local __VAL="$2"
  17. case $__OPT in
  18. logdir) # logdir handled later
  19. _LOGDIR="$__VAL" ;;
  20. logfile) # logfile handled later
  21. _LOGFILE="$__VAL" ;;
  22. *)
  23. # detect list options (LENGTH) and ignore
  24. echo $__OPT | grep -i "_LENGTH" >/dev/null 2>&1 && return
  25. # detect list options (ITEM) and ignore
  26. echo $__OPT | grep -i "_ITEM" >/dev/null 2>&1 && __OPT=$(echo $__OPT | sed -e "s#_ITEM.##g")
  27. # filter debug_*
  28. echo $__OPT | grep -i "debug_" >/dev/null 2>&1 && {
  29. [ $__VAL -eq 0 ] && return # not set ignore
  30. __VAL=$(echo $__OPT | sed -e "s#debug_##g")
  31. __OPT="debug"
  32. }
  33. # uci only accept "_" but we need "-"
  34. local __OPT=$(echo $__OPT | sed -e "s#_#-#g")
  35. # write to config
  36. echo -e "$__OPT\t$__VAL" >> $CFGTEMP
  37. ;;
  38. esac
  39. }
  40. mkdir -m0755 -p /var/etc
  41. echo "" > $CFGTEMP # create tmp config file
  42. chmod 644 $CFGTEMP # garantee that privoxy can read
  43. chgrp privoxy $CFGTEMP
  44. echo '### AUTO-GENERATED CONFIGURATION' >> $CFGTEMP
  45. echo '### USED BY PRIVOXY' >> $CFGTEMP
  46. echo '### DO NOT EDIT' >> $CFGTEMP
  47. echo '### SEE /etc/config/privoxy INSTEAD' >> $CFGTEMP
  48. echo '' >> $CFGTEMP
  49. config_load privoxy # calling above option_cb()
  50. # write logdir/logfile to config
  51. echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
  52. echo -e "logfile\t$_LOGFILE" >> $CFGTEMP
  53. # create logfile and set permissions
  54. touch $_LOGDIR/$_LOGFILE
  55. chmod 664 $_LOGDIR/$_LOGFILE
  56. chown privoxy:privoxy $_LOGDIR/$_LOGFILE
  57. # move temp to final privoxy readable configuration
  58. mv -f $CFGTEMP $CFGFILE
  59. }
  60. boot() {
  61. return 0 # will be started by "iface" hotplug events
  62. }
  63. start() {
  64. # if already running do nothing
  65. local _PID=$(cat $PIDFILE 2>/dev/null)
  66. kill -1 $_PID 2>/dev/null && return 0
  67. _uci2conf
  68. /usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
  69. # verify startup
  70. _PID=$(cat $PIDFILE 2>/dev/null)
  71. kill -1 $_PID 2>/dev/null
  72. local _ERR=$?
  73. [ $_ERR -eq 0 ] \
  74. && logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
  75. || logger -p daemon.warn -t "privoxy[-----]" "Failed to start"
  76. return $_ERR
  77. }
  78. reload() {
  79. # reload is also used by luci-app-privoxy
  80. local _PID=$(cat $PIDFILE 2>/dev/null)
  81. kill -1 $_PID 2>/dev/null
  82. if [ $? -eq 0 ]; then
  83. # only restart if already running
  84. restart
  85. else
  86. # only start if enabled
  87. enabled && start
  88. fi
  89. return 0
  90. }
  91. stop() {
  92. local _PID=$(cat $PIDFILE 2>/dev/null)
  93. kill -15 $_PID 2>/dev/null
  94. sleep 1 # give time to shutdown
  95. local _tmp=$(pgrep privoxy)
  96. if [ -z "$_tmp" ]; then
  97. logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
  98. else
  99. killall -9 privoxy
  100. logger -p daemon.warn -t "privoxy[-----]" "Shutdown forced by KILL"
  101. fi
  102. return 0
  103. }