|
| 1 | +// backend_ipc.cpp - generic backend IPC process launcher. |
| 2 | + |
| 3 | +#include "backend_ipc.h" |
| 4 | +#include "io_utils.h" |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstdlib> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#if !defined(_WIN32) |
| 13 | +# include <cerrno> |
| 14 | +# include <cstring> |
| 15 | +# include <sys/stat.h> |
| 16 | +# include <sys/wait.h> |
| 17 | +# include <unistd.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace dflash::common { |
| 21 | + |
| 22 | +const char * backend_ipc_mode_name(BackendIpcMode mode) { |
| 23 | + switch (mode) { |
| 24 | + case BackendIpcMode::DFlashDraft: return "dflash-draft"; |
| 25 | + } |
| 26 | + return "unknown"; |
| 27 | +} |
| 28 | + |
| 29 | +bool parse_backend_ipc_mode(const std::string & value, BackendIpcMode & out) { |
| 30 | + if (value == "dflash-draft") { |
| 31 | + out = BackendIpcMode::DFlashDraft; |
| 32 | + return true; |
| 33 | + } |
| 34 | + return false; |
| 35 | +} |
| 36 | + |
| 37 | +bool BackendIpcProcess::start(const BackendIpcLaunchConfig & cfg) { |
| 38 | +#if defined(_WIN32) |
| 39 | + (void)cfg; |
| 40 | + std::fprintf(stderr, "Backend IPC is only implemented on POSIX hosts\n"); |
| 41 | + return false; |
| 42 | +#else |
| 43 | + close(); |
| 44 | + if (cfg.bin.empty() || cfg.payload_path.empty()) return false; |
| 45 | + if (!init_work_dir(cfg.work_dir)) return false; |
| 46 | + |
| 47 | + int cmd_pipe[2] = {-1, -1}; |
| 48 | + int stream_pipe[2] = {-1, -1}; |
| 49 | + if (::pipe(cmd_pipe) != 0 || ::pipe(stream_pipe) != 0) { |
| 50 | + std::fprintf(stderr, "backend-ipc pipe failed: %s\n", std::strerror(errno)); |
| 51 | + if (cmd_pipe[0] >= 0) ::close(cmd_pipe[0]); |
| 52 | + if (cmd_pipe[1] >= 0) ::close(cmd_pipe[1]); |
| 53 | + if (stream_pipe[0] >= 0) ::close(stream_pipe[0]); |
| 54 | + if (stream_pipe[1] >= 0) ::close(stream_pipe[1]); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + pid_ = ::fork(); |
| 59 | + if (pid_ < 0) { |
| 60 | + std::fprintf(stderr, "backend-ipc fork failed: %s\n", std::strerror(errno)); |
| 61 | + ::close(cmd_pipe[0]); ::close(cmd_pipe[1]); |
| 62 | + ::close(stream_pipe[0]); ::close(stream_pipe[1]); |
| 63 | + pid_ = -1; |
| 64 | + return false; |
| 65 | + } |
| 66 | + if (pid_ == 0) { |
| 67 | + ::dup2(cmd_pipe[0], STDIN_FILENO); |
| 68 | + ::close(cmd_pipe[0]); |
| 69 | + ::close(cmd_pipe[1]); |
| 70 | + ::close(stream_pipe[0]); |
| 71 | + |
| 72 | + std::vector<std::string> argv_storage; |
| 73 | + argv_storage.reserve(cfg.args.size() + 5); |
| 74 | + argv_storage.emplace_back(cfg.bin); |
| 75 | + argv_storage.emplace_back( |
| 76 | + std::string("--backend-ipc-mode=") + backend_ipc_mode_name(cfg.mode)); |
| 77 | + argv_storage.emplace_back(cfg.payload_path); |
| 78 | + for (const std::string & arg : cfg.args) argv_storage.emplace_back(arg); |
| 79 | + argv_storage.emplace_back("--stream-fd=" + std::to_string(stream_pipe[1])); |
| 80 | + |
| 81 | + std::vector<char *> argv; |
| 82 | + argv.reserve(argv_storage.size() + 1); |
| 83 | + for (std::string & arg : argv_storage) argv.push_back(arg.data()); |
| 84 | + argv.push_back(nullptr); |
| 85 | + ::execv(cfg.bin.c_str(), argv.data()); |
| 86 | + std::fprintf(stderr, "backend-ipc exec failed: %s: %s\n", |
| 87 | + cfg.bin.c_str(), std::strerror(errno)); |
| 88 | + _exit(127); |
| 89 | + } |
| 90 | + |
| 91 | + ::close(cmd_pipe[0]); |
| 92 | + ::close(stream_pipe[1]); |
| 93 | + stream_fd_ = stream_pipe[0]; |
| 94 | + cmd_ = ::fdopen(cmd_pipe[1], "w"); |
| 95 | + if (!cmd_) { |
| 96 | + std::fprintf(stderr, "backend-ipc fdopen failed: %s\n", std::strerror(errno)); |
| 97 | + ::close(cmd_pipe[1]); |
| 98 | + close(); |
| 99 | + return false; |
| 100 | + } |
| 101 | + int32_t status = -1; |
| 102 | + if (!read_exact_fd(stream_fd_, &status, sizeof(status)) || status != 0) { |
| 103 | + std::fprintf(stderr, "backend-ipc daemon did not become ready (status=%d)\n", status); |
| 104 | + close(); |
| 105 | + return false; |
| 106 | + } |
| 107 | + active_ = true; |
| 108 | + std::printf("[backend-ipc] ready mode=%s bin=%s work_dir=%s\n", |
| 109 | + backend_ipc_mode_name(cfg.mode), cfg.bin.c_str(), work_dir_.c_str()); |
| 110 | + return true; |
| 111 | +#endif |
| 112 | +} |
| 113 | + |
| 114 | +void BackendIpcProcess::close() { |
| 115 | +#if !defined(_WIN32) |
| 116 | + if (cmd_) { |
| 117 | + std::fclose(cmd_); |
| 118 | + cmd_ = nullptr; |
| 119 | + } |
| 120 | + if (stream_fd_ >= 0) { |
| 121 | + ::close(stream_fd_); |
| 122 | + stream_fd_ = -1; |
| 123 | + } |
| 124 | + if (pid_ > 0) { |
| 125 | + int status = 0; |
| 126 | + ::waitpid(pid_, &status, 0); |
| 127 | + pid_ = -1; |
| 128 | + } |
| 129 | + if (owns_work_dir_ && !work_dir_.empty()) { |
| 130 | + ::rmdir(work_dir_.c_str()); |
| 131 | + } |
| 132 | +#endif |
| 133 | + active_ = false; |
| 134 | + owns_work_dir_ = false; |
| 135 | + work_dir_.clear(); |
| 136 | + seq_ = 0; |
| 137 | +} |
| 138 | + |
| 139 | +std::string BackendIpcProcess::next_path(const char * prefix) { |
| 140 | + return work_dir_ + "/" + prefix + "_" + std::to_string(seq_++) + ".bin"; |
| 141 | +} |
| 142 | + |
| 143 | +#if !defined(_WIN32) |
| 144 | +bool BackendIpcProcess::init_work_dir(const std::string & requested) { |
| 145 | + if (!requested.empty()) { |
| 146 | + work_dir_ = requested; |
| 147 | + owns_work_dir_ = false; |
| 148 | + if (::mkdir(work_dir_.c_str(), 0700) != 0 && errno != EEXIST) { |
| 149 | + std::fprintf(stderr, "backend-ipc mkdir failed: %s: %s\n", |
| 150 | + work_dir_.c_str(), std::strerror(errno)); |
| 151 | + return false; |
| 152 | + } |
| 153 | + return true; |
| 154 | + } |
| 155 | + const char * tmp = std::getenv("TMPDIR"); |
| 156 | + std::string templ = std::string(tmp && *tmp ? tmp : "/tmp") + |
| 157 | + "/backend-ipc-XXXXXX"; |
| 158 | + std::vector<char> buf(templ.begin(), templ.end()); |
| 159 | + buf.push_back('\0'); |
| 160 | + char * dir = ::mkdtemp(buf.data()); |
| 161 | + if (!dir) { |
| 162 | + std::fprintf(stderr, "backend-ipc mkdtemp failed: %s\n", std::strerror(errno)); |
| 163 | + return false; |
| 164 | + } |
| 165 | + work_dir_ = dir; |
| 166 | + owns_work_dir_ = true; |
| 167 | + return true; |
| 168 | +} |
| 169 | +#endif |
| 170 | + |
| 171 | +} // namespace dflash::common |
0 commit comments