services.c revision cc1de48dcdf06c76ee14abbe2a237aa51b5b3bad
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#else
35#include <sys/poll.h>
36#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);
66    if(hp == 0) {
67        writex(fd, &zero, 4);
68    } else {
69        writex(fd, hp->h_addr, 4);
70    }
71    adb_mutex_unlock(&dns_lock);
72    adb_close(fd);
73}
74#else
75extern int recovery_mode;
76
77static void recover_service(int s, void *cookie)
78{
79    unsigned char buf[4096];
80    unsigned count = (unsigned) cookie;
81    int fd;
82
83    fd = adb_creat("/tmp/update", 0644);
84    if(fd < 0) {
85        adb_close(s);
86        return;
87    }
88
89    while(count > 0) {
90        unsigned xfer = (count > 4096) ? 4096 : count;
91        if(readx(s, buf, xfer)) break;
92        if(writex(fd, buf, xfer)) break;
93        count -= xfer;
94    }
95
96    if(count == 0) {
97        writex(s, "OKAY", 4);
98    } else {
99        writex(s, "FAIL", 4);
100    }
101    adb_close(fd);
102    adb_close(s);
103
104    fd = adb_creat("/tmp/update.begin", 0644);
105    adb_close(fd);
106}
107
108void restart_root_service(int fd, void *cookie)
109{
110    char buf[100];
111    char value[PROPERTY_VALUE_MAX];
112
113    if (getuid() == 0) {
114        snprintf(buf, sizeof(buf), "adbd is already running as root\n");
115        writex(fd, buf, strlen(buf));
116        adb_close(fd);
117    } else {
118        property_get("ro.debuggable", value, "");
119        if (strcmp(value, "1") != 0) {
120            snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
121            writex(fd, buf, strlen(buf));
122            return;
123        }
124
125        property_set("service.adb.root", "1");
126        snprintf(buf, sizeof(buf), "restarting adbd as root\n");
127        writex(fd, buf, strlen(buf));
128        adb_close(fd);
129
130        // quit, and init will restart us as root
131        sleep(1);
132        exit(1);
133    }
134}
135
136#endif
137
138#if 0
139static void echo_service(int fd, void *cookie)
140{
141    char buf[4096];
142    int r;
143    char *p;
144    int c;
145
146    for(;;) {
147        r = read(fd, buf, 4096);
148        if(r == 0) goto done;
149        if(r < 0) {
150            if(errno == EINTR) continue;
151            else goto done;
152        }
153
154        c = r;
155        p = buf;
156        while(c > 0) {
157            r = write(fd, p, c);
158            if(r > 0) {
159                c -= r;
160                p += r;
161                continue;
162            }
163            if((r < 0) && (errno == EINTR)) continue;
164            goto done;
165        }
166    }
167done:
168    close(fd);
169}
170#endif
171
172static int create_service_thread(void (*func)(int, void *), void *cookie)
173{
174    stinfo *sti;
175    adb_thread_t t;
176    int s[2];
177
178    if(adb_socketpair(s)) {
179        printf("cannot create service socket pair\n");
180        return -1;
181    }
182
183    sti = malloc(sizeof(stinfo));
184    if(sti == 0) fatal("cannot allocate stinfo");
185    sti->func = func;
186    sti->cookie = cookie;
187    sti->fd = s[1];
188
189    if(adb_thread_create( &t, service_bootstrap_func, sti)){
190        free(sti);
191        adb_close(s[0]);
192        adb_close(s[1]);
193        printf("cannot create service thread\n");
194        return -1;
195    }
196
197    D("service thread started, %d:%d\n",s[0], s[1]);
198    return s[0];
199}
200
201#if !ADB_HOST
202static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
203{
204    char *devname;
205    int ptm;
206    pid_t pid;
207
208    ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
209    if(ptm < 0){
210        printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
211        return -1;
212    }
213    fcntl(ptm, F_SETFD, FD_CLOEXEC);
214
215    if(grantpt(ptm) || unlockpt(ptm) ||
216       ((devname = (char*) ptsname(ptm)) == 0)){
217        printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
218        return -1;
219    }
220
221    pid = fork();
222    if(pid < 0) {
223        printf("- fork failed: %s -\n", strerror(errno));
224        return -1;
225    }
226
227    if(pid == 0){
228        int pts;
229
230        setsid();
231
232        pts = unix_open(devname, O_RDWR);
233        if(pts < 0) exit(-1);
234
235        dup2(pts, 0);
236        dup2(pts, 1);
237        dup2(pts, 2);
238
239        adb_close(ptm);
240
241        execl(cmd, cmd, arg0, arg1, NULL);
242        fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
243                cmd, strerror(errno), errno);
244        exit(-1);
245    } else {
246        // set child's OOM adjustment to zero
247        char text[64];
248        snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
249        int fd = adb_open(text, O_WRONLY);
250        if (fd >= 0) {
251            adb_write(fd, "0", 1);
252            adb_close(fd);
253        } else {
254           D("adb: unable to open %s\n", text);
255        }
256
257        return ptm;
258    }
259}
260#endif /* !ADB_HOST */
261
262#if ADB_HOST
263#define SHELL_COMMAND "/bin/sh"
264#else
265#define SHELL_COMMAND "/system/bin/sh"
266#endif
267
268#if !ADB_HOST
269static void shell_service(int s, void *command)
270{
271    char    buffer[MAX_PAYLOAD];
272    char    buffer2[MAX_PAYLOAD];
273    struct pollfd ufds[2];
274    int     fd, ret = 0;
275    unsigned count = 0;
276    char** args = (char **)command;
277    fd = create_subprocess(SHELL_COMMAND, args[0], args[1]);
278
279    while (1) {
280        while (count < sizeof(buffer)) {
281            ufds[0].fd = fd;
282            ufds[0].events = POLLIN | POLLHUP;
283            ufds[0].revents = 0;
284            ufds[1].fd = s;
285            ufds[1].events = POLLIN | POLLHUP;
286            ufds[1].revents = 0;
287            // use a 100ms timeout so we don't block indefinitely with our
288            // buffer partially filled.
289            ret = poll(ufds, 2, 100);
290            if (ret <= 0) {
291                D("poll returned %d\n", ret);
292                // file has closed or we timed out
293                // set ret to 1 so we don't exit the outer loop
294                ret = 1;
295                break;
296            }
297
298            if (ufds[0].revents & POLLIN) {
299                ret = adb_read(fd, buffer + count, sizeof(buffer) - count);
300                D("read fd ret: %d, count: %d\n", ret, count);
301                if (ret > 0)
302                    count += ret;
303                else
304                    break;
305            }
306            if (ufds[1].revents & POLLIN) {
307                ret = adb_read(s, buffer2, sizeof(buffer2));
308                D("read s ret: %d\n", ret);
309                if (ret > 0)
310                    adb_write(fd, buffer2, ret);
311                else
312                    break;
313            }
314
315            if ((ufds[0].revents & POLLHUP) || (ufds[1].revents & POLLHUP)) {
316                // set flag to exit after flushing the buffer
317                ret = -1;
318                break;
319            }
320        }
321
322        D("writing: %d\n", count);
323        if (count > 0) {
324            adb_write(s, buffer, count);
325            count = 0;
326        }
327        if (ret <= 0)
328            break;
329    }
330
331    D("shell_service done\n");
332
333    adb_close(fd);
334    adb_close(s);
335}
336#endif // !ADB_HOST
337
338int service_to_fd(const char *name)
339{
340    int ret = -1;
341
342    if(!strncmp(name, "tcp:", 4)) {
343        int port = atoi(name + 4);
344        name = strchr(name + 4, ':');
345        if(name == 0) {
346            ret = socket_loopback_client(port, SOCK_STREAM);
347            if (ret >= 0)
348                disable_tcp_nagle(ret);
349        } else {
350#if ADB_HOST
351            adb_mutex_lock(&dns_lock);
352            ret = socket_network_client(name + 1, port, SOCK_STREAM);
353            adb_mutex_unlock(&dns_lock);
354#else
355            return -1;
356#endif
357        }
358#ifndef HAVE_WINSOCK   /* winsock doesn't implement unix domain sockets */
359    } else if(!strncmp(name, "local:", 6)) {
360        ret = socket_local_client(name + 6,
361                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
362    } else if(!strncmp(name, "localreserved:", 14)) {
363        ret = socket_local_client(name + 14,
364                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
365    } else if(!strncmp(name, "localabstract:", 14)) {
366        ret = socket_local_client(name + 14,
367                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
368    } else if(!strncmp(name, "localfilesystem:", 16)) {
369        ret = socket_local_client(name + 16,
370                ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
371#endif
372#if ADB_HOST
373    } else if(!strncmp("dns:", name, 4)){
374        char *n = strdup(name + 4);
375        if(n == 0) return -1;
376        ret = create_service_thread(dns_service, n);
377#else /* !ADB_HOST */
378    } else if(!strncmp("dev:", name, 4)) {
379        ret = unix_open(name + 4, O_RDWR);
380    } else if(!strncmp(name, "framebuffer:", 12)) {
381        ret = create_service_thread(framebuffer_service, 0);
382    } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
383        ret = create_service_thread(recover_service, (void*) atoi(name + 8));
384    } else if (!strncmp(name, "jdwp:", 5)) {
385        ret = create_jdwp_connection_fd(atoi(name+5));
386    } else if (!strncmp(name, "log:", 4)) {
387        ret = create_service_thread(log_service, get_log_file_path(name + 4));
388    } else if(!HOST && !strncmp(name, "shell:", 6)) {
389        const char* args[2];
390        if(name[6]) {
391            args[0] = "-c";
392            args[1] = name + 6;
393        } else {
394            args[0] = "-";
395            args[1] = 0;
396        }
397        ret = create_service_thread(shell_service, (void *)args);
398    } else if(!strncmp(name, "sync:", 5)) {
399        ret = create_service_thread(file_sync_service, NULL);
400    } else if(!strncmp(name, "remount:", 8)) {
401        ret = create_service_thread(remount_service, NULL);
402    } else if(!strncmp(name, "root:", 5)) {
403        ret = create_service_thread(restart_root_service, NULL);
404#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 */
480