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 <errno.h>
18 #include <inttypes.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <string>
25 #include <thread>
26
27 #include "adb.h"
28 #include "fdevent.h"
29 #include "fuse_adb_provider.h"
30 #include "sysdeps.h"
31
sideload_host_service(int sfd,const std::string & args)32 static void sideload_host_service(int sfd, const std::string& args) {
33 int file_size;
34 int block_size;
35 if (sscanf(args.c_str(), "%d:%d", &file_size, &block_size) != 2) {
36 printf("bad sideload-host arguments: %s\n", args.c_str());
37 exit(1);
38 }
39
40 printf("sideload-host file size %d block size %d\n", file_size, block_size);
41
42 int result = run_adb_fuse(sfd, file_size, block_size);
43
44 printf("sideload_host finished\n");
45 exit(result == 0 ? 0 : 1);
46 }
47
create_service_thread(void (* func)(int,const std::string &),const std::string & args)48 static int create_service_thread(void (*func)(int, const std::string&), const std::string& args) {
49 int s[2];
50 if (adb_socketpair(s)) {
51 printf("cannot create service socket pair\n");
52 return -1;
53 }
54
55 std::thread([s, func, args]() { func(s[1], args); }).detach();
56
57 VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
58 return s[0];
59 }
60
service_to_fd(const char * name,atransport *)61 int service_to_fd(const char* name, atransport* /* transport */) {
62 int ret = -1;
63
64 if (!strncmp(name, "sideload:", 9)) {
65 // this exit status causes recovery to print a special error
66 // message saying to use a newer adb (that supports
67 // sideload-host).
68 exit(3);
69 } else if (!strncmp(name, "sideload-host:", 14)) {
70 std::string arg(name + 14);
71 ret = create_service_thread(sideload_host_service, arg);
72 }
73 if (ret >= 0) {
74 close_on_exec(ret);
75 }
76 return ret;
77 }
78