
from __future__ import absolute_import
from __future__ import division

import errno

from vdsm.common import cpuarch
from . import expose


@expose
def getHardwareInfo(*args, **kwargs):
    arch = cpuarch.real()
    if cpuarch.is_x86(arch):
        from vdsm.dmidecodeUtil import getHardwareInfoStructure
        return getHardwareInfoStructure()
    elif cpuarch.is_ppc(arch):
        from vdsm.ppc64HardwareInfo import getHardwareInfoStructure
        return getHardwareInfoStructure()
    else:
        return {}


def read_debugfs_property(path):
    try:
        with open(path) as f:
            return int(f.read().strip())
    except IOError as e:
        if e.errno == errno.ENOENT:
            return -1


@expose
def get_pti(*args, **kwargs):
    return read_debugfs_property('/sys/kernel/debug/x86/pti_enabled')


@expose
def get_retp(*args, **kwargs):
    return read_debugfs_property('/sys/kernel/debug/x86/retp_enabled')


@expose
def get_ibrs(*args, **kwargs):
    return read_debugfs_property('/sys/kernel/debug/x86/ibrs_enabled')


@expose
def get_ssbd(*args, **kwargs):
    return read_debugfs_property('/sys/kernel/debug/x86/ssbd_enabled')
