installd.c revision 5b1ada2562c17921adf6a62ea62bcb445160983c
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, apkpath */
89    res = get_size(arg[0], arg[1], arg[2], arg[3], &codesize, &datasize, &cachesize, &asecsize);
90
91    /*
92     * Each int64_t can take up 22 characters printed out. Make sure it
93     * doesn't go over REPLY_MAX in the future.
94     */
95    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
96            codesize, datasize, cachesize, asecsize);
97    return res;
98}
99
100static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
101{
102    return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
103}
104
105static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
106{
107    return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
108}
109
110static int do_rm_user(char **arg, char reply[REPLY_MAX])
111{
112    return delete_persona(atoi(arg[0])); /* userid */
113}
114
115static int do_clone_user_data(char **arg, char reply[REPLY_MAX])
116{
117    return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2]));
118}
119
120static int do_movefiles(char **arg, char reply[REPLY_MAX])
121{
122    return movefiles();
123}
124
125static int do_linklib(char **arg, char reply[REPLY_MAX])
126{
127    return linklib(arg[0], arg[1]);
128}
129
130static int do_unlinklib(char **arg, char reply[REPLY_MAX])
131{
132    return unlinklib(arg[0]);
133}
134
135struct cmdinfo {
136    const char *name;
137    unsigned numargs;
138    int (*func)(char **arg, char reply[REPLY_MAX]);
139};
140
141struct cmdinfo cmds[] = {
142    { "ping",                 0, do_ping },
143    { "install",              3, do_install },
144    { "dexopt",               3, do_dexopt },
145    { "movedex",              2, do_move_dex },
146    { "rmdex",                1, do_rm_dex },
147    { "remove",               2, do_remove },
148    { "rename",               2, do_rename },
149    { "fixuid",               3, do_fixuid },
150    { "freecache",            1, do_free_cache },
151    { "rmcache",              1, do_rm_cache },
152    { "protect",              2, do_protect },
153    { "getsize",              4, do_get_size },
154    { "rmuserdata",           2, do_rm_user_data },
155    { "movefiles",            0, do_movefiles },
156    { "linklib",              2, do_linklib },
157    { "unlinklib",            1, do_unlinklib },
158    { "mkuserdata",           3, do_mk_user_data },
159    { "rmuser",               1, do_rm_user },
160    { "cloneuserdata",        3, do_clone_user_data },
161};
162
163static int readx(int s, void *_buf, int count)
164{
165    char *buf = _buf;
166    int n = 0, r;
167    if (count < 0) return -1;
168    while (n < count) {
169        r = read(s, buf + n, count - n);
170        if (r < 0) {
171            if (errno == EINTR) continue;
172            ALOGE("read error: %s\n", strerror(errno));
173            return -1;
174        }
175        if (r == 0) {
176            ALOGE("eof\n");
177            return -1; /* EOF */
178        }
179        n += r;
180    }
181    return 0;
182}
183
184static int writex(int s, const void *_buf, int count)
185{
186    const char *buf = _buf;
187    int n = 0, r;
188    if (count < 0) return -1;
189    while (n < count) {
190        r = write(s, buf + n, count - n);
191        if (r < 0) {
192            if (errno == EINTR) continue;
193            ALOGE("write error: %s\n", strerror(errno));
194            return -1;
195        }
196        n += r;
197    }
198    return 0;
199}
200
201
202/* Tokenize the command buffer, locate a matching command,
203 * ensure that the required number of arguments are provided,
204 * call the function(), return the result.
205 */
206static int execute(int s, char cmd[BUFFER_MAX])
207{
208    char reply[REPLY_MAX];
209    char *arg[TOKEN_MAX+1];
210    unsigned i;
211    unsigned n = 0;
212    unsigned short count;
213    int ret = -1;
214
215//    ALOGI("execute('%s')\n", cmd);
216
217        /* default reply is "" */
218    reply[0] = 0;
219
220        /* n is number of args (not counting arg[0]) */
221    arg[0] = cmd;
222    while (*cmd) {
223        if (isspace(*cmd)) {
224            *cmd++ = 0;
225            n++;
226            arg[n] = cmd;
227            if (n == TOKEN_MAX) {
228                ALOGE("too many arguments\n");
229                goto done;
230            }
231        }
232        cmd++;
233    }
234
235    for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
236        if (!strcmp(cmds[i].name,arg[0])) {
237            if (n != cmds[i].numargs) {
238                ALOGE("%s requires %d arguments (%d given)\n",
239                     cmds[i].name, cmds[i].numargs, n);
240            } else {
241                ret = cmds[i].func(arg + 1, reply);
242            }
243            goto done;
244        }
245    }
246    ALOGE("unsupported command '%s'\n", arg[0]);
247
248done:
249    if (reply[0]) {
250        n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
251    } else {
252        n = snprintf(cmd, BUFFER_MAX, "%d", ret);
253    }
254    if (n > BUFFER_MAX) n = BUFFER_MAX;
255    count = n;
256
257//    ALOGI("reply: '%s'\n", cmd);
258    if (writex(s, &count, sizeof(count))) return -1;
259    if (writex(s, cmd, count)) return -1;
260    return 0;
261}
262
263/**
264 * Initialize all the global variables that are used elsewhere. Returns 0 upon
265 * success and -1 on error.
266 */
267void free_globals() {
268    size_t i;
269
270    for (i = 0; i < android_system_dirs.count; i++) {
271        if (android_system_dirs.dirs[i].path != NULL) {
272            free(android_system_dirs.dirs[i].path);
273        }
274    }
275
276    free(android_system_dirs.dirs);
277}
278
279int initialize_globals() {
280    // Get the android data directory.
281    if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
282        return -1;
283    }
284
285    // Get the android app directory.
286    if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
287        return -1;
288    }
289
290    // Get the android protected app directory.
291    if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
292        return -1;
293    }
294
295    // Get the sd-card ASEC mount point.
296    if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
297        return -1;
298    }
299
300    // Get the android media directory.
301    if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
302        return -1;
303    }
304
305    // Take note of the system and vendor directories.
306    android_system_dirs.count = 2;
307
308    android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
309    if (android_system_dirs.dirs == NULL) {
310        ALOGE("Couldn't allocate array for dirs; aborting\n");
311        return -1;
312    }
313
314    // system
315    if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
316        free_globals();
317        return -1;
318    }
319
320    // append "app/" to dirs[0]
321    char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
322    android_system_dirs.dirs[0].path = system_app_path;
323    android_system_dirs.dirs[0].len = strlen(system_app_path);
324
325    // vendor
326    // TODO replace this with an environment variable (doesn't exist yet)
327    android_system_dirs.dirs[1].path = "/vendor/app/";
328    android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
329
330    return 0;
331}
332
333int initialize_directories() {
334    int res = -1;
335    int version = 0;
336    FILE* file;
337
338    // Read current filesystem layout version to handle upgrade paths
339    char version_path[PATH_MAX];
340    if (snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path) > PATH_MAX) {
341        return -1;
342    }
343    file = fopen(version_path, "r");
344    if (file != NULL) {
345        fscanf(file, "%d", &version);
346        fclose(file);
347    }
348
349    // /data/user
350    char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
351    // /data/data
352    char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
353    // /data/user/0
354    char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
355    if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
356        goto fail;
357    }
358
359    // Make the /data/user directory if necessary
360    if (access(user_data_dir, R_OK) < 0) {
361        if (mkdir(user_data_dir, 0711) < 0) {
362            goto fail;
363        }
364        if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
365            goto fail;
366        }
367        if (chmod(user_data_dir, 0711) < 0) {
368            goto fail;
369        }
370    }
371    // Make the /data/user/0 symlink to /data/data if necessary
372    if (access(primary_data_dir, R_OK) < 0) {
373        if (symlink(legacy_data_dir, primary_data_dir)) {
374            goto fail;
375        }
376    }
377
378    // /data/media/0
379    char owner_media_dir[PATH_MAX];
380    create_persona_media_path(owner_media_dir, 0);
381
382    if (version == 0) {
383        // Introducing multi-user, so migrate /data/media contents into /data/media/0
384        ALOGD("Migrating /data/media for multi-user");
385
386        // /data/media.tmp
387        char media_tmp_dir[PATH_MAX];
388        snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
389
390        // Only copy when upgrade not already in progress
391        if (access(media_tmp_dir, F_OK) == -1) {
392            if (rename(android_media_dir.path, media_tmp_dir) == -1) {
393                ALOGE("Failed to move legacy media path: %s", strerror(errno));
394                goto fail;
395            }
396        }
397
398        // Create /data/media again
399        if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
400            goto fail;
401        }
402
403        // Move any owner data into place
404        if (access(media_tmp_dir, F_OK) == 0) {
405            if (rename(media_tmp_dir, owner_media_dir) == -1) {
406                ALOGE("Failed to move owner media path: %s", strerror(errno));
407                goto fail;
408            }
409        }
410        version = 1;
411    }
412
413    // Ensure /data/media/0 is always ready
414    if (ensure_dir(owner_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
415        goto fail;
416    }
417
418    // Persist our current version
419    file = fopen(version_path, "w");
420    if (file != NULL) {
421        fprintf(file, "%d", version);
422        fsync(fileno(file));
423        fclose(file);
424    } else {
425        ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
426        goto fail;
427    }
428
429    // Success!
430    res = 0;
431
432fail:
433    free(user_data_dir);
434    free(legacy_data_dir);
435    free(primary_data_dir);
436    return res;
437}
438
439int main(const int argc, const char *argv[]) {
440    char buf[BUFFER_MAX];
441    struct sockaddr addr;
442    socklen_t alen;
443    int lsocket, s, count;
444
445    if (initialize_globals() < 0) {
446        ALOGE("Could not initialize globals; exiting.\n");
447        exit(1);
448    }
449
450    if (initialize_directories() < 0) {
451        ALOGE("Could not create directories; exiting.\n");
452        exit(1);
453    }
454
455    lsocket = android_get_control_socket(SOCKET_PATH);
456    if (lsocket < 0) {
457        ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
458        exit(1);
459    }
460    if (listen(lsocket, 5)) {
461        ALOGE("Listen on socket failed: %s\n", strerror(errno));
462        exit(1);
463    }
464    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
465
466    for (;;) {
467        alen = sizeof(addr);
468        s = accept(lsocket, &addr, &alen);
469        if (s < 0) {
470            ALOGE("Accept failed: %s\n", strerror(errno));
471            continue;
472        }
473        fcntl(s, F_SETFD, FD_CLOEXEC);
474
475        ALOGI("new connection\n");
476        for (;;) {
477            unsigned short count;
478            if (readx(s, &count, sizeof(count))) {
479                ALOGE("failed to read size\n");
480                break;
481            }
482            if ((count < 1) || (count >= BUFFER_MAX)) {
483                ALOGE("invalid size %d\n", count);
484                break;
485            }
486            if (readx(s, buf, count)) {
487                ALOGE("failed to read command\n");
488                break;
489            }
490            buf[count] = 0;
491            if (execute(s, buf)) break;
492        }
493        ALOGI("closing connection\n");
494        close(s);
495    }
496
497    return 0;
498}
499