-
Notifications
You must be signed in to change notification settings - Fork 978
Added support systemd for qt-service #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,43 @@ | |
| #include <QMap> | ||
| #include <QSettings> | ||
| #include <QProcess> | ||
| #include <QFile> | ||
|
|
||
| static bool isServiceInited = false; | ||
|
|
||
| static bool isSystemD() | ||
| { | ||
| return QFileInfo::exists("/etc/systemd/"); | ||
| } | ||
|
|
||
| static QString systemDPass() | ||
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return "/etc/systemd/system/"; | ||
| } | ||
|
|
||
| static void initService() | ||
| { | ||
|
|
||
| if (isSystemD() && !isServiceInited) | ||
| { | ||
| QSettings::setPath(QSettings::NativeFormat, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not setPah. It is need caller set. Please see the help of QSettings::setPath.
|
||
| QSettings::SystemScope, | ||
| systemDPass()); | ||
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| isServiceInited = true; | ||
| } | ||
| } | ||
|
|
||
| static QSettings& getSettings(const QString& serviceName) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function isn't need. |
||
| { | ||
| initService(); | ||
|
|
||
| if (isSystemD()) { | ||
| static QSettings res(QSettings::SystemScope, serviceName); | ||
| return res; | ||
| } | ||
| static QSettings res(QSettings::SystemScope, serviceName); | ||
| return res; | ||
| } | ||
|
|
||
| static QString encodeName(const QString &name, bool allowUpper = false) | ||
| { | ||
|
|
@@ -138,6 +175,9 @@ static QString absPath(const QString &path) | |
|
|
||
| QString QtServiceBasePrivate::filePath() const | ||
| { | ||
| if (!serviceCustomPass.isEmpty()) | ||
| return serviceCustomPass; | ||
|
|
||
| QString ret; | ||
| if (args.isEmpty()) | ||
| return ret; | ||
|
|
@@ -149,7 +189,7 @@ QString QtServiceBasePrivate::filePath() const | |
|
|
||
| QString QtServiceController::serviceDescription() const | ||
| { | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
| auto &settings = getSettings(serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. |
||
| settings.beginGroup("services"); | ||
| settings.beginGroup(serviceName()); | ||
|
|
||
|
|
@@ -163,7 +203,7 @@ QString QtServiceController::serviceDescription() const | |
|
|
||
| QtServiceController::StartupType QtServiceController::startupType() const | ||
| { | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
| auto &settings = getSettings(serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. |
||
| settings.beginGroup("services"); | ||
| settings.beginGroup(serviceName()); | ||
|
|
||
|
|
@@ -177,7 +217,7 @@ QtServiceController::StartupType QtServiceController::startupType() const | |
|
|
||
| QString QtServiceController::serviceFilePath() const | ||
| { | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
| auto &settings = getSettings(serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. |
||
| settings.beginGroup("services"); | ||
| settings.beginGroup(serviceName()); | ||
|
|
||
|
|
@@ -191,7 +231,23 @@ QString QtServiceController::serviceFilePath() const | |
|
|
||
| bool QtServiceController::uninstall() | ||
| { | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
|
|
||
| if (isSystemD()) { | ||
| uninstallSysD(systemDPass() + serviceName() + ".service"); | ||
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return uninstallUpStart(); | ||
| } | ||
|
|
||
| bool QtServiceController::uninstallSysD(const QString &serviceFile) | ||
| { | ||
| return QFile::remove(serviceFile); | ||
|
|
||
| } | ||
|
|
||
| bool QtServiceController::uninstallUpStart() | ||
| { | ||
| auto &settings = getSettings(serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. use QSettings settings(QSettings::SystemScope, "QtSoftware"); |
||
| settings.beginGroup("services"); | ||
|
|
||
| settings.remove(serviceName()); | ||
|
|
@@ -240,7 +296,12 @@ bool QtServiceController::sendCommand(int code) | |
|
|
||
| bool QtServiceController::isInstalled() const | ||
| { | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
|
|
||
| if (isSystemD()) { | ||
| return QFileInfo::exists(systemDPass() + serviceName() + ".service"); | ||
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| auto &settings = getSettings(serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. use QSettings settings(QSettings::SystemScope, "QtSoftware"); |
||
| settings.beginGroup("services"); | ||
|
|
||
| QStringList list = settings.childGroups(); | ||
|
|
@@ -297,7 +358,7 @@ private slots: | |
| }; | ||
|
|
||
| QtServiceSysPrivate::QtServiceSysPrivate() | ||
| : QtUnixServerSocket(), ident(0), serviceFlags(0) | ||
| : QtUnixServerSocket(), ident(nullptr), serviceFlags(nullptr) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. |
||
| { | ||
| } | ||
|
|
||
|
|
@@ -321,7 +382,7 @@ void QtServiceSysPrivate::incomingConnection(int socketDescriptor) | |
|
|
||
| void QtServiceSysPrivate::slotReady() | ||
| { | ||
| QTcpSocket *s = (QTcpSocket *)sender(); | ||
| QTcpSocket *s = static_cast<QTcpSocket*>(sender()); | ||
| cache[s] += QString(s->readAll()); | ||
| QString cmd = getCommand(s); | ||
| while (!cmd.isEmpty()) { | ||
|
|
@@ -362,7 +423,7 @@ void QtServiceSysPrivate::slotReady() | |
|
|
||
| void QtServiceSysPrivate::slotClosed() | ||
| { | ||
| QTcpSocket *s = (QTcpSocket *)sender(); | ||
| QTcpSocket *s = static_cast<QTcpSocket *>(sender()); | ||
| s->deleteLater(); | ||
| } | ||
|
|
||
|
|
@@ -400,7 +461,7 @@ void QtServiceBasePrivate::sysCleanup() | |
| if (sysd) { | ||
| sysd->close(); | ||
| delete sysd; | ||
| sysd = 0; | ||
| sysd = nullptr; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -417,10 +478,62 @@ bool QtServiceBasePrivate::start() | |
| } | ||
|
|
||
| bool QtServiceBasePrivate::install(const QString &account, const QString &password) | ||
| { | ||
| if (isSystemD()) { | ||
| return installSysD(account, password); | ||
| } | ||
|
|
||
| return installUpStart(account, password); | ||
| } | ||
|
|
||
| bool QtServiceBasePrivate::installSysD(const QString &account, const QString &password) | ||
| { | ||
| Q_UNUSED(account) | ||
| Q_UNUSED(password) | ||
|
|
||
| auto &settings = getSettings(controller.serviceName()); | ||
|
|
||
| settings.beginGroup("Unit"); | ||
| settings.setValue("Description", serviceDescription); | ||
| settings.endGroup(); | ||
|
|
||
| settings.beginGroup("Service"); | ||
| settings.setValue("Type", "forking"); | ||
| settings.setValue("User", "root"); | ||
| settings.setValue("Group", "root"); | ||
| settings.setValue("ExecStart", filePath()); | ||
| settings.setValue("ExecStop", filePath() + " -t"); | ||
| settings.endGroup(); | ||
|
|
||
| settings.beginGroup("Timer"); | ||
| settings.setValue("OnStartupSec", "60"); | ||
|
|
||
| settings.endGroup(); | ||
|
|
||
| settings.beginGroup("Install"); | ||
| settings.setValue("WantedBy", "multi-user.target"); | ||
| settings.endGroup(); | ||
|
|
||
| settings.sync(); | ||
|
|
||
| bool renamed = QFile(systemDPass() + controller.serviceName() + ".conf").rename( | ||
| systemDPass() + controller.serviceName() + ".service"); | ||
EndrII marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| QSettings::Status ret = settings.status(); | ||
| if (ret == QSettings::AccessError && renamed) { | ||
| fprintf(stderr, "Cannot install \"%s\". Cannot write to: %s. Check permissions.\n", | ||
| controller.serviceName().toLatin1().constData(), | ||
| settings.fileName().toLatin1().constData()); | ||
| } | ||
| return (ret == QSettings::NoError); | ||
| } | ||
|
|
||
| bool QtServiceBasePrivate::installUpStart(const QString &account, const QString &password) | ||
| { | ||
| Q_UNUSED(account) | ||
| Q_UNUSED(password) | ||
| QSettings settings(QSettings::SystemScope, "QtSoftware"); | ||
|
|
||
| auto &settings = getSettings(controller.serviceName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are not modify. |
||
|
|
||
| settings.beginGroup("services"); | ||
| settings.beginGroup(controller.serviceName()); | ||
|
|
@@ -480,3 +593,7 @@ void QtServiceBase::setServiceFlags(QtServiceBase::ServiceFlags flags) | |
| d_ptr->sysd->serviceFlags = flags; | ||
| } | ||
|
|
||
| void QtServiceBase::setServiceExecutable(const QString &exec) | ||
| { | ||
| d_ptr->serviceCustomPass= exec; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.