wg-quick: kill route monitor when loop terminates
If the route monitor doesn't attempt to write more to stdout, then this leaves a process hanging around. Kill it explicitly. We also switch to using exec in the process substitution, to reduce a bash process. Closes: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255286 Reported-by: Christos Chatzaras <chris@cretaforce.gr> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
		
							parent
							
								
									3124afbea3
								
							
						
					
					
						commit
						96e42feb3f
					
				@ -324,22 +324,24 @@ monitor_daemon() {
 | 
				
			|||||||
	echo "[+] Backgrounding route monitor" >&2
 | 
						echo "[+] Backgrounding route monitor" >&2
 | 
				
			||||||
	(trap 'del_routes; del_dns; exit 0' INT TERM EXIT
 | 
						(trap 'del_routes; del_dns; exit 0' INT TERM EXIT
 | 
				
			||||||
	exec >/dev/null 2>&1
 | 
						exec >/dev/null 2>&1
 | 
				
			||||||
	local event pid=$BASHPID
 | 
						exec 19< <(exec route -n monitor)
 | 
				
			||||||
 | 
						local event bpid=$BASHPID mpid=$!
 | 
				
			||||||
	[[ ${#DNS[@]} -gt 0 ]] && trap set_dns ALRM
 | 
						[[ ${#DNS[@]} -gt 0 ]] && trap set_dns ALRM
 | 
				
			||||||
	# TODO: this should also check to see if the endpoint actually changes
 | 
						# TODO: this should also check to see if the endpoint actually changes
 | 
				
			||||||
	# in response to incoming packets, and then call set_endpoint_direct_route
 | 
						# in response to incoming packets, and then call set_endpoint_direct_route
 | 
				
			||||||
	# then too. That function should be able to gracefully cleanup if the
 | 
						# then too. That function should be able to gracefully cleanup if the
 | 
				
			||||||
	# endpoints change.
 | 
						# endpoints change.
 | 
				
			||||||
	while read -r event; do
 | 
						while read -u 19 -r event; do
 | 
				
			||||||
		[[ $event == RTM_* ]] || continue
 | 
							[[ $event == RTM_* ]] || continue
 | 
				
			||||||
		ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break
 | 
							ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break
 | 
				
			||||||
		[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
							[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
				
			||||||
		[[ -z $MTU ]] && set_mtu
 | 
							[[ -z $MTU ]] && set_mtu
 | 
				
			||||||
		if [[ ${#DNS[@]} -gt 0 ]]; then
 | 
							if [[ ${#DNS[@]} -gt 0 ]]; then
 | 
				
			||||||
			set_dns
 | 
								set_dns
 | 
				
			||||||
			sleep 2 && kill -ALRM $pid 2>/dev/null &
 | 
								sleep 2 && kill -ALRM $bpid 2>/dev/null &
 | 
				
			||||||
		fi
 | 
							fi
 | 
				
			||||||
	done < <(route -n monitor)) &
 | 
						done
 | 
				
			||||||
 | 
						kill $mpid) &
 | 
				
			||||||
	[[ -n $LAUNCHED_BY_LAUNCHD ]] || disown
 | 
						[[ -n $LAUNCHED_BY_LAUNCHD ]] || disown
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -284,17 +284,19 @@ monitor_daemon() {
 | 
				
			|||||||
	(make_temp
 | 
						(make_temp
 | 
				
			||||||
	trap 'del_routes; clean_temp; exit 0' INT TERM EXIT
 | 
						trap 'del_routes; clean_temp; exit 0' INT TERM EXIT
 | 
				
			||||||
	exec >/dev/null 2>&1
 | 
						exec >/dev/null 2>&1
 | 
				
			||||||
	local event
 | 
						exec 19< <(exec route -n monitor)
 | 
				
			||||||
 | 
						local event pid=$!
 | 
				
			||||||
	# TODO: this should also check to see if the endpoint actually changes
 | 
						# TODO: this should also check to see if the endpoint actually changes
 | 
				
			||||||
	# in response to incoming packets, and then call set_endpoint_direct_route
 | 
						# in response to incoming packets, and then call set_endpoint_direct_route
 | 
				
			||||||
	# then too. That function should be able to gracefully cleanup if the
 | 
						# then too. That function should be able to gracefully cleanup if the
 | 
				
			||||||
	# endpoints change.
 | 
						# endpoints change.
 | 
				
			||||||
	while read -r event; do
 | 
						while read -u 19 -r event; do
 | 
				
			||||||
		[[ $event == RTM_* ]] || continue
 | 
							[[ $event == RTM_* ]] || continue
 | 
				
			||||||
		ifconfig "$INTERFACE" >/dev/null 2>&1 || break
 | 
							ifconfig "$INTERFACE" >/dev/null 2>&1 || break
 | 
				
			||||||
		[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
							[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
				
			||||||
		# TODO: set the mtu as well, but only if up
 | 
							# TODO: set the mtu as well, but only if up
 | 
				
			||||||
	done < <(route -n monitor)) & disown
 | 
						done
 | 
				
			||||||
 | 
						kill $pid) & disown
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HAVE_SET_DNS=0
 | 
					HAVE_SET_DNS=0
 | 
				
			||||||
 | 
				
			|||||||
@ -266,17 +266,19 @@ monitor_daemon() {
 | 
				
			|||||||
	echo "[+] Backgrounding route monitor" >&2
 | 
						echo "[+] Backgrounding route monitor" >&2
 | 
				
			||||||
	(trap 'del_routes; exit 0' INT TERM EXIT
 | 
						(trap 'del_routes; exit 0' INT TERM EXIT
 | 
				
			||||||
	exec >/dev/null 2>&1
 | 
						exec >/dev/null 2>&1
 | 
				
			||||||
	local event
 | 
						exec 19< <(exec route -n monitor)
 | 
				
			||||||
 | 
						local event pid=$!
 | 
				
			||||||
	# TODO: this should also check to see if the endpoint actually changes
 | 
						# TODO: this should also check to see if the endpoint actually changes
 | 
				
			||||||
	# in response to incoming packets, and then call set_endpoint_direct_route
 | 
						# in response to incoming packets, and then call set_endpoint_direct_route
 | 
				
			||||||
	# then too. That function should be able to gracefully cleanup if the
 | 
						# then too. That function should be able to gracefully cleanup if the
 | 
				
			||||||
	# endpoints change.
 | 
						# endpoints change.
 | 
				
			||||||
	while read -r event; do
 | 
						while read -u 19 -r event; do
 | 
				
			||||||
		[[ $event == RTM_* ]] || continue
 | 
							[[ $event == RTM_* ]] || continue
 | 
				
			||||||
		ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break
 | 
							ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break
 | 
				
			||||||
		[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
							[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
 | 
				
			||||||
		# TODO: set the mtu as well, but only if up
 | 
							# TODO: set the mtu as well, but only if up
 | 
				
			||||||
	done < <(route -n monitor)) & disown
 | 
						done
 | 
				
			||||||
 | 
						kill $pid) & disown
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set_dns() {
 | 
					set_dns() {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user