Blame CommonInstall/util_blacklist.pl

Packit 857059
#!/usr/bin/perl
Packit 857059
# BEGIN_ICS_COPYRIGHT8 ****************************************
Packit Service 3f7ca0
#
Packit Service 3f7ca0
# Copyright (c) 2015-2020, Intel Corporation
Packit Service 3f7ca0
#
Packit 857059
# Redistribution and use in source and binary forms, with or without
Packit 857059
# modification, are permitted provided that the following conditions are met:
Packit Service 3f7ca0
#
Packit 857059
#     * Redistributions of source code must retain the above copyright notice,
Packit 857059
#       this list of conditions and the following disclaimer.
Packit 857059
#     * Redistributions in binary form must reproduce the above copyright
Packit 857059
#       notice, this list of conditions and the following disclaimer in the
Packit 857059
#       documentation and/or other materials provided with the distribution.
Packit 857059
#     * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
#       may be used to endorse or promote products derived from this software
Packit 857059
#       without specific prior written permission.
Packit Service 3f7ca0
#
Packit 857059
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 3f7ca0
#
Packit 857059
# END_ICS_COPYRIGHT8   ****************************************
Packit 857059
Packit 857059
# [ICS VERSION STRING: unknown]
Packit 857059
use strict;
Packit 857059
#use Term::ANSIColor;
Packit 857059
#use Term::ANSIColor qw(:constants);
Packit 857059
#use File::Basename;
Packit 857059
#use Math::BigInt;
Packit 857059
Packit 857059
# =============================================================================
Packit 857059
# The functions and constants below assist in editing hotplug and blacklists
Packit 857059
# to prevent autoloading of drivers prior to startup scripts
Packit 857059
Packit 857059
my $HOTPLUG_HARDWARE_DIR = "/etc/sysconfig/hardware";
Packit 857059
Packit 857059
# if not existing file on OS, use this location and create empty file
Packit 857059
# code in add_blacklist depends on pre-existance of file
Packit 857059
my $HOTPLUG_BLACKLIST_FILE = "/etc/modprobe.d/opa-blacklist.conf";
Packit 857059
my $HOTPLUG_BLACKLIST_PREFIX = "blacklist";
Packit 857059
my $HOTPLUG_BLACKLIST_SPACE = " ";
Packit 857059
my $HOTPLUG_BLACKLIST_SPACE_WILDCARD = "[ 	]*";
Packit 857059
Packit 857059
# check if the module is blacklisted
Packit 857059
sub is_blacklisted($)
Packit 857059
{
Packit 857059
	my $module = shift();
Packit 857059
Packit 857059
	my $file = "${HOTPLUG_BLACKLIST_FILE}";
Packit 857059
	my $found;
Packit 857059
Packit 857059
	if (! -f "$file")
Packit 857059
	{
Packit 857059
		return 0;
Packit 857059
	}
Packit 857059
Packit 857059
	return ! system("grep -q $module $file");
Packit 857059
}
Packit 857059
Packit 857059
# add to list to prevent automatic hotplug of driver
Packit 857059
sub add_blacklist($)
Packit 857059
{
Packit 857059
	my $module = shift();
Packit 857059
Packit 857059
	my $file = "${HOTPLUG_BLACKLIST_FILE}";
Packit 857059
	my $found;
Packit 857059
Packit 857059
	if (! -e "$file")
Packit 857059
	{
Packit 857059
		system("touch $file"); # Blacklist file missing, let's create it.
Packit 857059
	} elsif (! -f "$file") {
Packit 857059
		return; # Blacklist file exists but is not regular file, abort.
Packit 857059
	}
Packit 857059
	open (INPUT, "$file");
Packit 857059
	open (OUTPUT, ">>$TMP_CONF");
Packit 857059
Packit 857059
	select (OUTPUT);
Packit 857059
	while (($_=<INPUT>))
Packit 857059
	{
Packit 857059
		if (/^${HOTPLUG_BLACKLIST_PREFIX}${HOTPLUG_BLACKLIST_SPACE_WILDCARD}$module[ 	]*$/)
Packit 857059
		{
Packit 857059
			$found++;
Packit 857059
		} 
Packit 857059
		print $_;
Packit 857059
	}
Packit 857059
	if (!defined($found))
Packit 857059
	{
Packit 857059
		print "${HOTPLUG_BLACKLIST_PREFIX}${HOTPLUG_BLACKLIST_SPACE}$module\n";
Packit 857059
	}
Packit 857059
	select(STDOUT);
Packit 857059
Packit 857059
	close (INPUT);
Packit 857059
	close (OUTPUT);
Packit 857059
	system "mv $TMP_CONF $file";
Packit 857059
Packit 857059
	# also remove any hotplug config files which load the driver
Packit 857059
	open hwconfig, "ls /$HOTPLUG_HARDWARE_DIR/hwcfg* 2>/dev/null |"
Packit 857059
			|| Abort "Unable to open pipe\n";
Packit 857059
	while (<hwconfig>) {
Packit 857059
		chop;
Packit 857059
		if ( ! system("grep -q \"MODULE.*'$module'\" $_ 2>/dev/null") ) {
Packit 857059
			system "rm -f $_"
Packit 857059
		}
Packit 857059
	}
Packit 857059
	close hwconfig;
Packit 857059
}
Packit 857059
Packit 857059
sub remove_blacklist($)
Packit 857059
{
Packit 857059
	my($module) = shift();
Packit 857059
Packit 857059
	my($file) = "${HOTPLUG_BLACKLIST_FILE}";
Packit 857059
	if (! -f "$file")
Packit 857059
	{
Packit 857059
		return;
Packit 857059
	}
Packit 857059
	open (INPUT, "$file");
Packit 857059
	open (OUTPUT, ">>$TMP_CONF");
Packit 857059
Packit 857059
	select (OUTPUT);
Packit 857059
	while (($_=<INPUT>))
Packit 857059
	{
Packit 857059
		if (/^${HOTPLUG_BLACKLIST_PREFIX}${HOTPLUG_BLACKLIST_SPACE_WILDCARD}$module[ 	]*$/)
Packit 857059
		{
Packit 857059
			next;
Packit 857059
		} else {
Packit 857059
			print $_;
Packit 857059
		}
Packit 857059
	}
Packit 857059
	select(STDOUT);
Packit 857059
Packit 857059
	close (INPUT);
Packit 857059
	close (OUTPUT);
Packit 857059
	system "mv $TMP_CONF $file";
Packit 857059
	if ( -z "$file" ) {
Packit 857059
	   system("rm -f $file"); # blacklist file is empty, remove it.
Packit 857059
	}
Packit 857059
}
Packit 857059