git-svn-id: svn://db.shs.com.ru/pip@361 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-04-14 14:15:22 +00:00
parent db1410d918
commit 8cdf50a45f
25 changed files with 175 additions and 64 deletions

View File

@@ -17,10 +17,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piprocess.h"
#include "pibase.h"
#ifdef WINDOWS
# include <winbase.h>
#else
# include <windef.h>
# include <winbase.h>
#endif
#include "piprocess.h"
#ifndef WINDOWS
# include <sys/wait.h>
# include <csignal>
#endif
@@ -31,12 +34,23 @@
# include <unistd.h>
#endif
PRIVATE_DEFINITION_START(PIProcess)
#ifdef WINDOWS
STARTUPINFOA si;
PROCESS_INFORMATION pi;
#else
pid_t pid;
#endif
PRIVATE_DEFINITION_END(PIProcess)
PIProcess::PIProcess(): PIThread() {
exit_code = -1;
#ifdef WINDOWS
pi.dwProcessId = 0;
PRIVATE->pi.dwProcessId = 0;
#else
pid = 0;
PRIVATE->pid = 0;
#endif
is_exec = false;
g_in = g_out = g_err = false;
@@ -128,7 +142,7 @@ void PIProcess::startProc(bool detached) {
if (!detached) execStarted(str);
#ifndef WINDOWS
int pid_ = fork();
if (!detached) pid = pid_;
if (!detached) PRIVATE->pid = pid_;
if (pid_ == 0) {
#endif
tf_in = tf_out = tf_err = 0;
@@ -141,8 +155,8 @@ void PIProcess::startProc(bool detached) {
if (!wd.isEmpty()) chdir(wd.data());
#endif
#ifdef WINDOWS
GetStartupInfoA(&si);
memset(&pi, 0, sizeof(pi));
GetStartupInfoA(&(PRIVATE->si));
memset(&(PRIVATE->pi), 0, sizeof(PRIVATE->pi));
if(CreateProcessA(0, // No module name (use command line)
a, // Command line
0, // Process handle not inheritable
@@ -151,12 +165,12 @@ void PIProcess::startProc(bool detached) {
detached ? DETACHED_PROCESS/*CREATE_NEW_CONSOLE*/ : 0, // Creation flags
0,//e, // Use environment
wd.isEmpty() ? 0 : wd.data(), // Use working directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
&(PRIVATE->si), // Pointer to STARTUPINFO structure
&(PRIVATE->pi))) // Pointer to PROCESS_INFORMATION structure
{
if (!detached) WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
if (!detached) WaitForSingleObject(PRIVATE->pi.hProcess, INFINITE);
CloseHandle(PRIVATE->pi.hThread);
CloseHandle(PRIVATE->pi.hProcess);
} else
piCoutObj << "\"CreateProcess\" error, " << errorString();
#else
@@ -173,7 +187,7 @@ void PIProcess::startProc(bool detached) {
if (tf_out != 0) fclose(tf_out);
if (tf_err != 0) fclose(tf_err);*/
pid_ = 0;
if (!detached) pid = pid_;
if (!detached) PRIVATE->pid = pid_;
//cout << "wait done" << endl;
}
}
@@ -195,13 +209,13 @@ void PIProcess::startProc(bool detached) {
void PIProcess::terminate() {
#ifdef WINDOWS
if (is_exec)
if (!TerminateProcess(pi.hProcess, 0))
if (!TerminateProcess(PRIVATE->pi.hProcess, 0))
return;
pi.dwProcessId = 0;
PRIVATE->pi.dwProcessId = 0;
#else
if (is_exec)
kill(pid, SIGKILL);
pid = 0;
kill(PRIVATE->pid, SIGKILL);
PRIVATE->pid = 0;
#endif
}
@@ -215,9 +229,9 @@ void PIProcess::execIndependent(const PIString & program, const PIStringList & a
int PIProcess::pID() const {
#ifdef WINDOWS
return pi.dwProcessId;
return PRIVATE->pi.dwProcessId;
#else
return pid;
return PRIVATE->pid;
#endif
}