Move main loop logic to main-loop.c

This patch moves most of the logic dealing with the internal main loop
into the main-loop.c file, to match upstream. Note however that the actual
implementation is not changed, and is still _very_ different from upstream.

However, this decouples a few more things to make upcoming refactors
significantly easier. This should not affect performance in any way.

+ Move the log rotation logic, which is specific to QEMU, to
  log-rotate-android.c and include/android/log-rotate.h, it's likely
  that the use of SIGUSR1 will not be maintained in the future, because
  upstream uses that to signal virtual CPU threads internally instead.

Change-Id: I7bc886778fa70742c165b1dfe77637564910764e
diff --git a/log-rotate-android.c b/log-rotate-android.c
new file mode 100644
index 0000000..747a0a7
--- /dev/null
+++ b/log-rotate-android.c
@@ -0,0 +1,93 @@
+// Copyright (C) 2011 The Android Open Source Project
+//
+// This software is licensed under the terms of the GNU General Public
+// License version 2, as published by the Free Software Foundation, and
+// may be copied, distributed, and modified under those terms.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+#include "android/log-rotate.h"
+#include "net/net.h"
+#include "slirp-android/libslirp.h"
+#include "sysemu/sysemu.h"
+
+#include <stdio.h>
+
+#ifdef _WIN32
+
+void qemu_log_rotation_init(void) {
+    // Nothing to do on Win32 for now.
+}
+
+void qemu_log_rotation_poll(void) {
+    // Nothing to do on Win32 for now.
+}
+
+#else // !_WIN32
+
+#include <sys/signal.h>
+
+static int rotate_logs_requested = 0;
+
+static void rotate_qemu_logs_handler(int signum) {
+  rotate_logs_requested = 1;
+}
+
+void qemu_log_rotation_init(void) {
+  // Install SIGUSR1 signal handler.
+  struct sigaction act;
+  sigfillset(&act.sa_mask);
+  act.sa_flags = 0;
+  act.sa_handler = rotate_qemu_logs_handler;
+  if (sigaction(SIGUSR1, &act, NULL) == -1) {
+    fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
+    exit(-1);
+  }
+}
+
+
+// Clears the passed qemu log when the rotate_logs_requested
+// is set. We need to clear the logs between the tasks that do not
+// require restarting Qemu.
+static FILE* rotate_log(FILE* old_log_fd, const char* filename) {
+  FILE* new_log_fd = NULL;
+  if (old_log_fd) {
+    if (fclose(old_log_fd) == -1) {
+      fprintf(stderr, "Cannot close old_log fd\n");
+      exit(errno);
+    }
+  }
+
+  if (!filename) {
+    fprintf(stderr, "The log filename to be rotated is not provided");
+    exit(-1);
+  }
+
+  new_log_fd = fopen(filename , "wb+");
+  if (new_log_fd == NULL) {
+    fprintf(stderr, "Cannot open the log file: %s for write.\n",
+            filename);
+    exit(1);
+  }
+
+  return new_log_fd;
+}
+
+
+void qemu_log_rotation_poll(void) {
+    if (!rotate_logs_requested)
+        return;
+    
+    FILE* new_dns_log_fd = rotate_log(get_slirp_dns_log_fd(),
+                                      dns_log_filename);
+    FILE* new_drop_log_fd = rotate_log(get_slirp_drop_log_fd(),
+                                       drop_log_filename);
+    slirp_dns_log_fd(new_dns_log_fd);
+    slirp_drop_log_fd(new_drop_log_fd);
+    rotate_logs_requested = 0;
+}
+
+#endif  // !_WIN32