blob: b5df5542b935c30e432d7ed2eb1317aa53dfa59b [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];
179 int ret;
180
181 sync();
Mike Lockwood26b88e32009-08-24 15:58:40 -0700182 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
183 LINUX_REBOOT_CMD_RESTART2, (char *)arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400184 if (ret < 0) {
185 snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno));
186 writex(fd, buf, strlen(buf));
187 }
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400188 free(arg);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400189 adb_close(fd);
190}
191
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192#endif
193
194#if 0
195static void echo_service(int fd, void *cookie)
196{
197 char buf[4096];
198 int r;
199 char *p;
200 int c;
201
202 for(;;) {
203 r = read(fd, buf, 4096);
204 if(r == 0) goto done;
205 if(r < 0) {
206 if(errno == EINTR) continue;
207 else goto done;
208 }
209
210 c = r;
211 p = buf;
212 while(c > 0) {
213 r = write(fd, p, c);
214 if(r > 0) {
215 c -= r;
216 p += r;
217 continue;
218 }
219 if((r < 0) && (errno == EINTR)) continue;
220 goto done;
221 }
222 }
223done:
224 close(fd);
225}
226#endif
227
228static int create_service_thread(void (*func)(int, void *), void *cookie)
229{
230 stinfo *sti;
231 adb_thread_t t;
232 int s[2];
233
234 if(adb_socketpair(s)) {
235 printf("cannot create service socket pair\n");
236 return -1;
237 }
238
239 sti = malloc(sizeof(stinfo));
240 if(sti == 0) fatal("cannot allocate stinfo");
241 sti->func = func;
242 sti->cookie = cookie;
243 sti->fd = s[1];
244
245 if(adb_thread_create( &t, service_bootstrap_func, sti)){
246 free(sti);
247 adb_close(s[0]);
248 adb_close(s[1]);
249 printf("cannot create service thread\n");
250 return -1;
251 }
252
253 D("service thread started, %d:%d\n",s[0], s[1]);
254 return s[0];
255}
256
257static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
258{
Joe Onorato590bca72009-09-03 16:30:43 -0700259#ifdef HAVE_WIN32_PROC
260 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
261 return -1;
262#else /* !HAVE_WIN32_PROC */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800263 char *devname;
264 int ptm;
265 pid_t pid;
266
267 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
268 if(ptm < 0){
269 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
270 return -1;
271 }
272 fcntl(ptm, F_SETFD, FD_CLOEXEC);
273
274 if(grantpt(ptm) || unlockpt(ptm) ||
275 ((devname = (char*) ptsname(ptm)) == 0)){
276 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
277 return -1;
278 }
279
280 pid = fork();
281 if(pid < 0) {
282 printf("- fork failed: %s -\n", strerror(errno));
283 return -1;
284 }
285
286 if(pid == 0){
287 int pts;
288
289 setsid();
290
291 pts = unix_open(devname, O_RDWR);
292 if(pts < 0) exit(-1);
293
294 dup2(pts, 0);
295 dup2(pts, 1);
296 dup2(pts, 2);
297
298 adb_close(ptm);
299
300 execl(cmd, cmd, arg0, arg1, NULL);
301 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
302 cmd, strerror(errno), errno);
303 exit(-1);
304 } else {
Joe Onorato590bca72009-09-03 16:30:43 -0700305#if !ADB_HOST
Mike Lockwood34106592009-05-20 09:14:30 -0400306 // set child's OOM adjustment to zero
307 char text[64];
308 snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
309 int fd = adb_open(text, O_WRONLY);
310 if (fd >= 0) {
311 adb_write(fd, "0", 1);
312 adb_close(fd);
313 } else {
314 D("adb: unable to open %s\n", text);
315 }
Joe Onorato590bca72009-09-03 16:30:43 -0700316#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 return ptm;
318 }
Joe Onorato590bca72009-09-03 16:30:43 -0700319#endif /* !HAVE_WIN32_PROC */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800320}
321
322#if ADB_HOST
323#define SHELL_COMMAND "/bin/sh"
324#else
325#define SHELL_COMMAND "/system/bin/sh"
326#endif
327
328int service_to_fd(const char *name)
329{
330 int ret = -1;
331
332 if(!strncmp(name, "tcp:", 4)) {
333 int port = atoi(name + 4);
334 name = strchr(name + 4, ':');
335 if(name == 0) {
336 ret = socket_loopback_client(port, SOCK_STREAM);
337 if (ret >= 0)
338 disable_tcp_nagle(ret);
339 } else {
340#if ADB_HOST
341 adb_mutex_lock(&dns_lock);
342 ret = socket_network_client(name + 1, port, SOCK_STREAM);
343 adb_mutex_unlock(&dns_lock);
344#else
345 return -1;
346#endif
347 }
348#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
349 } else if(!strncmp(name, "local:", 6)) {
350 ret = socket_local_client(name + 6,
351 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
352 } else if(!strncmp(name, "localreserved:", 14)) {
353 ret = socket_local_client(name + 14,
354 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
355 } else if(!strncmp(name, "localabstract:", 14)) {
356 ret = socket_local_client(name + 14,
357 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
358 } else if(!strncmp(name, "localfilesystem:", 16)) {
359 ret = socket_local_client(name + 16,
360 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
361#endif
362#if ADB_HOST
363 } else if(!strncmp("dns:", name, 4)){
364 char *n = strdup(name + 4);
365 if(n == 0) return -1;
366 ret = create_service_thread(dns_service, n);
367#else /* !ADB_HOST */
368 } else if(!strncmp("dev:", name, 4)) {
369 ret = unix_open(name + 4, O_RDWR);
370 } else if(!strncmp(name, "framebuffer:", 12)) {
371 ret = create_service_thread(framebuffer_service, 0);
372 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
373 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
374 } else if (!strncmp(name, "jdwp:", 5)) {
375 ret = create_jdwp_connection_fd(atoi(name+5));
376 } else if (!strncmp(name, "log:", 4)) {
377 ret = create_service_thread(log_service, get_log_file_path(name + 4));
Joe Onorato590bca72009-09-03 16:30:43 -0700378#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 } else if(!HOST && !strncmp(name, "shell:", 6)) {
380 if(name[6]) {
Joe Onorato590bca72009-09-03 16:30:43 -0700381 ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382 } else {
Joe Onorato590bca72009-09-03 16:30:43 -0700383 ret = create_subprocess(SHELL_COMMAND, "-", 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384 }
Joe Onorato590bca72009-09-03 16:30:43 -0700385#if !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800386 } else if(!strncmp(name, "sync:", 5)) {
387 ret = create_service_thread(file_sync_service, NULL);
388 } else if(!strncmp(name, "remount:", 8)) {
389 ret = create_service_thread(remount_service, NULL);
Mike Lockwood12a35ea2009-08-04 20:37:51 -0400390 } else if(!strncmp(name, "reboot:", 7)) {
Mike Lockwood1a855ae2009-09-19 16:52:58 -0400391 void* arg = strdup(name + 7);
392 if(arg == 0) return -1;
393 ret = create_service_thread(reboot_service, arg);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700394 } else if(!strncmp(name, "root:", 5)) {
395 ret = create_service_thread(restart_root_service, NULL);
Mike Lockwood26b88e32009-08-24 15:58:40 -0700396 } else if(!strncmp(name, "tcpip:", 6)) {
397 int port;
398 if (sscanf(name + 6, "%d", &port) == 0) {
399 port = 0;
400 }
401 ret = create_service_thread(restart_tcp_service, (void *)port);
402 } else if(!strncmp(name, "usb:", 4)) {
403 ret = create_service_thread(restart_usb_service, NULL);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404#endif
405#if 0
406 } else if(!strncmp(name, "echo:", 5)){
407 ret = create_service_thread(echo_service, 0);
408#endif
409 }
410 if (ret >= 0) {
411 close_on_exec(ret);
412 }
413 return ret;
414}
415
416#if ADB_HOST
417struct state_info {
418 transport_type transport;
419 char* serial;
420 int state;
421};
422
423static void wait_for_state(int fd, void* cookie)
424{
425 struct state_info* sinfo = cookie;
426 char* err = "unknown error";
427
428 D("wait_for_state %d\n", sinfo->state);
429
430 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
431 if(t != 0) {
432 writex(fd, "OKAY", 4);
433 } else {
434 sendfailmsg(fd, err);
435 }
436
437 if (sinfo->serial)
438 free(sinfo->serial);
439 free(sinfo);
440 adb_close(fd);
441 D("wait_for_state is done\n");
442}
443#endif
444
445#if ADB_HOST
446asocket* host_service_to_socket(const char* name, const char *serial)
447{
448 if (!strcmp(name,"track-devices")) {
449 return create_device_tracker();
450 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
451 struct state_info* sinfo = malloc(sizeof(struct state_info));
452
453 if (serial)
454 sinfo->serial = strdup(serial);
455 else
456 sinfo->serial = NULL;
457
458 name += strlen("wait-for-");
459
460 if (!strncmp(name, "local", strlen("local"))) {
461 sinfo->transport = kTransportLocal;
462 sinfo->state = CS_DEVICE;
463 } else if (!strncmp(name, "usb", strlen("usb"))) {
464 sinfo->transport = kTransportUsb;
465 sinfo->state = CS_DEVICE;
466 } else if (!strncmp(name, "any", strlen("any"))) {
467 sinfo->transport = kTransportAny;
468 sinfo->state = CS_DEVICE;
469 } else {
470 free(sinfo);
471 return NULL;
472 }
473
474 int fd = create_service_thread(wait_for_state, sinfo);
475 return create_local_socket(fd);
476 }
477 return NULL;
478}
479#endif /* ADB_HOST */