Flutter Engine
tempfs.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "tempfs.h"
6 
7 #include <future>
8 #include <string>
9 #include <thread>
10 
11 #include <lib/async-loop/cpp/loop.h>
12 #include <lib/async-loop/default.h>
13 #include <lib/async/cpp/task.h>
14 #include <lib/fdio/namespace.h>
15 #include <lib/memfs/memfs.h>
16 #include <lib/syslog/global.h>
17 #include <zircon/errors.h>
18 #include <zircon/status.h>
19 #include <zircon/syscalls.h>
20 
21 #include "logging.h"
22 
23 namespace {
24 
25 constexpr char kTmpPath[] = "/tmp";
26 [[maybe_unused]] constexpr size_t kMaxTmpPages = 1024;
27 
28 } // namespace
29 
30 namespace dart_utils {
31 
33  : loop_(std::make_unique<async::Loop>(
34  &kAsyncLoopConfigNoAttachToCurrentThread)) {
35  loop_->StartThread("RunnerTemp");
36  Start();
37 }
38 
39 RunnerTemp::~RunnerTemp() = default;
40 
41 void RunnerTemp::Start() {
42  std::promise<void> finished;
43  async::PostTask(loop_->dispatcher(), [this, &finished]() {
44 #if defined(DART_PRODUCT)
45  zx_status_t status = memfs_install_at_with_page_limit(
46  loop_->dispatcher(), kMaxTmpPages, kTmpPath);
47 #else
48  memfs_filesystem_t* fs;
49  // Hot reload uses /tmp to hold the updated dills and assets so do not
50  // impose any size limitation in non product runners.
51  zx_status_t status = memfs_install_at(loop_->dispatcher(), kTmpPath, &fs);
52 #endif
53  finished.set_value();
54  if (status != ZX_OK) {
55  FX_LOGF(ERROR, LOG_TAG, "Failed to install a /tmp memfs: %s",
56  zx_status_get_string(status));
57  return;
58  }
59  });
60  finished.get_future().wait();
61 }
62 
63 void RunnerTemp::SetupComponent(fdio_ns_t* ns) {
64  // TODO(zra): Should isolates share a /tmp file system within a process, or
65  // should isolates each get their own private memfs for /tmp? For now,
66  // sharing the process-wide /tmp simplifies hot reload since the hot reload
67  // devfs requires sharing between the service isolate and the app isolates.
68  zx_status_t status;
69  fdio_flat_namespace_t* rootns;
70  status = fdio_ns_export_root(&rootns);
71  if (status != ZX_OK) {
72  FX_LOGF(ERROR, LOG_TAG, "Failed to export root ns: %s",
73  zx_status_get_string(status));
74  return;
75  }
76 
77  zx_handle_t tmp_dir_handle;
78  for (size_t i = 0; i < rootns->count; i++) {
79  if (strcmp(rootns->path[i], kTmpPath) == 0) {
80  tmp_dir_handle = rootns->handle[i];
81  } else {
82  zx_handle_close(rootns->handle[i]);
83  rootns->handle[i] = ZX_HANDLE_INVALID;
84  }
85  }
86  free(rootns);
87  rootns = nullptr;
88 
89  status = fdio_ns_bind(ns, kTmpPath, tmp_dir_handle);
90  if (status != ZX_OK) {
91  zx_handle_close(tmp_dir_handle);
92  FX_LOGF(ERROR, LOG_TAG,
93  "Failed to bind /tmp directory into isolate namespace: %s",
94  zx_status_get_string(status));
95  }
96 }
97 
98 } // namespace dart_utils
Definition: ref_ptr.h:252
constexpr char kTmpPath[]
#define LOG_TAG
Definition: logging.h:11
static void SetupComponent(fdio_ns_t *ns)
Definition: tempfs.cc:63