Skip to content
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

Use systemd instead of upstart #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
41 changes: 25 additions & 16 deletions asteroidsyncservice/platforms/ubuntutouch/servicecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool ServiceControl::serviceFileInstalled() const
qDebug() << "Service name not set.";
return false;
}
QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
QFile f(QDir::homePath() + "/.config/systemd/user/"+ m_serviceName + ".service");
return f.exists();
}

Expand All @@ -41,7 +41,7 @@ bool ServiceControl::installServiceFile()
return false;
}

QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
QFile f(QDir::homePath() + "/.config/systemd/user/"+ m_serviceName + ".service");
if (f.exists()) {
qDebug() << "Service file already existing...";
return false;
Expand All @@ -56,11 +56,20 @@ bool ServiceControl::installServiceFile()
// Try to replace version with "current" to be more robust against updates
appDir.replace(QRegExp("[0-9].[0-9].[0-9]"), "current");

f.write("start on started unity8\n");
f.write("pre-start script\n");
f.write(" initctl set-env LD_LIBRARY_PATH=" + appDir.toUtf8() + "/usr/bin/../:$LD_LIBRARY_PATH\n");
f.write("end script\n");
f.write("exec " + appDir.toUtf8() + "/usr/bin/" + m_serviceName.toUtf8() + "\n");
f.write("[Unit]\n");
f.write("Description=asteroidsync\n");
f.write("After=graphical.target\n");
f.write("[Service]\n");
f.write("ExecStart=" + appDir.toUtf8() + "/usr/bin/" + m_serviceName.toUtf8() + "\n");

f.write("Restart=always\n");
f.write("RestartSec=5\n");
f.write("Environment=LD_LIBRARY_PATH=" + appDir.toUtf8() + "/usr/bin/../:$LD_LIBRARY_PATH\n");
f.write("Environment=HOME=%h XDG_CONFIG_HOME=/home/%u/.config DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/bus XDG_RUNTIME_DIR=/run/user/%U\n");
f.write("[Install]\n");
f.write("WantedBy=default.target\n");


f.close();
return true;
}
Expand All @@ -71,18 +80,18 @@ bool ServiceControl::removeServiceFile()
qDebug() << "Service name not set.";
return false;
}
QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf");
QFile f(QDir::homePath() + "/.config/systemd/user/"+ m_serviceName + ".service");
return f.remove();
}

bool ServiceControl::serviceRunning() const
{
QProcess p;
p.start("initctl", {"status", m_serviceName});
p.start("systemctl", {"is-active", "--user", m_serviceName});
p.waitForFinished();
QByteArray output = p.readAll();
qDebug() << output;
return output.contains("running");
return output.contains("active");
}

bool ServiceControl::setServiceRunning(bool running)
Expand All @@ -97,21 +106,21 @@ bool ServiceControl::setServiceRunning(bool running)

bool ServiceControl::startService()
{
qDebug() << "start service";
int ret = QProcess::execute("start", {m_serviceName});
int ret = QProcess::execute("systemctl", {"start", "--user", m_serviceName});
qDebug() << "start service" << ret;
return ret == 0;
}

bool ServiceControl::stopService()
{
qDebug() << "stop service";
int ret = QProcess::execute("stop", {m_serviceName});
int ret = QProcess::execute("systemctl", {"stop", "--user", m_serviceName});
qDebug() << "stop service" << ret;
return ret == 0;
}

bool ServiceControl::restartService()
{
qDebug() << "restart service";
int ret = QProcess::execute("restart", {m_serviceName});
int ret = QProcess::execute("systemctl", {"restart", "--user", m_serviceName});
qDebug() << "restart service" << ret;
return ret == 0;
}