aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/fjdisplay.py
blob: 3a8c9c2faa003d8a88fc3adbaf6a9f819379abff (plain) (blame)
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
#!/usr/bin/env python3
# This file is part of Firejail project
# Copyright (C) 2014-2024 Firejail Authors
# License GPL v2

import re
import sys
import subprocess

usage = """fjdisplay.py name-of-firejail
returns the display in the form of ':NNN'
"""


def getfirejails():
    output = subprocess.check_output(['firemon', '--x11'])
    firejails = {}
    name = ''
    for line in output.split('\n'):
        namematch = re.search('--name=(\w+\S*)', line)
        if namematch:
            name = namematch.group(1)
        displaymatch = re.search('DISPLAY (:\d+)', line)
        if displaymatch:
            firejails[name] = displaymatch.group(1)
    return firejails


def getdisplay(name):
    firejails = getfirejails()
    fjlist = '\n'.join(firejails.keys())
    namere = re.compile('^' + name + '.*', re.MULTILINE)
    matchingjails = namere.findall(fjlist)
    if len(matchingjails) == 1:
        return firejails[matchingjails[0]]
    if len(matchingjails) == 0:
        raise NameError("firejail {} does not exist".format(name))
    else:
        raise NameError("ambiguous firejail name")


if __name__ == '__main__':
    if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) > 2:
        print(usage)
        exit()
    if len(sys.argv) == 1:
        print(getfirejails())
    if len(sys.argv) == 2:
        print(getdisplay(sys.argv[1]))