~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to schroot/schroot-main-base.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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,
 
16
 * MA  02111-1307  USA
 
17
 *
 
18
 *********************************************************************/
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include "schroot-main-base.h"
 
23
 
 
24
#include <sbuild/sbuild-auth-conv.h>
 
25
#include <sbuild/sbuild-auth-conv-tty.h>
 
26
 
 
27
#include <cstdlib>
 
28
#include <ctime>
 
29
#include <iostream>
 
30
#include <locale>
 
31
 
 
32
#include <termios.h>
 
33
#include <unistd.h>
 
34
 
 
35
#include <boost/format.hpp>
 
36
 
 
37
using std::endl;
 
38
using boost::format;
 
39
using namespace schroot;
 
40
 
 
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)),
 
46
  options(options)
 
47
{
 
48
}
 
49
 
 
50
main_base::~main_base ()
 
51
{
 
52
}
 
53
 
 
54
void
 
55
main_base::action_info ()
 
56
{
 
57
  this->config->print_chroot_info(this->chroots, std::cout);
 
58
}
 
59
 
 
60
void
 
61
main_base::action_location ()
 
62
{
 
63
  this->config->print_chroot_location(this->chroots, std::cout);
 
64
}
 
65
 
 
66
void
 
67
main_base::compat_check ()
 
68
{
 
69
}
 
70
 
 
71
sbuild::string_list
 
72
main_base::get_chroot_options ()
 
73
{
 
74
  sbuild::string_list ret;
 
75
 
 
76
  if (this->options->all_chroots == true ||
 
77
      this->options->all_sessions == true)
 
78
    {
 
79
      sbuild::chroot_config::chroot_list const& list =
 
80
        this->config->get_chroots();
 
81
 
 
82
      for (sbuild::chroot_config::chroot_list::const_iterator chroot =
 
83
             list.begin();
 
84
           chroot != list.end();
 
85
           ++chroot)
 
86
        {
 
87
          if (((*chroot)->get_active() == false &&
 
88
               this->options->all_chroots == false) ||
 
89
              ((*chroot)->get_active() == true &&
 
90
               this->options->all_sessions == false))
 
91
            continue;
 
92
          ret.push_back((*chroot)->get_name());
 
93
        }
 
94
    }
 
95
  else
 
96
    {
 
97
      sbuild::string_list invalid_chroots =
 
98
        this->config->validate_chroots(this->options->chroots);
 
99
 
 
100
      if (!invalid_chroots.empty())
 
101
        {
 
102
          for (sbuild::string_list::const_iterator chroot =
 
103
                 invalid_chroots.begin();
 
104
               chroot != invalid_chroots.end();
 
105
               ++chroot)
 
106
            sbuild::log_error() << format(_("%1%: No such chroot")) % *chroot
 
107
                                << endl;
 
108
          exit(EXIT_FAILURE);
 
109
        }
 
110
      ret = this->options->chroots;
 
111
    }
 
112
 
 
113
  return ret;
 
114
}
 
115
 
 
116
void
 
117
main_base::load_config ()
 
118
{
 
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);
 
128
}
 
129
 
 
130
int
 
131
main_base::run_impl ()
 
