rebalance_interrupts: Change affinity ownership to system

Bug: 196058977
Test: Check affinity file ownerships
Change-Id: I4e63d9e6574e4730c452de4e18ae4b60b4ef8c64
diff --git a/rebalance_interrupts/rebalance_interrupts.cpp b/rebalance_interrupts/rebalance_interrupts.cpp
index b0d2f69..77a0bfb 100644
--- a/rebalance_interrupts/rebalance_interrupts.cpp
+++ b/rebalance_interrupts/rebalance_interrupts.cpp
@@ -224,6 +224,33 @@
   return true;
 }
 
+void ChownIrqAffinity() {
+    std::unique_ptr<DIR, decltype(&closedir)> irq_dir(opendir(PROC_IRQDIR), closedir);
+    if (!irq_dir) {
+        PLOG(ERROR) << "opening dir " PROC_IRQDIR;
+        return;
+    }
+
+    struct dirent *entry;
+    while ((entry = readdir(irq_dir.get()))) {
+        // If the directory entry isn't a parsable number, skip it.
+        // . and .. get skipped here.
+        unsigned throwaway;
+        if (!ParseUint(entry->d_name, &throwaway))
+            continue;
+
+        string affinity_path(PROC_IRQDIR "/");
+        affinity_path += entry->d_name;
+        affinity_path += "/smp_affinity";
+        chown(affinity_path.c_str(), 1000, 1000);
+
+        string affinity_list_path(PROC_IRQDIR "/");
+        affinity_list_path += entry->d_name;
+        affinity_list_path += "/smp_affinity_list";
+        chown(affinity_list_path.c_str(), 1000, 1000);
+    }
+}
+
 int main(int /* argc */, char* /* argv */[]) {
   map<string, list<string>> irq_mapping;
   list<pair<string, list<string>>> action_to_irqs;
@@ -238,6 +265,10 @@
     return 1;
   }
 
+  // Change ownership of smp_affinity and smp_affinity_list handles
+  // from root to system.
+  ChownIrqAffinity();
+
   // Some IRQs are already assigned to a subset of cores, usually for
   // good reason (like some drivers have an IRQ per core, for per-core
   // queues.)  Find the set of IRQs that haven't been mapped to specific