File: run-ns

package info (click to toggle)
erlang 1%3A27.3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 225,000 kB
  • sloc: erlang: 1,658,966; ansic: 405,769; cpp: 177,850; xml: 82,435; makefile: 15,031; sh: 14,401; lisp: 9,812; java: 8,603; asm: 6,541; perl: 5,836; python: 5,484; sed: 72
file content (186 lines) | stat: -rwxr-xr-x 4,355 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#! /bin/sh
##
## %CopyrightBegin%
##
## Copyright Ericsson AB 2009-2023. All Rights Reserved.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## %CopyrightEnd%
##
#

#
## run-ns
##
## $0 IPAddress PortNum SubDir
##
## Helper script to run a very local name server for this test suite,
## on a given port number, with a test specific configuration.
##
## * Create a work directory ./SubDir, create a $CONF_FILE there
##   that includes $INC_FILE that refers to the zone files.
## * Locate name server and check its version.
## * Zopy zone files and $INC_FILE from `dirname $0`/SubDir to ./SubDir.
## * Start name server in ./SubDir with logging to $LOG_FILE there.
## * Wait for "quit" on stdin.
## * Terminate name server and wait for it.
##
## Prints status lines starting with tag and colon (think mail header):
##   Skip: have given up; no point in starting this name server
##   Error: have given up; failed to start name server
##   Running: name server is running, waiting for "quit"
##   Other tags: diagnostics info
#

unset LDPATH CDPATH ENV BASH_ENV
IFS=' 	'
PATH=/usr/sbin:/sbin:/usr/bin:/bin
SHELL=/bin/sh
export PATH SHELL

NS=knotd
LOG_FILE=ns.log
CONF_FILE=knot.conf
INC_FILE=knot_inc.conf

ERROR="Error: "
error () {
    r=$?
    echo "$ERROR$*"
    exit $r
}

skip () {
    r=$?
    echo "Skip: $*"
    exit $r
}

ME="$0"
SELF=$$

# Check argument: IP address
test :"$1" != : || \
    error "Empty argument 1: IP address !"
IP="$1"

# Check argument: Port number
expr "0$2" + 0 '>' 0  '&'  "0$2" + 0 '<' 65536 >/dev/null 2>&1 || \
    error "Invalid argument 2: port number !"
PORT="$2"

# Check argument: Work/Zone subdir
test :"$3" != : || \
    error "Empty argument 3: Work/Zone subdir!"
SRCDIR="`dirname "$ME"`/$3"
test -d "$SRCDIR" || \
    error "Missing zone directory: $SRCDIR !"
test -f "$SRCDIR/$INC_FILE" || \
    error "Missing include file: $SRCDIR/$INC_FILE !"
DIR="$3"

# Locate name server and check version.
for n in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin
do
    test -x "$n/$NS" && { NS="$n/$NS"; break; }
done

NS_VER="`"$NS" -V 2>&1`" || \
    skip "$NS: Not found!"
NS_VER="`echo "$NS_VER" | head -1`"
{ echo "$NS_VER" |
      grep -q -e ' 2[.]9[.]' -e ' 2[.][1-9][0-9][0-9]*[.]' \
           -e ' [3-9][.]' -e ' [1-9][0-9][0-9]*[.]'
} || skip "$NS_VER < 2.9"

# Create working directory and cd to it
mkdir "$DIR" >/dev/null 2>&1
cd "$DIR" >/dev/null 2>&1 || \
    error "Cannot cd: $DIR !"
DIR="$(pwd)"

# Create $CONF_FILE
cat >"$CONF_FILE" <<CONF_FILE
#
# $CONF_FILE for $NS_VER
# Generated by $ME.
#
# Copyright: see $ME.
#
server:
    listen: $IP@$PORT
    user: "$LOGNAME"
    rundir: $DIR

log:
  - target: stdout
    any: info

control:
    listen: /tmp/knot.$SELF.sock

database:
    storage: "$DIR"

template:
  - id: default
    storage: "$DIR"

include: $INC_FILE
CONF_FILE

# Copy all subdir files
( cd "$SRCDIR" && ls -1 ) | while read f; do
    cp -fp "$SRCDIR/$f" .
done

# Start command
set -- "$NS" -c "$CONF_FILE"

# Start nameserver
echo "Cwd: $DIR"
echo "Nameserver: $NS_VER"
echo "IP: $IP"
echo "Port: $PORT"
echo "Command: $@"
"$@" >"$LOG_FILE" 2>&1 </dev/null &
NS_PID=$!
echo "Pid: $NS_PID"
trap "kill $NS_PID >/dev/null 2>&1; wait $NS_PID >/dev/null 2>&1" \
    0 1 2 3 15
NS_STARTED=" info: server started .* PID $NS_PID\$"

# Wait for nameserver to start, start timer kill sub process
{ sleep 5 &&
      { echo "\n$ERROR$NS failed to start"
        kill $SELF
      }
} &
TIMER=$!

{
    echo $(exec /bin/sh -c 'echo $PPID') # Subshell PID
    exec tail -f "$LOG_FILE"
} | {
    read TAIL_PID
    sed -n "/$NS_STARTED/q"
    kill $TAIL_PID # Cannot trust SIGPIPE to stop tail
}
kill $TIMER

echo "Running: Enter \`\`quit'' to terminate nameserver[$NS_PID]..."
while read LINE; do
    test :"$LINE" = :'quit' && break
done
echo "Closing: Terminating nameserver..."