summaryrefslogtreecommitdiff
path: root/regress/usr.sbin/rebound/run.sh
blob: 1ff6c7cd489d8b1794fa1964bea953677de2a5c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
#
# $OpenBSD: run.sh,v 1.1 2018/05/09 19:34:53 anton Exp $
#
# Copyright (c) 2018 Anton Lindqvist <anton@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

set -eu

usage() {
	echo "usage: sh run.sh -a addr -n rebound-ns -r rebound file" 1>&2
	exit 1
}

# asserteq WANT GOT [MESSAGE]
asserteq() {
	[ "$1" = "$2" ] && return 0

	printf 'FAIL:\n\tWANT:\t"%s"\n\tGOT:\t"%s"\n' "$1" "$2"
	[ $# -eq 3 ] && printf '\tREASON:\t"%s"\n' "$3"
	FAIL=1
}

atexit() {
	local _err=$?

	# Kill daemons.
	pkillw "^${REBOUND}" "^${NS}"

	# Cleanup temporary files.
	rm -f $@

	if [ $_err -ne 0 ] || [ $FAIL -ne 0 ]; then
		exit 1
	else
		exit 0
	fi
}

pkillw() {
	local _pat _sig

	for _pat; do
		_sig=TERM
		while pgrep -f "$_pat" >/dev/null 2>&1; do
			pkill "-${_sig}" -f "$_pat"
			sleep .5
			_sig=KILL
		done
	done
}

resolve() {
	host $@ | sed -n '$p'
}

ENV='env MALLOC_OPTIONS=S'
FAIL=0
NADDR=0
NS=
REBOUND=

while getopts "a:n:r:" opt; do
	case "$opt" in
	a)	NADDR=$((NADDR + 1))
		eval "ADDR${NADDR}=${OPTARG}"
		;;
	n)	NS=$OPTARG;;
	r)	REBOUND=$OPTARG;;
	*)	usage;;
	esac
done
shift $((OPTIND - 1))
([ $# -ne 1 ] || [ $NADDR -eq 0 ] || [ -z "$NS" ] || [ -z "$REBOUND" ]) && usage

CONF=$(mktemp -t rebound.XXXXXX)
trap 'atexit $CONF' EXIT

. $1