error handling cleanup

This commit is contained in:
erpalma
2018-12-27 13:58:16 +01:00
parent c7e292e282
commit d2361431cb

View File

@@ -76,14 +76,18 @@ ERR = bcolors.RED + bcolors.BOLD + 'ERR' + bcolors.RESET
LIM = bcolors.YELLOW + bcolors.BOLD + 'LIM' + bcolors.RESET
def fatal(msg, code=1):
print(msg)
sys.exit(code)
def writemsr(msr, val):
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
if not os.path.exists(msr_list[0]):
try:
subprocess.check_call(('modprobe', 'msr'))
except subprocess.CalledProcessError:
print('[E] Unable to load the msr module.')
sys.exit(1)
fatal('[E] Unable to load the msr module.')
try:
for addr in msr_list:
f = os.open(addr, os.O_WRONLY)
@@ -92,11 +96,10 @@ def writemsr(msr, val):
os.close(f)
except (IOError, OSError) as e:
if e.errno == EPERM or e.errno == EACCES:
print(
fatal(
'[E] Unable to write to MSR. Try to disable Secure Boot '
'and check if your kernel does not restrict access to MSR.'
)
sys.exit(1)
else:
raise e
@@ -105,15 +108,13 @@ def writemsr(msr, val):
def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
assert cpu is None or cpu in range(cpu_count())
if from_bit > to_bit:
print('[E] Wrong readmsr bit params')
sys.exit(1)
fatal('[E] Wrong readmsr bit params')
msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
if not os.path.exists(msr_list[0]):
try:
subprocess.check_call(('modprobe', 'msr'))
except subprocess.CalledProcessError:
print('[E] Unable to load the msr module.')
sys.exit(1)
fatal('[E] Unable to load the msr module.')
try:
output = []
for addr in msr_list:
@@ -127,8 +128,7 @@ def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
return output[cpu] if cpu is not None else output
except (IOError, OSError) as e:
if e.errno == EPERM or e.errno == EACCES:
print('[E] Unable to read from MSR. Try to disable Secure Boot.')
sys.exit(1)
fatal('[E] Unable to read from MSR. Try to disable Secure Boot.')
else:
raise e
@@ -160,8 +160,7 @@ def is_on_battery(config):
return not bool(int(f.read()))
except:
pass
print('[E] No valid Sysfs_Power_Path found!')
sys.exit(1)
fatal('[E] No valid Sysfs_Power_Path found!')
def get_cpu_platform_info():
@@ -272,8 +271,7 @@ def load_config():
if value is not None:
value = config.set(power_source, option, str(max(0.1, value)))
elif option == 'Update_Rate_s':
print('[E] The mandatory "Update_Rate_s" parameter is missing.')
sys.exit(1)
fatal('[E] The mandatory "Update_Rate_s" parameter is missing.')
trip_temp = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
if trip_temp is not None:
@@ -406,8 +404,7 @@ def power_thread(config, regs, exit_event):
try:
mchbar_mmio = MMIO(0xFED159A0, 8)
except MMIOError:
print('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
sys.exit(1)
fatal('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
while not exit_event.is_set():
# print thermal status
@@ -485,8 +482,7 @@ def power_thread(config, regs, exit_event):
def check_kernel():
if os.geteuid() != 0:
print('[E] No root no party. Try again with sudo.')
sys.exit(1)
fatal('[E] No root no party. Try again with sudo.')
kernel_config = None
try:
@@ -504,11 +500,10 @@ def check_kernel():
if kernel_config is None:
print('[W] Unable to obtain and validate kernel config.')
elif not re.search('CONFIG_DEVMEM=y', kernel_config):
print('[E] Bad kernel config: you need CONFIG_DEVMEM=y.')
sys.exit(1)
fatal('[E] Bad kernel config: you need CONFIG_DEVMEM=y.')
elif not re.search('CONFIG_X86_MSR=(y|m)', kernel_config):
print('[E] Bad kernel config: you need CONFIG_X86_MSR builtin or as module.')
sys.exit(1)
fatal('[E] Bad kernel config: you need CONFIG_X86_MSR builtin or as module.')
def monitor(exit_event, wait):