1// Copyright 2010 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 "cli/main.hpp"
30
31#if defined(HAVE_CONFIG_H)
32#   include "config.h"
33#endif
34
35extern "C" {
36#include <signal.h>
37#include <unistd.h>
38}
39
40#include <cstdlib>
41#include <iostream>
42#include <string>
43#include <utility>
44
45#include "cli/cmd_about.hpp"
46#include "cli/cmd_config.hpp"
47#include "cli/cmd_db_exec.hpp"
48#include "cli/cmd_db_migrate.hpp"
49#include "cli/cmd_debug.hpp"
50#include "cli/cmd_help.hpp"
51#include "cli/cmd_list.hpp"
52#include "cli/cmd_report.hpp"
53#include "cli/cmd_report_html.hpp"
54#include "cli/cmd_report_junit.hpp"
55#include "cli/cmd_test.hpp"
56#include "cli/common.ipp"
57#include "cli/config.hpp"
58#include "engine/atf.hpp"
59#include "engine/plain.hpp"
60#include "engine/scheduler.hpp"
61#include "engine/tap.hpp"
62#include "store/exceptions.hpp"
63#include "utils/cmdline/commands_map.ipp"
64#include "utils/cmdline/exceptions.hpp"
65#include "utils/cmdline/globals.hpp"
66#include "utils/cmdline/options.hpp"
67#include "utils/cmdline/parser.ipp"
68#include "utils/cmdline/ui.hpp"
69#include "utils/config/tree.ipp"
70#include "utils/env.hpp"
71#include "utils/format/macros.hpp"
72#include "utils/fs/operations.hpp"
73#include "utils/fs/path.hpp"
74#include "utils/logging/macros.hpp"
75#include "utils/logging/operations.hpp"
76#include "utils/optional.ipp"
77#include "utils/sanity.hpp"
78#include "utils/signals/exceptions.hpp"
79
80namespace cmdline = utils::cmdline;
81namespace config = utils::config;
82namespace fs = utils::fs;
83namespace logging = utils::logging;
84namespace signals = utils::signals;
85namespace scheduler = engine::scheduler;
86
87using utils::none;
88using utils::optional;
89
90
91namespace {
92
93
94/// Registers all valid scheduler interfaces.
95///
96/// This is part of Kyua's setup but it is a bit strange to find it here.  I am
97/// not sure what a better location would be though, so for now this is good
98/// enough.
99static void
100register_scheduler_interfaces(void)
101{
102    scheduler::register_interface(
103        "atf", std::shared_ptr< scheduler::interface >(
104            new engine::atf_interface()));
105    scheduler::register_interface(
106        "plain", std::shared_ptr< scheduler::interface >(
107            new engine::plain_interface()));
108    scheduler::register_interface(
109        "tap", std::shared_ptr< scheduler::interface >(
110            new engine::tap_interface()));
111}
112
113
114/// Executes the given subcommand with proper usage_error reporting.
115///
116/// \param ui Object to interact with the I/O of the program.
117/// \param command The subcommand to execute.
118/// \param args The part of the command line passed to the subcommand.  The
119///     first item of this collection must match the command name.
120/// \param user_config The runtime configuration to pass to the subcommand.
121///
122/// \return The exit code of the command.  Typically 0 on success, some other
123/// integer otherwise.
124///
125/// \throw cmdline::usage_error If the user input to the subcommand is invalid.
126///     This error does not encode the command name within it, so this function
127///     extends the message in the error to specify which subcommand was
128///     affected.
129/// \throw std::exception This propagates any uncaught exception.  Such
130///     exceptions are bugs, but we let them propagate so that the runtime will
131///     abort and dump core.
132static int
133run_subcommand(cmdline::ui* ui, cli::cli_command* command,
134               const cmdline::args_vector& args,
135               const config::tree& user_config)
136{
137    try {
138        PRE(command->name() == args[0]);
139        return command->main(ui, args, user_config);
140    } catch (const cmdline::usage_error& e) {
141        throw std::pair< std::string, cmdline::usage_error >(
142            command->name(), e);
143    }
144}
145
146
147/// Exception-safe version of main.
148///
149/// This function provides the real meat of the entry point of the program.  It
150/// is allowed to throw some known exceptions which are parsed by the caller.
151/// Doing so keeps this function simpler and allow tests to actually validate
152/// that the errors reported are accurate.
153///
154/// \return The exit code of the program.  Should be EXIT_SUCCESS on success and
155/// EXIT_FAILURE on failure.  The caller extends this to additional integers for
156/// errors reported through exceptions.
157///
158/// \param ui Object to interact with the I/O of the program.
159/// \param argc The number of arguments passed on the command line.
160/// \param argv NULL-terminated array containing the command line arguments.
161/// \param mock_command An extra command provided for testing purposes; should
162///     just be NULL other than for tests.
163///
164/// \throw cmdline::usage_error If the user ran the program with invalid
165///     arguments.
166/// \throw std::exception This propagates any uncaught exception.  Such
167///     exceptions are bugs, but we let them propagate so that the runtime will
168///     abort and dump core.
169static int
170safe_main(cmdline::ui* ui, int argc, const char* const argv[],
171          cli::cli_command_ptr mock_command)
172{
173    cmdline::options_vector options;
174    options.push_back(&cli::config_option);
175    options.push_back(&cli::variable_option);
176    const cmdline::string_option loglevel_option(
177        "loglevel", "Level of the messages to log", "level", "info");
178    options.push_back(&loglevel_option);
179    const cmdline::path_option logfile_option(
180        "logfile", "Path to the log file", "file",
181        cli::detail::default_log_name().c_str());
182    options.push_back(&logfile_option);
183
184    cmdline::commands_map< cli::cli_command > commands;
185
186    commands.insert(new cli::cmd_about());
187    commands.insert(new cli::cmd_config());
188    commands.insert(new cli::cmd_db_exec());
189    commands.insert(new cli::cmd_db_migrate());
190    commands.insert(new cli::cmd_help(&options, &commands));
191
192    commands.insert(new cli::cmd_debug(), "Workspace");
193    commands.insert(new cli::cmd_list(), "Workspace");
194    commands.insert(new cli::cmd_test(), "Workspace");
195
196    commands.insert(new cli::cmd_report(), "Reporting");
197    commands.insert(new cli::cmd_report_html(), "Reporting");
198    commands.insert(new cli::cmd_report_junit(), "Reporting");
199
200    if (mock_command.get() != NULL)
201        commands.insert(mock_command);
202
203    const cmdline::parsed_cmdline cmdline = cmdline::parse(argc, argv, options);
204
205    const fs::path logfile(cmdline.get_option< cmdline::path_option >(
206        "logfile"));
207    fs::mkdir_p(logfile.branch_path(), 0755);
208    LD(F("Log file is %s") % logfile);
209    utils::install_crash_handlers(logfile.str());
210    try {
211        logging::set_persistency(cmdline.get_option< cmdline::string_option >(
212            "loglevel"), logfile);
213    } catch (const std::range_error& e) {
214        throw cmdline::usage_error(e.what());
215    }
216
217    if (cmdline.arguments().empty())
218        throw cmdline::usage_error("No command provided");
219    const std::string cmdname = cmdline.arguments()[0];
220
221    const config::tree user_config = cli::load_config(cmdline,
222                                                      cmdname != "help");
223
224    cli::cli_command* command = commands.find(cmdname);
225    if (command == NULL)
226        throw cmdline::usage_error(F("Unknown command '%s'") % cmdname);
227    register_scheduler_interfaces();
228    return run_subcommand(ui, command, cmdline.arguments(), user_config);
229}
230
231
232}  // anonymous namespace
233
234
235/// Gets the name of the default log file.
236///
237/// \return The path to the log file.
238fs::path
239cli::detail::default_log_name(void)
240{
241    // Update doc/troubleshooting.texi if you change this algorithm.
242    const optional< std::string > home(utils::getenv("HOME"));
243    if (home) {
244        return logging::generate_log_name(fs::path(home.get()) / ".kyua" /
245                                          "logs", cmdline::progname());
246    } else {
247        const optional< std::string > tmpdir(utils::getenv("TMPDIR"));
248        if (tmpdir) {
249            return logging::generate_log_name(fs::path(tmpdir.get()),
250                                              cmdline::progname());
251        } else {
252            return logging::generate_log_name(fs::path("/tmp"),
253                                              cmdline::progname());
254        }
255    }
256}
257
258
259/// Testable entry point, with catch-all exception handlers.
260///
261/// This entry point does not perform any initialization of global state; it is
262/// provided to allow unit-testing of the utility's entry point.
263///
264/// \param ui Object to interact with the I/O of the program.
265/// \param argc The number of arguments passed on the command line.
266/// \param argv NULL-terminated array containing the command line arguments.
267/// \param mock_command An extra command provided for testing purposes; should
268///     just be NULL other than for tests.
269///
270/// \return 0 on success, some other integer on error.
271///
272/// \throw std::exception This propagates any uncaught exception.  Such
273///     exceptions are bugs, but we let them propagate so that the runtime will
274///     abort and dump core.
275int
276cli::main(cmdline::ui* ui, const int argc, const char* const* const argv,
277          cli_command_ptr mock_command)
278{
279    try {
280        const int exit_code = safe_main(ui, argc, argv, mock_command);
281
282        // Codes above 1 are reserved to report conditions captured as
283        // exceptions below.
284        INV(exit_code == EXIT_SUCCESS || exit_code == EXIT_FAILURE);
285
286        return exit_code;
287    } catch (const signals::interrupted_error& e) {
288        cmdline::print_error(ui, F("%s.") % e.what());
289        // Re-deliver the interruption signal to self so that we terminate with
290        // the right status.  At this point we should NOT have any custom signal
291        // handlers in place.
292        ::kill(getpid(), e.signo());
293        LD("Interrupt signal re-delivery did not terminate program");
294        // If we reach this, something went wrong because we did not exit as
295        // intended.  Return an internal error instead.  (Would be nicer to
296        // abort in principle, but it wouldn't be a nice experience if it ever
297        // happened.)
298        return 2;
299    } catch (const std::pair< std::string, cmdline::usage_error >& e) {
300        const std::string message = F("Usage error for command %s: %s.") %
301            e.first % e.second.what();
302        LE(message);
303        ui->err(message);
304        ui->err(F("Type '%s help %s' for usage information.") %
305                cmdline::progname() % e.first);
306        return 3;
307    } catch (const cmdline::usage_error& e) {
308        const std::string message = F("Usage error: %s.") % e.what();
309        LE(message);
310        ui->err(message);
311        ui->err(F("Type '%s help' for usage information.") %
312                cmdline::progname());
313        return 3;
314    } catch (const store::old_schema_error& e) {
315        const std::string message = F("The database has schema version %s, "
316                                      "which is too old; please use db-migrate "
317                                      "to upgrade it.") % e.old_version();
318        cmdline::print_error(ui, message);
319        return 2;
320    } catch (const std::runtime_error& e) {
321        cmdline::print_error(ui, F("%s.") % e.what());
322        return 2;
323    }
324}
325
326
327/// Delegate for ::main().
328///
329/// This function is supposed to be called directly from the top-level ::main()
330/// function.  It takes care of initializing internal libraries and then calls
331/// main(ui, argc, argv).
332///
333/// \pre This function can only be called once.
334///
335/// \throw std::exception This propagates any uncaught exception.  Such
336///     exceptions are bugs, but we let them propagate so that the runtime will
337///     abort and dump core.
338int
339cli::main(const int argc, const char* const* const argv)
340{
341    logging::set_inmemory();
342
343    LI(F("%s %s") % PACKAGE % VERSION);
344
345    std::string plain_args;
346    for (const char* const* arg = argv; *arg != NULL; arg++)
347        plain_args += F(" %s") % *arg;
348    LI(F("Command line:%s") % plain_args);
349
350    cmdline::init(argv[0]);
351    cmdline::ui ui;
352
353    const int exit_code = main(&ui, argc, argv);
354    LI(F("Clean exit with code %s") % exit_code);
355    return exit_code;
356}
357