2021-06-27 16:46:29 +02:00
|
|
|
import strformat
|
|
|
|
import posix
|
|
|
|
import bwrap
|
2021-06-19 16:33:47 +02:00
|
|
|
import args
|
2021-06-27 16:46:29 +02:00
|
|
|
import os
|
2021-06-19 16:33:47 +02:00
|
|
|
|
|
|
|
const APP_NAME = "bwsandbox"
|
|
|
|
|
|
|
|
proc getDataDir*(): string =
|
|
|
|
getEnv("XDG_DATA_DIR", getHomeDir().joinPath(".local/share"))
|
|
|
|
|
|
|
|
proc checkRelativePath*(p: string): string =
|
|
|
|
if p[0] == '/':
|
|
|
|
return p
|
|
|
|
getHomeDir().joinPath(p)
|
|
|
|
|
|
|
|
proc getProfilePath*(profile: string): string =
|
2021-10-16 12:58:11 +02:00
|
|
|
let pid = getCurrentProcessId()
|
|
|
|
|
|
|
|
for path in [
|
|
|
|
getConfigDir().joinPath(APP_NAME),
|
|
|
|
&"/usr/share/{APP_NAME}",
|
|
|
|
parentDir(expandSymlink(&"/proc/{pid}/exe")).joinPath("configs")
|
|
|
|
]:
|
|
|
|
let file = path.joinPath(profile)
|
|
|
|
|
|
|
|
if fileExists(file):
|
|
|
|
return file
|
|
|
|
|
|
|
|
raise newException(IOError, "Profile not found")
|
2021-06-19 16:33:47 +02:00
|
|
|
|
2021-06-20 14:09:30 +02:00
|
|
|
proc getProfilePath*(args: Args): string =
|
|
|
|
getProfilePath(args.getProfile())
|
2021-06-19 16:33:47 +02:00
|
|
|
|
|
|
|
proc getSandboxPath*(name: string): string =
|
|
|
|
getDataDir()
|
|
|
|
.joinPath(APP_NAME)
|
2021-06-27 16:46:29 +02:00
|
|
|
.joinPath(name)
|
|
|
|
|
|
|
|
proc deviceExists(path: string): bool =
|
|
|
|
var res: Stat
|
|
|
|
return stat(path, res) >= 0 and S_ISCHR(res.st_mode)
|
|
|
|
|
2021-12-27 16:39:18 +01:00
|
|
|
proc mountDriFolder(call: var BwrapCall, path: string) =
|
|
|
|
for file in walkPattern(&"{path}/*"):
|
|
|
|
if dirExists(file):
|
|
|
|
mountDriFolder(call, file)
|
|
|
|
elif deviceExists(file):
|
|
|
|
call.addMount("--dev-bind", file)
|
|
|
|
#else:
|
|
|
|
# call.addMount("--ro-bin", file)
|
|
|
|
|
2021-06-27 16:46:29 +02:00
|
|
|
# https://github.com/flatpak/flatpak/blob/1bdbb80ac57df437e46fce2cdd63e4ff7704718b/common/flatpak-run.c#L1496
|
|
|
|
proc enableDri*(call: var BwrapCall) =
|
2021-12-27 16:39:18 +01:00
|
|
|
const folder = "/dev/dri"
|
2021-06-27 16:46:29 +02:00
|
|
|
const mounts = [
|
2021-12-27 16:39:18 +01:00
|
|
|
folder, # general
|
2021-06-27 16:46:29 +02:00
|
|
|
"/dev/mali", "/dev/mali0", "/dev/umplock", # mali
|
|
|
|
"/dev/nvidiactl", "/dev/nvidia-modeset", # nvidia
|
|
|
|
"/dev/nvidia-uvm", "/dev/nvidia-uvm-tools" # nvidia OpenCl/CUDA
|
|
|
|
]
|
|
|
|
|
2021-12-27 16:39:18 +01:00
|
|
|
if dirExists(folder):
|
|
|
|
mountDriFolder(call, folder)
|
|
|
|
|
2021-06-27 16:46:29 +02:00
|
|
|
for mount in mounts:
|
2021-12-27 16:39:18 +01:00
|
|
|
if deviceExists(mount) or dirExists(mount):
|
2021-06-27 16:46:29 +02:00
|
|
|
call.addMount("--dev-bind", mount)
|
|
|
|
|
|
|
|
for i in 0..20:
|
|
|
|
let device = &"/dev/nvidia{i}"
|
|
|
|
|
|
|
|
if deviceExists(device):
|
|
|
|
call.addMount("--dev-bind", device)
|