Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions qtservice/src/qtservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class QT_QTSERVICE_EXPORT QtServiceController
bool sendCommand(int code);

private:
bool uninstallSysD(const QString &serviceName);
bool uninstallUpStart();

QtServiceControllerPrivate *d_ptr;
};

Expand Down Expand Up @@ -132,6 +135,7 @@ class QT_QTSERVICE_EXPORT QtServiceBase

ServiceFlags serviceFlags() const;
void setServiceFlags(ServiceFlags flags);
void setServiceExecutable(const QString& exec);

int exec();

Expand Down
5 changes: 4 additions & 1 deletion qtservice/src/qtservice_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class QtServiceBasePrivate
QtServiceBase *q_ptr;

QString serviceDescription;
QString serviceCustomPass;

QtServiceController::StartupType startupType;
QtServiceBase::ServiceFlags serviceFlags;
QStringList args;
Expand All @@ -74,7 +76,8 @@ class QtServiceBasePrivate
void startService();
int run(bool asService, const QStringList &argList);
bool install(const QString &account, const QString &password);

bool installSysD(const QString &account, const QString &password);
bool installUpStart(const QString &account, const QString &password);
bool start();

QString filePath() const;
Expand Down
137 changes: 127 additions & 10 deletions qtservice/src/qtservice_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
return "/etc/systemd/system/";
}

static void initService()
{

if (isSystemD() && !isServiceInited)
{
QSettings::setPath(QSettings::NativeFormat,
Copy link

@KangLin KangLin Jul 9, 2021

Choose a reason for hiding this comment

The 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.
Two solutions:

  1. The caller is call QSettings::setPath (The old way)
  2. Call QSettings::setPath in Constructor

QSettings::SystemScope,
systemDPass());
isServiceInited = true;
}
}

static QSettings& getSettings(const QString& serviceName)
Copy link

Choose a reason for hiding this comment

The 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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -149,7 +189,7 @@ QString QtServiceBasePrivate::filePath() const

QString QtServiceController::serviceDescription() const
{
QSettings settings(QSettings::SystemScope, "QtSoftware");
auto &settings = getSettings(serviceName());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not modify.

settings.beginGroup("services");
settings.beginGroup(serviceName());

Expand All @@ -163,7 +203,7 @@ QString QtServiceController::serviceDescription() const

QtServiceController::StartupType QtServiceController::startupType() const
{
QSettings settings(QSettings::SystemScope, "QtSoftware");
auto &settings = getSettings(serviceName());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not modify.

settings.beginGroup("services");
settings.beginGroup(serviceName());

Expand All @@ -177,7 +217,7 @@ QtServiceController::StartupType QtServiceController::startupType() const

QString QtServiceController::serviceFilePath() const
{
QSettings settings(QSettings::SystemScope, "QtSoftware");
auto &settings = getSettings(serviceName());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not modify.

settings.beginGroup("services");
settings.beginGroup(serviceName());

Expand All @@ -191,7 +231,23 @@ QString QtServiceController::serviceFilePath() const

bool QtServiceController::uninstall()
{
QSettings settings(QSettings::SystemScope, "QtSoftware");

if (isSystemD()) {
uninstallSysD(systemDPass() + serviceName() + ".service");
}

return uninstallUpStart();
}

bool QtServiceController::uninstallSysD(const QString &serviceFile)
{
return QFile::remove(serviceFile);

}

bool QtServiceController::uninstallUpStart()
{
auto &settings = getSettings(serviceName());
Copy link

Choose a reason for hiding this comment

The 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());
Expand Down Expand Up @@ -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");
}

auto &settings = getSettings(serviceName());
Copy link

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -297,7 +358,7 @@ private slots:
};

QtServiceSysPrivate::QtServiceSysPrivate()
: QtUnixServerSocket(), ident(0), serviceFlags(0)
: QtUnixServerSocket(), ident(nullptr), serviceFlags(nullptr)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not modify.

{
}

Expand All @@ -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()) {
Expand Down Expand Up @@ -362,7 +423,7 @@ void QtServiceSysPrivate::slotReady()

void QtServiceSysPrivate::slotClosed()
{
QTcpSocket *s = (QTcpSocket *)sender();
QTcpSocket *s = static_cast<QTcpSocket *>(sender());
s->deleteLater();
}

Expand Down Expand Up @@ -400,7 +461,7 @@ void QtServiceBasePrivate::sysCleanup()
if (sysd) {
sysd->close();
delete sysd;
sysd = 0;
sysd = nullptr;
}
}

Expand All @@ -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");

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());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not modify.


settings.beginGroup("services");
settings.beginGroup(controller.serviceName());
Expand Down Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions qtservice/src/qtservice_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ bool QtServiceBasePrivate::install(const QString &account, const QString &passwo

QString QtServiceBasePrivate::filePath() const
{
if (!serviceCustomPass.isEmpty())
return serviceCustomPass;

wchar_t path[_MAX_PATH];
::GetModuleFileNameW( 0, path, sizeof(path) );
return QString::fromUtf16((unsigned short*)path);
Expand Down Expand Up @@ -949,4 +952,9 @@ void QtServiceBase::setServiceFlags(QtServiceBase::ServiceFlags flags)
d_ptr->sysd->setServiceFlags(flags);
}

void QtServiceBase::setServiceExecutable(const QString &exec)
{
d_ptr->serviceCustomPass= exec;
}