1 // Copyright 2018 The Fuchsia 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 <lib/devmgr-integration-test/fixture.h>
6 
7 #include <stdint.h>
8 #include <utility>
9 
10 #include <fbl/algorithm.h>
11 #include <lib/devmgr-launcher/launch.h>
12 #include <lib/fdio/util.h>
13 #include <lib/zx/channel.h>
14 #include <zircon/processargs.h>
15 #include <zircon/status.h>
16 
17 namespace devmgr_integration_test {
18 
19 const char* IsolatedDevmgr::kSysdevDriver = "/boot/driver/test/sysdev.so";
20 
DefaultArgs()21 devmgr_launcher::Args IsolatedDevmgr::DefaultArgs() {
22     devmgr_launcher::Args args;
23     args.sys_device_driver = kSysdevDriver;
24     args.load_drivers.push_back("/boot/driver/test.so");
25     args.driver_search_paths.push_back("/boot/driver/test");
26     return args;
27 }
28 
~IsolatedDevmgr()29 IsolatedDevmgr::~IsolatedDevmgr() {
30     // Destroy the isolated devmgr
31     if (job_.is_valid()) {
32         job_.kill();
33     }
34 }
35 
Create(devmgr_launcher::Args args,fbl::unique_ptr<IsolatedDevmgr> * out)36 zx_status_t IsolatedDevmgr::Create(devmgr_launcher::Args args,
37                                    fbl::unique_ptr<IsolatedDevmgr>* out) {
38     auto devmgr = fbl::make_unique<IsolatedDevmgr>();
39 
40     zx::channel devfs;
41     zx_status_t status = devmgr_launcher::Launch(std::move(args), &devmgr->job_, &devfs);
42     if (status != ZX_OK) {
43         return status;
44     }
45 
46     int fd;
47     zx_handle_t devfs_handles[] = {
48         devfs.release(),
49     };
50     uint32_t devfs_types[fbl::count_of(devfs_handles)] = {
51         PA_FDIO_REMOTE,
52     };
53     status = fdio_create_fd(devfs_handles, devfs_types, fbl::count_of(devfs_handles), &fd);
54     if (status != ZX_OK) {
55         return status;
56     }
57     devmgr->devfs_root_.reset(fd);
58 
59     *out = std::move(devmgr);
60     return ZX_OK;
61 }
62 
63 } // namespace devmgr_integration_test
64