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