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