Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 3 | * Copyright (C) 2019 The LineageOS Project |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 18 | #include "fuse_sdcard_provider.h" |
| 19 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 22 | #include <pthread.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 26 | #include <sys/mount.h> |
| 27 | #include <sys/stat.h> |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 28 | #include <sys/wait.h> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 30 | |
| 31 | #include <functional> |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 32 | |
Tianjie Xu | 71e182b | 2016-08-31 18:06:33 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
| 34 | |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 35 | #include "fuse_sideload.h" |
| 36 | |
| 37 | struct file_data { |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 38 | int fd; // the underlying sdcard file |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 39 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 40 | uint64_t file_size; |
| 41 | uint32_t block_size; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 44 | static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer, |
| 45 | uint32_t fetch_size) { |
| 46 | off64_t offset = static_cast<off64_t>(block) * fd.block_size; |
| 47 | if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) { |
| 48 | fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno)); |
| 49 | return -EIO; |
| 50 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 51 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 52 | if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) { |
| 53 | fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno)); |
| 54 | return -EIO; |
| 55 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 56 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 57 | return 0; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 60 | struct token { |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 61 | pid_t pid; |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 62 | const char* path; |
| 63 | int result; |
| 64 | }; |
| 65 | |
| 66 | static void* run_sdcard_fuse(void* cookie) { |
| 67 | token* t = reinterpret_cast<token*>(cookie); |
| 68 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 69 | struct stat sb; |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 70 | if (stat(t->path, &sb) < 0) { |
| 71 | fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno)); |
| 72 | t->result = -1; |
| 73 | return nullptr; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 74 | } |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 75 | |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 76 | struct file_data fd; |
| 77 | struct provider_vtab vtab; |
| 78 | |
| 79 | fd.fd = open(t->path, O_RDONLY); |
| 80 | if (fd.fd < 0) { |
| 81 | fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno)); |
| 82 | t->result = -1; |
| 83 | return nullptr; |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 84 | } |
| 85 | fd.file_size = sb.st_size; |
| 86 | fd.block_size = 65536; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 87 | |
Tao Bao | 91a7aa4 | 2017-05-01 15:57:38 -0700 | [diff] [blame] | 88 | vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2, |
| 89 | std::placeholders::_3); |
| 90 | vtab.close = [&fd]() { close(fd.fd); }; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 91 | |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 92 | t->result = run_fuse_sideload(vtab, fd.file_size, fd.block_size); |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | // How long (in seconds) we wait for the fuse-provided package file to |
| 97 | // appear, before timing out. |
| 98 | #define SDCARD_INSTALL_TIMEOUT 10 |
| 99 | |
| 100 | void* start_sdcard_fuse(const char* path) { |
| 101 | token* t = new token; |
| 102 | |
| 103 | t->path = path; |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 104 | if ((t->pid = fork()) < 0) { |
| 105 | free(t); |
| 106 | return nullptr; |
| 107 | } |
| 108 | if (t->pid == 0) { |
| 109 | run_sdcard_fuse(t); |
| 110 | _exit(0); |
| 111 | } |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 112 | |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 113 | time_t start_time = time(nullptr); |
| 114 | time_t now = start_time; |
| 115 | |
| 116 | while (now - start_time < SDCARD_INSTALL_TIMEOUT) { |
| 117 | struct stat st; |
| 118 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) == 0) { |
| 119 | break; |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 120 | } |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 121 | if (errno != ENOENT && errno != ENOTCONN) { |
| 122 | free(t); |
| 123 | t = nullptr; |
| 124 | break; |
| 125 | } |
| 126 | sleep(1); |
| 127 | now = time(nullptr); |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 130 | return t; |
| 131 | } |
| 132 | |
| 133 | void finish_sdcard_fuse(void* cookie) { |
| 134 | if (cookie == NULL) return; |
| 135 | token* t = reinterpret_cast<token*>(cookie); |
| 136 | |
Tom Marshall | 5b4a9d8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 137 | kill(t->pid, SIGTERM); |
| 138 | int status; |
| 139 | waitpid(t->pid, &status, 0); |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 140 | |
Tom Marshall | b4591eb | 2017-09-08 21:51:49 +0000 | [diff] [blame] | 141 | delete t; |
Doug Zongker | 945fc68 | 2014-07-10 10:50:39 -0700 | [diff] [blame] | 142 | } |