blob: eb6454f1df7be460a6271fd00ec207ed6b7f757d [file] [log] [blame]
Doug Zongker945fc682014-07-10 10:50:39 -07001/*
2 * Copyright (C) 2014 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>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Doug Zongker945fc682014-07-10 10:50:39 -070020#include <errno.h>
21#include <pthread.h>
22#include <sys/mount.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26
27#include "fuse_sideload.h"
28
29struct file_data {
30 int fd; // the underlying sdcard file
31
32 uint64_t file_size;
33 uint32_t block_size;
34};
35
36static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
Tao Bao71dc3652015-07-19 08:40:37 -070037 file_data* fd = reinterpret_cast<file_data*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -070038
Elliott Hughes7bad7c42015-04-28 17:24:24 -070039 off64_t offset = ((off64_t) block) * fd->block_size;
40 if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) {
41 fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
Doug Zongker945fc682014-07-10 10:50:39 -070042 return -EIO;
43 }
44
45 while (fetch_size > 0) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -070046 ssize_t r = TEMP_FAILURE_RETRY(read(fd->fd, buffer, fetch_size));
47 if (r == -1) {
48 fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
49 return -EIO;
Doug Zongker945fc682014-07-10 10:50:39 -070050 }
51 fetch_size -= r;
52 buffer += r;
53 }
54
55 return 0;
56}
57
58static void close_file(void* cookie) {
Tao Bao71dc3652015-07-19 08:40:37 -070059 file_data* fd = reinterpret_cast<file_data*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -070060 close(fd->fd);
61}
62
63struct token {
64 pthread_t th;
65 const char* path;
66 int result;
67};
68
69static void* run_sdcard_fuse(void* cookie) {
Tao Bao71dc3652015-07-19 08:40:37 -070070 token* t = reinterpret_cast<token*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -070071
72 struct stat sb;
73 if (stat(t->path, &sb) < 0) {
74 fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno));
75 t->result = -1;
76 return NULL;
77 }
78
79 struct file_data fd;
80 struct provider_vtab vtab;
81
82 fd.fd = open(t->path, O_RDONLY);
83 if (fd.fd < 0) {
84 fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno));
85 t->result = -1;
86 return NULL;
87 }
88 fd.file_size = sb.st_size;
89 fd.block_size = 65536;
90
91 vtab.read_block = read_block_file;
92 vtab.close = close_file;
93
94 t->result = run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size);
95 return NULL;
96}
97
98// How long (in seconds) we wait for the fuse-provided package file to
99// appear, before timing out.
100#define SDCARD_INSTALL_TIMEOUT 10
101
102void* start_sdcard_fuse(const char* path) {
Tao Bao71dc3652015-07-19 08:40:37 -0700103 token* t = new token;
Doug Zongker945fc682014-07-10 10:50:39 -0700104
105 t->path = path;
106 pthread_create(&(t->th), NULL, run_sdcard_fuse, t);
107
108 struct stat st;
109 int i;
110 for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
111 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
112 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
113 sleep(1);
114 continue;
115 } else {
116 return NULL;
117 }
118 }
119 }
120
121 // The installation process expects to find the sdcard unmounted.
122 // Unmount it with MNT_DETACH so that our open file continues to
123 // work but new references see it as unmounted.
124 umount2("/sdcard", MNT_DETACH);
125
126 return t;
127}
128
129void finish_sdcard_fuse(void* cookie) {
130 if (cookie == NULL) return;
Tao Bao71dc3652015-07-19 08:40:37 -0700131 token* t = reinterpret_cast<token*>(cookie);
Doug Zongker945fc682014-07-10 10:50:39 -0700132
133 // Calling stat() on this magic filename signals the fuse
134 // filesystem to shut down.
135 struct stat st;
136 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
137
138 pthread_join(t->th, NULL);
Tao Bao71dc3652015-07-19 08:40:37 -0700139 delete t;
Doug Zongker945fc682014-07-10 10:50:39 -0700140}