Browse Source

changing queue program to have more sensible behaviour on interrupt signal

tristangurtler 3 years ago
parent
commit
b0fdd8441b

+ 2 - 1
prsona/inc/networkOrchestrator.hpp

@@ -7,13 +7,14 @@
 
 #include "networking.hpp"
 
+const std::chrono::seconds TWO_SECONDS(2);
 const std::chrono::seconds ONE_SECOND(1);
 const std::chrono::milliseconds HALF_SECOND(500);
 
 /* "PUBLIC" FUNCTIONS */
 
 // START UP AND SHUT DOWN INSTANCES
-int start_remote_actor(
+void start_remote_actor(
     const std::string& target,
     bool server,
     const std::string& id,

+ 2 - 2
prsona/scripts/bringDownTestServers.sh

@@ -1,7 +1,7 @@
 #!/bin/sh
 
-servers=$(cat cfg/serverIPs.cfg)
-clients=$(cat cfg/clientIPs.cfg)
+servers=$(cat cfg/$1/serverIPs.cfg)
+clients=$(cat cfg/$1/clientIPs.cfg)
 
 for server in $servers
 do

+ 70 - 9
prsona/src/experimentQueueMain.cpp

@@ -10,26 +10,87 @@
 #include <fstream>
 #include <cstring>
 #include <cstdlib>
+#include <csignal>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+const char *ORCHESTRATOR = "bin/orchestrator";
+const char *SHUT_DOWN = "scripts/bringDownTestServers.sh";
+const int INPUT_BUFFER_LEN = 133;
+char whichTest[INPUT_BUFFER_LEN];
+int currPid = 0;
+
+void exitInterruptHandler(int signum)
+{
+    std::cout << "Interrupt signal received, quitting." << std::endl;
+
+    kill(-currPid, SIGINT);
+
+    char *argv[3];
+    char shutdownBuffer[INPUT_BUFFER_LEN];
+    strncpy(shutdownBuffer, SHUT_DOWN, INPUT_BUFFER_LEN);
+
+    argv[0] = shutdownBuffer;
+    argv[1] = whichTest;
+    argv[2] = NULL;
+
+    int lastPid = fork();
+    if (currPid < 0)
+        exit(-signum);
+    if (currPid == 0)
+        execv(SHUT_DOWN, argv);
+    else
+        waitpid(lastPid, NULL, 0);
+
+    exit(signum);
+}
 
 int main(int argc, char* argv[])
 {
-    const char *ORCHESTRATOR = "bin/orchestrator ";
-    const int ORCHESTRATOR_LEN = strlen(ORCHESTRATOR);
-    const int INPUT_BUFFER_LEN = 133;
-    const int OUTPUT_BUFFER_LEN = ORCHESTRATOR_LEN + INPUT_BUFFER_LEN;
+    signal(SIGINT, exitInterruptHandler);
 
-    char inputBuffer[INPUT_BUFFER_LEN], outputBuffer[OUTPUT_BUFFER_LEN];
+    char inputBuffer[INPUT_BUFFER_LEN];
     std::ifstream configFile("cfg/queue.cfg");
     while (!configFile.eof())
     {
         configFile.getline(inputBuffer, INPUT_BUFFER_LEN);
         if (strlen(inputBuffer) > 0)
         {
-            strncpy(outputBuffer, ORCHESTRATOR, ORCHESTRATOR_LEN);
-            strncpy(outputBuffer + ORCHESTRATOR_LEN, inputBuffer, INPUT_BUFFER_LEN);
-            outputBuffer[OUTPUT_BUFFER_LEN] = 0;
+            char *helper = strtok(inputBuffer, " ");
+            char *argv[6];
+            
+            char orchestratorBuffer[INPUT_BUFFER_LEN];
+            strncpy(orchestratorBuffer, ORCHESTRATOR, INPUT_BUFFER_LEN);
+            argv[0] = orchestratorBuffer;
+            argv[1] = helper;
+
+            strncpy(whichTest, helper, INPUT_BUFFER_LEN);
+
+            size_t i = 2;
+            while (helper != NULL && i < 6)
+            {
+                helper = strtok(NULL, " ");
+                argv[i] = helper;
+                i++;
+            }
+            argv[5] = NULL;
             
-            int sysOut = system(outputBuffer);
+            currPid = fork();
+            if (currPid < 0)
+            {
+                std::cerr << "Problem making new exec, aborting." << std::endl;
+                return 1;
+            }
+            else if (currPid == 0)
+            {
+                execv(ORCHESTRATOR, argv);
+            }
+            else
+            {
+                waitpid(currPid, NULL, 0);
+            }
+            currPid = 0;
         }
     }
 

+ 72 - 8
prsona/src/networkOrchestrator.cpp

@@ -4,6 +4,9 @@
 #include <algorithm>
 #include <cstdlib>
 #include <thread>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "networkOrchestrator.hpp"
 
@@ -17,7 +20,7 @@
  * START UP AND SHUT DOWN INSTANCES
  */
 
-int start_remote_actor(
+void start_remote_actor(
     const std::string& target,
     bool server,
     const std::string& id,
@@ -25,16 +28,77 @@ int start_remote_actor(
     size_t lambda,
     bool maliciousServers)
 {
-    std::stringstream buffer;
-    std::string command;
+    const char* sshFile = "ssh";
+    const char* serverFile = "bin/server";
+    const char* clientFile = "bin/client";
+    const char* calledFile = (target != "tick0" && !target.empty() ? sshFile : (server ? serverFile : clientFile));
 
-    if (target != "tick0" && !target.empty())
-        buffer << "ssh -n " << target << " \"~/prsona/prsona/scripts/startup.sh " << (server ? "server " : "client ") << id << " " << output << " " << lambda << (maliciousServers ? " T\" &" : " F\" &");
+    char *argv[6];
+
+    char fileBuffer[11];
+    strncpy(fileBuffer, calledFile, 11);
+    argv[0] = fileBuffer;
+
+    char flagBuffer[3];
+    strncpy(flagBuffer, "-n", 3);
+
+    char targetBuffer[64];
+    strncpy(targetBuffer, target.c_str(), 64);
+
+    std::string fullArgString;
+    char fullArgBuffer[256];
+
+    char idBuffer[64];
+    strncpy(idBuffer, id.c_str(), 64);
+
+    char outputBuffer[128];
+    strncpy(outputBuffer, output.c_str(), 128);
+
+    std::stringstream lambdaStream;
+    lambdaStream << lambda;
+    char lambdaBuffer[3];
+    strncpy(lambdaBuffer, lambdaStream.str().c_str(), 3);
+
+    char maliciousBuffer[2];
+    if (maliciousServers)
+        strncpy(maliciousBuffer, "T", 2);
     else
-        buffer << "bin/" << (server ? "server " : "client ") << id << " " << output << " " << lambda << (maliciousServers ? " T &" : " F &");
-    command = buffer.str();
+        strncpy(maliciousBuffer, "F", 2);
 
-    return system(command.c_str());
+    if (target != "tick0" && !target.empty())
+    {
+        fullArgString = "~/prsona/prsona/scripts/startup.sh ";
+        fullArgString += (server ? "server" : "client");
+        fullArgString += " ";
+        fullArgString += id;
+        fullArgString += " ";
+        fullArgString += output;
+        fullArgString += " ";
+        fullArgString += lambdaStream.str();
+        fullArgString += " ";
+        fullArgString += (maliciousServers ? "T" : "F");
+
+        strncpy(fullArgBuffer, fullArgString.c_str(), 256);
+
+        argv[1] = flagBuffer;
+        argv[2] = targetBuffer;
+        argv[3] = fullArgBuffer;
+        argv[4] = NULL;
+    }
+    else
+    {
+        argv[1] = idBuffer;
+        argv[2] = outputBuffer;
+        argv[3] = lambdaBuffer;
+        argv[4] = maliciousBuffer;
+        argv[5] = NULL;
+    }
+    
+    int pid = fork();
+    if (pid < 0)
+        exit(1);
+    if (pid == 0)
+        execv(calledFile, argv);
 }
 
 void shut_down_remote_actors(

+ 12 - 1
prsona/src/orchestratorMain.cpp

@@ -9,11 +9,20 @@
 #include <iostream>
 #include <fstream>
 #include <thread>
+#include <csignal>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "networkOrchestrator.hpp"
 
 using namespace std;
 
+void cleanup(int signum)
+{
+    while (waitpid(-1, NULL, WNOHANG) > 0) {}
+}
+
 /**
  * This program (bin/orchestrator) expects to be called as follows:
  * `bin/orchestrator <output> <servers_are_malicious>`
@@ -83,6 +92,8 @@ int main(int argc, char* argv[])
     size_t numServers = serverIPs.size();
     size_t numClients = clientIPs.size();
 
+    signal(SIGCHLD, cleanup);
+
     /*
      * ORCHESTRATOR SETUP CODE
      */
@@ -95,7 +106,7 @@ int main(int argc, char* argv[])
 
     vector<thread> serverStartup, clientStartup, clientReady;
     serverStartup.push_back(thread(start_remote_actor, targeter[dealerIP], true, "d", output, lambda, maliciousServers));
-    this_thread::sleep_for(HALF_SECOND);
+    this_thread::sleep_for(TWO_SECONDS);
 
     cout << "[ORC] Starting other servers." << endl;