blob: 064ee32e585c616411946f79b4dfab88ce238ffc [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
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
Nick Kralevichd7471292013-02-28 16:59:13 -080017#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070018#include <linux/prctl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040019#include <selinux/android.h>
20#include <selinux/avc.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070021
22#include "installd.h"
23
24
25#define BUFFER_MAX 1024 /* input buffer for commands */
26#define TOKEN_MAX 8 /* max number of arguments in buffer */
27#define REPLY_MAX 256 /* largest reply allowed */
28
29static int do_ping(char **arg, char reply[REPLY_MAX])
30{
31 return 0;
32}
33
34static int do_install(char **arg, char reply[REPLY_MAX])
35{
Robert Craig4d3fd4e2013-03-25 06:33:03 -040036 return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -070037}
38
39static int do_dexopt(char **arg, char reply[REPLY_MAX])
40{
Narayan Kamath1b400322014-04-11 13:17:00 +010041 /* apk_path, uid, is_public, pkgname, instruction_set */
42 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4]);
Mike Lockwood94afecf2012-10-24 10:45:23 -070043}
44
45static int do_move_dex(char **arg, char reply[REPLY_MAX])
46{
Narayan Kamath1b400322014-04-11 13:17:00 +010047 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070048}
49
50static int do_rm_dex(char **arg, char reply[REPLY_MAX])
51{
Narayan Kamath1b400322014-04-11 13:17:00 +010052 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
55static int do_remove(char **arg, char reply[REPLY_MAX])
56{
57 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
58}
59
60static int do_rename(char **arg, char reply[REPLY_MAX])
61{
62 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
63}
64
65static int do_fixuid(char **arg, char reply[REPLY_MAX])
66{
67 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
68}
69
70static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
71{
72 return free_cache((int64_t)atoll(arg[0])); /* free_size */
73}
74
75static int do_rm_cache(char **arg, char reply[REPLY_MAX])
76{
77 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
78}
79
80static int do_get_size(char **arg, char reply[REPLY_MAX])
81{
82 int64_t codesize = 0;
83 int64_t datasize = 0;
84 int64_t cachesize = 0;
85 int64_t asecsize = 0;
86 int res = 0;
87
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -070088 /* pkgdir, userid, apkpath */
Dianne Hackborn8b417802013-05-01 18:55:10 -070089 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
Narayan Kamath1b400322014-04-11 13:17:00 +010090 arg[6], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -070091
92 /*
93 * Each int64_t can take up 22 characters printed out. Make sure it
94 * doesn't go over REPLY_MAX in the future.
95 */
96 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
97 codesize, datasize, cachesize, asecsize);
98 return res;
99}
100
101static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
102{
103 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
104}
105
106static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
107{
Robert Craig880d1a92013-07-29 09:18:09 -0400108 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
109 /* pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110}
111
Robin Lee095c7632014-04-25 15:05:19 +0100112static int do_mk_user(char **arg, char reply[REPLY_MAX])
113{
114 return create_user(atoi(arg[0])); /* userid */
115}
116
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117static int do_rm_user(char **arg, char reply[REPLY_MAX])
118{
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700119 return delete_user(atoi(arg[0])); /* userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700120}
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122static int do_movefiles(char **arg, char reply[REPLY_MAX])
123{
124 return movefiles();
125}
126
127static int do_linklib(char **arg, char reply[REPLY_MAX])
128{
129 return linklib(arg[0], arg[1], atoi(arg[2]));
130}
131
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100132static int do_idmap(char **arg, char reply[REPLY_MAX])
133{
134 return idmap(arg[0], arg[1], atoi(arg[2]));
135}
136
Robert Craigda30dc72014-03-27 10:21:12 -0400137static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500138{
Robert Craigda30dc72014-03-27 10:21:12 -0400139 return restorecon_data(arg[0], arg[1], atoi(arg[2]));
140 /* pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500141}
142
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143struct cmdinfo {
144 const char *name;
145 unsigned numargs;
146 int (*func)(char **arg, char reply[REPLY_MAX]);
147};
148
149struct cmdinfo cmds[] = {
150 { "ping", 0, do_ping },
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400151 { "install", 4, do_install },
Narayan Kamath1b400322014-04-11 13:17:00 +0100152 { "dexopt", 5, do_dexopt },
153 { "movedex", 3, do_move_dex },
154 { "rmdex", 2, do_rm_dex },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155 { "remove", 2, do_remove },
156 { "rename", 2, do_rename },
157 { "fixuid", 3, do_fixuid },
158 { "freecache", 1, do_free_cache },
159 { "rmcache", 2, do_rm_cache },
Narayan Kamath1b400322014-04-11 13:17:00 +0100160 { "getsize", 7, do_get_size },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161 { "rmuserdata", 2, do_rm_user_data },
162 { "movefiles", 0, do_movefiles },
163 { "linklib", 3, do_linklib },
Robert Craig880d1a92013-07-29 09:18:09 -0400164 { "mkuserdata", 4, do_mk_user_data },
Robin Lee095c7632014-04-25 15:05:19 +0100165 { "mkuser", 1, do_mk_user },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700166 { "rmuser", 1, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100167 { "idmap", 3, do_idmap },
Robert Craigda30dc72014-03-27 10:21:12 -0400168 { "restorecondata", 3, do_restorecon_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169};
170
171static int readx(int s, void *_buf, int count)
172{
173 char *buf = _buf;
174 int n = 0, r;
175 if (count < 0) return -1;
176 while (n < count) {
177 r = read(s, buf + n, count - n);
178 if (r < 0) {
179 if (errno == EINTR) continue;
180 ALOGE("read error: %s\n", strerror(errno));
181 return -1;
182 }
183 if (r == 0) {
184 ALOGE("eof\n");
185 return -1; /* EOF */
186 }
187 n += r;
188 }
189 return 0;
190}
191
192static int writex(int s, const void *_buf, int count)
193{
194 const char *buf = _buf;
195 int n = 0, r;
196 if (count < 0) return -1;
197 while (n < count) {
198 r = write(s, buf + n, count - n);
199 if (r < 0) {
200 if (errno == EINTR) continue;
201 ALOGE("write error: %s\n", strerror(errno));
202 return -1;
203 }
204 n += r;
205 }
206 return 0;
207}
208
209
210/* Tokenize the command buffer, locate a matching command,
211 * ensure that the required number of arguments are provided,
212 * call the function(), return the result.
213 */
214static int execute(int s, char cmd[BUFFER_MAX])
215{
216 char reply[REPLY_MAX];
217 char *arg[TOKEN_MAX+1];
218 unsigned i;
219 unsigned n = 0;
220 unsigned short count;
221 int ret = -1;
222
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700223 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224
225 /* default reply is "" */
226 reply[0] = 0;
227
228 /* n is number of args (not counting arg[0]) */
229 arg[0] = cmd;
230 while (*cmd) {
231 if (isspace(*cmd)) {
232 *cmd++ = 0;
233 n++;
234 arg[n] = cmd;
235 if (n == TOKEN_MAX) {
236 ALOGE("too many arguments\n");
237 goto done;
238 }
239 }
240 cmd++;
241 }
242
243 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
244 if (!strcmp(cmds[i].name,arg[0])) {
245 if (n != cmds[i].numargs) {
246 ALOGE("%s requires %d arguments (%d given)\n",
247 cmds[i].name, cmds[i].numargs, n);
248 } else {
249 ret = cmds[i].func(arg + 1, reply);
250 }
251 goto done;
252 }
253 }
254 ALOGE("unsupported command '%s'\n", arg[0]);
255
256done:
257 if (reply[0]) {
258 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
259 } else {
260 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
261 }
262 if (n > BUFFER_MAX) n = BUFFER_MAX;
263 count = n;
264
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700265 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700266 if (writex(s, &count, sizeof(count))) return -1;
267 if (writex(s, cmd, count)) return -1;
268 return 0;
269}
270
271/**
272 * Initialize all the global variables that are used elsewhere. Returns 0 upon
273 * success and -1 on error.
274 */
275void free_globals() {
276 size_t i;
277
278 for (i = 0; i < android_system_dirs.count; i++) {
279 if (android_system_dirs.dirs[i].path != NULL) {
280 free(android_system_dirs.dirs[i].path);
281 }
282 }
283
284 free(android_system_dirs.dirs);
285}
286
287int initialize_globals() {
288 // Get the android data directory.
289 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
290 return -1;
291 }
292
293 // Get the android app directory.
294 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
295 return -1;
296 }
297
298 // Get the android protected app directory.
299 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
300 return -1;
301 }
302
303 // Get the android app native library directory.
304 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
305 return -1;
306 }
307
308 // Get the sd-card ASEC mount point.
309 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
310 return -1;
311 }
312
313 // Get the android media directory.
314 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
315 return -1;
316 }
317
318 // Take note of the system and vendor directories.
319 android_system_dirs.count = 2;
320
321 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
322 if (android_system_dirs.dirs == NULL) {
323 ALOGE("Couldn't allocate array for dirs; aborting\n");
324 return -1;
325 }
326
327 // system
328 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
329 free_globals();
330 return -1;
331 }
332
333 // append "app/" to dirs[0]
334 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
335 android_system_dirs.dirs[0].path = system_app_path;
336 android_system_dirs.dirs[0].len = strlen(system_app_path);
337
338 // vendor
339 // TODO replace this with an environment variable (doesn't exist yet)
340 android_system_dirs.dirs[1].path = "/vendor/app/";
341 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
342
343 return 0;
344}
345
346int initialize_directories() {
347 int res = -1;
348
349 // Read current filesystem layout version to handle upgrade paths
350 char version_path[PATH_MAX];
351 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
352
353 int oldVersion;
354 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
355 oldVersion = 0;
356 }
357 int version = oldVersion;
358
359 // /data/user
360 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
361 // /data/data
362 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
363 // /data/user/0
364 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
365 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
366 goto fail;
367 }
368
369 // Make the /data/user directory if necessary
370 if (access(user_data_dir, R_OK) < 0) {
371 if (mkdir(user_data_dir, 0711) < 0) {
372 goto fail;
373 }
374 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
375 goto fail;
376 }
377 if (chmod(user_data_dir, 0711) < 0) {
378 goto fail;
379 }
380 }
381 // Make the /data/user/0 symlink to /data/data if necessary
382 if (access(primary_data_dir, R_OK) < 0) {
383 if (symlink(legacy_data_dir, primary_data_dir)) {
384 goto fail;
385 }
386 }
387
388 if (version == 0) {
389 // Introducing multi-user, so migrate /data/media contents into /data/media/0
390 ALOGD("Upgrading /data/media for multi-user");
391
392 // Ensure /data/media
393 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
394 goto fail;
395 }
396
397 // /data/media.tmp
398 char media_tmp_dir[PATH_MAX];
399 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
400
401 // Only copy when upgrade not already in progress
402 if (access(media_tmp_dir, F_OK) == -1) {
403 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
404 ALOGE("Failed to move legacy media path: %s", strerror(errno));
405 goto fail;
406 }
407 }
408
409 // Create /data/media again
410 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
411 goto fail;
412 }
413
Stephen Smalley26288202014-02-07 09:16:46 -0500414 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500415 goto fail;
416 }
417
Mike Lockwood94afecf2012-10-24 10:45:23 -0700418 // /data/media/0
419 char owner_media_dir[PATH_MAX];
420 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
421
422 // Move any owner data into place
423 if (access(media_tmp_dir, F_OK) == 0) {
424 if (rename(media_tmp_dir, owner_media_dir) == -1) {
425 ALOGE("Failed to move owner media path: %s", strerror(errno));
426 goto fail;
427 }
428 }
429
430 // Ensure media directories for any existing users
431 DIR *dir;
432 struct dirent *dirent;
433 char user_media_dir[PATH_MAX];
434
435 dir = opendir(user_data_dir);
436 if (dir != NULL) {
437 while ((dirent = readdir(dir))) {
438 if (dirent->d_type == DT_DIR) {
439 const char *name = dirent->d_name;
440
441 // skip "." and ".."
442 if (name[0] == '.') {
443 if (name[1] == 0) continue;
444 if ((name[1] == '.') && (name[2] == 0)) continue;
445 }
446
447 // /data/media/<user_id>
448 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
449 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
450 goto fail;
451 }
452 }
453 }
454 closedir(dir);
455 }
456
457 version = 1;
458 }
459
460 // /data/media/obb
461 char media_obb_dir[PATH_MAX];
462 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
463
464 if (version == 1) {
465 // Introducing /data/media/obb for sharing OBB across users; migrate
466 // any existing OBB files from owner.
467 ALOGD("Upgrading to shared /data/media/obb");
468
469 // /data/media/0/Android/obb
470 char owner_obb_path[PATH_MAX];
471 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
472
473 // Only move if target doesn't already exist
474 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
475 if (rename(owner_obb_path, media_obb_dir) == -1) {
476 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
477 goto fail;
478 }
479 }
480
481 version = 2;
482 }
483
484 if (ensure_media_user_dirs(0) == -1) {
485 ALOGE("Failed to setup media for user 0");
486 goto fail;
487 }
488 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
489 goto fail;
490 }
491
Robin Lee095c7632014-04-25 15:05:19 +0100492 if (version == 2) {
493 ALOGD("Upgrading to /data/misc/user directories");
494
495 DIR *dir;
496 struct dirent *dirent;
497 char user_data_dir[PATH_MAX];
498
499 dir = opendir(user_data_dir);
500 if (dir != NULL) {
501 while ((dirent = readdir(dir))) {
502 if (dirent->d_type == DT_DIR) {
503 const char *name = dirent->d_name;
504
505 // skip "." and ".."
506 if (name[0] == '.') {
507 if (name[1] == 0) continue;
508 if ((name[1] == '.') && (name[2] == 0)) continue;
509 }
510
511 // /data/misc/user/<user_id>
512 if (ensure_config_user_dirs(atoi(name)) == -1) {
513 goto fail;
514 }
515 }
516 }
517 closedir(dir);
518 }
519
520 version = 3;
521 }
522
523 if (ensure_config_user_dirs(0) == -1) {
524 ALOGE("Failed to setup misc for user 0");
525 goto fail;
526 }
527
Mike Lockwood94afecf2012-10-24 10:45:23 -0700528 // Persist layout version if changed
529 if (version != oldVersion) {
530 if (fs_write_atomic_int(version_path, version) == -1) {
531 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
532 goto fail;
533 }
534 }
535
536 // Success!
537 res = 0;
538
539fail:
540 free(user_data_dir);
541 free(legacy_data_dir);
542 free(primary_data_dir);
543 return res;
544}
545
546static void drop_privileges() {
547 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
548 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
549 exit(1);
550 }
551
552 if (setgid(AID_INSTALL) < 0) {
553 ALOGE("setgid() can't drop privileges; exiting.\n");
554 exit(1);
555 }
556
557 if (setuid(AID_INSTALL) < 0) {
558 ALOGE("setuid() can't drop privileges; exiting.\n");
559 exit(1);
560 }
561
562 struct __user_cap_header_struct capheader;
563 struct __user_cap_data_struct capdata[2];
564 memset(&capheader, 0, sizeof(capheader));
565 memset(&capdata, 0, sizeof(capdata));
566 capheader.version = _LINUX_CAPABILITY_VERSION_3;
567 capheader.pid = 0;
568
569 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
570 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
571 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
572 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100573 capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700574
575 capdata[0].effective = capdata[0].permitted;
576 capdata[1].effective = capdata[1].permitted;
577 capdata[0].inheritable = 0;
578 capdata[1].inheritable = 0;
579
580 if (capset(&capheader, &capdata[0]) < 0) {
581 ALOGE("capset failed: %s\n", strerror(errno));
582 exit(1);
583 }
584}
585
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400586static int log_callback(int type, const char *fmt, ...) {
587 va_list ap;
588 int priority;
589
590 switch (type) {
591 case SELINUX_WARNING:
592 priority = ANDROID_LOG_WARN;
593 break;
594 case SELINUX_INFO:
595 priority = ANDROID_LOG_INFO;
596 break;
597 default:
598 priority = ANDROID_LOG_ERROR;
599 break;
600 }
601 va_start(ap, fmt);
602 LOG_PRI_VA(priority, "SELinux", fmt, ap);
603 va_end(ap);
604 return 0;
605}
606
Mike Lockwood94afecf2012-10-24 10:45:23 -0700607int main(const int argc, const char *argv[]) {
608 char buf[BUFFER_MAX];
609 struct sockaddr addr;
610 socklen_t alen;
611 int lsocket, s, count;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400612 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700613
614 ALOGI("installd firing up\n");
615
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400616 union selinux_callback cb;
617 cb.func_log = log_callback;
618 selinux_set_callback(SELINUX_CB_LOG, cb);
619
Mike Lockwood94afecf2012-10-24 10:45:23 -0700620 if (initialize_globals() < 0) {
621 ALOGE("Could not initialize globals; exiting.\n");
622 exit(1);
623 }
624
625 if (initialize_directories() < 0) {
626 ALOGE("Could not create directories; exiting.\n");
627 exit(1);
628 }
629
Stephen Smalleybd558d62013-04-16 12:16:50 -0400630 if (selinux_enabled && selinux_status_open(true) < 0) {
631 ALOGE("Could not open selinux status; exiting.\n");
632 exit(1);
633 }
634
Mike Lockwood94afecf2012-10-24 10:45:23 -0700635 drop_privileges();
636
637 lsocket = android_get_control_socket(SOCKET_PATH);
638 if (lsocket < 0) {
639 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
640 exit(1);
641 }
642 if (listen(lsocket, 5)) {
643 ALOGE("Listen on socket failed: %s\n", strerror(errno));
644 exit(1);
645 }
646 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
647
648 for (;;) {
649 alen = sizeof(addr);
650 s = accept(lsocket, &addr, &alen);
651 if (s < 0) {
652 ALOGE("Accept failed: %s\n", strerror(errno));
653 continue;
654 }
655 fcntl(s, F_SETFD, FD_CLOEXEC);
656
657 ALOGI("new connection\n");
658 for (;;) {
659 unsigned short count;
660 if (readx(s, &count, sizeof(count))) {
661 ALOGE("failed to read size\n");
662 break;
663 }
664 if ((count < 1) || (count >= BUFFER_MAX)) {
665 ALOGE("invalid size %d\n", count);
666 break;
667 }
668 if (readx(s, buf, count)) {
669 ALOGE("failed to read command\n");
670 break;
671 }
672 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400673 if (selinux_enabled && selinux_status_updated() > 0) {
674 selinux_android_seapp_context_reload();
675 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700676 if (execute(s, buf)) break;
677 }
678 ALOGI("closing connection\n");
679 close(s);
680 }
681
682 return 0;
683}