tests/killdaemons.py
author Simon Heimberg <simohe@besonet.ch>
Wed, 12 Feb 2014 15:38:59 +0100
changeset 20495 0cf1c8c452d3
parent 20494 ea3adeac5248
child 20496 acbd19b9fbe1
permissions -rwxr-xr-x
tests: killdaemons.py for checks reason when getting no process handle
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7344
58fd3c718ca4 tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
#!/usr/bin/env python
58fd3c718ca4 tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
17466
d5a3bda6e170 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 17465
diff changeset
     3
import os, sys, time, errno, signal
7344
58fd3c718ca4 tests: add killdaemons helper script
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
17465
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
     5
if os.name =='nt':
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
     6
    import ctypes
20493
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
     7
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
     8
    def _check(ret, expectederr=None):
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
     9
        if ret == 0:
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    10
            winerrno = ctypes.GetLastError()
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    11
            if winerrno == expectederr:
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    12
                return True
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    13
            raise ctypes.WinError(winerrno)
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    14
17465
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    15
    def kill(pid, logfn, tryhard=True):
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    16
        logfn('# Killing daemon process %d' % pid)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    17
        PROCESS_TERMINATE = 1
20494
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    18
        SYNCHRONIZE = 0x00100000L
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    19
        WAIT_OBJECT_0 = 0
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    20
        WAIT_TIMEOUT = 258
17465
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    21
        handle = ctypes.windll.kernel32.OpenProcess(
20494
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    22
                PROCESS_TERMINATE|SYNCHRONIZE, False, pid)
20493
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    23
        if handle == 0:
20495
0cf1c8c452d3 tests: killdaemons.py for checks reason when getting no process handle
Simon Heimberg <simohe@besonet.ch>
parents: 20494
diff changeset
    24
            _check(0, 87) # err 87 when process not found
20493
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    25
            return # process not found, already finished
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    26
        try:
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    27
            _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5)
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    28
            #      windows error 5 when process does not exist or no access TODO
20494
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    29
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    30
            # TODO?: forcefully kill when timeout
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    31
            #        and ?shorter waiting time? when tryhard==True
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    32
            r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    33
                                                       # timeout = 100 ms
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    34
            if r == WAIT_OBJECT_0:
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    35
                pass # process is terminated
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    36
            elif r == WAIT_TIMEOUT:
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    37
                logfn('# Daemon process %d is stuck')
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    38
            else:
ea3adeac5248 tests: killdaemons.py for windows waits for killed process to terminate
Simon Heimberg <simohe@besonet.ch>
parents: 20493
diff changeset
    39
                check(r) # any error
20493
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    40
        except: #re-raises
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    41
            ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    42
            raise
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    43
        _check(ctypes.windll.kernel32.CloseHandle(handle))
b5f43dbf64ca tests: kill for windows in killdaemons.py checks return values
Simon Heimberg <simohe@besonet.ch>
parents: 17466
diff changeset
    44
17465
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    45
else:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    46
    def kill(pid, logfn, tryhard=True):
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    47
        try:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    48
            os.kill(pid, 0)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    49
            logfn('# Killing daemon process %d' % pid)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    50
            os.kill(pid, signal.SIGTERM)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    51
            if tryhard:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    52
                for i in range(10):
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    53
                    time.sleep(0.05)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    54
                    os.kill(pid, 0)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    55
            else:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    56
                time.sleep(0.1)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    57
                os.kill(pid, 0)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    58
            logfn('# Daemon process %d is stuck - really killing it' % pid)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    59
            os.kill(pid, signal.SIGKILL)
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    60
        except OSError, err:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    61
            if err.errno != errno.ESRCH:
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    62
                raise
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    63
17464
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    64
def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    65
    if not logfn:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    66
        logfn = lambda s: s
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    67
    # Kill off any leftover daemon processes
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    68
    try:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    69
        fp = open(pidfile)
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    70
        for line in fp:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    71
            try:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    72
                pid = int(line)
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    73
            except ValueError:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    74
                continue
17465
2d4a096e213c killdaemons: add windows implementation
Patrick Mezard <patrick@mezard.eu>
parents: 17464
diff changeset
    75
            kill(pid, logfn, tryhard)
17464
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    76
        fp.close()
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    77
        if remove:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    78
            os.unlink(pidfile)
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    79
    except IOError:
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    80
        pass
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    81
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    82
if __name__ == '__main__':
17466
d5a3bda6e170 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 17465
diff changeset
    83
    path, = sys.argv[1:]
d5a3bda6e170 killdaemons: take file argument explicitely
Patrick Mezard <patrick@mezard.eu>
parents: 17465
diff changeset
    84
    killdaemons(path)
17464
eddfb9a550d0 run-tests: do not duplicate killdaemons() code
Patrick Mezard <patrick@mezard.eu>
parents: 10905
diff changeset
    85