Home | History | Annotate | Download | only in minadbd
      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 #include "fdevent.h"
     25 #include "fuse_adb_provider.h"
     26 
     27 #define  TRACE_TAG  TRACE_SERVICES
     28 #include "adb.h"
     29 
     30 typedef struct stinfo stinfo;
     31 
     32 struct stinfo {
     33     void (*func)(int fd, void *cookie);
     34     int fd;
     35     void *cookie;
     36 };
     37 
     38 
     39 void *service_bootstrap_func(void *x)
     40 {
     41     stinfo *sti = x;
     42     sti->func(sti->fd, sti->cookie);
     43     free(sti);
     44     return 0;
     45 }
     46 
     47 static void sideload_host_service(int sfd, void* cookie)
     48 {
     49     char* saveptr;
     50     const char* s = strtok_r(cookie, ":", &saveptr);
     51     uint64_t file_size = strtoull(s, NULL, 10);
     52     s = strtok_r(NULL, ":", &saveptr);
     53     uint32_t block_size = strtoul(s, NULL, 10);
     54 
     55     printf("sideload-host file size %llu block size %lu\n", file_size, block_size);
     56 
     57     int result = run_adb_fuse(sfd, file_size, block_size);
     58 
     59     printf("sideload_host finished\n");
     60     sleep(1);
     61     exit(result == 0 ? 0 : 1);
     62 }
     63 
     64 #if 0
     65 static void echo_service(int fd, void *cookie)
     66 {
     67     char buf[4096];
     68     int r;
     69     char *p;
     70     int c;
     71 
     72     for(;;) {
     73         r = read(fd, buf, 4096);
     74         if(r == 0) goto done;
     75         if(r < 0) {
     76             if(errno == EINTR) continue;
     77             else goto done;
     78         }
     79 
     80         c = r;
     81         p = buf;
     82         while(c > 0) {
     83             r = write(fd, p, c);
     84             if(r > 0) {
     85                 c -= r;
     86                 p += r;
     87                 continue;
     88             }
     89             if((r < 0) && (errno == EINTR)) continue;
     90             goto done;
     91         }
     92     }
     93 done:
     94     close(fd);
     95 }
     96 #endif
     97 
     98 static int create_service_thread(void (*func)(int, void *), void *cookie)
     99 {
    100     stinfo *sti;
    101     adb_thread_t t;
    102     int s[2];
    103 
    104     if(adb_socketpair(s)) {
    105         printf("cannot create service socket pair\n");
    106         return -1;
    107     }
    108 
    109     sti = malloc(sizeof(stinfo));
    110     if(sti == 0) fatal("cannot allocate stinfo");
    111     sti->func = func;
    112     sti->cookie = cookie;
    113     sti->fd = s[1];
    114 
    115     if(adb_thread_create( &t, service_bootstrap_func, sti)){
    116         free(sti);
    117         adb_close(s[0]);
    118         adb_close(s[1]);
    119         printf("cannot create service thread\n");
    120         return -1;
    121     }
    122 
    123     D("service thread started, %d:%d\n",s[0], s[1]);
    124     return s[0];
    125 }
    126 
    127 int service_to_fd(const char *name)
    128 {
    129     int ret = -1;
    130 
    131     if (!strncmp(name, "sideload:", 9)) {
    132         // this exit status causes recovery to print a special error
    133         // message saying to use a newer adb (that supports
    134         // sideload-host).
    135         exit(3);
    136     } else if (!strncmp(name, "sideload-host:", 14)) {
    137         ret = create_service_thread(sideload_host_service, (void*)(name + 14));
    138 #if 0
    139     } else if(!strncmp(name, "echo:", 5)){
    140         ret = create_service_thread(echo_service, 0);
    141 #endif
    142     }
    143     if (ret >= 0) {
    144         close_on_exec(ret);
    145     }
    146     return ret;
    147 }
    148