| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (C) 2014 Red Hat Inc. |
| 4 |
# |
| 5 |
# This file is part of systemtap, and is free software. You can |
| 6 |
# redistribute it and/or modify it under the terms of the GNU General |
| 7 |
# Public License (GPL); either version 2, or (at your option) any |
| 8 |
# later version. |
| 9 |
|
| 10 |
import sys |
| 11 |
import os |
| 12 |
import os.path |
| 13 |
import time |
| 14 |
import select |
| 15 |
import signal |
| 16 |
from config_opts import config_opts |
| 17 |
|
| 18 |
def run_module(): |
| 19 |
logfile = 'data.log' |
| 20 |
os.system('rm -f ./data.log') |
| 21 |
|
| 22 |
# Insert the module |
| 23 |
print "Inserting module..." |
| 24 |
stap_cmd = [ "staprun", "-o", "data.log", "probe_module.ko" ] |
| 25 |
stap_pid = os.spawnvp(os.P_NOWAIT, stap_cmd[0], stap_cmd) |
| 26 |
print "stap pid is", stap_pid |
| 27 |
|
| 28 |
# Run the load generate commands (if any). Note we ignore the |
| 29 |
# return values, since we don't really care if the commands |
| 30 |
# succeed or not. |
| 31 |
if config_opts.has_key('load_cmds'): |
| 32 |
print "Running commands..." |
| 33 |
|
| 34 |
# Redirect the output to 'run_module.log' by modifying |
| 35 |
# what stdout/stderr points to. |
| 36 |
sys.stdout.flush() |
| 37 |
old_stdout = os.dup(sys.stdout.fileno()) |
| 38 |
sys.stderr.flush() |
| 39 |
old_stderr = os.dup(sys.stderr.fileno()) |
| 40 |
fd = os.open('run_module.log', os.O_CREAT | os.O_WRONLY) |
| 41 |
os.dup2(fd, sys.stdout.fileno()) |
| 42 |
os.dup2(fd, sys.stderr.fileno()) |
| 43 |
|
| 44 |
# Run the commands. |
| 45 |
pid_list = list() |
| 46 |
for cmd in config_opts['load_cmds']: |
| 47 |
pid = os.spawnvp(os.P_NOWAIT, cmd[0], cmd) |
| 48 |
pid_list.append(pid) |
| 49 |
for pid in pid_list: |
| 50 |
(childpid, status) = os.waitpid(pid, 0) |
| 51 |
|
| 52 |
# Restore old value of stdout/stderr. |
| 53 |
os.close(fd) |
| 54 |
os.dup2(old_stdout, sys.stdout.fileno()) |
| 55 |
os.dup2(old_stderr, sys.stderr.fileno()) |
| 56 |
else: |
| 57 |
time.sleep(10) |
| 58 |
|
| 59 |
# Remove the module |
| 60 |
print "Removing module..." |
| 61 |
os.kill(stap_pid, signal.SIGINT) |
| 62 |
(childpid, rc) = os.waitpid(stap_pid, 0) |
| 63 |
#if os.WEXITSTATUS(rc) != 0: |
| 64 |
# print >>sys.stderr, "Error: rmmod failed" |
| 65 |
# return -1 |
| 66 |
|
| 67 |
# Now we have to wait until everything is flushed to the logfile |
| 68 |
print "Looking for output..." |
| 69 |
rc = 0 |
| 70 |
f = open(config_opts['probes_result'], 'w') |
| 71 |
attempts = 0 |
| 72 |
start_pos = 0 |
| 73 |
while 1: |
| 74 |
l = open(logfile, 'r') |
| 75 |
|
| 76 |
# Find the ending size of the logfile |
| 77 |
end_pos = os.path.getsize(logfile) |
| 78 |
|
| 79 |
# Try to wait until data is available |
| 80 |
while 1: |
| 81 |
try: |
| 82 |
input, output, exc = select.select([l.fileno()], [], [], 60) |
| 83 |
break |
| 84 |
except select.error, err: |
| 85 |
if err[0] != EINTR: |
| 86 |
raise |
| 87 |
|
| 88 |
# Get the new stuff logged |
| 89 |
data = l.read(end_pos - start_pos + 1) |
| 90 |
if not data: |
| 91 |
# ignore EOF |
| 92 |
time.sleep(2) |
| 93 |
attempts += 1 |
| 94 |
if attempts < 30: |
| 95 |
l.close() |
| 96 |
continue |
| 97 |
else: |
| 98 |
print >>sys.stderr, "Error: Couldn't find data" |
| 99 |
rc = -1 |
| 100 |
break; |
| 101 |
|
| 102 |
# Write results data |
| 103 |
f.write(data) |
| 104 |
|
| 105 |
# See if we can find 'probe_module unloaded' in the data we |
| 106 |
# just read. |
| 107 |
if data.find('probe_module unloaded') == -1: |
| 108 |
start_pos = end_pos |
| 109 |
else: |
| 110 |
break |
| 111 |
|
| 112 |
l.close() |
| 113 |
f.close() |
| 114 |
return rc |
| 115 |
|
| 116 |
def main(): |
| 117 |
rc = run_module() |
| 118 |
sys.exit(rc) |
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
main() |