diff options
Diffstat (limited to 'prog/jelka/wrapper.py')
-rwxr-xr-x | prog/jelka/wrapper.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/prog/jelka/wrapper.py b/prog/jelka/wrapper.py new file mode 100755 index 0000000..ad2f11d --- /dev/null +++ b/prog/jelka/wrapper.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 +import io +import os +import pwd +import subprocess +import psutil +import mmap +import time +import resource +from jelka_config import luči +from sys import argv +if not os.getenv("DRY_RUN"): + import jelka_hardware + +def izriši(): + if os.getenv("DRY_RUN"): + print("wrapper: ", end="") + for i in range(luči*3): + print(f"{buffer[i]:02x}", end="") + print() + return + for i in range(luči): + jelka_hardware.nastavi(i, (buffer[3*i], buffer[3*i+1], buffer[3*i+2])) + jelka_hardware.izriši() + +def demote(uid, gid, chrootpath): + def result(): + resource.setrlimit(resource.RLIMIT_NPROC, (1, 1)) + os.chroot(chrootpath) + os.setgid(gid) + os.setuid(uid) + return result + +shmf = open("chroot/dev/shm/jelka", mode="w+b") +os.ftruncate(shmf.fileno(), luči*3) +buffer = mmap.mmap(shmf.fileno(), 0) +cwd = "/" +r, w = os.pipe() +args = ["/jelka/vzorci/" + argv[1], str(w)] +pw_record = pwd.getpwnam("z") +os.chown("chroot/dev/shm/jelka", 0, pw_record.pw_gid) +os.chmod("chroot/dev/shm/jelka", 0o660) +env = os.environ.copy() +env["HOME"] = pw_record.pw_dir +env["LOGNAME"] = pw_record.pw_name +env["PWD"] = cwd +env["USER"] = pw_record.pw_name +env["PYTHONPATH"] = "/jelka" +process = subprocess.Popen( + args, preexec_fn=demote(pw_record.pw_uid, pw_record.pw_uid, os.path.abspath("chroot")), cwd=cwd, env=env, pass_fds=[w] +) +try: + while io.FileIO(r, closefd=False).read(1): + izriši() + result = process.wait() + buffer.close() + shmf.close() +except: + print("cleaning up!") + process.kill() |