• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3#
4# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
5# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
6
7TST_SETUP=binfmt_misc_setup
8TST_CLEANUP=binfmt_misc_cleanup
9TST_NEEDS_DRIVERS="binfmt_misc"
10TST_NEEDS_TMPDIR=1
11TST_NEEDS_ROOT=1
12TST_NEEDS_CMDS="${TST_NEEDS_CMDS} modprobe mount umount mkdir rm"
13
14. tst_test.sh
15
16rmod_binfmt_misc=0
17umount_binfmt_misc=0
18binfmt_misc_mntpoint="ltp_binfmt_misc"
19
20remove_binary_type()
21{
22	local name=$1
23
24	(echo -1 >"$name") 2>/dev/null
25	[ $? -ne 0 -o -f "$name" ] && \
26		tst_res TWARN "Fail to remove a binary type"
27}
28
29get_binfmt_misc_mntpoint()
30{
31	local mntpoint
32
33	mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts)
34	[ -z "$mntpoint" ] && tst_brk TBROK "Can't get binfmt_misc mntpoint"
35
36	echo "$mntpoint"
37}
38
39binfmt_misc_setup()
40{
41	local mntpoint
42
43	if ! grep -q "binfmt_misc" /proc/filesystems; then
44		ROD modprobe binfmt_misc
45		rmod_binfmt_misc=1
46	fi
47
48	# Match fs type accurately, because autofs is also mounted on
49	# /proc/sys/fs/binfmt_misc on some distros, as below:
50	# cat /proc/mounts | grep binfmt_misc
51	# systemd-1 /proc/sys/fs/binfmt_misc autofs ...
52	# binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc ...
53	mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts)
54	[ -n "$mntpoint" ] && return
55
56	ROD mkdir ${binfmt_misc_mntpoint}
57	ROD mount -t binfmt_misc none ${binfmt_misc_mntpoint}
58	umount_binfmt_misc=1
59}
60
61binfmt_misc_cleanup()
62{
63	if [ ${umount_binfmt_misc} -ne 0 ]; then
64		umount ${binfmt_misc_mntpoint}
65		umount_binfmt_misc=0
66	fi
67
68	[ -d ${binfmt_misc_mntpoint} ] && rm -rf ${binfmt_misc_mntpoint}
69
70	if [ ${rmod_binfmt_misc} -ne 0 ]; then
71		modprobe -r binfmt_misc
72		rmod_binfmt_misc=0
73	fi
74}
75