| 
									
										
										
										
											2013-11-05 18:10:22 +04:00
										 |  |  | #!/usr/bin/env watchman | 
					
						
							| 
									
										
										
										
											2014-07-25 00:31:44 +04:00
										 |  |  | # Two warnings: | 
					
						
							|  |  |  | # 1) This script uses a script to run cjdroute and feed it | 
					
						
							| 
									
										
										
										
											2015-05-18 14:19:04 +03:00
										 |  |  | #    the config. It can be found in the main cjdns repo: | 
					
						
							|  |  |  | #    contrib/sh/run-cjdroute.sh | 
					
						
							|  |  |  | #    Or here: http://ix.io/fYT | 
					
						
							| 
									
										
										
										
											2014-07-25 00:31:44 +04:00
										 |  |  | # 2) service_respawn is disabled by default because you need | 
					
						
							|  |  |  | #    "noBackground":1 in cjdroute.conf for it to actually work. | 
					
						
							|  |  |  | #    Barely. | 
					
						
							| 
									
										
										
										
											2013-11-05 18:10:22 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | unset reload | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-11 14:50:48 +03:00
										 |  |  | #service_respawn=true | 
					
						
							| 
									
										
										
										
											2014-09-30 08:49:21 +04:00
										 |  |  | service_command='/usr/bin/run-cjdroute' | 
					
						
							| 
									
										
										
										
											2014-04-05 23:59:52 +04:00
										 |  |  | cjdroute_admin_port='11234' | 
					
						
							| 
									
										
										
										
											2013-11-05 18:10:22 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-13 11:04:27 +04:00
										 |  |  | cjdroute.check_tun_module() { | 
					
						
							|  |  |  | 	[[ -e '/dev/net/tun' ]] || { | 
					
						
							|  |  |  | 		modprobe tun || { | 
					
						
							|  |  |  | 			watchman.err "Could not load module tun!" | 
					
						
							|  |  |  | 			return 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 18:10:22 +04:00
										 |  |  | start() { | 
					
						
							| 
									
										
										
										
											2015-10-12 21:26:04 +03:00
										 |  |  | 	if cjdroute.check_tun_module; then | 
					
						
							|  |  |  | 		watchman.start | 
					
						
							|  |  |  | 	fi | 
					
						
							| 
									
										
										
										
											2014-07-03 11:05:52 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 14:19:04 +03:00
										 |  |  | get_control_pid() { | 
					
						
							|  |  |  | 	lsof -i :"$cjdroute_admin_port" | tail -1 | cut -d ' ' -f 2 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | status() { | 
					
						
							|  |  |  | 	# Check the actual cjdroute process and not the watchdog | 
					
						
							|  |  |  | 	cjdroute_control_pid=$( get_control_pid ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	counter=0 | 
					
						
							|  |  |  | 	until kill -0 "$cjdroute_control_pid"; do | 
					
						
							|  |  |  | 		cjdroute_control_pid=$( get_control_pid ) | 
					
						
							|  |  |  | 		(( counter >= 5 )) && { | 
					
						
							|  |  |  | 			watchman.msg "cjdroute is down" | 
					
						
							|  |  |  | 			return 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		(( counter++ )) | 
					
						
							|  |  |  | 		sleep 1 | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	watchman.msg "cjdroute is running ($cjdroute_control_pid)" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-03 11:05:52 +04:00
										 |  |  | stop() { | 
					
						
							|  |  |  | 	[[ "$service_respawn" == 'true' ]] && { watchman.stop; } # stopping the watchdog | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	# Stopping the actual s-o-b daemon that won't die properly. | 
					
						
							| 
									
										
										
										
											2015-05-18 14:19:04 +03:00
										 |  |  | 	cjdroute_control_pid=$( get_control_pid ) | 
					
						
							| 
									
										
										
										
											2014-07-03 11:05:52 +04:00
										 |  |  | 	watchman.pid_check "$cjdroute_control_pid" && { | 
					
						
							|  |  |  | 		kill "$cjdroute_control_pid" | 
					
						
							|  |  |  | 		watchman.pid_wait "$cjdroute_control_pid" | 
					
						
							| 
									
										
										
										
											2014-06-28 10:54:06 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-05 18:10:22 +04:00
										 |  |  | } |