vnc: Add shutdown signal handler
diff --git a/src/VNCFlinger.cpp b/src/VNCFlinger.cpp
index 37a8614..d1670ce 100644
--- a/src/VNCFlinger.cpp
+++ b/src/VNCFlinger.cpp
@@ -47,10 +47,26 @@
 
     rfbRunEventLoop(mVNCScreen, -1, true);
 
-    ALOGD("VNCFlinger is running!");
+    ALOGD("VNCFlinger ready to fling");
 
     eventLoop();
 
+    ALOGI("VNCFlinger has left the building");
+
+    return NO_ERROR;
+}
+
+status_t VNCFlinger::stop() {
+    Mutex::Autolock _L(mEventMutex);
+
+    ALOGV("Shutting down");
+
+    destroyVirtualDisplayLocked();
+    mClientCount = 0;
+    mRunning = false;
+
+    mEventCond.signal();
+
     return NO_ERROR;
 }
 
@@ -115,6 +131,10 @@
 }
 
 status_t VNCFlinger::destroyVirtualDisplayLocked() {
+    if (!mVDSActive) {
+        return NO_INIT;
+    }
+
     mCpuConsumer.clear();
     mProducer.clear();
     SurfaceComposerClient::destroyDisplay(mDpy);
@@ -163,17 +183,6 @@
     return err;
 }
 
-status_t VNCFlinger::stop() {
-    Mutex::Autolock _L(mEventMutex);
-
-    mClientCount = 0;
-    mRunning = false;
-
-    mEventCond.signal();
-
-    return NO_ERROR;
-}
-
 size_t VNCFlinger::addClient() {
     Mutex::Autolock _l(mEventMutex);
     if (mClientCount == 0) {
diff --git a/src/main.cpp b/src/main.cpp
index e7dc163..dc34921 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -15,11 +15,22 @@
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //
 
+#include <csignal>
+
 #include "VNCFlinger.h"
 
 using namespace android;
 
+static VNCFlinger* VNC;
+
+static void onSignal(int /* signal */) {
+    VNC->stop();
+}
+
 int main(int argc, char** argv) {
-    VNCFlinger flinger(argc, argv);
-    flinger.start();
+    std::signal(SIGINT, onSignal);
+    std::signal(SIGHUP, onSignal);
+
+    VNC = new VNCFlinger(argc, argv);
+    VNC->start();
 }