From afd34b8072cafb6dc9c7b5eb236b486bcb64336f Mon Sep 17 00:00:00 2001
From: Rom Walton <rwalton@ssl.berkeley.edu>
Date: Fri, 12 Sep 2014 11:18:43 -0400
Subject: [PATCH] VBOX: Check for the completion file regardless if the VM is
 running or not.

---
 samples/vboxwrapper/vbox.cpp        |  7 +++++
 samples/vboxwrapper/vbox.h          |  2 +-
 samples/vboxwrapper/vboxwrapper.cpp | 42 +++++++++++------------------
 3 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/samples/vboxwrapper/vbox.cpp b/samples/vboxwrapper/vbox.cpp
index 2803bb0809..6b879ce2fe 100644
--- a/samples/vboxwrapper/vbox.cpp
+++ b/samples/vboxwrapper/vbox.cpp
@@ -1941,6 +1941,13 @@ bool VBOX_VM::is_logged_failure_guest_job_out_of_memory() {
     return false;
 }
 
+bool VBOX_VM::is_logged_completion_file_exists() {
+    char path[MAXPATHLEN];
+    sprintf(path, "shared/%s", completion_trigger_file.c_str());
+    if (boinc_file_exists(path)) return true;
+    return false;
+}
+
 bool VBOX_VM::is_virtualbox_version_newer(int maj, int min, int rel) {
     int vbox_major = 0, vbox_minor = 0, vbox_release = 0;
     if (3 == sscanf(virtualbox_version.c_str(), "%d.%d.%d", &vbox_major, &vbox_minor, &vbox_release)) {
diff --git a/samples/vboxwrapper/vbox.h b/samples/vboxwrapper/vbox.h
index 9c6b811aa7..1d4eac52c3 100644
--- a/samples/vboxwrapper/vbox.h
+++ b/samples/vboxwrapper/vbox.h
@@ -205,7 +205,6 @@ public:
     int pause();
     int resume();
     void check_trickle_triggers();
-    void check_completion_trigger();
     int create_snapshot(double elapsed_time);
     int cleanup_snapshots(bool delete_active);
     int restore_snapshot();
@@ -223,6 +222,7 @@ public:
     bool is_logged_failure_vm_extensions_not_supported();
     bool is_logged_failure_host_out_of_memory();
     bool is_logged_failure_guest_job_out_of_memory();
+    bool is_logged_completion_file_exists();
     bool is_virtualbox_version_newer(int maj, int min, int rel);
     bool is_virtualbox_error_recoverable(int retval);
 
diff --git a/samples/vboxwrapper/vboxwrapper.cpp b/samples/vboxwrapper/vboxwrapper.cpp
index 8ec7609fb1..a9e368004d 100644
--- a/samples/vboxwrapper/vboxwrapper.cpp
+++ b/samples/vboxwrapper/vboxwrapper.cpp
@@ -407,40 +407,22 @@ void set_remote_desktop_info(APP_INIT_DATA& /* aid */, VBOX_VM& vm) {
     }
 }
 
-// check for completion trigger file
-//
-void VBOX_VM::check_completion_trigger() {
+void extract_completion_file_info(VBOX_VM& vm, unsigned long& exit_code, string& message) {
     char path[MAXPATHLEN];
-    static double detect_time = 0;
+    char buf[1024];
 
-    if (detect_time) {
-        if (dtime() > detect_time + 60) {
-            cleanup();
-            dump_hypervisor_logs(true);
-            boinc_finish(0);
-        }
-        return;
-    }
-    sprintf(path, "shared/%s", completion_trigger_file.c_str());
-    if (!boinc_file_exists(path)) return;
-    detect_time = dtime();
-#if 0
-    int exit_code = 0;
+    sprintf(path, "shared/%s", vm.completion_trigger_file.c_str());
     FILE* f = fopen(path, "r");
     if (f) {
-        char buf[1024];
+        message = "";
         if (fgets(buf, 1024, f) != NULL) {
             exit_code = atoi(buf);
         }
         while (fgets(buf, 1024, f) != NULL) {
-            fputs(buf, stderr);
+            message += buf;
         }
         fclose(f);
     }
-    cleanup();
-    dump_hypervisor_logs(true);
-    boinc_finish(exit_code);
-#endif
 }
 
 // check for trickle trigger files, and send trickles if find them.
@@ -1074,6 +1056,17 @@ int main(int argc, char** argv) {
             vm.dump_hypervisor_logs(true);
             boinc_finish(EXIT_ABORTED_BY_CLIENT);
         }
+        if (vm.is_logged_completion_file_exists()) {
+            vm.reset_vm_process_priority();
+            vm.cleanup();
+            fprintf(
+                stderr,
+                "%s VM Completion File Detected.\n",
+                vboxwrapper_msg_prefix(buf, sizeof(buf))
+            );
+            extract_completion_file_info(vm, vm_exit_code, message);
+            boinc_finish(vm_exit_code);
+        }
         if (!vm.online) {
             // Is this a type of event we can recover from?
             if (vm.is_logged_failure_host_out_of_memory()) {
@@ -1160,9 +1153,6 @@ int main(int argc, char** argv) {
             if ((loop_iteration % 10) == 0) {
                 current_cpu_time = starting_cpu_time + vm.get_vm_cpu_time();
                 vm.check_trickle_triggers();
-                if (!vm.completion_trigger_file.empty()) {
-                    vm.check_completion_trigger();
-                }
             }
 
             if (vm.job_duration) {
-- 
GitLab