blob: a6aa321cadb7101461de7296e83cc77f0f7e6d4d [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
Josh Gao2aa0d3a2017-04-13 12:45:55 -070024#include <thread>
25
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"
Elliott Hughes24eb8a02016-06-15 15:12:17 -070029#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080030
Elliott Hughesba45ddf2015-04-27 18:39:27 -070031static void sideload_host_service(int sfd, void* data) {
Yabin Cuic8a3c802015-09-29 18:05:30 -070032 char* args = reinterpret_cast<char*>(data);
Elliott Hughesba45ddf2015-04-27 18:39:27 -070033 int file_size;
34 int block_size;
35 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
36 printf("bad sideload-host arguments: %s\n", args);
37 exit(1);
38 }
Yabin Cuic8a3c802015-09-29 18:05:30 -070039 free(args);
Doug Zongker9270a202012-01-09 15:16:13 -080040
Elliott Hughesba45ddf2015-04-27 18:39:27 -070041 printf("sideload-host file size %d block size %d\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080042
Doug Zongker18a78e02014-07-10 07:31:46 -070043 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080044
Doug Zongker075ad802014-06-26 15:35:51 -070045 printf("sideload_host finished\n");
46 sleep(1);
47 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080048}
49
Elliott Hughes921431f2015-05-05 14:37:53 -070050static int create_service_thread(void (*func)(int, void *), void *cookie) {
Doug Zongker9270a202012-01-09 15:16:13 -080051 int s[2];
Elliott Hughes24eb8a02016-06-15 15:12:17 -070052 if (adb_socketpair(s)) {
Doug Zongker9270a202012-01-09 15:16:13 -080053 printf("cannot create service socket pair\n");
54 return -1;
55 }
56
Josh Gao2aa0d3a2017-04-13 12:45:55 -070057 std::thread([s, func, cookie]() { func(s[1], cookie); }).detach();
Doug Zongker9270a202012-01-09 15:16:13 -080058
Yabin Cui7c913e52015-09-23 16:03:11 -070059 VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
Doug Zongker9270a202012-01-09 15:16:13 -080060 return s[0];
61}
62
David Pursellc3d4d532015-08-25 12:50:47 -070063int service_to_fd(const char* name, const atransport* transport) {
Doug Zongker9270a202012-01-09 15:16:13 -080064 int ret = -1;
65
66 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -070067 // this exit status causes recovery to print a special error
68 // message saying to use a newer adb (that supports
69 // sideload-host).
70 exit(3);
71 } else if (!strncmp(name, "sideload-host:", 14)) {
Yabin Cuic8a3c802015-09-29 18:05:30 -070072 char* arg = strdup(name + 14);
73 ret = create_service_thread(sideload_host_service, arg);
Doug Zongker9270a202012-01-09 15:16:13 -080074 }
75 if (ret >= 0) {
76 close_on_exec(ret);
77 }
78 return ret;
79}