The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 25 | #define TRACE_TAG TRACE_SERVICES |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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> |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 33 | # include <sys/ioctl.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | # endif |
Mike Lockwood | fa311c3 | 2009-07-30 16:23:56 -0700 | [diff] [blame] | 35 | #else |
Ken Sumrall | 78438c9 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 36 | # include <cutils/android_reboot.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | typedef struct stinfo stinfo; |
| 40 | |
| 41 | struct stinfo { |
| 42 | void (*func)(int fd, void *cookie); |
| 43 | int fd; |
| 44 | void *cookie; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | void *service_bootstrap_func(void *x) |
| 49 | { |
| 50 | stinfo *sti = x; |
| 51 | sti->func(sti->fd, sti->cookie); |
| 52 | free(sti); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | #if ADB_HOST |
| 57 | ADB_MUTEX_DEFINE( dns_lock ); |
| 58 | |
| 59 | static void dns_service(int fd, void *cookie) |
| 60 | { |
| 61 | char *hostname = cookie; |
| 62 | struct hostent *hp; |
| 63 | unsigned zero = 0; |
| 64 | |
| 65 | adb_mutex_lock(&dns_lock); |
| 66 | hp = gethostbyname(hostname); |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 67 | free(cookie); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | if(hp == 0) { |
| 69 | writex(fd, &zero, 4); |
| 70 | } else { |
| 71 | writex(fd, hp->h_addr, 4); |
| 72 | } |
| 73 | adb_mutex_unlock(&dns_lock); |
| 74 | adb_close(fd); |
| 75 | } |
| 76 | #else |
| 77 | extern int recovery_mode; |
| 78 | |
| 79 | static void recover_service(int s, void *cookie) |
| 80 | { |
| 81 | unsigned char buf[4096]; |
| 82 | unsigned count = (unsigned) cookie; |
| 83 | int fd; |
| 84 | |
| 85 | fd = adb_creat("/tmp/update", 0644); |
| 86 | if(fd < 0) { |
| 87 | adb_close(s); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | while(count > 0) { |
| 92 | unsigned xfer = (count > 4096) ? 4096 : count; |
| 93 | if(readx(s, buf, xfer)) break; |
| 94 | if(writex(fd, buf, xfer)) break; |
| 95 | count -= xfer; |
| 96 | } |
| 97 | |
| 98 | if(count == 0) { |
| 99 | writex(s, "OKAY", 4); |
| 100 | } else { |
| 101 | writex(s, "FAIL", 4); |
| 102 | } |
| 103 | adb_close(fd); |
| 104 | adb_close(s); |
| 105 | |
| 106 | fd = adb_creat("/tmp/update.begin", 0644); |
| 107 | adb_close(fd); |
| 108 | } |
| 109 | |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 110 | void restart_root_service(int fd, void *cookie) |
| 111 | { |
| 112 | char buf[100]; |
| 113 | char value[PROPERTY_VALUE_MAX]; |
| 114 | |
| 115 | if (getuid() == 0) { |
| 116 | snprintf(buf, sizeof(buf), "adbd is already running as root\n"); |
| 117 | writex(fd, buf, strlen(buf)); |
| 118 | adb_close(fd); |
| 119 | } else { |
| 120 | property_get("ro.debuggable", value, ""); |
| 121 | if (strcmp(value, "1") != 0) { |
| 122 | snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n"); |
| 123 | writex(fd, buf, strlen(buf)); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 124 | adb_close(fd); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
Benoit Goby | ad926c1 | 2012-03-16 14:44:17 -0700 | [diff] [blame] | 128 | property_set("service.adb.root", "1"); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 129 | snprintf(buf, sizeof(buf), "restarting adbd as root\n"); |
| 130 | writex(fd, buf, strlen(buf)); |
| 131 | adb_close(fd); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 135 | void restart_tcp_service(int fd, void *cookie) |
| 136 | { |
| 137 | char buf[100]; |
| 138 | char value[PROPERTY_VALUE_MAX]; |
| 139 | int port = (int)cookie; |
| 140 | |
| 141 | if (port <= 0) { |
| 142 | snprintf(buf, sizeof(buf), "invalid port\n"); |
| 143 | writex(fd, buf, strlen(buf)); |
| 144 | adb_close(fd); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | snprintf(value, sizeof(value), "%d", port); |
| 149 | property_set("service.adb.tcp.port", value); |
| 150 | snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port); |
| 151 | writex(fd, buf, strlen(buf)); |
| 152 | adb_close(fd); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void restart_usb_service(int fd, void *cookie) |
| 156 | { |
| 157 | char buf[100]; |
| 158 | |
| 159 | property_set("service.adb.tcp.port", "0"); |
| 160 | snprintf(buf, sizeof(buf), "restarting in USB mode\n"); |
| 161 | writex(fd, buf, strlen(buf)); |
| 162 | adb_close(fd); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void reboot_service(int fd, void *arg) |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 166 | { |
| 167 | char buf[100]; |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame^] | 168 | char property_val[PROPERTY_VALUE_MAX]; |
Mike Lockwood | d49e9eb | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 169 | int pid, ret; |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 170 | |
| 171 | sync(); |
Mike Lockwood | d49e9eb | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 172 | |
| 173 | /* Attempt to unmount the SD card first. |
| 174 | * No need to bother checking for errors. |
| 175 | */ |
| 176 | pid = fork(); |
| 177 | if (pid == 0) { |
| 178 | /* ask vdc to unmount it */ |
| 179 | execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount", |
| 180 | getenv("EXTERNAL_STORAGE"), "force", NULL); |
| 181 | } else if (pid > 0) { |
| 182 | /* wait until vdc succeeds or fails */ |
| 183 | waitpid(pid, &ret, 0); |
| 184 | } |
| 185 | |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame^] | 186 | ret = snprintf(property_val, sizeof(property_val), "reboot,%s", (char *) arg); |
| 187 | if (ret >= (int) sizeof(property_val)) { |
| 188 | snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret); |
| 189 | writex(fd, buf, strlen(buf)); |
| 190 | goto cleanup; |
| 191 | } |
| 192 | |
| 193 | ret = property_set(ANDROID_RB_PROPERTY, property_val); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 194 | if (ret < 0) { |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame^] | 195 | snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 196 | writex(fd, buf, strlen(buf)); |
| 197 | } |
Nick Kralevich | b7df017 | 2013-04-18 12:20:02 -0700 | [diff] [blame^] | 198 | cleanup: |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 199 | free(arg); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 200 | adb_close(fd); |
| 201 | } |
| 202 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 203 | #endif |
| 204 | |
| 205 | #if 0 |
| 206 | static void echo_service(int fd, void *cookie) |
| 207 | { |
| 208 | char buf[4096]; |
| 209 | int r; |
| 210 | char *p; |
| 211 | int c; |
| 212 | |
| 213 | for(;;) { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 214 | r = adb_read(fd, buf, 4096); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | if(r == 0) goto done; |
| 216 | if(r < 0) { |
| 217 | if(errno == EINTR) continue; |
| 218 | else goto done; |
| 219 | } |
| 220 | |
| 221 | c = r; |
| 222 | p = buf; |
| 223 | while(c > 0) { |
| 224 | r = write(fd, p, c); |
| 225 | if(r > 0) { |
| 226 | c -= r; |
| 227 | p += r; |
| 228 | continue; |
| 229 | } |
| 230 | if((r < 0) && (errno == EINTR)) continue; |
| 231 | goto done; |
| 232 | } |
| 233 | } |
| 234 | done: |
| 235 | close(fd); |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | static int create_service_thread(void (*func)(int, void *), void *cookie) |
| 240 | { |
| 241 | stinfo *sti; |
| 242 | adb_thread_t t; |
| 243 | int s[2]; |
| 244 | |
| 245 | if(adb_socketpair(s)) { |
| 246 | printf("cannot create service socket pair\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | sti = malloc(sizeof(stinfo)); |
| 251 | if(sti == 0) fatal("cannot allocate stinfo"); |
| 252 | sti->func = func; |
| 253 | sti->cookie = cookie; |
| 254 | sti->fd = s[1]; |
| 255 | |
| 256 | if(adb_thread_create( &t, service_bootstrap_func, sti)){ |
| 257 | free(sti); |
| 258 | adb_close(s[0]); |
| 259 | adb_close(s[1]); |
| 260 | printf("cannot create service thread\n"); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | D("service thread started, %d:%d\n",s[0], s[1]); |
| 265 | return s[0]; |
| 266 | } |
| 267 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 268 | #if !ADB_HOST |
| 269 | static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | { |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 271 | #ifdef HAVE_WIN32_PROC |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 272 | D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1); |
| 273 | fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1); |
| 274 | return -1; |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 275 | #else /* !HAVE_WIN32_PROC */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | char *devname; |
| 277 | int ptm; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | |
| 279 | ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY); |
| 280 | if(ptm < 0){ |
| 281 | printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno)); |
| 282 | return -1; |
| 283 | } |
| 284 | fcntl(ptm, F_SETFD, FD_CLOEXEC); |
| 285 | |
| 286 | if(grantpt(ptm) || unlockpt(ptm) || |
| 287 | ((devname = (char*) ptsname(ptm)) == 0)){ |
| 288 | printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno)); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 289 | adb_close(ptm); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 290 | return -1; |
| 291 | } |
| 292 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 293 | *pid = fork(); |
| 294 | if(*pid < 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | printf("- fork failed: %s -\n", strerror(errno)); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 296 | adb_close(ptm); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | return -1; |
| 298 | } |
| 299 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 300 | if(*pid == 0){ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | int pts; |
| 302 | |
| 303 | setsid(); |
| 304 | |
| 305 | pts = unix_open(devname, O_RDWR); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 306 | if(pts < 0) { |
| 307 | fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname); |
| 308 | exit(-1); |
| 309 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 310 | |
| 311 | dup2(pts, 0); |
| 312 | dup2(pts, 1); |
| 313 | dup2(pts, 2); |
| 314 | |
Benoit Goby | f4ded74 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 315 | adb_close(pts); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | adb_close(ptm); |
| 317 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 318 | // set OOM adjustment to zero |
Mike Lockwood | 3410659 | 2009-05-20 09:14:30 -0400 | [diff] [blame] | 319 | char text[64]; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 320 | snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid()); |
Mike Lockwood | 3410659 | 2009-05-20 09:14:30 -0400 | [diff] [blame] | 321 | int fd = adb_open(text, O_WRONLY); |
| 322 | if (fd >= 0) { |
| 323 | adb_write(fd, "0", 1); |
| 324 | adb_close(fd); |
| 325 | } else { |
| 326 | D("adb: unable to open %s\n", text); |
| 327 | } |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 328 | execl(cmd, cmd, arg0, arg1, NULL); |
| 329 | fprintf(stderr, "- exec '%s' failed: %s (%d) -\n", |
| 330 | cmd, strerror(errno), errno); |
| 331 | exit(-1); |
| 332 | } else { |
| 333 | // Don't set child's OOM adjustment to zero. |
| 334 | // Let the child do it itself, as sometimes the parent starts |
| 335 | // running before the child has a /proc/pid/oom_adj. |
| 336 | // """adb: unable to open /proc/644/oom_adj""" seen in some logs. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | return ptm; |
| 338 | } |
Joe Onorato | 590bca7 | 2009-09-03 16:30:43 -0700 | [diff] [blame] | 339 | #endif /* !HAVE_WIN32_PROC */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | } |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 341 | #endif /* !ABD_HOST */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | |
| 343 | #if ADB_HOST |
| 344 | #define SHELL_COMMAND "/bin/sh" |
| 345 | #else |
| 346 | #define SHELL_COMMAND "/system/bin/sh" |
| 347 | #endif |
| 348 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 349 | #if !ADB_HOST |
| 350 | static void subproc_waiter_service(int fd, void *cookie) |
| 351 | { |
| 352 | pid_t pid = (pid_t)cookie; |
| 353 | |
| 354 | D("entered. fd=%d of pid=%d\n", fd, pid); |
| 355 | for (;;) { |
| 356 | int status; |
| 357 | pid_t p = waitpid(pid, &status, 0); |
| 358 | if (p == pid) { |
| 359 | D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status); |
| 360 | if (WIFSIGNALED(status)) { |
| 361 | D("*** Killed by signal %d\n", WTERMSIG(status)); |
| 362 | break; |
| 363 | } else if (!WIFEXITED(status)) { |
| 364 | D("*** Didn't exit!!. status %d\n", status); |
| 365 | break; |
| 366 | } else if (WEXITSTATUS(status) >= 0) { |
| 367 | D("*** Exit code %d\n", WEXITSTATUS(status)); |
| 368 | break; |
| 369 | } |
| 370 | } |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 371 | } |
| 372 | D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno); |
| 373 | if (SHELL_EXIT_NOTIFY_FD >=0) { |
| 374 | int res; |
| 375 | res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd)); |
| 376 | D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n", |
| 377 | SHELL_EXIT_NOTIFY_FD, pid, res, errno); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static int create_subproc_thread(const char *name) |
| 382 | { |
| 383 | stinfo *sti; |
| 384 | adb_thread_t t; |
| 385 | int ret_fd; |
| 386 | pid_t pid; |
| 387 | if(name) { |
| 388 | ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid); |
| 389 | } else { |
| 390 | ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid); |
| 391 | } |
| 392 | D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid); |
| 393 | |
| 394 | sti = malloc(sizeof(stinfo)); |
| 395 | if(sti == 0) fatal("cannot allocate stinfo"); |
| 396 | sti->func = subproc_waiter_service; |
| 397 | sti->cookie = (void*)pid; |
| 398 | sti->fd = ret_fd; |
| 399 | |
| 400 | if(adb_thread_create( &t, service_bootstrap_func, sti)){ |
| 401 | free(sti); |
| 402 | adb_close(ret_fd); |
| 403 | printf("cannot create service thread\n"); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | D("service thread started, fd=%d pid=%d\n",ret_fd, pid); |
| 408 | return ret_fd; |
| 409 | } |
| 410 | #endif |
| 411 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 412 | int service_to_fd(const char *name) |
| 413 | { |
| 414 | int ret = -1; |
| 415 | |
| 416 | if(!strncmp(name, "tcp:", 4)) { |
| 417 | int port = atoi(name + 4); |
| 418 | name = strchr(name + 4, ':'); |
| 419 | if(name == 0) { |
| 420 | ret = socket_loopback_client(port, SOCK_STREAM); |
| 421 | if (ret >= 0) |
| 422 | disable_tcp_nagle(ret); |
| 423 | } else { |
| 424 | #if ADB_HOST |
| 425 | adb_mutex_lock(&dns_lock); |
| 426 | ret = socket_network_client(name + 1, port, SOCK_STREAM); |
| 427 | adb_mutex_unlock(&dns_lock); |
| 428 | #else |
| 429 | return -1; |
| 430 | #endif |
| 431 | } |
| 432 | #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */ |
| 433 | } else if(!strncmp(name, "local:", 6)) { |
| 434 | ret = socket_local_client(name + 6, |
| 435 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 436 | } else if(!strncmp(name, "localreserved:", 14)) { |
| 437 | ret = socket_local_client(name + 14, |
| 438 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 439 | } else if(!strncmp(name, "localabstract:", 14)) { |
| 440 | ret = socket_local_client(name + 14, |
| 441 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 442 | } else if(!strncmp(name, "localfilesystem:", 16)) { |
| 443 | ret = socket_local_client(name + 16, |
| 444 | ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); |
| 445 | #endif |
| 446 | #if ADB_HOST |
| 447 | } else if(!strncmp("dns:", name, 4)){ |
| 448 | char *n = strdup(name + 4); |
| 449 | if(n == 0) return -1; |
| 450 | ret = create_service_thread(dns_service, n); |
| 451 | #else /* !ADB_HOST */ |
| 452 | } else if(!strncmp("dev:", name, 4)) { |
| 453 | ret = unix_open(name + 4, O_RDWR); |
| 454 | } else if(!strncmp(name, "framebuffer:", 12)) { |
| 455 | ret = create_service_thread(framebuffer_service, 0); |
| 456 | } else if(recovery_mode && !strncmp(name, "recover:", 8)) { |
| 457 | ret = create_service_thread(recover_service, (void*) atoi(name + 8)); |
| 458 | } else if (!strncmp(name, "jdwp:", 5)) { |
| 459 | ret = create_jdwp_connection_fd(atoi(name+5)); |
| 460 | } else if (!strncmp(name, "log:", 4)) { |
| 461 | ret = create_service_thread(log_service, get_log_file_path(name + 4)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | } else if(!HOST && !strncmp(name, "shell:", 6)) { |
| 463 | if(name[6]) { |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 464 | ret = create_subproc_thread(name + 6); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | } else { |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 466 | ret = create_subproc_thread(0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | } else if(!strncmp(name, "sync:", 5)) { |
| 469 | ret = create_service_thread(file_sync_service, NULL); |
| 470 | } else if(!strncmp(name, "remount:", 8)) { |
| 471 | ret = create_service_thread(remount_service, NULL); |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 472 | } else if(!strncmp(name, "reboot:", 7)) { |
Mike Lockwood | 1a855ae | 2009-09-19 16:52:58 -0400 | [diff] [blame] | 473 | void* arg = strdup(name + 7); |
| 474 | if(arg == 0) return -1; |
| 475 | ret = create_service_thread(reboot_service, arg); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 476 | } else if(!strncmp(name, "root:", 5)) { |
| 477 | ret = create_service_thread(restart_root_service, NULL); |
Christopher Tate | 7377912 | 2011-04-21 12:53:28 -0700 | [diff] [blame] | 478 | } else if(!strncmp(name, "backup:", 7)) { |
| 479 | char* arg = strdup(name+7); |
| 480 | if (arg == NULL) return -1; |
Christopher Tate | cf5379b | 2011-05-17 15:52:54 -0700 | [diff] [blame] | 481 | ret = backup_service(BACKUP, arg); |
| 482 | } else if(!strncmp(name, "restore:", 8)) { |
| 483 | ret = backup_service(RESTORE, NULL); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 484 | } else if(!strncmp(name, "tcpip:", 6)) { |
| 485 | int port; |
| 486 | if (sscanf(name + 6, "%d", &port) == 0) { |
| 487 | port = 0; |
| 488 | } |
| 489 | ret = create_service_thread(restart_tcp_service, (void *)port); |
| 490 | } else if(!strncmp(name, "usb:", 4)) { |
| 491 | ret = create_service_thread(restart_usb_service, NULL); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | #endif |
| 493 | #if 0 |
| 494 | } else if(!strncmp(name, "echo:", 5)){ |
| 495 | ret = create_service_thread(echo_service, 0); |
| 496 | #endif |
| 497 | } |
| 498 | if (ret >= 0) { |
| 499 | close_on_exec(ret); |
| 500 | } |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | #if ADB_HOST |
| 505 | struct state_info { |
| 506 | transport_type transport; |
| 507 | char* serial; |
| 508 | int state; |
| 509 | }; |
| 510 | |
| 511 | static void wait_for_state(int fd, void* cookie) |
| 512 | { |
| 513 | struct state_info* sinfo = cookie; |
| 514 | char* err = "unknown error"; |
| 515 | |
| 516 | D("wait_for_state %d\n", sinfo->state); |
| 517 | |
| 518 | atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); |
| 519 | if(t != 0) { |
| 520 | writex(fd, "OKAY", 4); |
| 521 | } else { |
| 522 | sendfailmsg(fd, err); |
| 523 | } |
| 524 | |
| 525 | if (sinfo->serial) |
| 526 | free(sinfo->serial); |
| 527 | free(sinfo); |
| 528 | adb_close(fd); |
| 529 | D("wait_for_state is done\n"); |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | #if ADB_HOST |
| 534 | asocket* host_service_to_socket(const char* name, const char *serial) |
| 535 | { |
| 536 | if (!strcmp(name,"track-devices")) { |
| 537 | return create_device_tracker(); |
| 538 | } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { |
| 539 | struct state_info* sinfo = malloc(sizeof(struct state_info)); |
| 540 | |
| 541 | if (serial) |
| 542 | sinfo->serial = strdup(serial); |
| 543 | else |
| 544 | sinfo->serial = NULL; |
| 545 | |
| 546 | name += strlen("wait-for-"); |
| 547 | |
| 548 | if (!strncmp(name, "local", strlen("local"))) { |
| 549 | sinfo->transport = kTransportLocal; |
| 550 | sinfo->state = CS_DEVICE; |
| 551 | } else if (!strncmp(name, "usb", strlen("usb"))) { |
| 552 | sinfo->transport = kTransportUsb; |
| 553 | sinfo->state = CS_DEVICE; |
| 554 | } else if (!strncmp(name, "any", strlen("any"))) { |
| 555 | sinfo->transport = kTransportAny; |
| 556 | sinfo->state = CS_DEVICE; |
| 557 | } else { |
| 558 | free(sinfo); |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | int fd = create_service_thread(wait_for_state, sinfo); |
| 563 | return create_local_socket(fd); |
| 564 | } |
| 565 | return NULL; |
| 566 | } |
| 567 | #endif /* ADB_HOST */ |