summaryrefslogtreecommitdiffstats
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp127
1 files changed, 124 insertions, 3 deletions
diff --git a/main.cpp b/main.cpp
index 23e4330..695fe7d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,9 +1,130 @@
#include <QCoreApplication>
-#include "daemon.h"
+#include <QFile>
+#include <QDebug>
+#include <errno.h>
+#include <unistd.h>
+#include <QStringList>
+
+#define PID_FILE "/data/user/.appcontroller"
+
+static void loadDefaults(QStringList &defaultArgs)
+{
+ QFile f("/system/bin/appdaemon.conf");
+
+ if (!f.open(QFile::ReadOnly)) {
+ qWarning("Could not read config file.");
+ return;
+ }
+
+ while (!f.atEnd()) {
+ QString line = f.readLine();
+ if (line.startsWith("env=")) {
+ QString sub = line.mid(4).simplified();
+ int index = sub.indexOf('=');
+ if (index < 2) {
+ // ignore
+ } else {
+ setenv(sub.left(index).toLocal8Bit().constData(), sub.mid(index+1).toLocal8Bit().constData(), 1);
+ qDebug() << sub.left(index) << sub.mid(index+1);
+ }
+ } else if (line.startsWith("append=")) {
+ defaultArgs += line.mid(7).simplified();
+ qDebug() << defaultArgs;
+ }
+ }
+
+ // env=...
+ // append=...
+}
+
+static pid_t lastPID()
+{
+ QFile f(PID_FILE);
+ if (!f.open(QFile::ReadOnly)) {
+ qDebug() << "Could not open" << f.fileName();
+ return 0;
+ }
+ bool ok;
+ pid_t pid = f.readAll().toUInt(&ok);
+ f.close();
+ if (!ok)
+ qFatal("Invalid last PID.");
+
+ return pid;
+}
+
+static void stop()
+{
+ pid_t pid = lastPID();
+ if (pid == 0)
+ return;
+
+ int rc = ::kill(pid, SIGTERM);
+ if (rc != 0) {
+ if (errno == ESRCH)
+ return;
+ else
+ qFatal("Kill not permitted/invalid");
+ }
+
+ sleep(1);
+
+ rc = ::kill(pid, SIGKILL);
+ if (rc != 0) {
+ if (errno == ESRCH)
+ return;
+ else
+ qFatal("Kill not permitted/invalid");
+ }
+}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
- Daemon daemon;
- return app.exec();
+ QStringList defaultArgs;
+ QString binary;
+
+ QStringList args = app.arguments();
+ args.removeFirst();
+ if (args.size() == 0)
+ qFatal("No arguments given.");
+
+ while (!args.isEmpty()) {
+ if (args[0] == "--start") {
+ if (args.size() < 2) {
+ qFatal("--start requires and argument");
+ }
+ binary = args[1];
+ args.removeFirst();
+ if (binary.isEmpty())
+ qFatal("App path is empty");
+ stop();
+ loadDefaults(defaultArgs);
+ } else if (args[0] == "--stop") {
+ stop();
+ return 0;
+ } else {
+ qFatal("unknown argument: %s", args.first().toLocal8Bit().constData());
+ }
+ args.removeFirst();
+ }
+
+ defaultArgs.push_front(binary);
+
+ char **arglist = new char*[defaultArgs.size()+1];
+ for (int i = 0; i < defaultArgs.size(); i++) {
+ arglist[i] = strdup(defaultArgs[i].toLocal8Bit().constData());
+ }
+ arglist[defaultArgs.size()] = 0;
+ defaultArgs.clear();
+
+ QFile f(PID_FILE);
+ if (!f.open(QFile::WriteOnly))
+ qDebug() << "Could not write PID";
+ f.write(QString::number(getpid()).toLatin1());
+ f.close();
+
+ execv(binary.toLocal8Bit().constData(), arglist);
+
+ return 0;
}