Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 17 | #include <fcntl.h> |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 18 | #include <selinux/android.h> |
| 19 | #include <selinux/avc.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 20 | #include <sys/capability.h> |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 21 | #include <sys/fsuid.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <sys/prctl.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | #include <cutils/fs.h> |
| 28 | #include <cutils/log.h> // TODO: Move everything to base::logging. |
| 29 | #include <cutils/properties.h> |
| 30 | #include <cutils/sockets.h> |
| 31 | #include <private/android_filesystem_config.h> |
| 32 | |
| 33 | #include <commands.h> |
| 34 | #include <globals.h> |
| 35 | #include <installd_constants.h> |
| 36 | #include <installd_deps.h> // Need to fill in requirements of commands. |
| 37 | #include <utils.h> |
| 38 | |
| 39 | #ifndef LOG_TAG |
| 40 | #define LOG_TAG "installd" |
| 41 | #endif |
| 42 | #define SOCKET_PATH "installd" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 43 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 44 | #define BUFFER_MAX 1024 /* input buffer for commands */ |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 45 | #define TOKEN_MAX 16 /* max number of arguments in buffer */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 46 | #define REPLY_MAX 256 /* largest reply allowed */ |
| 47 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 48 | #define DEBUG_FBE 0 |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | namespace installd { |
| 52 | |
| 53 | // Check that installd-deps sizes match cutils sizes. |
| 54 | static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch."); |
| 55 | static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch."); |
| 56 | |
| 57 | //////////////////////// |
| 58 | // Plug-in functions. // |
| 59 | //////////////////////// |
| 60 | |
| 61 | int get_property(const char *key, char *value, const char *default_value) { |
| 62 | return property_get(key, value, default_value); |
| 63 | } |
| 64 | |
| 65 | // Compute the output path of |
| 66 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], |
| 67 | const char *oat_dir, |
| 68 | const char *apk_path, |
| 69 | const char *instruction_set) { |
| 70 | char *file_name_start; |
| 71 | char *file_name_end; |
| 72 | |
| 73 | file_name_start = strrchr(apk_path, '/'); |
| 74 | if (file_name_start == NULL) { |
| 75 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); |
| 76 | return false; |
| 77 | } |
| 78 | file_name_end = strrchr(apk_path, '.'); |
| 79 | if (file_name_end < file_name_start) { |
| 80 | ALOGE("apk_path '%s' has no extension\n", apk_path); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | // Calculate file_name |
| 85 | int file_name_len = file_name_end - file_name_start - 1; |
| 86 | char file_name[file_name_len + 1]; |
| 87 | memcpy(file_name, file_name_start + 1, file_name_len); |
| 88 | file_name[file_name_len] = '\0'; |
| 89 | |
| 90 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex |
| 91 | snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Computes the odex file for the given apk_path and instruction_set. |
| 97 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex |
| 98 | * |
| 99 | * Returns false if it failed to determine the odex file path. |
| 100 | */ |
| 101 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], |
| 102 | const char *apk_path, |
| 103 | const char *instruction_set) { |
| 104 | if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set) |
| 105 | + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) { |
| 106 | ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | strcpy(path, apk_path); |
| 111 | char *end = strrchr(path, '/'); |
| 112 | if (end == NULL) { |
| 113 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); |
| 114 | return false; |
| 115 | } |
| 116 | const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/'); |
| 117 | |
| 118 | strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0 |
| 119 | strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0 |
| 120 | strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0 |
| 121 | end = strrchr(path, '.'); |
| 122 | if (end == NULL) { |
| 123 | ALOGE("apk_path '%s' has no extension.\n", apk_path); |
| 124 | return false; |
| 125 | } |
| 126 | strcpy(end + 1, "odex"); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool create_cache_path(char path[PKG_PATH_MAX], |
| 131 | const char *src, |
| 132 | const char *instruction_set) { |
| 133 | size_t srclen = strlen(src); |
| 134 | |
| 135 | /* demand that we are an absolute path */ |
| 136 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | size_t dstlen = |
| 145 | android_data_dir.len + |
| 146 | strlen(DALVIK_CACHE) + |
| 147 | 1 + |
| 148 | strlen(instruction_set) + |
| 149 | srclen + |
| 150 | strlen(DALVIK_CACHE_POSTFIX) + 2; |
| 151 | |
| 152 | if (dstlen > PKG_PATH_MAX) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | sprintf(path,"%s%s/%s/%s%s", |
| 157 | android_data_dir.path, |
| 158 | DALVIK_CACHE, |
| 159 | instruction_set, |
| 160 | src + 1, /* skip the leading / */ |
| 161 | DALVIK_CACHE_POSTFIX); |
| 162 | |
| 163 | char* tmp = |
| 164 | path + |
| 165 | android_data_dir.len + |
| 166 | strlen(DALVIK_CACHE) + |
| 167 | 1 + |
| 168 | strlen(instruction_set) + 1; |
| 169 | |
| 170 | for(; *tmp; tmp++) { |
| 171 | if (*tmp == '/') { |
| 172 | *tmp = '@'; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 180 | static char* parse_null(char* arg) { |
| 181 | if (strcmp(arg, "!") == 0) { |
| 182 | return nullptr; |
| 183 | } else { |
| 184 | return arg; |
| 185 | } |
| 186 | } |
| 187 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 188 | static int do_ping(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 193 | static int do_create_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) { |
| 194 | /* const char *uuid, const char *pkgname, userid_t userid, int flags, |
| 195 | appid_t appid, const char* seinfo */ |
| 196 | return create_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]); |
| 197 | } |
| 198 | |
| 199 | static int do_restorecon_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) { |
| 200 | /* const char* uuid, const char* pkgName, userid_t userid, int flags, |
| 201 | appid_t appid, const char* seinfo */ |
| 202 | return restorecon_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]); |
| 203 | } |
| 204 | |
| 205 | static int do_clear_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) { |
| 206 | /* const char *uuid, const char *pkgname, userid_t userid, int flags */ |
| 207 | return clear_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); |
| 208 | } |
| 209 | |
| 210 | static int do_destroy_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) { |
| 211 | /* const char *uuid, const char *pkgname, userid_t userid, int flags */ |
| 212 | return destroy_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 215 | static int do_dexopt(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 216 | { |
Calin Juravle | 60a794d | 2015-12-24 12:36:41 +0200 | [diff] [blame^] | 217 | /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags, volume_uuid, |
| 218 | use_profiles */ |
Todd Kennedy | 76e767c | 2015-09-25 07:47:47 -0700 | [diff] [blame] | 219 | return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]), |
Calin Juravle | 60a794d | 2015-12-24 12:36:41 +0200 | [diff] [blame^] | 220 | arg[5], atoi(arg[6]), parse_null(arg[7]), (atoi(arg[8]) == 0 ? false : true)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 223 | static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 224 | { |
| 225 | return mark_boot_complete(arg[0] /* instruction set */); |
| 226 | } |
| 227 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 228 | static int do_rm_dex(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 229 | { |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 230 | return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 233 | static int do_free_cache(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) /* TODO int:free_size */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 234 | { |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 235 | return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 238 | static int do_get_app_size(char **arg, char reply[REPLY_MAX]) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 239 | int64_t codesize = 0; |
| 240 | int64_t datasize = 0; |
| 241 | int64_t cachesize = 0; |
| 242 | int64_t asecsize = 0; |
| 243 | int res = 0; |
| 244 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 245 | /* const char *uuid, const char *pkgname, userid_t userid, int flags, |
| 246 | const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath, |
| 247 | const char *asecpath, const char *instruction_set */ |
| 248 | res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4], arg[5], |
| 249 | arg[6], arg[7], arg[8], &codesize, &datasize, &cachesize, &asecsize); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Each int64_t can take up 22 characters printed out. Make sure it |
| 253 | * doesn't go over REPLY_MAX in the future. |
| 254 | */ |
| 255 | snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, |
| 256 | codesize, datasize, cachesize, asecsize); |
| 257 | return res; |
| 258 | } |
| 259 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 260 | static int do_move_complete_app(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) { |
| 261 | /* const char* from_uuid, const char *to_uuid, const char *package_name, |
| 262 | const char *data_app_name, appid_t appid, const char* seinfo */ |
| 263 | return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 266 | static int do_mk_user_config(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 267 | { |
Robin Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 268 | return make_user_config(atoi(arg[0])); /* userid */ |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 271 | static int do_rm_user(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 272 | { |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 273 | return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 276 | static int do_movefiles(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 277 | { |
| 278 | return movefiles(); |
| 279 | } |
| 280 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 281 | static int do_linklib(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 282 | { |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 283 | return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 286 | static int do_idmap(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 287 | { |
| 288 | return idmap(arg[0], arg[1], atoi(arg[2])); |
| 289 | } |
| 290 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 291 | static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 292 | { |
| 293 | /* oat_dir, instruction_set */ |
| 294 | return create_oat_dir(arg[0], arg[1]); |
| 295 | } |
| 296 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 297 | static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 298 | { |
| 299 | /* oat_dir */ |
| 300 | return rm_package_dir(arg[0]); |
Alex Light | 43c5d30 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 303 | static int do_link_file(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 304 | { |
| 305 | /* relative_path, from_base, to_base */ |
| 306 | return link_file(arg[0], arg[1], arg[2]); |
| 307 | } |
| 308 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 309 | struct cmdinfo { |
| 310 | const char *name; |
| 311 | unsigned numargs; |
| 312 | int (*func)(char **arg, char reply[REPLY_MAX]); |
| 313 | }; |
| 314 | |
| 315 | struct cmdinfo cmds[] = { |
| 316 | { "ping", 0, do_ping }, |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 317 | |
| 318 | { "create_app_data", 6, do_create_app_data }, |
| 319 | { "restorecon_app_data", 6, do_restorecon_app_data }, |
| 320 | { "clear_app_data", 4, do_clear_app_data }, |
| 321 | { "destroy_app_data", 4, do_destroy_app_data }, |
| 322 | { "move_complete_app", 6, do_move_complete_app }, |
| 323 | { "get_app_size", 9, do_get_app_size }, |
| 324 | |
Calin Juravle | 60a794d | 2015-12-24 12:36:41 +0200 | [diff] [blame^] | 325 | { "dexopt", 9, do_dexopt }, |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 326 | { "markbootcomplete", 1, do_mark_boot_complete }, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 327 | { "rmdex", 2, do_rm_dex }, |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 328 | { "freecache", 2, do_free_cache }, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 329 | { "movefiles", 0, do_movefiles }, |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 330 | { "linklib", 4, do_linklib }, |
Robin Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 331 | { "mkuserconfig", 1, do_mk_user_config }, |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 332 | { "rmuser", 2, do_rm_user }, |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 333 | { "idmap", 3, do_idmap }, |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 334 | { "createoatdir", 2, do_create_oat_dir }, |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 335 | { "rmpackagedir", 1, do_rm_package_dir }, |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 336 | { "linkfile", 3, do_link_file }, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | static int readx(int s, void *_buf, int count) |
| 340 | { |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 341 | char *buf = (char *) _buf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 342 | int n = 0, r; |
| 343 | if (count < 0) return -1; |
| 344 | while (n < count) { |
| 345 | r = read(s, buf + n, count - n); |
| 346 | if (r < 0) { |
| 347 | if (errno == EINTR) continue; |
| 348 | ALOGE("read error: %s\n", strerror(errno)); |
| 349 | return -1; |
| 350 | } |
| 351 | if (r == 0) { |
| 352 | ALOGE("eof\n"); |
| 353 | return -1; /* EOF */ |
| 354 | } |
| 355 | n += r; |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int writex(int s, const void *_buf, int count) |
| 361 | { |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 362 | const char *buf = (const char *) _buf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 363 | int n = 0, r; |
| 364 | if (count < 0) return -1; |
| 365 | while (n < count) { |
| 366 | r = write(s, buf + n, count - n); |
| 367 | if (r < 0) { |
| 368 | if (errno == EINTR) continue; |
| 369 | ALOGE("write error: %s\n", strerror(errno)); |
| 370 | return -1; |
| 371 | } |
| 372 | n += r; |
| 373 | } |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /* Tokenize the command buffer, locate a matching command, |
| 379 | * ensure that the required number of arguments are provided, |
| 380 | * call the function(), return the result. |
| 381 | */ |
| 382 | static int execute(int s, char cmd[BUFFER_MAX]) |
| 383 | { |
| 384 | char reply[REPLY_MAX]; |
| 385 | char *arg[TOKEN_MAX+1]; |
| 386 | unsigned i; |
| 387 | unsigned n = 0; |
| 388 | unsigned short count; |
| 389 | int ret = -1; |
| 390 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 391 | // ALOGI("execute('%s')\n", cmd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 392 | |
| 393 | /* default reply is "" */ |
| 394 | reply[0] = 0; |
| 395 | |
| 396 | /* n is number of args (not counting arg[0]) */ |
| 397 | arg[0] = cmd; |
| 398 | while (*cmd) { |
| 399 | if (isspace(*cmd)) { |
| 400 | *cmd++ = 0; |
| 401 | n++; |
| 402 | arg[n] = cmd; |
| 403 | if (n == TOKEN_MAX) { |
| 404 | ALOGE("too many arguments\n"); |
| 405 | goto done; |
| 406 | } |
| 407 | } |
Serguei Katkov | 62bb385 | 2014-10-29 19:38:01 +0600 | [diff] [blame] | 408 | if (*cmd) { |
| 409 | cmd++; |
| 410 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { |
| 414 | if (!strcmp(cmds[i].name,arg[0])) { |
| 415 | if (n != cmds[i].numargs) { |
| 416 | ALOGE("%s requires %d arguments (%d given)\n", |
| 417 | cmds[i].name, cmds[i].numargs, n); |
| 418 | } else { |
| 419 | ret = cmds[i].func(arg + 1, reply); |
| 420 | } |
| 421 | goto done; |
| 422 | } |
| 423 | } |
| 424 | ALOGE("unsupported command '%s'\n", arg[0]); |
| 425 | |
| 426 | done: |
| 427 | if (reply[0]) { |
| 428 | n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); |
| 429 | } else { |
| 430 | n = snprintf(cmd, BUFFER_MAX, "%d", ret); |
| 431 | } |
| 432 | if (n > BUFFER_MAX) n = BUFFER_MAX; |
| 433 | count = n; |
| 434 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 435 | // ALOGI("reply: '%s'\n", cmd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 436 | if (writex(s, &count, sizeof(count))) return -1; |
| 437 | if (writex(s, cmd, count)) return -1; |
| 438 | return 0; |
| 439 | } |
| 440 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 441 | bool initialize_globals() { |
| 442 | const char* data_path = getenv("ANDROID_DATA"); |
| 443 | if (data_path == nullptr) { |
| 444 | ALOGE("Could not find ANDROID_DATA"); |
| 445 | return false; |
| 446 | } |
| 447 | const char* root_path = getenv("ANDROID_ROOT"); |
| 448 | if (root_path == nullptr) { |
| 449 | ALOGE("Could not find ANDROID_ROOT"); |
| 450 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 453 | return init_globals_from_data_and_root(data_path, root_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 456 | static int initialize_directories() { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 457 | int res = -1; |
| 458 | |
| 459 | // Read current filesystem layout version to handle upgrade paths |
| 460 | char version_path[PATH_MAX]; |
| 461 | snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); |
| 462 | |
| 463 | int oldVersion; |
| 464 | if (fs_read_atomic_int(version_path, &oldVersion) == -1) { |
| 465 | oldVersion = 0; |
| 466 | } |
| 467 | int version = oldVersion; |
| 468 | |
| 469 | // /data/user |
| 470 | char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); |
| 471 | // /data/data |
| 472 | char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); |
| 473 | // /data/user/0 |
| 474 | char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); |
| 475 | if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { |
| 476 | goto fail; |
| 477 | } |
| 478 | |
| 479 | // Make the /data/user directory if necessary |
| 480 | if (access(user_data_dir, R_OK) < 0) { |
| 481 | if (mkdir(user_data_dir, 0711) < 0) { |
| 482 | goto fail; |
| 483 | } |
| 484 | if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { |
| 485 | goto fail; |
| 486 | } |
| 487 | if (chmod(user_data_dir, 0711) < 0) { |
| 488 | goto fail; |
| 489 | } |
| 490 | } |
| 491 | // Make the /data/user/0 symlink to /data/data if necessary |
| 492 | if (access(primary_data_dir, R_OK) < 0) { |
| 493 | if (symlink(legacy_data_dir, primary_data_dir)) { |
| 494 | goto fail; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (version == 0) { |
| 499 | // Introducing multi-user, so migrate /data/media contents into /data/media/0 |
| 500 | ALOGD("Upgrading /data/media for multi-user"); |
| 501 | |
| 502 | // Ensure /data/media |
| 503 | if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 504 | goto fail; |
| 505 | } |
| 506 | |
| 507 | // /data/media.tmp |
| 508 | char media_tmp_dir[PATH_MAX]; |
| 509 | snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); |
| 510 | |
| 511 | // Only copy when upgrade not already in progress |
| 512 | if (access(media_tmp_dir, F_OK) == -1) { |
| 513 | if (rename(android_media_dir.path, media_tmp_dir) == -1) { |
| 514 | ALOGE("Failed to move legacy media path: %s", strerror(errno)); |
| 515 | goto fail; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // Create /data/media again |
| 520 | if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 521 | goto fail; |
| 522 | } |
| 523 | |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 524 | if (selinux_android_restorecon(android_media_dir.path, 0)) { |
Stephen Smalley | 47a3518 | 2013-12-17 16:04:20 -0500 | [diff] [blame] | 525 | goto fail; |
| 526 | } |
| 527 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 528 | // /data/media/0 |
| 529 | char owner_media_dir[PATH_MAX]; |
| 530 | snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); |
| 531 | |
| 532 | // Move any owner data into place |
| 533 | if (access(media_tmp_dir, F_OK) == 0) { |
| 534 | if (rename(media_tmp_dir, owner_media_dir) == -1) { |
| 535 | ALOGE("Failed to move owner media path: %s", strerror(errno)); |
| 536 | goto fail; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Ensure media directories for any existing users |
| 541 | DIR *dir; |
| 542 | struct dirent *dirent; |
| 543 | char user_media_dir[PATH_MAX]; |
| 544 | |
| 545 | dir = opendir(user_data_dir); |
| 546 | if (dir != NULL) { |
| 547 | while ((dirent = readdir(dir))) { |
| 548 | if (dirent->d_type == DT_DIR) { |
| 549 | const char *name = dirent->d_name; |
| 550 | |
| 551 | // skip "." and ".." |
| 552 | if (name[0] == '.') { |
| 553 | if (name[1] == 0) continue; |
| 554 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 555 | } |
| 556 | |
| 557 | // /data/media/<user_id> |
| 558 | snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); |
| 559 | if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 560 | goto fail; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | closedir(dir); |
| 565 | } |
| 566 | |
| 567 | version = 1; |
| 568 | } |
| 569 | |
| 570 | // /data/media/obb |
| 571 | char media_obb_dir[PATH_MAX]; |
| 572 | snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); |
| 573 | |
| 574 | if (version == 1) { |
| 575 | // Introducing /data/media/obb for sharing OBB across users; migrate |
| 576 | // any existing OBB files from owner. |
| 577 | ALOGD("Upgrading to shared /data/media/obb"); |
| 578 | |
| 579 | // /data/media/0/Android/obb |
| 580 | char owner_obb_path[PATH_MAX]; |
| 581 | snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); |
| 582 | |
| 583 | // Only move if target doesn't already exist |
| 584 | if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { |
| 585 | if (rename(owner_obb_path, media_obb_dir) == -1) { |
| 586 | ALOGE("Failed to move OBB from owner: %s", strerror(errno)); |
| 587 | goto fail; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | version = 2; |
| 592 | } |
| 593 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 594 | if (ensure_media_user_dirs(nullptr, 0) == -1) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 595 | ALOGE("Failed to setup media for user 0"); |
| 596 | goto fail; |
| 597 | } |
| 598 | if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { |
| 599 | goto fail; |
| 600 | } |
| 601 | |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 602 | if (ensure_config_user_dirs(0) == -1) { |
| 603 | ALOGE("Failed to setup misc for user 0"); |
| 604 | goto fail; |
| 605 | } |
| 606 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 607 | if (version == 2) { |
| 608 | ALOGD("Upgrading to /data/misc/user directories"); |
| 609 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 610 | char misc_dir[PATH_MAX]; |
| 611 | snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path); |
| 612 | |
| 613 | char keychain_added_dir[PATH_MAX]; |
| 614 | snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); |
| 615 | |
| 616 | char keychain_removed_dir[PATH_MAX]; |
| 617 | snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); |
| 618 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 619 | DIR *dir; |
| 620 | struct dirent *dirent; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 621 | dir = opendir(user_data_dir); |
| 622 | if (dir != NULL) { |
| 623 | while ((dirent = readdir(dir))) { |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 624 | const char *name = dirent->d_name; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 625 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 626 | // skip "." and ".." |
| 627 | if (name[0] == '.') { |
| 628 | if (name[1] == 0) continue; |
| 629 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 630 | } |
| 631 | |
| 632 | uint32_t user_id = atoi(name); |
| 633 | |
| 634 | // /data/misc/user/<user_id> |
| 635 | if (ensure_config_user_dirs(user_id) == -1) { |
| 636 | goto fail; |
| 637 | } |
| 638 | |
| 639 | char misc_added_dir[PATH_MAX]; |
| 640 | snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); |
| 641 | |
| 642 | char misc_removed_dir[PATH_MAX]; |
| 643 | snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); |
| 644 | |
| 645 | uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); |
| 646 | gid_t gid = uid; |
| 647 | if (access(keychain_added_dir, F_OK) == 0) { |
| 648 | if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { |
| 649 | ALOGE("Some files failed to copy"); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 650 | } |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 651 | } |
| 652 | if (access(keychain_removed_dir, F_OK) == 0) { |
| 653 | if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { |
| 654 | ALOGE("Some files failed to copy"); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
| 658 | closedir(dir); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 659 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 660 | if (access(keychain_added_dir, F_OK) == 0) { |
| 661 | delete_dir_contents(keychain_added_dir, 1, 0); |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 662 | } |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 663 | if (access(keychain_removed_dir, F_OK) == 0) { |
| 664 | delete_dir_contents(keychain_removed_dir, 1, 0); |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | version = 3; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 671 | // Persist layout version if changed |
| 672 | if (version != oldVersion) { |
| 673 | if (fs_write_atomic_int(version_path, version) == -1) { |
| 674 | ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); |
| 675 | goto fail; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // Success! |
| 680 | res = 0; |
| 681 | |
| 682 | fail: |
| 683 | free(user_data_dir); |
| 684 | free(legacy_data_dir); |
| 685 | free(primary_data_dir); |
| 686 | return res; |
| 687 | } |
| 688 | |
Stephen Smalley | 7abb52b | 2014-03-26 09:30:37 -0400 | [diff] [blame] | 689 | static int log_callback(int type, const char *fmt, ...) { |
| 690 | va_list ap; |
| 691 | int priority; |
| 692 | |
| 693 | switch (type) { |
| 694 | case SELINUX_WARNING: |
| 695 | priority = ANDROID_LOG_WARN; |
| 696 | break; |
| 697 | case SELINUX_INFO: |
| 698 | priority = ANDROID_LOG_INFO; |
| 699 | break; |
| 700 | default: |
| 701 | priority = ANDROID_LOG_ERROR; |
| 702 | break; |
| 703 | } |
| 704 | va_start(ap, fmt); |
| 705 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 706 | va_end(ap); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 710 | static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 711 | char buf[BUFFER_MAX]; |
| 712 | struct sockaddr addr; |
| 713 | socklen_t alen; |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 714 | int lsocket, s; |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 715 | int selinux_enabled = (is_selinux_enabled() > 0); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 716 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 717 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 718 | android::base::InitLogging(argv); |
| 719 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 720 | ALOGI("installd firing up\n"); |
| 721 | |
Stephen Smalley | 7abb52b | 2014-03-26 09:30:37 -0400 | [diff] [blame] | 722 | union selinux_callback cb; |
| 723 | cb.func_log = log_callback; |
| 724 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 725 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 726 | if (!initialize_globals()) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 727 | ALOGE("Could not initialize globals; exiting.\n"); |
| 728 | exit(1); |
| 729 | } |
| 730 | |
| 731 | if (initialize_directories() < 0) { |
| 732 | ALOGE("Could not create directories; exiting.\n"); |
| 733 | exit(1); |
| 734 | } |
| 735 | |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 736 | if (selinux_enabled && selinux_status_open(true) < 0) { |
| 737 | ALOGE("Could not open selinux status; exiting.\n"); |
| 738 | exit(1); |
| 739 | } |
| 740 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 741 | lsocket = android_get_control_socket(SOCKET_PATH); |
| 742 | if (lsocket < 0) { |
| 743 | ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); |
| 744 | exit(1); |
| 745 | } |
| 746 | if (listen(lsocket, 5)) { |
| 747 | ALOGE("Listen on socket failed: %s\n", strerror(errno)); |
| 748 | exit(1); |
| 749 | } |
| 750 | fcntl(lsocket, F_SETFD, FD_CLOEXEC); |
| 751 | |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 752 | // Perform all filesystem access as system so that FBE emulation mode |
| 753 | // can block access using chmod 000. |
| 754 | #if DEBUG_FBE |
| 755 | setfsuid(AID_SYSTEM); |
| 756 | #endif |
| 757 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 758 | for (;;) { |
| 759 | alen = sizeof(addr); |
| 760 | s = accept(lsocket, &addr, &alen); |
| 761 | if (s < 0) { |
| 762 | ALOGE("Accept failed: %s\n", strerror(errno)); |
| 763 | continue; |
| 764 | } |
| 765 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 766 | |
| 767 | ALOGI("new connection\n"); |
| 768 | for (;;) { |
| 769 | unsigned short count; |
| 770 | if (readx(s, &count, sizeof(count))) { |
| 771 | ALOGE("failed to read size\n"); |
| 772 | break; |
| 773 | } |
| 774 | if ((count < 1) || (count >= BUFFER_MAX)) { |
| 775 | ALOGE("invalid size %d\n", count); |
| 776 | break; |
| 777 | } |
| 778 | if (readx(s, buf, count)) { |
| 779 | ALOGE("failed to read command\n"); |
| 780 | break; |
| 781 | } |
| 782 | buf[count] = 0; |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 783 | if (selinux_enabled && selinux_status_updated() > 0) { |
| 784 | selinux_android_seapp_context_reload(); |
| 785 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 786 | if (execute(s, buf)) break; |
| 787 | } |
| 788 | ALOGI("closing connection\n"); |
| 789 | close(s); |
| 790 | } |
| 791 | |
| 792 | return 0; |
| 793 | } |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 794 | |
| 795 | } // namespace installd |
| 796 | } // namespace android |
| 797 | |
| 798 | int main(const int argc, char *argv[]) { |
| 799 | return android::installd::installd_main(argc, argv); |
| 800 | } |