version 0.5.0_alpha
git-svn-id: svn://db.shs.com.ru/pip@8 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process
|
||||
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2015 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,6 +18,15 @@
|
||||
*/
|
||||
|
||||
#include "piprocess.h"
|
||||
#ifdef WINDOWS
|
||||
//# include <.h>
|
||||
#else
|
||||
# include <sys/wait.h>
|
||||
# include <csignal>
|
||||
#endif
|
||||
#ifdef MAC_OS
|
||||
# include <crt_externs.h>
|
||||
#endif
|
||||
|
||||
|
||||
PIProcess::PIProcess(): PIThread() {
|
||||
@@ -51,9 +60,47 @@ void PIProcess::exec_() {
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::terminate() {
|
||||
#ifdef WINDOWS
|
||||
if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;
|
||||
#else
|
||||
if (is_exec) kill(pid, SIGKILL); pid = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int PIProcess::pID() const {
|
||||
#ifdef WINDOWS
|
||||
return pi.dwProcessId;
|
||||
#else
|
||||
return pid;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int PIProcess::currentPID() {
|
||||
#ifdef WINDOWS
|
||||
return GetCurrentProcessId();
|
||||
#else
|
||||
return getpid();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
PIStringList PIProcess::currentEnvironment() {
|
||||
PIStringList l;
|
||||
int i = 0;
|
||||
while (environ[i] != 0) {
|
||||
l << environ[i];
|
||||
++i;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::run() {
|
||||
//cout << "run" << endl;
|
||||
string str;
|
||||
PIString str;
|
||||
/// arguments convertion
|
||||
as = 0;
|
||||
#ifdef WINDOWS
|
||||
@@ -64,17 +111,17 @@ void PIProcess::run() {
|
||||
memset(a, ' ', as - 1);
|
||||
as = 0;
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i].stdString();
|
||||
memcpy(&a[as], str.c_str(), str.size());
|
||||
str = args[i];
|
||||
memcpy(&a[as], str.data(), str.lengthAscii());
|
||||
as += str.length() + 1;
|
||||
}
|
||||
a[as - 1] = 0;
|
||||
#else
|
||||
char * a[args.size_s() + 1];
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i].stdString();
|
||||
str = args[i];
|
||||
a[i] = new char[str.size() + 1];
|
||||
memcpy(a[i], str.c_str(), str.size());
|
||||
memcpy(a[i], str.data(), str.lengthAscii());
|
||||
a[i][str.size()] = 0;
|
||||
//cout << a[i] << endl;
|
||||
}
|
||||
@@ -83,9 +130,9 @@ void PIProcess::run() {
|
||||
/// environment convertion
|
||||
char ** e = new char*[env.size_s() + 1];
|
||||
for (int i = 0; i < env.size_s(); ++i) {
|
||||
str = env[i].stdString();
|
||||
str = env[i];
|
||||
e[i] = new char[str.size() + 1];
|
||||
memcpy(e[i], str.c_str(), str.size());
|
||||
memcpy(e[i], str.data(), str.lengthAscii());
|
||||
e[i][str.size()] = 0;
|
||||
//cout << e[i] << endl;
|
||||
}
|
||||
@@ -108,9 +155,9 @@ void PIProcess::run() {
|
||||
}
|
||||
//f_err.open(PIIODevice::WriteOnly); f_err.close();
|
||||
|
||||
str = args.front().stdString();
|
||||
str = args.front();
|
||||
is_exec = true;
|
||||
execStarted(PIString(str));
|
||||
execStarted(str);
|
||||
#ifndef WINDOWS
|
||||
pid = fork();
|
||||
if (pid == 0) {
|
||||
@@ -145,7 +192,7 @@ void PIProcess::run() {
|
||||
#else
|
||||
|
||||
//cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
||||
if (execve(str.c_str(), a, e) < 0)
|
||||
if (execve(str.data(), a, e) < 0)
|
||||
piCoutObj << "\"execve\" error, " << errorString();
|
||||
} else {
|
||||
msleep(1);
|
||||
@@ -158,7 +205,7 @@ void PIProcess::run() {
|
||||
//cout << "wait done" << endl;
|
||||
}
|
||||
#endif
|
||||
execFinished(PIString(str), exit_code);
|
||||
execFinished(str, exit_code);
|
||||
is_exec = false;
|
||||
for (int i = 0; i < env.size_s(); ++i)
|
||||
delete e[i];
|
||||
|
||||
Reference in New Issue
Block a user