summaryrefslogtreecommitdiffstats
path: root/daemon.cpp
blob: cd87dc4ae3206ecf9fd491793126f54106351578 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "daemon.h"
#include "protocol.h"
#include "app.h"

#include <QTcpServer>
#include <QTcpSocket>
#include <QFile>

Daemon::Daemon(QObject *parent)
    : QObject(parent)
    , mServer(new QTcpServer(this))
    , mClient(0)
    , mProtocol(new Protocol(this))
    , mApp(new App(this))
    , mPort(10066)
{
    loadDefaults();

    if (!mServer->listen(QHostAddress::Any, mPort))
        qDebug() << "Could not listen:" << mServer->errorString();

    connect(mServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
    connect(mProtocol, SIGNAL(commandReceived(const QStringList &)), this, SLOT(printCommand(const QStringList &)));
    connect(mProtocol, SIGNAL(commandReceived(const QStringList &)), this, SLOT(handleCommands(const QStringList &)));
    connect(mApp, SIGNAL(started(pid_t)), this, SLOT(appStarted(pid_t)));
    connect(mApp, SIGNAL(stopped(pid_t,int,int)), this, SLOT(appStopped(pid_t,int,int)));
    connect(mApp, SIGNAL(stdOut(pid_t,const QByteArray&)), this, SLOT(appStdout(pid_t,const QByteArray&)));
    connect(mApp, SIGNAL(stdErr(pid_t,const QByteArray&)), this, SLOT(appStderr(pid_t,const QByteArray&)));
    connect(mApp, SIGNAL(debugging(pid_t,quint16)), this, SLOT(appDebugging(pid_t,quint16)));
    connect(mApp, SIGNAL(error(const QString&)), this, SLOT(appError(const QString&)));

    if (!defaultApp.isEmpty())
        mApp->start(defaultApp, defaultArgs);
}

Daemon::~Daemon()
{
}

void Daemon::newConnection()
{
    if (!mServer->hasPendingConnections())
        return;

    clientDisconnect();
    mClient = mServer->nextPendingConnection();
    connect(mClient, SIGNAL(disconnected()), this, SLOT(clientDisconnect()));
    connect(mClient, SIGNAL(readyRead()), this, SLOT(clientRead()));
}

void Daemon::printCommand(const QStringList &l)
{
    qDebug() << l;
}

void Daemon::clientRead()
{
    mProtocol->write(mClient->readAll());
}

void Daemon::clientDisconnect()
{
    delete mClient;
    mClient = 0;
}

void Daemon::handleCommands(const QStringList &list)
{
    if (list.size() == 0) {
        qDebug() << "no commands";
        return;
    }

    const QString command(list.first());
    if (command == "start") {
        if (list.size() < 2) {
            qDebug() << "too less options";
            return;
        }
        mApp->start(list[1], defaultArgs + list.mid(2));
    } else if (command == "stop") {
        mApp->stop();
    } else if (command == "debug") {
        qDebug() << "debug not implemented";
    } else if (command == "stdin") {
        if(list.size() < 2) {
            qDebug() << "too less options";
            return;
        }
        mApp->write(list[1].toLocal8Bit());
    } else if (command == "env") {
        if(list.size() == 2) {
            mApp->delEnv(list[1]);
        } else if (list.size() == 3) {
            mApp->addEnv(list[1], list[2]);
        } else {
            qDebug() << "Invalid arg count for env";
            return;
        }
    } else {
        qDebug() << "unknown command:" << list[0];
    }

}

void Daemon::appStarted(pid_t pid)
{
    if (!mClient)
        return;
    QStringList l("started");
    l += QString::number(pid);
    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::appStopped(pid_t pid, int exitStatus, int exitCode)
{
    if (!mClient)
        return;
    QStringList l("stopped");
    l += QString::number(pid);
    l += QString::number(exitStatus);
    l += QString::number(exitCode);

    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::appStdout(pid_t pid, const QByteArray &data)
{
    if (!mClient)
        return;
    QStringList l("stdout");
    l += QString::number(pid);
    l += QString::fromLocal8Bit(data);
    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::appStderr(pid_t pid, const QByteArray &data)
{
    if (!mClient)
        return;
    QStringList l("stderr");
    l += QString::number(pid);
    l += data;
    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::appDebugging(pid_t pid, quint16 port)
{
    if (!mClient)
        return;
    QStringList l("debugging");
    l += QString::number(pid);
    l += QString::number(port);
    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::appError(const QString &text)
{
    if (!mClient)
        return;
    QStringList l("error");
    l += text;
    mClient->write(mProtocol->sendToClient(l).toLocal8Bit());
}

void Daemon::loadDefaults()
{
    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("start=")) {
            defaultApp = line.mid(6).simplified();
            qDebug() << "start=" << defaultApp;
        } else if (line.startsWith("port=")) {
            mPort = line.mid(5).simplified().toUInt();
            if (mPort == 0)
                qFatal("Invalid port");
        } else if (line.startsWith("env=")) {
                QString sub = line.mid(4).simplified();
                int index = sub.indexOf('=');
                if (index < 2) {
                    // ignore
                } else {
                    mApp->addEnv(sub.left(index), sub.mid(index+1));
                    qDebug() << sub.left(index) << sub.mid(index+1);
                }
        } else if (line.startsWith("append=")) {
              defaultArgs += line.mid(7).simplified();
              qDebug() << defaultArgs;
        }
    }

    // start=/usr/bin/launcher
    // port=10066
    // env=...
    // append=...
}