20
20
#include <config.h>
22
#include "schroot-main.h"
23
#include "schroot-options.h"
29
27
#include <boost/format.hpp>
31
29
#include <syslog.h>
35
#include "schroot-options.h"
37
#ifdef SBUILD_DCHROOT_COMPAT
38
#include "dchroot-chroot-config.h"
39
#include "dchroot-session.h"
43
32
using boost::format;
44
33
using namespace schroot;
46
#ifdef SBUILD_DCHROOT_COMPAT
47
#define DCHROOT_CONF "/etc/dchroot.conf"
51
* Print version information.
53
* @param stream the stream to output to.
54
* @param options the command line options.
57
print_version (std::ostream& stream,
58
schroot::options& options)
60
stream << format(_("%1% (Debian sbuild) %2%\n"))
61
% (options.dchroot_compat ? "dchroot" : "schroot")
63
<< _("Written by Roger Leigh\n\n")
64
<< _("Copyright (C) 2004-2006 Roger Leigh\n")
65
<< _("This is free software; see the source for copying conditions. There is NO\n"
66
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n")
71
* Get a list of chroots based on the specified options (--all, --chroot).
73
* @param config the chroot configuration
74
* @param options the command-line options
76
* @returns a list of chroots.
79
get_chroot_options (std::tr1::shared_ptr<sbuild::chroot_config>& config,
80
schroot::options& options)
82
sbuild::string_list ret;
84
if (options.all_chroots == true || options.all_sessions == true)
86
sbuild::chroot_config::chroot_list const& list = config->get_chroots();
88
for (sbuild::chroot_config::chroot_list::const_iterator chroot = list.begin();
92
if (((*chroot)->get_active() == false && options.all_chroots == false) ||
93
((*chroot)->get_active() == true && options.all_sessions == false))
95
ret.push_back((*chroot)->get_name());
100
sbuild::string_list invalid_chroots =
101
config->validate_chroots(options.chroots);
103
if (!invalid_chroots.empty())
105
for (sbuild::string_list::const_iterator chroot = invalid_chroots.begin();
106
chroot != invalid_chroots.end();
108
sbuild::log_error() << format(_("%1%: No such chroot")) % *chroot
112
ret = options.chroots;
131
struct termios saved_termios;
132
bool termios_ok = false;
137
std::locale::global(std::locale(""));
138
std::cout.imbue(std::locale());
139
std::cerr.imbue(std::locale());
141
// Save terminal state.
142
if (isatty(STDIN_FILENO))
144
if (tcgetattr(STDIN_FILENO, &saved_termios) < 0)
147
sbuild::log_warning()
148
<< _("Error saving terminal settings")
155
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
156
textdomain (GETTEXT_PACKAGE);
159
sbuild::debug_level = sbuild::DEBUG_NOTICE;
161
sbuild::debug_level = sbuild::DEBUG_NONE;
164
openlog("schroot", LOG_PID|LOG_NDELAY, LOG_AUTHPRIV);
166
/* Parse command-line options into opt structure. */
167
options options(argc, argv);
169
if (options.dchroot_compat && options.verbose)
171
sbuild::log_warning()
172
<< _("Running schroot in dchroot compatibility mode")
175
<< _("Run 'schroot' for full capability")
179
if (options.action == options::ACTION_VERSION)
181
print_version(std::cout, options);
185
/* Initialise chroot configuration. */
186
#ifdef SBUILD_DCHROOT_COMPAT
187
bool use_dchroot_conf = false;
188
if (options.dchroot_compat)
191
if (stat(DCHROOT_CONF, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
193
use_dchroot_conf = true;
197
sbuild::log_warning()
198
<< _("Using dchroot configuration file: ") << DCHROOT_CONF
201
<< format(_("Run '%1%'"))
202
% "dchroot --config >> " SCHROOT_CONF
205
<< _("to migrate to a schroot configuration.")
208
<< format(_("Edit '%1%' to add appropriate group access."))
212
<< format(_("Remove '%1%' to use the new configuration."))
220
sbuild::chroot_config::ptr config;
221
#ifdef SBUILD_DCHROOT_COMPAT
222
if (options.dchroot_compat && use_dchroot_conf)
224
config = sbuild::chroot_config::ptr(new dchroot::chroot_config);
225
if (options.load_chroots == true)
226
config->add(DCHROOT_CONF, false);
231
config = sbuild::chroot_config::ptr(new sbuild::chroot_config);
232
/* The normal chroot list is used when starting a session or running
233
any chroot type or session, or displaying chroot information. */
234
if (options.load_chroots == true)
235
config->add(SCHROOT_CONF, false);
236
/* The session chroot list is used when running or ending an
237
existing session, or displaying chroot information. */
238
if (options.load_sessions == true)
239
config->add(SCHROOT_SESSION_DIR, true);
242
if (config->get_chroots().empty() && options.quiet == false)
244
if (options.load_chroots == true && options.load_sessions == true)
245
sbuild::log_warning()
246
<< format(_("No chroots are defined in %1% or %2%"))
247
% SCHROOT_CONF % SCHROOT_SESSION_DIR
251
const char *cfile = (options.load_sessions) ? SCHROOT_CONF : SCHROOT_SESSION_DIR;
252
sbuild::log_warning()
253
<< format(_("No chroots are defined in %1%")) % cfile
258
/* Print chroot list (including aliases). */
259
if (options.action == options::ACTION_LIST)
261
if (options.dchroot_compat)
262
config->print_chroot_list_simple(std::cout);
264
config->print_chroot_list(std::cout);
268
/* Get list of chroots to use */
269
sbuild::string_list const& chroots = get_chroot_options(config, options);
273
<< format(_("The specified chroots are not defined in %1%"))
279
/* Print chroot information for specified chroots. */
280
if (options.action == options::ACTION_INFO)
282
config->print_chroot_info(chroots, std::cout);
285
if (options.action == options::ACTION_LOCATION)
287
if (options.dchroot_compat)
289
sbuild::string_list chroot;
290
chroot.push_back(options.chroot_path);
291
config->print_chroot_location(chroot, std::cout);
295
config->print_chroot_location(chroots, std::cout);
299
if (options.action == options::ACTION_CONFIG)
302
<< format(_("schroot configuration generated by %1% %2%"))
303
% (options.dchroot_compat ? "dchroot" : "schroot")
306
#ifdef SBUILD_DCHROOT_COMPAT
307
// Help text at head of new config.
308
std::cout << "# " << endl
310
<< _("To allow users access to the chroots, add their groups to the groups keys.") << endl;
312
<< _("To allow passwordless root access, add their groups to the root-groups keys.") << endl;
314
<< format(_("Remove '%1%' to use the new configuration."))
319
config->print_chroot_config(chroots, std::cout);
323
if (options.action == options::ACTION_SESSION_BEGIN &&
327
<< _("Only one chroot may be specified when beginning a session")
332
/* Create a session. */
333
sbuild::session::operation sess_op(sbuild::session::OPERATION_AUTOMATIC);
334
if (options.action == options::ACTION_SESSION_BEGIN)
335
sess_op = sbuild::session::OPERATION_BEGIN;
336
else if (options.action == options::ACTION_SESSION_RECOVER)
337
sess_op = sbuild::session::OPERATION_RECOVER;
338
else if (options.action == options::ACTION_SESSION_RUN)
339
sess_op = sbuild::session::OPERATION_RUN;
340
else if (options.action == options::ACTION_SESSION_END)
341
sess_op = sbuild::session::OPERATION_END;
343
// Using dchroot.conf implies using dchroot_session, which does
344
// not require group access. If loading schroot.conf, we always
345
// want normal session management.
346
sbuild::session::ptr session;
347
#ifdef SBUILD_DCHROOT_COMPAT
348
if (options.dchroot_compat && use_dchroot_conf)
349
session = sbuild::session::ptr
350
(new dchroot::session("schroot", config, sess_op, chroots));
353
session = sbuild::session::ptr
354
(new sbuild::session("schroot", config, sess_op, chroots));
358
if (!options.user.empty() && !options.dchroot_compat)
359
session->set_user(options.user);
360
if (!options.command.empty())
361
session->set_command(options.command);
362
if (options.preserve)
363
session->set_environment(environ);
364
session->set_force(options.session_force);
365
sbuild::auth::verbosity verbosity = sbuild::auth::VERBOSITY_NORMAL;
367
verbosity = sbuild::auth::VERBOSITY_QUIET;
368
else if (options.verbose)
369
verbosity = sbuild::auth::VERBOSITY_VERBOSE;
370
session->set_verbosity(verbosity);
372
/* Set up authentication timeouts. */
373
std::tr1::shared_ptr<sbuild::auth_conv>
374
conv(new sbuild::auth_conv_tty);
377
conv->set_warning_timeout(curtime + 15);
378
conv->set_fatal_timeout(curtime + 20);
379
session->set_conv(conv);
384
catch (std::runtime_error& e)
388
<< format(_("Session failure: %1%")) % e.what() << endl;
393
if (isatty(STDIN_FILENO) && termios_ok)
395
if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
396
sbuild::log_warning()
397
<< _("Error restoring terminal settings")
401
exit(session->get_child_status());
50
schroot::options::ptr opts(new schroot::options);
51
schroot::main kit(opts);
52
exit (kit.run(argc, argv));
403
54
catch (std::exception const& e)
405
56
sbuild::log_error() << e.what() << endl;
409
if (isatty(STDIN_FILENO) && termios_ok)
411
if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
412
sbuild::log_warning()
413
<< _("Error restoring terminal settings")
61
sbuild::log_error() << _("An unknown exception occured") << endl;
417
62
exit(EXIT_FAILURE);