blob: ef92a690a3b138776bcde9fa6c103ccb20a67386 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Jeff Sharkey19803802015-04-07 12:44:51 -07004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Jeff Sharkey19803802015-04-07 12:44:51 -07008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Jeff Sharkey19803802015-04-07 12:44:51 -070010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include <fcntl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040018#include <selinux/android.h>
19#include <selinux/avc.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080020#include <sys/capability.h>
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070021#include <sys/fsuid.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <sys/prctl.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25
26#include <android-base/logging.h>
27#include <cutils/fs.h>
28#include <cutils/log.h> // TODO: Move everything to base::logging.
29#include <cutils/properties.h>
30#include <cutils/sockets.h>
31#include <private/android_filesystem_config.h>
32
33#include <commands.h>
34#include <globals.h>
35#include <installd_constants.h>
36#include <installd_deps.h> // Need to fill in requirements of commands.
37#include <utils.h>
38
39#ifndef LOG_TAG
40#define LOG_TAG "installd"
41#endif
42#define SOCKET_PATH "installd"
Mike Lockwood94afecf2012-10-24 10:45:23 -070043
Mike Lockwood94afecf2012-10-24 10:45:23 -070044#define BUFFER_MAX 1024 /* input buffer for commands */
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080045#define TOKEN_MAX 16 /* max number of arguments in buffer */
Mike Lockwood94afecf2012-10-24 10:45:23 -070046#define REPLY_MAX 256 /* largest reply allowed */
47
Andreas Gampe02d0de52015-11-11 20:43:16 -080048namespace android {
49namespace installd {
50
51// Check that installd-deps sizes match cutils sizes.
52static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
53static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
54
55////////////////////////
56// Plug-in functions. //
57////////////////////////
58
59int get_property(const char *key, char *value, const char *default_value) {
60 return property_get(key, value, default_value);
61}
62
63// Compute the output path of
64bool calculate_oat_file_path(char path[PKG_PATH_MAX],
65 const char *oat_dir,
66 const char *apk_path,
67 const char *instruction_set) {
Dan Austin63235022016-03-28 15:09:02 -070068 const char *file_name_start;
69 const char *file_name_end;
Andreas Gampe02d0de52015-11-11 20:43:16 -080070
71 file_name_start = strrchr(apk_path, '/');
72 if (file_name_start == NULL) {
73 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
74 return false;
75 }
76 file_name_end = strrchr(apk_path, '.');
77 if (file_name_end < file_name_start) {
78 ALOGE("apk_path '%s' has no extension\n", apk_path);
79 return false;
80 }
81
82 // Calculate file_name
83 int file_name_len = file_name_end - file_name_start - 1;
84 char file_name[file_name_len + 1];
85 memcpy(file_name, file_name_start + 1, file_name_len);
86 file_name[file_name_len] = '\0';
87
88 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
89 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
90 return true;
91}
92
93/*
94 * Computes the odex file for the given apk_path and instruction_set.
95 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
96 *
97 * Returns false if it failed to determine the odex file path.
98 */
99bool calculate_odex_file_path(char path[PKG_PATH_MAX],
100 const char *apk_path,
101 const char *instruction_set) {
102 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
103 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
104 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
105 return false;
106 }
107
108 strcpy(path, apk_path);
109 char *end = strrchr(path, '/');
110 if (end == NULL) {
111 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
112 return false;
113 }
114 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
115
116 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
117 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
118 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
119 end = strrchr(path, '.');
120 if (end == NULL) {
121 ALOGE("apk_path '%s' has no extension.\n", apk_path);
122 return false;
123 }
124 strcpy(end + 1, "odex");
125 return true;
126}
127
128bool create_cache_path(char path[PKG_PATH_MAX],
129 const char *src,
130 const char *instruction_set) {
Greg Kaiser00087b72016-03-14 13:29:10 -0700131 /* demand that we are an absolute path */
132 if ((src == nullptr) || (src[0] != '/') || strstr(src,"..")) {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800133 return false;
134 }
135
Greg Kaiser00087b72016-03-14 13:29:10 -0700136 size_t srclen = strlen(src);
137
Andreas Gampe02d0de52015-11-11 20:43:16 -0800138 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
139 return false;
140 }
141
142 size_t dstlen =
143 android_data_dir.len +
144 strlen(DALVIK_CACHE) +
145 1 +
146 strlen(instruction_set) +
147 srclen +
148 strlen(DALVIK_CACHE_POSTFIX) + 2;
149
150 if (dstlen > PKG_PATH_MAX) {
151 return false;
152 }
153
David Brazdil249c1792016-09-06 15:35:28 +0100154 sprintf(path,"%s%s/%s/%s",
Andreas Gampe02d0de52015-11-11 20:43:16 -0800155 android_data_dir.path,
156 DALVIK_CACHE,
157 instruction_set,
David Brazdil249c1792016-09-06 15:35:28 +0100158 src + 1 /* skip the leading / */);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800159
160 char* tmp =
161 path +
162 android_data_dir.len +
163 strlen(DALVIK_CACHE) +
164 1 +
165 strlen(instruction_set) + 1;
166
167 for(; *tmp; tmp++) {
168 if (*tmp == '/') {
169 *tmp = '@';
170 }
171 }
172
David Brazdil249c1792016-09-06 15:35:28 +0100173 strcat(path, DALVIK_CACHE_POSTFIX);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800174 return true;
175}
176
177
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700178static char* parse_null(char* arg) {
179 if (strcmp(arg, "!") == 0) {
180 return nullptr;
181 } else {
182 return arg;
183 }
184}
185
Andreas Gampe02d0de52015-11-11 20:43:16 -0800186static int do_ping(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187{
188 return 0;
189}
190
Andreas Gampe01ad5982016-03-09 16:27:29 -0800191// We use otapreopt_chroot to get into the chroot.
192static constexpr const char* kOtaPreopt = "/system/bin/otapreopt_chroot";
Andreas Gampe73dae112015-11-19 14:12:14 -0800193
Andreas Gampe548bdb92016-06-02 17:56:45 -0700194static int do_ota_dexopt(const char* args[DEXOPT_PARAM_COUNT],
195 char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
Andreas Gampe01ad5982016-03-09 16:27:29 -0800196 // Time to fork and run otapreopt.
197
198 // Check that the tool exists.
199 struct stat s;
200 if (stat(kOtaPreopt, &s) != 0) {
201 LOG(ERROR) << "Otapreopt chroot tool not found.";
202 return -1;
Andreas Gampe73dae112015-11-19 14:12:14 -0800203 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800204
205 pid_t pid = fork();
206 if (pid == 0) {
Andreas Gampe548bdb92016-06-02 17:56:45 -0700207 const char* argv[1 + DEXOPT_PARAM_COUNT + 1];
Andreas Gampe01ad5982016-03-09 16:27:29 -0800208 argv[0] = kOtaPreopt;
Andreas Gampe548bdb92016-06-02 17:56:45 -0700209
210 for (size_t i = 0; i < DEXOPT_PARAM_COUNT; ++i) {
211 argv[i + 1] = args[i];
Andreas Gampe01ad5982016-03-09 16:27:29 -0800212 }
Andreas Gampe548bdb92016-06-02 17:56:45 -0700213
214 argv[DEXOPT_PARAM_COUNT + 1] = nullptr;
Andreas Gampe01ad5982016-03-09 16:27:29 -0800215
216 execv(argv[0], (char * const *)argv);
217 PLOG(ERROR) << "execv(OTAPREOPT_CHROOT) failed";
218 exit(99);
219 } else {
220 int res = wait_child(pid);
221 if (res == 0) {
222 ALOGV("DexInv: --- END OTAPREOPT (success) ---\n");
223 } else {
224 ALOGE("DexInv: --- END OTAPREOPT --- status=0x%04x, process failed\n", res);
225 }
226 return res;
227 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800228}
229
Andreas Gampe548bdb92016-06-02 17:56:45 -0700230static int do_regular_dexopt(const char* args[DEXOPT_PARAM_COUNT],
231 char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
232 return dexopt(args);
233}
234
235using DexoptFn = int (*)(const char* args[DEXOPT_PARAM_COUNT],
236 char reply[REPLY_MAX]);
237
Andreas Gampe73dae112015-11-19 14:12:14 -0800238static int do_dexopt(char **arg, char reply[REPLY_MAX])
Mike Lockwood94afecf2012-10-24 10:45:23 -0700239{
Andreas Gampe548bdb92016-06-02 17:56:45 -0700240 const char* args[DEXOPT_PARAM_COUNT];
241 for (size_t i = 0; i < DEXOPT_PARAM_COUNT; ++i) {
242 CHECK(arg[i] != nullptr);
243 args[i] = arg[i];
Andreas Gampe73dae112015-11-19 14:12:14 -0800244 }
Andreas Gampe548bdb92016-06-02 17:56:45 -0700245
246 int dexopt_flags = atoi(arg[6]);
247 DexoptFn dexopt_fn;
248 if ((dexopt_flags & DEXOPT_OTA) != 0) {
249 dexopt_fn = do_ota_dexopt;
250 } else {
251 dexopt_fn = do_regular_dexopt;
252 }
253 return dexopt_fn(args, reply);
Andreas Gampe4d0f8252016-03-20 11:30:28 -0700254}
255
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700256static int do_get_app_size(char **arg, char reply[REPLY_MAX]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700257 int64_t codesize = 0;
258 int64_t datasize = 0;
259 int64_t cachesize = 0;
260 int64_t asecsize = 0;
261 int res = 0;
262
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600263 /* const char *uuid, const char *pkgname, int userid, int flags, ino_t ce_data_inode,
264 const char* code_path */
265 res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atol(arg[4]),
266 arg[5], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267
268 /*
269 * Each int64_t can take up 22 characters printed out. Make sure it
270 * doesn't go over REPLY_MAX in the future.
271 */
272 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
273 codesize, datasize, cachesize, asecsize);
274 return res;
275}
276
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277struct cmdinfo {
278 const char *name;
279 unsigned numargs;
280 int (*func)(char **arg, char reply[REPLY_MAX]);
281};
282
283struct cmdinfo cmds[] = {
284 { "ping", 0, do_ping },
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600285 { "get_app_size", 6, do_get_app_size },
Jeff Haob63d91f2016-03-16 15:59:25 -0700286 { "dexopt", 10, do_dexopt },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700287};
288
289static int readx(int s, void *_buf, int count)
290{
Jeff Sharkey19803802015-04-07 12:44:51 -0700291 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700292 int n = 0, r;
293 if (count < 0) return -1;
294 while (n < count) {
295 r = read(s, buf + n, count - n);
296 if (r < 0) {
297 if (errno == EINTR) continue;
298 ALOGE("read error: %s\n", strerror(errno));
299 return -1;
300 }
301 if (r == 0) {
302 ALOGE("eof\n");
303 return -1; /* EOF */
304 }
305 n += r;
306 }
307 return 0;
308}
309
310static int writex(int s, const void *_buf, int count)
311{
Jeff Sharkey19803802015-04-07 12:44:51 -0700312 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700313 int n = 0, r;
314 if (count < 0) return -1;
315 while (n < count) {
316 r = write(s, buf + n, count - n);
317 if (r < 0) {
318 if (errno == EINTR) continue;
319 ALOGE("write error: %s\n", strerror(errno));
320 return -1;
321 }
322 n += r;
323 }
324 return 0;
325}
326
327
328/* Tokenize the command buffer, locate a matching command,
329 * ensure that the required number of arguments are provided,
330 * call the function(), return the result.
331 */
332static int execute(int s, char cmd[BUFFER_MAX])
333{
334 char reply[REPLY_MAX];
335 char *arg[TOKEN_MAX+1];
336 unsigned i;
337 unsigned n = 0;
338 unsigned short count;
339 int ret = -1;
340
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700341 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700342
343 /* default reply is "" */
344 reply[0] = 0;
345
346 /* n is number of args (not counting arg[0]) */
347 arg[0] = cmd;
348 while (*cmd) {
349 if (isspace(*cmd)) {
350 *cmd++ = 0;
351 n++;
352 arg[n] = cmd;
353 if (n == TOKEN_MAX) {
354 ALOGE("too many arguments\n");
355 goto done;
356 }
357 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600358 if (*cmd) {
359 cmd++;
360 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700361 }
362
363 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
364 if (!strcmp(cmds[i].name,arg[0])) {
365 if (n != cmds[i].numargs) {
366 ALOGE("%s requires %d arguments (%d given)\n",
367 cmds[i].name, cmds[i].numargs, n);
368 } else {
369 ret = cmds[i].func(arg + 1, reply);
370 }
371 goto done;
372 }
373 }
374 ALOGE("unsupported command '%s'\n", arg[0]);
375
376done:
377 if (reply[0]) {
378 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
379 } else {
380 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
381 }
382 if (n > BUFFER_MAX) n = BUFFER_MAX;
383 count = n;
384
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700385 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386 if (writex(s, &count, sizeof(count))) return -1;
387 if (writex(s, cmd, count)) return -1;
388 return 0;
389}
390
Andreas Gamped089ca12016-06-27 14:25:30 -0700391static bool initialize_globals() {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800392 const char* data_path = getenv("ANDROID_DATA");
393 if (data_path == nullptr) {
394 ALOGE("Could not find ANDROID_DATA");
395 return false;
396 }
397 const char* root_path = getenv("ANDROID_ROOT");
398 if (root_path == nullptr) {
399 ALOGE("Could not find ANDROID_ROOT");
400 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700401 }
402
Andreas Gampe02d0de52015-11-11 20:43:16 -0800403 return init_globals_from_data_and_root(data_path, root_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700404}
405
Andreas Gampe02d0de52015-11-11 20:43:16 -0800406static int initialize_directories() {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700407 int res = -1;
408
409 // Read current filesystem layout version to handle upgrade paths
410 char version_path[PATH_MAX];
411 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
412
413 int oldVersion;
414 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
415 oldVersion = 0;
416 }
417 int version = oldVersion;
418
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700419 if (version < 2) {
420 SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700421 version = 2;
422 }
423
Robin Lee07053fc2014-04-29 19:42:01 +0100424 if (ensure_config_user_dirs(0) == -1) {
425 ALOGE("Failed to setup misc for user 0");
426 goto fail;
427 }
428
Robin Lee095c7632014-04-25 15:05:19 +0100429 if (version == 2) {
430 ALOGD("Upgrading to /data/misc/user directories");
431
Robin Lee60fd3fe2014-10-07 16:55:02 +0100432 char misc_dir[PATH_MAX];
433 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
434
435 char keychain_added_dir[PATH_MAX];
436 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
437
438 char keychain_removed_dir[PATH_MAX];
439 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
440
Robin Lee095c7632014-04-25 15:05:19 +0100441 DIR *dir;
442 struct dirent *dirent;
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700443 dir = opendir("/data/user");
Robin Lee095c7632014-04-25 15:05:19 +0100444 if (dir != NULL) {
445 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100446 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100447
Robin Lee60fd3fe2014-10-07 16:55:02 +0100448 // skip "." and ".."
449 if (name[0] == '.') {
450 if (name[1] == 0) continue;
451 if ((name[1] == '.') && (name[2] == 0)) continue;
452 }
453
454 uint32_t user_id = atoi(name);
455
456 // /data/misc/user/<user_id>
457 if (ensure_config_user_dirs(user_id) == -1) {
458 goto fail;
459 }
460
461 char misc_added_dir[PATH_MAX];
462 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
463
464 char misc_removed_dir[PATH_MAX];
465 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
466
467 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
468 gid_t gid = uid;
469 if (access(keychain_added_dir, F_OK) == 0) {
470 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
471 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100472 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100473 }
474 if (access(keychain_removed_dir, F_OK) == 0) {
475 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
476 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100477 }
478 }
479 }
480 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100481
Robin Lee60fd3fe2014-10-07 16:55:02 +0100482 if (access(keychain_added_dir, F_OK) == 0) {
483 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100484 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100485 if (access(keychain_removed_dir, F_OK) == 0) {
486 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100487 }
488 }
489
490 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100491 }
492
Mike Lockwood94afecf2012-10-24 10:45:23 -0700493 // Persist layout version if changed
494 if (version != oldVersion) {
495 if (fs_write_atomic_int(version_path, version) == -1) {
496 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
497 goto fail;
498 }
499 }
500
501 // Success!
502 res = 0;
503
504fail:
Mike Lockwood94afecf2012-10-24 10:45:23 -0700505 return res;
506}
507
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400508static int log_callback(int type, const char *fmt, ...) {
509 va_list ap;
510 int priority;
511
512 switch (type) {
513 case SELINUX_WARNING:
514 priority = ANDROID_LOG_WARN;
515 break;
516 case SELINUX_INFO:
517 priority = ANDROID_LOG_INFO;
518 break;
519 default:
520 priority = ANDROID_LOG_ERROR;
521 break;
522 }
523 va_start(ap, fmt);
524 LOG_PRI_VA(priority, "SELinux", fmt, ap);
525 va_end(ap);
526 return 0;
527}
528
Andreas Gampe02d0de52015-11-11 20:43:16 -0800529static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700530 char buf[BUFFER_MAX];
531 struct sockaddr addr;
532 socklen_t alen;
Jeff Sharkey90874002016-12-05 11:18:55 -0700533 int lsocket, s, ret;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400534 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700535
Jeff Sharkeye3637242015-04-08 20:56:42 -0700536 setenv("ANDROID_LOG_TAGS", "*:v", 1);
537 android::base::InitLogging(argv);
538
Mike Lockwood94afecf2012-10-24 10:45:23 -0700539 ALOGI("installd firing up\n");
540
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400541 union selinux_callback cb;
542 cb.func_log = log_callback;
543 selinux_set_callback(SELINUX_CB_LOG, cb);
544
Andreas Gampe02d0de52015-11-11 20:43:16 -0800545 if (!initialize_globals()) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700546 ALOGE("Could not initialize globals; exiting.\n");
547 exit(1);
548 }
549
550 if (initialize_directories() < 0) {
551 ALOGE("Could not create directories; exiting.\n");
552 exit(1);
553 }
554
Stephen Smalleybd558d62013-04-16 12:16:50 -0400555 if (selinux_enabled && selinux_status_open(true) < 0) {
556 ALOGE("Could not open selinux status; exiting.\n");
557 exit(1);
558 }
559
Jeff Sharkey90874002016-12-05 11:18:55 -0700560 if ((ret = InstalldNativeService::start()) != android::OK) {
561 ALOGE("Unable to start InstalldNativeService: %d", ret);
562 exit(1);
563 }
564
Mike Lockwood94afecf2012-10-24 10:45:23 -0700565 lsocket = android_get_control_socket(SOCKET_PATH);
566 if (lsocket < 0) {
567 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
568 exit(1);
569 }
570 if (listen(lsocket, 5)) {
571 ALOGE("Listen on socket failed: %s\n", strerror(errno));
572 exit(1);
573 }
574 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
575
576 for (;;) {
577 alen = sizeof(addr);
578 s = accept(lsocket, &addr, &alen);
579 if (s < 0) {
580 ALOGE("Accept failed: %s\n", strerror(errno));
581 continue;
582 }
583 fcntl(s, F_SETFD, FD_CLOEXEC);
584
585 ALOGI("new connection\n");
586 for (;;) {
587 unsigned short count;
588 if (readx(s, &count, sizeof(count))) {
589 ALOGE("failed to read size\n");
590 break;
591 }
592 if ((count < 1) || (count >= BUFFER_MAX)) {
593 ALOGE("invalid size %d\n", count);
594 break;
595 }
596 if (readx(s, buf, count)) {
597 ALOGE("failed to read command\n");
598 break;
599 }
600 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400601 if (selinux_enabled && selinux_status_updated() > 0) {
602 selinux_android_seapp_context_reload();
603 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700604 if (execute(s, buf)) break;
605 }
606 ALOGI("closing connection\n");
607 close(s);
608 }
609
610 return 0;
611}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800612
613} // namespace installd
614} // namespace android
615
616int main(const int argc, char *argv[]) {
617 return android::installd::installd_main(argc, argv);
618}