tests/killdaemons.py
author Augie Fackler <raf@durin42.com>
Thu, 31 Dec 2015 11:23:19 -0500
changeset 1877 69077c65919d
parent 1839 1bc5e62fc0c7
permissions -rwxr-xr-x
topic: handle merge.update function signature change I think this is backwards compatible with how it used to be called, but I didn't worry about it too much either.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#!/usr/bin/env python
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
import os, sys, time, errno, signal
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
if os.name =='nt':
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
    import ctypes
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
    def _check(ret, expectederr=None):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
        if ret == 0:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
            winerrno = ctypes.GetLastError()
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
            if winerrno == expectederr:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
                return True
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
            raise ctypes.WinError(winerrno)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
    def kill(pid, logfn, tryhard=True):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
        logfn('# Killing daemon process %d' % pid)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
        PROCESS_TERMINATE = 1
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
        PROCESS_QUERY_INFORMATION = 0x400
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
        SYNCHRONIZE = 0x00100000
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
        WAIT_OBJECT_0 = 0
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
        WAIT_TIMEOUT = 258
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
        handle = ctypes.windll.kernel32.OpenProcess(
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
                PROCESS_TERMINATE|SYNCHRONIZE|PROCESS_QUERY_INFORMATION,
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
                False, pid)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
        if handle == 0:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
            _check(0, 87) # err 87 when process not found
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
            return # process not found, already finished
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
        try:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
            r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
            if r == WAIT_OBJECT_0:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
                pass # terminated, but process handle still available
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
            elif r == WAIT_TIMEOUT:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
                _check(ctypes.windll.kernel32.TerminateProcess(handle, -1))
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
            else:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
                _check(r)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
            # TODO?: forcefully kill when timeout
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
            #        and ?shorter waiting time? when tryhard==True
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
            r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
                                                       # timeout = 100 ms
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
            if r == WAIT_OBJECT_0:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
                pass # process is terminated
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
            elif r == WAIT_TIMEOUT:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
                logfn('# Daemon process %d is stuck')
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
            else:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
                _check(r) # any error
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
        except: #re-raises
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
            ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
            raise
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
        _check(ctypes.windll.kernel32.CloseHandle(handle))
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
else:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
    def kill(pid, logfn, tryhard=True):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
        try:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
            os.kill(pid, 0)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
            logfn('# Killing daemon process %d' % pid)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
            os.kill(pid, signal.SIGTERM)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
            if tryhard:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
                for i in range(10):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
                    time.sleep(0.05)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
                    os.kill(pid, 0)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
            else:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
                time.sleep(0.1)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
                os.kill(pid, 0)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
            logfn('# Daemon process %d is stuck - really killing it' % pid)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
            os.kill(pid, signal.SIGKILL)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
        except OSError as err:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
            if err.errno != errno.ESRCH:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
                raise
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
    if not logfn:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
        logfn = lambda s: s
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    # Kill off any leftover daemon processes
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
    try:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
        fp = open(pidfile)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
        for line in fp:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
            try:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
                pid = int(line)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
            except ValueError:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    81
                continue
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
            kill(pid, logfn, tryhard)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
        fp.close()
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
        if remove:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    85
            os.unlink(pidfile)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
    except IOError:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    87
        pass
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
if __name__ == '__main__':
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
    path, = sys.argv[1:]
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
    killdaemons(path)