From ffc8a8702d9e1568995ce155c648fd029909cdac Mon Sep 17 00:00:00 2001 From: Tom Marshall Date: Thu, 5 Nov 2015 21:30:40 -0800 Subject: recovery: Provide sideload cancellation Change-Id: I13f0c9ae5444652a2141442ef24258679a78d320 --- fuse_sdcard_provider.cpp | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'fuse_sdcard_provider.cpp') 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 #include #include +#include #include #include @@ -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(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; } -- cgit v1.1