|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2026, The OpenThread Authors. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# 1. Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# 3. Neither the name of the copyright holder nor the |
| 14 | +# names of its contributors may be used to endorse or promote products |
| 15 | +# derived from this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | + |
| 30 | +# ot-br.sh - script to start an OTBR node from an OTNS simulation. |
| 31 | + |
| 32 | +# When a running script receives SIGTERM |
| 33 | +cleanup() |
| 34 | +{ |
| 35 | + echo "[DEBG] ot-br.sh: caught signal, cleaning up child processes" |
| 36 | + jobs -p | xargs -r kill |
| 37 | + wait |
| 38 | + echo "[DEBG] ot-br.sh: script exit" |
| 39 | + exit 0 |
| 40 | +} |
| 41 | + |
| 42 | +trap cleanup SIGINT SIGTERM |
| 43 | + |
| 44 | +sudo_command_failed() |
| 45 | +{ |
| 46 | + local cmd="$1" |
| 47 | + local cmd_path |
| 48 | + echo "[CRIT] passwordless sudo failed for '${cmd}'" |
| 49 | + echo "[CRIT] add below lines to /etc/sudoers (run: sudo visudo):" |
| 50 | + |
| 51 | + cmd="otbr-agent" |
| 52 | + cmd_path=$(command -v "$cmd" 2>/dev/null) |
| 53 | + echo "[CRIT] ${USER} ALL=(ALL) NOPASSWD: SETENV: ${cmd_path:-/usr/local/sbin/${cmd}}" |
| 54 | + |
| 55 | + cmd="ot-ctl" |
| 56 | + cmd_path=$(command -v "$cmd" 2>/dev/null) |
| 57 | + echo "[CRIT] ${USER} ALL=(ALL) NOPASSWD: ${cmd_path:-/usr/local/bin/${cmd}}" |
| 58 | + exit 1 |
| 59 | +} |
| 60 | + |
| 61 | +socket_in_use() |
| 62 | +{ |
| 63 | + if command -v ss >/dev/null 2>&1; then |
| 64 | + ss -x src "$1" | grep -q "$1" |
| 65 | + elif command -v lsof >/dev/null 2>&1; then |
| 66 | + lsof "$1" >/dev/null 2>&1 |
| 67 | + else |
| 68 | + return 1 # can't check, assume not in use (1 = false) |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +echo "[DEBG] ot-br.sh started" |
| 73 | + |
| 74 | +NODE_ID=$1 |
| 75 | +BACKBONE_IF_NAME=$2 |
| 76 | +AGENT_PARAM=$3 |
| 77 | +RADIO_URL=$4 |
| 78 | +# TODO: 'wpan1' for node 1 etc doesn't work - needs to be wpan0. Check later if there's a way to use others. |
| 79 | +THREAD_IF_NAME="wpan0" |
| 80 | +EXTRA_DELAY=0 |
| 81 | +MAX_WAIT_SEC=5 |
| 82 | +SOCKET_PATH="/run/openthread-${THREAD_IF_NAME}.sock" |
| 83 | +DATA_PATH="/var/lib/thread" |
| 84 | + |
| 85 | +echo "[DEBG] PORT_OFFSET =${PORT_OFFSET}" |
| 86 | +echo "[DEBG] NODE_ID =${NODE_ID}" |
| 87 | +echo "[DEBG] BACKBONE_IF_NAME=${BACKBONE_IF_NAME}" |
| 88 | +echo "[DEBG] THREAD_IF_NAME =${THREAD_IF_NAME}" |
| 89 | +echo "[DEBG] RADIO_URL =${RADIO_URL}" |
| 90 | +echo "[DEBG] SOCKET_PATH =${SOCKET_PATH}" |
| 91 | +echo "[DEBG] DATA_PATH =${DATA_PATH}" |
| 92 | +echo "[DEBG] AGENT_PARAM =${AGENT_PARAM}" |
| 93 | + |
| 94 | +# check for passwordless sudo access to commands |
| 95 | +sudo -n otbr-agent -V >/dev/null 2>&1 || sudo_command_failed otbr-agent |
| 96 | +sudo -n ot-ctl -h >/dev/null 2>&1 || sudo_command_failed ot-ctl |
| 97 | + |
| 98 | +# check for existing socket usage |
| 99 | +if [ -S "${SOCKET_PATH}" ]; then |
| 100 | + if socket_in_use "${SOCKET_PATH}"; then |
| 101 | + echo "[CRIT] socket ${SOCKET_PATH} is already in use - is otbr-agent already running on ${THREAD_IF_NAME}?" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + echo "[DEBG] existing (unused?) file ${SOCKET_PATH} detected - start with extra delay" |
| 105 | + EXTRA_DELAY=3 |
| 106 | +fi |
| 107 | + |
| 108 | +echo "[DEBG] starting otbr-agent" |
| 109 | +# All otbr-agent output redirected to stderr, so that ot-ctl CLI interactions are not garbled. |
| 110 | +sudo -n PORT_OFFSET="${PORT_OFFSET}" otbr-agent --data-path "${DATA_PATH}" -s -d 7 -I ${THREAD_IF_NAME} -B "${BACKBONE_IF_NAME}" \ |
| 111 | + "${AGENT_PARAM}" "${RADIO_URL}" 1>&2 & |
| 112 | +SUDO_OTBR_PID=$! |
| 113 | + |
| 114 | +echo "[DEBG] otbr-agent started in background (parent PID=${SUDO_OTBR_PID}) - waiting until ready" |
| 115 | + |
| 116 | +# wait for otbr-agent to create its Unix socket |
| 117 | +elapsed=0 |
| 118 | +while [ ! -S "${SOCKET_PATH}" ]; do |
| 119 | + if ! kill -0 "${SUDO_OTBR_PID}" 2>/dev/null; then |
| 120 | + echo "[CRIT] otbr-agent exited before socket was ready" |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | + if [ "${elapsed}" -ge "$((MAX_WAIT_SEC * 10))" ]; then |
| 124 | + echo "[CRIT] timed out waiting for otbr-agent socket (${MAX_WAIT_SEC}s)" |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + sleep 0.1 |
| 128 | + elapsed=$((elapsed + 1)) |
| 129 | +done |
| 130 | +echo "[DEBG] otbr-agent socket ready after $((elapsed / 10)).$((elapsed % 10))s, starting ot-ctl CLI" |
| 131 | +sleep ${EXTRA_DELAY} |
| 132 | +sudo -n ot-ctl |
| 133 | + |
| 134 | +echo "[DEBG] ot-br.sh: ot-ctl CLI exited, cleaning up child processes" |
| 135 | +jobs -p | xargs -r kill |
| 136 | +wait |
| 137 | +echo "[DEBG] ot-br.sh: script exit" |
0 commit comments