Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 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 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 23 | |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 24 | #include <string> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 25 | #include <thread> |
| 26 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 27 | #include "adb.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 28 | #include "fdevent.h" |
| 29 | #include "fuse_adb_provider.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 30 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 31 | |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 32 | static void sideload_host_service(int sfd, const std::string& args) { |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 33 | int file_size; |
| 34 | int block_size; |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 35 | if (sscanf(args.c_str(), "%d:%d", &file_size, &block_size) != 2) { |
| 36 | printf("bad sideload-host arguments: %s\n", args.c_str()); |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 37 | exit(1); |
| 38 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | ba45ddf | 2015-04-27 18:39:27 -0700 | [diff] [blame] | 40 | printf("sideload-host file size %d block size %d\n", file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 41 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 42 | int result = run_adb_fuse(sfd, file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 43 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 44 | printf("sideload_host finished\n"); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 45 | exit(result == 0 ? 0 : 1); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 48 | static int create_service_thread(void (*func)(int, const std::string&), const std::string& args) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 49 | int s[2]; |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 50 | if (adb_socketpair(s)) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 51 | printf("cannot create service socket pair\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 55 | std::thread([s, func, args]() { func(s[1], args); }).detach(); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 56 | |
Yabin Cui | 7c913e5 | 2015-09-23 16:03:11 -0700 | [diff] [blame] | 57 | VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1]; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 58 | return s[0]; |
| 59 | } |
| 60 | |
David Pursell | c3d4d53 | 2015-08-25 12:50:47 -0700 | [diff] [blame] | 61 | int service_to_fd(const char* name, const atransport* transport) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 62 | int ret = -1; |
| 63 | |
| 64 | if (!strncmp(name, "sideload:", 9)) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 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)) { |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 70 | std::string arg(name + 14); |
Yabin Cui | c8a3c80 | 2015-09-29 18:05:30 -0700 | [diff] [blame] | 71 | ret = create_service_thread(sideload_host_service, arg); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 72 | } |
| 73 | if (ret >= 0) { |
| 74 | close_on_exec(ret); |
| 75 | } |
| 76 | return ret; |
| 77 | } |