Skip to content

Commit 877f2f0

Browse files
committed
tests: support special block devices
Hard-linking from /dev to a jailed directory doesn't work. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent b88f89e commit 877f2f0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

tests/framework/jailer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
import shutil
7+
import stat
78

89
from subprocess import run, PIPE
910

@@ -119,19 +120,24 @@ def chroot_path(self):
119120
return os.path.join(self.chroot_base_with_id(), 'root')
120121

121122
def jailed_path(self, file_path, create=False, create_jail=False):
122-
"""Create a hard link owned by uid:gid.
123+
"""Create a hard link or block special device owned by uid:gid.
123124
124-
Create a hard link to the specified file, changes the owner to
125-
uid:gid, and returns a path to the link which is valid within the jail.
125+
Create a hard link or block special device from the specified file, changes the owner to
126+
uid:gid, and returns a path to the file which is valid within the jail.
126127
"""
127128
file_name = os.path.basename(file_path)
128129
global_p = os.path.join(self.chroot_path(), file_name)
129130
if create_jail:
130131
os.makedirs(self.chroot_path(), exist_ok=True)
131132
jailed_p = os.path.join("/", file_name)
132133
if create:
133-
cmd = 'ln -f {} {}'.format(file_path, global_p)
134-
run(cmd, shell=True, check=True)
134+
stat_result = os.stat(file_path)
135+
if stat.S_ISBLK(stat_result.st_mode):
136+
cmd = ['mknod', global_p, 'b', str(os.major(stat_result.st_rdev)), str(os.minor(stat_result.st_rdev))]
137+
run(cmd, check=True)
138+
else:
139+
cmd = 'ln -f {} {}'.format(file_path, global_p)
140+
run(cmd, shell=True, check=True)
135141
cmd = 'chown {}:{} {}'.format(self.uid, self.gid, global_p)
136142
run(cmd, shell=True, check=True)
137143
return jailed_p

0 commit comments

Comments
 (0)