Skip to content

Commit 9f54f7e

Browse files
committed
scripts(run-docker): better superuser detection
1 parent c81fa2a commit 9f54f7e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

scripts/run-docker.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ _detect_runtime() {
2929
fi
3030
}
3131

32+
# Detect superuser command.
33+
# If running as root, no sudo is needed. Otherwise, check for sudo.
34+
_detect_sudo() {
35+
if [ "$(id -u)" = "0" ]; then
36+
echo ""
37+
elif command -v sudo >/dev/null 2>&1; then
38+
echo "sudo"
39+
elif command -v doas >/dev/null 2>&1; then
40+
echo "doas"
41+
else
42+
echo "Error: This script must be run as root or with sudo/doas available in PATH" 1>&2
43+
exit 1
44+
fi
45+
}
46+
3247
_show_usage() {
3348
echo "Usage: $0 [OPTIONS] [COMMAND]"
3449
echo ""
@@ -153,9 +168,10 @@ else
153168
fi
154169

155170
USER=builder
171+
REAL_SUDO=$(_detect_sudo) || exit 1
156172

157173
if [ -n "${TERMUX_DOCKER_USE_SUDO-}" ]; then
158-
SUDO="sudo"
174+
SUDO=$REAL_SUDO
159175
else
160176
SUDO=""
161177
fi
@@ -190,7 +206,7 @@ if [ "$RUNTIME" = "docker" ]; then
190206
if [ -n "$msg" ]; then
191207
echo "$msg..."
192208
fi
193-
cat "$profile_path" | sed -e "s/{{CONTAINER_NAME}}/$CONTAINER_NAME/g" | sudo "$APPARMOR_PARSER" -rK
209+
cat "$profile_path" | sed -e "s/{{CONTAINER_NAME}}/$CONTAINER_NAME/g" | $REAL_SUDO "$APPARMOR_PARSER" -rK
194210
fi
195211
}
196212

0 commit comments

Comments
 (0)