xref: /freebsd/contrib/kyua/os/freebsd/prepare_kmods.cpp (revision edb230c4af499203d7a6894b3711fe6574b26040)
1 // Copyright 2024 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "os/freebsd/prepare_kmods.hpp"
30 
31 #include "cli/common.hpp"
32 #include "drivers/list_tests.hpp"
33 #include "engine/filters.hpp"
34 #include "model/metadata.hpp"
35 #include "model/test_case.hpp"
36 #include "model/test_program.hpp"
37 
38 #include <system_error>
39 
40 // FreeBSD kldload syscall
41 extern "C" {
42 #include <sys/param.h>
43 #include <sys/linker.h>
44 }
45 
46 
47 namespace {
48 
49 
50 static const std::string _name = "kmods";
51 static const std::string _description = "FreeBSD: load modules specified "
52     "in the required_kmods metadata";
53 
54 
55 /// Hooks for list_tests to examine test cases as they come.
56 class list_hooks : public drivers::list_tests::base_hooks {
57     /// Collected names of required kernel modules.
58     std::set< std::string >& _modules;
59 
60 public:
61     /// Initializes the hooks.
62     ///
63     /// \param modules_ The set of modules to fill.
list_hooks(std::set<std::string> & modules_)64     list_hooks(std::set< std::string >& modules_) :
65         _modules(modules_)
66     {
67     }
68 
69     /// Examine a test case as soon as it is found.
70     ///
71     /// \param test_program The test program containing the test case.
72     /// \param test_case_name The name of the located test case.
73     void
got_test_case(const model::test_program & test_program,const std::string & test_case_name)74     got_test_case(const model::test_program& test_program,
75                   const std::string& test_case_name)
76     {
77         auto test_case = test_program.find(test_case_name);
78         auto kmods = test_case.get_metadata().required_kmods();
79         _modules.insert(kmods.begin(), kmods.end());
80     }
81 };
82 
83 
84 }  // anonymous namespace
85 
86 
87 namespace freebsd {
88 
89 
90 const std::string&
name() const91 prepare_kmods::name() const
92 {
93     return _name;
94 }
95 
96 
97 const std::string&
description() const98 prepare_kmods::description() const
99 {
100     return _description;
101 }
102 
103 
104 int
exec(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & user_config) const105 prepare_kmods::exec(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
106                const config::tree& user_config) const
107 {
108     int err = 0;
109 
110     // Collect required modules
111     std::set< std::string > modules;
112     list_hooks hooks(modules);
113     const std::set< engine::test_filter > nofilters;
114     const drivers::list_tests::result result = drivers::list_tests::drive(
115         cli::kyuafile_path(cmdline), cli::build_root_path(cmdline),
116         nofilters, user_config, hooks);
117 
118     // Nothing to do
119     if (modules.empty())
120         return 0;
121 
122     // Announce the work
123     ui->out("kldload", false);
124     for (auto& m : modules)
125         ui->out(F(" %s") % m, false);
126     ui->out("");
127 
128     if (cmdline.has_option("dry-run"))
129         return 0;
130 
131     // Load the modules
132     for (auto& m : modules) {
133         if (::kldload(m.c_str()) != -1 || errno == EEXIST)
134             continue;
135         ui->out(F("WARN: cannot kldload %s") % m);
136         err = EINVAL;
137     }
138 
139     return err;
140 }
141 
142 
143 }  // namespace freebsd
144