[Pm-utils] [PATCH] unload modules recursively

Stefan Seyfried seife at suse.de
Thu Oct 5 09:05:46 PDT 2006


Hi.

Sometimes it is usefull to unload a complete stack of modules.
This patch implements that:

Index: pm/functions
===================================================================
RCS file: /cvs/pm-utils/pm-utils/pm/functions,v
retrieving revision 1.20
diff -u -p -r1.20 functions
--- pm/functions	28 Sep 2006 21:25:36 -0000	1.20
+++ pm/functions	5 Oct 2006 16:00:39 -0000
@@ -113,13 +113,46 @@ 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()
 {
-	/sbin/lsmod 2>/dev/null | grep -q "$1"
-	if [ "$?" == "0" ]; then
-		echo "export ${1}_MODULE_LOAD=yes" >> /var/run/pm-suspend
-		/sbin/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, return 0.
+	#     if the module was not loaded, also return 0 since this is no error.
+	#     if no module was unloaded successfully, return 1
+	while read MOD D C USED D; do
+		[ "$MOD" != "$UNL" ] && continue
+		if [ "$USED" == "-" ]; then
+			if [ $C -eq 0 ]; then
+				if rmmod $UNL; then
+					echo "export ${UNL}_MODULE_LOAD=yes" >> /var/run/pm-suspend
+					RET=0
+				else
+					echo "## could not unload '$UNL', usage count was 0" \
+						>> /var/run/pm-suspend
+				fi
+			else
+				echo "## could not unload '$UNL', usage count: $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()

-- 
Stefan Seyfried
QA / R&D Team Mobile Devices        |              "Any ideas, John?"
SUSE LINUX Products GmbH, Nürnberg  | "Well, surrounding them's out." 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pm-utils-unload-modules-recursively.diff
Type: text/x-patch
Size: 1835 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/pm-utils/attachments/20061005/a109fa36/pm-utils-unload-modules-recursively.bin


More information about the Pm-utils mailing list