| 1 |
#! /usr/bin/python3 |
| 2 |
# binfmt_misc filesystem emulator using FUSE. |
| 3 |
|
| 4 |
# Copyright (C) 2010 Colin Watson. |
| 5 |
# |
| 6 |
# This file is part of binfmt-support. |
| 7 |
# |
| 8 |
# binfmt-support is free software; you can redistribute it and/or modify it |
| 9 |
# under the terms of the GNU General Public License as published by the |
| 10 |
# Free Software Foundation; either version 3 of the License, or (at your |
| 11 |
# option) any later version. |
| 12 |
# |
| 13 |
# binfmt-support is distributed in the hope that it will be useful, but |
| 14 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 16 |
# Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License along |
| 19 |
# with binfmt-support; if not, write to the Free Software Foundation, Inc., |
| 20 |
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
|
| 22 |
import errno |
| 23 |
import os |
| 24 |
import stat |
| 25 |
|
| 26 |
import fuse |
| 27 |
|
| 28 |
fuse.fuse_python_api = (0, 2) |
| 29 |
|
| 30 |
# We only need to emulate a small number of operations here: |
| 31 |
# |
| 32 |
# * echo 1 >status |
| 33 |
# * echo "$regstring" >register |
| 34 |
# * test -e "$entry" |
| 35 |
# * cat "$entry" |
| 36 |
# * printf %d -1 >"$entry" |
| 37 |
# |
| 38 |
# Anything else is gravy. |
| 39 |
|
| 40 |
|
| 41 |
class BinfmtMisc(fuse.Fuse): |
| 42 |
def __init__(self, *args, **kwargs): |
| 43 |
fuse.Fuse.__init__(self, *args, **kwargs) |
| 44 |
self.enabled = False |
| 45 |
self.names = {} |
| 46 |
|
| 47 |
def create_entry(self, path, fh=None): |
| 48 |
def format_arg(s): |
| 49 |
return "".join(["%02x" % ord(c) for c in s]) |
| 50 |
|
| 51 |
if not path.startswith("/") or path[1:] not in self.names: |
| 52 |
raise ValueError |
| 53 |
|
| 54 |
fields = self.names[path[1:]] |
| 55 |
lines = ["enabled"] |
| 56 |
lines.append("interpreter %s" % fields[4]) |
| 57 |
lines.append("flags: ") # TODO |
| 58 |
if fields[0] == "E": |
| 59 |
lines.append("extension .%s" % fields[2]) |
| 60 |
else: |
| 61 |
lines.append("offset %d" % fields[1]) |
| 62 |
lines.append("magic %s" % format_arg(fields[2])) |
| 63 |
if fields[3]: |
| 64 |
lines.append("mask %s" % format_arg(fields[3])) |
| 65 |
lines.append("") |
| 66 |
return "\n".join(lines).encode("ASCII") |
| 67 |
|
| 68 |
def getattr(self, path): |
| 69 |
st = fuse.Stat() |
| 70 |
st.st_ino = 0 |
| 71 |
st.st_dev = 0 |
| 72 |
st.st_blksize = 0 |
| 73 |
st.st_rdev = 0 |
| 74 |
st.st_uid = 0 |
| 75 |
st.st_gid = 0 |
| 76 |
st.st_size = 0 |
| 77 |
st.st_blocks = 0 |
| 78 |
st.st_atime = 0 |
| 79 |
st.st_mtime = 0 |
| 80 |
st.st_ctime = 0 |
| 81 |
|
| 82 |
if path == "/": |
| 83 |
st.st_mode = stat.S_IFDIR | 0o755 |
| 84 |
st.st_nlink = 2 |
| 85 |
return st |
| 86 |
else: |
| 87 |
st.st_mode = stat.S_IFREG | 0o644 |
| 88 |
st.st_nlink = 1 |
| 89 |
|
| 90 |
if path == "/register": |
| 91 |
st.st_mode = (st.st_mode & ~0o777) | 0o200 |
| 92 |
return st |
| 93 |
elif path == "/status": |
| 94 |
return st |
| 95 |
else: |
| 96 |
try: |
| 97 |
st.st_size = len(self.create_entry(path)) |
| 98 |
return st |
| 99 |
except ValueError: |
| 100 |
return -errno.ENOENT |
| 101 |
|
| 102 |
def readdir(self, path, offset): |
| 103 |
if path != "/": |
| 104 |
raise OSError(errno.ENOENT, "readdir only works on /", path) |
| 105 |
|
| 106 |
dirents = [".", ".."] |
| 107 |
dirents.extend(list(self.names)) |
| 108 |
dirents.extend(("register", "status")) |
| 109 |
for dirent in dirents: |
| 110 |
yield fuse.Direntry(dirent) |
| 111 |
|
| 112 |
def readlink(self, path): |
| 113 |
return -errno.EINVAL |
| 114 |
|
| 115 |
def truncate(self, path, offset): |
| 116 |
pass |
| 117 |
|
| 118 |
def open(self, path, flags): |
| 119 |
if not isinstance(self.getattr(path), fuse.Stat): |
| 120 |
return -errno.ENOENT |
| 121 |
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR |
| 122 |
if path == "/register" and (flags & accmode) != os.O_WRONLY: |
| 123 |
return -errno.EACCES |
| 124 |
|
| 125 |
def read(self, path, length, offset, fh=None): |
| 126 |
if path == "/status" or path == "/register": |
| 127 |
return -errno.EINVAL |
| 128 |
else: |
| 129 |
try: |
| 130 |
return self.create_entry(path)[offset : offset + length] |
| 131 |
except ValueError: |
| 132 |
return -errno.ENOENT |
| 133 |
|
| 134 |
def write(self, path, buf, offset, fh=None): |
| 135 |
def parse_arg(s): |
| 136 |
# left out of Python for some bizarre reason |
| 137 |
def isxdigit(c): |
| 138 |
return c.isdigit() or c.lower() in "abcdef" |
| 139 |
|
| 140 |
out = "" |
| 141 |
end = len(s) |
| 142 |
i = 0 |
| 143 |
while i < end: |
| 144 |
if i + 1 < end and s[i] == "\\" and s[i + 1] == "x": |
| 145 |
if ( |
| 146 |
i + 3 < end |
| 147 |
and isxdigit(s[i + 2]) |
| 148 |
and isxdigit(s[i + 3]) |
| 149 |
): |
| 150 |
out += chr(int(s[i + 2 : i + 4], 16)) |
| 151 |
i += 4 |
| 152 |
else: |
| 153 |
raise ValueError |
| 154 |
else: |
| 155 |
out += s[i] |
| 156 |
i += 1 |
| 157 |
return out |
| 158 |
|
| 159 |
try: |
| 160 |
buf = buf.decode("ASCII") |
| 161 |
except UnicodeError: |
| 162 |
return -errno.EINVAL |
| 163 |
|
| 164 |
if path == "/status": |
| 165 |
if buf.rstrip("\n") == "1": |
| 166 |
self.enabled = True |
| 167 |
else: |
| 168 |
self.enabled = False |
| 169 |
elif path == "/register": |
| 170 |
if not buf or len(buf) < 11 or len(buf) > 256: |
| 171 |
return -errno.EINVAL |
| 172 |
fields = buf.rstrip("\n").split(buf[0])[1:] |
| 173 |
if len(fields) < 6: |
| 174 |
return -errno.EINVAL |
| 175 |
name = fields.pop(0) |
| 176 |
if name in self.names: |
| 177 |
return -errno.EEXIST |
| 178 |
if not name or name == "." or name == ".." or "/" in name: |
| 179 |
return -errno.EINVAL |
| 180 |
if fields[0] != "E" and fields[0] != "M": |
| 181 |
return -errno.EINVAL |
| 182 |
if not fields[2]: |
| 183 |
return -errno.EINVAL |
| 184 |
if fields[0] == "M": |
| 185 |
try: |
| 186 |
fields[1] = int(fields[1]) |
| 187 |
fields[2] = parse_arg(fields[2]) |
| 188 |
fields[3] = parse_arg(fields[3]) |
| 189 |
if fields[3] and len(fields[2]) != len(fields[3]): |
| 190 |
return -errno.EINVAL |
| 191 |
except ValueError: |
| 192 |
return -errno.EINVAL |
| 193 |
else: |
| 194 |
if "/" in fields[2]: |
| 195 |
return -errno.EINVAL |
| 196 |
if not fields[4]: |
| 197 |
return -errno.EINVAL |
| 198 |
# TODO do something with flags |
| 199 |
self.names[name] = fields |
| 200 |
elif path.startswith("/") and path[1:] in self.names: |
| 201 |
if buf.rstrip("\n") == "-1": |
| 202 |
del self.names[path[1:]] |
| 203 |
else: |
| 204 |
return -errno.EINVAL |
| 205 |
else: |
| 206 |
return -errno.ENOENT |
| 207 |
return len(buf) |
| 208 |
|
| 209 |
def flush(self, path): |
| 210 |
pass |
| 211 |
|
| 212 |
|
| 213 |
def main(): |
| 214 |
usage = "Incomplete binfmt_misc emulator\n\n%s" % fuse.Fuse.fusage |
| 215 |
server = BinfmtMisc( |
| 216 |
version="%prog " + fuse.__version__, usage=usage, dash_s_do="setsingle" |
| 217 |
) |
| 218 |
server.multithreaded = False |
| 219 |
server.parse(errex=2) |
| 220 |
server.main() |
| 221 |
|
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
main() |