blob: 1de55e6cdc800d064b7cc0f9325b98f8279aae27 [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
34#endif
35
36typedef struct stinfo stinfo;
37
38struct stinfo {
39 void (*func)(int fd, void *cookie);
40 int fd;
41 void *cookie;
42};
43
44
45void *service_bootstrap_func(void *x)
46{
47 stinfo *sti = x;
48 sti->func(sti->fd, sti->cookie);
49 free(sti);
50 return 0;
51}
52
53#if ADB_HOST
54ADB_MUTEX_DEFINE( dns_lock );
55
56static void dns_service(int fd, void *cookie)
57{
58 char *hostname = cookie;
59 struct hostent *hp;
60 unsigned zero = 0;
61
62 adb_mutex_lock(&dns_lock);
63 hp = gethostbyname(hostname);
64 if(hp == 0) {
65 writex(fd, &zero, 4);
66 } else {
67 writex(fd, hp->h_addr, 4);
68 }
69 adb_mutex_unlock(&dns_lock);
70 adb_close(fd);
71}
72#else
73extern int recovery_mode;
74
75static void recover_service(int s, void *cookie)
76{
77 unsigned char buf[4096];
78 unsigned count = (unsigned) cookie;
79 int fd;
80
81 fd = adb_creat("/tmp/update", 0644);
82 if(fd < 0) {
83 adb_close(s);
84 return;
85 }
86
87 while(count > 0) {
88 unsigned xfer = (count > 4096) ? 4096 : count;
89 if(readx(s, buf, xfer)) break;
90 if(writex(fd, buf, xfer)) break;
91 count -= xfer;
92 }
93
94 if(count == 0) {
95 writex(s, "OKAY", 4);
96 } else {
97 writex(s, "FAIL", 4);
98 }
99 adb_close(fd);
100 adb_close(s);
101
102 fd = adb_creat("/tmp/update.begin", 0644);
103 adb_close(fd);
104}
105
The Android Open Source Project9c753402009-03-13 13:04:37 -0700106void restart_root_service(int fd, void *cookie)
107{
108 char buf[100];
109 char value[PROPERTY_VALUE_MAX];
110
111 if (getuid() == 0) {
112 snprintf(buf, sizeof(buf), "adbd is already running as root\n");
113 writex(fd, buf, strlen(buf));
114 adb_close(fd);
115 } else {
116 property_get("ro.debuggable", value, "");
117 if (strcmp(value, "1") != 0) {
118 snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
119 writex(fd, buf, strlen(buf));
120 return;
121 }
122
123 property_set("service.adb.root", "1");
124 snprintf(buf, sizeof(buf), "restarting adbd as root\n");
125 writex(fd, buf, strlen(buf));
126 adb_close(fd);
127
128 // quit, and init will restart us as root
129 sleep(1);
130 exit(1);
131 }
132}
133
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134#endif
135
136#if 0
137static void echo_service(int fd, void *cookie)
138{
139 char buf[4096];
140 int r;
141 char *p;
142 int c;
143
144 for(;;) {
145 r = read(fd, buf, 4096);
146 if(r == 0) goto done;
147 if(r < 0) {
148 if(errno == EINTR) continue;
149 else goto done;
150 }
151
152 c = r;
153 p = buf;
154 while(c > 0) {
155 r = write(fd, p, c);
156 if(r > 0) {
157 c -= r;
158 p += r;
159 continue;
160 }
161 if((r < 0) && (errno == EINTR)) continue;
162 goto done;
163 }
164 }
165done:
166 close(fd);
167}
168#endif
169
170static int create_service_thread(void (*func)(int, void *), void *cookie)
171{
172 stinfo *sti;
173 adb_thread_t t;
174 int s[2];
175
176 if(adb_socketpair(s)) {
177 printf("cannot create service socket pair\n");
178 return -1;
179 }
180
181 sti = malloc(sizeof(stinfo));
182 if(sti == 0) fatal("cannot allocate stinfo");
183 sti->func = func;
184 sti->cookie = cookie;
185 sti->fd = s[1];
186
187 if(adb_thread_create( &t, service_bootstrap_func, sti)){
188 free(sti);
189 adb_close(s[0]);
190 adb_close(s[1]);
191 printf("cannot create service thread\n");
192 return -1;
193 }
194
195 D("service thread started, %d:%d\n",s[0], s[1]);
196 return s[0];
197}
198
199static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
200{
201#ifdef HAVE_WIN32_PROC
202 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
203 return -1;
204#else /* !HAVE_WIN32_PROC */
205 char *devname;
206 int ptm;
207 pid_t pid;
208
209 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
210 if(ptm < 0){
211 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
212 return -1;
213 }
214 fcntl(ptm, F_SETFD, FD_CLOEXEC);
215
216 if(grantpt(ptm) || unlockpt(ptm) ||
217 ((devname = (char*) ptsname(ptm)) == 0)){
218 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
219 return -1;
220 }
221
222 pid = fork();
223 if(pid < 0) {
224 printf("- fork failed: %s -\n", strerror(errno));
225 return -1;
226 }
227
228 if(pid == 0){
229 int pts;
230
231 setsid();
232
233 pts = unix_open(devname, O_RDWR);
234 if(pts < 0) exit(-1);
235
236 dup2(pts, 0);
237 dup2(pts, 1);
238 dup2(pts, 2);
239
240 adb_close(ptm);
241
242 execl(cmd, cmd, arg0, arg1, NULL);
243 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
244 cmd, strerror(errno), errno);
245 exit(-1);
246 } else {
247 return ptm;
248 }
249#endif /* !HAVE_WIN32_PROC */
250}
251
252#if ADB_HOST
253#define SHELL_COMMAND "/bin/sh"
254#else
255#define SHELL_COMMAND "/system/bin/sh"
256#endif
257
258int service_to_fd(const char *name)
259{
260 int ret = -1;
261
262 if(!strncmp(name, "tcp:", 4)) {
263 int port = atoi(name + 4);
264 name = strchr(name + 4, ':');
265 if(name == 0) {
266 ret = socket_loopback_client(port, SOCK_STREAM);
267 if (ret >= 0)
268 disable_tcp_nagle(ret);
269 } else {
270#if ADB_HOST
271 adb_mutex_lock(&dns_lock);
272 ret = socket_network_client(name + 1, port, SOCK_STREAM);
273 adb_mutex_unlock(&dns_lock);
274#else
275 return -1;
276#endif
277 }
278#ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
279 } else if(!strncmp(name, "local:", 6)) {
280 ret = socket_local_client(name + 6,
281 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
282 } else if(!strncmp(name, "localreserved:", 14)) {
283 ret = socket_local_client(name + 14,
284 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
285 } else if(!strncmp(name, "localabstract:", 14)) {
286 ret = socket_local_client(name + 14,
287 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
288 } else if(!strncmp(name, "localfilesystem:", 16)) {
289 ret = socket_local_client(name + 16,
290 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
291#endif
292#if ADB_HOST
293 } else if(!strncmp("dns:", name, 4)){
294 char *n = strdup(name + 4);
295 if(n == 0) return -1;
296 ret = create_service_thread(dns_service, n);
297#else /* !ADB_HOST */
298 } else if(!strncmp("dev:", name, 4)) {
299 ret = unix_open(name + 4, O_RDWR);
300 } else if(!strncmp(name, "framebuffer:", 12)) {
301 ret = create_service_thread(framebuffer_service, 0);
302 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
303 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
304 } else if (!strncmp(name, "jdwp:", 5)) {
305 ret = create_jdwp_connection_fd(atoi(name+5));
306 } else if (!strncmp(name, "log:", 4)) {
307 ret = create_service_thread(log_service, get_log_file_path(name + 4));
308#endif
309 } else if(!HOST && !strncmp(name, "shell:", 6)) {
310 if(name[6]) {
311 ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
312 } else {
313 ret = create_subprocess(SHELL_COMMAND, "-", 0);
314 }
315#if !ADB_HOST
316 } else if(!strncmp(name, "sync:", 5)) {
317 ret = create_service_thread(file_sync_service, NULL);
318 } else if(!strncmp(name, "remount:", 8)) {
319 ret = create_service_thread(remount_service, NULL);
The Android Open Source Project9c753402009-03-13 13:04:37 -0700320 } else if(!strncmp(name, "root:", 5)) {
321 ret = create_service_thread(restart_root_service, NULL);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322#endif
323#if 0
324 } else if(!strncmp(name, "echo:", 5)){
325 ret = create_service_thread(echo_service, 0);
326#endif
327 }
328 if (ret >= 0) {
329 close_on_exec(ret);
330 }
331 return ret;
332}
333
334#if ADB_HOST
335struct state_info {
336 transport_type transport;
337 char* serial;
338 int state;
339};
340
341static void wait_for_state(int fd, void* cookie)
342{
343 struct state_info* sinfo = cookie;
344 char* err = "unknown error";
345
346 D("wait_for_state %d\n", sinfo->state);
347
348 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
349 if(t != 0) {
350 writex(fd, "OKAY", 4);
351 } else {
352 sendfailmsg(fd, err);
353 }
354
355 if (sinfo->serial)
356 free(sinfo->serial);
357 free(sinfo);
358 adb_close(fd);
359 D("wait_for_state is done\n");
360}
361#endif
362
363#if ADB_HOST
364asocket* host_service_to_socket(const char* name, const char *serial)
365{
366 if (!strcmp(name,"track-devices")) {
367 return create_device_tracker();
368 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
369 struct state_info* sinfo = malloc(sizeof(struct state_info));
370
371 if (serial)
372 sinfo->serial = strdup(serial);
373 else
374 sinfo->serial = NULL;
375
376 name += strlen("wait-for-");
377
378 if (!strncmp(name, "local", strlen("local"))) {
379 sinfo->transport = kTransportLocal;
380 sinfo->state = CS_DEVICE;
381 } else if (!strncmp(name, "usb", strlen("usb"))) {
382 sinfo->transport = kTransportUsb;
383 sinfo->state = CS_DEVICE;
384 } else if (!strncmp(name, "any", strlen("any"))) {
385 sinfo->transport = kTransportAny;
386 sinfo->state = CS_DEVICE;
387 } else {
388 free(sinfo);
389 return NULL;
390 }
391
392 int fd = create_service_thread(wait_for_state, sinfo);
393 return create_local_socket(fd);
394 }
395 return NULL;
396}
397#endif /* ADB_HOST */