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