Fix crash on shutdown when update in progress.

The ActionProcessor destructor normally calls the ProcessingStop method
on the delegate. For the UpdateAttempter this call re-schedules a new
update attempt on a half-destroyed update_attempter instance, crashing
update_engine on SIGTERM when the ActionProcessor was running.

This patch inhibits the ActionProcessor from notifying the delegate of
the processor stopping when destroying the update_attempter instance.
It also fixes the declaration order of the dbus_adaptor_ and disables
its usage during daemon shutdown.

Bug: 24989397
TEST=start update-engine; (update_engine_client --update &); sleep 6; stop update-engine; tail /var/log/update_engine.log
TEST=FEATURES=test emerge-link update_engine

Change-Id: I0a40067f63e89759ff80c79cecb6f89b10dba0c2
diff --git a/common/action_processor.cc b/common/action_processor.cc
index c5270a4..7ccdfbd 100644
--- a/common/action_processor.cc
+++ b/common/action_processor.cc
@@ -30,13 +30,10 @@
     : current_action_(nullptr), delegate_(nullptr) {}
 
 ActionProcessor::~ActionProcessor() {
-  if (IsRunning()) {
+  if (IsRunning())
     StopProcessing();
-  }
-  for (std::deque<AbstractAction*>::iterator it = actions_.begin();
-       it != actions_.end(); ++it) {
-    (*it)->SetProcessor(nullptr);
-  }
+  for (auto action : actions_)
+    action->SetProcessor(nullptr);
 }
 
 void ActionProcessor::EnqueueAction(AbstractAction* action) {
diff --git a/daemon.cc b/daemon.cc
index 0830feb..0b13c18 100644
--- a/daemon.cc
+++ b/daemon.cc
@@ -54,6 +54,14 @@
 }
 }  // namespace
 
+UpdateEngineDaemon::~UpdateEngineDaemon() {
+  UpdateAttempter* update_attempter = real_system_state_->update_attempter();
+  // Prevent any DBus communication from UpdateAttempter when shutting down the
+  // daemon.
+  if (update_attempter)
+    update_attempter->set_dbus_adaptor(nullptr);
+}
+
 int UpdateEngineDaemon::OnInit() {
   // Register the |subprocess_| singleton with this Daemon as the signal
   // handler.
diff --git a/daemon.h b/daemon.h
index 5aef8c5..66841f6 100644
--- a/daemon.h
+++ b/daemon.h
@@ -31,6 +31,7 @@
 class UpdateEngineDaemon : public brillo::DBusDaemon {
  public:
   UpdateEngineDaemon() = default;
+  ~UpdateEngineDaemon();
 
  protected:
   int OnInit() override;
@@ -46,9 +47,11 @@
   // the main() function.
   Subprocess subprocess_;
 
-  std::unique_ptr<RealSystemState> real_system_state_;
   std::unique_ptr<UpdateEngineAdaptor> dbus_adaptor_;
 
+  // The RealSystemState uses the previous classes so it should be defined last.
+  std::unique_ptr<RealSystemState> real_system_state_;
+
   DISALLOW_COPY_AND_ASSIGN(UpdateEngineDaemon);
 };
 
diff --git a/update_attempter.cc b/update_attempter.cc
index 3cdd613..93fda66 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -129,6 +129,9 @@
 
 UpdateAttempter::~UpdateAttempter() {
   CleanupCpuSharesManagement();
+  // Release ourselves as the ActionProcessor's delegate to prevent
+  // re-scheduling the updates due to the processing stopped.
+  processor_->set_delegate(nullptr);
 }
 
 void UpdateAttempter::Init() {