Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool DartComponentController::CreateAndBindNamespace() {
zx_status_get_string(ns_create_status));
}

dart_utils::RunnerTemp::SetupComponent(namespace_);
dart_utils::BindTemp(namespace_);

// Bind each directory in start_info's namespace to the controller's namespace
// instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ bool DartTestComponentController::CreateAndBindNamespace() {
zx_status_get_string(ns_create_status));
}

dart_utils::RunnerTemp::SetupComponent(namespace_);
dart_utils::BindTemp(namespace_);

// Bind each directory in start_info's namespace to the controller's namespace
// instance.
Expand Down
1 change: 0 additions & 1 deletion shell/platform/fuchsia/dart_runner/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ int main(int argc, const char** argv) {
#endif // defined(AOT_RUNTIME)
#endif // !defined(DART_PRODUCT)

dart_utils::RunnerTemp runner_temp;
dart_runner::DartRunner runner(context.get());

// Wait to serve until we have finished all of our setup.
Expand Down
5 changes: 5 additions & 0 deletions shell/platform/fuchsia/dart_runner/meta/common.shard.cml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
},
],
use: [
// This is used by the Dart VM to communicate from Dart code to C++ code.
{
storage: "tmp",
path: "/tmp",
},
// This is used by the Dart VM to load i18n data.
{
directory: "config-data",
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/fuchsia/flutter/component_v2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ ComponentV2::ComponentV2(
return;
}

// Setup /tmp to be mapped to a process-local virtual filesystem.
dart_utils::RunnerTemp::SetupComponent(fdio_ns_.get());
dart_utils::BindTemp(fdio_ns_.get());

// ComponentStartInfo::ns (optional)
if (start_info.has_ns()) {
Expand Down
3 changes: 0 additions & 3 deletions shell/platform/fuchsia/flutter/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ int main(int argc, char const* argv[]) {
&already_started);
}

// Set up the process-wide /tmp virtual filesystem.
dart_utils::RunnerTemp runner_temp;

fml::MessageLoop& loop = fml::MessageLoop::GetCurrent();
flutter_runner::Runner runner(loop.GetTaskRunner(), context.get());

Expand Down
74 changes: 8 additions & 66 deletions shell/platform/fuchsia/runtime/dart/utils/tempfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@

#include "tempfs.h"

#include <future>
#include <string>
#include <thread>
#include <string_view>

#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/async/cpp/task.h>
#include <lib/fdio/namespace.h>
#include <lib/syslog/global.h>
#include <lib/vfs/cpp/pseudo_dir.h>
#include <zircon/errors.h>
#include <zircon/status.h>
#include <zircon/syscalls.h>
Expand All @@ -28,79 +21,28 @@ constexpr char kTmpPath[] = "/tmp";

namespace dart_utils {

RunnerTemp::RunnerTemp()
: loop_(std::make_unique<async::Loop>(
&kAsyncLoopConfigNoAttachToCurrentThread)) {
loop_->StartThread("RunnerTemp");
Start();
}

RunnerTemp::~RunnerTemp() = default;

static vfs::PseudoDir tmp_dir;

void RunnerTemp::Start() {
std::promise<zx_status_t> finished;
async::PostTask(loop_->dispatcher(), [this, &finished]() {
finished.set_value([this]() {
zx::channel client, server;
if (zx_status_t status = zx::channel::create(0, &client, &server);
status != ZX_OK) {
return status;
}
if (zx_status_t status =
tmp_dir.Serve(fuchsia::io::OpenFlags::RIGHT_READABLE |
fuchsia::io::OpenFlags::RIGHT_WRITABLE |
fuchsia::io::OpenFlags::DIRECTORY,
std::move(server), loop_->dispatcher());
status != ZX_OK) {
return status;
}
fdio_ns_t* ns;
if (zx_status_t status = fdio_ns_get_installed(&ns); status != ZX_OK) {
return status;
}
if (zx_status_t status = fdio_ns_bind(ns, kTmpPath, client.release());
status != ZX_OK) {
return status;
}
return ZX_OK;
}());
});
if (zx_status_t status = finished.get_future().get(); status != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "Failed to install a /tmp virtual filesystem: %s",
zx_status_get_string(status));
}
}

void RunnerTemp::SetupComponent(fdio_ns_t* ns) {
void BindTemp(fdio_ns_t* ns) {
// TODO(zra): Should isolates share a /tmp file system within a process, or
// should isolates each get their own private file system for /tmp? For now,
// sharing the process-wide /tmp simplifies hot reload since the hot reload
// devfs requires sharing between the service isolate and the app isolates.
zx_status_t status;
fdio_flat_namespace_t* rootns;
status = fdio_ns_export_root(&rootns);
if (status != ZX_OK) {
if (zx_status_t status = fdio_ns_export_root(&rootns); status != ZX_OK) {
FX_LOGF(ERROR, LOG_TAG, "Failed to export root ns: %s",
zx_status_get_string(status));
return;
}

zx_handle_t tmp_dir_handle;
for (size_t i = 0; i < rootns->count; i++) {
if (strcmp(rootns->path[i], kTmpPath) == 0) {
tmp_dir_handle = rootns->handle[i];
} else {
zx_handle_close(rootns->handle[i]);
rootns->handle[i] = ZX_HANDLE_INVALID;
if (std::string_view{rootns->path[i]} == kTmpPath) {
tmp_dir_handle = std::exchange(rootns->handle[i], ZX_HANDLE_INVALID);
}
}
free(rootns);
rootns = nullptr;
fdio_ns_free_flat_ns(rootns);

status = fdio_ns_bind(ns, kTmpPath, tmp_dir_handle);
if (status != ZX_OK) {
if (zx_status_t status = fdio_ns_bind(ns, kTmpPath, tmp_dir_handle);
status != ZX_OK) {
zx_handle_close(tmp_dir_handle);
FX_LOGF(ERROR, LOG_TAG,
"Failed to bind /tmp directory into isolate namespace: %s",
Expand Down
27 changes: 3 additions & 24 deletions shell/platform/fuchsia/runtime/dart/utils/tempfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,13 @@
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_TEMPFS_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_TEMPFS_H_

#include <mutex>

#include <lib/async-loop/cpp/loop.h>
#include <lib/fdio/namespace.h>

namespace dart_utils {

// Sets up /tmp for the dart_runner and flutter_runner.
class RunnerTemp {
public:
// Sets up a virtual filesystem bound to /tmp in the process-wide namespace
// that has the lifetime of this instance.
RunnerTemp();
~RunnerTemp();

// Take the virtual filesystem mapped into the process-wide namespace for
// /tmp, and map it to /tmp in the given namespace.
static void SetupComponent(fdio_ns_t* ns);

private:
void Start();

std::unique_ptr<async::Loop> loop_;

// Disallow copy and assignment.
RunnerTemp(const RunnerTemp&) = delete;
RunnerTemp& operator=(const RunnerTemp&) = delete;
};
// Take the virtual filesystem mapped into the process-wide namespace for /tmp,
// and map it to /tmp in the given namespace.
void BindTemp(fdio_ns_t* ns);

} // namespace dart_utils

Expand Down