diff --git a/CMakeLists.txt b/CMakeLists.txt index 0319c611..8d75b111 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ add_executable(hpcore src/status.cpp src/consensus.cpp src/main.cpp + src/hpsh/hpsh.cpp ) target_link_libraries(hpcore killswitch diff --git a/src/hpsh/hpsh.cpp b/src/hpsh/hpsh.cpp new file mode 100644 index 00000000..e7b5e30b --- /dev/null +++ b/src/hpsh/hpsh.cpp @@ -0,0 +1,92 @@ +#include "../conf.hpp" +#include "../util/util.hpp" + +namespace hpsh +{ + pid_t hpsh_pid; + static int fd1[2]; + static int fd2[2]; + + int deinit() + { + //kill(hpsh_pid, SIGTERM); + close(fd1[0]); + close(fd1[1]); + close(fd2[0]); + close(fd1[1]); + + LOG_INFO << "HPSH stopped."; + + return 0; + } + int init() + { + LOG_INFO << "Initializing HPSH"; + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd1) == -1 || socketpair(AF_UNIX, SOCK_STREAM, 0, fd2) == -1) + { + return -1; + } + + pid_t pid = fork(); + if (pid == -1) + { + return -1; + } + + if (pid == 0) + { + hpsh_pid = getpid(); + char cfd1[10], cfd2[10]; + snprintf(cfd1, 10, "%d", fd1[0]); + snprintf(cfd2, 10, "%d", fd2[1]); + + char *argv[] = {const_cast(conf::ctx.hpsh_exe_path.c_str()), (char *)("-s1"), cfd1, (char *)("-s2"), cfd2, NULL}; + LOG_DEBUG << "Starting HPSH Executable"; + execv(argv[0], argv); + LOG_DEBUG << "Failed to execute hpsh"; + exit(EXIT_FAILURE); + } + else + { + close(fd1[0]); + close(fd2[1]); + + return 0; + } + } + + std::string serve(const char *message) + { + char buffer[1024]; + + ssize_t bytes_written = write(fd1[1], message, strlen(message)); + if (bytes_written == -1) { + perror("Error when writing to HPSH socket"); + } + + LOG_DEBUG << "\nMessage sent from hpcore: " << message; + + while (true) + { + int bytesRead; + bytesRead = read(fd2[0], buffer, sizeof(buffer)); + if (bytesRead < 0) + { + // Handle read error + perror("read"); + return "error when reading"; + } + + + buffer[bytesRead] = '\0'; // Null-terminate the buffer + + LOG_DEBUG << "\nMessage received in hpcore: " << buffer; + if(bytesRead<1024){ + break; + } + + } + return buffer; + } +} \ No newline at end of file diff --git a/src/hpsh/hpsh.hpp b/src/hpsh/hpsh.hpp new file mode 100644 index 00000000..4d9bad19 --- /dev/null +++ b/src/hpsh/hpsh.hpp @@ -0,0 +1,14 @@ +#ifndef HPSH_H +#define HPSH_H + +#include "../conf.hpp" +#include "../util/util.hpp" + +namespace hpsh +{ + int deinit(); + int init(); + std::string serve(const char* command); +} + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bc711b27..4f50fae1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include "ledger/ledger.hpp" #include "unl.hpp" #include "killswitch/killswitch.h" +#include "hpsh/hpsh.hpp" /** * Parses CLI args and extracts HotPocket command and parameters given. @@ -75,6 +76,7 @@ void deinit() sc::deinit(); ledger::deinit(); conf::deinit(); + hpsh::deinit(); } void sig_exit_handler(int signum) @@ -213,7 +215,8 @@ int main(int argc, char **argv) consensus::init() == -1 || read_req::init() == -1 || p2p::init() == -1 || - usr::init() == -1) + usr::init() == -1 || + hpsh::init() == -1) { deinit(); return -1; diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 8a514ee2..87655008 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -17,6 +17,7 @@ #include "user_input.hpp" #include "read_req.hpp" #include "input_nonce_map.hpp" +#include "../hpsh/hpsh.hpp" namespace usr { @@ -278,91 +279,9 @@ namespace usr if (parser.extract_shell_input(id, content) != -1) { - int p1[2] ; // 0,1 are hpcore to hphs - int p2[2] ; // 2,3 are hpsh to hpcore - if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, p1)) - perror("could not create unix domain socket pair"); - - if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, p2)) - perror("could not create unix domain socket pair"); - - pid_t pid = -1; - - pid = fork(); - - if (pid == -1) - { - perror("Error occurred when forking"); - return -1; - } - if (pid != 0) - { - // parent process - close(p1[0]); - close(p2[1]); - LOG_INFO << "parent: Received Shell Input.\n"; - LOG_INFO << "parent: User PubKey:" << user.pubkey << "\n"; - LOG_INFO << "parent: ID:" << id << "\n"; - LOG_INFO << "parent: Content:" << content << "\n"; - LOG_INFO << "parent: writing content to pipe\n"; - ssize_t bytes_written = write(p1[1], content.c_str(), content.size()); - if (bytes_written == -1) - { - perror("write to pipe failed"); - // handle the error appropriately - } - char buffer[1024] = {0}; - if(read(p2[0], &buffer, sizeof(buffer))==-1){ - perror("Error in reading to the fd"); - } - std::cout <<"bufferread"<< buffer << std::endl; - - LOG_INFO << "parent: closing pipe\n"; - close(p1[1]); - close(p2[0]); - LOG_INFO << "parent: closed\n"; - } - else - { - - // child process - close(p1[1]); - close(p2[0]); - char read_end[16]; - snprintf(read_end, sizeof(read_end), "%d", p1[0]); - char write_end[16]; - snprintf(write_end, sizeof(write_end), "%d", p2[1]); - std::string receivedContent; - LOG_INFO << "child: reading from pipe:\n"; - const int BUFFER_SIZE = 1024; - char buffer[BUFFER_SIZE]; - - ssize_t bytes_read = read(p1[0], buffer, BUFFER_SIZE); - if (bytes_read == -1) - { - perror("read from pipe failed"); - // handle the error appropriately - } - else - { - buffer[bytes_read] = '\0'; // Null-terminate the string - receivedContent = buffer; - LOG_INFO << "child: Content:" << receivedContent << "\n"; - } - - receivedContent = "\"" + receivedContent + "\""; - - char *passed_command = new char[receivedContent.length() + 1]; - std::strcpy(passed_command, receivedContent.c_str()); - - char *args[] = {const_cast(conf::ctx.hpsh_exe_path.c_str()), passed_command,read_end, write_end, NULL}; - if (execv(conf::ctx.hpsh_exe_path.c_str(), args) == -1) - { - perror("Error when executing HPSH"); - } - close(p1[0]); - close(p2[1]); - } + LOG_INFO << "shell input received:" << content; + std::string response = hpsh::serve(content.c_str()); + LOG_INFO << "response: " << response; return 0; } diff --git a/test/bin/hpsh b/test/bin/hpsh index cffa8ef3..d58f320b 100755 Binary files a/test/bin/hpsh and b/test/bin/hpsh differ