bluez: Add udev rule to give btd access to coredump_disabled sysfs

To enable or disable the devcoredump feature via chrome flag, bluetoothd
needs access to /sys/class/bluetooth/hci0/device/coredump_disabled sysfs
entry. Add udev rule assign ownership of that sysfs entry to bluetooth
user.

BUG=b:203034370
TEST=Change chrome flag and verify changes reflect in the
     '/sys/class/bluetooth/hci0/device/coredump_disabled' file

Change-Id: I703e1217d6238a7a1bf6842b578b50842281049a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/3814544
Commit-Queue: Manish Mandlik <mmandlik@chromium.org>
Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@google.com>
Tested-by: Manish Mandlik <mmandlik@chromium.org>
diff --git a/net-wireless/bluez/bluez-9999.ebuild b/net-wireless/bluez/bluez-9999.ebuild
index 7fb8762..5dbe17e 100644
--- a/net-wireless/bluez/bluez-9999.ebuild
+++ b/net-wireless/bluez/bluez-9999.ebuild
@@ -127,6 +127,7 @@
 	dobin "${FILESDIR}/get_bluetooth_device_class.sh"
 	dobin "${FILESDIR}/start_bluetoothd.sh"
 	dobin "${FILESDIR}/start_bluetoothlog.sh"
+	dobin "${FILESDIR}/set_bluetooth_coredump.sh"
 
 	# Install init scripts.
 	if use systemd; then
@@ -151,6 +152,7 @@
 	udev_dorules "${FILESDIR}/99-uhid.rules"
 	udev_dorules "${FILESDIR}/99-ps3-gamepad.rules"
 	udev_dorules "${FILESDIR}/99-bluetooth-quirks.rules"
+	udev_dorules "${FILESDIR}/99-bluetooth-devcoredump.rules"
 
 	# Install the config files.
 	cp "${FILESDIR}/main.conf" main.conf || die
diff --git a/net-wireless/bluez/files/99-bluetooth-devcoredump.rules b/net-wireless/bluez/files/99-bluetooth-devcoredump.rules
new file mode 100644
index 0000000..6338b7d
--- /dev/null
+++ b/net-wireless/bluez/files/99-bluetooth-devcoredump.rules
@@ -0,0 +1,9 @@
+# Copyright 2022 The ChromiumOS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Give bluetoothd appropriate permissions to write to /sys/.../coredump_disabled
+# and set the default state of the bluetooth devcoredump feature.
+ACTION!="remove", SUBSYSTEM=="bluetooth", ENV{DEVTYPE}=="host", \
+	ATTR{device/coredump_disabled}=="*", \
+	PROGRAM="/usr/bin/set_bluetooth_coredump.sh $sys $devpath"
diff --git a/net-wireless/bluez/files/set_bluetooth_coredump.sh b/net-wireless/bluez/files/set_bluetooth_coredump.sh
new file mode 100644
index 0000000..36246ac
--- /dev/null
+++ b/net-wireless/bluez/files/set_bluetooth_coredump.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+# Copyright 2022 The ChromiumOS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+coredump_conf_path="/run/bluetooth/coredump_disabled"
+sys=$1
+devpath=$2
+
+# Bluetoothd will need to write into /sys/.../device/coredump_disabled to enable
+# or disable the devcoredump feature based on a chrome://flags. Give appropriate
+# permissions to write.
+/bin/chown bluetooth "${sys}/${devpath}/device/coredump_disabled"
+
+# Apply the chrome flag value if available, else disable by default.
+if test -f "${coredump_conf_path}"; then
+    /bin/cat "${coredump_conf_path}" > \
+      "${sys}/${devpath}/device/coredump_disabled"
+else
+    /bin/echo 1 > "${sys}/${devpath}/device/coredump_disabled"
+fi