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