summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/gui/housekeeping.py
blob: b6c29b474ccac613c99a099f35f84356ea911f2b (plain)
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
61
62
63
64
65
66
67
68
69
70
71
import os
import signal
import time

import psutil

from leap.common.config import get_path_prefix


class NoAuthTokenError(Exception):
    pass


def get_authenticated_url():
    url = "http://localhost:7070"
    path = os.path.join(get_path_prefix(), 'leap', 'authtoken')
    waiting = 20
    while not os.path.isfile(path):
        if waiting == 0:
            # If we arrive here, something really messed up happened,
            # because touching the token file is one of the first
            # things the backend does, and this BrowserWindow
            # should be called *right after* launching the backend.
            raise NoAuthTokenError(
                'No authentication token found!')
        time.sleep(0.1)
        waiting -= 1
    token = open(path).read().strip()
    url += '#' + token
    return url


def terminate(pid):
    if os.path.isfile(pid):
        with open(pid) as f:
            pidno = int(f.read())
        print('[bitmask] terminating bitmaskd...')
        os.kill(pidno, signal.SIGTERM)


def reset_authtoken():
    prev_auth = os.path.join(get_path_prefix(), 'leap', 'authtoken')
    try:
        os.remove(prev_auth)
    except OSError:
        pass


def check_stale_pidfile():

    def is_pid_running(pidno):
        return 1 == len(filter(lambda p: p.pid == int(pidno), psutil.process_iter()))

    pidno = None
    pidfile = os.path.join(get_path_prefix(), 'leap', 'bitmaskd.pid')
    if os.path.isfile(pidfile):
        with open(pidfile, 'r') as pid_fd:
            pidno = pid_fd.readline().strip()
    if pidno and pidno.isdigit():
        if not is_pid_running(pidno):
            os.unlink(pidfile)


def cleanup():
    print('[bitmask] cleaning up files')
    base = os.path.join(get_path_prefix(), 'leap')
    token = os.path.join(base, 'authtoken')
    pid = os.path.join(base, 'bitmaskd.pid')
    for _f in [token, pid]:
        if os.path.isfile(_f):
            os.unlink(_f)