diff --git a/.clang-format b/.clang-format index 37743a5..5b16361 100644 --- a/.clang-format +++ b/.clang-format @@ -2,3 +2,4 @@ BasedOnStyle: LLVM IndentWidth: 4 FixNamespaceComments: false +ColumnLimit: 100000 \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 4b1909e..7a1c7a3 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -17,11 +17,9 @@ std::vector sk::config::loadHostsFromToml(const fs::path &confFile) { } toml::table &serverInfo = *serverInfoPtr; - if (serverInfo.get_as("ip") && - serverInfo.get_as("nbContainerMax")) { + if (serverInfo.get_as("ip") && serverInfo.get_as("nbContainerMax")) { std::string serverIp = serverInfo.get_as("ip")->get(); - int serverMaxContainers = - serverInfo.get_as("nbContainerMax")->get(); + int serverMaxContainers = serverInfo.get_as("nbContainerMax")->get(); hosts.push_back(sk::host(serverIp, serverMaxContainers)); } diff --git a/src/host.cpp b/src/host.cpp index 479281f..6b32fb8 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -1,23 +1,16 @@ #include "host.hpp" namespace sk { -host::host(const std::string &ip, unsigned int connectionsMax) - : ip{ip}, connections{0}, connectionsMax{connectionsMax}, runners{} {} +host::host(const std::string &ip, unsigned int connectionsMax) : ip{ip}, connections{0}, connectionsMax{connectionsMax}, runners{} {} -void host::addConnection(sk::runner& runner) { +void host::addConnection(sk::runner &runner) { runners.push(runner); connections += 1; } -const std::string& host::getIp() const { - return ip; -} +const std::string &host::getIp() const { return ip; } -unsigned int host::getNbConnections() const { - return connections; -} +unsigned int host::getNbConnections() const { return connections; } -unsigned int host::getNbConnectionsMax() const { - return connectionsMax; -} +unsigned int host::getNbConnectionsMax() const { return connectionsMax; } } diff --git a/src/host.hpp b/src/host.hpp index f5fe88f..a423692 100644 --- a/src/host.hpp +++ b/src/host.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include "runner.hpp" #include -#include "runner.hpp" +#include namespace sk { class host { @@ -15,16 +15,13 @@ class host { public: host(const std::string &ip, unsigned int connectionsMax); - void addConnection(sk::runner& runner); + void addConnection(sk::runner &runner); const std::string &getIp() const; unsigned int getNbConnections() const; unsigned int getNbConnectionsMax() const; - bool operator<(const host &other) const { - return (connectionsMax - connections) < - other.getNbConnectionsMax() - other.getNbConnections(); - } + bool operator<(const host &other) const { return (connectionsMax - connections) < other.getNbConnectionsMax() - other.getNbConnections(); } }; } diff --git a/src/main.cpp b/src/main.cpp index d761217..7268c51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,8 +20,7 @@ static constexpr uint32_t MIN_MESSAGE_LEN = JOB_ID_LEN + sizeof(uint32_t) * 2; sk::runner_backend detect_backend() { const char *const argv[] = {"docker", "stats", "--no-stream", nullptr}; pid_t pid; - if (posix_spawnp(&pid, argv[0], nullptr, nullptr, - const_cast(argv), nullptr) != 0) { + if (posix_spawnp(&pid, argv[0], nullptr, nullptr, const_cast(argv), nullptr) != 0) { return sk::runner_backend::BubbleWrap; } int status = 0; @@ -35,8 +34,7 @@ sk::runner_backend detect_backend() { } int main() { - std::vector listsHosts = - sk::config::loadHostsFromToml("../conf.toml"); + std::vector listsHosts = sk::config::loadHostsFromToml("../conf.toml"); std::priority_queue hosts(listsHosts.begin(), listsHosts.end()); std::queue programQueue; @@ -61,25 +59,20 @@ int main() { std::cerr << "Invalid request" << std::endl; break; } + std::string_view jobId(static_cast(request.data()), JOB_ID_LEN); - uint32_t imageLen = - sk::read_uint32(static_cast(request.data()) + JOB_ID_LEN); - uint32_t codeLen = - sk::read_uint32(static_cast(request.data()) + JOB_ID_LEN + sizeof(uint32_t)); + uint32_t imageLen = sk::read_uint32(static_cast(request.data()) + JOB_ID_LEN); + uint32_t codeLen = sk::read_uint32(static_cast(request.data()) + JOB_ID_LEN + sizeof(uint32_t)); + if (request.size() < MIN_MESSAGE_LEN + imageLen + codeLen) { std::cerr << "Invalid request" << std::endl; break; } - std::string imageString(static_cast(request.data()) + - MIN_MESSAGE_LEN, - imageLen); - std::string requestString(static_cast(request.data()) + - MIN_MESSAGE_LEN + imageLen, - codeLen); + std::string imageString(static_cast(request.data()) + MIN_MESSAGE_LEN, imageLen); + std::string requestString(static_cast(request.data()) + MIN_MESSAGE_LEN + imageLen, codeLen); std::cout << "Executing " << codeLen << " bytes code.\n"; - sk::program program{requestString, - "ghcr.io/moshell-lang/moshell:master"}; + sk::program program{requestString, imageString}; sk::run_result result = runner.run_blocking(program); std::cout << "Result: " << result.out << std::endl; @@ -87,8 +80,7 @@ int main() { // Send the job id and result.out to sink zmq::message_t reply(JOB_ID_LEN + result.out.size()); memcpy(reply.data(), jobId.data(), JOB_ID_LEN); - memcpy(static_cast(reply.data()) + JOB_ID_LEN, result.out.data(), - result.out.size()); + memcpy(static_cast(reply.data()) + JOB_ID_LEN, result.out.data(), result.out.size()); sender.send(reply, zmq::send_flags::none); } return 0; diff --git a/src/network.cpp b/src/network.cpp index 8aaa451..d5d3a0c 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,10 +1,5 @@ #include "network.hpp" namespace sk { -uint32_t read_uint32(const char *buffer) { - return static_cast(buffer[3]) | - static_cast(buffer[2]) << 8 | - static_cast(buffer[1]) << 16 | - static_cast(buffer[0]) << 24; -} +uint32_t read_uint32(const char *buffer) { return static_cast(buffer[3]) | static_cast(buffer[2]) << 8 | static_cast(buffer[1]) << 16 | static_cast(buffer[0]) << 24; } } diff --git a/src/runner.cpp b/src/runner.cpp index a3ad56d..f473c0c 100644 --- a/src/runner.cpp +++ b/src/runner.cpp @@ -30,33 +30,15 @@ run_result runner::run_blocking(const program &program) { posix_spawn_file_actions_addclose(&actions, in_pipe[0]); posix_spawn_file_actions_addclose(&actions, out_pipe[1]); posix_spawn_file_actions_addclose(&actions, err_pipe[1]); - const char *const docker_args[] = {"docker", - "run", - "--rm", - "-i", - "--pull=never", - "--cap-drop=ALL", - "--network=none", - "--memory=64m", - "--memory-swap=64m", - "--pids-limit=128", - program.image.c_str(), - nullptr}; - const char *const bwrap_args[] = { - "bwrap", "--ro-bind", "/usr", "/usr", "--dir", - "/tmp", "--dir", "/var", "--proc", "/proc", - "--dev", "/dev", "--symlink", "usr/lib", "/lib", - "--symlink", "usr/lib64", "/lib64", "--symlink", "usr/bin", - "/bin", "--symlink", "usr/sbin", "/sbin", "--unshare-all", - "/bin/sh", nullptr}; + const char *const docker_args[] = {"docker", "run", "--rm", "-i", "--pull=never", "--cap-drop=ALL", "--network=none", "--memory=64m", "--memory-swap=64m", "--pids-limit=128", program.image.c_str(), nullptr}; + const char *const bwrap_args[] = {"bwrap", "--ro-bind", "/usr", "/usr", "--dir", "/tmp", "--dir", "/var", "--proc", "/proc", "--dev", "/dev", "--symlink", "usr/lib", "/lib", "--symlink", "usr/lib64", "/lib64", "--symlink", "usr/bin", "/bin", "--symlink", "usr/sbin", "/sbin", "--unshare-all", "/bin/sh", nullptr}; const char *const *args = docker_args; if (backend == runner_backend::BubbleWrap) { args = bwrap_args; } pid_t pid; int exit_code; - if (posix_spawnp(&pid, args[0], &actions, nullptr, - const_cast(args), nullptr) != 0) { + if (posix_spawnp(&pid, args[0], &actions, nullptr, const_cast(args), nullptr) != 0) { throw std::system_error{errno, std::generic_category()}; } @@ -70,19 +52,16 @@ run_result runner::run_blocking(const program &program) { std::array buffer{}; std::string out; std::string err; - std::array plist = {pollfd{out_pipe[0], POLLIN, 0}, - pollfd{err_pipe[0], POLLIN, 0}}; + std::array plist = {pollfd{out_pipe[0], POLLIN, 0}, pollfd{err_pipe[0], POLLIN, 0}}; while (poll(plist.data(), plist.size(), /*timeout*/ -1) > 0) { if (plist[0].revents & POLLIN) { - ssize_t bytes_read = - read(out_pipe[0], buffer.data(), buffer.size()); + ssize_t bytes_read = read(out_pipe[0], buffer.data(), buffer.size()); if (bytes_read == -1) { throw std::system_error{errno, std::generic_category()}; } out.append(buffer.data(), bytes_read); } else if (plist[1].revents & POLLIN) { - ssize_t bytes_read = - read(err_pipe[0], buffer.data(), buffer.size()); + ssize_t bytes_read = read(err_pipe[0], buffer.data(), buffer.size()); if (bytes_read == -1) { throw std::system_error{errno, std::generic_category()}; }