shill: Disconnect services before exiting.

Add SIGINT/SIGTERM handler to gracefully exit the main loop and add
ability to disconnect from all services during the clean up.

BUG=chromium-os:19997
TEST=Unit tests, WiFiManager, manually run shill and Control-C

Change-Id: Ida02ade729af624ef0ea4e32e3fb80436aaf870b
Reviewed-on: https://gerrit.chromium.org/gerrit/11092
Commit-Ready: Thieu Le <thieule@chromium.org>
Reviewed-by: Thieu Le <thieule@chromium.org>
Tested-by: Thieu Le <thieule@chromium.org>
diff --git a/shill_main.cc b/shill_main.cc
index 2213466..c3af22b 100644
--- a/shill_main.cc
+++ b/shill_main.cc
@@ -14,6 +14,10 @@
 #include <base/string_split.h>
 #include <chromeos/syslog_logging.h>
 
+extern "C" {
+#include <glib-unix.h>
+}
+
 #include "shill/dbus_control.h"
 #include "shill/shill_config.h"
 #include "shill/shill_daemon.h"
@@ -77,6 +81,12 @@
   delete dbus_control;
 }
 
+gboolean ExitSigHandler(gpointer data) {
+  shill::Daemon* daemon = reinterpret_cast<shill::Daemon*>(data);
+  daemon->Quit();
+  return TRUE;
+}
+
 
 int main(int argc, char** argv) {
   base::AtExitManager exit_manager;
@@ -130,6 +140,10 @@
       daemon.AddDeviceToBlackList(*i);
     }
   }
+
+  g_unix_signal_add(SIGINT, ExitSigHandler, &daemon);
+  g_unix_signal_add(SIGTERM, ExitSigHandler, &daemon);
+
   daemon.Run();
 
   return 0;