Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. cp /old/etc/init.d/tor /etc/init.d/tor
  2. cd /etc/init.d/
  3. chmod +x tor
  4. update-rc.d tor defaults 99
  5.  
  6. ./tor start
  7. [ ok ] Starting tor (via systemctl): tor.service.
  8.  
  9. #! /bin/bash
  10.  
  11. ### BEGIN INIT INFO
  12. # Provides: tor
  13. # Required-Start: $local_fs $remote_fs $network $named $time
  14. # Required-Stop: $local_fs $remote_fs $network $named $time
  15. # Should-Start: $syslog
  16. # Should-Stop: $syslog
  17. # Default-Start: 2 3 4 5
  18. # Default-Stop: 0 1 6
  19. # Short-Description: Starts The Onion Router daemon processes
  20. # Description: Start The Onion Router, a TCP overlay
  21. # network client that provides anonymous
  22. # transport.
  23. ### END INIT INFO
  24.  
  25. # Load the VERBOSE setting and other rcS variables
  26. . /lib/init/vars.sh
  27.  
  28. # Define LSB log_* functions.
  29. . /lib/lsb/init-functions
  30.  
  31. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  32. DAEMON=/usr/bin/tor
  33. NAME=tor
  34. DESC="tor daemon"
  35. TORLOGDIR=/var/log/tor
  36. TORPIDDIR=/var/lib/tor
  37. TORPID=$TORPIDDIR/tor0.pid
  38. DEFAULTSFILE=/etc/default/$NAME
  39. WAITFORDAEMON=60
  40. DEFAULT_ARGS="--defaults-torrc /usr/share/tor/tor-service-defaults-torrc"
  41. VERIFY_ARGS="--verify-config $DEFAULT_ARGS"
  42. USE_AA_EXEC="yes"
  43. ARGS=""
  44. if [ "${VERBOSE:-}" != "yes" ]; then
  45. ARGS="$ARGS --hush"
  46. fi
  47.  
  48. # Let's try to figure our some sane defaults:
  49. if [ -r /proc/sys/fs/file-max ]; then
  50. system_max=`cat /proc/sys/fs/file-max`
  51. if [ "$system_max" -gt "80000" ] ; then
  52. MAX_FILEDESCRIPTORS=32768
  53. elif [ "$system_max" -gt "40000" ] ; then
  54. MAX_FILEDESCRIPTORS=16384
  55. elif [ "$system_max" -gt "10000" ] ; then
  56. MAX_FILEDESCRIPTORS=8192
  57. else
  58. MAX_FILEDESCRIPTORS=1024
  59. cat << EOF
  60.  
  61. Warning: Your system has very few filedescriptors available in total.
  62.  
  63. Maybe you should try raising that by adding 'fs.file-max=100000' to your
  64. /etc/sysctl.conf file. Feel free to pick any number that you deem appropriate.
  65. Then run 'sysctl -p'. See /proc/sys/fs/file-max for the current value, and
  66. file-nr in the same directory for how many of those are used at the moment.
  67.  
  68. EOF
  69. fi
  70. else
  71. MAX_FILEDESCRIPTORS=8192
  72. fi
  73.  
  74. NICE=""
  75.  
  76. test -x $DAEMON || exit 0
  77.  
  78. # Include tor defaults if available
  79. if [ -f $DEFAULTSFILE ] ; then
  80. . $DEFAULTSFILE
  81. fi
  82.  
  83. wait_for_deaddaemon () {
  84. pid=$1
  85. sleep 1
  86. if test -n "$pid"
  87. then
  88. if kill -0 $pid 2>/dev/null
  89. then
  90. cnt=0
  91. while kill -0 $pid 2>/dev/null
  92. do
  93. cnt=`expr $cnt + 1`
  94. if [ $cnt -gt $WAITFORDAEMON ]
  95. then
  96. log_action_end_msg 1 "still running"
  97. exit 1
  98. fi
  99. sleep 1
  100. [ "`expr $cnt % 3`" != 2 ] || log_action_cont_msg ""
  101. done
  102. fi
  103. fi
  104. log_action_end_msg 0
  105. }
  106.  
  107.  
  108. check_torpiddir () {
  109. if test ! -d $TORPIDDIR; then
  110. mkdir -m 02750 "$TORPIDDIR"
  111. chown debian-tor:debian-tor "$TORPIDDIR"
  112. ! [ -x /sbin/restorecon ] || /sbin/restorecon "$TORPIDDIR"
  113. fi
  114.  
  115. if test ! -x $TORPIDDIR; then
  116. log_action_end_msg 1 "cannot access $TORPIDDIR directory, are you root?"
  117. exit 1
  118. fi
  119. }
  120.  
  121. check_torlogdir () {
  122. if test ! -d $TORLOGDIR; then
  123. mkdir -m 02750 "$TORLOGDIR"
  124. chown debian-tor:adm "$TORLOGDIR"
  125. ! [ -x /sbin/restorecon ] || /sbin/restorecon "$TORPIDDIR"
  126. fi
  127. }
  128.  
  129.  
  130. check_config () {
  131. if ! $DAEMON $VERIFY_ARGS > /dev/null; then
  132. log_failure_msg "Checking if $NAME configuration is valid"
  133. $DAEMON $VERIFY_ARGS >&2
  134. exit 1
  135. fi
  136. }
  137.  
  138. execute () {
  139. case "$1" in
  140. start)
  141. if [ "$RUN_DAEMON" != "yes" ]; then
  142. log_action_msg "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  143. exit 0
  144. fi
  145.  
  146. if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  147. [ "${VERBOSE:-}" != "yes" ] || log_action_begin_msg "Raising maximum number of filedescriptors (ulimit -n) for tor to $MAX_FILEDESCRIPTORS"
  148. if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  149. [ "${VERBOSE:-}" != "yes" ] || log_action_end_msg 0
  150. else
  151. [ "${VERBOSE:-}" != "yes" ] || log_action_end_msg 1
  152. fi
  153. fi
  154.  
  155. check_torpiddir
  156. check_torlogdir
  157. check_config
  158. rm -rf /var/run/tor
  159. mkdir -m 02750 /var/run/tor
  160. chown debian-tor:debian-tor /var/run/tor
  161.  
  162. log_action_begin_msg "Starting $DESC"
  163.  
  164. echo $TORPID
  165.  
  166. if start-stop-daemon --stop --signal 0 --quiet --pidfile $TORPID --exec $DAEMON; then
  167. log_action_end_msg 0 "already running"
  168. else
  169. if [ "$USE_AA_EXEC" = "yes" ] &&
  170. [ -x /usr/sbin/aa-status ] &&
  171. [ -x /usr/sbin/aa-exec ] &&
  172. [ -e /etc/apparmor.d/system_tor ] &&
  173. /usr/sbin/aa-status --enabled ; then
  174. AA_EXEC="--startas /usr/sbin/aa-exec"
  175. AA_EXEC_ARGS="--profile=system_tor -- $DAEMON"
  176. else
  177. AA_EXEC=""
  178. AA_EXEC_ARGS=""
  179. fi
  180. if start-stop-daemon --start --quiet
  181. --pidfile $TORPID
  182. $NICE
  183. $AA_EXEC
  184. --exec $DAEMON -- $AA_EXEC_ARGS $DEFAULT_ARGS $ARGS
  185. then
  186. log_action_end_msg 0
  187. else
  188. log_action_end_msg 1
  189. exit 1
  190. fi
  191. fi
  192. ;;
  193. stop)
  194. log_action_begin_msg "Stopping $DESC"
  195. pid=`cat $TORPID 2>/dev/null` || true
  196.  
  197. if test ! -f $TORPID -o -z "$pid"; then
  198. log_action_end_msg 0 "not running - there is no $TORPID"
  199. exit 0
  200. fi
  201.  
  202. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  203. wait_for_deaddaemon $pid
  204. elif kill -0 $pid 2>/dev/null; then
  205. log_action_end_msg 1 "Is $pid not $NAME? Is $DAEMON a different binary now?"
  206. exit 1
  207. else
  208. log_action_end_msg 1 "$DAEMON died: process $pid not running; or permission denied"
  209. exit 1
  210. fi
  211. ;;
  212. reload|force-reload)
  213. check_config
  214.  
  215. log_action_begin_msg "Reloading $DESC configuration"
  216. pid=`cat $TORPID 2>/dev/null` || true
  217.  
  218. if test ! -f $TORPID -o -z "$pid"; then
  219. log_action_end_msg 1 "not running - there is no $TORPID"
  220. exit 1
  221. fi
  222.  
  223. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  224. then
  225. log_action_end_msg 0
  226. elif kill -0 $pid 2>/dev/null; then
  227. log_action_end_msg 1 "Is $pid not $NAME? Is $DAEMON a different binary now?"
  228. exit 1
  229. else
  230. log_action_end_msg 1 "$DAEMON died: process $pid not running; or permission denied"
  231. exit 1
  232. fi
  233. ;;
  234. restart)
  235. check_config
  236.  
  237. $0 stop
  238. sleep 1
  239. $0 start
  240. ;;
  241. status)
  242. if test ! -r $(dirname $TORPID); then
  243. log_failure_msg "cannot read PID file $TORPID"
  244. exit 4
  245. fi
  246. pid=`cat $TORPID 2>/dev/null` || true
  247. if test ! -f $TORPID -o -z "$pid"; then
  248. log_failure_msg "$NAME is not running"
  249. exit 3
  250. fi
  251. if ps "$pid" >/dev/null 2>&1; then
  252. log_success_msg "$NAME is running"
  253. exit 0
  254. else
  255. log_failure_msg "$NAME is not running"
  256. exit 1
  257. fi
  258. ;;
  259. *)
  260. log_action_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
  261. exit 1
  262. ;;
  263. esac
  264. }
  265. # --- Multi-instance init ---
  266.  
  267. config="/etc/tor"
  268. arrrgs="$ARGS"
  269. command=$1
  270. shift
  271. instances=$*
  272.  
  273. instances() {
  274. case $instances in
  275. "")
  276. for c in $config/*.cfg
  277. do
  278. base=${c##*/}
  279. test -f "$c" && echo ${base%.cfg}
  280. done
  281. ;;
  282. *)
  283. echo "$instances"
  284. ;;
  285. esac
  286. }
  287.  
  288. case "$command" in
  289. start|stop|restart|reload|force-reload|status)
  290. highest=0
  291. for i in $(instances)
  292. do
  293. NAME=$i
  294. DESC=$i
  295. TORPID="$TORPIDDIR/$i.pid"
  296. ARGS="$arrrgs -f $config/$i.cfg"
  297. execute $command
  298. status=$?
  299. test $status -gt $highest && highest=$status
  300. done
  301. exit $highest
  302. ;;
  303. *)
  304. execute
  305. exit 1
  306. ;;
  307. esac
  308.  
  309. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement