Registered signal handlers for joysticks and control loops

This *should* prevent us from leeking messages.  I'm still seeing
leaks, so I bet we aren't fully set up to handle that yet.

Change-Id: Id4a8c5c1741db3e70a3d0a836667b957aba8165a
diff --git a/aos/input/joystick_input.cc b/aos/input/joystick_input.cc
index a6ada96..7508c3c 100644
--- a/aos/input/joystick_input.cc
+++ b/aos/input/joystick_input.cc
@@ -1,6 +1,7 @@
 #include "aos/input/joystick_input.h"
 
 #include <string.h>
+#include <atomic>
 
 #include "aos/common/messages/robot_state.q.h"
 #include "aos/common/logging/logging.h"
@@ -9,9 +10,23 @@
 namespace aos {
 namespace input {
 
+::std::atomic<bool> JoystickInput::run_;
+
+void JoystickInput::Quit(int /*signum*/) { run_ = false; }
+
 void JoystickInput::Run() {
+  run_ = true;
+  struct sigaction action;
+  action.sa_handler = &JoystickInput::Quit;
+  sigemptyset(&action.sa_mask);
+  action.sa_flags = SA_RESETHAND;
+
+  PCHECK(sigaction(SIGTERM, &action, nullptr));
+  PCHECK(sigaction(SIGQUIT, &action, nullptr));
+  PCHECK(sigaction(SIGINT, &action, nullptr));
+
   driver_station::Data data;
-  while (true) {
+  while (run_) {
     joystick_state.FetchAnother();
 
     data.Update(*joystick_state);
@@ -60,6 +75,7 @@
 
     RunIteration(data);
   }
+  LOG(INFO, "Shutting down\n");
 }
 
 }  // namespace input
diff --git a/aos/input/joystick_input.h b/aos/input/joystick_input.h
index ec8b52a..b10b1ba 100644
--- a/aos/input/joystick_input.h
+++ b/aos/input/joystick_input.h
@@ -1,6 +1,8 @@
 #ifndef AOS_INPUT_JOYSTICK_INPUT_H_
 #define AOS_INPUT_JOYSTICK_INPUT_H_
 
+#include <atomic>
+
 #include "aos/common/input/driver_station_data.h"
 
 namespace aos {
@@ -18,6 +20,10 @@
  private:
   // Subclasses should do whatever they want with data here.
   virtual void RunIteration(const driver_station::Data &data) = 0;
+
+  static void Quit(int /*signum*/);
+
+  static ::std::atomic<bool> run_;
 };
 
 // Class which will proxy joystick information from UDP packets to the queues.