blob: 588468b840563381a225dd5d7932c0d91b9ca286 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/sys/appmgr/dynamic_library_loader.h"
#include <fidl/fuchsia.io/cpp/wire.h>
#include <lib/async-loop/default.h>
#include <lib/async-loop/loop.h>
#include <lib/fdio/directory.h>
#include <lib/syslog/cpp/macros.h>
#include "src/lib/loader_service/loader_service.h"
namespace component {
namespace DynamicLibraryLoader {
namespace fio = fuchsia_io;
static async_loop_t* ld_loop = nullptr;
zx::status<fidl::ClientEnd<fuchsia_ldsvc::Loader>> Start(int package_fd, std::string name) {
if (!ld_loop) {
zx_status_t status = async_loop_create(&kAsyncLoopConfigNoAttachToCurrentThread, &ld_loop);
if (status != ZX_OK) {
return zx::error(status);
}
status = async_loop_start_thread(ld_loop, "appmgr-loader", nullptr);
if (status != ZX_OK) {
return zx::error(status);
}
}
fbl::unique_fd lib_fd;
zx_status_t status =
fdio_open_fd_at(package_fd, "lib",
fio::wire::kOpenFlagDirectory | fio::wire::kOpenRightReadable |
fio::wire::kOpenRightExecutable,
lib_fd.reset_and_get_address());
if (status != ZX_OK) {
return zx::error(status);
}
auto loader =
loader::LoaderService::Create(async_loop_get_dispatcher(ld_loop), std::move(lib_fd), name);
return loader->Connect();
}
} // namespace DynamicLibraryLoader
} // namespace component