services.c revision e3aeeb4de34dbb93e832e6554f494122ba633f3b
1/* 2 * Copyright (C) 2007 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 <stdlib.h> 18#include <stdio.h> 19#include <unistd.h> 20#include <string.h> 21#include <errno.h> 22 23#include "sysdeps.h" 24 25#define TRACE_TAG TRACE_ADB 26#include "adb.h" 27#include "file_sync_service.h" 28 29#if ADB_HOST 30# ifndef HAVE_WINSOCK 31# include <netinet/in.h> 32# include <netdb.h> 33# endif 34#else 35# include <cutils/android_reboot.h> 36#endif 37 38typedef struct stinfo stinfo; 39 40struct stinfo { 41 void (*func)(int fd, void *cookie); 42 int fd; 43 void *cookie; 44}; 45 46 47void *service_bootstrap_func(void *x) 48{ 49 stinfo *sti = x; 50 sti->func(sti->fd, sti->cookie); 51 free(sti); 52 return 0; 53} 54 55#if ADB_HOST 56ADB_MUTEX_DEFINE( dns_lock ); 57 58static void dns_service(int fd, void *cookie) 59{ 60 char *hostname = cookie; 61 struct hostent *hp; 62 unsigned zero = 0; 63 64 adb_mutex_lock(&dns_lock); 65 hp = gethostbyname(hostname); 66 free(cookie); 67 if(hp == 0) { 68 writex(fd, &zero, 4); 69 } else { 70 writex(fd, hp->h_addr, 4); 71 } 72 adb_mutex_unlock(&dns_lock); 73 adb_close(fd); 74} 75#else 76extern int recovery_mode; 77 78static void recover_service(int s, void *cookie) 79{ 80 unsigned char buf[4096]; 81 unsigned count = (unsigned) cookie; 82 int fd; 83 84 fd = adb_creat("/tmp/update", 0644); 85 if(fd < 0) { 86 adb_close(s); 87 return; 88 } 89 90 while(count > 0) { 91 unsigned xfer = (count > 4096) ? 4096 : count; 92 if(readx(s, buf, xfer)) break; 93 if(writex(fd, buf, xfer)) break; 94 count -= xfer; 95 } 96 97 if(count == 0) { 98 writex(s, "OKAY", 4); 99 } else { 100 writex(s, "FAIL", 4); 101 } 102 adb_close(fd); 103 adb_close(s); 104 105 fd = adb_creat("/tmp/update.begin", 0644); 106 adb_close(fd); 107} 108 109void restart_root_service(int fd, void *cookie) 110{ 111 char buf[100]; 112 char value[PROPERTY_VALUE_MAX]; 113 114 if (getuid() == 0) { 115 snprintf(buf, sizeof(buf), "adbd is already running as root\n"); 116 writex(fd, buf, strlen(buf)); 117 adb_close(fd); 118 } else { 119 property_get("ro.debuggable", value, ""); 120 if (strcmp(value, "1") != 0) { 121 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n"); 122 writex(fd, buf, strlen(buf)); 123 adb_close(fd); 124 return; 125 } 126 127 property_set("service.adb.root", "1"); 128 snprintf(buf, sizeof(buf), "restarting adbd as root\n"); 129 writex(fd, buf, strlen(buf)); 130 adb_close(fd); 131 132 // quit, and init will restart us as root 133 sleep(1); 134 exit(1); 135 } 136} 137 138void restart_tcp_service(int fd, void *cookie) 139{ 140 char buf[100]; 141 char value[PROPERTY_VALUE_MAX]; 142 int port = (int)cookie; 143 144 if (port <= 0) { 145 snprintf(buf, sizeof(buf), "invalid port\n"); 146 writex(fd, buf, strlen(buf)); 147 adb_close(fd); 148 return; 149 } 150 151 snprintf(value, sizeof(value), "%d", port); 152 property_set("service.adb.tcp.port", value); 153 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port); 154 writex(fd, buf, strlen(buf)); 155 adb_close(fd); 156 157 // quit, and init will restart us in TCP mode 158 sleep(1); 159 exit(1); 160} 161 162void restart_usb_service(int fd, void *cookie) 163{ 164 char buf[100]; 165 166 property_set("service.adb.tcp.port", "0"); 167 snprintf(buf, sizeof(buf), "restarting in USB mode\n"); 168 writex(fd, buf, strlen(buf)); 169 adb_close(fd); 170 171 // quit, and init will restart us in USB mode 172 sleep(1); 173 exit(1); 174} 175 176void reboot_service(int fd, void *arg) 177{ 178 char buf[100]; 179 int pid, ret; 180 181 sync(); 182 183 /* Attempt to unmount the SD card first. 184 * No need to bother checking for errors. 185 */ 186 pid = fork(); 187 if (pid == 0) { 188 /* ask vdc to unmount it */ 189 execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount", 190 getenv("EXTERNAL_STORAGE"), "force", NULL); 191 } else if (pid > 0) { 192 /* wait until vdc succeeds or fails */ 193 waitpid(pid, &ret, 0); 194 } 195 196 ret = android_reboot(ANDROID_RB_RESTART2, 0, (char *) arg); 197 if (ret < 0) { 198 snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno)); 199 writex(fd, buf, strlen(buf)); 200 } 201 free(arg); 202 adb_close(fd); 203} 204 205#endif 206 207#if 0 208static void echo_service(int fd, void *cookie) 209{ 210 char buf[4096]; 211 int r; 212 char *p; 213 int c; 214 215 for(;;) { 216 r = read(fd, buf, 4096); 217 if(r == 0) goto done; 218 if(r < 0) { 219 if(errno == EINTR) continue; 220 else goto done; 221 } 222 223 c = r; 224 p = buf; 225 while(c > 0) { 226 r = write(fd, p, c); 227 if(r > 0) { 228 c -= r; 229 p += r; 230 continue; 231 } 232 if((r < 0) && (errno == EINTR)) continue; 233 goto done; 234 } 235 } 236done: 237 close(fd); 238} 239#endif 240 241static int create_service_thread(void (*func)(int, void *), void *cookie) 242{ 243 stinfo *sti; 244 adb_thread_t t; 245 int s[2]; 246 247 if(adb_socketpair(s)) { 248 printf("cannot create service socket pair\n"); 249 return -1; 250 } 251 252 sti = malloc(sizeof(stinfo)); 253 if(sti == 0) fatal("cannot allocate stinfo"); 254 sti->func = func; 255 sti->cookie = cookie; 256 sti->fd = s[1]; 257 258 if(adb_thread_create( &t, service_bootstrap_func, sti)){ 259 free(sti); 260 adb_close(s[0]); 261 adb_close(s[1]); 262 printf("cannot create service thread\n"); 263 return -1; 264 } 265 266 D("service thread started, %d:%d\n",s[0], s[1]); 267 return s[0]; 268} 269 270static int create_subprocess(const char *cmd, const char *arg0, const char *arg1) 271{ 272#ifdef HAVE_WIN32_PROC 273 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1); 274 return -1; 275#else /* !HAVE_WIN32_PROC */ 276 char *devname; 277 int ptm; 278 pid_t pid; 279 280 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY); 281 if(ptm < 0){ 282 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno)); 283 return -1; 284 } 285 fcntl(ptm, F_SETFD, FD_CLOEXEC); 286 287 if(grantpt(ptm) || unlockpt(ptm) || 288 ((devname = (char*) ptsname(ptm)) == 0)){ 289 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno)); 290 return -1; 291 } 292 293 pid = fork(); 294 if(pid < 0) { 295 printf("- fork failed: %s -\n", strerror(errno)); 296 return -1; 297 } 298 299 if(pid == 0){ 300 int pts; 301 302 setsid(); 303 304 pts = unix_open(devname, O_RDWR); 305 if(pts < 0) exit(-1); 306 307 dup2(pts, 0); 308 dup2(pts, 1); 309 dup2(pts, 2); 310 311 adb_close(pts); 312 adb_close(ptm); 313 314 execl(cmd, cmd, arg0, arg1, NULL); 315 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n", 316 cmd, strerror(errno), errno); 317 exit(-1); 318 } else { 319#if !ADB_HOST 320 // set child's OOM adjustment to zero 321 char text[64]; 322 snprintf(text, sizeof text, "/proc/%d/oom_adj", pid); 323 int fd = adb_open(text, O_WRONLY); 324 if (fd >= 0) { 325 adb_write(fd, "0", 1); 326 adb_close(fd); 327 } else { 328 D("adb: unable to open %s\n", text); 329 } 330#endif 331 return ptm; 332 } 333#endif /* !HAVE_WIN32_PROC */ 334} 335 336#if ADB_HOST 337#define SHELL_COMMAND "/bin/sh" 338#else 339#define SHELL_COMMAND "/system/bin/sh" 340#endif 341 342int service_to_fd(const char *name) 343{ 344 int ret = -1; 345 346 if(!strncmp(name, "tcp:", 4)) { 347 int port = atoi(name + 4); 348 name = strchr(name + 4, ':'); 349 if(name == 0) { 350 ret = socket_loopback_client(port, SOCK_STREAM); 351 if (ret >= 0) 352 disable_tcp_nagle(ret); 353 } else { 354#if ADB_HOST 355 adb_mutex_lock(&dns_lock); 356 ret = socket_network_client(name + 1, port, SOCK_STREAM); 357 adb_mutex_unlock(&dns_lock); 358#else 359 return -1; 360#endif 361 } 362#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */ 363 } else if(!strncmp(name, "local:", 6)) { 364 ret = socket_local_client(name + 6, 365 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); 366 } else if(!strncmp(name, "localreserved:", 14)) { 367 ret = socket_local_client(name + 14, 368 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); 369 } else if(!strncmp(name, "localabstract:", 14)) { 370 ret = socket_local_client(name + 14, 371 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); 372 } else if(!strncmp(name, "localfilesystem:", 16)) { 373 ret = socket_local_client(name + 16, 374 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); 375#endif 376#if ADB_HOST 377 } else if(!strncmp("dns:", name, 4)){ 378 char *n = strdup(name + 4); 379 if(n == 0) return -1; 380 ret = create_service_thread(dns_service, n); 381#else /* !ADB_HOST */ 382 } else if(!strncmp("dev:", name, 4)) { 383 ret = unix_open(name + 4, O_RDWR); 384 } else if(!strncmp(name, "framebuffer:", 12)) { 385 ret = create_service_thread(framebuffer_service, 0); 386 } else if(recovery_mode && !strncmp(name, "recover:", 8)) { 387 ret = create_service_thread(recover_service, (void*) atoi(name + 8)); 388 } else if (!strncmp(name, "jdwp:", 5)) { 389 ret = create_jdwp_connection_fd(atoi(name+5)); 390 } else if (!strncmp(name, "log:", 4)) { 391 ret = create_service_thread(log_service, get_log_file_path(name + 4)); 392#endif 393 } else if(!HOST && !strncmp(name, "shell:", 6)) { 394 if(name[6]) { 395 ret = create_subprocess(SHELL_COMMAND, "-c", name + 6); 396 } else { 397 ret = create_subprocess(SHELL_COMMAND, "-", 0); 398 } 399#if !ADB_HOST 400 } else if(!strncmp(name, "sync:", 5)) { 401 ret = create_service_thread(file_sync_service, NULL); 402 } else if(!strncmp(name, "remount:", 8)) { 403 ret = create_service_thread(remount_service, NULL); 404 } else if(!strncmp(name, "reboot:", 7)) { 405 void* arg = strdup(name + 7); 406 if(arg == 0) return -1; 407 ret = create_service_thread(reboot_service, arg); 408 } else if(!strncmp(name, "root:", 5)) { 409 ret = create_service_thread(restart_root_service, NULL); 410 } else if(!strncmp(name, "tcpip:", 6)) { 411 int port; 412 if (sscanf(name + 6, "%d", &port) == 0) { 413 port = 0; 414 } 415 ret = create_service_thread(restart_tcp_service, (void *)port); 416 } else if(!strncmp(name, "usb:", 4)) { 417 ret = create_service_thread(restart_usb_service, NULL); 418#endif 419#if 0 420 } else if(!strncmp(name, "echo:", 5)){ 421 ret = create_service_thread(echo_service, 0); 422#endif 423 } 424 if (ret >= 0) { 425 close_on_exec(ret); 426 } 427 return ret; 428} 429 430#if ADB_HOST 431struct state_info { 432 transport_type transport; 433 char* serial; 434 int state; 435}; 436 437static void wait_for_state(int fd, void* cookie) 438{ 439 struct state_info* sinfo = cookie; 440 char* err = "unknown error"; 441 442 D("wait_for_state %d\n", sinfo->state); 443 444 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); 445 if(t != 0) { 446 writex(fd, "OKAY", 4); 447 } else { 448 sendfailmsg(fd, err); 449 } 450 451 if (sinfo->serial) 452 free(sinfo->serial); 453 free(sinfo); 454 adb_close(fd); 455 D("wait_for_state is done\n"); 456} 457#endif 458 459#if ADB_HOST 460asocket* host_service_to_socket(const char* name, const char *serial) 461{ 462 if (!strcmp(name,"track-devices")) { 463 return create_device_tracker(); 464 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { 465 struct state_info* sinfo = malloc(sizeof(struct state_info)); 466 467 if (serial) 468 sinfo->serial = strdup(serial); 469 else 470 sinfo->serial = NULL; 471 472 name += strlen("wait-for-"); 473 474 if (!strncmp(name, "local", strlen("local"))) { 475 sinfo->transport = kTransportLocal; 476 sinfo->state = CS_DEVICE; 477 } else if (!strncmp(name, "usb", strlen("usb"))) { 478 sinfo->transport = kTransportUsb; 479 sinfo->state = CS_DEVICE; 480 } else if (!strncmp(name, "any", strlen("any"))) { 481 sinfo->transport = kTransportAny; 482 sinfo->state = CS_DEVICE; 483 } else { 484 free(sinfo); 485 return NULL; 486 } 487 488 int fd = create_service_thread(wait_for_state, sinfo); 489 return create_local_socket(fd); 490 } 491 return NULL; 492} 493#endif /* ADB_HOST */ 494