am ff5be396: Merge changes from topic \'init-rc-breakup\'

* commit 'ff5be396d77cdf7f18e7d8f7a14f62ec760f5a1e':
  init: Queue Triggers instead of Actions
  bundle init.rc contents with its service
diff --git a/adb/Android.mk b/adb/Android.mk
index e2d0bb1..e271a63 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -277,6 +277,8 @@
 
 LOCAL_MODULE := adbd
 
+LOCAL_INIT_RC := adbd.rc
+
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
diff --git a/adb/adbd.rc b/adb/adbd.rc
new file mode 100644
index 0000000..b91d8b5
--- /dev/null
+++ b/adb/adbd.rc
@@ -0,0 +1,14 @@
+on post-fs-data
+    mkdir /data/misc/adb 02750 system shell
+    mkdir /data/adb 0700 root root
+
+# adbd is controlled via property triggers in init.<platform>.usb.rc
+service adbd /sbin/adbd --root_seclabel=u:r:su:s0
+    class core
+    socket adbd stream 660 system system
+    disabled
+    seclabel u:r:adbd:s0
+
+# adbd on at boot in emulator
+on property:ro.kernel.qemu=1
+    start adbd
diff --git a/debuggerd/Android.mk b/debuggerd/Android.mk
index 3fca709..f7a5f82 100644
--- a/debuggerd/Android.mk
+++ b/debuggerd/Android.mk
@@ -27,6 +27,9 @@
 
 LOCAL_CPPFLAGS := $(common_cppflags)
 
+LOCAL_INIT_RC_32 := debuggerd.rc
+LOCAL_INIT_RC_64 := debuggerd64.rc
+
 ifeq ($(TARGET_IS_64_BIT),true)
 LOCAL_CPPFLAGS += -DTARGET_IS_64_BIT
 endif
