| 36 | } |
| 37 | |
| 38 | CommandResult dispatch(int client_fd, const char* cmd_buffer, size_t cmd_size) override { |
| 39 | std::vector<std::string> cmd_parts = split(cmd_buffer, ' '); |
| 40 | if (cmd_parts.size() < 2) { |
| 41 | const char* error = "ERROR: Usage: l <filename>\n"; |
| 42 | write(client_fd, error, strlen(error)); |
| 43 | return CommandResult(false, "Invalid arguments"); |
| 44 | } |
| 45 | |
| 46 | std::string file_path = cmd_parts[1]; |
| 47 | |
| 48 | std::string lua_script = read_file(file_path.c_str()); |
| 49 | if (lua_script.empty()) { |
| 50 | std::string error = "ERROR: Cannot read file: " + file_path + "\n"; |
| 51 | write(client_fd, error.c_str(), error.length()); |
| 52 | return CommandResult(false, "File read failed"); |
| 53 | } |
| 54 | |
| 55 | int pid = CommandRegistry::instance().get_current_pid(); |
| 56 | if (pid <= 0) { |
| 57 | const char* error_msg = "ERROR: No target PID set. Please attach first.\n"; |
| 58 | write(client_fd, error_msg, strlen(error_msg)); |
| 59 | return CommandResult(false, "No target PID set"); |
| 60 | } |
| 61 | |
| 62 | SocketHelper& socket_helper = CommandRegistry::instance().get_socket_helper(); |
| 63 | int sock = socket_helper.ensure_connection(pid); |
| 64 | if (sock < 0) { |
| 65 | const char* error_msg = "ERROR: Failed to connect to agent\n"; |
| 66 | write(client_fd, error_msg, strlen(error_msg)); |
| 67 | return CommandResult(false, "Socket connection failed"); |
| 68 | } |
| 69 | |
| 70 | std::string hex = hex_encode(lua_script); |
| 71 | std::string command = "hexexec " + hex + "\n"; |
| 72 | socket_helper.send_data(command.c_str(), command.length()); |
| 73 | |
| 74 | // Spawn gate: resume frozen process after script data is buffered |
| 75 | int gated = CommandRegistry::instance().gated_pid; |
| 76 | if (gated > 0 && gated == pid) { |
| 77 | fprintf(stderr, "[spawn-gate] Resuming gated process (pid=%d)\n", gated); |
| 78 | kill(gated, SIGCONT); |
| 79 | CommandRegistry::instance().gated_pid = -1; |
| 80 | } |
| 81 | |
| 82 | int flags = fcntl(sock, F_GETFL, 0); |
| 83 | fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
| 84 | |
| 85 | char buffer[4096]; |
| 86 | bool script_done = false; |
| 87 | int timeout_count = 0; |
| 88 | const int max_timeout = 50; |
| 89 | |
| 90 | while (!script_done && timeout_count < max_timeout) { |
| 91 | struct pollfd pfd = {sock, POLLIN, 0}; |
| 92 | int ret = poll(&pfd, 1, 100); |
| 93 | |
| 94 | if (ret > 0 && (pfd.revents & POLLIN)) { |
| 95 | ssize_t n = recv(sock, buffer, sizeof(buffer) - 1, 0); |
nothing calls this directly
no test coverage detected