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
bde051
        checksum, symbol, directory, type, *ns = in_line.split()
bde051
        ns = ns[0] if ns else None
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
bde051
        checksum, symbol, directory, type, *ns = in_line.split()
bde051
        ns = ns[0] if ns else None
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 = []
bde051
    ns_symbols = []
Justin Vreeland 794355
Justin Vreeland 794355
    for symbol in kabi:
bde051
        abi_hash, abi_sym, abi_dir, abi_type, *abi_ns = kabi[symbol].split()
bde051
        abi_ns = abi_ns[0] if abi_ns else None
Justin Vreeland 794355
        if symbol in symvers:
bde051
            sym_hash, sym_sym, sym_dir, sym_type, *sym_ns = symvers[symbol].split()
bde051
            sym_ns = sym_ns[0] if sym_ns else None
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)
bde051
bde051
            if abi_ns != sym_ns:
bde051
                warn = 1
bde051
                ns_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 ***")
bde051
        if moved_symbols:
bde051
            print("")
bde051
            print("The following symbols moved (typically caused by moving a symbol from being")
bde051
            print("provided by the kernel vmlinux out to a loadable module):")
bde051
            print("")
bde051
            for symbol in moved_symbols:
bde051
                print(symbol)
bde051
            print("")
bde051
        if ns_symbols:
bde051
            print("")
bde051
            print("The following symbols changed symbol namespaces:")
bde051
            print("")
bde051
            for symbol in ns_symbols:
bde051
                print(symbol)
bde051
            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)