Justin Vreeland 794355
#!/usr/bin/python3
Justin Vreeland 794355
#
Justin Vreeland 794355
# check-kabi - Red Hat kABI reference checking tool
Justin Vreeland 794355
#
Justin Vreeland 794355
# We use this script to check against reference Module.kabi files.
Justin Vreeland 794355
#
Justin Vreeland 794355
# Author: Jon Masters <jcm@redhat.com>
Justin Vreeland 794355
# Copyright (C) 2007-2009 Red Hat, Inc.
Justin Vreeland 794355
#
Justin Vreeland 794355
# This software may be freely redistributed under the terms of the GNU
Justin Vreeland 794355
# General Public License (GPL).
Justin Vreeland 794355
Justin Vreeland 794355
# Changelog:
Justin Vreeland 794355
#
Justin Vreeland 794355
# 2018/06/01 - Update for python3 by Petr Oros.
Justin Vreeland 794355
# 2009/08/15 - Updated for use in RHEL6.
Justin Vreeland 794355
# 2007/06/13 - Initial rewrite in python by Jon Masters.
Justin Vreeland 794355
Justin Vreeland 794355
__author__ = "Jon Masters <jcm@redhat.com>"
Justin Vreeland 794355
__version__ = "2.0"
Justin Vreeland 794355
__date__ = "2009/08/15"
Justin Vreeland 794355
__copyright__ = "Copyright (C) 2007-2009 Red Hat, Inc"
Justin Vreeland 794355
__license__ = "GPL"
Justin Vreeland 794355
Justin Vreeland 794355
import getopt
Justin Vreeland 794355
import string
Justin Vreeland 794355
import sys
Justin Vreeland 794355
Justin Vreeland 794355
true = 1
Justin Vreeland 794355
false = 0
Justin Vreeland 794355
Justin Vreeland 794355
Justin Vreeland 794355
def load_symvers(symvers, filename):
Justin Vreeland 794355
    """Load a Module.symvers file."""
Justin Vreeland 794355
Justin Vreeland 794355
    symvers_file = open(filename, "r")
Justin Vreeland 794355
Justin Vreeland 794355
    while true:
Justin Vreeland 794355
        in_line = symvers_file.readline()
Justin Vreeland 794355
        if in_line == "":
Justin Vreeland 794355
            break
Justin Vreeland 794355
        if in_line == "\n":
Justin Vreeland 794355
            continue
Justin Vreeland 794355
        checksum, symbol, directory, type = in_line.split()
Justin Vreeland 794355
Justin Vreeland 794355
        symvers[symbol] = in_line[0:-1]
Justin Vreeland 794355
Justin Vreeland 794355
Justin Vreeland 794355
def load_kabi(kabi, filename):
Justin Vreeland 794355
    """Load a Module.kabi file."""
Justin Vreeland 794355
Justin Vreeland 794355
    kabi_file = open(filename, "r")
Justin Vreeland 794355
Justin Vreeland 794355
    while true:
Justin Vreeland 794355
        in_line = kabi_file.readline()
Justin Vreeland 794355
        if in_line == "":
Justin Vreeland 794355
            break
Justin Vreeland 794355
        if in_line == "\n":
Justin Vreeland 794355
            continue
Justin Vreeland 794355
        checksum, symbol, directory, type = in_line.split()
Justin Vreeland 794355
Justin Vreeland 794355
        kabi[symbol] = in_line[0:-1]
Justin Vreeland 794355
Justin Vreeland 794355
Justin Vreeland 794355
def check_kabi(symvers, kabi):
Justin Vreeland 794355
    """Check Module.kabi and Module.symvers files."""
Justin Vreeland 794355
Justin Vreeland 794355
    fail = 0
Justin Vreeland 794355
    warn = 0
Justin Vreeland 794355
    changed_symbols = []
Justin Vreeland 794355
    moved_symbols = []
Justin Vreeland 794355
Justin Vreeland 794355
    for symbol in kabi:
