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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */ |
| 61 | { |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 62 | 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] | 63 | } |
| 64 | |
| 65 | static int do_rm_cache(char **arg, char reply[REPLY_MAX]) |
| 66 | { |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 67 | return delete_cache(arg[0]); /* pkgname */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int do_protect(char **arg, char reply[REPLY_MAX]) |
| 71 | { |
| 72 | return protect(arg[0], atoi(arg[1])); /* pkgname, gid */ |
| 73 | } |
| 74 | |
| 75 | static int do_get_size(char **arg, char reply[REPLY_MAX]) |
| 76 | { |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 77 | int64_t codesize = 0; |
| 78 | int64_t datasize = 0; |
| 79 | int64_t cachesize = 0; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 80 | int64_t asecsize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | int res = 0; |
| 82 | |
| 83 | /* pkgdir, apkpath */ |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 84 | res = get_size(arg[0], arg[1], arg[2], arg[3], &codesize, &datasize, &cachesize, &asecsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 86 | /* |
| 87 | * Each int64_t can take up 22 characters printed out. Make sure it |
| 88 | * doesn't go over REPLY_MAX in the future. |
| 89 | */ |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 90 | snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, |
| 91 | codesize, datasize, cachesize, asecsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | return res; |
| 93 | } |
| 94 | |
| 95 | static int do_rm_user_data(char **arg, char reply[REPLY_MAX]) |
| 96 | { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 97 | return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ |
| 98 | } |
| 99 | |
| 100 | static int do_mk_user_data(char **arg, char reply[REPLY_MAX]) |
| 101 | { |
| 102 | return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */ |
| 103 | } |
| 104 | |
| 105 | static int do_rm_user(char **arg, char reply[REPLY_MAX]) |
| 106 | { |
| 107 | return delete_persona(atoi(arg[0])); /* userid */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 110 | static int do_clone_user_data(char **arg, char reply[REPLY_MAX]) |
| 111 | { |
| 112 | return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2])); |
| 113 | } |
| 114 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 115 | static int do_movefiles(char **arg, char reply[REPLY_MAX]) |
| 116 | { |
| 117 | return movefiles(); |
| 118 | } |
| 119 | |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 120 | static int do_linklib(char **arg, char reply[REPLY_MAX]) |
| 121 | { |
| 122 | return linklib(arg[0], arg[1]); |
| 123 | } |
| 124 | |
| 125 | static int do_unlinklib(char **arg, char reply[REPLY_MAX]) |
| 126 | { |
| 127 | return unlinklib(arg[0]); |
| 128 | } |
| 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | struct cmdinfo { |
| 131 | const char *name; |
| 132 | unsigned numargs; |
| 133 | int (*func)(char **arg, char reply[REPLY_MAX]); |
| 134 | }; |
| 135 | |
| 136 | struct cmdinfo cmds[] = { |
| 137 | { "ping", 0, do_ping }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 138 | { "install", 3, do_install }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | { "dexopt", 3, do_dexopt }, |
| 140 | { "movedex", 2, do_move_dex }, |
| 141 | { "rmdex", 1, do_rm_dex }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 142 | { "remove", 2, do_remove }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 143 | { "rename", 2, do_rename }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | { "freecache", 1, do_free_cache }, |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 145 | { "rmcache", 1, do_rm_cache }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | { "protect", 2, do_protect }, |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 147 | { "getsize", 4, do_get_size }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 148 | { "rmuserdata", 2, do_rm_user_data }, |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 149 | { "movefiles", 0, do_movefiles }, |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 150 | { "linklib", 2, do_linklib }, |
| 151 | { "unlinklib", 1, do_unlinklib }, |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 152 | { "mkuserdata", 3, do_mk_user_data }, |
| 153 | { "rmuser", 1, do_rm_user }, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 154 | { "cloneuserdata", 3, do_clone_user_data }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static int readx(int s, void *_buf, int count) |
| 158 | { |
| 159 | char *buf = _buf; |
| 160 | int n = 0, r; |
| 161 | if (count < 0) return -1; |
| 162 | while (n < count) { |
| 163 | r = read(s, buf + n, count - n); |
| 164 | if (r < 0) { |
| 165 | if (errno == EINTR) continue; |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 166 | ALOGE("read error: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | if (r == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 170 | ALOGE("eof\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | return -1; /* EOF */ |
| 172 | } |
| 173 | n += r; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int writex(int s, const void *_buf, int count) |
| 179 | { |
| 180 | const char *buf = _buf; |
| 181 | int n = 0, r; |
| 182 | if (count < 0) return -1; |
| 183 | while (n < count) { |
| 184 | r = write(s, buf + n, count - n); |
| 185 | if (r < 0) { |
| 186 | if (errno == EINTR) continue; |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 187 | ALOGE("write error: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | n += r; |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* Tokenize the command buffer, locate a matching command, |
| 197 | * ensure that the required number of arguments are provided, |
| 198 | * call the function(), return the result. |
| 199 | */ |
| 200 | static int execute(int s, char cmd[BUFFER_MAX]) |
| 201 | { |
| 202 | char reply[REPLY_MAX]; |
| 203 | char *arg[TOKEN_MAX+1]; |
| 204 | unsigned i; |
| 205 | unsigned n = 0; |
| 206 | unsigned short count; |
| 207 | int ret = -1; |
| 208 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 209 | // ALOGI("execute('%s')\n", cmd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
| 211 | /* default reply is "" */ |
| 212 | reply[0] = 0; |
| 213 | |
| 214 | /* n is number of args (not counting arg[0]) */ |
| 215 | arg[0] = cmd; |
| 216 | while (*cmd) { |
| 217 | if (isspace(*cmd)) { |
| 218 | *cmd++ = 0; |
| 219 | n++; |
| 220 | arg[n] = cmd; |
| 221 | if (n == TOKEN_MAX) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 222 | ALOGE("too many arguments\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | goto done; |
| 224 | } |
| 225 | } |
| 226 | cmd++; |
| 227 | } |
| 228 | |
| 229 | for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { |
| 230 | if (!strcmp(cmds[i].name,arg[0])) { |
| 231 | if (n != cmds[i].numargs) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 232 | ALOGE("%s requires %d arguments (%d given)\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | cmds[i].name, cmds[i].numargs, n); |
| 234 | } else { |
| 235 | ret = cmds[i].func(arg + 1, reply); |
| 236 | } |
| 237 | goto done; |
| 238 | } |
| 239 | } |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 240 | ALOGE("unsupported command '%s'\n", arg[0]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
| 242 | done: |
| 243 | if (reply[0]) { |
| 244 | n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); |
| 245 | } else { |
| 246 | n = snprintf(cmd, BUFFER_MAX, "%d", ret); |
| 247 | } |
| 248 | if (n > BUFFER_MAX) n = BUFFER_MAX; |
| 249 | count = n; |
| 250 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 251 | // ALOGI("reply: '%s'\n", cmd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | if (writex(s, &count, sizeof(count))) return -1; |
| 253 | if (writex(s, cmd, count)) return -1; |
| 254 | return 0; |
| 255 | } |
| 256 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Initialize all the global variables that are used elsewhere. Returns 0 upon |
| 259 | * success and -1 on error. |
| 260 | */ |
| 261 | void free_globals() { |
| 262 | size_t i; |
| 263 | |
| 264 | for (i = 0; i < android_system_dirs.count; i++) { |
| 265 | if (android_system_dirs.dirs[i].path != NULL) { |
| 266 | free(android_system_dirs.dirs[i].path); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | free(android_system_dirs.dirs); |
| 271 | } |
| 272 | |
| 273 | int initialize_globals() { |
| 274 | // Get the android data directory. |
| 275 | if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | // Get the android app directory. |
| 280 | if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | // Get the android protected app directory. |
| 285 | if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | // Get the sd-card ASEC mount point. |
| 290 | if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | // Take note of the system and vendor directories. |
| 295 | android_system_dirs.count = 2; |
| 296 | |
| 297 | android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t)); |
| 298 | if (android_system_dirs.dirs == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 299 | ALOGE("Couldn't allocate array for dirs; aborting\n"); |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | // system |
| 304 | if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) { |
| 305 | free_globals(); |
| 306 | return -1; |
| 307 | } |
| 308 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 309 | // append "app/" to dirs[0] |
| 310 | char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR); |
| 311 | android_system_dirs.dirs[0].path = system_app_path; |
| 312 | android_system_dirs.dirs[0].len = strlen(system_app_path); |
| 313 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 314 | // vendor |
| 315 | // TODO replace this with an environment variable (doesn't exist yet) |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 316 | android_system_dirs.dirs[1].path = "/vendor/app/"; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 317 | android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 322 | int initialize_directories() { |
| 323 | // /data/user |
| 324 | char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); |
| 325 | // /data/data |
| 326 | char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); |
| 327 | // /data/user/0 |
| 328 | char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, |
| 329 | "0"); |
| 330 | int ret = -1; |
| 331 | if (user_data_dir != NULL && primary_data_dir != NULL && legacy_data_dir != NULL) { |
| 332 | ret = 0; |
| 333 | // Make the /data/user directory if necessary |
| 334 | if (access(user_data_dir, R_OK) < 0) { |
Nick Kralevich | 7ac3ed1 | 2012-03-23 17:52:33 -0700 | [diff] [blame] | 335 | if (mkdir(user_data_dir, 0711) < 0) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { |
| 339 | return -1; |
| 340 | } |
Nick Kralevich | 7ac3ed1 | 2012-03-23 17:52:33 -0700 | [diff] [blame] | 341 | if (chmod(user_data_dir, 0711) < 0) { |
| 342 | return -1; |
| 343 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 344 | } |
| 345 | // Make the /data/user/0 symlink to /data/data if necessary |
| 346 | if (access(primary_data_dir, R_OK) < 0) { |
| 347 | ret = symlink(legacy_data_dir, primary_data_dir); |
| 348 | } |
| 349 | free(user_data_dir); |
| 350 | free(legacy_data_dir); |
| 351 | free(primary_data_dir); |
| 352 | } |
| 353 | return ret; |
| 354 | } |
| 355 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 356 | int main(const int argc, const char *argv[]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | char buf[BUFFER_MAX]; |
| 358 | struct sockaddr addr; |
| 359 | socklen_t alen; |
| 360 | int lsocket, s, count; |
| 361 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 362 | if (initialize_globals() < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 363 | ALOGE("Could not initialize globals; exiting.\n"); |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 364 | exit(1); |
| 365 | } |
| 366 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 367 | if (initialize_directories() < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 368 | ALOGE("Could not create directories; exiting.\n"); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 369 | exit(1); |
| 370 | } |
| 371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | lsocket = android_get_control_socket(SOCKET_PATH); |
| 373 | if (lsocket < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 374 | 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] | 375 | exit(1); |
| 376 | } |
| 377 | if (listen(lsocket, 5)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 378 | ALOGE("Listen on socket failed: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | exit(1); |
| 380 | } |
| 381 | fcntl(lsocket, F_SETFD, FD_CLOEXEC); |
| 382 | |
| 383 | for (;;) { |
| 384 | alen = sizeof(addr); |
| 385 | s = accept(lsocket, &addr, &alen); |
| 386 | if (s < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 387 | ALOGE("Accept failed: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 391 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 392 | ALOGI("new connection\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | for (;;) { |
| 394 | unsigned short count; |
| 395 | if (readx(s, &count, sizeof(count))) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 396 | ALOGE("failed to read size\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | if ((count < 1) || (count >= BUFFER_MAX)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 400 | ALOGE("invalid size %d\n", count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | break; |
| 402 | } |
| 403 | if (readx(s, buf, count)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 404 | ALOGE("failed to read command\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | break; |
| 406 | } |
| 407 | buf[count] = 0; |
| 408 | if (execute(s, buf)) break; |
| 409 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 410 | ALOGI("closing connection\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | close(s); |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |