services.c revision 249ad57a887680538d1dc0195e746b1d877ebd6a
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
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
106void 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
134#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#if !ADB_HOST
248        // set child's OOM adjustment to zero
249        char text[64];
250        snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
251        int fd = adb_open(text, O_WRONLY);
252        if (fd >= 0) {
253            adb_write(fd, "0", 1);
254            adb_close(fd);
255        } else {
256           D("adb: unable to open %s\n", text);
257        }
258#endif
259        return ptm;
260    }
261#endif /* !HAVE_WIN32_PROC */
262}
263
264#if ADB_HOST
265#define SHELL_COMMAND "/bin/sh"
266#else
267#define SHELL_COMMAND "/system/bin/sh"
268#endif
269
270int service_to_fd(const char *name)
271{
272    int ret = -1;
273
274    if(!strncmp(name, "tcp:", 4)) {
275        int port = atoi(name + 4);
276        name = strchr(name + 4, ':');
277        if(name == 0) {
278            ret = socket_loopback_client(port, SOCK_STREAM);
279            if (ret >= 0)
280                disable_tcp_nagle(ret);
281        } else {
282#if ADB_HOST
283            adb_mutex_lock(&dns_lock);
284            ret = socket_network_client(name + 1, port, SOCK_STREAM);
285            adb_mutex_unlock(&dns_lock);
286#else
287            return -1;
288#endif
289        }
290#ifndef HAVE_WINSOCK   /* winsock doesn't implement unix domain sockets */
291    } else if(!strncmp(name, "local:", 6)) {
292        ret = socket_local_client(name + 6,
293                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
294    } else if(!strncmp(name, "localreserved:", 14)) {
295        ret = socket_local_client(name + 14,
296                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
297    } else if(!strncmp(name, "localabstract:", 14)) {
298        ret = socket_local_client(name + 14,
299                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
300    } else if(!strncmp(name, "localfilesystem:", 16)) {
301        ret = socket_local_client(name + 16,
302                ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
303#endif
304#if ADB_HOST
305    } else if(!strncmp("dns:", name, 4)){
306        char *n = strdup(name + 4);
307        if(n == 0) return -1;
308        ret = create_service_thread(dns_service, n);
309#else /* !ADB_HOST */
310    } else if(!strncmp("dev:", name, 4)) {
311        ret = unix_open(name + 4, O_RDWR);
312    } else if(!strncmp(name, "framebuffer:", 12)) {
313        ret = create_service_thread(framebuffer_service, 0);
314    } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
315        ret = create_service_thread(recover_service, (void*) atoi(name + 8));
316    } else if (!strncmp(name, "jdwp:", 5)) {
317        ret = create_jdwp_connection_fd(atoi(name+5));
318    } else if (!strncmp(name, "log:", 4)) {
319        ret = create_service_thread(log_service, get_log_file_path(name + 4));
320#endif
321    } else if(!HOST && !strncmp(name, "shell:", 6)) {
322        if(name[6]) {
323            ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
324        } else {
325            ret = create_subprocess(SHELL_COMMAND, "-", 0);
326        }
327#if !ADB_HOST
328    } else if(!strncmp(name, "sync:", 5)) {
329        ret = create_service_thread(file_sync_service, NULL);
330    } else if(!strncmp(name, "remount:", 8)) {
331        ret = create_service_thread(remount_service, NULL);
332    } else if(!strncmp(name, "root:", 5)) {
333        ret = create_service_thread(restart_root_service, NULL);
334#endif
335#if 0
336    } else if(!strncmp(name, "echo:", 5)){
337        ret = create_service_thread(echo_service, 0);
338#endif
339    }
340    if (ret >= 0) {
341        close_on_exec(ret);
342    }
343    return ret;
344}
345
346#if ADB_HOST
347struct state_info {
348    transport_type transport;
349    char* serial;
350    int state;
351};
352
353static void wait_for_state(int fd, void* cookie)
354{
355    struct state_info* sinfo = cookie;
356    char* err = "unknown error";
357
358    D("wait_for_state %d\n", sinfo->state);
359
360    atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
361    if(t != 0) {
362        writex(fd, "OKAY", 4);
363    } else {
364        sendfailmsg(fd, err);
365    }
366
367    if (sinfo->serial)
368        free(sinfo->serial);
369    free(sinfo);
370    adb_close(fd);
371    D("wait_for_state is done\n");
372}
373#endif
374
375#if ADB_HOST
376asocket*  host_service_to_socket(const char*  name, const char *serial)
377{
378    if (!strcmp(name,"track-devices")) {
379        return create_device_tracker();
380    } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
381        struct state_info* sinfo = malloc(sizeof(struct state_info));
382
383        if (serial)
384            sinfo->serial = strdup(serial);
385        else
386            sinfo->serial = NULL;
387
388        name += strlen("wait-for-");
389
390        if (!strncmp(name, "local", strlen("local"))) {
391            sinfo->transport = kTransportLocal;
392            sinfo->state = CS_DEVICE;
393        } else if (!strncmp(name, "usb", strlen("usb"))) {
394            sinfo->transport = kTransportUsb;
395            sinfo->state = CS_DEVICE;
396        } else if (!strncmp(name, "any", strlen("any"))) {
397            sinfo->transport = kTransportAny;
398            sinfo->state = CS_DEVICE;
399        } else {
400            free(sinfo);
401            return NULL;
402        }
403
404        int fd = create_service_thread(wait_for_state, sinfo);
405        return create_local_socket(fd);
406    }
407    return NULL;
408}
409#endif /* ADB_HOST */
410