132
{
 
133
  struct termios saved_termios;
 
134
  bool termios_ok = false;
 
135
 
 
136
  try
 
137
    {
 
138
      // Set up locale.
 
139
      std::locale::global(std::locale(""));
 
140
      std::cout.imbue(std::locale());
 
141
      std::cerr.imbue(std::locale());
 
142
 
 
143
      // Save terminal state.
 
144
      if (isatty(STDIN_FILENO))
 
145
        {
 
146
          if (tcgetattr(STDIN_FILENO, &saved_termios) < 0)
 
147
            {
 
148
              termios_ok = false;
 
149
              sbuild::log_warning()
 
150
                << _("Error saving terminal settings")
 
151
                << endl;
 
152
            }
 
153
          else
 
154
            termios_ok = true;
 
155
        }
 
156
 
 
157
      bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 
158
      textdomain (GETTEXT_PACKAGE);
 
159
 
 
160
#ifdef SBUILD_DEBUG
 
161
      sbuild::debug_level = sbuild::DEBUG_CRITICAL;
 
162
#endif
 
163
 
 
164
      openlog("schroot", LOG_PID|LOG_NDELAY, LOG_AUTHPRIV);
 
165
 
 
166
      compat_check();
 
167
 
 
168
      if (this->options->action == options_base::ACTION_HELP)
 
169
        {
 
170
          action_help(std::cout);
 
171
          exit(EXIT_SUCCESS);
 
172
        }
 
173
 
 
174
      if (this->options->action == options_base::ACTION_VERSION)
 
175
        {
 
176
          action_version(std::cout);
 
177
          exit(EXIT_SUCCESS);
 
178
        }
 
179
 
 
180
      /* Initialise chroot configuration. */
 
181
      load_config();
 
182
 
 
183
  if (this->config->get_chroots().empty() && this->options->quiet == false)
 
184
    {
 
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
 
190
          << endl;
 
191
      else
 
192
        {
 
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
 
197
            << endl;
 
198
        }
 
199
    }
 
200
 
 
201
      /* Print chroot list (including aliases). */
 
202
      if (this->options->action == options_base::ACTION_LIST)
 
203
        {
 
204
          action_list();
 
205
          exit(EXIT_SUCCESS);
 
206
        }
 
207
 
 
208
      /* Get list of chroots to use */
 
209
      chroots = get_chroot_options();
 
210
      if (this->chroots.empty())
 
211
        {
 
212
          sbuild::log_error()
 
213
            << format(_("The specified chroots are not defined in %1%"))
 
214
            % SCHROOT_CONF
 
215
            << endl;
 
216
          exit (EXIT_FAILURE);
 
217
        }
 
218
 
 
219
      /* Print chroot information for specified chroots. */
 
220
      if (this->options->action == options_base::ACTION_INFO)
 
221
        {
 
222
          action_info();
 
223
          exit (EXIT_SUCCESS);
 
224
        }
 
225
      if (this->options->action == options_base::ACTION_LOCATION)
 
226
        {
 
227
          action_location();
 
228
          exit (EXIT_SUCCESS);
 
229
        }
 
230
      if (this->options->action == options_base::ACTION_CONFIG)
 
231
        {
 
232
          action_config();
 
233
          exit (EXIT_SUCCESS);
 
234
        }
 
235
 
 
236
      if (this->options->action == options_base::ACTION_SESSION_BEGIN &&
 
237
          this->chroots.size() != 1)
 
238
        {
 
239
          sbuild::log_error()
 
240
            << _("Only one chroot may be specified when beginning a session")
 
241
            << endl;
 
242
          exit (EXIT_FAILURE);
 
243
        }
 
244
 
 
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;
 
255
 
 
256
 
 
257
      try
 
258
        {
 
259
          create_session(sess_op);
 
260
 
 
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);
 
272
 
 
273
          /* Set up authentication timeouts. */
 
274
          std::tr1::shared_ptr<sbuild::auth_conv>
 
275
            conv(new sbuild::auth_conv_tty);
 
276
          time_t curtime = 0;
 
277
          time(&curtime);
 
278
          conv->set_warning_timeout(curtime + 15);
 
279
          conv->set_fatal_timeout(curtime + 20);
 
280
          this->session->set_conv(conv);
 
281
 
 
282
          /* Run session. */
 
283
          this->session->run();
 
284
        }
 
285
      catch (std::runtime_error& e)
 
286
        {
 
287
          if (!this->options->quiet)
 
288
            sbuild::log_error() << e.what() << endl;
 
289
        }
 
290
 
 
291
      closelog();
 
292
 
 
293
      if (isatty(STDIN_FILENO) && termios_ok)
 
294
        {
 
295
          if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
 
296
            sbuild::log_warning()
 
297
              << _("Error restoring terminal settings")
 
298
              << endl;
 
299
        }
 
300
 
 
301
      exit(this->session->get_child_status());
 
302
    }
 
303
  catch (std::exception const& e)
 
304
    {
 
305
      sbuild::log_error() << e.what() << endl;
 
306
 
 
307
      closelog();
 
308
 
 
309
      if (isatty(STDIN_FILENO) && termios_ok)
 
310
        {
 
311
          if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_termios) < 0)
 
312
            sbuild::log_warning()
 
313
              << _("Error restoring terminal settings")
 
314
              << endl;
 
315
        }
 
316
 
 
317
      exit(EXIT_FAILURE);
 
318
    }
 
319
}
 
320
 
 
321
/*
 
322
 * Local Variables:
 
323
 * mode:C++
 
324
 * End:
 
325
 */