1168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat/*
2168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * Copyright (C) 2008 The Android Open Source Project
3168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat *
4168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * you may not use this file except in compliance with the License.
6168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * You may obtain a copy of the License at
7168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat *
8168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat *
10168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * Unless required by applicable law or agreed to in writing, software
11168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * See the License for the specific language governing permissions and
14168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat * limitations under the License.
15168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat */
16168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat#include <errno.h>
173d40729054803fae1c4d4bb5ac7554665a132b26San Mehat#include <string.h>
18b93e5812faffd3b6c5fb349072413aace31918d8Olivier Bailly#include <stdlib.h>
19168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
20168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat#define LOG_TAG "FrameworkListener"
21168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
22168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat#include <cutils/log.h>
23168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
24168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat#include <sysutils/FrameworkListener.h>
25168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat#include <sysutils/FrameworkCommand.h>
26fa644ffe944c01a9b00f8d7676d58394fabee285San Mehat#include <sysutils/SocketClient.h>
27168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
288702bb17f40022e970e8acd40b348d074e39afc7Robert GreenwaltFrameworkListener::FrameworkListener(const char *socketName, bool withSeq) :
298702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                            SocketListener(socketName, true, withSeq) {
308702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    init(socketName, withSeq);
318702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt}
328702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt
33168415b822cae1f8b54ef09c41c11a9b97b87f40San MehatFrameworkListener::FrameworkListener(const char *socketName) :
348702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                            SocketListener(socketName, true, false) {
358702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    init(socketName, false);
368702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt}
378702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt
388702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwaltvoid FrameworkListener::init(const char *socketName, bool withSeq) {
39168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    mCommands = new FrameworkCommandCollection();
408702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    errorRate = 0;
418702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    mCommandCount = 0;
428702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    mWithSeq = withSeq;
43168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat}
44168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
45fa644ffe944c01a9b00f8d7676d58394fabee285San Mehatbool FrameworkListener::onDataAvailable(SocketClient *c) {
46d768066ef54270a0d3ccfccd50ae8238db5a2cddSan Mehat    char buffer[255];
47168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    int len;
48168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
49c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    len = TEMP_FAILURE_RETRY(read(c->getSocket(), buffer, sizeof(buffer)));
50c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    if (len < 0) {
517e8529a8b528fd30586aa037f15a31b29582c537San Mehat        SLOGE("read() failed (%s)", strerror(errno));
52f31d2ed1fd3a39a92bccc12eb66728594290ef3bKenny Root        return false;
53c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat    } else if (!len)
54168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat        return false;
55168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
56d768066ef54270a0d3ccfccd50ae8238db5a2cddSan Mehat    int offset = 0;
57168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    int i;
58168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
59168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    for (i = 0; i < len; i++) {
60c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat        if (buffer[i] == '\0') {
61c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner            /* IMPORTANT: dispatchCommand() expects a zero-terminated string */
62d768066ef54270a0d3ccfccd50ae8238db5a2cddSan Mehat            dispatchCommand(c, buffer + offset);
63d768066ef54270a0d3ccfccd50ae8238db5a2cddSan Mehat            offset = i + 1;
64168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat        }
65168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    }
66168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    return true;
67168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat}
68168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
69168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehatvoid FrameworkListener::registerCmd(FrameworkCommand *cmd) {
70168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    mCommands->push_back(cmd);
71168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat}
72168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
73c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehatvoid FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
74c4a895b7094461c98101924cf096680bfb7856f1San Mehat    FrameworkCommandCollection::iterator i;
75c4a895b7094461c98101924cf096680bfb7856f1San Mehat    int argc = 0;
76c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat    char *argv[FrameworkListener::CMD_ARGS_MAX];
77c4a895b7094461c98101924cf096680bfb7856f1San Mehat    char tmp[255];
78c4a895b7094461c98101924cf096680bfb7856f1San Mehat    char *p = data;
79c4a895b7094461c98101924cf096680bfb7856f1San Mehat    char *q = tmp;
80c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    char *qlimit = tmp + sizeof(tmp) - 1;
81c4a895b7094461c98101924cf096680bfb7856f1San Mehat    bool esc = false;
82c4a895b7094461c98101924cf096680bfb7856f1San Mehat    bool quote = false;
83c4a895b7094461c98101924cf096680bfb7856f1San Mehat    int k;
848702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    bool haveCmdNum = !mWithSeq;
85c4a895b7094461c98101924cf096680bfb7856f1San Mehat
86c4a895b7094461c98101924cf096680bfb7856f1San Mehat    memset(argv, 0, sizeof(argv));
87c4a895b7094461c98101924cf096680bfb7856f1San Mehat    memset(tmp, 0, sizeof(tmp));
88c4a895b7094461c98101924cf096680bfb7856f1San Mehat    while(*p) {
89c4a895b7094461c98101924cf096680bfb7856f1San Mehat        if (*p == '\\') {
90c4a895b7094461c98101924cf096680bfb7856f1San Mehat            if (esc) {
91c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                if (q >= qlimit)
92c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                    goto overflow;
93c4a895b7094461c98101924cf096680bfb7856f1San Mehat                *q++ = '\\';
94c4a895b7094461c98101924cf096680bfb7856f1San Mehat                esc = false;
95c4a895b7094461c98101924cf096680bfb7856f1San Mehat            } else
96c4a895b7094461c98101924cf096680bfb7856f1San Mehat                esc = true;
97c4a895b7094461c98101924cf096680bfb7856f1San Mehat            p++;
98c4a895b7094461c98101924cf096680bfb7856f1San Mehat            continue;
99c4a895b7094461c98101924cf096680bfb7856f1San Mehat        } else if (esc) {
100c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner            if (*p == '"') {
101c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                if (q >= qlimit)
102c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                    goto overflow;
103c4a895b7094461c98101924cf096680bfb7856f1San Mehat                *q++ = '"';
104c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner            } else if (*p == '\\') {
105c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                if (q >= qlimit)
106c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner                    goto overflow;
107c4a895b7094461c98101924cf096680bfb7856f1San Mehat                *q++ = '\\';
108c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner            } else {
109c4a895b7094461c98101924cf096680bfb7856f1San Mehat                cli->sendMsg(500, "Unsupported escape sequence", false);
110c4a895b7094461c98101924cf096680bfb7856f1San Mehat                goto out;
111c4a895b7094461c98101924cf096680bfb7856f1San Mehat            }
112c4a895b7094461c98101924cf096680bfb7856f1San Mehat            p++;
113c4a895b7094461c98101924cf096680bfb7856f1San Mehat            esc = false;
114c4a895b7094461c98101924cf096680bfb7856f1San Mehat            continue;
115c4a895b7094461c98101924cf096680bfb7856f1San Mehat        }
116c4a895b7094461c98101924cf096680bfb7856f1San Mehat
117c4a895b7094461c98101924cf096680bfb7856f1San Mehat        if (*p == '"') {
118c4a895b7094461c98101924cf096680bfb7856f1San Mehat            if (quote)
119c4a895b7094461c98101924cf096680bfb7856f1San Mehat                quote = false;
120c4a895b7094461c98101924cf096680bfb7856f1San Mehat            else
121c4a895b7094461c98101924cf096680bfb7856f1San Mehat                quote = true;
122c4a895b7094461c98101924cf096680bfb7856f1San Mehat            p++;
123c4a895b7094461c98101924cf096680bfb7856f1San Mehat            continue;
124c4a895b7094461c98101924cf096680bfb7856f1San Mehat        }
125c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat
126c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner        if (q >= qlimit)
127c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner            goto overflow;
128c4a895b7094461c98101924cf096680bfb7856f1San Mehat        *q = *p++;
129c4a895b7094461c98101924cf096680bfb7856f1San Mehat        if (!quote && *q == ' ') {
130c4a895b7094461c98101924cf096680bfb7856f1San Mehat            *q = '\0';
1318702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt            if (!haveCmdNum) {
1328702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                char *endptr;
1338702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                int cmdNum = (int)strtol(tmp, &endptr, 0);
1348702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                if (endptr == NULL || *endptr != '\0') {
1358702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                    cli->sendMsg(500, "Invalid sequence number", false);
1368702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                    goto out;
1378702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                }
1388702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                cli->setCmdNum(cmdNum);
1398702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                haveCmdNum = true;
1408702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt            } else {
1418702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                if (argc >= CMD_ARGS_MAX)
1428702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                    goto overflow;
1438702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt                argv[argc++] = strdup(tmp);
1448702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt            }
145c4a895b7094461c98101924cf096680bfb7856f1San Mehat            memset(tmp, 0, sizeof(tmp));
146c4a895b7094461c98101924cf096680bfb7856f1San Mehat            q = tmp;
147c4a895b7094461c98101924cf096680bfb7856f1San Mehat            continue;
148c4a895b7094461c98101924cf096680bfb7856f1San Mehat        }
149c4a895b7094461c98101924cf096680bfb7856f1San Mehat        q++;
150fa644ffe944c01a9b00f8d7676d58394fabee285San Mehat    }
151fa644ffe944c01a9b00f8d7676d58394fabee285San Mehat
152c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    *q = '\0';
153c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    if (argc >= CMD_ARGS_MAX)
154c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner        goto overflow;
155c4a895b7094461c98101924cf096680bfb7856f1San Mehat    argv[argc++] = strdup(tmp);
156c4a895b7094461c98101924cf096680bfb7856f1San Mehat#if 0
157c4a895b7094461c98101924cf096680bfb7856f1San Mehat    for (k = 0; k < argc; k++) {
1587e8529a8b528fd30586aa037f15a31b29582c537San Mehat        SLOGD("arg[%d] = '%s'", k, argv[k]);
159c4a895b7094461c98101924cf096680bfb7856f1San Mehat    }
160c4a895b7094461c98101924cf096680bfb7856f1San Mehat#endif
161168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
162c4a895b7094461c98101924cf096680bfb7856f1San Mehat    if (quote) {
163c4a895b7094461c98101924cf096680bfb7856f1San Mehat        cli->sendMsg(500, "Unclosed quotes error", false);
164c4a895b7094461c98101924cf096680bfb7856f1San Mehat        goto out;
165c4a895b7094461c98101924cf096680bfb7856f1San Mehat    }
166c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner
1678702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    if (errorRate && (++mCommandCount % errorRate == 0)) {
1688702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt        /* ignore this command - let the timeout handler handle it */
1698702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt        SLOGE("Faking a timeout");
1708702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt        goto out;
1718702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt    }
1728702bb17f40022e970e8acd40b348d074e39afc7Robert Greenwalt
173168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    for (i = mCommands->begin(); i != mCommands->end(); ++i) {
174168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat        FrameworkCommand *c = *i;
175168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
176c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat        if (!strcmp(argv[0], c->getCommand())) {
177c73a3a5771a2d29d1bae666bfde12f751d66fc96San Mehat            if (c->runCommand(cli, argc, argv)) {
1787e8529a8b528fd30586aa037f15a31b29582c537San Mehat                SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
179168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat            }
180c4a895b7094461c98101924cf096680bfb7856f1San Mehat            goto out;
181168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat        }
182168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat    }
183168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat
184d768066ef54270a0d3ccfccd50ae8238db5a2cddSan Mehat    cli->sendMsg(500, "Command not recognized", false);
185c4a895b7094461c98101924cf096680bfb7856f1San Mehatout:
186c4a895b7094461c98101924cf096680bfb7856f1San Mehat    int j;
187c4a895b7094461c98101924cf096680bfb7856f1San Mehat    for (j = 0; j < argc; j++)
188c4a895b7094461c98101924cf096680bfb7856f1San Mehat        free(argv[j]);
189fa644ffe944c01a9b00f8d7676d58394fabee285San Mehat    return;
190c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner
191c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turneroverflow:
1926bbaaa630195a1bcfa90c168a354f76b90125ed6Nick Kralevich    LOG_EVENT_INT(78001, cli->getUid());
193c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    cli->sendMsg(500, "Command too long", false);
194c6b0def5f039dc3bbe1d4b7dc1666c24316eb020David 'Digit' Turner    goto out;
195168415b822cae1f8b54ef09c41c11a9b97b87f40San Mehat}
196