lights: reworked light HAL to detect backlight path

The light HAL will now traverse /sys/class/backlight to find the first
backlight device rather than hardcoding /sys/class/backlight/wled. This
is needed for devices like the OnePlus 6 which expose a "virtual"
backlight device because OLED display don't have a real backlight.
diff --git a/init.common.rc b/init.common.rc
index d93f96c..a8a6342 100644
--- a/init.common.rc
+++ b/init.common.rc
@@ -24,8 +24,6 @@
     chmod 777 /sys/kernel/debug/sync/sw_sync
     chown graphics graphics /sys/kernel/debug/sync/info
 
-    chown system system /sys/class/backlight/wled/brightness
-
 on zygote-start
     mkdir /data/vendor/wifi 0770 wifi wifi
     mkdir /data/vendor/wifi/wpa 0770 wifi wifi
diff --git a/liblight/lights.c b/liblight/lights.c
index abda1ff..4c97681 100644
--- a/liblight/lights.c
+++ b/liblight/lights.c
@@ -22,8 +22,11 @@
 #include <log/log.h>
 #include <cutils/properties.h>
 
+#include <dirent.h>
+#include <fcntl.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
@@ -42,8 +45,10 @@
 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
 static int rgb_brightness_ratio = 255;
 
-char const*const LCD_FILE
-        = "/sys/class/backlight/wled/brightness";
+char const*const BACKLIGHT_CLASS
+        = "/sys/class/backlight";
+char const*const BACKLIGHT_CLASS_FSTRING
+        = "/sys/class/backlight/%s/brightness";
 
 /**
  * device methods
@@ -69,13 +74,37 @@
     }
 }
 
+static bool findFirstBacklightDevice(char *filename) {
+    struct dirent *de;
+    DIR *dir = opendir("/sys/class/backlight");
+    if (dir == NULL)
+        return NULL;
+    while((de = readdir(dir))) {
+        if(de->d_name[0] == '.' &&
+           (de->d_name[1] == '\0' ||
+            (de->d_name[1] == '.' && de->d_name[2] == '\0')))
+            continue;
+        strcpy(filename, de->d_name);
+        closedir(dir);
+        return true;
+    }
+    closedir(dir);
+    return false;
+}
+
 static int
-write_int(char const* path, int value)
+write_brightness(int value)
 {
     int fd;
-    static int already_warned = 0;
+    char brightness_path[4096];
+    char filename[256];
+    if (!findFirstBacklightDevice(filename)) {
+        ALOGE("CA:: Couldn't find backlight brightness path");
+        return -EINVAL;
+    }
 
-    fd = open(path, O_WRONLY);
+    snprintf(brightness_path, 4096, BACKLIGHT_CLASS_FSTRING, filename);
+    fd = open(brightness_path, O_WRONLY);
     if (fd >= 0) {
         char buffer[20];
         ssize_t amt;
@@ -89,10 +118,7 @@
         close(fd);
         return amt == -1 ? -errno : 0;
     } else {
-        if (already_warned == 0) {
-            ALOGE("write_int failed to open %s\n", path);
-            already_warned = 1;
-        }
+        ALOGE("write_brightness failed to open %s, fd = %d, errno = %d\n", brightness_path, fd, errno);
         return -errno;
     }
 }
@@ -114,7 +140,7 @@
     }
 
     int brightness = rgb_to_brightness(state) << 4; // Scale up to 4095
-    write_int(LCD_FILE, brightness);
+    write_brightness(brightness);
 
     return 0;
 }
@@ -140,6 +166,7 @@
 static int open_lights(const struct hw_module_t* module, char const* name,
         struct hw_device_t** device)
 {
+    (void)name;
     pthread_once(&g_init, init_globals);
 
     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 2472171..4764120 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -17,6 +17,8 @@
 /sys/devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc	u:object_r:sysfs_rtc:s0
 
+/sys/class/backlight(/.*)?									u:object_r:sysfs_lights:s0
+
 /sys/class/remoteproc									u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/remoteproc-adsp/remoteproc					u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/remoteproc-cdsp/remoteproc					u:object_r:sysfs_remoteproc:s0
diff --git a/sepolicy/genfs_contexts b/sepolicy/genfs_contexts
index 1766cc6..d2248d2 100644
--- a/sepolicy/genfs_contexts
+++ b/sepolicy/genfs_contexts
@@ -5,4 +5,3 @@
 genfscon sysfs   /devices/platform/soc@0/4080000.remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000	u:object_r:sysfs_rtc:s0
-genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:wled@d800/backlight/wled/ u:object_r:sysfs_lights:s0
diff --git a/sepolicy/hal_light_default.te b/sepolicy/hal_light_default.te
index 04eaaa6..c8483af 100644
--- a/sepolicy/hal_light_default.te
+++ b/sepolicy/hal_light_default.te
@@ -1,2 +1,6 @@
 # audit2allow
-allow hal_light_default sysfs_lights:file { open write };
+allow hal_light_default sysfs_lights:file { read open write };
+allow hal_light_default sysfs_lights:lnk_file { read };
+allow hal_light_default sysfs_gpu:dir { search };
+allow hal_light_default sysfs_gpu:file { read open write };
+allow hal_light_default sysfs_lights:dir { open search read };
diff --git a/ueventd.common.rc b/ueventd.common.rc
index bde2ad5..1cd2a14 100644
--- a/ueventd.common.rc
+++ b/ueventd.common.rc
@@ -11,3 +11,6 @@
 
 # Input devices
 /dev/input/* 0660 system input
+
+# Backlights
+/sys/class/backlight/* brightness 0660 system system
\ No newline at end of file