summaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: 695fe7d0838b5b5d1a0ab091b295736d408b0e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <QCoreApplication>
#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);
    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;
}