#!/bin/bash

# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2025 Oracle.  All Rights Reserved.
#
# Try starting things in a private pid/mount namespace with a private /tmp
# and /proc so that child process trees cannot interfere with each other.

if [ -n "${FSTESTS_ISOL}" ]; then
	# Don't allow changes to these mountpoints to propagate
	# outside of our mount namespace...
	for path in /proc /tmp; do
		mountpoint "$path" >/dev/null && \
			mount --make-private "$path"
	done

	# ...because here we create new mounts that are private
	# to this mount namespace that we don't want to escape.
	mount -t proc proc /proc
	mount -t tmpfs tmpfs /tmp

	# Allow the test to become a target of the oom killer
	oom_knob="/proc/self/oom_score_adj"
	test -w "${oom_knob}" && echo 250 > "${oom_knob}"

	exec "$@"
fi

if [ -z "$1" ] || [ "$1" = "--help" ]; then
	echo "Usage: $0 command [args...]"
	exit 1
fi

FSTESTS_ISOL=privatens exec "$(dirname "$0")/../src/nsexec" -z -m -p "$0" "$@"
