blob: c2c749ace793a6a53279afab017728c2f81d8ceb [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024static int do_ping(char **arg, char reply[REPLY_MAX])
25{
26 return 0;
27}
28
29static int do_install(char **arg, char reply[REPLY_MAX])
30{
Kenny Root35ab3ad2011-02-02 16:42:14 -080031 return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032}
33
34static int do_dexopt(char **arg, char reply[REPLY_MAX])
35{
36 /* apk_path, uid, is_public */
37 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
38}
39
40static int do_move_dex(char **arg, char reply[REPLY_MAX])
41{
42 return move_dex(arg[0], arg[1]); /* src, dst */
43}
44
45static int do_rm_dex(char **arg, char reply[REPLY_MAX])
46{
47 return rm_dex(arg[0]); /* pkgname */
48}
49
50static int do_remove(char **arg, char reply[REPLY_MAX])
51{
Amith Yamasani0b285492011-04-14 17:35:23 -070052 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080055static int do_rename(char **arg, char reply[REPLY_MAX])
56{
Kenny Root35ab3ad2011-02-02 16:42:14 -080057 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080058}
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
61{
Kenny Root3e319a92010-09-07 13:58:28 -070062 return free_cache((int64_t)atoll(arg[0])); /* free_size */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063}
64
65static int do_rm_cache(char **arg, char reply[REPLY_MAX])
66{
Kenny Root35ab3ad2011-02-02 16:42:14 -080067 return delete_cache(arg[0]); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70static int do_protect(char **arg, char reply[REPLY_MAX])
71{
72 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
73}
74
75static int do_get_size(char **arg, char reply[REPLY_MAX])
76{
Kenny Root3e319a92010-09-07 13:58:28 -070077 int64_t codesize = 0;
78 int64_t datasize = 0;
79 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070080 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 int res = 0;
82
83 /* pkgdir, apkpath */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070084 res = get_size(arg[0], arg[1], arg[2], arg[3], &codesize, &datasize, &cachesize, &asecsize);
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 */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070090 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
91 codesize, datasize, cachesize, asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 return res;
93}
94
95static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
96{
Amith Yamasani0b285492011-04-14 17:35:23 -070097 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
98}
99
100static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
101{
102 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
103}
104
105static int do_rm_user(char **arg, char reply[REPLY_MAX])
106{
107 return delete_persona(atoi(arg[0])); /* userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
Amith Yamasani742a6712011-05-04 14:49:28 -0700110static int do_clone_user_data(char **arg, char reply[REPLY_MAX])
111{
112 return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2]));
113}
114
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800115static int do_movefiles(char **arg, char reply[REPLY_MAX])
116{
117 return movefiles();
118}
119
Kenny Root6a6b0072010-10-07 16:46:10 -0700120static int do_linklib(char **arg, char reply[REPLY_MAX])
121{
122 return linklib(arg[0], arg[1]);
123}
124
125static int do_unlinklib(char **arg, char reply[REPLY_MAX])
126{
127 return unlinklib(arg[0]);
128}
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130struct cmdinfo {
131 const char *name;
132 unsigned numargs;
133 int (*func)(char **arg, char reply[REPLY_MAX]);
134};
135
136struct cmdinfo cmds[] = {
137 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800138 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 { "dexopt", 3, do_dexopt },
140 { "movedex", 2, do_move_dex },
141 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700142 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800143 { "rename", 2, do_rename },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 { "freecache", 1, do_free_cache },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800145 { "rmcache", 1, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 { "protect", 2, do_protect },
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700147 { "getsize", 4, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700148 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800149 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700150 { "linklib", 2, do_linklib },
151 { "unlinklib", 1, do_unlinklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700152 { "mkuserdata", 3, do_mk_user_data },
153 { "rmuser", 1, do_rm_user },
Amith Yamasani742a6712011-05-04 14:49:28 -0700154 { "cloneuserdata", 3, do_clone_user_data },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155};
156
157static int readx(int s, void *_buf, int count)
158{
159 char *buf = _buf;
160 int n = 0, r;
161 if (count < 0) return -1;
162 while (n < count) {
163 r = read(s, buf + n, count - n);
164 if (r < 0) {
165 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000166 ALOGE("read error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 return -1;
168 }
169 if (r == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000170 ALOGE("eof\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 return -1; /* EOF */
172 }
173 n += r;
174 }
175 return 0;
176}
177
178static int writex(int s, const void *_buf, int count)
179{
180 const char *buf = _buf;
181 int n = 0, r;
182 if (count < 0) return -1;
183 while (n < count) {
184 r = write(s, buf + n, count - n);
185 if (r < 0) {
186 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000187 ALOGE("write error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 return -1;
189 }
190 n += r;
191 }
192 return 0;
193}
194
195
196/* Tokenize the command buffer, locate a matching command,
197 * ensure that the required number of arguments are provided,
198 * call the function(), return the result.
199 */
200static int execute(int s, char cmd[BUFFER_MAX])
201{
202 char reply[REPLY_MAX];
203 char *arg[TOKEN_MAX+1];
204 unsigned i;
205 unsigned n = 0;
206 unsigned short count;
207 int ret = -1;
208
Steve Block6215d3f2012-01-04 20:05:49 +0000209// ALOGI("execute('%s')\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 /* default reply is "" */
212 reply[0] = 0;
213
214 /* n is number of args (not counting arg[0]) */
215 arg[0] = cmd;
216 while (*cmd) {
217 if (isspace(*cmd)) {
218 *cmd++ = 0;
219 n++;
220 arg[n] = cmd;
221 if (n == TOKEN_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000222 ALOGE("too many arguments\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 goto done;
224 }
225 }
226 cmd++;
227 }
228
229 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
230 if (!strcmp(cmds[i].name,arg[0])) {
231 if (n != cmds[i].numargs) {
Steve Block3762c312012-01-06 19:20:56 +0000232 ALOGE("%s requires %d arguments (%d given)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 cmds[i].name, cmds[i].numargs, n);
234 } else {
235 ret = cmds[i].func(arg + 1, reply);
236 }
237 goto done;
238 }
239 }
Steve Block3762c312012-01-06 19:20:56 +0000240 ALOGE("unsupported command '%s'\n", arg[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
242done:
243 if (reply[0]) {
244 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
245 } else {
246 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
247 }
248 if (n > BUFFER_MAX) n = BUFFER_MAX;
249 count = n;
250
Steve Block6215d3f2012-01-04 20:05:49 +0000251// ALOGI("reply: '%s'\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 if (writex(s, &count, sizeof(count))) return -1;
253 if (writex(s, cmd, count)) return -1;
254 return 0;
255}
256
Kenny Root86c95842011-03-31 13:16:12 -0700257/**
258 * Initialize all the global variables that are used elsewhere. Returns 0 upon
259 * success and -1 on error.
260 */
261void free_globals() {
262 size_t i;
263
264 for (i = 0; i < android_system_dirs.count; i++) {
265 if (android_system_dirs.dirs[i].path != NULL) {
266 free(android_system_dirs.dirs[i].path);
267 }
268 }
269
270 free(android_system_dirs.dirs);
271}
272
273int initialize_globals() {
274 // Get the android data directory.
275 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
276 return -1;
277 }
278
279 // Get the android app directory.
280 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
281 return -1;
282 }
283
284 // Get the android protected app directory.
285 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
286 return -1;
287 }
288
289 // Get the sd-card ASEC mount point.
290 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
291 return -1;
292 }
293
294 // Take note of the system and vendor directories.
295 android_system_dirs.count = 2;
296
297 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
298 if (android_system_dirs.dirs == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000299 ALOGE("Couldn't allocate array for dirs; aborting\n");
Kenny Root86c95842011-03-31 13:16:12 -0700300 return -1;
301 }
302
303 // system
304 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
305 free_globals();
306 return -1;
307 }
308
Amith Yamasani0b285492011-04-14 17:35:23 -0700309 // append "app/" to dirs[0]
310 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
311 android_system_dirs.dirs[0].path = system_app_path;
312 android_system_dirs.dirs[0].len = strlen(system_app_path);
313
Kenny Root86c95842011-03-31 13:16:12 -0700314 // vendor
315 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700316 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700317 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
318
319 return 0;
320}
321
Amith Yamasani0b285492011-04-14 17:35:23 -0700322int initialize_directories() {
323 // /data/user
324 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
325 // /data/data
326 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
327 // /data/user/0
328 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX,
329 "0");
330 int ret = -1;
331 if (user_data_dir != NULL && primary_data_dir != NULL && legacy_data_dir != NULL) {
332 ret = 0;
333 // Make the /data/user directory if necessary
334 if (access(user_data_dir, R_OK) < 0) {
Nick Kralevich7ac3ed12012-03-23 17:52:33 -0700335 if (mkdir(user_data_dir, 0711) < 0) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700336 return -1;
337 }
338 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
339 return -1;
340 }
Nick Kralevich7ac3ed12012-03-23 17:52:33 -0700341 if (chmod(user_data_dir, 0711) < 0) {
342 return -1;
343 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700344 }
345 // Make the /data/user/0 symlink to /data/data if necessary
346 if (access(primary_data_dir, R_OK) < 0) {
347 ret = symlink(legacy_data_dir, primary_data_dir);
348 }
349 free(user_data_dir);
350 free(legacy_data_dir);
351 free(primary_data_dir);
352 }
353 return ret;
354}
355
Kenny Root86c95842011-03-31 13:16:12 -0700356int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 char buf[BUFFER_MAX];
358 struct sockaddr addr;
359 socklen_t alen;
360 int lsocket, s, count;
361
Kenny Root86c95842011-03-31 13:16:12 -0700362 if (initialize_globals() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000363 ALOGE("Could not initialize globals; exiting.\n");
Kenny Root86c95842011-03-31 13:16:12 -0700364 exit(1);
365 }
366
Amith Yamasani0b285492011-04-14 17:35:23 -0700367 if (initialize_directories() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000368 ALOGE("Could not create directories; exiting.\n");
Amith Yamasani0b285492011-04-14 17:35:23 -0700369 exit(1);
370 }
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 lsocket = android_get_control_socket(SOCKET_PATH);
373 if (lsocket < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000374 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 exit(1);
376 }
377 if (listen(lsocket, 5)) {
Steve Block3762c312012-01-06 19:20:56 +0000378 ALOGE("Listen on socket failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 exit(1);
380 }
381 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
382
383 for (;;) {
384 alen = sizeof(addr);
385 s = accept(lsocket, &addr, &alen);
386 if (s < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000387 ALOGE("Accept failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 continue;
389 }
390 fcntl(s, F_SETFD, FD_CLOEXEC);
391
Steve Block6215d3f2012-01-04 20:05:49 +0000392 ALOGI("new connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 for (;;) {
394 unsigned short count;
395 if (readx(s, &count, sizeof(count))) {
Steve Block3762c312012-01-06 19:20:56 +0000396 ALOGE("failed to read size\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 break;
398 }
399 if ((count < 1) || (count >= BUFFER_MAX)) {
Steve Block3762c312012-01-06 19:20:56 +0000400 ALOGE("invalid size %d\n", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 break;
402 }
403 if (readx(s, buf, count)) {
Steve Block3762c312012-01-06 19:20:56 +0000404 ALOGE("failed to read command\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 break;
406 }
407 buf[count] = 0;
408 if (execute(s, buf)) break;
409 }
Steve Block6215d3f2012-01-04 20:05:49 +0000410 ALOGI("closing connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 close(s);
412 }
413
414 return 0;
415}