aboutsummaryrefslogtreecommitdiffstats
path: root/fuse_sdcard_provider.cpp
diff options
context:
space:
mode:
authorTom Marshall <tdm@cyngn.com>2015-11-05 21:30:40 -0800
committerTom Marshall <tdm@cyngn.com>2015-11-25 15:34:35 -0800
commitffc8a8702d9e1568995ce155c648fd029909cdac (patch)
tree3dcf3d34ebb03c3ac8d140d249e55838a90e2e14 /fuse_sdcard_provider.cpp
parent3381ac447175af3252c79c3e066cbbc38c400c85 (diff)
downloadbootable_recovery-ffc8a8702d9e1568995ce155c648fd029909cdac.zip
bootable_recovery-ffc8a8702d9e1568995ce155c648fd029909cdac.tar.gz
bootable_recovery-ffc8a8702d9e1568995ce155c648fd029909cdac.tar.bz2
recovery: Provide sideload cancellation
Change-Id: I13f0c9ae5444652a2141442ef24258679a78d320
Diffstat (limited to 'fuse_sdcard_provider.cpp')
-rw-r--r--fuse_sdcard_provider.cpp46
1 files changed, 28 insertions, 18 deletions
diff --git a/fuse_sdcard_provider.cpp b/fuse_sdcard_provider.cpp
index 879f90b..8681425 100644
--- a/fuse_sdcard_provider.cpp
+++ b/fuse_sdcard_provider.cpp
@@ -21,6 +21,7 @@
#include <pthread.h>
#include <sys/mount.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
@@ -61,7 +62,7 @@ static void close_file(void* cookie) {
}
struct token {
- pthread_t th;
+ pid_t pid;
const char* path;
int result;
};
@@ -103,19 +104,30 @@ void* start_sdcard_fuse(const char* path) {
token* t = new token;
t->path = path;
- pthread_create(&(t->th), NULL, run_sdcard_fuse, t);
-
- struct stat st;
- int i;
- for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
- if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
- if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
- sleep(1);
- continue;
- } else {
- return NULL;
- }
+ if ((t->pid = fork()) < 0) {
+ free(t);
+ return NULL;
+ }
+ if (t->pid == 0) {
+ run_sdcard_fuse(t);
+ _exit(0);
+ }
+
+ time_t start_time = time(NULL);
+ time_t now = start_time;
+
+ while (now - start_time < SDCARD_INSTALL_TIMEOUT) {
+ struct stat st;
+ if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) == 0) {
+ break;
+ }
+ if (errno != ENOENT && errno != ENOTCONN) {
+ free(t);
+ t = NULL;
+ break;
}
+ sleep(1);
+ now = time(NULL);
}
return t;
@@ -125,11 +137,9 @@ void finish_sdcard_fuse(void* cookie) {
if (cookie == NULL) return;
token* t = reinterpret_cast<token*>(cookie);
- // Calling stat() on this magic filename signals the fuse
- // filesystem to shut down.
- struct stat st;
- stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
+ kill(t->pid, SIGTERM);
+ int status;
+ waitpid(t->pid, &status, 0);
- pthread_join(t->th, NULL);
delete t;
}