From 08fa00a2c2ce125d83b8de2cadf1f941dec6c55a Mon Sep 17 00:00:00 2001 From: EndrII Date: Mon, 25 Oct 2021 19:54:15 +0300 Subject: [PATCH] added support of custom users --- Patronum/src/Private/installersystemd.cpp | 10 +++++++--- Patronum/src/Private/pcommon.cpp | 22 +++++++++++++++++++--- Patronum/src/Private/pcommon.h | 6 ++++-- Patronum/src/Private/serviceprivate.cpp | 5 +---- Patronum/src/Private/serviceprivate.h | 2 +- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Patronum/src/Private/installersystemd.cpp b/Patronum/src/Private/installersystemd.cpp index c33753e..9609f09 100644 --- a/Patronum/src/Private/installersystemd.cpp +++ b/Patronum/src/Private/installersystemd.cpp @@ -47,10 +47,14 @@ bool InstallerSystemD::install(const QString &executable, const QString& user) { templ.close(); service = service.arg(executable); - service = service.arg(PCommon::instance()->getPidfile()); - service = service.arg(PCommon::instance()->getPWD()); + service = service.arg(PCommon::instance()->getPidfile(user)); + service = service.arg(PCommon::instance()->getPWD(user)); service = service.arg(PCommon::instance()->getServiceName()); - service = service.arg(user); + + if (user.isEmpty()) + service = service.arg(DEFAULT_USER); + else + service = service.arg(user); templ.setFileName(absaluteServicePath()); diff --git a/Patronum/src/Private/pcommon.cpp b/Patronum/src/Private/pcommon.cpp index 11190fa..e9e1bf2 100644 --- a/Patronum/src/Private/pcommon.cpp +++ b/Patronum/src/Private/pcommon.cpp @@ -3,6 +3,7 @@ #include #include +#include "quasarapp.h" namespace Patronum { @@ -13,8 +14,8 @@ const class PCommon *PCommon::instance() { return inst; } -QString PCommon::getPidfile() const { - return getPWD() + "/" + getServiceName() + ".pid"; +QString PCommon::getPidfile(const QString& customUser) const { + return getPWD(customUser) + "/" + getServiceName() + ".pid"; } qint64 PCommon::getPidFromPidfile() const { @@ -30,8 +31,23 @@ qint64 PCommon::getPidFromPidfile() const { return data.toLongLong(); } -QString PCommon::getPWD() const { +QString PCommon::getPWD(const QString& customUser) const { + if (!customUser.size()) + return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + +#ifdef Q_OS_LINUX + if (customUser == "root") + return "/root/.local/share/" + getServiceName(); + + return "/home/" + customUser + "/.local/share/" + getServiceName(); +#else + + QuasarAppUtils::Params::log("The custom user not support for " + + QSysInfo::kernelType(), + QuasarAppUtils::Error); + return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); +#endif } QString PCommon::getServiceName() const { diff --git a/Patronum/src/Private/pcommon.h b/Patronum/src/Private/pcommon.h index a7e98c1..568ab28 100644 --- a/Patronum/src/Private/pcommon.h +++ b/Patronum/src/Private/pcommon.h @@ -26,9 +26,10 @@ public: /** * @brief pidFilePath This method return the absalute path to the service pid file. + * @param customUser This is name of custom user for that will return PWD dir. * @return absalute path to the service pid file. */ - QString getPidfile() const; + QString getPidfile(const QString& customUser = "") const; /** * @brief getPidFromPidfile This method return the pid from pidFile. @@ -38,9 +39,10 @@ public: /** * @brief getPWD This method return PWD of the service. + * @param customUser This is name of custom user for that will return PWD dir. * @return absalute path of the work directory. */ - QString getPWD() const; + QString getPWD(const QString &customUser = "") const; /** * @brief getServiceName return the service name. diff --git a/Patronum/src/Private/serviceprivate.cpp b/Patronum/src/Private/serviceprivate.cpp index d54ca58..b41f8f3 100644 --- a/Patronum/src/Private/serviceprivate.cpp +++ b/Patronum/src/Private/serviceprivate.cpp @@ -78,7 +78,7 @@ bool ServicePrivate::sendCloseConnection() { return _socket->send(_parser->createPackage(Command::CloseConnection)); } -bool ServicePrivate::install(QString user) { +bool ServicePrivate::install(const QString &user) { if (!_installer) { QuasarAppUtils::Params::log(errorToString(UnsupportedPlatform), @@ -86,9 +86,6 @@ bool ServicePrivate::install(QString user) { return false; } - if (user.isEmpty()) - user = DEFAULT_USER; - if (!_installer->install(getServiceLauncher(), user)) { return false; } diff --git a/Patronum/src/Private/serviceprivate.h b/Patronum/src/Private/serviceprivate.h index 8170f59..c7f5e87 100644 --- a/Patronum/src/Private/serviceprivate.h +++ b/Patronum/src/Private/serviceprivate.h @@ -51,7 +51,7 @@ public: * @param user This is name of custom user that will be run your service after reboot system. * @return return true if the service installed succesful */ - bool install(QString user); + bool install(const QString &user); /** * @brief uninstall This method unistall your service in thec current system.