[Pm-utils] [PATCH] unload modules recursively

Stefan Seyfried seife at suse.de
Fri Oct 20 04:38:49 PDT 2006


On Wed, Oct 18, 2006 at 11:53:19PM +0200, Holger Macht wrote:
> Hi,
> 
> any comments on this? I think it's some kind of mandatory for us to have
> such a module unload logic.

I agree :-)

This is an improved version of that patch (modprobe -r makes sure that e.g.
the remove hooks from modprobe.conf are run before the module is removed).

Index: pm/functions
===================================================================
RCS file: /cvs/pm-utils/pm-utils/pm/functions,v
retrieving revision 1.22
diff -u -p -r1.22 functions
--- pm/functions	11 Oct 2006 22:49:27 -0000	1.22
+++ pm/functions	20 Oct 2006 11:34:15 -0000
@@ -120,13 +120,42 @@ pm_main()
 	return 0
 }
 
+# this recursively unloads the given modules and all that depend on it
+# first parameter is the module to be unloaded
 modunload()
 {
-	lsmod 2>/dev/null | grep -q "$1"
-	if [ "$?" == "0" ]; then
-		echo "export ${1}_MODULE_LOAD=yes" >> /var/run/pm-suspend
-		modprobe -r "$1" >/dev/null 2>&1
-	fi
+	local MOD D C USED MODS I
+	local UNL=$1 RET=1
+	# RET is the return code. If at least one module was unloaded, or the
+	#     module was not loaded at all, return 0.
+	#     if no module was unloaded successfully, return 1
+	while read MOD D C USED D; do
+		[ "$MOD" != "$UNL" ] && continue
+		if [ "$USED" == "-" ]; then
+			if modprobe -r $UNL; then
+				echo "export RESUME_MODULES=\"$UNL \$RESUME_MODULES\"" \
+					>> /var/run/pm-suspend
+				RET=0
+			else
+				echo "## could not unload '$UNL', usage count was $C" \
+					>> /var/run/pm-suspend
+			fi
+		else
+			USED=${USED//,/ }
+			MODS=($USED)
+			# it seems slightly more likely to rmmod in one pass, if we try backwards.
+			for I in `seq $[${#MODS[@]}-1] -1 0`; do
+				MOD=${MODS[$I]}
+				modunload $MOD && RET=0
+			done
+			# if we unloaded at least one module, then let's try again!
+			[ $RET -eq 0 ] && modunload $UNL
+			RET=$?
+		fi
+		return $RET
+	done < /proc/modules
+	# if we came this far, there was nothing to do, the module is no longer loaded.
+	return 0
 }
 
 modreload()
Index: pm/hooks/50modules
===================================================================
RCS file: /cvs/pm-utils/pm-utils/pm/hooks/50modules,v
retrieving revision 1.5
diff -u -p -r1.5 50modules
--- pm/hooks/50modules	17 Apr 2006 19:00:07 -0000	1.5
+++ pm/hooks/50modules	20 Oct 2006 11:34:15 -0000
@@ -13,9 +13,8 @@ suspend_modules()
 
 resume_modules()
 {
-	[ -z "$SUSPEND_MODULES" ] && return 0
-	for x in $SUSPEND_MODULES ; do
-		modreload $x
+	for x in $RESUME_MODULES ; do
+		modprobe $x > /dev/null 2>&1
 	done
 	return 0
 }

-- 
Stefan Seyfried
QA / R&D Team Mobile Devices        |              "Any ideas, John?"
SUSE LINUX Products GmbH, Nürnberg  | "Well, surrounding them's out." 


More information about the Pm-utils mailing list