From f1d2e6732c7bf91081aa2851c9c4670c18de26d9 Mon Sep 17 00:00:00 2001 From: nephros Date: Fri, 29 Nov 2024 14:24:23 +0100 Subject: [PATCH 1/4] Declare help static --- src/bin/patchmanager-daemon/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/patchmanager-daemon/main.cpp b/src/bin/patchmanager-daemon/main.cpp index 86ccadd4..75d4b99b 100644 --- a/src/bin/patchmanager-daemon/main.cpp +++ b/src/bin/patchmanager-daemon/main.cpp @@ -53,7 +53,7 @@ #endif -void help() +static void help() { std::cout << "Patchmanager " << BUILD_VERSION << std::endl; std::cout << "Usage:" << std::endl; From 2fa5d0c275b5f777b58f250393e85bfac265c4e1 Mon Sep 17 00:00:00 2001 From: nephros Date: Fri, 29 Nov 2024 15:29:09 +0100 Subject: [PATCH 2/4] Add a version() method --- src/bin/patchmanager-daemon/main.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bin/patchmanager-daemon/main.cpp b/src/bin/patchmanager-daemon/main.cpp index 75d4b99b..6aa80463 100644 --- a/src/bin/patchmanager-daemon/main.cpp +++ b/src/bin/patchmanager-daemon/main.cpp @@ -52,10 +52,14 @@ #define BUILD_VERSION "99.99.99" #endif +static void version() +{ + std::cout << "Patchmanager " << BUILD_VERSION << std::endl; +} static void help() { - std::cout << "Patchmanager " << BUILD_VERSION << std::endl; + version(); std::cout << "Usage:" << std::endl; std::cout << " patchmanager [--help] : Print this help text" << std::endl; std::cout << " patchmanager -a : Enable and activate a Patch" << std::endl; @@ -64,6 +68,9 @@ static void help() std::cout << " patchmanager --backup-working : Save list of enabled Patches as \"working\"" << std::endl; std::cout << " patchmanager --restore-working : Enable backup list of \"working\" Patches" << std::endl; std::cout << " patchmanager --daemon : Start Patchmanager as daemon" << std::endl; + std::cout << " patchmanager --version : Print the build version and exit." << std::endl; + std::cout << std::endl; + std::cout << "Patchmanager must be run as root." << std::endl; } int main(int argc, char **argv) From b3d2ead688d8f395dce8621dc5696aabba65636c Mon Sep 17 00:00:00 2001 From: nephros Date: Fri, 29 Nov 2024 15:30:06 +0100 Subject: [PATCH 3/4] Adapt parameter handling --- src/bin/patchmanager-daemon/main.cpp | 22 +++++++++++++------ .../patchmanagerobject.cpp | 22 ++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/bin/patchmanager-daemon/main.cpp b/src/bin/patchmanager-daemon/main.cpp index 6aa80463..55e71d9c 100644 --- a/src/bin/patchmanager-daemon/main.cpp +++ b/src/bin/patchmanager-daemon/main.cpp @@ -75,19 +75,27 @@ static void help() int main(int argc, char **argv) { - qputenv("NO_PM_PRELOAD", "1"); + // further argument processing in patchmanagerobject.cpp + switch (argc) { + case 1: // we want arguments. + help(); exit(2); break; + case 2: + if (!strcmp(argv[1], "--help")) { + help(); exit(0); + } else if (!strcmp(argv[1], "--version")) { + version(); exit(0); + } + break; + } if (getuid() != 0) { - fprintf(stderr, "%s: Not running as root, exiting.\n", argv[0]); + fprintf(stderr, "%s: Not running as root, exiting.\n\n", argv[0]); + help(); exit(2); } + qputenv("NO_PM_PRELOAD", "1"); QCoreApplication app(argc, argv); - if (app.arguments().length() < 2) { - help(); - return 0; - } - app.setApplicationVersion(QStringLiteral(BUILD_VERSION)); PatchManagerObject patchManager; diff --git a/src/bin/patchmanager-daemon/patchmanagerobject.cpp b/src/bin/patchmanager-daemon/patchmanagerobject.cpp index 6d786d93..0378e7f4 100644 --- a/src/bin/patchmanager-daemon/patchmanagerobject.cpp +++ b/src/bin/patchmanager-daemon/patchmanagerobject.cpp @@ -1127,22 +1127,28 @@ QString PatchManagerObject::getRpmName(const QString &rpm) const void PatchManagerObject::process() { const QStringList args = QCoreApplication::arguments(); + const int argc = args.count(); - if (args.count() == 1) { - return; // Prints help text. - } else if (args.count() == 2) { - if (args[1] == QStringLiteral("--help")) { - return; // Also prints help text. - } else if (args[1] == QStringLiteral("--daemon")) { + /* argc == 1 and "--help/--version" should already be handled in main.cpp */ + if (argc == 1) { + qCritical() << "Something went wrong handling the arguments."; + QCoreApplication::exit(2); + return; + } else if (argc == 2) { + if (args[1] == QStringLiteral("--daemon")) { initialize(); } else if (args[1] == QStringLiteral("--reset-system")) { resetSystem(); QCoreApplication::exit(2); return; } - } else if (args.count() > 1) { // Must be "> 1", not "> 2" for "--unapply-all" + /* + * "client mode": from here on, handle options which send dbus messages + * Must be "> 1", not "> 2" for "--unapply-all" and other singular arguments. + */ + } else if (argc > 1) { QDBusConnection connection = QDBusConnection::systemBus(); - qDebug() << Q_FUNC_INFO << "Has arguments, sending D-Bus message and quit."; + qDebug() << Q_FUNC_INFO << "Called with arguments, will send a D-Bus message and quit."; QString method; QVariantList data; From d56bb12362a95169aa676da21d90f89520319480 Mon Sep 17 00:00:00 2001 From: nephros Date: Fri, 29 Nov 2024 17:18:59 +0100 Subject: [PATCH 4/4] Adapt parameter handling II --- .../patchmanagerobject.cpp | 70 +++++++++++-------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/bin/patchmanager-daemon/patchmanagerobject.cpp b/src/bin/patchmanager-daemon/patchmanagerobject.cpp index 0378e7f4..953f619b 100644 --- a/src/bin/patchmanager-daemon/patchmanagerobject.cpp +++ b/src/bin/patchmanager-daemon/patchmanagerobject.cpp @@ -1126,58 +1126,62 @@ QString PatchManagerObject::getRpmName(const QString &rpm) const */ void PatchManagerObject::process() { + // FIXME: all of this is probably better handled using QCommandLineParser. + const QStringList args = QCoreApplication::arguments(); const int argc = args.count(); - /* argc == 1 and "--help/--version" should already be handled in main.cpp */ - if (argc == 1) { - qCritical() << "Something went wrong handling the arguments."; - QCoreApplication::exit(2); - return; - } else if (argc == 2) { - if (args[1] == QStringLiteral("--daemon")) { - initialize(); - } else if (args[1] == QStringLiteral("--reset-system")) { - resetSystem(); - QCoreApplication::exit(2); - return; - } - /* - * "client mode": from here on, handle options which send dbus messages + /* argc == 1 and "--help/--version" should already be handled in main.cpp * Must be "> 1", not "> 2" for "--unapply-all" and other singular arguments. */ - } else if (argc > 1) { - QDBusConnection connection = QDBusConnection::systemBus(); - qDebug() << Q_FUNC_INFO << "Called with arguments, will send a D-Bus message and quit."; - - QString method; - QVariantList data; - if (args[1] == QStringLiteral("-a")) { - method = QStringLiteral("applyPatch"); - if (args.length() < 3) { + if (argc > 1) { + const QString firstArg = args[1]; + if (argc == 2) { // --daemonize and --reset-system + if (firstArg == QStringLiteral("--daemon")) { + initialize(); + return; + } else if (firstArg == QStringLiteral("--reset-system")) { + // FIXME: see #204, #278 + qWarning() << Q_FUNC_INFO << "This is very likely broken in this version of patchmanager."; + resetSystem(); QCoreApplication::exit(2); return; - } else { - data.append(args[2]); } - } else if (args[1] == QStringLiteral("-u")) { - method = QStringLiteral("unapplyPatch"); + } + + /* + * "Client mode": from here on, handle options which send dbus messages + */ + + QString method; + QVariantList data; + + if ((firstArg == QStringLiteral("-a")) || (firstArg == QStringLiteral("-u"))) { if (args.length() < 3) { + qCritical() << "Option" << firstArg << "requires an argument."; QCoreApplication::exit(2); return; } else { data.append(args[2]); } - } else if (args[1] == QStringLiteral("--unapply-all")) { + if (firstArg == QStringLiteral("-a")) + method = QStringLiteral("applyPatch"); + if (firstArg == QStringLiteral("-u")) + method = QStringLiteral("unapplyPatch"); + } else if (firstArg == QStringLiteral("--unapply-all")) { method = QStringLiteral("unapplyAllPatches"); - } else if (args[1] == QStringLiteral("--backup-working")) { + } else if (firstArg == QStringLiteral("--backup-working")) { method = QStringLiteral("backupWorkingPatchList"); - } else if (args[1] == QStringLiteral("--restore-working")) { + } else if (firstArg == QStringLiteral("--restore-working")) { method = QStringLiteral("restorePatchList"); } else { + qCritical() << "Something went wrong handling the arguments."; + QCoreApplication::exit(2); return; } + qInfo() << "Called with arguments, will send a D-Bus message and quit."; + QDBusConnection connection = QDBusConnection::systemBus(); QDBusMessage msg = QDBusMessage::createMethodCall(DBUS_SERVICE_NAME, DBUS_PATH_NAME, DBUS_SERVICE_NAME, method); if (!data.isEmpty()) { msg.setArguments(data); @@ -1186,6 +1190,10 @@ void PatchManagerObject::process() QCoreApplication::exit(0); return; + } else { + qCritical() << "Something went wrong handling the arguments."; + QCoreApplication::exit(2); + return; } }