installd.cpp revision d107c305f74592e8078754b317d840a2dde661db
1/*
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 <sys/capability.h>
18#include <sys/prctl.h>
19#include <selinux/android.h>
20#include <selinux/avc.h>
21
22#include "installd.h"
23
24
25#define BUFFER_MAX    1024  /* input buffer for commands */
26#define TOKEN_MAX     16    /* max number of arguments in buffer */
27#define REPLY_MAX     256   /* largest reply allowed */
28
29static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused)
30{
31    return 0;
32}
33
34static int do_install(char **arg, char reply[REPLY_MAX] __unused)
35{
36    return install(nullptr, arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */
37}
38
39static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused)
40{
41    /* apk_path, uid, is_public, pkgname, instruction_set,
42     * dexopt_needed, vm_safe_mode, debuggable, oat_dir */
43    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]),
44                  atoi(arg[6]), atoi(arg[7]), arg[8]);
45}
46
47static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
48{
49    return mark_boot_complete(arg[0] /* instruction set */);
50}
51
52static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
53{
54    return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
55}
56
57static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
58{
59    return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
60}
61
62static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
63{
64    return uninstall(nullptr, arg[0], atoi(arg[1])); /* pkgname, userid */
65}
66
67static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
68{
69    return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
70}
71
72static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
73{
74    return fix_uid(nullptr, arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
75}
76
77static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
78{
79    return free_cache((int64_t)atoll(arg[0])); /* free_size */
80}
81
82static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
83{
84    return delete_cache(nullptr, arg[0], atoi(arg[1])); /* pkgname, userid */
85}
86
87static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
88{
89    return delete_code_cache(nullptr, arg[0], atoi(arg[1])); /* pkgname, userid */
90}
91
92static int do_get_size(char **arg, char reply[REPLY_MAX])
93{
94    int64_t codesize = 0;
95    int64_t datasize = 0;
96    int64_t cachesize = 0;
97    int64_t asecsize = 0;
98    int res = 0;
99
100        /* pkgdir, userid, apkpath */
101    res = get_size(nullptr, arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
102            arg[6], &codesize, &datasize, &cachesize, &asecsize);
103
104    /*
105     * Each int64_t can take up 22 characters printed out. Make sure it
106     * doesn't go over REPLY_MAX in the future.
107     */
108    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
109            codesize, datasize, cachesize, asecsize);
110    return res;
111}
112
113static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
114{
115    return delete_user_data(nullptr, arg[0], atoi(arg[1])); /* pkgname, userid */
116}
117
118static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
119{
120    return make_user_data(nullptr, arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
121                             /* pkgname, uid, userid, seinfo */
122}
123
124static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
125{
126    return make_user_config(atoi(arg[0])); /* userid */
127}
128
129static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
130{
131    return delete_user(atoi(arg[0])); /* userid */
132}
133
134static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
135{
136    return movefiles();
137}
138
139static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
140{
141    return linklib(nullptr, arg[0], arg[1], atoi(arg[2]));
142}
143
144static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
145{
146    return idmap(arg[0], arg[1], atoi(arg[2]));
147}
148
149static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
150{
151    return restorecon_data(nullptr, arg[0], arg[1], atoi(arg[2]));
152                             /* pkgName, seinfo, uid*/
153}
154
155static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
156{
157    /* oat_dir, instruction_set */
158    return create_oat_dir(arg[0], arg[1]);
159}
160
161static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
162{
163    /* oat_dir */
164    return rm_package_dir(arg[0]);
165}
166
167struct cmdinfo {
168    const char *name;
169    unsigned numargs;
170    int (*func)(char **arg, char reply[REPLY_MAX]);
171};
172
173struct cmdinfo cmds[] = {
174    { "ping",                 0, do_ping },
175    { "install",              4, do_install },
176    { "dexopt",               9, do_dexopt },
177    { "markbootcomplete",     1, do_mark_boot_complete },
178    { "movedex",              3, do_move_dex },
179    { "rmdex",                2, do_rm_dex },
180    { "remove",               2, do_remove },
181    { "rename",               2, do_rename },
182    { "fixuid",               3, do_fixuid },
183    { "freecache",            1, do_free_cache },
184    { "rmcache",              2, do_rm_cache },
185    { "rmcodecache",          2, do_rm_code_cache },
186    { "getsize",              7, do_get_size },
187    { "rmuserdata",           2, do_rm_user_data },
188    { "movefiles",            0, do_movefiles },
189    { "linklib",              3, do_linklib },
190    { "mkuserdata",           4, do_mk_user_data },
191    { "mkuserconfig",         1, do_mk_user_config },
192    { "rmuser",               1, do_rm_user },
193    { "idmap",                3, do_idmap },
194    { "restorecondata",       3, do_restorecon_data },
195    { "createoatdir",         2, do_create_oat_dir },
196    { "rmpackagedir",         1, do_rm_package_dir},
197};
198
199static int readx(int s, void *_buf, int count)
200{
201    char *buf = (char *) _buf;
202    int n = 0, r;
203    if (count < 0) return -1;
204    while (n < count) {
205        r = read(s, buf + n, count - n);
206        if (r < 0) {
207            if (errno == EINTR) continue;
208            ALOGE("read error: %s\n", strerror(errno));
209            return -1;
210        }
211        if (r == 0) {
212            ALOGE("eof\n");
213            return -1; /* EOF */
214        }
215        n += r;
216    }
217    return 0;
218}
219
220static int writex(int s, const void *_buf, int count)
221{
222    const char *buf = (const char *) _buf;
223    int n = 0, r;
224    if (count < 0) return -1;
225    while (n < count) {
226        r = write(s, buf + n, count - n);
227        if (r < 0) {
228            if (errno == EINTR) continue;
229            ALOGE("write error: %s\n", strerror(errno));
230            return -1;
231        }
232        n += r;
233    }
234    return 0;
235}
236
237
238/* Tokenize the command buffer, locate a matching command,
239 * ensure that the required number of arguments are provided,
240 * call the function(), return the result.
241 */
242static int execute(int s, char cmd[BUFFER_MAX])
243{
244    char reply[REPLY_MAX];
245    char *arg[TOKEN_MAX+1];
246    unsigned i;
247    unsigned n = 0;
248    unsigned short count;
249    int ret = -1;
250
251    // ALOGI("execute('%s')\n", cmd);
252
253        /* default reply is "" */
254    reply[0] = 0;
255
256        /* n is number of args (not counting arg[0]) */
257    arg[0] = cmd;
258    while (*cmd) {
259        if (isspace(*cmd)) {
260            *cmd++ = 0;
261            n++;
262            arg[n] = cmd;
263            if (n == TOKEN_MAX) {
264                ALOGE("too many arguments\n");
265                goto done;
266            }
267        }
268        if (*cmd) {
269          cmd++;
270        }
271    }
272
273    for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
274        if (!strcmp(cmds[i].name,arg[0])) {
275            if (n != cmds[i].numargs) {
276                ALOGE("%s requires %d arguments (%d given)\n",
277                     cmds[i].name, cmds[i].numargs, n);
278            } else {
279                ret = cmds[i].func(arg + 1, reply);
280            }
281            goto done;
282        }
283    }
284    ALOGE("unsupported command '%s'\n", arg[0]);
285
286done:
287    if (reply[0]) {
288        n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
289    } else {
290        n = snprintf(cmd, BUFFER_MAX, "%d", ret);
291    }
292    if (n > BUFFER_MAX) n = BUFFER_MAX;
293    count = n;
294
295    // ALOGI("reply: '%s'\n", cmd);
296    if (writex(s, &count, sizeof(count))) return -1;
297    if (writex(s, cmd, count)) return -1;
298    return 0;
299}
300
301/**
302 * Initialize all the global variables that are used elsewhere. Returns 0 upon
303 * success and -1 on error.
304 */
305void free_globals() {
306    size_t i;
307
308    for (i = 0; i < android_system_dirs.count; i++) {
309        if (android_system_dirs.dirs[i].path != NULL) {
310            free(android_system_dirs.dirs[i].path);
311        }
312    }
313
314    free(android_system_dirs.dirs);
315}
316
317int initialize_globals() {
318    // Get the android data directory.
319    if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
320        return -1;
321    }
322
323    // Get the android app directory.
324    if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
325        return -1;
326    }
327
328    // Get the android protected app directory.
329    if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
330        return -1;
331    }
332
333    // Get the android app native library directory.
334    if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
335        return -1;
336    }
337
338    // Get the sd-card ASEC mount point.
339    if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
340        return -1;
341    }
342
343    // Get the android media directory.
344    if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
345        return -1;
346    }
347
348    // Get the android external app directory.
349    if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
350        return -1;
351    }
352
353    // Take note of the system and vendor directories.
354    android_system_dirs.count = 4;
355
356    android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
357    if (android_system_dirs.dirs == NULL) {
358        ALOGE("Couldn't allocate array for dirs; aborting\n");
359        return -1;
360    }
361
362    dir_rec_t android_root_dir;
363    if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
364        ALOGE("Missing ANDROID_ROOT; aborting\n");
365        return -1;
366    }
367
368    android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
369    android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
370
371    android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
372    android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
373
374    android_system_dirs.dirs[2].path = strdup("/vendor/app/");
375    android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
376
377    android_system_dirs.dirs[3].path = strdup("/oem/app/");
378    android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
379
380    return 0;
381}
382
383int initialize_directories() {
384    int res = -1;
385
386    // Read current filesystem layout version to handle upgrade paths
387    char version_path[PATH_MAX];
388    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
389
390    int oldVersion;
391    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
392        oldVersion = 0;
393    }
394    int version = oldVersion;
395
396    // /data/user
397    char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
398    // /data/data
399    char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
400    // /data/user/0
401    char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
402    if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
403        goto fail;
404    }
405
406    // Make the /data/user directory if necessary
407    if (access(user_data_dir, R_OK) < 0) {
408        if (mkdir(user_data_dir, 0711) < 0) {
409            goto fail;
410        }
411        if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
412            goto fail;
413        }
414        if (chmod(user_data_dir, 0711) < 0) {
415            goto fail;
416        }
417    }
418    // Make the /data/user/0 symlink to /data/data if necessary
419    if (access(primary_data_dir, R_OK) < 0) {
420        if (symlink(legacy_data_dir, primary_data_dir)) {
421            goto fail;
422        }
423    }
424
425    if (version == 0) {
426        // Introducing multi-user, so migrate /data/media contents into /data/media/0
427        ALOGD("Upgrading /data/media for multi-user");
428
429        // Ensure /data/media
430        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
431            goto fail;
432        }
433
434        // /data/media.tmp
435        char media_tmp_dir[PATH_MAX];
436        snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
437
438        // Only copy when upgrade not already in progress
439        if (access(media_tmp_dir, F_OK) == -1) {
440            if (rename(android_media_dir.path, media_tmp_dir) == -1) {
441                ALOGE("Failed to move legacy media path: %s", strerror(errno));
442                goto fail;
443            }
444        }
445
446        // Create /data/media again
447        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
448            goto fail;
449        }
450
451        if (selinux_android_restorecon(android_media_dir.path, 0)) {
452            goto fail;
453        }
454
455        // /data/media/0
456        char owner_media_dir[PATH_MAX];
457        snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
458
459        // Move any owner data into place
460        if (access(media_tmp_dir, F_OK) == 0) {
461            if (rename(media_tmp_dir, owner_media_dir) == -1) {
462                ALOGE("Failed to move owner media path: %s", strerror(errno));
463                goto fail;
464            }
465        }
466
467        // Ensure media directories for any existing users
468        DIR *dir;
469        struct dirent *dirent;
470        char user_media_dir[PATH_MAX];
471
472        dir = opendir(user_data_dir);
473        if (dir != NULL) {
474            while ((dirent = readdir(dir))) {
475                if (dirent->d_type == DT_DIR) {
476                    const char *name = dirent->d_name;
477
478                    // skip "." and ".."
479                    if (name[0] == '.') {
480                        if (name[1] == 0) continue;
481                        if ((name[1] == '.') && (name[2] == 0)) continue;
482                    }
483
484                    // /data/media/<user_id>
485                    snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
486                    if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
487                        goto fail;
488                    }
489                }
490            }
491            closedir(dir);
492        }
493
494        version = 1;
495    }
496
497    // /data/media/obb
498    char media_obb_dir[PATH_MAX];
499    snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
500
501    if (version == 1) {
502        // Introducing /data/media/obb for sharing OBB across users; migrate
503        // any existing OBB files from owner.
504        ALOGD("Upgrading to shared /data/media/obb");
505
506        // /data/media/0/Android/obb
507        char owner_obb_path[PATH_MAX];
508        snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
509
510        // Only move if target doesn't already exist
511        if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
512            if (rename(owner_obb_path, media_obb_dir) == -1) {
513                ALOGE("Failed to move OBB from owner: %s", strerror(errno));
514                goto fail;
515            }
516        }
517
518        version = 2;
519    }
520
521    if (ensure_media_user_dirs(0) == -1) {
522        ALOGE("Failed to setup media for user 0");
523        goto fail;
524    }
525    if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
526        goto fail;
527    }
528
529    if (ensure_config_user_dirs(0) == -1) {
530        ALOGE("Failed to setup misc for user 0");
531        goto fail;
532    }
533
534    if (version == 2) {
535        ALOGD("Upgrading to /data/misc/user directories");
536
537        char misc_dir[PATH_MAX];
538        snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
539
540        char keychain_added_dir[PATH_MAX];
541        snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
542
543        char keychain_removed_dir[PATH_MAX];
544        snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
545
546        DIR *dir;
547        struct dirent *dirent;
548        dir = opendir(user_data_dir);
549        if (dir != NULL) {
550            while ((dirent = readdir(dir))) {
551                const char *name = dirent->d_name;
552
553                // skip "." and ".."
554                if (name[0] == '.') {
555                    if (name[1] == 0) continue;
556                    if ((name[1] == '.') && (name[2] == 0)) continue;
557                }
558
559                uint32_t user_id = atoi(name);
560
561                // /data/misc/user/<user_id>
562                if (ensure_config_user_dirs(user_id) == -1) {
563                    goto fail;
564                }
565
566                char misc_added_dir[PATH_MAX];
567                snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
568
569                char misc_removed_dir[PATH_MAX];
570                snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
571
572                uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
573                gid_t gid = uid;
574                if (access(keychain_added_dir, F_OK) == 0) {
575                    if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
576                        ALOGE("Some files failed to copy");
577                    }
578                }
579                if (access(keychain_removed_dir, F_OK) == 0) {
580                    if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
581                        ALOGE("Some files failed to copy");
582                    }
583                }
584            }
585            closedir(dir);
586
587            if (access(keychain_added_dir, F_OK) == 0) {
588                delete_dir_contents(keychain_added_dir, 1, 0);
589            }
590            if (access(keychain_removed_dir, F_OK) == 0) {
591                delete_dir_contents(keychain_removed_dir, 1, 0);
592            }
593        }
594
595        version = 3;
596    }
597
598    // Persist layout version if changed
599    if (version != oldVersion) {
600        if (fs_write_atomic_int(version_path, version) == -1) {
601            ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
602            goto fail;
603        }
604    }
605
606    // Success!
607    res = 0;
608
609fail:
610    free(user_data_dir);
611    free(legacy_data_dir);
612    free(primary_data_dir);
613    return res;
614}
615
616static void drop_privileges() {
617    if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
618        ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
619        exit(1);
620    }
621
622    if (setgid(AID_INSTALL) < 0) {
623        ALOGE("setgid() can't drop privileges; exiting.\n");
624        exit(1);
625    }
626
627    if (setuid(AID_INSTALL) < 0) {
628        ALOGE("setuid() can't drop privileges; exiting.\n");
629        exit(1);
630    }
631
632    struct __user_cap_header_struct capheader;
633    struct __user_cap_data_struct capdata[2];
634    memset(&capheader, 0, sizeof(capheader));
635    memset(&capdata, 0, sizeof(capdata));
636    capheader.version = _LINUX_CAPABILITY_VERSION_3;
637    capheader.pid = 0;
638
639    capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
640    capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted        |= CAP_TO_MASK(CAP_CHOWN);
641    capdata[CAP_TO_INDEX(CAP_SETUID)].permitted       |= CAP_TO_MASK(CAP_SETUID);
642    capdata[CAP_TO_INDEX(CAP_SETGID)].permitted       |= CAP_TO_MASK(CAP_SETGID);
643    capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted       |= CAP_TO_MASK(CAP_FOWNER);
644
645    capdata[0].effective = capdata[0].permitted;
646    capdata[1].effective = capdata[1].permitted;
647    capdata[0].inheritable = 0;
648    capdata[1].inheritable = 0;
649
650    if (capset(&capheader, &capdata[0]) < 0) {
651        ALOGE("capset failed: %s\n", strerror(errno));
652        exit(1);
653    }
654}
655
656static int log_callback(int type, const char *fmt, ...) {
657    va_list ap;
658    int priority;
659
660    switch (type) {
661    case SELINUX_WARNING:
662        priority = ANDROID_LOG_WARN;
663        break;
664    case SELINUX_INFO:
665        priority = ANDROID_LOG_INFO;
666        break;
667    default:
668        priority = ANDROID_LOG_ERROR;
669        break;
670    }
671    va_start(ap, fmt);
672    LOG_PRI_VA(priority, "SELinux", fmt, ap);
673    va_end(ap);
674    return 0;
675}
676
677int main(const int argc __unused, const char *argv[] __unused) {
678    char buf[BUFFER_MAX];
679    struct sockaddr addr;
680    socklen_t alen;
681    int lsocket, s;
682    int selinux_enabled = (is_selinux_enabled() > 0);
683
684    ALOGI("installd firing up\n");
685
686    union selinux_callback cb;
687    cb.func_log = log_callback;
688    selinux_set_callback(SELINUX_CB_LOG, cb);
689
690    if (initialize_globals() < 0) {
691        ALOGE("Could not initialize globals; exiting.\n");
692        exit(1);
693    }
694
695    if (initialize_directories() < 0) {
696        ALOGE("Could not create directories; exiting.\n");
697        exit(1);
698    }
699
700    if (selinux_enabled && selinux_status_open(true) < 0) {
701        ALOGE("Could not open selinux status; exiting.\n");
702        exit(1);
703    }
704
705    drop_privileges();
706
707    lsocket = android_get_control_socket(SOCKET_PATH);
708    if (lsocket < 0) {
709        ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
710        exit(1);
711    }
712    if (listen(lsocket, 5)) {
713        ALOGE("Listen on socket failed: %s\n", strerror(errno));
714        exit(1);
715    }
716    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
717
718    for (;;) {
719        alen = sizeof(addr);
720        s = accept(lsocket, &addr, &alen);
721        if (s < 0) {
722            ALOGE("Accept failed: %s\n", strerror(errno));
723            continue;
724        }
725        fcntl(s, F_SETFD, FD_CLOEXEC);
726
727        ALOGI("new connection\n");
728        for (;;) {
729            unsigned short count;
730            if (readx(s, &count, sizeof(count))) {
731                ALOGE("failed to read size\n");
732                break;
733            }
734            if ((count < 1) || (count >= BUFFER_MAX)) {
735                ALOGE("invalid size %d\n", count);
736                break;
737            }
738            if (readx(s, buf, count)) {
739                ALOGE("failed to read command\n");
740                break;
741            }
742            buf[count] = 0;
743            if (selinux_enabled && selinux_status_updated() > 0) {
744                selinux_android_seapp_context_reload();
745            }
746            if (execute(s, buf)) break;
747        }
748        ALOGI("closing connection\n");
749        close(s);
750    }
751
752    return 0;
753}
754