1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
3
* schroot is free software; you can redistribute it and/or modify it
4
* under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* schroot is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
22
#include "schroot-main-base.h"
24
#include <sbuild/sbuild-auth-conv.h>
25
#include <sbuild/sbuild-auth-conv-tty.h>
35
#include <boost/format.hpp>
39
using namespace schroot;
41
main_base::main_base (std::string const& program_name,
42
std::string const& program_usage,
43
options_base::ptr& options):
44
schroot_base::main(program_name, program_usage,
45
std::tr1::static_pointer_cast<schroot_base::options>(options)),
50
main_base::~main_base ()
55
main_base::action_info ()
57
this->config->print_chroot_info(this->chroots, std::cout);
61
main_base::action_location ()
63
this->config->print_chroot_location(this->chroots, std::cout);
67
main_base::compat_check ()
72
main_base::get_chroot_options ()
74
sbuild::string_list ret;
76
if (this->options->all_chroots == true ||
77
this->options->all_sessions == true)
79
sbuild::chroot_config::chroot_list const& list =
80
this->config->get_chroots();
82
for (sbuild::chroot_config::chroot_list::const_iterator chroot =
87
if (((*chroot)->get_active() == false &&
88
this->options->all_chroots == false) ||
89
((*chroot)->get_active() == true &&
90
this->options->all_sessions == false))
92
ret.push_back((*chroot)->get_name());
97
sbuild::string_list invalid_chroots =
98
this->config->validate_chroots(this->options->chroots);
100
if (!invalid_chroots.empty())
102
for (sbuild::string_list::const_iterator chroot =
103
invalid_chroots.begin();
104
chroot != invalid_chroots.end();
106
sbuild::log_error() << format(_("%1%: No such chroot")) % *chroot
110
ret = this->options->chroots;
117
main_base::load_config ()
119
this->config = sbuild::chroot_config::ptr(new sbuild::chroot_config);
120
/* The normal chroot list is used when starting a session or running
121
any chroot type or session, or displaying chroot information. */
122
if (this->options->load_chroots == true)
123
this->config->add(SCHROOT_CONF, false);
124
/* The session chroot list is used when running or ending an
125
existing session, or displaying chroot information. */
126
if (this->options->load_sessions == true)
127
this->config->add(SCHROOT_SESSION_DIR, true);
131
main_base::run_impl ()
133
struct termios saved_termios;
134
bool termios_ok = false;
139
std::locale::global(std::locale(""));
140
std::cout.imbue(std::locale());
141
std::cerr.imbue(std::locale());
143
// Save terminal state.
144
if (isatty(STDIN_FILENO))
146
if (tcgetattr(STDIN_FILENO, &saved_termios) < 0)
149
sbuild::log_warning()
150
<< _("Error saving terminal settings")
157
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
158
textdomain (GETTEXT_PACKAGE);
161
sbuild::debug_level = sbuild::DEBUG_CRITICAL;
164
openlog("schroot", LOG_PID|LOG_NDELAY, LOG_AUTHPRIV);
168
if (this->options->action == options_base::ACTION_HELP)
170
action_help(std::cout);
174
if (this->options->action == options_base::ACTION_VERSION)
176
action_version(std::cout);
180
/* Initialise chroot configuration. */
183
if (this->config->get_chroots().empty() && this->options->quiet == false)
185
if (this->options->load_chroots == true &&
186
this->options->load_sessions == true)
187
sbuild::log_warning()
188
<< format(_("No chroots are defined in %1% or %2%"))
189
% SCHROOT_CONF % SCHROOT_SESSION_DIR
193
const char *cfile = (this->options->load_sessions)
194
? SCHROOT_CONF : SCHROOT_SESSION_DIR;
195
sbuild::log_warning()
196
<< format(_("No chroots are defined in %1%")) % cfile
201
/* Print chroot list (including aliases). */
202
if (this->options->action == options_base::ACTION_LIST)
208
/* Get list of chroots to use */
209
chroots = get_chroot_options();
210
if (this->chroots.empty())
213
<< format(_("The specified chroots are not defined in %1%"))
219
/* Print chroot information for specified chroots. */
220
if (this->options->action == options_base::ACTION_INFO)
225
if (this->options->action == options_base::ACTION_LOCATION)
230
if (this->options->action == options_base::ACTION_CONFIG)
236
if (this->options->action == options_base::ACTION_SESSION_BEGIN &&
237
this->chroots.size() != 1)
240
<< _("Only one chroot may be specified when beginning a session")
245
/* Create a session. */
246
sbuild::session::operation sess_op(sbuild::session::OPERATION_AUTOMATIC);
247
if (this->options->action == options_base::ACTION_SESSION_BEGIN)
248
sess_op = sbuild::session::OPERATION_BEGIN;
249
else if (this->options->action == options_base::ACTION_SESSION_RECOVER)
250
sess_op = sbuild::session::OPERATION_RECOVER;
251
else if (this->options->action == options_base::ACTION_SESSION_RUN)
252
sess_op = sbuild::session::OPERATION_RUN;
253
else if (this->options->action == options_base::ACTION_SESSION_END)
254
sess_op = sbuild::session::OPERATION_END;
259
create_session(sess_op);
261
if (!this->options->command.empty())
262
this->session->set_command(this->options->command);
263
if (this->options->preserve)
264
this->session->set_environment(environ);
265
this->session->set_force(this->options->session_force);
266
sbuild::auth::verbosity verbosity = sbuild::auth::VERBOSITY_NORMAL;
267
if (this->options->quiet)
268
verbosity = sbuild::auth::VERBOSITY_QUIET;
269
else if (this->options->verbose)
270
verbosity = sbuild::auth::VERBOSITY_VERBOSE;
271
this->session->set_verbosity(verbosity);
273
/* Set up authentication timeouts. */
274
std::tr1::shared_ptr<sbuild::auth_conv>
275
conv(new sbuild::auth_conv_tty);
278
conv->set_warning_timeout(curtime + 15);
279
conv->set_fatal_timeout(curtime + 20);
280
this->session->set_conv(conv);
283
this->session->run();
285
catch (std::runtime_error& e)
287
if (!this->options->quiet)
288
sbuild::log_error() << e.what() << endl;
293
if (isatty(STDIN_FILENO) && termios_ok)
295
if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
296
sbuild::log_warning()
297
<< _("Error restoring terminal settings")
301
exit(this->session->get_child_status());
303
catch (std::exception const& e)
305
sbuild::log_error() << e.what() << endl;
309
if (isatty(STDIN_FILENO) && termios_ok)
311
if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
312
sbuild::log_warning()
313
<< _("Error restoring terminal settings")