blob: 3416ceb124c2f2d97be25bf4ec7ab91b6eab92ff [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
28FrameworkListener::FrameworkListener(const char *socketName) :
29 SocketListener(socketName, true) {
30 mCommands = new FrameworkCommandCollection();
31}
32
San Mehatfa644ff2009-05-08 11:15:53 -070033bool FrameworkListener::onDataAvailable(SocketClient *c) {
San Mehatd7680662009-05-12 11:16:59 -070034 char buffer[255];
San Mehat168415b2009-05-06 11:14:21 -070035 int len;
36
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010037 len = TEMP_FAILURE_RETRY(read(c->getSocket(), buffer, sizeof(buffer)));
38 if (len < 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070039 SLOGE("read() failed (%s)", strerror(errno));
Kenny Rootf31d2ed2010-09-14 09:55:22 -070040 return false;
San Mehatc73a3a52009-06-15 14:06:03 -070041 } else if (!len)
San Mehat168415b2009-05-06 11:14:21 -070042 return false;
San Mehat168415b2009-05-06 11:14:21 -070043
San Mehatd7680662009-05-12 11:16:59 -070044 int offset = 0;
San Mehat168415b2009-05-06 11:14:21 -070045 int i;
46
San Mehat168415b2009-05-06 11:14:21 -070047 for (i = 0; i < len; i++) {
San Mehatc73a3a52009-06-15 14:06:03 -070048 if (buffer[i] == '\0') {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010049 /* IMPORTANT: dispatchCommand() expects a zero-terminated string */
San Mehatd7680662009-05-12 11:16:59 -070050 dispatchCommand(c, buffer + offset);
51 offset = i + 1;
San Mehat168415b2009-05-06 11:14:21 -070052 }
53 }
54 return true;
55}
56
57void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
58 mCommands->push_back(cmd);
59}
60
San Mehatc73a3a52009-06-15 14:06:03 -070061void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
San Mehatc4a895b2009-06-23 21:10:57 -070062 FrameworkCommandCollection::iterator i;
63 int argc = 0;
San Mehatc73a3a52009-06-15 14:06:03 -070064 char *argv[FrameworkListener::CMD_ARGS_MAX];
San Mehatc4a895b2009-06-23 21:10:57 -070065 char tmp[255];
66 char *p = data;
67 char *q = tmp;
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010068 char *qlimit = tmp + sizeof(tmp) - 1;
San Mehatc4a895b2009-06-23 21:10:57 -070069 bool esc = false;
70 bool quote = false;
71 int k;
San Mehatfa644ff2009-05-08 11:15:53 -070072
San Mehatc4a895b2009-06-23 21:10:57 -070073 memset(argv, 0, sizeof(argv));
74 memset(tmp, 0, sizeof(tmp));
75 while(*p) {
76 if (*p == '\\') {
77 if (esc) {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010078 if (q >= qlimit)
79 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -070080 *q++ = '\\';
81 esc = false;
82 } else
83 esc = true;
84 p++;
85 continue;
86 } else if (esc) {
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010087 if (*p == '"') {
88 if (q >= qlimit)
89 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -070090 *q++ = '"';
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010091 } else if (*p == '\\') {
92 if (q >= qlimit)
93 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -070094 *q++ = '\\';
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +010095 } else {
San Mehatc4a895b2009-06-23 21:10:57 -070096 cli->sendMsg(500, "Unsupported escape sequence", false);
97 goto out;
98 }
99 p++;
100 esc = false;
101 continue;
102 }
San Mehatc73a3a52009-06-15 14:06:03 -0700103
San Mehatc4a895b2009-06-23 21:10:57 -0700104 if (*p == '"') {
105 if (quote)
106 quote = false;
107 else
108 quote = true;
109 p++;
110 continue;
111 }
112
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100113 if (q >= qlimit)
114 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700115 *q = *p++;
116 if (!quote && *q == ' ') {
117 *q = '\0';
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100118 if (argc >= CMD_ARGS_MAX)
119 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700120 argv[argc++] = strdup(tmp);
121 memset(tmp, 0, sizeof(tmp));
122 q = tmp;
123 continue;
124 }
125 q++;
San Mehatfa644ff2009-05-08 11:15:53 -0700126 }
127
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100128 *q = '\0';
129 if (argc >= CMD_ARGS_MAX)
130 goto overflow;
San Mehatc4a895b2009-06-23 21:10:57 -0700131 argv[argc++] = strdup(tmp);
132#if 0
133 for (k = 0; k < argc; k++) {
San Mehat7e8529a2010-03-25 09:31:42 -0700134 SLOGD("arg[%d] = '%s'", k, argv[k]);
San Mehatc4a895b2009-06-23 21:10:57 -0700135 }
136#endif
San Mehat168415b2009-05-06 11:14:21 -0700137
San Mehatc4a895b2009-06-23 21:10:57 -0700138 if (quote) {
139 cli->sendMsg(500, "Unclosed quotes error", false);
140 goto out;
141 }
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100142
San Mehat168415b2009-05-06 11:14:21 -0700143 for (i = mCommands->begin(); i != mCommands->end(); ++i) {
144 FrameworkCommand *c = *i;
145
San Mehatc73a3a52009-06-15 14:06:03 -0700146 if (!strcmp(argv[0], c->getCommand())) {
147 if (c->runCommand(cli, argc, argv)) {
San Mehat7e8529a2010-03-25 09:31:42 -0700148 SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
San Mehat168415b2009-05-06 11:14:21 -0700149 }
San Mehatc4a895b2009-06-23 21:10:57 -0700150 goto out;
San Mehat168415b2009-05-06 11:14:21 -0700151 }
152 }
153
San Mehatd7680662009-05-12 11:16:59 -0700154 cli->sendMsg(500, "Command not recognized", false);
San Mehatc4a895b2009-06-23 21:10:57 -0700155out:
156 int j;
157 for (j = 0; j < argc; j++)
158 free(argv[j]);
San Mehatfa644ff2009-05-08 11:15:53 -0700159 return;
David 'Digit' Turnerc6b0def2011-01-17 03:29:04 +0100160
161overflow:
162 cli->sendMsg(500, "Command too long", false);
163 goto out;
San Mehat168415b2009-05-06 11:14:21 -0700164}