improve debugging, fix likely mess

This commit is contained in:
Syping 2023-03-20 08:28:37 +01:00
parent 684e3962de
commit 529a4a8be9
6 changed files with 105 additions and 88 deletions

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -30,13 +30,20 @@ SMSubProcess::SMSubProcess(const QString &executable, const QStringList &argumen
process.setArguments(arguments); process.setArguments(arguments);
process.setWorkingDirectory(workingDirectory); process.setWorkingDirectory(workingDirectory);
// manage input channel
process.setInputChannelMode(QProcess::ManagedInputChannel);
// stdout and stderr in same IO stream // stdout and stderr in same IO stream
process.setProcessChannelMode(QProcess::MergedChannels); process.setProcessChannelMode(QProcess::MergedChannels);
// Connect process signal handlers // Connect process signal handlers
QObject::connect(&process, &QProcess::readyRead, this, &SMSubProcess::readyRead); QObject::connect(&process, &QProcess::readyRead, this, &SMSubProcess::readyRead);
QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError); QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError);
QObject::connect(&process, &QProcess::started, this, [=]() {
QTextStream(stderr) << "Subprocess started!" << smsub_endl;
});
QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode) { QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode) {
QTextStream(stderr) << "Subprocess exited!" << smsub_endl;
// Exit with the same exit code as the process // Exit with the same exit code as the process
emit processStopped(); emit processStopped();
QCoreApplication::exit(exitCode); QCoreApplication::exit(exitCode);
@ -50,8 +57,14 @@ void SMSubProcess::start()
void SMSubProcess::readyRead() void SMSubProcess::readyRead()
{ {
#ifdef SMSUB_IODEBUG
QTextStream(stderr) << "Subprocess I/O RR!" << smsub_endl;
#endif
// Read process output and emit event // Read process output and emit event
while (process.canReadLine()) { while (process.canReadLine()) {
#ifdef SMSUB_IODEBUG
QTextStream(stderr) << "Subprocess I/O WL!" << smsub_endl;
#endif
const QByteArray readData = process.readLine().trimmed(); const QByteArray readData = process.readLine().trimmed();
emit outputWritten(readData + '\n'); emit outputWritten(readData + '\n');
} }
@ -60,7 +73,7 @@ void SMSubProcess::readyRead()
void SMSubProcess::processError(QProcess::ProcessError error) void SMSubProcess::processError(QProcess::ProcessError error)
{ {
// Handle process errors // Handle process errors
if (likely(error == QProcess::FailedToStart)) { if (Q_LIKELY(error == QProcess::FailedToStart)) {
QTextStream(stderr) << "Process failed to start!" << smsub_endl; QTextStream(stderr) << "Process failed to start!" << smsub_endl;
QCoreApplication::exit(1); QCoreApplication::exit(1);
} }
@ -99,6 +112,9 @@ void SMSubProcess::stopProcess()
void SMSubProcess::writeInput(const QByteArray &input) void SMSubProcess::writeInput(const QByteArray &input)
{ {
#ifdef SMSUB_IODEBUG
QTextStream(stderr) << "Subprocess I/O IN!" << smsub_endl;
#endif
// Write input from Unix/IPC socket to process // Write input from Unix/IPC socket to process
process.write(input); process.write(input);
} }

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -47,7 +47,7 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &ser
bool SMSubServer::isListening() bool SMSubServer::isListening()
{ {
if (likely(type == ServerType::Local)) { if (Q_LIKELY(type == ServerType::Local)) {
return static_cast<QLocalServer*>(server)->isListening(); return static_cast<QLocalServer*>(server)->isListening();
} }
else if (type == ServerType::WebSocket) { else if (type == ServerType::WebSocket) {
@ -59,12 +59,13 @@ bool SMSubServer::isListening()
void SMSubServer::newConnection() void SMSubServer::newConnection()
{ {
QObject *socket; QObject *socket;
if (likely(type == ServerType::Local)) { if (Q_LIKELY(type == ServerType::Local)) {
QLocalSocket *localSocket = static_cast<QLocalServer*>(server)->nextPendingConnection(); QLocalSocket *localSocket = static_cast<QLocalServer*>(server)->nextPendingConnection();
QObject::connect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead); QObject::connect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
QObject::connect(localSocket, &QLocalSocket::disconnected, this, &SMSubServer::deleteSocket); QObject::connect(localSocket, &QLocalSocket::disconnected, this, &SMSubServer::deleteSocket);
localSocket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8()); localSocket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
socket = localSocket; socket = localSocket;
QTextStream(stderr) << "LocalSocket connected!" << smsub_endl;
} }
else if (type == ServerType::WebSocket) { else if (type == ServerType::WebSocket) {
QWebSocket *webSocket = static_cast<QWebSocketServer*>(server)->nextPendingConnection(); QWebSocket *webSocket = static_cast<QWebSocketServer*>(server)->nextPendingConnection();
@ -72,6 +73,7 @@ void SMSubServer::newConnection()
QObject::connect(webSocket, &QWebSocket::disconnected, this, &SMSubServer::deleteSocket); QObject::connect(webSocket, &QWebSocket::disconnected, this, &SMSubServer::deleteSocket);
webSocket->sendBinaryMessage(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8()); webSocket->sendBinaryMessage(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
socket = webSocket; socket = webSocket;
QTextStream(stderr) << QString("WebSocket %1:%2 connected!").arg(webSocket->peerName(), QString::number(webSocket->peerPort())) << smsub_endl;
} }
else { else {
// Just for being sure // Just for being sure
@ -92,7 +94,7 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
{ {
// Only allow commands being sent if authenticated // Only allow commands being sent if authenticated
const bool isAuthenticated = socket->property("Authenticated").toBool(); const bool isAuthenticated = socket->property("Authenticated").toBool();
if (likely(isAuthenticated)) { if (Q_LIKELY(isAuthenticated)) {
if (message.startsWith("+dbg")) { if (message.startsWith("+dbg")) {
socket->setProperty("ReceiveDbgMsg", true); socket->setProperty("ReceiveDbgMsg", true);
sendMessage(socket, "Debug messages enabled!\n"); sendMessage(socket, "Debug messages enabled!\n");
@ -110,7 +112,7 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
debugOutput(socket, "Log output disabled!"); debugOutput(socket, "Log output disabled!");
} }
else if (message.startsWith("+reg")) { else if (message.startsWith("+reg")) {
if (likely(serverSettings->canRegister)) { if (Q_LIKELY(serverSettings->canRegister)) {
QByteArray authUuid = QUuid::createUuid().toByteArray(QUuid::Id128); QByteArray authUuid = QUuid::createUuid().toByteArray(QUuid::Id128);
authUuid = QByteArray::fromHex(authUuid).toBase64(QByteArray::OmitTrailingEquals); authUuid = QByteArray::fromHex(authUuid).toBase64(QByteArray::OmitTrailingEquals);
emit tokenRegistered(QString::fromUtf8(authUuid)); emit tokenRegistered(QString::fromUtf8(authUuid));
@ -141,7 +143,7 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
} }
else { else {
// Authenticate when token is valid, otherwise disconnect // Authenticate when token is valid, otherwise disconnect
if (unlikely(tokens.contains(QString::fromUtf8(message)))) { if (Q_UNLIKELY(tokens.contains(QString::fromUtf8(message)))) {
// Set client as authenticated and add it to vector // Set client as authenticated and add it to vector
socket->setProperty("Authenticated", true); socket->setProperty("Authenticated", true);
sendMessage(socket, "Login successful!\n"); sendMessage(socket, "Login successful!\n");
@ -149,7 +151,7 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
} }
else { else {
// Stop receiving data and disconnect socket // Stop receiving data and disconnect socket
if (likely(type == ServerType::Local)) { if (Q_LIKELY(type == ServerType::Local)) {
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket); QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
QObject::disconnect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead); QObject::disconnect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
localSocket->write("Incorrect token!\n"); localSocket->write("Incorrect token!\n");
@ -177,7 +179,13 @@ void SMSubServer::wsMessageReceived(const QByteArray &message)
void SMSubServer::lsReadyRead() void SMSubServer::lsReadyRead()
{ {
QLocalSocket *socket = static_cast<QLocalSocket*>(sender()); QLocalSocket *socket = static_cast<QLocalSocket*>(sender());
#ifdef SMSUB_IODEBUG
QTextStream(stderr) << "LocalSocket I/O RR!" << smsub_endl;
#endif
while (socket->canReadLine()) { while (socket->canReadLine()) {
#ifdef SMSUB_IODEBUG
QTextStream(stderr) << "LocalSocket I/O WL!" << smsub_endl;
#endif
const QByteArray message = socket->readLine().trimmed(); const QByteArray message = socket->readLine().trimmed();
if (!messageReceived(socket, message)) if (!messageReceived(socket, message))
return; return;
@ -196,9 +204,13 @@ void SMSubServer::debugOutput(QObject *socket, const QByteArray &message)
{ {
// Only send debug messages when the client opted-in // Only send debug messages when the client opted-in
const QVariant variant = socket->property("ReceiveDbgMsg"); const QVariant variant = socket->property("ReceiveDbgMsg");
if (unlikely(variant.type() == QVariant::Bool)) { #if QT_VERSION >= 0x060000
if (Q_UNLIKELY(variant.typeId() == QMetaType::Bool)) {
#else
if (Q_UNLIKELY(variant.type() == QVariant::Bool)) {
#endif
bool receiveDbgMsg = variant.toBool(); bool receiveDbgMsg = variant.toBool();
if (likely(receiveDbgMsg)) { if (Q_LIKELY(receiveDbgMsg)) {
sendMessage(socket, message + '\n'); sendMessage(socket, message + '\n');
} }
} }
@ -209,9 +221,13 @@ void SMSubServer::writeOutput(const QByteArray &output)
// Read process output when client opted-in for log // Read process output when client opted-in for log
for (auto it = sockets.constBegin(); it != sockets.constEnd(); it++) { for (auto it = sockets.constBegin(); it != sockets.constEnd(); it++) {
const QVariant variant = (*it)->property("ReceiveLog"); const QVariant variant = (*it)->property("ReceiveLog");
if (unlikely(variant.type() == QVariant::Bool)) { #if QT_VERSION >= 0x060000
if (Q_UNLIKELY(variant.typeId() == QMetaType::Bool)) {
#else
if (Q_UNLIKELY(variant.type() == QVariant::Bool)) {
#endif
bool receiveLog = variant.toBool(); bool receiveLog = variant.toBool();
if (likely(receiveLog)) { if (Q_LIKELY(receiveLog)) {
sendMessage(*it, output); sendMessage(*it, output);
} }
} }
@ -220,7 +236,7 @@ void SMSubServer::writeOutput(const QByteArray &output)
void SMSubServer::sendMessage(QObject *socket, const QByteArray &message) void SMSubServer::sendMessage(QObject *socket, const QByteArray &message)
{ {
if (likely(type == ServerType::Local)) { if (Q_LIKELY(type == ServerType::Local)) {
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket); QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
localSocket->write(message); localSocket->write(message);
} }

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:

109
main.cpp
View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -33,7 +33,6 @@
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
#include <initializer_list> #include <initializer_list>
#include "signal.h" #include "signal.h"
#include "unistd.h"
#endif #endif
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
@ -135,7 +134,7 @@ int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
a.setApplicationName("Server Manager Subprocess"); a.setApplicationName("Server Manager Subprocess");
a.setApplicationVersion("0.5.1"); a.setApplicationVersion("0.6");
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM}); catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
@ -151,25 +150,25 @@ int main(int argc, char *argv[])
QString workingDirectory; QString workingDirectory;
QStringList argumentList; QStringList argumentList;
QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE"); const QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE");
QByteArray envManifest = qgetenv("SMSUB_JSON"); const QByteArray envManifest = qgetenv("SMSUB_JSON");
QByteArray envExecutable = qgetenv("SMSUB_EXEC"); const QByteArray envExecutable = qgetenv("SMSUB_EXEC");
QByteArray envArguments = qgetenv("SMSUB_ARGS"); const QByteArray envArguments = qgetenv("SMSUB_ARGS");
QByteArray envSocket = qgetenv("SMSUB_SOCK"); const QByteArray envSocket = qgetenv("SMSUB_SOCK");
QByteArray envRemotePort = qgetenv("SMSUB_RPORT"); const QByteArray envRemotePort = qgetenv("SMSUB_RPORT");
QByteArray envRemoteSocket = qgetenv("SMSUB_RSOCK"); const QByteArray envRemoteSocket = qgetenv("SMSUB_RSOCK");
QByteArray envTimeout = qgetenv("SMSUB_TIMEOUT"); const QByteArray envTimeout = qgetenv("SMSUB_TIMEOUT");
QByteArray envWorkDir = qgetenv("SMSUB_WORKDIR"); const QByteArray envWorkDir = qgetenv("SMSUB_WORKDIR");
if (unlikely(envEnvironmentMode == "1" || envEnvironmentMode.toLower() == "true")) { if (envEnvironmentMode == "1" || envEnvironmentMode.toLower() == "true") {
if (likely(envExecutable.isEmpty() && envArguments.isEmpty())) { if (envExecutable.isEmpty() && envArguments.isEmpty()) {
QStringList arguments = a.arguments(); QStringList arguments = a.arguments();
arguments.removeFirst(); arguments.removeFirst();
executable = arguments.takeFirst(); executable = arguments.takeFirst();
argumentList = arguments; argumentList = arguments;
} }
else { else {
if (likely(!envExecutable.isEmpty())) { if (!envExecutable.isEmpty()) {
executable = QString::fromUtf8(envExecutable); executable = QString::fromUtf8(envExecutable);
} }
else { else {
@ -177,7 +176,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (likely(!envArguments.isEmpty())) { if (!envArguments.isEmpty()) {
argumentList = parseStringArguments(QString::fromUtf8(envArguments)); argumentList = parseStringArguments(QString::fromUtf8(envArguments));
if (argumentList.empty()) { if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl; QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
@ -190,7 +189,7 @@ int main(int argc, char *argv[])
} }
} }
if (unlikely(!envTimeout.isEmpty())) { if (!envTimeout.isEmpty()) {
bool ok; bool ok;
const int _termTimeout = envTimeout.toInt(&ok); const int _termTimeout = envTimeout.toInt(&ok);
if (ok) { if (ok) {
@ -203,14 +202,14 @@ int main(int argc, char *argv[])
} }
} }
if (unlikely(!envWorkDir.isEmpty())) { if (!envWorkDir.isEmpty()) {
workingDirectory = QString::fromUtf8(envWorkDir); workingDirectory = QString::fromUtf8(envWorkDir);
} }
else { else {
workingDirectory = QFileInfo(executable).absolutePath(); workingDirectory = QFileInfo(executable).absolutePath();
} }
if (likely(!envSocket.isEmpty())) { if (!envSocket.isEmpty()) {
socket = QString::fromUtf8(envSocket); socket = QString::fromUtf8(envSocket);
} }
else { else {
@ -222,11 +221,11 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (unlikely(!envRemoteSocket.isEmpty())) { if (!envRemoteSocket.isEmpty()) {
rsocket = QString::fromUtf8(envRemoteSocket); rsocket = QString::fromUtf8(envRemoteSocket);
} }
if (unlikely(!envRemotePort.isEmpty())) { if (!envRemotePort.isEmpty()) {
bool ok; bool ok;
const quint16 _rport = envRemotePort.toUShort(&ok); const quint16 _rport = envRemotePort.toUShort(&ok);
if (ok) { if (ok) {
@ -275,26 +274,26 @@ int main(int argc, char *argv[])
commandLineParser.process(a); commandLineParser.process(a);
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable) || if (commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable) ||
!envManifest.isEmpty() && !envExecutable.isEmpty() || !envManifest.isEmpty() && !envExecutable.isEmpty() ||
commandLineParser.isSet(processManifest) && !envExecutable.isEmpty() || commandLineParser.isSet(processManifest) && !envExecutable.isEmpty() ||
!envManifest.isEmpty() && commandLineParser.isSet(processExecutable))) { !envManifest.isEmpty() && commandLineParser.isSet(processExecutable)) {
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << smsub_endl; QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << smsub_endl;
return 1; return 1;
} }
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processArguments) || if (commandLineParser.isSet(processManifest) && commandLineParser.isSet(processArguments) ||
!envManifest.isEmpty() && !envArguments.isEmpty() || !envManifest.isEmpty() && !envArguments.isEmpty() ||
commandLineParser.isSet(processManifest) && !envArguments.isEmpty() || commandLineParser.isSet(processManifest) && !envArguments.isEmpty() ||
!envManifest.isEmpty() && commandLineParser.isSet(processArguments))) { !envManifest.isEmpty() && commandLineParser.isSet(processArguments)) {
QTextStream(stderr) << "You can't define a Process arguments and a JSON process manifest at the same time!" << smsub_endl; QTextStream(stderr) << "You can't define a Process arguments and a JSON process manifest at the same time!" << smsub_endl;
return 1; return 1;
} }
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket) || if (commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket) ||
!envRemotePort.isEmpty() && !envRemoteSocket.isEmpty() || !envRemotePort.isEmpty() && !envRemoteSocket.isEmpty() ||
commandLineParser.isSet(subprocessRemotePort) && !envRemoteSocket.isEmpty() || commandLineParser.isSet(subprocessRemotePort) && !envRemoteSocket.isEmpty() ||
!envRemotePort.isEmpty() && commandLineParser.isSet(subprocessRemoteSocket))) { !envRemotePort.isEmpty() && commandLineParser.isSet(subprocessRemoteSocket)) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl; QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << smsub_endl;
#else #else
@ -303,7 +302,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (unlikely(commandLineParser.isSet(processTimeout))) { if (commandLineParser.isSet(processTimeout)) {
bool ok; bool ok;
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok); const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
if (ok) { if (ok) {
@ -315,7 +314,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
} }
else if (unlikely(!envTimeout.isEmpty())) { else if (!envTimeout.isEmpty()) {
bool ok; bool ok;
const int _termTimeout = envTimeout.toInt(&ok); const int _termTimeout = envTimeout.toInt(&ok);
if (ok) { if (ok) {
@ -328,14 +327,14 @@ int main(int argc, char *argv[])
} }
} }
if (unlikely(commandLineParser.isSet(processExecutable))) { if (commandLineParser.isSet(processExecutable)) {
executable = commandLineParser.value(processExecutable); executable = commandLineParser.value(processExecutable);
} }
else if (unlikely(!envExecutable.isEmpty())) { else if (!envExecutable.isEmpty()) {
executable = QString::fromUtf8(envExecutable); executable = QString::fromUtf8(envExecutable);
} }
if (unlikely(!envWorkDir.isEmpty())) { if (!envWorkDir.isEmpty()) {
workingDirectory = QString::fromUtf8(envWorkDir); workingDirectory = QString::fromUtf8(envWorkDir);
} }
else { else {
@ -343,23 +342,23 @@ int main(int argc, char *argv[])
} }
QString manifestPath; QString manifestPath;
if (likely(commandLineParser.isSet(processManifest))) { if (commandLineParser.isSet(processManifest)) {
manifestPath = commandLineParser.value(processManifest); manifestPath = commandLineParser.value(processManifest);
} }
else if (!envManifest.isEmpty()) { else if (!envManifest.isEmpty()) {
manifestPath = QString::fromUtf8(envManifest); manifestPath = QString::fromUtf8(envManifest);
} }
if (likely(!manifestPath.isEmpty())) { if (!manifestPath.isEmpty()) {
QFile manifestFile(manifestPath); QFile manifestFile(manifestPath);
if (likely(manifestFile.open(QIODevice::ReadOnly))) { if (manifestFile.open(QIODevice::ReadOnly)) {
const QByteArray jsonData = manifestFile.readAll(); const QByteArray jsonData = manifestFile.readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData); QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = jsonDocument.object(); QJsonObject jsonObject = jsonDocument.object();
if (likely(jsonObject.contains("Executable"))) { if (jsonObject.contains("Executable")) {
const QJsonValue jsonExecutable = jsonObject.value("Executable"); const QJsonValue jsonExecutable = jsonObject.value("Executable");
if (unlikely(!jsonExecutable.isString())) { if (!jsonExecutable.isString()) {
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << smsub_endl; QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close(); manifestFile.close();
return 1; return 1;
@ -372,9 +371,9 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (likely(jsonObject.contains("WorkingDirectory"))) { if (jsonObject.contains("WorkingDirectory")) {
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory"); const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
if (unlikely(!jsonWorkingDirectory.isString())) { if (!jsonWorkingDirectory.isString()) {
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << smsub_endl; QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << smsub_endl;
manifestFile.close(); manifestFile.close();
return 1; return 1;
@ -382,9 +381,9 @@ int main(int argc, char *argv[])
workingDirectory = jsonWorkingDirectory.toString(); workingDirectory = jsonWorkingDirectory.toString();
} }
if (likely(jsonObject.contains("Arguments"))) { if (jsonObject.contains("Arguments")) {
const QJsonValue jsonArguments = jsonObject.value("Arguments"); const QJsonValue jsonArguments = jsonObject.value("Arguments");
if (likely(jsonArguments.isArray())) { if (jsonArguments.isArray()) {
const QJsonArray jsonArray = jsonArguments.toArray(); const QJsonArray jsonArray = jsonArguments.toArray();
for (auto it = jsonArray.constBegin(); it != jsonArray.constEnd(); it++) { for (auto it = jsonArray.constBegin(); it != jsonArray.constEnd(); it++) {
argumentList << it->toString(); argumentList << it->toString();
@ -397,9 +396,9 @@ int main(int argc, char *argv[])
} }
} }
if (unlikely(!timeoutSet && jsonObject.contains("TerminationTimeout"))) { if (!timeoutSet && jsonObject.contains("TerminationTimeout")) {
const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout"); const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout");
if (unlikely(!jsonTimeout.isDouble())) { if (!jsonTimeout.isDouble()) {
termTimeout = qRound(jsonTimeout.toDouble()); termTimeout = qRound(jsonTimeout.toDouble());
} }
else { else {
@ -411,14 +410,14 @@ int main(int argc, char *argv[])
manifestFile.close(); manifestFile.close();
} }
} }
else if (unlikely(commandLineParser.isSet(processArguments))) { else if (commandLineParser.isSet(processArguments)) {
argumentList = parseStringArguments(commandLineParser.value(processArguments)); argumentList = parseStringArguments(commandLineParser.value(processArguments));
if (argumentList.empty()) { if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl; QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
return 1; return 1;
} }
} }
else if (unlikely(!envArguments.isEmpty())) { else if (!envArguments.isEmpty()) {
argumentList = parseStringArguments(QString::fromUtf8(envArguments)); argumentList = parseStringArguments(QString::fromUtf8(envArguments));
if (argumentList.empty()) { if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl; QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
@ -426,7 +425,7 @@ int main(int argc, char *argv[])
} }
} }
if (likely(commandLineParser.isSet(subprocessSocket))) { if (commandLineParser.isSet(subprocessSocket)) {
socket = commandLineParser.value(subprocessSocket); socket = commandLineParser.value(subprocessSocket);
} }
else if (!envSocket.isEmpty()) { else if (!envSocket.isEmpty()) {
@ -441,14 +440,14 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (unlikely(commandLineParser.isSet(subprocessRemoteSocket))) { if (commandLineParser.isSet(subprocessRemoteSocket)) {
rsocket = commandLineParser.value(subprocessRemoteSocket); rsocket = commandLineParser.value(subprocessRemoteSocket);
} }
else if (unlikely(!envRemoteSocket.isEmpty())) { else if (!envRemoteSocket.isEmpty()) {
rsocket = QString::fromUtf8(envRemoteSocket); rsocket = QString::fromUtf8(envRemoteSocket);
} }
if (unlikely(commandLineParser.isSet(subprocessRemotePort))) { if (commandLineParser.isSet(subprocessRemotePort)) {
bool ok; bool ok;
const quint16 _rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok); const quint16 _rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok);
if (ok) { if (ok) {
@ -478,7 +477,7 @@ int main(int argc, char *argv[])
localSettings.isLocal = true; localSettings.isLocal = true;
SMSubServer subLocal(&localSettings, socket); SMSubServer subLocal(&localSettings, socket);
if (unlikely(!subLocal.isListening())) { if (!subLocal.isListening()) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QTextStream(stderr) << "Failed to start local IPC socket!" << smsub_endl; QTextStream(stderr) << "Failed to start local IPC socket!" << smsub_endl;
#else #else
@ -493,9 +492,9 @@ int main(int argc, char *argv[])
remoteSettings.canRegister = false; remoteSettings.canRegister = false;
remoteSettings.isLocal = false; remoteSettings.isLocal = false;
if (unlikely(!rsocket.isEmpty())) { if (!rsocket.isEmpty()) {
SMSubServer *subRemote = new SMSubServer(&remoteSettings, rsocket); SMSubServer *subRemote = new SMSubServer(&remoteSettings, rsocket);
if (unlikely(!subRemote->isListening())) { if (!subRemote->isListening()) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QTextStream(stderr) << "Failed to start remote IPC socket!" << smsub_endl; QTextStream(stderr) << "Failed to start remote IPC socket!" << smsub_endl;
#else #else
@ -510,9 +509,9 @@ int main(int argc, char *argv[])
QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess); QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess); QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
} }
else if (unlikely(rportSet)) { else if (rportSet) {
SMSubServer *subRemote = new SMSubServer(&remoteSettings, QString(), rport); SMSubServer *subRemote = new SMSubServer(&remoteSettings, QString(), rport);
if (unlikely(!subRemote->isListening())) { if (!subRemote->isListening()) {
QTextStream(stderr) << "Failed to start remote WebSockets server!" << smsub_endl; QTextStream(stderr) << "Failed to start remote WebSockets server!" << smsub_endl;
return 1; return 1;
} }
@ -535,5 +534,7 @@ int main(int argc, char *argv[])
subProcess.start(); subProcess.start();
QTextStream(stderr) << QString("SMSub Version %1 initialized!").arg(QCoreApplication::applicationVersion()) << smsub_endl;
return a.exec(); return a.exec();
} }

20
smsub.h
View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * smsub Server Manager Subprocess
* Copyright (C) 2020-2021 Syping * Copyright (C) 2020-2023 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -20,23 +20,7 @@
#define SMSUB_H #define SMSUB_H
#include <QtGlobal> #include <QtGlobal>
#ifndef SMSUB_WITHOUT_EXPECT #if QT_VERSION >= 0x050F00
#ifndef likely
#define likely(x) __builtin_expect((x),1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect((x),0)
#endif
#else
#ifndef likely
#define likely(x) (x)
#endif
#ifndef unlikely
#define unlikely(x) (x)
#endif
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#define smsub_endl Qt::endl #define smsub_endl Qt::endl
#else #else
#define smsub_endl endl #define smsub_endl endl