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