154 lines
6.3 KiB
C++
154 lines
6.3 KiB
C++
/*****************************************************************************
|
|
* 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 <QCommandLineParser>
|
|
#include <QCommandLineOption>
|
|
#include <QCoreApplication>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QTextStream>
|
|
#include <QJsonValue>
|
|
#include <QJsonArray>
|
|
#include <QFileInfo>
|
|
#include <QFile>
|
|
#include "SMSubProcess.h"
|
|
#include "SMSubServer.h"
|
|
#include "smsub.h"
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication a(argc, argv);
|
|
a.setApplicationName("Server Manager Subprocess");
|
|
a.setApplicationVersion("0.1");
|
|
|
|
QCommandLineParser commandLineParser;
|
|
commandLineParser.addHelpOption();
|
|
commandLineParser.addVersionOption();
|
|
|
|
QCommandLineOption processManifest("json", "JSON process manifest.", "json");
|
|
commandLineParser.addOption(processManifest);
|
|
|
|
QCommandLineOption processExecutable(QStringList() << "exec" << "executable", "Process executable to run.", "exec");
|
|
commandLineParser.addOption(processExecutable);
|
|
|
|
QCommandLineOption processArguments(QStringList() << "args" << "arguments", "Arguments given to process.", "args");
|
|
commandLineParser.addOption(processArguments);
|
|
|
|
#ifdef Q_OS_WIN
|
|
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "IPC socket used for communication.", "sock");
|
|
#else
|
|
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "Unix socket used for communication.", "sock");
|
|
#endif
|
|
commandLineParser.addOption(subprocessSocket);
|
|
|
|
commandLineParser.process(a);
|
|
|
|
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable))) {
|
|
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << endl;
|
|
return 1;
|
|
}
|
|
|
|
QString executable;
|
|
QString workingDirectory;
|
|
QStringList argumentList;
|
|
if (likely(commandLineParser.isSet(processManifest))) {
|
|
QFile manifestFile(commandLineParser.value(processManifest));
|
|
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
|
|
const QByteArray jsonData = manifestFile.readAll();
|
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
|
|
QJsonObject jsonObject = jsonDocument.object();
|
|
|
|
if (likely(jsonObject.contains("Executable"))) {
|
|
const QJsonValue jsonExecutable = jsonObject.value("Executable");
|
|
if (unlikely(!jsonExecutable.isString())) {
|
|
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << endl;
|
|
manifestFile.close();
|
|
return 1;
|
|
}
|
|
executable = jsonExecutable.toString();
|
|
}
|
|
else {
|
|
QTextStream(stderr) << "Executable is not defined in manifest, aborting!" << endl;
|
|
manifestFile.close();
|
|
return 1;
|
|
}
|
|
|
|
if (likely(jsonObject.contains("WorkingDirectory"))) {
|
|
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
|
|
if (unlikely(!jsonWorkingDirectory.isString())) {
|
|
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << endl;
|
|
manifestFile.close();
|
|
return 1;
|
|
}
|
|
workingDirectory = jsonWorkingDirectory.toString();
|
|
}
|
|
else {
|
|
workingDirectory = QFileInfo(executable).absolutePath();
|
|
}
|
|
|
|
if (likely(jsonObject.contains("Arguments"))) {
|
|
const QJsonValue jsonArguments = jsonObject.value("Arguments");
|
|
if (likely(jsonArguments.isArray())) {
|
|
const QJsonArray jsonArray = jsonArguments.toArray();
|
|
QJsonArray::const_iterator it = jsonArray.constBegin();
|
|
QJsonArray::const_iterator end = jsonArray.constEnd();
|
|
while (it != end) {
|
|
argumentList << it->toString();
|
|
}
|
|
}
|
|
else {
|
|
QTextStream(stderr) << "Arguments is not a array in manifest, aborting!" << endl;
|
|
manifestFile.close();
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
manifestFile.close();
|
|
}
|
|
}
|
|
else if (unlikely(commandLineParser.isSet(processArguments))) {
|
|
QTextStream(stderr) << "Arguments over command line are not supported yet!" << endl;
|
|
return 1;
|
|
}
|
|
|
|
QString socket;
|
|
if (likely(commandLineParser.isSet(subprocessSocket))) {
|
|
socket = commandLineParser.value(subprocessSocket);
|
|
}
|
|
|
|
SMSubServer subServer(socket);
|
|
if (unlikely(!subServer.isListening())) {
|
|
#ifdef Q_OS_WIN
|
|
QTextStream(stderr) << "Failed to start IPC socket!" << endl;
|
|
#else
|
|
QTextStream(stderr) << "Failed to start Unix socket!" << endl;
|
|
#endif
|
|
return 1;
|
|
}
|
|
|
|
SMSubProcess subProcess(executable, argumentList, workingDirectory);
|
|
|
|
QObject::connect(&subProcess, SIGNAL(outputWritten(QByteArray)), &subServer, SLOT(writeOutput(QByteArray)));
|
|
QObject::connect(&subServer, SIGNAL(inputWritten(QByteArray)), &subProcess, SLOT(writeInput(QByteArray)));
|
|
QObject::connect(&subServer, SIGNAL(killRequested()), &subProcess, SLOT(killProcess()));
|
|
QObject::connect(&subServer, SIGNAL(stopRequested()), &subProcess, SLOT(stopProcess()));
|
|
|
|
subProcess.start();
|
|
|
|
return a.exec();
|
|
}
|