While browsing through the source of pm-utils, I noticed that the
lock/unlocking functions were using file creation/removal and PID
checking for locking purposes.  In a shell scripting language,
however, there is no way to atomically create files.

Directory creation, however, is atomic.  Here is a patch to atomicize
the lock/unlick functions.
diff -ur -X diffignore pm-utils/pm/functions working/pm/functions
--- pm-utils/pm/functions	2007-12-29 05:58:18.000000000 -0600
+++ working/pm/functions	2008-01-04 14:52:18.000000000 -0600
@@ -40,13 +40,9 @@
 {
 	VT=$(fgconsole)
 	chvt 63
-	if [ -f /.suspended ]; then
-		read pid < /.suspended
-		if [ -d /proc/$pid ]; then
-			return 1
-		fi
-	fi
-        echo "$$" > /.suspended
+	# file creation and reading is not atomic.
+	# Directory creation is.
+	mkdir /.suspended || return 1
 	rm -f /var/run/pm-suspend
 	touch /var/run/pm-suspend
 	return 0
@@ -57,7 +53,7 @@
 	rm -f /var/run/pm-suspend
 	chvt 1
 	chvt $VT
-	openvt -- sh -c "usleep $1 ; rm -f /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
+	openvt -- sh -c "usleep $1 ; rm -rf /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
 }
 
 find_sleepd_files()
diff -ur -X diffignore pm-utils/src/pm/functions working/src/pm/functions
--- pm-utils/src/pm/functions	2007-12-29 05:58:18.000000000 -0600
+++ working/src/pm/functions	2008-01-04 14:53:41.000000000 -0600
@@ -40,13 +40,9 @@
 {
 	VT=$(fgconsole)
 	chvt 63
-	if [ -f /.suspended ]; then
-		read pid < /.suspended
-		if [ -d /proc/$pid ]; then
-			return 1
-		fi
-	fi
-        echo "$$" > /.suspended
+	# file creation and reading is not atomic.
+	# Directory creation is.
+	mkdir /.suspended || return 1
 	rm -f /var/run/pm-suspend
 	touch /var/run/pm-suspend
 	return 0
@@ -57,7 +53,7 @@
 	rm -f /var/run/pm-suspend
 	chvt 1
 	chvt $VT
-	openvt -- sh -c "usleep $1 ; rm -f /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
+	openvt -- sh -c "usleep $1 ; rm -rf /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
 }
 
 find_sleepd_files()
_______________________________________________
Pm-utils mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/pm-utils

Reply via email to