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 | |
| 24 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 25 | |
| 26 | #define TRACE_TAG TRACE_SERVICES |
| 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" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 30 | |
| 31 | typedef struct stinfo stinfo; |
| 32 | |
| 33 | struct stinfo { |
| 34 | void (*func)(int fd, void *cookie); |
| 35 | int fd; |
| 36 | void *cookie; |
| 37 | }; |
| 38 | |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 39 | void* service_bootstrap_func(void* x) { |
| 40 | stinfo* sti = reinterpret_cast<stinfo*>(x); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 41 | sti->func(sti->fd, sti->cookie); |
| 42 | free(sti); |
| 43 | return 0; |
| 44 | } |
| 45 | |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 46 | static void sideload_host_service(int sfd, void* cookie) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 47 | char* saveptr; |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 48 | const char* s = adb_strtok_r(reinterpret_cast<char*>(cookie), ":", &saveptr); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 49 | uint64_t file_size = strtoull(s, NULL, 10); |
Dan Albert | 1ddd350 | 2015-02-18 15:58:15 -0800 | [diff] [blame] | 50 | s = adb_strtok_r(NULL, ":", &saveptr); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 51 | uint32_t block_size = strtoul(s, NULL, 10); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 52 | |
Narayan Kamath | 017db6d | 2015-02-25 15:18:24 +0000 | [diff] [blame] | 53 | printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", |
| 54 | file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 55 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 56 | int result = run_adb_fuse(sfd, file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 57 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 58 | printf("sideload_host finished\n"); |
| 59 | sleep(1); |
| 60 | exit(result == 0 ? 0 : 1); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 63 | static int create_service_thread(void (*func)(int, void *), void *cookie) |
| 64 | { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 65 | int s[2]; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 66 | if(adb_socketpair(s)) { |
| 67 | printf("cannot create service socket pair\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 71 | stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo))); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 72 | if(sti == 0) fatal("cannot allocate stinfo"); |
| 73 | sti->func = func; |
| 74 | sti->cookie = cookie; |
| 75 | sti->fd = s[1]; |
| 76 | |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 77 | adb_thread_t t; |
| 78 | if (adb_thread_create( &t, service_bootstrap_func, sti)){ |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 79 | free(sti); |
| 80 | adb_close(s[0]); |
| 81 | adb_close(s[1]); |
| 82 | printf("cannot create service thread\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | D("service thread started, %d:%d\n",s[0], s[1]); |
| 87 | return s[0]; |
| 88 | } |
| 89 | |
Elliott Hughes | 20531ef | 2015-04-10 13:59:19 -0700 | [diff] [blame] | 90 | int service_to_fd(const char* name) { |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 91 | int ret = -1; |
| 92 | |
| 93 | if (!strncmp(name, "sideload:", 9)) { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 94 | // this exit status causes recovery to print a special error |
| 95 | // message saying to use a newer adb (that supports |
| 96 | // sideload-host). |
| 97 | exit(3); |
| 98 | } else if (!strncmp(name, "sideload-host:", 14)) { |
| 99 | ret = create_service_thread(sideload_host_service, (void*)(name + 14)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 100 | } |
| 101 | if (ret >= 0) { |
| 102 | close_on_exec(ret); |
| 103 | } |
| 104 | return ret; |
| 105 | } |