Justin Vreeland 794355
        abi_hash, abi_sym, abi_dir, abi_type = kabi[symbol].split()
Justin Vreeland 794355
        if symbol in symvers:
Justin Vreeland 794355
            sym_hash, sym_sym, sym_dir, sym_type = symvers[symbol].split()
Justin Vreeland 794355
            if abi_hash != sym_hash:
Justin Vreeland 794355
                fail = 1
Justin Vreeland 794355
                changed_symbols.append(symbol)
Justin Vreeland 794355
Justin Vreeland 794355
            if abi_dir != sym_dir:
Justin Vreeland 794355
                warn = 1
Justin Vreeland 794355
                moved_symbols.append(symbol)
Justin Vreeland 794355
        else:
Justin Vreeland 794355
            fail = 1
Justin Vreeland 794355
            changed_symbols.append(symbol)
Justin Vreeland 794355
Justin Vreeland 794355
    if fail:
Justin Vreeland 794355
        print("*** ERROR - ABI BREAKAGE WAS DETECTED ***")
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
        print("The following symbols have been changed (this will cause an ABI breakage):")
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
        for symbol in changed_symbols:
Justin Vreeland 794355
            print(symbol)
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
Justin Vreeland 794355
    if warn:
Justin Vreeland 794355
        print("*** WARNING - ABI SYMBOLS MOVED ***")
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
        print("The following symbols moved (typically caused by moving a symbol from being")
Justin Vreeland 794355
        print("provided by the kernel vmlinux out to a loadable module):")
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
        for symbol in moved_symbols:
Justin Vreeland 794355
            print(symbol)
Justin Vreeland 794355
        print("")
Justin Vreeland 794355
Justin Vreeland 794355
    """Halt the build, if we got errors and/or warnings. In either case,
Justin Vreeland 794355
       double-checkig is required to avoid introducing / concealing
Justin Vreeland 794355
       KABI inconsistencies."""
Justin Vreeland 794355
    if fail or warn:
Justin Vreeland 794355
        sys.exit(1)
Justin Vreeland 794355
    sys.exit(0)
Justin Vreeland 794355
Justin Vreeland 794355
Justin Vreeland 794355
def usage():
Justin Vreeland 794355
    print("""
Justin Vreeland 794355
check-kabi: check Module.kabi and Module.symvers files.
Justin Vreeland 794355
Justin Vreeland 794355
    check-kabi [ -k Module.kabi ] [ -s Module.symvers ]
Justin Vreeland 794355
Justin Vreeland 794355
""")
Justin Vreeland 794355
Justin Vreeland 794355
Justin Vreeland 794355
if __name__ == "__main__":
Justin Vreeland 794355
Justin Vreeland 794355
    symvers_file = ""
Justin Vreeland 794355
    kabi_file = ""
Justin Vreeland 794355
Justin Vreeland 794355
    opts, args = getopt.getopt(sys.argv[1:], 'hk:s:')
Justin Vreeland 794355
Justin Vreeland 794355
    for o, v in opts:
Justin Vreeland 794355
        if o == "-s":
Justin Vreeland 794355
            symvers_file = v
Justin Vreeland 794355
        if o == "-h":
Justin Vreeland 794355
            usage()
Justin Vreeland 794355
            sys.exit(0)
Justin Vreeland 794355
        if o == "-k":
Justin Vreeland 794355
            kabi_file = v
Justin Vreeland 794355
Justin Vreeland 794355
    if (symvers_file == "") or (kabi_file == ""):
Justin Vreeland 794355
        usage()
Justin Vreeland 794355
        sys.exit(1)
Justin Vreeland 794355
Justin Vreeland 794355
    symvers = {}
Justin Vreeland 794355
    kabi = {}
Justin Vreeland 794355
Justin Vreeland 794355
    load_symvers(symvers, symvers_file)
Justin Vreeland 794355
    load_kabi(kabi, kabi_file)
Justin Vreeland 794355
    check_kabi(symvers, kabi)