The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | static int do_ping(char **arg, char reply[REPLY_MAX]) |
| 25 | { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | static int do_install(char **arg, char reply[REPLY_MAX]) |
| 30 | { |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 31 | return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static 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 | |
| 40 | static int do_move_dex(char **arg, char reply[REPLY_MAX]) |
| 41 | { |
| 42 | return move_dex(arg[0], arg[1]); /* src, dst */ |
| 43 | } |
| 44 | |
| 45 | static int do_rm_dex(char **arg, char reply[REPLY_MAX]) |
| 46 | { |
| 47 | return rm_dex(arg[0]); /* pkgname */ |
| 48 | } |
| 49 | |
| 50 | static int do_remove(char **arg, char reply[REPLY_MAX]) |
| 51 | { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 52 | return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 55 | static int do_rename(char **arg, char reply[REPLY_MAX]) |
| 56 | { |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 57 | return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 60 | static 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */ |
| 66 | { |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 67 | return free_cache((int64_t)atoll(arg[0])); /* free_size */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int do_rm_cache(char **arg, char reply[REPLY_MAX]) |
| 71 | { |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 72 | return delete_cache(arg[0]); /* pkgname */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int do_protect(char **arg, char reply[REPLY_MAX]) |
| 76 | { |
| 77 | return protect(arg[0], atoi(arg[1])); /* pkgname, gid */ |
| 78 | } |
| 79 | |
| 80 | static int do_get_size(char **arg, char reply[REPLY_MAX]) |
| 81 | { |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 82 | int64_t codesize = 0; |
| 83 | int64_t datasize = 0; |
| 84 | int64_t cachesize = 0; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 85 | int64_t asecsize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | int res = 0; |
| 87 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 88 | /* pkgdir, persona, apkpath */ |
| 89 | res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], |
| 90 | &codesize, &datasize, &cachesize, &asecsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 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 | */ |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 96 | snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, |
| 97 | codesize, datasize, cachesize, asecsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | return res; |
| 99 | } |
| 100 | |
| 101 | static int do_rm_user_data(char **arg, char reply[REPLY_MAX]) |
| 102 | { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 103 | return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ |
| 104 | } |
| 105 | |
| 106 | static 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 | |
| 111 | static int do_rm_user(char **arg, char reply[REPLY_MAX]) |
| 112 | { |
| 113 | return delete_persona(atoi(arg[0])); /* userid */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 116 | static 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 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 121 | static int do_movefiles(char **arg, char reply[REPLY_MAX]) |
| 122 | { |
| 123 | return movefiles(); |
| 124 | } |
| 125 | |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 126 | static int do_linklib(char **arg, char reply[REPLY_MAX]) |
| 127 | { |
| 128 | return linklib(arg[0], arg[1]); |
| 129 | } |
| 130 | |
| 131 | static int do_unlinklib(char **arg, char reply[REPLY_MAX]) |
| 132 | { |
| 133 | return unlinklib(arg[0]); |
| 134 | } |
| 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | struct cmdinfo { |
| 137 | const char *name; |
| 138 | unsigned numargs; |
| 139 | int (*func)(char **arg, char reply[REPLY_MAX]); |
| 140 | }; |
| 141 | |
| 142 | struct cmdinfo cmds[] = { |
| 143 | { "ping", 0, do_ping }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 144 | { "install", 3, do_install }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | { "dexopt", 3, do_dexopt }, |
| 146 | { "movedex", 2, do_move_dex }, |
| 147 | { "rmdex", 1, do_rm_dex }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 148 | { "remove", 2, do_remove }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 149 | { "rename", 2, do_rename }, |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 150 | { "fixuid", 3, do_fixuid }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | { "freecache", 1, do_free_cache }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 152 | { "rmcache", 1, do_rm_cache }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | { "protect", 2, do_protect }, |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 154 | { "getsize", 5, do_get_size }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 155 | { "rmuserdata", 2, do_rm_user_data }, |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 156 | { "movefiles", 0, do_movefiles }, |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 157 | { "linklib", 2, do_linklib }, |
| 158 | { "unlinklib", 1, do_unlinklib }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 159 | { "mkuserdata", 3, do_mk_user_data }, |
| 160 | { "rmuser", 1, do_rm_user }, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 161 | { "cloneuserdata", 3, do_clone_user_data }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | static 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; |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 173 | ALOGE("read error: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | if (r == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 177 | ALOGE("eof\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | return -1; /* EOF */ |
| 179 | } |
| 180 | n += r; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static 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; |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 194 | ALOGE("write error: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 207 | static 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 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 216 | // ALOGI("execute('%s')\n", cmd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 229 | ALOGE("too many arguments\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 239 | ALOGE("%s requires %d arguments (%d given)\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | cmds[i].name, cmds[i].numargs, n); |
| 241 | } else { |
| 242 | ret = cmds[i].func(arg + 1, reply); |
| 243 | } |
| 244 | goto done; |
| 245 | } |
| 246 | } |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 247 | ALOGE("unsupported command '%s'\n", arg[0]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
| 249 | done: |
| 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 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 258 | // ALOGI("reply: '%s'\n", cmd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | if (writex(s, &count, sizeof(count))) return -1; |
| 260 | if (writex(s, cmd, count)) return -1; |
| 261 | return 0; |
| 262 | } |
| 263 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Initialize all the global variables that are used elsewhere. Returns 0 upon |
| 266 | * success and -1 on error. |
| 267 | */ |
| 268 | void 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 | |
| 280 | int 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 | |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 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 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 311 | ALOGE("Couldn't allocate array for dirs; aborting\n"); |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 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 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 326 | // vendor |
| 327 | // TODO replace this with an environment variable (doesn't exist yet) |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 328 | android_system_dirs.dirs[1].path = "/vendor/app/"; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 329 | android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 334 | int initialize_directories() { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 335 | int res = -1; |
| 336 | int version = 0; |
| 337 | FILE* file; |
| 338 | |
| 339 | // Read current filesystem layout version to handle upgrade paths |
| 340 | char version_path[PATH_MAX]; |
| 341 | if (snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path) > PATH_MAX) { |
| 342 | return -1; |
| 343 | } |
| 344 | file = fopen(version_path, "r"); |
| 345 | if (file != NULL) { |
| 346 | fscanf(file, "%d", &version); |
| 347 | fclose(file); |
| 348 | } |
| 349 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 350 | // /data/user |
| 351 | char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); |
| 352 | // /data/data |
| 353 | char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); |
| 354 | // /data/user/0 |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 355 | char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); |
| 356 | if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { |
| 357 | goto fail; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 358 | } |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 359 | |
| 360 | // Make the /data/user directory if necessary |
| 361 | if (access(user_data_dir, R_OK) < 0) { |
| 362 | if (mkdir(user_data_dir, 0711) < 0) { |
| 363 | goto fail; |
| 364 | } |
| 365 | if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { |
| 366 | goto fail; |
| 367 | } |
| 368 | if (chmod(user_data_dir, 0711) < 0) { |
| 369 | goto fail; |
| 370 | } |
| 371 | } |
| 372 | // Make the /data/user/0 symlink to /data/data if necessary |
| 373 | if (access(primary_data_dir, R_OK) < 0) { |
| 374 | if (symlink(legacy_data_dir, primary_data_dir)) { |
| 375 | goto fail; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // /data/media/0 |
| 380 | char owner_media_dir[PATH_MAX]; |
| 381 | create_persona_media_path(owner_media_dir, 0); |
| 382 | |
| 383 | if (version == 0) { |
| 384 | // Introducing multi-user, so migrate /data/media contents into /data/media/0 |
| 385 | ALOGD("Migrating /data/media for multi-user"); |
| 386 | |
Jeff Sharkey | dc9b012 | 2012-08-27 11:41:55 -0700 | [diff] [blame^] | 387 | // Ensure /data/media |
| 388 | if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 389 | goto fail; |
| 390 | } |
| 391 | |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 392 | // /data/media.tmp |
| 393 | char media_tmp_dir[PATH_MAX]; |
| 394 | snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); |
| 395 | |
| 396 | // Only copy when upgrade not already in progress |
| 397 | if (access(media_tmp_dir, F_OK) == -1) { |
| 398 | if (rename(android_media_dir.path, media_tmp_dir) == -1) { |
| 399 | ALOGE("Failed to move legacy media path: %s", strerror(errno)); |
| 400 | goto fail; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Create /data/media again |
| 405 | if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 406 | goto fail; |
| 407 | } |
| 408 | |
| 409 | // Move any owner data into place |
| 410 | if (access(media_tmp_dir, F_OK) == 0) { |
| 411 | if (rename(media_tmp_dir, owner_media_dir) == -1) { |
| 412 | ALOGE("Failed to move owner media path: %s", strerror(errno)); |
| 413 | goto fail; |
| 414 | } |
| 415 | } |
Jeff Sharkey | 91bbb8a | 2012-08-16 23:28:50 -0700 | [diff] [blame] | 416 | |
| 417 | // Ensure media directories for any existing users |
| 418 | DIR *dir; |
| 419 | struct dirent *dirent; |
| 420 | char user_media_dir[PATH_MAX]; |
| 421 | |
| 422 | dir = opendir(user_data_dir); |
| 423 | if (dir != NULL) { |
| 424 | while ((dirent = readdir(dir))) { |
| 425 | if (dirent->d_type == DT_DIR) { |
| 426 | const char *name = dirent->d_name; |
| 427 | |
| 428 | // skip "." and ".." |
| 429 | if (name[0] == '.') { |
| 430 | if (name[1] == 0) continue; |
| 431 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 432 | } |
| 433 | |
| 434 | // /data/media/<user_id> |
| 435 | snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); |
| 436 | if (ensure_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 437 | ALOGE("Failed to ensure %s: %s", user_media_dir, strerror(errno)); |
| 438 | goto fail; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | closedir(dir); |
| 443 | } |
| 444 | |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 445 | version = 1; |
| 446 | } |
| 447 | |
| 448 | // Ensure /data/media/0 is always ready |
| 449 | if (ensure_dir(owner_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 450 | goto fail; |
| 451 | } |
| 452 | |
| 453 | // Persist our current version |
| 454 | file = fopen(version_path, "w"); |
| 455 | if (file != NULL) { |
| 456 | fprintf(file, "%d", version); |
| 457 | fsync(fileno(file)); |
| 458 | fclose(file); |
| 459 | } else { |
| 460 | ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); |
| 461 | goto fail; |
| 462 | } |
| 463 | |
| 464 | // Success! |
| 465 | res = 0; |
| 466 | |
| 467 | fail: |
| 468 | free(user_data_dir); |
| 469 | free(legacy_data_dir); |
| 470 | free(primary_data_dir); |
| 471 | return res; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 474 | int main(const int argc, const char *argv[]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | char buf[BUFFER_MAX]; |
| 476 | struct sockaddr addr; |
| 477 | socklen_t alen; |
| 478 | int lsocket, s, count; |
| 479 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 480 | if (initialize_globals() < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 481 | ALOGE("Could not initialize globals; exiting.\n"); |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 482 | exit(1); |
| 483 | } |
| 484 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 485 | if (initialize_directories() < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 486 | ALOGE("Could not create directories; exiting.\n"); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 487 | exit(1); |
| 488 | } |
| 489 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | lsocket = android_get_control_socket(SOCKET_PATH); |
| 491 | if (lsocket < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 492 | ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | exit(1); |
| 494 | } |
| 495 | if (listen(lsocket, 5)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 496 | ALOGE("Listen on socket failed: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | exit(1); |
| 498 | } |
| 499 | fcntl(lsocket, F_SETFD, FD_CLOEXEC); |
| 500 | |
| 501 | for (;;) { |
| 502 | alen = sizeof(addr); |
| 503 | s = accept(lsocket, &addr, &alen); |
| 504 | if (s < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 505 | ALOGE("Accept failed: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | continue; |
| 507 | } |
| 508 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 509 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 510 | ALOGI("new connection\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | for (;;) { |
| 512 | unsigned short count; |
| 513 | if (readx(s, &count, sizeof(count))) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 514 | ALOGE("failed to read size\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | if ((count < 1) || (count >= BUFFER_MAX)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 518 | ALOGE("invalid size %d\n", count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | if (readx(s, buf, count)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 522 | ALOGE("failed to read command\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | buf[count] = 0; |
| 526 | if (execute(s, buf)) break; |
| 527 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 528 | ALOGI("closing connection\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | close(s); |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |