|
|
|
|
@@ -171,10 +171,7 @@ void PIProcess::startProc(bool detached) {
|
|
|
|
|
if (g_in) PRIVATE->tf_in = freopen(f_in.path().data(), "r", stdin);
|
|
|
|
|
if (g_out) PRIVATE->tf_out = freopen(f_out.path().data(), "w", stdout);
|
|
|
|
|
if (g_err) PRIVATE->tf_err = freopen(f_err.path().data(), "w", stderr);
|
|
|
|
|
# ifndef WINDOWS
|
|
|
|
|
if (!wd.isEmpty())
|
|
|
|
|
if (!chdir(wd.data())) piCoutObj << "Error while set working directory";
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
GetStartupInfoA(&(PRIVATE->si));
|
|
|
|
|
piZeroMemory(PRIVATE->pi);
|
|
|
|
|
@@ -196,128 +193,150 @@ void PIProcess::startProc(bool detached) {
|
|
|
|
|
}
|
|
|
|
|
CloseHandle(PRIVATE->pi.hThread);
|
|
|
|
|
CloseHandle(PRIVATE->pi.hProcess);
|
|
|
|
|
} else
|
|
|
|
|
} else {
|
|
|
|
|
piCoutObj << "\"CreateProcess\" error: %1"_tr("PIProcess").arg(errorString());
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
|
|
// cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
|
|
|
|
if (execve(str.data(), (char * const *)argscc, (char * const *)envcc) < 0)
|
|
|
|
|
piCoutObj << "\"execve" << str << args << "\" error :" << errorString();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
piMinSleep();
|
|
|
|
|
// cout << "wait" << endl;
|
|
|
|
|
if (!detached) {
|
|
|
|
|
wait(&exit_code);
|
|
|
|
|
pid_ = 0;
|
|
|
|
|
if (!detached) PRIVATE->pid = pid_;
|
|
|
|
|
// cout << "wait done" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# endif
|
|
|
|
|
if (!detached) execFinished(str, exit_code);
|
|
|
|
|
is_exec = false;
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
delete[] a;
|
|
|
|
|
}
|
|
|
|
|
# endif
|
|
|
|
|
# ifndef WINDOWS
|
|
|
|
|
if (!wd.isEmpty()) {
|
|
|
|
|
if (!chdir(wd.data())) piCoutObj << "Error while set working directory";
|
|
|
|
|
}
|
|
|
|
|
// cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
|
|
|
|
if (execve(str.data(), (char * const *)argscc, (char * const *)envcc) < 0) {
|
|
|
|
|
piCoutObj << "\"execve" << str << args << "\" error :" << errorString();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
piMinSleep();
|
|
|
|
|
// cout << "wait" << endl;
|
|
|
|
|
if (!detached) {
|
|
|
|
|
wait(&exit_code);
|
|
|
|
|
pid_ = 0;
|
|
|
|
|
if (!detached) PRIVATE->pid = pid_;
|
|
|
|
|
// cout << "wait done" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::terminate() {
|
|
|
|
|
# endif
|
|
|
|
|
if (!detached) execFinished(str, exit_code);
|
|
|
|
|
is_exec = false;
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
if (is_exec)
|
|
|
|
|
if (!TerminateProcess(PRIVATE->pi.hProcess, 0)) return;
|
|
|
|
|
PRIVATE->pi.dwProcessId = 0;
|
|
|
|
|
delete[] a;
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PIByteArray PIProcess::readFile(PIFile & f, bool clear)
|
|
|
|
|
{
|
|
|
|
|
f.open(PIIODevice::ReadOnly);
|
|
|
|
|
const auto ret = f.readAll();
|
|
|
|
|
if (clear) {
|
|
|
|
|
f.clear();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::terminate() {
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
if (is_exec)
|
|
|
|
|
if (!TerminateProcess(PRIVATE->pi.hProcess, 0)) return;
|
|
|
|
|
PRIVATE->pi.dwProcessId = 0;
|
|
|
|
|
# else
|
|
|
|
|
if (is_exec) kill(PRIVATE->pid, SIGKILL);
|
|
|
|
|
PRIVATE->pid = 0;
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool PIProcess::waitForFinish() {
|
|
|
|
|
return PIThread::waitForFinish(1_m);
|
|
|
|
|
}
|
|
|
|
|
bool PIProcess::waitForFinish() {
|
|
|
|
|
return PIThread::waitForFinish(1_m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
|
|
|
|
|
PIProcess p;
|
|
|
|
|
p.args << program << args_;
|
|
|
|
|
p.startProc(true);
|
|
|
|
|
}
|
|
|
|
|
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
|
|
|
|
|
PIProcess p;
|
|
|
|
|
p.args << program << args_;
|
|
|
|
|
p.startProc(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int PIProcess::pID() const {
|
|
|
|
|
int PIProcess::pID() const {
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
return PRIVATE->pi.dwProcessId;
|
|
|
|
|
return PRIVATE->pi.dwProcessId;
|
|
|
|
|
# else
|
|
|
|
|
return PRIVATE->pid;
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PIByteArray PIProcess::readOutput(bool clear) {
|
|
|
|
|
return readFile(f_out, clear);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PIByteArray PIProcess::readError(bool clear) {
|
|
|
|
|
return readFile(f_err, clear);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int PIProcess::currentPID() {
|
|
|
|
|
int PIProcess::currentPID() {
|
|
|
|
|
# ifdef WINDOWS
|
|
|
|
|
return GetCurrentProcessId();
|
|
|
|
|
return GetCurrentProcessId();
|
|
|
|
|
# else
|
|
|
|
|
return getpid();
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIStringList PIProcess::currentEnvironment() {
|
|
|
|
|
PIStringList l;
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (environ[i] != 0) {
|
|
|
|
|
l << environ[i];
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIStringList PIProcess::currentEnvironment() {
|
|
|
|
|
PIStringList l;
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (environ[i] != 0) {
|
|
|
|
|
l << environ[i];
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
void PIProcess::run() {
|
|
|
|
|
startProc(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::run() {
|
|
|
|
|
startProc(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::removeEnvironmentVariable(const PIString & variable) {
|
|
|
|
|
PIString s;
|
|
|
|
|
for (int i = 0; i < env.size_s(); ++i) {
|
|
|
|
|
s = env[i];
|
|
|
|
|
if (s.left(s.find("=")).trimmed() == variable) {
|
|
|
|
|
env.remove(i);
|
|
|
|
|
--i;
|
|
|
|
|
}
|
|
|
|
|
void PIProcess::removeEnvironmentVariable(const PIString & variable) {
|
|
|
|
|
PIString s;
|
|
|
|
|
for (int i = 0; i < env.size_s(); ++i) {
|
|
|
|
|
s = env[i];
|
|
|
|
|
if (s.left(s.find("=")).trimmed() == variable) {
|
|
|
|
|
env.remove(i);
|
|
|
|
|
--i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PIProcess::setEnvironmentVariable(const PIString & variable, const PIString & value) {
|
|
|
|
|
PIString s, v;
|
|
|
|
|
for (int i = 0; i < env.size_s(); ++i) {
|
|
|
|
|
s = env[i];
|
|
|
|
|
v = s.left(s.find("=")).trimmed();
|
|
|
|
|
if (v == variable) {
|
|
|
|
|
env[i] = v + "=" + value;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
void PIProcess::setEnvironmentVariable(const PIString & variable, const PIString & value) {
|
|
|
|
|
PIString s, v;
|
|
|
|
|
for (int i = 0; i < env.size_s(); ++i) {
|
|
|
|
|
s = env[i];
|
|
|
|
|
v = s.left(s.find("=")).trimmed();
|
|
|
|
|
if (v == variable) {
|
|
|
|
|
env[i] = v + "=" + value;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
env << variable + "=" + value;
|
|
|
|
|
}
|
|
|
|
|
env << variable + "=" + value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIString PIProcess::getEnvironmentVariable(const PIString & variable) {
|
|
|
|
|
PIStringList env_ = currentEnvironment();
|
|
|
|
|
PIString s, v;
|
|
|
|
|
for (int i = 0; i < env_.size_s(); ++i) {
|
|
|
|
|
s = env_[i];
|
|
|
|
|
v = s.left(s.find("=")).trimmed();
|
|
|
|
|
if (v == variable) {
|
|
|
|
|
return s.right(s.size() - 1 - s.find("=")).trimmed();
|
|
|
|
|
}
|
|
|
|
|
PIString PIProcess::getEnvironmentVariable(const PIString & variable) {
|
|
|
|
|
PIStringList env_ = currentEnvironment();
|
|
|
|
|
PIString s, v;
|
|
|
|
|
for (int i = 0; i < env_.size_s(); ++i) {
|
|
|
|
|
s = env_[i];
|
|
|
|
|
v = s.left(s.find("=")).trimmed();
|
|
|
|
|
if (v == variable) {
|
|
|
|
|
return s.right(s.size() - 1 - s.find("=")).trimmed();
|
|
|
|
|
}
|
|
|
|
|
return PIString();
|
|
|
|
|
}
|
|
|
|
|
return PIString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // MICRO_PIP
|
|
|
|
|
|