[PATCH] Add on demand functionality to bluetooth init script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi.

This patch adds on demand functionality for bluetooth service based on udev events.
It's adapted from Fedora [1].

Idea is that bluetoothd doesn't need to be running unless the user actually has a bluetooth device.


[1] https://bugzilla.redhat.com/show_bug.cgi?id=484345

Cheers,

Petr
--
Petr Lautrbach, Red Hat, Inc.
From 96c257e72a6a2ab5ef1325f98498817e0709600e Mon Sep 17 00:00:00 2001
From: Petr Lautrbach <plautrba@xxxxxxxxxx>
Date: Thu, 4 Jun 2009 15:09:52 +0200
Subject: [PATCH] bluetooth ondemand service based on udev

---
 acinclude.m4                     |    6 ++++++
 scripts/Makefile.am              |   10 +++++++++-
 scripts/bluetooth-ondemand.rules |    3 +++
 scripts/bluetooth.init           |   24 ++++++++++++++++++++++--
 4 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100644 scripts/bluetooth-ondemand.rules

diff --git a/acinclude.m4 b/acinclude.m4
index eb7cdeb..4082110 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -199,6 +199,7 @@ AC_DEFUN([AC_ARG_BLUEZ], [
 	configfiles_enable=yes
 	initscripts_enable=no
 	pcmciarules_enable=no
+	ondemand_enable=no
 	telephony_driver=dummy
 
 	AC_ARG_ENABLE(fortify, AC_HELP_STRING([--disable-fortify], [disable compile time buffer checks]), [
@@ -297,6 +298,10 @@ AC_DEFUN([AC_ARG_BLUEZ], [
 		pcmciarules_enable=${enableval}
 	])
 
+	AC_ARG_ENABLE(ondemand, AC_HELP_STRING([--enable-ondemand], [install ONDEMAND udev rules]), [
+		ondemand_enable=${enableval}
+	])
+
 	AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug], [enable compiling with debugging information]), [
 		debug_enable=${enableval}
 	])
@@ -357,4 +362,5 @@ AC_DEFUN([AC_ARG_BLUEZ], [
 	AM_CONDITIONAL(CONFIGFILES, test "${configfiles_enable}" = "yes")
 	AM_CONDITIONAL(INITSCRIPTS, test "${initscripts_enable}" = "yes")
 	AM_CONDITIONAL(PCMCIARULES, test "${pcmciarules_enable}" = "yes")
+	AM_CONDITIONAL(ONDEMAND, test "${ondemand_enable}" = "yes")
 ])
diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index 494a9c2..4914612 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -11,6 +11,10 @@ endif
 if PCMCIARULES
 rules_DATA += bluetooth-serial.rules
 endif
+
+if ONDEMAND
+rules_DATA += bluetooth-ondemand.rules
+endif
 endif
 
 if PCMCIARULES
@@ -20,7 +24,8 @@ udev_SCRIPTS = bluetooth_serial
 endif
 
 EXTRA_DIST = bluetooth.init bluetooth.default bluetooth-hid2hci.rules \
-				bluetooth-serial.rules bluetooth_serial
+				bluetooth-serial.rules bluetooth_serial \
+				bluetooth-ondemand.rules
 
 MAINTAINERCLEANFILES = Makefile.in
 
@@ -30,6 +35,9 @@ install-data-local:
 	$(mkinstalldirs) $(DESTDIR)$(sysconfdir)/default
 	[ -f $(DESTDIR)$(sysconfdir)/default/bluetooth ] || \
 		$(INSTALL_DATA) $(srcdir)/bluetooth.default $(DESTDIR)$(sysconfdir)/default/bluetooth
+if ONDEMAND
+	echo "DAEMON_ONDEMAND=true" >> $(DESTDIR)$(sysconfdir)/default/bluetooth
+endif
 
 uninstall-local:
 	@rm -f $(DESTDIR)$(sysconfdir)/init.d/bluetooth
diff --git a/scripts/bluetooth-ondemand.rules b/scripts/bluetooth-ondemand.rules
new file mode 100644
index 0000000..6146935
--- /dev/null
+++ b/scripts/bluetooth-ondemand.rules
@@ -0,0 +1,3 @@
+#bluetooth on demand rules
+SUBSYSTEM=="bluetooth", ACTION=="add", RUN+="/etc/init.d/bluetooth condstart"
+SUBSYSTEM=="bluetooth", ACTION=="remove", RUN+="/etc/init.d/bluetooth condstop"
diff --git a/scripts/bluetooth.init b/scripts/bluetooth.init
index 3ea8a89..b9a1454 100644
--- a/scripts/bluetooth.init
+++ b/scripts/bluetooth.init
@@ -17,21 +17,41 @@ DAEMON_ENABLE=true
 
 [ -e /etc/default/bluetooth ] && . /etc/default/bluetooth
 
+has_bt_devices()
+{
+	#Look for Bluetooth adapters:
+	udevadm info --export-db | grep -q -e '/devices/.*/bluetooth/.*'
+
+	return $?
+}
+
 case "$1" in
   start)
 	echo -n "Starting $DESC:"
 	if $DAEMON_ENABLE && [ -x "$DAEMON_EXEC" ]; then
-		$DAEMON_EXEC
-		echo -n " $DAEMON_NAME"
+		if [ -z $DAEMON_ONDEMAND ] || has_bt_devices; then
+			$DAEMON_EXEC
+			echo -n " $DAEMON_NAME"
+		fi
 	fi
 	echo "."
 	;;
+  condstart)
+	if [ -n $DAEMON_ONDEMAND ]; then
+		/etc/init.d/$NAME start
+	fi
+	;;
   stop)
 	echo -n "Stopping $DESC:"
 	killall $DAEMON_NAME > /dev/null 2>&1 || true
 	echo -n " $DAEMON_NAME"
 	echo "."
 	;;
+  condstop)
+	if [ -n $DAEMON_ONDEMAND ]; then
+		has_bt_devices || /etc/init.d/$NAME stop
+	fi
+	;;
   *)
 	N=/etc/init.d/$NAME
 	echo "Usage: $N {start|stop}" >&2
-- 
1.6.0.6


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux