Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix process: multiple changes #3304

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions CodeLite/UnixProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/syscall.h>

UnixProcess::UnixProcess(wxEvtHandler* owner, const wxArrayString& args)
: m_owner(owner)
Expand All @@ -35,10 +36,16 @@ UnixProcess::UnixProcess(wxEvtHandler* owner, const wxArrayString& args)
m_childStderr.Close();

// prevent descriptor leak into child process
#ifdef SYS_close_range
close_range(3, ~0U, 0);
#elif defined(SYS_closefrom)
closefrom(3);
#else
const int fd_max = (sysconf(_SC_OPEN_MAX) != -1 ? sysconf(_SC_OPEN_MAX) : FD_SETSIZE);
for (int fd = 3; fd < fd_max; fd++) {
close(fd);
}
#endif

char** argv = new char*[args.size() + 1];
for(size_t i = 0; i < args.size(); ++i) {
Expand Down Expand Up @@ -81,41 +88,31 @@ UnixProcess::~UnixProcess()
Stop();
Wait();
}
#define CHUNK_SIZE 1024
#define MAX_BUFF_SIZE (1024 * 2048)

bool UnixProcess::ReadAll(int fd, std::string& content, int timeoutMilliseconds)
{
fd_set rset;
char buff[CHUNK_SIZE];
char buff[65536];
FD_ZERO(&rset);
FD_SET(fd, &rset);

int seconds = timeoutMilliseconds / 1000;
int ms = timeoutMilliseconds % 1000;

struct timeval tv = { seconds, ms * 1000 }; // 10 milliseconds timeout
while(true) {
int rc = ::select(fd + 1, &rset, nullptr, nullptr, &tv);
if(rc > 0) {
int rc = ::select(fd + 1, &rset, nullptr, nullptr, &tv);
if(rc > 0) {
for (;;) {
int len = read(fd, buff, (sizeof(buff) - 1));
if(len > 0) {
buff[len] = 0;
content.append(buff);
if(content.length() >= MAX_BUFF_SIZE) {
return true;
}
// clear the tv struct so next select() call will return immediately
tv.tv_usec = 0;
tv.tv_sec = 0;
FD_ZERO(&rset);
FD_SET(fd, &rset);
continue;
}
} else if(rc == 0) {
// timeout
return true;
if(len <= 0)
break;
buff[len] = 0;
content.append(buff);
}
break;
return true;
} else if(rc == 0) {
// timeout
return true;
}
// error
return false;
Expand All @@ -125,7 +122,7 @@ bool UnixProcess::Write(int fd, const std::string& message, std::atomic_bool& sh
{
int bytes = 0;
std::string tmp = message;
const int chunkSize = 4096;
const int chunkSize = 65536;
while(!tmp.empty() && !shutdown.load()) {
errno = 0;
bytes = ::write(fd, tmp.c_str(), tmp.length() > chunkSize ? chunkSize : tmp.length());
Expand Down Expand Up @@ -191,6 +188,9 @@ void UnixProcess::StartReaderThread()
{
m_readerThread = new std::thread(
[](UnixProcess* process, int stdoutFd, int stderrFd) {
fcntl(stdoutFd, F_SETFL, O_NONBLOCK);
fcntl(stderrFd, F_SETFL, O_NONBLOCK);

while(!process->m_goingDown.load()) {
std::string content;
if(!ReadAll(stdoutFd, content, 10)) {
Expand Down
23 changes: 18 additions & 5 deletions codelite-stdio/UnixProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "file_logger.h"
#include <cl_command_event.h>
#include <processreaderthread.h>
Expand All @@ -25,10 +26,16 @@ UnixProcess::UnixProcess(wxEvtHandler* owner, const wxArrayString& args)
m_childStderr.close();

// prevent descriptor leak into child process
#ifdef SYS_close_range
close_range(3, ~0U, 0);
#elif defined(SYS_closefrom)
closefrom(3);
#else
const int fd_max = (sysconf(_SC_OPEN_MAX) != -1 ? sysconf(_SC_OPEN_MAX) : FD_SETSIZE);
for (int fd = 3; fd < fd_max; fd++) {
close(fd);
}
#endif

char** argv = new char*[args.size() + 1];
for(size_t i = 0; i < args.size(); ++i) {
Expand Down Expand Up @@ -69,7 +76,7 @@ UnixProcess::~UnixProcess()
bool UnixProcess::ReadAll(int fd, std::string& content, int timeoutMilliseconds)
{
fd_set rset;
char buff[1024];
char buff[65536];
FD_ZERO(&rset);
FD_SET(fd, &rset);

Expand All @@ -79,11 +86,14 @@ bool UnixProcess::ReadAll(int fd, std::string& content, int timeoutMilliseconds)
struct timeval tv = { seconds, ms * 1000 }; // 10 milliseconds timeout
int rc = ::select(fd + 1, &rset, nullptr, nullptr, &tv);
if(rc > 0) {
memset(buff, 0, sizeof(buff));
if(read(fd, buff, (sizeof(buff) - 1)) > 0) {
for (;;) {
int len = read(fd, buff, (sizeof(buff) - 1));
if(len <= 0)
break;
buff[len] = 0;
content.append(buff);
return true;
}
return true;
} else if(rc == 0) {
// timeout
return true;
Expand All @@ -96,7 +106,7 @@ bool UnixProcess::Write(int fd, const std::string& message, std::atomic_bool& sh
{
int bytes = 0;
std::string tmp = message;
const int chunkSize = 4096;
const int chunkSize = 65536;
while(!tmp.empty() && !shutdown.load()) {
errno = 0;
bytes = ::write(fd, tmp.c_str(), tmp.length() > chunkSize ? chunkSize : tmp.length());
Expand Down Expand Up @@ -154,6 +164,9 @@ void UnixProcess::StartReaderThread()
{
m_readerThread = new std::thread(
[](UnixProcess* process, int stdoutFd, int stderrFd) {
fcntl(stdoutFd, F_SETFL, O_NONBLOCK);
fcntl(stderrFd, F_SETFL, O_NONBLOCK);

while(!process->m_goingDown.load()) {
std::string content;
if(!ReadAll(stdoutFd, content, 10)) {
Expand Down