make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / root-tools / hidedir
blob481b1f551a3900f3993ede9d1d0704c15079cc8d
1 #!/bin/bash
3 declare -a dirs
4 declare -A dirs_k=()
5 declare -a types
6 declare -A types_k
8 hidemount()
10 echo hide $1 >&2
11 umount -i "$1"
12 if [ $? != 0 -a -d "$1" ]
13 then
14 local emptydir
15 if [ -d /var/run/empty ]
16 then
17 emptydir=/var/run/empty
18 elif [ -d /var/empty ]
19 then
20 emptydir=/var/empty
21 else
22 mkdir /var/run/empty
23 emptydir=/var/run/empty
25 mount -o bind "$emptydir" "$1"
26 if [ $? != 0 ]
27 then
28 echo "Unable to hide \`$1'. Exiting." >&2
29 exit 2
34 getoptions()
36 netdev=0
37 dirs=()
38 types=()
39 types_k=()
40 inner=0
42 while [ -n "$1" ]
44 case "$1" in
45 --help|-h|'')
46 echo "Usage: $0 [--netdev] [--dir DIR1:DIR2:...] [--fstype TYPE1,TYPE2,...] [cmd [args...]]" >&2
47 exit 1
49 --netdev)
50 netdev=1
52 --dir)
53 shift
54 dirs=(${1//:/ })
56 --fstype)
57 shift
58 types=(${1//,/ })
59 for t in "${types[@]}"
61 types_k["$t"]=1
62 done
64 -u)
65 inner=1
67 --)
68 shift
69 break
71 -*)
72 echo "Unknown option: $1" >&2
73 exit 1
76 break
78 esac
79 shift
80 done
82 cmdargs=("$@")
86 myargs=("$@")
87 getoptions "$@"
90 if [ $inner = 0 ]
91 then
92 exec unshare -m -- $0 -u "${myargs[@]}"
93 exit
97 if [ ${#dirs[@]} = 0 -a ${#types[@]} = 0 ]
98 then
99 netdev=1
102 for d in "${dirs[@]}"
104 dirs_k["$d"]=1
105 done
107 while read mntdev mntpnt mntype mntopt rest
109 if [ $netdev = 1 ]
110 then
111 if [[ "$mntdev" =~ ^// ]] || [[ "$mntdev" =~ : ]]
112 then
113 dirs_k["$mntpnt"]=1
116 if [ -n "$mntype" ]
117 then
118 if [ "${types_k[$mntype]}" = 1 ]
119 then
120 dirs_k["$mntpnt"]=1
121 else
122 for typepatt in "${types[@]}"
124 typepatt=${typepatt//./\\.}
125 typepatt=${typepatt//\*/.*}
126 typepatt=${typepatt//\?/.}
127 if [[ "$mntype" =~ ^$typepatt$ ]]
128 then
129 dirs_k["$mntpnt"]=1
131 done
134 done </proc/mounts
136 for d in "${!dirs_k[@]}"
138 hidemount "$d"
139 done
142 exec "${cmdargs[@]}"