blob: 357c222b4164fc80f0584c787dba0d93eb0c3d86 [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -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#include "fdevent.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070025#include "fuse_adb_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080026
27#define TRACE_TAG TRACE_SERVICES
28#include "adb.h"
29
30typedef struct stinfo stinfo;
31
32struct stinfo {
33 void (*func)(int fd, void *cookie);
34 int fd;
35 void *cookie;
36};
37
38
39void *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
Doug Zongker075ad802014-06-26 15:35:51 -070047static void sideload_host_service(int sfd, void* cookie)
Doug Zongker9270a202012-01-09 15:16:13 -080048{
Doug Zongker075ad802014-06-26 15:35:51 -070049 char* saveptr;
Dan Albert1ddd3502015-02-18 15:58:15 -080050 const char* s = adb_strtok_r(cookie, ":", &saveptr);
Doug Zongker075ad802014-06-26 15:35:51 -070051 uint64_t file_size = strtoull(s, NULL, 10);
Dan Albert1ddd3502015-02-18 15:58:15 -080052 s = adb_strtok_r(NULL, ":", &saveptr);
Doug Zongker075ad802014-06-26 15:35:51 -070053 uint32_t block_size = strtoul(s, NULL, 10);
Doug Zongker9270a202012-01-09 15:16:13 -080054
Doug Zongker075ad802014-06-26 15:35:51 -070055 printf("sideload-host file size %llu block size %lu\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080056
Doug Zongker18a78e02014-07-10 07:31:46 -070057 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080058
Doug Zongker075ad802014-06-26 15:35:51 -070059 printf("sideload_host finished\n");
60 sleep(1);
61 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080062}
63
Doug Zongker9270a202012-01-09 15:16:13 -080064static int create_service_thread(void (*func)(int, void *), void *cookie)
65{
66 stinfo *sti;
67 adb_thread_t t;
68 int s[2];
69
70 if(adb_socketpair(s)) {
71 printf("cannot create service socket pair\n");
72 return -1;
73 }
74
75 sti = malloc(sizeof(stinfo));
76 if(sti == 0) fatal("cannot allocate stinfo");
77 sti->func = func;
78 sti->cookie = cookie;
79 sti->fd = s[1];
80
81 if(adb_thread_create( &t, service_bootstrap_func, sti)){
82 free(sti);
83 adb_close(s[0]);
84 adb_close(s[1]);
85 printf("cannot create service thread\n");
86 return -1;
87 }
88
89 D("service thread started, %d:%d\n",s[0], s[1]);
90 return s[0];
91}
92
93int service_to_fd(const char *name)
94{
95 int ret = -1;
96
97 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -070098 // this exit status causes recovery to print a special error
99 // message saying to use a newer adb (that supports
100 // sideload-host).
101 exit(3);
102 } else if (!strncmp(name, "sideload-host:", 14)) {
103 ret = create_service_thread(sideload_host_service, (void*)(name + 14));
Doug Zongker9270a202012-01-09 15:16:13 -0800104 }
105 if (ret >= 0) {
106 close_on_exec(ret);
107 }
108 return ret;
109}