blob: 487c7d37ddfb7773d4bd72e4e5ed994a36544b2b [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Lockwoodfa311c32009-07-30 16:23:56 -070034#else
Joe Onorato590bca72009-09-03 16:30:43 -070035# include <sys/reboot.h>
The Android Open Source Project9ca14dc2009-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 Lockwood1a855ae2009-09-19 16:52:58 -040066 free(cookie);
The Android Open Source Project9ca14dc2009-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 Project9c753402009-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 Lockwood26b88e32009-08-24 15:58:40 -0700123 adb_close(fd);
The Android Open Source Project9c753402009-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 Lockwood26b88e32009-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 Lockwood12a35ea2009-08-04 20:37:51 -0400177{
178 char buf[100];
Mike Lockwoodd49e9eb2010-02-24 16:07:23 -0500179 int pid, ret;
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400180
181 sync();
Mike Lockwoodd49e9eb2010-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 Lockwood26b88e32009-08-24 15:58:40 -0700196 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
197 LINUX_REBOOT_CMD_RESTART2, (char *)arg);
Mike Lockwood12a35ea2009-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 Lockwood1a855ae2009-09-19 16:52:58 -0400202 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400203 adb_close(fd);
204}
205
The Android Open Source Project9ca14dc2009-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
271static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
272{
Joe Onorato590bca72009-09-03 16:30:43 -0700273#ifdef HAVE_WIN32_PROC
274 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
275 return -1;
276#else /* !HAVE_WIN32_PROC */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277 char *devname;
278 int ptm;
279 pid_t pid;
280
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
294 pid = fork();
295 if(pid < 0) {
296 printf("- fork failed: %s -\n", strerror(errno));
297 return -1;
298 }
299
300 if(pid == 0){
301 int pts;
302
303 setsid();
304
305 pts = unix_open(devname, O_RDWR);
306 if(pts < 0) exit(-1);
307
308 dup2(pts, 0);
309 dup2(pts, 1);
310 dup2(pts, 2);
311
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 {
Joe Onorato590bca72009-09-03 16:30:43 -0700319#if !ADB_HOST
Mike Lockwood34106592009-05-20 09:14:30 -0400320 // 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 }
Joe Onorato590bca72009-09-03 16:30:43 -0700330#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331 return ptm;
332 }
Joe Onorato590bca72009-09-03 16:30:43 -0700333#endif /* !HAVE_WIN32_PROC */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334}
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));
Joe Onorato590bca72009-09-03 16:30:43 -0700392#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800393 } else if(!HOST && !strncmp(name, "shell:", 6)) {
394 if(name[6]) {
Joe Onorato590bca72009-09-03 16:30:43 -0700395 ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396 } else {
Joe Onorato590bca72009-09-03 16:30:43 -0700397 ret = create_subprocess(SHELL_COMMAND, "-", 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 }
Joe Onorato590bca72009-09-03 16:30:43 -0700399#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400 } 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);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400404 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400405 void* arg = strdup(name + 7);
406 if(arg == 0) return -1;
407 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700408 } else if(!strncmp(name, "root:", 5)) {
409 ret = create_service_thread(restart_root_service, NULL);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700410 } 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);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418#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 */