blob: e2b58fee20179cd2f7c06c82c9795e3bab1e71c2 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Mike Lockwoodcc1de482009-07-30 16:23:56 -070034#else
Joe Onorato91acb142009-09-03 16:30:43 -070035# include <sys/reboot.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#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);
Mike Lockwoodb6b40072009-09-19 16:52:58 -040066 free(cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 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
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700109void 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));
Mike Lockwoodff196702009-08-24 15:58:40 -0700123 adb_close(fd);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700124 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
Mike Lockwoodff196702009-08-24 15:58:40 -0700138void 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)
Mike Lockwoodee156622009-08-04 20:37:51 -0400177{
178 char buf[100];
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500179 int pid, ret;
Mike Lockwoodee156622009-08-04 20:37:51 -0400180
181 sync();
Mike Lockwoodd969faa2010-02-24 16:07:23 -0500182
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
Mike Lockwoodff196702009-08-24 15:58:40 -0700196 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
197 LINUX_REBOOT_CMD_RESTART2, (char *)arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400198 if (ret < 0) {
199 snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno));
200 writex(fd, buf, strlen(buf));
201 }
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400202 free(arg);
Mike Lockwoodee156622009-08-04 20:37:51 -0400203 adb_close(fd);
204}
205
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206#endif
207
208#if 0
209static void echo_service(int fd, void *cookie)
210{
211 char buf[4096];
212 int r;
213 char *p;
214 int c;
215
216 for(;;) {
217 r = read(fd, buf, 4096);
218 if(r == 0) goto done;
219 if(r < 0) {
220 if(errno == EINTR) continue;
221 else goto done;
222 }
223
224 c = r;
225 p = buf;
226 while(c > 0) {
227 r = write(fd, p, c);
228 if(r > 0) {
229 c -= r;
230 p += r;
231 continue;
232 }
233 if((r < 0) && (errno == EINTR)) continue;
234 goto done;
235 }
236 }
237done:
238 close(fd);
239}
240#endif
241
242static int create_service_thread(void (*func)(int, void *), void *cookie)
243{
244 stinfo *sti;
245 adb_thread_t t;
246 int s[2];
247
248 if(adb_socketpair(s)) {
249 printf("cannot create service socket pair\n");
250 return -1;
251 }
252
253 sti = malloc(sizeof(stinfo));
254 if(sti == 0) fatal("cannot allocate stinfo");
255 sti->func = func;
256 sti->cookie = cookie;
257 sti->fd = s[1];
258
259 if(adb_thread_create( &t, service_bootstrap_func, sti)){
260 free(sti);
261 adb_close(s[0]);
262 adb_close(s[1]);
263 printf("cannot create service thread\n");
264 return -1;
265 }
266
267 D("service thread started, %d:%d\n",s[0], s[1]);
268 return s[0];
269}
270
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800271static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272{
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800273 D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
Joe Onorato91acb142009-09-03 16:30:43 -0700274#ifdef HAVE_WIN32_PROC
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800275 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
276 return -1;
Joe Onorato91acb142009-09-03 16:30:43 -0700277#else /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 char *devname;
279 int ptm;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280
281 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
282 if(ptm < 0){
283 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
284 return -1;
285 }
286 fcntl(ptm, F_SETFD, FD_CLOEXEC);
287
288 if(grantpt(ptm) || unlockpt(ptm) ||
289 ((devname = (char*) ptsname(ptm)) == 0)){
290 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
291 return -1;
292 }
293
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800294 *pid = fork();
295 if(*pid < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 printf("- fork failed: %s -\n", strerror(errno));
297 return -1;
298 }
299
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800300 if(*pid == 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 int pts;
302
303 setsid();
304
305 pts = unix_open(devname, O_RDWR);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800306 if(pts < 0) {
307 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
308 exit(-1);
309 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
311 dup2(pts, 0);
312 dup2(pts, 1);
313 dup2(pts, 2);
314
Benoit Goby95ef8282011-02-01 18:57:41 -0800315 adb_close(pts);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 adb_close(ptm);
317
Joe Onorato91acb142009-09-03 16:30:43 -0700318#if !ADB_HOST
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800319 // set OOM adjustment to zero
Mike Lockwood249ad572009-05-20 09:14:30 -0400320 char text[64];
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800321 snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
Mike Lockwood249ad572009-05-20 09:14:30 -0400322 int fd = adb_open(text, O_WRONLY);
323 if (fd >= 0) {
324 adb_write(fd, "0", 1);
325 adb_close(fd);
326 } else {
327 D("adb: unable to open %s\n", text);
328 }
Joe Onorato91acb142009-09-03 16:30:43 -0700329#endif
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800330 execl(cmd, cmd, arg0, arg1, NULL);
331 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
332 cmd, strerror(errno), errno);
333 exit(-1);
334 } else {
335#if !ADB_HOST
336 // Don't set child's OOM adjustment to zero.
337 // Let the child do it itself, as sometimes the parent starts
338 // running before the child has a /proc/pid/oom_adj.
339 // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
340#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 return ptm;
342 }
Joe Onorato91acb142009-09-03 16:30:43 -0700343#endif /* !HAVE_WIN32_PROC */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344}
345
346#if ADB_HOST
347#define SHELL_COMMAND "/bin/sh"
348#else
349#define SHELL_COMMAND "/system/bin/sh"
350#endif
351
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800352#if !ADB_HOST
353static void subproc_waiter_service(int fd, void *cookie)
354{
355 pid_t pid = (pid_t)cookie;
356
357 D("entered. fd=%d of pid=%d\n", fd, pid);
358 for (;;) {
359 int status;
360 pid_t p = waitpid(pid, &status, 0);
361 if (p == pid) {
362 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
363 if (WIFSIGNALED(status)) {
364 D("*** Killed by signal %d\n", WTERMSIG(status));
365 break;
366 } else if (!WIFEXITED(status)) {
367 D("*** Didn't exit!!. status %d\n", status);
368 break;
369 } else if (WEXITSTATUS(status) >= 0) {
370 D("*** Exit code %d\n", WEXITSTATUS(status));
371 break;
372 }
373 }
374 usleep(100000); // poll every 0.1 sec
375 }
376 D("closing fd=%d of pid=%d\n", fd, pid);
377 sleep(5); // Give a chance for the exiting proc to flush.
378 adb_close(fd);
379}
380
381static 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
388 if(name) {
389 ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
390 } else {
391 ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
392 }
393 D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
394
395 sti = malloc(sizeof(stinfo));
396 if(sti == 0) fatal("cannot allocate stinfo");
397 sti->func = subproc_waiter_service;
398 sti->cookie = (void*)pid;
399 sti->fd = ret_fd;
400
401 if(adb_thread_create( &t, service_bootstrap_func, sti)){
402 free(sti);
403 adb_close(ret_fd);
404 printf("cannot create service thread\n");
405 return -1;
406 }
407
408 D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
409 return ret_fd;
410}
411#endif
412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413int service_to_fd(const char *name)
414{
415 int ret = -1;
416
417 if(!strncmp(name, "tcp:", 4)) {
418 int port = atoi(name + 4);
419 name = strchr(name + 4, ':');
420 if(name == 0) {
421 ret = socket_loopback_client(port, SOCK_STREAM);
422 if (ret >= 0)
423 disable_tcp_nagle(ret);
424 } else {
425#if ADB_HOST
426 adb_mutex_lock(&dns_lock);
427 ret = socket_network_client(name + 1, port, SOCK_STREAM);
428 adb_mutex_unlock(&dns_lock);
429#else
430 return -1;
431#endif
432 }
433#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
434 } else if(!strncmp(name, "local:", 6)) {
435 ret = socket_local_client(name + 6,
436 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
437 } else if(!strncmp(name, "localreserved:", 14)) {
438 ret = socket_local_client(name + 14,
439 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
440 } else if(!strncmp(name, "localabstract:", 14)) {
441 ret = socket_local_client(name + 14,
442 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
443 } else if(!strncmp(name, "localfilesystem:", 16)) {
444 ret = socket_local_client(name + 16,
445 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
446#endif
447#if ADB_HOST
448 } else if(!strncmp("dns:", name, 4)){
449 char *n = strdup(name + 4);
450 if(n == 0) return -1;
451 ret = create_service_thread(dns_service, n);
452#else /* !ADB_HOST */
453 } else if(!strncmp("dev:", name, 4)) {
454 ret = unix_open(name + 4, O_RDWR);
455 } else if(!strncmp(name, "framebuffer:", 12)) {
456 ret = create_service_thread(framebuffer_service, 0);
457 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
458 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
459 } else if (!strncmp(name, "jdwp:", 5)) {
460 ret = create_jdwp_connection_fd(atoi(name+5));
461 } else if (!strncmp(name, "log:", 4)) {
462 ret = create_service_thread(log_service, get_log_file_path(name + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 } else if(!HOST && !strncmp(name, "shell:", 6)) {
464 if(name[6]) {
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800465 ret = create_subproc_thread(name + 6);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 } else {
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800467 ret = create_subproc_thread(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 } else if(!strncmp(name, "sync:", 5)) {
470 ret = create_service_thread(file_sync_service, NULL);
471 } else if(!strncmp(name, "remount:", 8)) {
472 ret = create_service_thread(remount_service, NULL);
Mike Lockwoodee156622009-08-04 20:37:51 -0400473 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwoodb6b40072009-09-19 16:52:58 -0400474 void* arg = strdup(name + 7);
475 if(arg == 0) return -1;
476 ret = create_service_thread(reboot_service, arg);
The Android Open Source Projecte037fd72009-03-13 13:04:37 -0700477 } else if(!strncmp(name, "root:", 5)) {
478 ret = create_service_thread(restart_root_service, NULL);
Mike Lockwoodff196702009-08-24 15:58:40 -0700479 } else if(!strncmp(name, "tcpip:", 6)) {
480 int port;
481 if (sscanf(name + 6, "%d", &port) == 0) {
482 port = 0;
483 }
484 ret = create_service_thread(restart_tcp_service, (void *)port);
485 } else if(!strncmp(name, "usb:", 4)) {
486 ret = create_service_thread(restart_usb_service, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487#endif
488#if 0
489 } else if(!strncmp(name, "echo:", 5)){
490 ret = create_service_thread(echo_service, 0);
491#endif
492 }
493 if (ret >= 0) {
494 close_on_exec(ret);
495 }
496 return ret;
497}
498
499#if ADB_HOST
500struct state_info {
501 transport_type transport;
502 char* serial;
503 int state;
504};
505
506static void wait_for_state(int fd, void* cookie)
507{
508 struct state_info* sinfo = cookie;
509 char* err = "unknown error";
510
511 D("wait_for_state %d\n", sinfo->state);
512
513 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
514 if(t != 0) {
515 writex(fd, "OKAY", 4);
516 } else {
517 sendfailmsg(fd, err);
518 }
519
520 if (sinfo->serial)
521 free(sinfo->serial);
522 free(sinfo);
523 adb_close(fd);
524 D("wait_for_state is done\n");
525}
526#endif
527
528#if ADB_HOST
529asocket* host_service_to_socket(const char* name, const char *serial)
530{
531 if (!strcmp(name,"track-devices")) {
532 return create_device_tracker();
533 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
534 struct state_info* sinfo = malloc(sizeof(struct state_info));
535
536 if (serial)
537 sinfo->serial = strdup(serial);
538 else
539 sinfo->serial = NULL;
540
541 name += strlen("wait-for-");
542
543 if (!strncmp(name, "local", strlen("local"))) {
544 sinfo->transport = kTransportLocal;
545 sinfo->state = CS_DEVICE;
546 } else if (!strncmp(name, "usb", strlen("usb"))) {
547 sinfo->transport = kTransportUsb;
548 sinfo->state = CS_DEVICE;
549 } else if (!strncmp(name, "any", strlen("any"))) {
550 sinfo->transport = kTransportAny;
551 sinfo->state = CS_DEVICE;
552 } else {
553 free(sinfo);
554 return NULL;
555 }
556
557 int fd = create_service_thread(wait_for_state, sinfo);
558 return create_local_socket(fd);
559 }
560 return NULL;
561}
562#endif /* ADB_HOST */