2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-21 18:58:35 +08:00
|
|
|
#include "executablerunner.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2021-06-25 12:40:11 +08:00
|
|
|
#include "compilermanager.h"
|
2021-09-14 23:56:08 +08:00
|
|
|
#include "../settings.h"
|
|
|
|
#include "../systemconsts.h"
|
2021-12-24 23:18:20 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-03-16 20:08:39 +08:00
|
|
|
#include <QUuid>
|
2021-12-24 23:18:20 +08:00
|
|
|
#include <windows.h>
|
2021-12-29 13:16:03 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h> /* For mode constants */
|
|
|
|
#include <fcntl.h> /* For O_* constants */
|
2021-12-24 23:18:20 +08:00
|
|
|
#endif
|
|
|
|
|
2021-04-21 18:58:35 +08:00
|
|
|
|
2021-11-01 09:18:23 +08:00
|
|
|
ExecutableRunner::ExecutableRunner(const QString &filename, const QString &arguments, const QString &workDir
|
|
|
|
,QObject* parent):
|
|
|
|
Runner(filename,arguments,workDir,parent),
|
2021-10-25 09:16:00 +08:00
|
|
|
mRedirectInput(false),
|
2021-12-29 22:03:18 +08:00
|
|
|
mStartConsole(false),
|
|
|
|
mQuitSemaphore(0)
|
2021-04-21 18:58:35 +08:00
|
|
|
{
|
2022-01-28 08:21:56 +08:00
|
|
|
setWaitForFinishTime(1000);
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 09:16:00 +08:00
|
|
|
bool ExecutableRunner::startConsole() const
|
2021-10-24 23:13:00 +08:00
|
|
|
{
|
2021-10-25 09:16:00 +08:00
|
|
|
return mStartConsole;
|
2021-10-24 23:13:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 09:16:00 +08:00
|
|
|
void ExecutableRunner::setStartConsole(bool newStartConsole)
|
2021-10-24 23:13:00 +08:00
|
|
|
{
|
2021-10-25 09:16:00 +08:00
|
|
|
mStartConsole = newStartConsole;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:08:39 +08:00
|
|
|
const QString &ExecutableRunner::shareMemoryId() const
|
|
|
|
{
|
|
|
|
return mShareMemoryId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutableRunner::setShareMemoryId(const QString &newShareMemoryId)
|
|
|
|
{
|
|
|
|
mShareMemoryId = newShareMemoryId;
|
|
|
|
}
|
|
|
|
|
2021-10-25 09:16:00 +08:00
|
|
|
bool ExecutableRunner::redirectInput() const
|
|
|
|
{
|
|
|
|
return mRedirectInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutableRunner::setRedirectInput(bool isRedirect)
|
|
|
|
{
|
|
|
|
mRedirectInput = isRedirect;
|
2021-10-24 23:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &ExecutableRunner::redirectInputFilename() const
|
|
|
|
{
|
|
|
|
return mRedirectInputFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutableRunner::setRedirectInputFilename(const QString &newDataFile)
|
|
|
|
{
|
|
|
|
mRedirectInputFilename = newDataFile;
|
|
|
|
}
|
|
|
|
|
2021-04-21 18:58:35 +08:00
|
|
|
void ExecutableRunner::run()
|
|
|
|
{
|
|
|
|
emit started();
|
2021-11-01 09:18:23 +08:00
|
|
|
auto action = finally([this]{
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess.reset();
|
|
|
|
setPausing(false);
|
2021-11-01 09:18:23 +08:00
|
|
|
emit terminated();
|
|
|
|
});
|
2021-04-21 18:58:35 +08:00
|
|
|
mStop = false;
|
2021-06-25 12:40:11 +08:00
|
|
|
bool errorOccurred = false;
|
|
|
|
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess = std::make_shared<QProcess>();
|
|
|
|
mProcess->setProgram(mFilename);
|
2022-01-04 16:50:54 +08:00
|
|
|
mProcess->setArguments(splitProcessCommand(mArguments));
|
2022-01-23 21:04:08 +08:00
|
|
|
//qDebug()<<splitProcessCommand(mArguments);
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess->setWorkingDirectory(mWorkDir);
|
2021-09-14 23:56:08 +08:00
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
|
|
QString path = env.value("PATH");
|
|
|
|
QStringList pathAdded;
|
|
|
|
if (pSettings->compilerSets().defaultSet()) {
|
|
|
|
foreach(const QString& dir, pSettings->compilerSets().defaultSet()->binDirs()) {
|
|
|
|
pathAdded.append(dir);
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 15:08:54 +08:00
|
|
|
pathAdded.append(pSettings->dirs().appDir());
|
2021-09-14 23:56:08 +08:00
|
|
|
if (!path.isEmpty()) {
|
|
|
|
path+= PATH_SEPARATOR + pathAdded.join(PATH_SEPARATOR);
|
|
|
|
} else {
|
|
|
|
path = pathAdded.join(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
env.insert("PATH",path);
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess->setProcessEnvironment(env);
|
2021-12-24 23:18:20 +08:00
|
|
|
connect(
|
|
|
|
mProcess.get(), &QProcess::errorOccurred,
|
|
|
|
[&errorOccurred](){
|
|
|
|
errorOccurred= true;
|
|
|
|
});
|
|
|
|
#ifdef Q_OS_WIN
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess->setCreateProcessArgumentsModifier([this](QProcess::CreateProcessArguments * args){
|
2021-10-25 09:16:00 +08:00
|
|
|
if (mStartConsole) {
|
2021-12-13 19:10:16 +08:00
|
|
|
args->flags |= CREATE_NEW_CONSOLE;
|
|
|
|
args->flags &= ~CREATE_NO_WINDOW;
|
2021-10-25 09:16:00 +08:00
|
|
|
}
|
|
|
|
if (!mRedirectInput) {
|
2021-10-24 23:13:00 +08:00
|
|
|
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
|
2021-10-25 09:16:00 +08:00
|
|
|
}
|
|
|
|
});
|
2021-12-23 17:07:27 +08:00
|
|
|
HANDLE hSharedMemory=INVALID_HANDLE_VALUE;
|
|
|
|
int BUF_SIZE=1024;
|
|
|
|
char* pBuf=nullptr;
|
|
|
|
if (mStartConsole) {
|
|
|
|
hSharedMemory = CreateFileMappingA(
|
|
|
|
INVALID_HANDLE_VALUE,
|
|
|
|
NULL,
|
|
|
|
PAGE_READWRITE,
|
|
|
|
0,
|
|
|
|
100,
|
2022-03-16 20:08:39 +08:00
|
|
|
mShareMemoryId.toLocal8Bit().data()
|
2021-12-23 17:07:27 +08:00
|
|
|
);
|
|
|
|
if (hSharedMemory != NULL)
|
|
|
|
{
|
|
|
|
pBuf = (char*) MapViewOfFile(hSharedMemory, // handle to map object
|
|
|
|
FILE_MAP_ALL_ACCESS, // read/write permission
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
BUF_SIZE);
|
|
|
|
if (pBuf) {
|
|
|
|
pBuf[0]=0;
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 23:13:00 +08:00
|
|
|
}
|
2021-12-29 13:16:03 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
int BUF_SIZE=1024;
|
|
|
|
char* pBuf=nullptr;
|
2022-03-16 20:39:11 +08:00
|
|
|
int fd_shm = shm_open(mShareMemoryId.toLocal8Bit().data(),O_RDWR | O_CREAT,S_IRWXU);
|
2021-12-29 13:16:03 +08:00
|
|
|
if (fd_shm==-1) {
|
|
|
|
qDebug()<<QString("shm open failed %1:%2").arg(errno).arg(strerror(errno));
|
|
|
|
} else {
|
|
|
|
if (ftruncate(fd_shm,BUF_SIZE)==-1){
|
|
|
|
qDebug()<<QString("truncate failed %1:%2").arg(errno).arg(strerror(errno));
|
|
|
|
} else {
|
|
|
|
pBuf = (char*)mmap(NULL,BUF_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm,0);
|
|
|
|
if (pBuf == MAP_FAILED) {
|
|
|
|
qDebug()<<QString("mmap failed %1:%2").arg(errno).arg(strerror(errno));
|
|
|
|
pBuf = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 23:18:20 +08:00
|
|
|
#endif
|
|
|
|
// if (!redirectInput()) {
|
|
|
|
// process.closeWriteChannel();
|
|
|
|
// }
|
|
|
|
mProcess->start();
|
|
|
|
mProcess->waitForStarted(5000);
|
|
|
|
if (mProcess->state()==QProcess::Running && redirectInput()) {
|
|
|
|
mProcess->write(readFileToByteArray(redirectInputFilename()));
|
2022-03-31 09:56:49 +08:00
|
|
|
mProcess->waitForFinished(0);
|
2021-12-24 23:18:20 +08:00
|
|
|
}
|
2022-03-30 19:28:46 +08:00
|
|
|
bool writeChannelClosed = false;
|
2021-04-21 18:58:35 +08:00
|
|
|
while (true) {
|
2022-03-31 09:56:49 +08:00
|
|
|
if (mProcess->bytesToWrite()==0 && redirectInput() && !writeChannelClosed) {
|
|
|
|
writeChannelClosed=true;
|
|
|
|
mProcess->closeWriteChannel();
|
|
|
|
}
|
2022-01-28 08:21:56 +08:00
|
|
|
mProcess->waitForFinished(mWaitForFinishTime);
|
2021-12-23 17:07:27 +08:00
|
|
|
if (mProcess->state()!=QProcess::Running) {
|
2021-04-21 18:58:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-03-31 09:56:49 +08:00
|
|
|
if (errorOccurred)
|
|
|
|
break;
|
2021-04-21 18:58:35 +08:00
|
|
|
if (mStop) {
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess->terminate();
|
|
|
|
if (mProcess->waitForFinished(1000)) {
|
|
|
|
break;
|
2021-12-15 19:12:16 +08:00
|
|
|
}
|
|
|
|
for (int i=0;i<10;i++) {
|
2021-12-23 17:07:27 +08:00
|
|
|
mProcess->kill();
|
|
|
|
if (mProcess->waitForFinished(500)) {
|
2021-12-15 19:12:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 23:24:37 +08:00
|
|
|
break;
|
2021-06-25 12:40:11 +08:00
|
|
|
}
|
2021-12-29 13:16:03 +08:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
|
2021-12-23 17:07:27 +08:00
|
|
|
if (mStartConsole && !mPausing && pBuf) {
|
|
|
|
if (strncmp(pBuf,"FINISHED",sizeof("FINISHED"))==0) {
|
2021-12-29 22:03:18 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (pBuf) {
|
|
|
|
UnmapViewOfFile(pBuf);
|
|
|
|
pBuf = nullptr;
|
|
|
|
}
|
2022-03-16 20:08:39 +08:00
|
|
|
if (hSharedMemory!=INVALID_HANDLE_VALUE && hSharedMemory!=NULL) {
|
2021-12-29 22:03:18 +08:00
|
|
|
hSharedMemory = INVALID_HANDLE_VALUE;
|
|
|
|
CloseHandle(hSharedMemory);
|
|
|
|
}
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
if (pBuf) {
|
|
|
|
munmap(pBuf,BUF_SIZE);
|
|
|
|
pBuf = nullptr;
|
|
|
|
}
|
|
|
|
if (fd_shm!=-1) {
|
2022-03-16 20:39:11 +08:00
|
|
|
shm_unlink(mShareMemoryId.toLocal8Bit().data());
|
2021-12-29 22:03:18 +08:00
|
|
|
fd_shm = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-23 17:07:27 +08:00
|
|
|
setPausing(true);
|
|
|
|
emit pausingForFinish();
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 23:18:20 +08:00
|
|
|
#endif
|
2021-06-25 12:40:11 +08:00
|
|
|
}
|
2021-12-24 23:18:20 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2021-12-23 17:07:27 +08:00
|
|
|
if (pBuf)
|
|
|
|
UnmapViewOfFile(pBuf);
|
2022-03-16 20:08:39 +08:00
|
|
|
if (hSharedMemory!=INVALID_HANDLE_VALUE && hSharedMemory!=NULL)
|
2021-12-23 17:07:27 +08:00
|
|
|
CloseHandle(hSharedMemory);
|
2021-12-29 13:16:03 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
if (pBuf) {
|
|
|
|
munmap(pBuf,BUF_SIZE);
|
|
|
|
}
|
|
|
|
if (fd_shm!=-1) {
|
2022-03-16 20:39:11 +08:00
|
|
|
shm_unlink(mShareMemoryId.toLocal8Bit().data());
|
2021-12-29 13:16:03 +08:00
|
|
|
}
|
2021-12-24 23:18:20 +08:00
|
|
|
#endif
|
2021-06-25 12:40:11 +08:00
|
|
|
if (errorOccurred) {
|
2021-09-19 17:59:03 +08:00
|
|
|
//qDebug()<<"process error:"<<process.error();
|
2021-12-23 17:07:27 +08:00
|
|
|
switch (mProcess->error()) {
|
2021-06-25 12:40:11 +08:00
|
|
|
case QProcess::FailedToStart:
|
2021-09-04 19:37:33 +08:00
|
|
|
emit runErrorOccurred(tr("The runner process '%1' failed to start.").arg(mFilename));
|
2021-06-25 12:40:11 +08:00
|
|
|
break;
|
2021-09-04 19:37:33 +08:00
|
|
|
// case QProcess::Crashed:
|
|
|
|
// if (!mStop)
|
|
|
|
// emit runErrorOccurred(tr("The runner process crashed after starting successfully."));
|
|
|
|
// break;
|
2021-06-25 12:40:11 +08:00
|
|
|
case QProcess::Timedout:
|
|
|
|
emit runErrorOccurred(tr("The last waitFor...() function timed out."));
|
|
|
|
break;
|
|
|
|
case QProcess::WriteError:
|
|
|
|
emit runErrorOccurred(tr("An error occurred when attempting to write to the runner process."));
|
|
|
|
break;
|
|
|
|
case QProcess::ReadError:
|
|
|
|
emit runErrorOccurred(tr("An error occurred when attempting to read from the runner process."));
|
2021-04-21 18:58:35 +08:00
|
|
|
break;
|
2021-10-20 18:05:43 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 22:03:18 +08:00
|
|
|
mQuitSemaphore.release(1);
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
2021-12-23 17:07:27 +08:00
|
|
|
|
|
|
|
void ExecutableRunner::doStop()
|
|
|
|
{
|
2021-12-29 22:03:18 +08:00
|
|
|
mQuitSemaphore.acquire(1);
|
2021-12-23 17:07:27 +08:00
|
|
|
}
|