blob: 02a401d4105056e2030e546d389010ae7863ebf0 [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <errno.h>
San Mehat3d407292009-05-07 08:49:30 -070017#include <string.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080018#include <stdlib.h>
San Mehat168415b2009-05-06 11:14:21 -070019
20#define LOG_TAG "FrameworkListener"
21
22#include <cutils/log.h>
23
24#include <sysutils/FrameworkListener.h>
25#include <sysutils/FrameworkCommand.h>
San Mehatfa644ff2009-05-08 11:15:53 -070026#include <sysutils/SocketClient.h>
San Mehat168415b2009-05-06 11:14:21 -070027
Josef Kindberg6d358ae2011-02-23 12:40:44 +010028static const int CMD_BUF_SIZE = 1024;
29
Robert Greenwalt8702bb12012-02-07 12:23:14 -080030FrameworkListener::FrameworkListener(const char *socketName, bool withSeq) :
31 SocketListener(socketName, true, withSeq) {
32 init(socketName, withSeq);
33}
34
San Mehat168415b2009-05-06 11:14:21 -070035FrameworkListener::FrameworkListener(const char *socketName) :
Robert Greenwalt8702bb12012-02-07 12:23:14 -080036 SocketListener(socketName, true, false) {
37 init(socketName, false);
38}
39
40void FrameworkListener::init(const char *socketName, bool withSeq) {
San Mehat168415b2009-05-06 11:14:21 -070041 mCommands = new FrameworkCommandCollection();
Robert Greenwalt8702bb12012-02-07 12:23:14 -080042 errorRate = 0;
43 mCommandCount = 0;
44 mWithSeq = withSeq;
San Mehat168415b2009-05-06 11:14:21 -070045}
46
San Mehatfa644ff2009-05-08 11:15:53 -070047bool FrameworkListener::onDataAvailable(SocketClient *c) {
Josef Kindberg6d358ae2011-02-23 12:40:44 +010048 char buffer[CMD_BUF_SIZE];
San Mehat168415b2009-05-06 11:14:21 -070049 int len;
50
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010051 len = TEMP_FAILURE_RETRY(read(c->getSocket(), buffer, sizeof(buffer)));
52 if (len < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070053 SLOGE("read() failed (%s)", strerror(errno));
Kenny Rootf31d2ed2010-09-14 09:55:22 -070054 return false;
San Mehatc73a3a52009-06-15 14:06:03 -070055 } else if (!len)
San Mehat168415b2009-05-06 11:14:21 -070056 return false;
Josef Kindberg6d358ae2011-02-23 12:40:44 +010057 if(buffer[len-1] != '\0')
58 SLOGW("String is not zero-terminated");
San Mehat168415b2009-05-06 11:14:21 -070059
San Mehatd7680662009-05-12 11:16:59 -070060 int offset = 0;
San Mehat168415b2009-05-06 11:14:21 -070061 int i;
62
San Mehat168415b2009-05-06 11:14:21 -070063 for (i = 0; i < len; i++) {
San Mehatc73a3a52009-06-15 14:06:03 -070064 if (buffer[i] == '\0') {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010065 /* IMPORTANT: dispatchCommand() expects a zero-terminated string */
San Mehatd7680662009-05-12 11:16:59 -070066 dispatchCommand(c, buffer + offset);
67 offset = i + 1;
San Mehat168415b2009-05-06 11:14:21 -070068 }
69 }
Josef Kindberg6d358ae2011-02-23 12:40:44 +010070
San Mehat168415b2009-05-06 11:14:21 -070071 return true;
72}
73
74void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
75 mCommands->push_back(cmd);
76}
77
San Mehatc73a3a52009-06-15 14:06:03 -070078void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
San Mehatc4a895b2009-06-23 21:10:57 -070079 FrameworkCommandCollection::iterator i;
80 int argc = 0;
San Mehatc73a3a52009-06-15 14:06:03 -070081 char *argv[FrameworkListener::CMD_ARGS_MAX];
Josef Kindberg6d358ae2011-02-23 12:40:44 +010082 char tmp[CMD_BUF_SIZE];
San Mehatc4a895b2009-06-23 21:10:57 -070083 char *p = data;
84 char *q = tmp;
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010085 char *qlimit = tmp + sizeof(tmp) - 1;
San Mehatc4a895b2009-06-23 21:10:57 -070086 bool esc = false;
87 bool quote = false;
88 int k;
Robert Greenwalt8702bb12012-02-07 12:23:14 -080089 bool haveCmdNum = !mWithSeq;
San Mehatfa644ff2009-05-08 11:15:53 -070090
San Mehatc4a895b2009-06-23 21:10:57 -070091 memset(argv, 0, sizeof(argv));
92 memset(tmp, 0, sizeof(tmp));
93 while(*p) {
94 if (*p == '\\') {
95 if (esc) {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010096 if (q >= qlimit)
97 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -070098 *q++ = '\\';
99 esc = false;
100 } else
101 esc = true;
102 p++;
103 continue;
104 } else if (esc) {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100105 if (*p == '"') {
106 if (q >= qlimit)
107 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700108 *q++ = '"';
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100109 } else if (*p == '\\') {
110 if (q >= qlimit)
111 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700112 *q++ = '\\';
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100113 } else {
San Mehatc4a895b2009-06-23 21:10:57 -0700114 cli->sendMsg(500, "Unsupported escape sequence", false);
115 goto out;
116 }
117 p++;
118 esc = false;
119 continue;
120 }
San Mehatc73a3a52009-06-15 14:06:03 -0700121
San Mehatc4a895b2009-06-23 21:10:57 -0700122 if (*p == '"') {
123 if (quote)
124 quote = false;
125 else
126 quote = true;
127 p++;
128 continue;
129 }
130
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100131 if (q >= qlimit)
132 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700133 *q = *p++;
134 if (!quote && *q == ' ') {
135 *q = '\0';
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800136 if (!haveCmdNum) {
137 char *endptr;
138 int cmdNum = (int)strtol(tmp, &endptr, 0);
139 if (endptr == NULL || *endptr != '\0') {
140 cli->sendMsg(500, "Invalid sequence number", false);
141 goto out;
142 }
143 cli->setCmdNum(cmdNum);
144 haveCmdNum = true;
145 } else {
146 if (argc >= CMD_ARGS_MAX)
147 goto overflow;
148 argv[argc++] = strdup(tmp);
149 }
San Mehatc4a895b2009-06-23 21:10:57 -0700150 memset(tmp, 0, sizeof(tmp));
151 q = tmp;
152 continue;
153 }
154 q++;
San Mehatfa644ff2009-05-08 11:15:53 -0700155 }
156
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100157 *q = '\0';
158 if (argc >= CMD_ARGS_MAX)
159 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700160 argv[argc++] = strdup(tmp);
161#if 0
162 for (k = 0; k < argc; k++) {
San Mehat7e8529a2010-03-25 09:31:42 -0700163 SLOGD("arg[%d] = '%s'", k, argv[k]);
San Mehatc4a895b2009-06-23 21:10:57 -0700164 }
165#endif
San Mehat168415b2009-05-06 11:14:21 -0700166
San Mehatc4a895b2009-06-23 21:10:57 -0700167 if (quote) {
168 cli->sendMsg(500, "Unclosed quotes error", false);
169 goto out;
170 }
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100171
Robert Greenwalt8702bb12012-02-07 12:23:14 -0800172 if (errorRate && (++mCommandCount % errorRate == 0)) {
173 /* ignore this command - let the timeout handler handle it */
174 SLOGE("Faking a timeout");
175 goto out;
176 }
177
San Mehat168415b2009-05-06 11:14:21 -0700178 for (i = mCommands->begin(); i != mCommands->end(); ++i) {
179 FrameworkCommand *c = *i;
180
San Mehatc73a3a52009-06-15 14:06:03 -0700181 if (!strcmp(argv[0], c->getCommand())) {
182 if (c->runCommand(cli, argc, argv)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700183 SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
San Mehat168415b2009-05-06 11:14:21 -0700184 }
San Mehatc4a895b2009-06-23 21:10:57 -0700185 goto out;
San Mehat168415b2009-05-06 11:14:21 -0700186 }
187 }
San Mehatd7680662009-05-12 11:16:59 -0700188 cli->sendMsg(500, "Command not recognized", false);
San Mehatc4a895b2009-06-23 21:10:57 -0700189out:
190 int j;
191 for (j = 0; j < argc; j++)
192 free(argv[j]);
San Mehatfa644ff2009-05-08 11:15:53 -0700193 return;
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100194
195overflow:
Nick Kralevich6bbaaa62011-11-18 08:46:54 -0800196 LOG_EVENT_INT(78001, cli->getUid());
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100197 cli->sendMsg(500, "Command too long", false);
198 goto out;
San Mehat168415b2009-05-06 11:14:21 -0700199}