services.c revision 95ef82866c7a922bf588027aa38c58a45eb84d9c
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/reboot.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    free(cookie);
67    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
109void 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));
123            adb_close(fd);
124            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
138void 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)
177{
178    char buf[100];
179    int pid, ret;
180
181    sync();
182
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
196    ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
197                    LINUX_REBOOT_CMD_RESTART2, (char *)arg);
198    if (ret < 0) {
199        snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno));
200        writex(fd, buf, strlen(buf));
201    }
202    free(arg);
203    adb_close(fd);
204}
205
206#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{
273#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 */
277    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(pts);
313        adb_close(ptm);
314
315        execl(cmd, cmd, arg0, arg1, NULL);
316        fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
317                cmd, strerror(errno), errno);
318        exit(-1);
319    } else {
320#if !ADB_HOST
321        // set child's OOM adjustment to zero
322        char text[64];
323        snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
324        int fd = adb_open(text, O_WRONLY);
325        if (fd >= 0) {
326            adb_write(fd, "0", 1);
327            adb_close(fd);
328        } else {
329           D("adb: unable to open %s\n", text);
330        }
331#endif
332        return ptm;
333    }
334#endif /* !HAVE_WIN32_PROC */
335}
336
337#if ADB_HOST
338#define SHELL_COMMAND "/bin/sh"
339#else
340#define SHELL_COMMAND "/system/bin/sh"
341#endif
342
343int service_to_fd(const char *name)
344{
345    int ret = -1;
346
347    if(!strncmp(name, "tcp:", 4)) {
348        int port = atoi(name + 4);
349        name = strchr(name + 4, ':');
350        if(name == 0) {
351            ret = socket_loopback_client(port, SOCK_STREAM);
352            if (ret >= 0)
353                disable_tcp_nagle(ret);
354        } else {
355#if ADB_HOST
356            adb_mutex_lock(&dns_lock);
357            ret = socket_network_client(name + 1, port, SOCK_STREAM);
358            adb_mutex_unlock(&dns_lock);
359#else
360            return -1;
361#endif
362        }
363#ifndef HAVE_WINSOCK   /* winsock doesn't implement unix domain sockets */
364    } else if(!strncmp(name, "local:", 6)) {
365        ret = socket_local_client(name + 6,
366                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
367    } else if(!strncmp(name, "localreserved:", 14)) {
368        ret = socket_local_client(name + 14,
369                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
370    } else if(!strncmp(name, "localabstract:", 14)) {
371        ret = socket_local_client(name + 14,
372                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
373    } else if(!strncmp(name, "localfilesystem:", 16)) {
374        ret = socket_local_client(name + 16,
375                ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
376#endif
377#if ADB_HOST
378    } else if(!strncmp("dns:", name, 4)){
379        char *n = strdup(name + 4);
380        if(n == 0) return -1;
381        ret = create_service_thread(dns_service, n);
382#else /* !ADB_HOST */
383    } else if(!strncmp("dev:", name, 4)) {
384        ret = unix_open(name + 4, O_RDWR);
385    } else if(!strncmp(name, "framebuffer:", 12)) {
386        ret = create_service_thread(framebuffer_service, 0);
387    } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
388        ret = create_service_thread(recover_service, (void*) atoi(name + 8));
389    } else if (!strncmp(name, "jdwp:", 5)) {
390        ret = create_jdwp_connection_fd(atoi(name+5));
391    } else if (!strncmp(name, "log:", 4)) {
392        ret = create_service_thread(log_service, get_log_file_path(name + 4));
393#endif
394    } else if(!HOST && !strncmp(name, "shell:", 6)) {
395        if(name[6]) {
396            ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
397        } else {
398            ret = create_subprocess(SHELL_COMMAND, "-", 0);
399        }
400#if !ADB_HOST
401    } else if(!strncmp(name, "sync:", 5)) {
402        ret = create_service_thread(file_sync_service, NULL);
403    } else if(!strncmp(name, "remount:", 8)) {
404        ret = create_service_thread(remount_service, NULL);
405    } else if(!strncmp(name, "reboot:", 7)) {
406        void* arg = strdup(name + 7);
407        if(arg == 0) return -1;
408        ret = create_service_thread(reboot_service, arg);
409    } else if(!strncmp(name, "root:", 5)) {
410        ret = create_service_thread(restart_root_service, NULL);
411    } else if(!strncmp(name, "tcpip:", 6)) {
412        int port;
413        if (sscanf(name + 6, "%d", &port) == 0) {
414            port = 0;
415        }
416        ret = create_service_thread(restart_tcp_service, (void *)port);
417    } else if(!strncmp(name, "usb:", 4)) {
418        ret = create_service_thread(restart_usb_service, NULL);
419#endif
420#if 0
421    } else if(!strncmp(name, "echo:", 5)){
422        ret = create_service_thread(echo_service, 0);
423#endif
424    }
425    if (ret >= 0) {
426        close_on_exec(ret);
427    }
428    return ret;
429}
430
431#if ADB_HOST
432struct state_info {
433    transport_type transport;
434    char* serial;
435    int state;
436};
437
438static void wait_for_state(int fd, void* cookie)
439{
440    struct state_info* sinfo = cookie;
441    char* err = "unknown error";
442
443    D("wait_for_state %d\n", sinfo->state);
444
445    atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
446    if(t != 0) {
447        writex(fd, "OKAY", 4);
448    } else {
449        sendfailmsg(fd, err);
450    }
451
452    if (sinfo->serial)
453        free(sinfo->serial);
454    free(sinfo);
455    adb_close(fd);
456    D("wait_for_state is done\n");
457}
458#endif
459
460#if ADB_HOST
461asocket*  host_service_to_socket(const char*  name, const char *serial)
462{
463    if (!strcmp(name,"track-devices")) {
464        return create_device_tracker();
465    } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
466        struct state_info* sinfo = malloc(sizeof(struct state_info));
467
468        if (serial)
469            sinfo->serial = strdup(serial);
470        else
471            sinfo->serial = NULL;
472
473        name += strlen("wait-for-");
474
475        if (!strncmp(name, "local", strlen("local"))) {
476            sinfo->transport = kTransportLocal;
477            sinfo->state = CS_DEVICE;
478        } else if (!strncmp(name, "usb", strlen("usb"))) {
479            sinfo->transport = kTransportUsb;
480            sinfo->state = CS_DEVICE;
481        } else if (!strncmp(name, "any", strlen("any"))) {
482            sinfo->transport = kTransportAny;
483            sinfo->state = CS_DEVICE;
484        } else {
485            free(sinfo);
486            return NULL;
487        }
488
489        int fd = create_service_thread(wait_for_state, sinfo);
490        return create_local_socket(fd);
491    }
492    return NULL;
493}
494#endif /* ADB_HOST */
495