No Description

fuse_sdcard_provider.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "fuse_sdcard_provider.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <sys/mount.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. #include <functional>
  28. #include <android-base/file.h>
  29. #include "fuse_sideload.h"
  30. struct file_data {
  31. int fd; // the underlying sdcard file
  32. uint64_t file_size;
  33. uint32_t block_size;
  34. };
  35. static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer,
  36. uint32_t fetch_size) {
  37. off64_t offset = static_cast<off64_t>(block) * fd.block_size;
  38. if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) {
  39. fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
  40. return -EIO;
  41. }
  42. if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) {
  43. fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
  44. return -EIO;
  45. }
  46. return 0;
  47. }
  48. struct token {
  49. pid_t pid;
  50. const char* path;
  51. int result;
  52. };
  53. static void* run_sdcard_fuse(void* cookie) {
  54. token* t = reinterpret_cast<token*>(cookie);
  55. struct stat sb;
  56. if (stat(t->path, &sb) < 0) {
  57. fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno));
  58. t->result = -1;
  59. return nullptr;
  60. }
  61. struct file_data fd;
  62. struct provider_vtab vtab;
  63. fd.fd = open(t->path, O_RDONLY);
  64. if (fd.fd < 0) {
  65. fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno));
  66. t->result = -1;
  67. return nullptr;
  68. }
  69. fd.file_size = sb.st_size;
  70. fd.block_size = 65536;
  71. vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2,
  72. std::placeholders::_3);
  73. vtab.close = [&fd]() { close(fd.fd); };
  74. t->result = run_fuse_sideload(vtab, fd.file_size, fd.block_size);
  75. return nullptr;
  76. }
  77. // How long (in seconds) we wait for the fuse-provided package file to
  78. // appear, before timing out.
  79. #define SDCARD_INSTALL_TIMEOUT 10
  80. void* start_sdcard_fuse(const char* path) {
  81. token* t = new token;
  82. t->path = path;
  83. if ((t->pid = fork()) < 0) {
  84. free(t);
  85. return nullptr;
  86. }
  87. if (t->pid == 0) {
  88. run_sdcard_fuse(t);
  89. _exit(0);
  90. }
  91. time_t start_time = time(nullptr);
  92. time_t now = start_time;
  93. while (now - start_time < SDCARD_INSTALL_TIMEOUT) {
  94. struct stat st;
  95. if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) == 0) {
  96. break;
  97. }
  98. if (errno != ENOENT && errno != ENOTCONN) {
  99. free(t);
  100. t = nullptr;
  101. break;
  102. }
  103. sleep(1);
  104. now = time(nullptr);
  105. }
  106. return t;
  107. }
  108. void finish_sdcard_fuse(void* cookie) {
  109. if (cookie == NULL) return;
  110. token* t = reinterpret_cast<token*>(cookie);
  111. kill(t->pid, SIGTERM);
  112. int status;
  113. waitpid(t->pid, &status, 0);
  114. delete t;
  115. }