1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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("umetnik")
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_gid, 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()
|