From 89579f241c5c5fda75420c5e21ca4a46879bee2f Mon Sep 17 00:00:00 2001
From: Glenn Kasten <gkasten@google.com>
Date: Thu, 6 Oct 2016 12:58:46 -0700
Subject: [PATCH] Traverse /etc/init in a well-defined order

Bug: 31996208
Test: will need a CTS, not yet done
Change-Id: I5ecc7f0519d42a83065b7b97a31cdb5b33549cda
---
 init/init_parser.cpp | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/init/init_parser.cpp b/init/init_parser.cpp
index b44ca59d58..f2e5d6df94 100644
--- a/init/init_parser.cpp
+++ b/init/init_parser.cpp
@@ -122,14 +122,20 @@ bool Parser::ParseConfigDir(const std::string& path) {
         return false;
     }
     dirent* current_file;
+    std::vector<std::string> files;
     while ((current_file = readdir(config_dir.get()))) {
-        std::string current_path =
-            android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
         // Ignore directories and only process regular files.
         if (current_file->d_type == DT_REG) {
-            if (!ParseConfigFile(current_path)) {
-                ERROR("could not import file '%s'\n", current_path.c_str());
-            }
+            std::string current_path =
+                android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
+            files.emplace_back(current_path);
+        }
+    }
+    // Sort first so we load files in a consistent order (bug 31996208)
+    std::sort(files.begin(), files.end());
+    for (const auto& file : files) {
+        if (!ParseConfigFile(file)) {
+            ERROR("Could not import file '%s'\n", file.c_str());
         }
     }
     return true;
-- 
GitLab