diff options
Diffstat (limited to 'fuse_sdcard_provider.cpp')
-rw-r--r-- | fuse_sdcard_provider.cpp | 46 |
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; } |