2020-07-16 16:59:05 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* smsub Server Manager Subprocess
|
|
|
|
* Copyright (C) 2020 Syping
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* This software is provided as-is, no warranties are given to you, we are not
|
|
|
|
* responsible for anything with use of the software, you are self responsible.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include "SMSubProcess.h"
|
|
|
|
#include "smsub.h"
|
|
|
|
|
|
|
|
SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory)
|
|
|
|
{
|
|
|
|
process.setProgram(executable);
|
|
|
|
process.setArguments(arguments);
|
|
|
|
process.setWorkingDirectory(workingDirectory);
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
|
|
|
|
QObject::connect(&process, SIGNAL(readyRead()), this, SLOT(readyRead()));
|
|
|
|
QObject::connect(&process, SIGNAL(finished(int)), this, SLOT(processExit(int)));
|
|
|
|
QObject::connect(&process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::start()
|
|
|
|
{
|
|
|
|
process.start(QIODevice::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::readyRead()
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Read process output and emit event
|
2020-07-16 16:59:05 +02:00
|
|
|
while (process.canReadLine()) {
|
|
|
|
const QByteArray readData = process.readLine().trimmed();
|
|
|
|
emit outputWritten(readData + '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::processExit(int exitCode)
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Exit with the same exit code as the process
|
2020-07-16 16:59:05 +02:00
|
|
|
emit processStopped();
|
|
|
|
QCoreApplication::exit(exitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::processError(QProcess::ProcessError error)
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Handle process errors
|
2020-07-16 16:59:05 +02:00
|
|
|
if (likely(error == QProcess::FailedToStart)) {
|
|
|
|
QTextStream(stderr) << "Process failed to start!" << endl;
|
|
|
|
QCoreApplication::exit(1);
|
|
|
|
}
|
|
|
|
else if (error == QProcess::UnknownError) {
|
|
|
|
QTextStream(stderr) << "Unknown error occurred!" << endl;
|
|
|
|
QCoreApplication::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::killProcess()
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Kill process as requested
|
2020-07-16 16:59:05 +02:00
|
|
|
process.kill();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::stopProcess()
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Terminate process as requested
|
2020-07-16 16:59:05 +02:00
|
|
|
process.terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMSubProcess::writeInput(const QByteArray &input)
|
|
|
|
{
|
2020-07-16 20:02:21 +02:00
|
|
|
// Write input from Unix/IPC socket to process
|
2020-07-16 16:59:05 +02:00
|
|
|
process.write(input);
|
|
|
|
}
|