• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
19 #include <string.h>
20 #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 
29 struct file_data {
30     int fd;  // the underlying sdcard file
31 
32     uint64_t file_size;
33     uint32_t block_size;
34 };
35 
read_block_file(void * cookie,uint32_t block,uint8_t * buffer,uint32_t fetch_size)36 static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
37     struct file_data* fd = (struct file_data*)cookie;
38 
39     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));
42         return -EIO;
43     }
44 
45     while (fetch_size > 0) {
46         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;
50         }
51         fetch_size -= r;
52         buffer += r;
53     }
54 
55     return 0;
56 }
57 
close_file(void * cookie)58 static void close_file(void* cookie) {
59     struct file_data* fd = (struct file_data*)cookie;
60     close(fd->fd);
61 }
62 
63 struct token {
64     pthread_t th;
65     const char* path;
66     int result;
67 };
68 
run_sdcard_fuse(void * cookie)69 static void* run_sdcard_fuse(void* cookie) {
70     struct token* t = (struct token*)cookie;
71 
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 
start_sdcard_fuse(const char * path)102 void* start_sdcard_fuse(const char* path) {
103     struct token* t = malloc(sizeof(struct token));
104 
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 
finish_sdcard_fuse(void * cookie)129 void finish_sdcard_fuse(void* cookie) {
130     if (cookie == NULL) return;
131     struct token* t = (struct token*)cookie;
132 
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);
139     free(t);
140 }
141