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="${TST_SETUP:-binfmt_misc_setup}" 8TST_CLEANUP="${TST_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 14rmod_binfmt_misc=0 15umount_binfmt_misc=0 16binfmt_misc_mntpoint="ltp_binfmt_misc" 17 18remove_binary_type() 19{ 20 local name=$1 21 22 (echo -1 >"$name") 2>/dev/null 23 [ $? -ne 0 -o -f "$name" ] && \ 24 tst_res TWARN "Fail to remove a binary type" 25} 26 27get_binfmt_misc_mntpoint() 28{ 29 local mntpoint 30 31 mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts) 32 [ -z "$mntpoint" ] && tst_brk TBROK "Can't get binfmt_misc mntpoint" 33 34 echo "$mntpoint" 35} 36 37binfmt_misc_setup() 38{ 39 local mntpoint 40 41 if ! grep -q "binfmt_misc" /proc/filesystems; then 42 ROD modprobe binfmt_misc 43 rmod_binfmt_misc=1 44 fi 45 46 # Match fs type accurately, because autofs is also mounted on 47 # /proc/sys/fs/binfmt_misc on some distros, as below: 48 # cat /proc/mounts | grep binfmt_misc 49 # systemd-1 /proc/sys/fs/binfmt_misc autofs ... 50 # binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc ... 51 mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts) 52 [ -n "$mntpoint" ] && return 53 54 ROD mkdir ${binfmt_misc_mntpoint} 55 ROD mount -t binfmt_misc none ${binfmt_misc_mntpoint} 56 umount_binfmt_misc=1 57} 58 59binfmt_misc_cleanup() 60{ 61 if [ ${umount_binfmt_misc} -ne 0 ]; then 62 umount ${binfmt_misc_mntpoint} 63 umount_binfmt_misc=0 64 fi 65 66 [ -d ${binfmt_misc_mntpoint} ] && rm -rf ${binfmt_misc_mntpoint} 67 68 if [ ${rmod_binfmt_misc} -ne 0 ]; then 69 modprobe -r binfmt_misc 70 rmod_binfmt_misc=0 71 fi 72} 73 74. tst_test.sh 75