blob: 882c493b59719fd4fc405dbb0a04952a56356733 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2** Copyright 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
17#include "installd.h"
18
19
20#define BUFFER_MAX 1024 /* input buffer for commands */
21#define TOKEN_MAX 8 /* max number of arguments in buffer */
22#define REPLY_MAX 256 /* largest reply allowed */
23
24
25static int do_ping(char **arg, char reply[REPLY_MAX])
26{
27 return 0;
28}
29
30static int do_install(char **arg, char reply[REPLY_MAX])
31{
Oscar Montemayora8529f62009-11-18 10:14:20 -080032 return install(arg[0], atoi(arg[1]), atoi(arg[2]), atoi(arg[3])); /* pkgname, uid, gid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033}
34
35static int do_dexopt(char **arg, char reply[REPLY_MAX])
36{
37 /* apk_path, uid, is_public */
38 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
39}
40
41static int do_move_dex(char **arg, char reply[REPLY_MAX])
42{
43 return move_dex(arg[0], arg[1]); /* src, dst */
44}
45
46static int do_rm_dex(char **arg, char reply[REPLY_MAX])
47{
48 return rm_dex(arg[0]); /* pkgname */
49}
50
51static int do_remove(char **arg, char reply[REPLY_MAX])
52{
Oscar Montemayora8529f62009-11-18 10:14:20 -080053 return uninstall(arg[0], atoi(arg[1])); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054}
55
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080056static int do_rename(char **arg, char reply[REPLY_MAX])
57{
58 return renamepkg(arg[0], arg[1], atoi(arg[2])); /* oldpkgname, newpkgname */
59}
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
62{
63 return free_cache(atoi(arg[0])); /* free_size */
64}
65
66static int do_rm_cache(char **arg, char reply[REPLY_MAX])
67{
Oscar Montemayora8529f62009-11-18 10:14:20 -080068 return delete_cache(arg[0], atoi(arg[1])); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
71static int do_protect(char **arg, char reply[REPLY_MAX])
72{
73 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
74}
75
76static int do_get_size(char **arg, char reply[REPLY_MAX])
77{
78 int codesize = 0;
79 int datasize = 0;
80 int cachesize = 0;
81 int res = 0;
82
83 /* pkgdir, apkpath */
Oscar Montemayora8529f62009-11-18 10:14:20 -080084 res = get_size(arg[0], arg[1], arg[2], &codesize, &datasize, &cachesize, atoi(arg[3]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86 sprintf(reply,"%d %d %d", codesize, datasize, cachesize);
87 return res;
88}
89
90static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
91{
Oscar Montemayora8529f62009-11-18 10:14:20 -080092 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093}
94
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080095static int do_movefiles(char **arg, char reply[REPLY_MAX])
96{
97 return movefiles();
98}
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100struct cmdinfo {
101 const char *name;
102 unsigned numargs;
103 int (*func)(char **arg, char reply[REPLY_MAX]);
104};
105
106struct cmdinfo cmds[] = {
107 { "ping", 0, do_ping },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800108 { "install", 4, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 { "dexopt", 3, do_dexopt },
110 { "movedex", 2, do_move_dex },
111 { "rmdex", 1, do_rm_dex },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800112 { "remove", 2, do_remove },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800113 { "rename", 3, do_rename },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 { "freecache", 1, do_free_cache },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800115 { "rmcache", 2, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 { "protect", 2, do_protect },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800117 { "getsize", 4, do_get_size },
118 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800119 { "movefiles", 0, do_movefiles },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120};
121
122static int readx(int s, void *_buf, int count)
123{
124 char *buf = _buf;
125 int n = 0, r;
126 if (count < 0) return -1;
127 while (n < count) {
128 r = read(s, buf + n, count - n);
129 if (r < 0) {
130 if (errno == EINTR) continue;
131 LOGE("read error: %s\n", strerror(errno));
132 return -1;
133 }
134 if (r == 0) {
135 LOGE("eof\n");
136 return -1; /* EOF */
137 }
138 n += r;
139 }
140 return 0;
141}
142
143static int writex(int s, const void *_buf, int count)
144{
145 const char *buf = _buf;
146 int n = 0, r;
147 if (count < 0) return -1;
148 while (n < count) {
149 r = write(s, buf + n, count - n);
150 if (r < 0) {
151 if (errno == EINTR) continue;
152 LOGE("write error: %s\n", strerror(errno));
153 return -1;
154 }
155 n += r;
156 }
157 return 0;
158}
159
160
161/* Tokenize the command buffer, locate a matching command,
162 * ensure that the required number of arguments are provided,
163 * call the function(), return the result.
164 */
165static int execute(int s, char cmd[BUFFER_MAX])
166{
167 char reply[REPLY_MAX];
168 char *arg[TOKEN_MAX+1];
169 unsigned i;
170 unsigned n = 0;
171 unsigned short count;
172 int ret = -1;
173
174// LOGI("execute('%s')\n", cmd);
175
176 /* default reply is "" */
177 reply[0] = 0;
178
179 /* n is number of args (not counting arg[0]) */
180 arg[0] = cmd;
181 while (*cmd) {
182 if (isspace(*cmd)) {
183 *cmd++ = 0;
184 n++;
185 arg[n] = cmd;
186 if (n == TOKEN_MAX) {
187 LOGE("too many arguments\n");
188 goto done;
189 }
190 }
191 cmd++;
192 }
193
194 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
195 if (!strcmp(cmds[i].name,arg[0])) {
196 if (n != cmds[i].numargs) {
197 LOGE("%s requires %d arguments (%d given)\n",
198 cmds[i].name, cmds[i].numargs, n);
199 } else {
200 ret = cmds[i].func(arg + 1, reply);
201 }
202 goto done;
203 }
204 }
205 LOGE("unsupported command '%s'\n", arg[0]);
206
207done:
208 if (reply[0]) {
209 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
210 } else {
211 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
212 }
213 if (n > BUFFER_MAX) n = BUFFER_MAX;
214 count = n;
215
216// LOGI("reply: '%s'\n", cmd);
217 if (writex(s, &count, sizeof(count))) return -1;
218 if (writex(s, cmd, count)) return -1;
219 return 0;
220}
221
222int main(const int argc, const char *argv[]) {
223 char buf[BUFFER_MAX];
224 struct sockaddr addr;
225 socklen_t alen;
226 int lsocket, s, count;
227
228 lsocket = android_get_control_socket(SOCKET_PATH);
229 if (lsocket < 0) {
230 LOGE("Failed to get socket from environment: %s\n", strerror(errno));
231 exit(1);
232 }
233 if (listen(lsocket, 5)) {
234 LOGE("Listen on socket failed: %s\n", strerror(errno));
235 exit(1);
236 }
237 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
238
239 for (;;) {
240 alen = sizeof(addr);
241 s = accept(lsocket, &addr, &alen);
242 if (s < 0) {
243 LOGE("Accept failed: %s\n", strerror(errno));
244 continue;
245 }
246 fcntl(s, F_SETFD, FD_CLOEXEC);
247
248 LOGI("new connection\n");
249 for (;;) {
250 unsigned short count;
251 if (readx(s, &count, sizeof(count))) {
252 LOGE("failed to read size\n");
253 break;
254 }
255 if ((count < 1) || (count >= BUFFER_MAX)) {
256 LOGE("invalid size %d\n", count);
257 break;
258 }
259 if (readx(s, buf, count)) {
260 LOGE("failed to read command\n");
261 break;
262 }
263 buf[count] = 0;
264 if (execute(s, buf)) break;
265 }
266 LOGI("closing connection\n");
267 close(s);
268 }
269
270 return 0;
271}