installd.c revision 8ea0dc6a89b011d4f478c0c8192570d69cf7ce79
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
20#define BUFFER_MAX    1024  /* input buffer for commands */
21#define TOKEN_MAX     8     /* max number of arguments in buffer */
22#define REPLY_MAX     256   /* largest reply allowed */
23
24static int do_ping(char **arg, char reply[REPLY_MAX])
25{
26    return 0;
27}
28
29static int do_install(char **arg, char reply[REPLY_MAX])
30{
31    return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
32}
33
34static int do_dexopt(char **arg, char reply[REPLY_MAX])
35{
36        /* apk_path, uid, is_public */
37    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
38}
39
40static int do_move_dex(char **arg, char reply[REPLY_MAX])
41{
42    return move_dex(arg[0], arg[1]); /* src, dst */
43}
44
45static int do_rm_dex(char **arg, char reply[REPLY_MAX])
46{
47    return rm_dex(arg[0]); /* pkgname */
48}
49
50static int do_remove(char **arg, char reply[REPLY_MAX])
51{
52    return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
53}
54
55static int do_rename(char **arg, char reply[REPLY_MAX])
56{
57    return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
58}
59
60static int do_fixuid(char **arg, char reply[REPLY_MAX])
61{
62    return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
63}
64
65static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
66{
67    return free_cache((int64_t)atoll(arg[0])); /* free_size */
68}
69
70static int do_rm_cache(char **arg, char reply[REPLY_MAX])
71{
72    return delete_cache(arg[0]); /* pkgname */
73}
74
75static int do_protect(char **arg, char reply[REPLY_MAX])
76{
77    return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
78}
79
80static int do_get_size(char **arg, char reply[REPLY_MAX])
81{
82    int64_t codesize = 0;
83    int64_t datasize = 0;
84    int64_t cachesize = 0;
85    int64_t asecsize = 0;
86    int res = 0;
87
88        /* pkgdir, persona, apkpath */
89    res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4],
90            &codesize, &datasize, &cachesize, &asecsize);
91
92    /*
93     * Each int64_t can take up 22 characters printed out. Make sure it
94     * doesn't go over REPLY_MAX in the future.
95     */
96    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
97            codesize, datasize, cachesize, asecsize);
98    return res;
99}
100
101static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
102{
103    return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
104}
105
106static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
107{
108    return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
109}
110
111static int do_rm_user(char **arg, char reply[REPLY_MAX])
112{
113    return delete_persona(atoi(arg[0])); /* userid */
114}
115
116static int do_clone_user_data(char **arg, char reply[REPLY_MAX])
117{
118    return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2]));
119}
120
121static int do_movefiles(char **arg, char reply[REPLY_MAX])
122{
123    return movefiles();
124}
125
126static int do_linklib(char **arg, char reply[REPLY_MAX])
127{
128    return linklib(arg[0], arg[1]);
129}
130
131static int do_unlinklib(char **arg, char reply[REPLY_MAX])
132{
133    return unlinklib(arg[0]);
134}
135
136struct cmdinfo {
137    const char *name;
138    unsigned numargs;
139    int (*func)(char **arg, char reply[REPLY_MAX]);
140};
141
142struct cmdinfo cmds[] = {
143    { "ping",                 0, do_ping },
144    { "install",              3, do_install },
145    { "dexopt",               3, do_dexopt },
146    { "movedex",              2, do_move_dex },
147    { "rmdex",                1, do_rm_dex },
148    { "remove",               2, do_remove },
149    { "rename",               2, do_rename },
150    { "fixuid",               3, do_fixuid },
151    { "freecache",            1, do_free_cache },
152    { "rmcache",              1, do_rm_cache },
153    { "protect",              2, do_protect },
154    { "getsize",              5, do_get_size },
155    { "rmuserdata",           2, do_rm_user_data },
156    { "movefiles",            0, do_movefiles },
157    { "linklib",              2, do_linklib },
158    { "unlinklib",            1, do_unlinklib },
159    { "mkuserdata",           3, do_mk_user_data },
160    { "rmuser",               1, do_rm_user },
161    { "cloneuserdata",        3, do_clone_user_data },
162};
163
164static int readx(int s, void *_buf, int count)
165{
166    char *buf = _buf;
167    int n = 0, r;
168    if (count < 0) return -1;
169    while (n < count) {
170        r = read(s, buf + n, count - n);
171        if (r < 0) {
172            if (errno == EINTR) continue;
173            ALOGE("read error: %s\n", strerror(errno));
174            return -1;
175        }
176        if (r == 0) {
177            ALOGE("eof\n");
178            return -1; /* EOF */
179        }
180        n += r;
181    }
182    return 0;
183}
184
185static int writex(int s, const void *_buf, int count)
186{
187    const char *buf = _buf;
188    int n = 0, r;
189    if (count < 0) return -1;
190    while (n < count) {
191        r = write(s, buf + n, count - n);
192        if (r < 0) {
193            if (errno == EINTR) continue;
194            ALOGE("write error: %s\n", strerror(errno));
195            return -1;
196        }
197        n += r;
198    }
199    return 0;
200}
201
202
203/* Tokenize the command buffer, locate a matching command,
204 * ensure that the required number of arguments are provided,
205 * call the function(), return the result.
206 */
207static int execute(int s, char cmd[BUFFER_MAX])
208{
209    char reply[REPLY_MAX];
210    char *arg[TOKEN_MAX+1];
211    unsigned i;
212    unsigned n = 0;
213    unsigned short count;
214    int ret = -1;
215
216//    ALOGI("execute('%s')\n", cmd);
217
218        /* default reply is "" */
219    reply[0] = 0;
220
221        /* n is number of args (not counting arg[0]) */
222    arg[0] = cmd;
223    while (*cmd) {
224        if (isspace(*cmd)) {
225            *cmd++ = 0;
226            n++;
227            arg[n] = cmd;
228            if (n == TOKEN_MAX) {
229                ALOGE("too many arguments\n");
230                goto done;
231            }
232        }
233        cmd++;
234    }
235
236    for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
237        if (!strcmp(cmds[i].name,arg[0])) {
238            if (n != cmds[i].numargs) {
239                ALOGE("%s requires %d arguments (%d given)\n",
240                     cmds[i].name, cmds[i].numargs, n);
241            } else {
242                ret = cmds[i].func(arg + 1, reply);
243            }
244            goto done;
245        }
246    }
247    ALOGE("unsupported command '%s'\n", arg[0]);
248
249done:
250    if (reply[0]) {
251        n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
252    } else {
253        n = snprintf(cmd, BUFFER_MAX, "%d", ret);
254    }
255    if (n > BUFFER_MAX) n = BUFFER_MAX;
256    count = n;
257
258//    ALOGI("reply: '%s'\n", cmd);
259    if (writex(s, &count, sizeof(count))) return -1;
260    if (writex(s, cmd, count)) return -1;
261    return 0;
262}
263
264/**
265 * Initialize all the global variables that are used elsewhere. Returns 0 upon
266 * success and -1 on error.
267 */
268void free_globals() {
269    size_t i;
270
271    for (i = 0; i < android_system_dirs.count; i++) {
272        if (android_system_dirs.dirs[i].path != NULL) {
273            free(android_system_dirs.dirs[i].path);
274        }
275    }
276
277    free(android_system_dirs.dirs);
278}
279
280int initialize_globals() {
281    // Get the android data directory.
282    if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
283        return -1;
284    }
285
286    // Get the android app directory.
287    if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
288        return -1;
289    }
290
291    // Get the android protected app directory.
292    if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
293        return -1;
294    }
295
296    // Get the sd-card ASEC mount point.
297    if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
298        return -1;
299    }
300
301    // Get the android media directory.
302    if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
303        return -1;
304    }
305
306    // Take note of the system and vendor directories.
307    android_system_dirs.count = 2;
308
309    android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
310    if (android_system_dirs.dirs == NULL) {
311        ALOGE("Couldn't allocate array for dirs; aborting\n");
312        return -1;
313    }
314
315    // system
316    if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
317        free_globals();
318        return -1;
319    }
320
321    // append "app/" to dirs[0]
322    char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
323    android_system_dirs.dirs[0].path = system_app_path;
324    android_system_dirs.dirs[0].len = strlen(system_app_path);
325
326    // vendor
327    // TODO replace this with an environment variable (doesn't exist yet)
328    android_system_dirs.dirs[1].path = "/vendor/app/";
329    android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
330
331    return 0;
332}
333
334int initialize_directories() {
335    int res = -1;
336
337    // Read current filesystem layout version to handle upgrade paths
338    char version_path[PATH_MAX];
339    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
340
341    int oldVersion;
342    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
343        oldVersion = 0;
344    }
345    int version = oldVersion;
346
347    // /data/user
348    char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
349    // /data/data
350    char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
351    // /data/user/0
352    char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
353    if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
354        goto fail;
355    }
356
357    // Make the /data/user directory if necessary
358    if (access(user_data_dir, R_OK) < 0) {
359        if (mkdir(user_data_dir, 0711) < 0) {
360            goto fail;
361        }
362        if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
363            goto fail;
364        }
365        if (chmod(user_data_dir, 0711) < 0) {
366            goto fail;
367        }
368    }
369    // Make the /data/user/0 symlink to /data/data if necessary
370    if (access(primary_data_dir, R_OK) < 0) {
371        if (symlink(legacy_data_dir, primary_data_dir)) {
372            goto fail;
373        }
374    }
375
376    if (version == 0) {
377        // Introducing multi-user, so migrate /data/media contents into /data/media/0
378        ALOGD("Upgrading /data/media for multi-user");
379
380        // Ensure /data/media
381        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
382            goto fail;
383        }
384
385        // /data/media.tmp
386        char media_tmp_dir[PATH_MAX];
387        snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
388
389        // Only copy when upgrade not already in progress
390        if (access(media_tmp_dir, F_OK) == -1) {
391            if (rename(android_media_dir.path, media_tmp_dir) == -1) {
392                ALOGE("Failed to move legacy media path: %s", strerror(errno));
393                goto fail;
394            }
395        }
396
397        // Create /data/media again
398        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
399            goto fail;
400        }
401
402        // /data/media/0
403        char owner_media_dir[PATH_MAX];
404        snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
405
406        // Move any owner data into place
407        if (access(media_tmp_dir, F_OK) == 0) {
408            if (rename(media_tmp_dir, owner_media_dir) == -1) {
409                ALOGE("Failed to move owner media path: %s", strerror(errno));
410                goto fail;
411            }
412        }
413
414        // Ensure media directories for any existing users
415        DIR *dir;
416        struct dirent *dirent;
417        char user_media_dir[PATH_MAX];
418
419        dir = opendir(user_data_dir);
420        if (dir != NULL) {
421            while ((dirent = readdir(dir))) {
422                if (dirent->d_type == DT_DIR) {
423                    const char *name = dirent->d_name;
424
425                    // skip "." and ".."
426                    if (name[0] == '.') {
427                        if (name[1] == 0) continue;
428                        if ((name[1] == '.') && (name[2] == 0)) continue;
429                    }
430
431                    // /data/media/<user_id>
432                    snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
433                    if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
434                        goto fail;
435                    }
436                }
437            }
438            closedir(dir);
439        }
440
441        version = 1;
442    }
443
444    // /data/media/obb
445    char media_obb_dir[PATH_MAX];
446    snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
447
448    if (version == 1) {
449        // Introducing /data/media/obb for sharing OBB across users; migrate
450        // any existing OBB files from owner.
451        ALOGD("Upgrading to shared /data/media/obb");
452
453        // /data/media/0/Android/obb
454        char owner_obb_path[PATH_MAX];
455        snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
456
457        // Only move if target doesn't already exist
458        if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
459            if (rename(owner_obb_path, media_obb_dir) == -1) {
460                ALOGE("Failed to move OBB from owner: %s", strerror(errno));
461                goto fail;
462            }
463        }
464
465        version = 2;
466    }
467
468    if (ensure_media_user_dirs(0) == -1) {
469        ALOGE("Failed to setup media for user 0");
470        goto fail;
471    }
472    if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
473        goto fail;
474    }
475
476    // Persist layout version if changed
477    if (version != oldVersion) {
478        if (fs_write_atomic_int(version_path, version) == -1) {
479            ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
480            goto fail;
481        }
482    }
483
484    // Success!
485    res = 0;
486
487fail:
488    free(user_data_dir);
489    free(legacy_data_dir);
490    free(primary_data_dir);
491    return res;
492}
493
494int main(const int argc, const char *argv[]) {
495    char buf[BUFFER_MAX];
496    struct sockaddr addr;
497    socklen_t alen;
498    int lsocket, s, count;
499
500    if (initialize_globals() < 0) {
501        ALOGE("Could not initialize globals; exiting.\n");
502        exit(1);
503    }
504
505    if (initialize_directories() < 0) {
506        ALOGE("Could not create directories; exiting.\n");
507        exit(1);
508    }
509
510    lsocket = android_get_control_socket(SOCKET_PATH);
511    if (lsocket < 0) {
512        ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
513        exit(1);
514    }
515    if (listen(lsocket, 5)) {
516        ALOGE("Listen on socket failed: %s\n", strerror(errno));
517        exit(1);
518    }
519    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
520
521    for (;;) {
522        alen = sizeof(addr);
523        s = accept(lsocket, &addr, &alen);
524        if (s < 0) {
525            ALOGE("Accept failed: %s\n", strerror(errno));
526            continue;
527        }
528        fcntl(s, F_SETFD, FD_CLOEXEC);
529
530        ALOGI("new connection\n");
531        for (;;) {
532            unsigned short count;
533            if (readx(s, &count, sizeof(count))) {
534                ALOGE("failed to read size\n");
535                break;
536            }
537            if ((count < 1) || (count >= BUFFER_MAX)) {
538                ALOGE("invalid size %d\n", count);
539                break;
540            }
541            if (readx(s, buf, count)) {
542                ALOGE("failed to read command\n");
543                break;
544            }
545            buf[count] = 0;
546            if (execute(s, buf)) break;
547        }
548        ALOGI("closing connection\n");
549        close(s);
550    }
551
552    return 0;
553}
554