blob: 753a14e381934db2da88e43fb272902d59320d4f [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
Doug Zongker9270a202012-01-09 15:16:13 -080017#include <errno.h>
Dan Albert8f6eb5c2015-02-24 22:07:18 -080018#include <inttypes.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080023
24#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080025
Doug Zongker9270a202012-01-09 15:16:13 -080026#include "adb.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080027#include "fdevent.h"
28#include "fuse_adb_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080029
30typedef struct stinfo stinfo;
31
32struct stinfo {
33 void (*func)(int fd, void *cookie);
34 int fd;
35 void *cookie;
36};
37
Josh Gaocd324762016-02-12 15:00:52 -080038void service_bootstrap_func(void* x) {
Elliott Hughes20531ef2015-04-10 13:59:19 -070039 stinfo* sti = reinterpret_cast<stinfo*>(x);
Doug Zongker9270a202012-01-09 15:16:13 -080040 sti->func(sti->fd, sti->cookie);
41 free(sti);
Doug Zongker9270a202012-01-09 15:16:13 -080042}
43
Elliott Hughesba45ddf2015-04-27 18:39:27 -070044static void sideload_host_service(int sfd, void* data) {
Yabin Cuic8a3c802015-09-29 18:05:30 -070045 char* args = reinterpret_cast<char*>(data);
Elliott Hughesba45ddf2015-04-27 18:39:27 -070046 int file_size;
47 int block_size;
48 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
49 printf("bad sideload-host arguments: %s\n", args);
50 exit(1);
51 }
Yabin Cuic8a3c802015-09-29 18:05:30 -070052 free(args);
Doug Zongker9270a202012-01-09 15:16:13 -080053
Elliott Hughesba45ddf2015-04-27 18:39:27 -070054 printf("sideload-host file size %d block size %d\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080055
Doug Zongker18a78e02014-07-10 07:31:46 -070056 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080057
Doug Zongker075ad802014-06-26 15:35:51 -070058 printf("sideload_host finished\n");
59 sleep(1);
60 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080061}
62
Elliott Hughes921431f2015-05-05 14:37:53 -070063static int create_service_thread(void (*func)(int, void *), void *cookie) {
Doug Zongker9270a202012-01-09 15:16:13 -080064 int s[2];
Doug Zongker9270a202012-01-09 15:16:13 -080065 if(adb_socketpair(s)) {
66 printf("cannot create service socket pair\n");
67 return -1;
68 }
69
Elliott Hughes20531ef2015-04-10 13:59:19 -070070 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
Doug Zongker9270a202012-01-09 15:16:13 -080071 if(sti == 0) fatal("cannot allocate stinfo");
72 sti->func = func;
73 sti->cookie = cookie;
74 sti->fd = s[1];
75
Ethan Yonker534d4e02016-08-26 10:05:03 -050076#if PLATFORM_SDK_VERSION == 23
77 adb_thread_t t;
78 if (adb_thread_create( &t, (adb_thread_func_t)service_bootstrap_func, sti)){
79#else
Elliott Hughes921431f2015-05-05 14:37:53 -070080 if (!adb_thread_create(service_bootstrap_func, sti)) {
Ethan Yonker534d4e02016-08-26 10:05:03 -050081#endif
Doug Zongker9270a202012-01-09 15:16:13 -080082 free(sti);
83 adb_close(s[0]);
84 adb_close(s[1]);
85 printf("cannot create service thread\n");
86 return -1;
87 }
88
Ethan Yonker534d4e02016-08-26 10:05:03 -050089 //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
Doug Zongker9270a202012-01-09 15:16:13 -080090 return s[0];
91}
92
David Pursellc3d4d532015-08-25 12:50:47 -070093int service_to_fd(const char* name, const atransport* transport) {
Doug Zongker9270a202012-01-09 15:16:13 -080094 int ret = -1;
95
96 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -070097 // this exit status causes recovery to print a special error
98 // message saying to use a newer adb (that supports
99 // sideload-host).
100 exit(3);
101 } else if (!strncmp(name, "sideload-host:", 14)) {
Yabin Cuic8a3c802015-09-29 18:05:30 -0700102 char* arg = strdup(name + 14);
103 ret = create_service_thread(sideload_host_service, arg);
Doug Zongker9270a202012-01-09 15:16:13 -0800104 }
105 if (ret >= 0) {
106 close_on_exec(ret);
107 }
108 return ret;
109}
Ethan Yonker534d4e02016-08-26 10:05:03 -0500110
111#if PLATFORM_SDK_VERSION == 23
112int service_to_fd(const char* name) {
113 atransport transport;
114 return service_to_fd(name, &transport);
115}
116#endif