chore: WlClipboard remove unused executeCommand

This commit is contained in:
sithlord48
2025-11-17 20:14:34 -05:00
committed by Chris Rizzitello
parent c5f7a3792a
commit 7b0e8e8188
2 changed files with 0 additions and 115 deletions

View File

@ -402,118 +402,6 @@ std::string WlClipboard::get(Format format) const
return data;
}
std::string WlClipboard::executeCommand(const std::vector<const char *> &args) const
{
int pipefd[2];
if (pipe(pipefd) == -1) {
LOG_WARN("failed to create pipe");
return std::string();
}
FdGuard readFd(pipefd[0]);
FdGuard writeFd(pipefd[1]);
// Set FD_CLOEXEC on pipe file descriptors
fcntl(readFd.get(), F_SETFD, FD_CLOEXEC);
fcntl(writeFd.get(), F_SETFD, FD_CLOEXEC);
// Set up file actions for posix_spawn
posix_spawn_file_actions_t fileActions;
posix_spawn_file_actions_init(&fileActions);
// Redirect stdout to pipe write end
posix_spawn_file_actions_adddup2(&fileActions, writeFd.get(), STDOUT_FILENO);
// Redirect stderr to /dev/null
posix_spawn_file_actions_addopen(&fileActions, STDERR_FILENO, "/dev/null", O_WRONLY, 0);
// Close pipe file descriptors in child
posix_spawn_file_actions_addclose(&fileActions, readFd.get());
posix_spawn_file_actions_addclose(&fileActions, writeFd.get());
extern char **environ;
pid_t pid;
int spawnResult = posix_spawnp(&pid, args[0], &fileActions, nullptr, const_cast<char *const *>(args.data()), environ);
posix_spawn_file_actions_destroy(&fileActions);
if (spawnResult != 0) {
LOG_WARN("failed to spawn process: %s", strerror(spawnResult));
return std::string();
}
ProcessGuard processGuard(pid);
// Parent process - close write end
writeFd.close();
std::string result;
char buffer[4096];
// Use poll to read with timeout
struct pollfd pfd;
pfd.fd = readFd.get();
pfd.events = POLLIN;
auto start = std::chrono::steady_clock::now();
while (true) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
int remainingTimeout = kCommandTimeout - elapsed;
if (remainingTimeout <= 0) {
break; // Timeout
}
int pollResult = poll(&pfd, 1, remainingTimeout);
if (pollResult == 0) {
break; // Timeout
} else if (pollResult < 0) {
if (errno == EINTR) {
continue; // Interrupted, try again
}
break; // Error
}
if (pfd.revents & POLLIN) {
ssize_t bytesRead = read(readFd.get(), buffer, sizeof(buffer));
if (bytesRead > 0) {
result.append(buffer, bytesRead);
} else if (bytesRead == 0) {
break; // EOF
} else if (errno != EINTR) {
break; // Error
}
}
if (pfd.revents & (POLLHUP | POLLERR)) {
break; // Pipe closed or error
}
}
readFd.close();
int status;
bool processFinished = waitpidWithTimeout(pid, &status, 1000); // Give 1 more second
if (processFinished) {
processGuard.release();
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
// Remove trailing newline if present
if (!result.empty() && result.back() == '\n') {
result.pop_back();
}
return result;
} else {
LOG_DEBUG1("command failed with status %d", WEXITSTATUS(status));
return std::string();
}
} else {
LOG_WARN("process did not terminate properly");
return std::string();
}
}
bool WlClipboard::executeCommandWithInput(const std::vector<const char *> &args, const std::string &input) const
{
int pipefd[2];

View File

@ -65,9 +65,6 @@ public:
std::string get(Format format) const override;
private:
//! Execute a command and return its output
std::string executeCommand(const std::vector<const char *> &args) const;
//! Execute a command with input data
bool executeCommandWithInput(const std::vector<const char *> &args, const std::string &input) const;