blob: c9918459e957c6403990f6af797ea9be140cd0ec [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{
Kenny Root3e319a92010-09-07 13:58:28 -070063 return free_cache((int64_t)atoll(arg[0])); /* free_size */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064}
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{
Kenny Root3e319a92010-09-07 13:58:28 -070078 int64_t codesize = 0;
79 int64_t datasize = 0;
80 int64_t cachesize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 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
Kenny Root3e319a92010-09-07 13:58:28 -070086 /*
87 * Each int64_t can take up 22 characters printed out. Make sure it
88 * doesn't go over REPLY_MAX in the future.
89 */
90 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64, codesize, datasize, cachesize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 return res;
92}
93
94static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
95{
Oscar Montemayora8529f62009-11-18 10:14:20 -080096 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097}
98
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080099static int do_movefiles(char **arg, char reply[REPLY_MAX])
100{
101 return movefiles();
102}
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104struct cmdinfo {
105 const char *name;
106 unsigned numargs;
107 int (*func)(char **arg, char reply[REPLY_MAX]);
108};
109
110struct cmdinfo cmds[] = {
111 { "ping", 0, do_ping },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800112 { "install", 4, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 { "dexopt", 3, do_dexopt },
114 { "movedex", 2, do_move_dex },
115 { "rmdex", 1, do_rm_dex },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800116 { "remove", 2, do_remove },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800117 { "rename", 3, do_rename },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 { "freecache", 1, do_free_cache },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800119 { "rmcache", 2, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 { "protect", 2, do_protect },
Oscar Montemayora8529f62009-11-18 10:14:20 -0800121 { "getsize", 4, do_get_size },
122 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800123 { "movefiles", 0, do_movefiles },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124};
125
126static int readx(int s, void *_buf, int count)
127{
128 char *buf = _buf;
129 int n = 0, r;
130 if (count < 0) return -1;
131 while (n < count) {
132 r = read(s, buf + n, count - n);
133 if (r < 0) {
134 if (errno == EINTR) continue;
135 LOGE("read error: %s\n", strerror(errno));
136 return -1;
137 }
138 if (r == 0) {
139 LOGE("eof\n");
140 return -1; /* EOF */
141 }
142 n += r;
143 }
144 return 0;
145}
146
147static int writex(int s, const void *_buf, int count)
148{
149 const char *buf = _buf;
150 int n = 0, r;
151 if (count < 0) return -1;
152 while (n < count) {
153 r = write(s, buf + n, count - n);
154 if (r < 0) {
155 if (errno == EINTR) continue;
156 LOGE("write error: %s\n", strerror(errno));
157 return -1;
158 }
159 n += r;
160 }
161 return 0;
162}
163
164
165/* Tokenize the command buffer, locate a matching command,
166 * ensure that the required number of arguments are provided,
167 * call the function(), return the result.
168 */
169static int execute(int s, char cmd[BUFFER_MAX])
170{
171 char reply[REPLY_MAX];
172 char *arg[TOKEN_MAX+1];
173 unsigned i;
174 unsigned n = 0;
175 unsigned short count;
176 int ret = -1;
177
178// LOGI("execute('%s')\n", cmd);
179
180 /* default reply is "" */
181 reply[0] = 0;
182
183 /* n is number of args (not counting arg[0]) */
184 arg[0] = cmd;
185 while (*cmd) {
186 if (isspace(*cmd)) {
187 *cmd++ = 0;
188 n++;
189 arg[n] = cmd;
190 if (n == TOKEN_MAX) {
191 LOGE("too many arguments\n");
192 goto done;
193 }
194 }
195 cmd++;
196 }
197
198 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
199 if (!strcmp(cmds[i].name,arg[0])) {
200 if (n != cmds[i].numargs) {
201 LOGE("%s requires %d arguments (%d given)\n",
202 cmds[i].name, cmds[i].numargs, n);
203 } else {
204 ret = cmds[i].func(arg + 1, reply);
205 }
206 goto done;
207 }
208 }
209 LOGE("unsupported command '%s'\n", arg[0]);
210
211done:
212 if (reply[0]) {
213 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
214 } else {
215 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
216 }
217 if (n > BUFFER_MAX) n = BUFFER_MAX;
218 count = n;
219
220// LOGI("reply: '%s'\n", cmd);
221 if (writex(s, &count, sizeof(count))) return -1;
222 if (writex(s, cmd, count)) return -1;
223 return 0;
224}
225
226int main(const int argc, const char *argv[]) {
227 char buf[BUFFER_MAX];
228 struct sockaddr addr;
229 socklen_t alen;
230 int lsocket, s, count;
231
232 lsocket = android_get_control_socket(SOCKET_PATH);
233 if (lsocket < 0) {
234 LOGE("Failed to get socket from environment: %s\n", strerror(errno));
235 exit(1);
236 }
237 if (listen(lsocket, 5)) {
238 LOGE("Listen on socket failed: %s\n", strerror(errno));
239 exit(1);
240 }
241 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
242
243 for (;;) {
244 alen = sizeof(addr);
245 s = accept(lsocket, &addr, &alen);
246 if (s < 0) {
247 LOGE("Accept failed: %s\n", strerror(errno));
248 continue;
249 }
250 fcntl(s, F_SETFD, FD_CLOEXEC);
251
252 LOGI("new connection\n");
253 for (;;) {
254 unsigned short count;
255 if (readx(s, &count, sizeof(count))) {
256 LOGE("failed to read size\n");
257 break;
258 }
259 if ((count < 1) || (count >= BUFFER_MAX)) {
260 LOGE("invalid size %d\n", count);
261 break;
262 }
263 if (readx(s, buf, count)) {
264 LOGE("failed to read command\n");
265 break;
266 }
267 buf[count] = 0;
268 if (execute(s, buf)) break;
269 }
270 LOGI("closing connection\n");
271 close(s);
272 }
273
274 return 0;
275}