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