aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/qemuimage-testlib-pythonhelper
blob: 4e6432b0fecd6acd51686f061ad81080cb277b48 (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
#!/usr/bin/env python

import optparse
import subprocess
import sys
import os

parser = optparse.OptionParser(
    usage = """
  %prog [options]
""")

parser.add_option("-q", "--findqemu",
        help = "find a qemu beneath the process <pid>",
        action="store", dest="findqemu")

options, args = parser.parse_args(sys.argv)

if options.findqemu:
    #
    # Walk the process tree from the process specified looking for a qemu-system. Return its pid.
    #
    ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command'], stdout=subprocess.PIPE).communicate()[0]
    processes = ps.split('\n')
    nfields = len(processes[0].split()) - 1
    pids = {}
    commands = {}
    for row in processes[1:]:
        data = row.split(None, nfields)
        if len(data) != 3:
            continue
        if data[1] not in pids:
            pids[data[1]] = []
        pids[data[1]].append(data[0])
        commands[data[0]] = data[2]

    if options.findqemu not in pids:
        sys.stderr.write("No children found matching %s" % options.findqemu)
        sys.exit(1)

    parents = []
    newparents = pids[options.findqemu]
    while newparents:
        next = []
        for p in newparents:
            if p in pids:
                for n in pids[p]:
                    if n not in parents and n not in next:
                        next.append(n)

            if p not in parents:
                parents.append(p)
        newparents = next
    #print "Children matching %s:" % str(parents)
    for p in parents:
        # Need to be careful here since runqemu-internal runs "ldd qemu-system-xxxx"
        basecmd = commands[p].split()[0]
        basecmd = os.path.basename(basecmd)
        if "qemu-system" in basecmd:
            print p
            sys.exit(0)
    sys.exit(1)
else:
    parser.print_help()