diff --git a/debuggerd/debuggerd.rc b/debuggerd/debuggerd.rc
new file mode 100644
index 0000000..4338ae9
--- /dev/null
+++ b/debuggerd/debuggerd.rc
@@ -0,0 +1,2 @@
+service debuggerd /system/bin/debuggerd
+    class main
diff --git a/debuggerd/debuggerd64.rc b/debuggerd/debuggerd64.rc
new file mode 100644
index 0000000..341a329
--- /dev/null
+++ b/debuggerd/debuggerd64.rc
@@ -0,0 +1,2 @@
+service debuggerd64 /system/bin/debuggerd64
+    class main
diff --git a/init/action.cpp b/init/action.cpp
index 2eb809e..dd366d3 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -154,7 +154,7 @@
     const static std::string prop_str("property:");
     for (std::size_t i = 0; i < args.size(); ++i) {
         if (i % 2) {
-            if (args[i].compare("&&")) {
+            if (args[i] != "&&") {
                 *err = "&& is the only symbol allowed to concatenate actions";
                 return false;
             } else {
@@ -189,24 +189,24 @@
 bool Action::CheckPropertyTriggers(const std::string& name,
                                    const std::string& value) const
 {
-    bool found = !name.compare("");
+    bool found = name.empty();
     if (property_triggers_.empty()) {
         return true;
     }
 
     for (const auto& t : property_triggers_) {
-        if (!t.first.compare(name)) {
-            if (t.second.compare("*") &&
-                t.second.compare(value)) {
+        const auto& trigger_name = t.first;
+        const auto& trigger_value = t.second;
+        if (trigger_name == name) {
+            if (trigger_value != "*" && trigger_value != value) {
                 return false;
             } else {
                 found = true;
             }
         } else {
-            std::string prop_val = property_get(t.first.c_str());
-            if (prop_val.empty() ||
-                (t.second.compare("*") &&
-                 t.second.compare(prop_val))) {
+            std::string prop_val = property_get(trigger_name.c_str());
+            if (prop_val.empty() || (trigger_value != "*" &&
+                                     trigger_value != prop_val)) {
                 return false;
             }
         }
@@ -217,7 +217,7 @@
 bool Action::CheckEventTrigger(const std::string& trigger) const
 {
     return !event_trigger_.empty() &&
-        !trigger.compare(event_trigger_) &&
+        trigger == event_trigger_ &&
         CheckPropertyTriggers();
 }
 
@@ -229,10 +229,8 @@
 
 bool Action::TriggersEqual(const class Action& other) const
 {
-    return property_triggers_.size() == other.property_triggers_.size() &&
-        std::equal(property_triggers_.begin(), property_triggers_.end(),
-                   other.property_triggers_.begin()) &&
-        !event_trigger_.compare(other.event_trigger_);
+    return property_triggers_ == other.property_triggers_ &&
+        event_trigger_ == other.event_trigger_;
 }
 
 std::string Action::BuildTriggersString() const
@@ -255,19 +253,53 @@
 
 void Action::DumpState() const
 {
-    INFO("on ");
     std::string trigger_name = BuildTriggersString();
-    INFO("%s", trigger_name.c_str());
-    INFO("\n");
+    INFO("on %s\n", trigger_name.c_str());
 
     for (const auto& c : commands_) {
         std::string cmd_str = c->BuildCommandString();
-        INFO(" %s", cmd_str.c_str());
+        INFO(" %s\n", cmd_str.c_str());
     }
     INFO("\n");
 }
 
-ActionManager::ActionManager() : cur_command_(0)
+
+class EventTrigger : public Trigger {
+public:
+    EventTrigger(const std::string& trigger) : trigger_(trigger) {
+    }
+    bool CheckTriggers(const Action* action) override {
+        return action->CheckEventTrigger(trigger_);
+    }
+private:
+    std::string trigger_;
+};
+
+class PropertyTrigger : public Trigger {
+public:
+    PropertyTrigger(const std::string& name, const std::string& value)
+        : name_(name), value_(value) {
+    }
+    bool CheckTriggers(const Action* action) override {
+        return action->CheckPropertyTrigger(name_, value_);
+    }
+private:
+    std::string name_;
+    std::string value_;
+};
+
+class BuiltinTrigger : public Trigger {
+public:
+    BuiltinTrigger(Action* action) : action_(action) {
+    }
+    bool CheckTriggers(const Action* action) override {
+        return action == action_;
+    }
+private:
+    Action* action_;
+};
+
+ActionManager::ActionManager() : current_command_(0)
 {
 }
 
@@ -278,21 +310,13 @@
 
 void ActionManager::QueueEventTrigger(const std::string& trigger)
 {
-    for (const auto& a : action_list_) {
-        if (a->CheckEventTrigger(trigger)) {
-            action_queue_.push(a);
-        }
-    }
+    trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
 }
 
 void ActionManager::QueuePropertyTrigger(const std::string& name,
                                          const std::string& value)
 {
-    for (const auto& a : action_list_) {
-        if (a->CheckPropertyTrigger(name, value)) {
-            action_queue_.push(a);
-        }
-    }
+    trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
 }
 
 void ActionManager::QueueAllPropertyTriggers()
@@ -312,35 +336,45 @@
 
     act->AddCommand(func, name_vector);
 
-    action_queue_.push(act);
+    actions_.push_back(act);
+    trigger_queue_.push(std::make_unique<BuiltinTrigger>(act));
 }
 
 void ActionManager::ExecuteOneCommand() {
-    if (action_queue_.empty()) {
+    while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
+        std::copy_if(actions_.begin(), actions_.end(),
+                     std::back_inserter(current_executing_actions_),
+                     [this] (Action* act) {
+                         return trigger_queue_.front()->CheckTriggers(act);
+                     });
+        trigger_queue_.pop();
+    }
+
+    if (current_executing_actions_.empty()) {
         return;
     }
 
-    Action* action = action_queue_.front();
+    Action* action = current_executing_actions_.back();
     if (!action->NumCommands()) {
-        action_queue_.pop();
+        current_executing_actions_.pop_back();
         return;
     }
 
-    if (cur_command_ == 0) {
+    if (current_command_ == 0) {
         std::string trigger_name = action->BuildTriggersString();
         INFO("processing action %p (%s)\n", action, trigger_name.c_str());
     }
 
-    action->ExecuteOneCommand(cur_command_++);
-    if (cur_command_ == action->NumCommands()) {
-        cur_command_ = 0;
-        action_queue_.pop();
+    action->ExecuteOneCommand(current_command_++);
+    if (current_command_ == action->NumCommands()) {
+        current_command_ = 0;
+        current_executing_actions_.pop_back();
     }
 }
 
 bool ActionManager::HasMoreCommands() const
 {
-    return !action_queue_.empty();
+    return !current_executing_actions_.empty() || !trigger_queue_.empty();
 }
 
 Action* ActionManager::AddNewAction(const std::vector<std::string>& triggers,
@@ -357,21 +391,21 @@
     }
 
     auto old_act_it =
-        std::find_if(action_list_.begin(), action_list_.end(),
+        std::find_if(actions_.begin(), actions_.end(),
                      [&act] (Action* a) { return act->TriggersEqual(*a); });
 
-    if (old_act_it != action_list_.end()) {
+    if (old_act_it != actions_.end()) {
         delete act;
         return *old_act_it;
     }
 
-    action_list_.push_back(act);
+    actions_.push_back(act);
     return act;
 }
 
 void ActionManager::DumpState() const
 {
-    for (const auto& a : action_list_) {
+    for (const auto& a : actions_) {
         a->DumpState();
     }
     INFO("\n");
diff --git a/init/action.h b/init/action.h
index ae28fe1..5088c71 100644
--- a/init/action.h
+++ b/init/action.h
@@ -54,6 +54,12 @@
     std::vector<Command*> commands_;
 };
 
+class Trigger {
+public:
+    virtual ~Trigger() { }
+    virtual bool CheckTriggers(const Action* action) = 0;
+};
+
 class ActionManager {
 public:
     static ActionManager& GetInstance();
@@ -74,9 +80,10 @@
     ActionManager(ActionManager const&) = delete;
     void operator=(ActionManager const&) = delete;
 
-    std::vector<Action*> action_list_;
-    std::queue<Action*> action_queue_;
-    std::size_t cur_command_;
+    std::vector<Action*> actions_;
+    std::queue<std::unique_ptr<Trigger>> trigger_queue_;
+    std::vector<Action*> current_executing_actions_;
+    std::size_t current_command_;
 };
 
 #endif
diff --git a/lmkd/Android.mk b/lmkd/Android.mk
index 39081d6..8c88661 100644
--- a/lmkd/Android.mk
+++ b/lmkd/Android.mk
@@ -7,4 +7,6 @@
 
 LOCAL_MODULE := lmkd
 
+LOCAL_INIT_RC := lmkd.rc
+
 include $(BUILD_EXECUTABLE)
diff --git a/lmkd/lmkd.rc b/lmkd/lmkd.rc
new file mode 100644
index 0000000..83c5ff0
--- /dev/null
+++ b/lmkd/lmkd.rc
@@ -0,0 +1,4 @@
+service lmkd /system/bin/lmkd
+    class core
+    critical
+    socket lmkd seqpacket 0660 system system
diff --git a/logcat/Android.mk b/logcat/Android.mk
index 7115f9b..844ab8b 100644
--- a/logcat/Android.mk
+++ b/logcat/Android.mk
@@ -11,6 +11,8 @@
 
 LOCAL_CFLAGS := -Werror
 
+LOCAL_INIT_RC := logcatd.rc
+
 include $(BUILD_EXECUTABLE)
 
 include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/logcat/logcatd.rc b/logcat/logcatd.rc
new file mode 100644
index 0000000..0bc581e
--- /dev/null
+++ b/logcat/logcatd.rc
@@ -0,0 +1,13 @@
+on property:persist.logd.logpersistd=logcatd
+    # all exec/services are called with umask(077), so no gain beyond 0700
+    mkdir /data/misc/logd 0700 logd log
+    # logd for write to /data/misc/logd, log group for read from pstore (-L)
+    exec - logd log -- /system/bin/logcat -L -b all -v threadtime -v usec -v printable -D -f /data/misc/logd/logcat -r 64 -n 256
+    start logcatd
+
+service logcatd /system/bin/logcat -b all -v threadtime -v usec -v printable -D -f /data/misc/logd/logcat -r 64 -n 256
+    class late_start
+    disabled
+    # logd for write to /data/misc/logd, log group for read from log daemon
+    user logd
+    group log
diff --git a/logd/Android.mk b/logd/Android.mk
index 75df484..01c51c7 100644
--- a/logd/Android.mk
+++ b/logd/Android.mk
@@ -4,6 +4,8 @@
 
 LOCAL_MODULE:= logd
 
+LOCAL_INIT_RC := logd.rc
+
 LOCAL_SRC_FILES := \
     main.cpp \
     LogCommand.cpp \
diff --git a/logd/logd.rc b/logd/logd.rc
new file mode 100644
index 0000000..498baec
--- /dev/null
+++ b/logd/logd.rc
@@ -0,0 +1,9 @@
+service logd /system/bin/logd
+    class core
+    socket logd stream 0666 logd logd
+    socket logdr seqpacket 0666 logd logd
+    socket logdw dgram 0222 logd logd
+
+service logd-reinit /system/bin/logd --reinit
+    oneshot
+    disabled