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