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

« back to all changes in this revision

Viewing changes to schroot/sbuild-chroot.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 "sbuild.h"
23
 
 
24
 
#include <algorithm>
25
 
#include <cerrno>
26
 
#include <map>
27
 
#include <set>
28
 
#include <utility>
29
 
#include <ext/stdio_filebuf.h>
30
 
 
31
 
#include <boost/format.hpp>
32
 
 
33
 
using boost::format;
34
 
using namespace sbuild;
35
 
 
36
 
sbuild::chroot::chroot ():
37
 
  name(),
38
 
  description(),
39
 
  priority(0),
40
 
  groups(),
41
 
  root_groups(),
42
 
  aliases(),
43
 
  mount_location(),
44
 
  location(),
45
 
  mount_device(),
46
 
  active(false),
47
 
  run_setup_scripts(false),
48
 
  run_exec_scripts(false),
49
 
  command_prefix(),
50
 
  persona(
51
 
#ifdef __linux__
52
 
          personality("linux")
53
 
#else
54
 
          personality("undefined")
55
 
#endif
56
 
          )
57
 
{
58
 
}
59
 
 
60
 
sbuild::chroot::~chroot ()
61
 
{
62
 
}
63
 
 
64
 
sbuild::chroot::ptr
65
 
sbuild::chroot::create (std::string const& type)
66
 
{
67
 
  chroot *new_chroot = 0;
68
 
 
69
 
  if (type == "plain")
70
 
    new_chroot = new chroot_plain();
71
 
  else if (type == "file")
72
 
    new_chroot = new chroot_file();
73
 
  else if (type == "block-device")
74
 
    new_chroot = new chroot_block_device();
75
 
  else if (type == "lvm-snapshot")
76
 
    new_chroot = new chroot_lvm_snapshot();
77
 
  else
78
 
    {
79
 
      format fmt(_("unknown chroot type \"%1%\""));
80
 
      fmt % type;
81
 
      throw error(fmt);
82
 
    }
83
 
 
84
 
  if (new_chroot == 0)
85
 
    throw error(_("chroot creation failed"));
86
 
 
87
 
  return ptr(new_chroot);
88
 
}
89
 
 
90
 
std::string const&
91
 
sbuild::chroot::get_name () const
92
 
{
93
 
  return this->name;
94
 
}
95
 
 
96
 
void
97
 
sbuild::chroot::set_name (std::string const& name)
98
 
{
99
 
  this->name = name;
100
 
}
101
 
 
102
 
std::string const&
103
 
sbuild::chroot::get_description () const
104
 
{
105
 
  return this->description;
106
 
}
107
 
 
108
 
void
109
 
sbuild::chroot::set_description (std::string const& description)
110
 
{
111
 
  this->description = description;
112
 
}
113
 
 
114
 
std::string const&
115
 
sbuild::chroot::get_mount_location () const
116
 
{
117
 
  return this->mount_location;
118
 
}
119
 
 
120
 
void
121
 
sbuild::chroot::set_mount_location (std::string const& location)
122
 
{
123
 
  this->mount_location = location;
124
 
}
125
 
 
126
 
std::string const&
127
 
sbuild::chroot::get_location () const
128
 
{
129
 
  return this->location;
130
 
}
131
 
 
132
 
void
133
 
sbuild::chroot::set_location (std::string const& location)
134
 
{
135
 
  this->location = location;
136
 
}
137
 
 
138
 
std::string
139
 
sbuild::chroot::get_path () const
140
 
{
141
 
  return get_mount_location() + get_location();
142
 
}
143
 
 
144
 
std::string const&
145
 
sbuild::chroot::get_mount_device () const
146
 
{
147
 
  return this->mount_device;
148
 
}
149
 
 
150
 
void
151
 
sbuild::chroot::set_mount_device (std::string const& device)
152
 
{
153
 
  this->mount_device = device;
154
 
}
155
 
 
156
 
unsigned int
157
 
sbuild::chroot::get_priority () const
158
 
{
159
 
  return this->priority;
160
 
}
161
 
 
162
 
void
163
 
sbuild::chroot::set_priority (unsigned int priority)
164
 
{
165
 
  this->priority = priority;
166
 
}
167
 
 
168
 
string_list const&
169
 
sbuild::chroot::get_groups () const
170
 
{
171
 
  return this->groups;
172
 
}
173
 
 
174
 
void
175
 
sbuild::chroot::set_groups (string_list const& groups)
176
 
{
177
 
  this->groups = groups;
178
 
}
179
 
 
180
 
string_list const&
181
 
sbuild::chroot::get_root_groups () const
182
 
{
183
 
  return this->root_groups;
184
 
}
185
 
 
186
 
void
187
 
sbuild::chroot::set_root_groups (string_list const& groups)
188
 
{
189
 
  this->root_groups = groups;
190
 
}
191
 
 
192
 
string_list const&
193
 
sbuild::chroot::get_aliases () const
194
 
{
195
 
  return this->aliases;
196
 
}
197
 
 
198
 
void
199
 
sbuild::chroot::set_aliases (string_list const& aliases)
200
 
{
201
 
  this->aliases = aliases;
202
 
}
203
 
 
204
 
bool
205
 
sbuild::chroot::get_active () const
206
 
{
207
 
  return this->active;
208
 
}
209
 
 
210
 
void
211
 
sbuild::chroot::set_active (bool active)
212
 
{
213
 
  this->active = active;
214
 
}
215
 
 
216
 
bool
217
 
sbuild::chroot::get_run_setup_scripts () const
218
 
{
219
 
  return this->run_setup_scripts;
220
 
}
221
 
 
222
 
void
223
 
sbuild::chroot::set_run_setup_scripts (bool run_setup_scripts)
224
 
{
225
 
  this->run_setup_scripts = run_setup_scripts;
226
 
}
227
 
 
228
 
bool
229
 
sbuild::chroot::get_run_exec_scripts () const
230
 
{
231
 
  return this->run_exec_scripts;
232
 
}
233
 
 
234
 
void
235
 
sbuild::chroot::set_run_exec_scripts (bool run_exec_scripts)
236
 
{
237
 
  this->run_exec_scripts = run_exec_scripts;
238
 
}
239
 
 
240
 
string_list const&
241
 
sbuild::chroot::get_command_prefix () const
242
 
{
243
 
  return this->command_prefix;
244
 
}
245
 
 
246
 
void
247
 
sbuild::chroot::set_command_prefix (string_list const& command_prefix)
248
 
{
249
 
  this->command_prefix = command_prefix;
250
 
}
251
 
 
252
 
personality const&
253
 
sbuild::chroot::get_persona () const
254
 
{
255
 
  return this->persona;
256
 
}
257
 
 
258
 
void
259
 
sbuild::chroot::set_persona (personality const& persona)
260
 
{
261
 
  this->persona = persona;
262
 
}
263
 
 
264
 
void
265
 
sbuild::chroot::setup_env (environment& env)
266
 
{
267
 
  env.add("CHROOT_TYPE", get_chroot_type());
268
 
  env.add("CHROOT_NAME", get_name());
269
 
  env.add("CHROOT_DESCRIPTION", get_description());
270
 
  env.add("CHROOT_LOCATION", get_location());
271
 
  env.add("CHROOT_MOUNT_LOCATION", get_mount_location());
272
 
  env.add("CHROOT_PATH", get_path());
273
 
  env.add("CHROOT_MOUNT_DEVICE", get_mount_device());
274
 
}
275
 
 
276
 
void
277
 
sbuild::chroot::setup_session_info (bool start)
278
 
{
279
 
  /* Create or unlink session information. */
280
 
  std::string file = std::string(SCHROOT_SESSION_DIR) + "/" + get_name();
281
 
 
282
 
  if (start)
283
 
    {
284
 
      int fd = open(file.c_str(), O_CREAT|O_EXCL|O_WRONLY, 0664);
285
 
      if (fd < 0)
286
 
        {
287
 
          format fmt(_("%1%: failed to create session file: %2%\n"));
288
 
          fmt % file % strerror(errno);
289
 
          throw error(fmt);
290
 
        }
291
 
 
292
 
      // Create a stream buffer from the file descriptor.  The fd will
293
 
      // be closed when the buffer is destroyed.
294
 
#ifdef SCHROOT_FILEBUF_OLD
295
 
      __gnu_cxx::stdio_filebuf<char> fdbuf(fd, std::ios::out, true, BUFSIZ);
296
 
#else
297
 
      __gnu_cxx::stdio_filebuf<char> fdbuf(fd, std::ios::out);
298
 
#endif
299
 
      std::ostream output(&fdbuf);
300
 
      output.imbue(std::locale("C"));
301
 
 
302
 
      sbuild::file_lock lock(fd);
303
 
      try
304
 
        {
305
 
          lock.set_lock(lock::LOCK_EXCLUSIVE, 2);
306
 
        }
307
 
      catch (lock::error const& e)
308
 
        {
309
 
          format fmt(_("%1%: lock acquisition failure: %2%\n"));
310
 
          fmt % file % e.what();
311
 
          throw error(fmt);
312
 
        }
313
 
 
314
 
      keyfile details;
315
 
      get_keyfile(details);
316
 
      output << details;
317
 
 
318
 
      try
319
 
        {
320
 
          lock.unset_lock();
321
 
        }
322
 
      catch (lock::error const& e)
323
 
        {
324
 
          format fmt(_("%1%: lock discard failure: %2%\n"));
325
 
          fmt % file % e.what();
326
 
          throw error(fmt);
327
 
        }
328
 
    }
329
 
  else /* start == false */
330
 
    {
331
 
      if (unlink(file.c_str()) != 0)
332
 
        {
333
 
          format fmt(_("%1%: failed to unlink session file: %2%\n"));
334
 
          fmt % file % strerror(errno);
335
 
          throw error(fmt);
336
 
        }
337
 
    }
338
 
}
339
 
 
340
 
void
341
 
sbuild::chroot::lock (setup_type type)
342
 
{
343
 
  setup_lock(type, true, 0);
344
 
}
345
 
 
346
 
void
347
 
sbuild::chroot::unlock (setup_type type,
348
 
                        int        status)
349
 
{
350
 
  setup_lock(type, false, status);
351
 
}
352
 
 
353
 
void
354
 
sbuild::chroot::print_details (std::ostream& stream) const
355
 
{
356
 
  if (this->active == true)
357
 
    stream << _("  --- Session ---\n");
358
 
  else
359
 
    stream << _("  --- Chroot ---\n");
360
 
  stream << format_details(_("Name"), get_name())
361
 
         << format_details(_("Description"), get_description())
362
 
         << format_details(_("Type"), get_chroot_type())
363
 
         << format_details(_("Priority"), get_priority())
364
 
         << format_details(_("Groups"), get_groups())
365
 
         << format_details(_("Root Groups"), get_root_groups())
366
 
         << format_details(_("Aliases"), get_aliases())
367
 
         << format_details(_("Run Setup Scripts"), get_run_setup_scripts())
368
 
         << format_details(_("Run Execution Scripts"),
369
 
                           get_run_exec_scripts())
370
 
         << format_details(_("Session Managed"),
371
 
                           static_cast<bool>(get_session_flags() &
372
 
                                             chroot::SESSION_CREATE));
373
 
 
374
 
  if (!get_command_prefix().empty())
375
 
    stream << format_details(_("Command Prefix"), get_command_prefix());
376
 
 
377
 
  stream << format_details(_("Personality"), get_persona().get_name());
378
 
 
379
 
  /* Non user-settable properties are listed last. */
380
 
  if (!get_location().empty())
381
 
    stream << format_details(_("Location"),
382
 
                             get_location());
383
 
  if (!get_mount_location().empty())
384
 
    stream << format_details(_("Mount Location"),
385
 
                             get_mount_location());
386
 
  if (!get_path().empty())
387
 
    stream << format_details(_("Path"),
388
 
                             get_path());
389
 
  if (!get_mount_device().empty())
390
 
    stream << format_details(_("Mount Device"), get_mount_device());
391
 
}
392
 
 
393
 
void
394
 
sbuild::chroot::get_keyfile (keyfile& keyfile) const
395
 
{
396
 
  keyfile.remove_group(this->name);
397
 
 
398
 
  keyfile.set_value(this->name, "type",
399
 
                    get_chroot_type());
400
 
 
401
 
  keyfile.set_value(this->name, "active",
402
 
                    get_active());
403
 
 
404
 
  keyfile.set_value(this->name, "run-setup-scripts",
405
 
                    get_run_setup_scripts());
406
 
 
407
 
  keyfile.set_value(this->name, "run-exec-scripts",
408
 
                    get_run_exec_scripts());
409
 
 
410
 
  keyfile.set_value(this->name, "priority",
411
 
                    get_priority());
412
 
 
413
 
  string_list const& aliases = get_aliases();
414
 
  keyfile.set_list_value(this->name, "aliases",
415
 
                         aliases.begin(), aliases.end());
416
 
 
417
 
  keyfile.set_value(this->name, "description",
418
 
                    get_description());
419
 
 
420
 
  string_list const& groups = get_groups();
421
 
  keyfile.set_list_value(this->name, "groups",
422
 
                         groups.begin(), groups.end());
423
 
 
424
 
  string_list const& root_groups = get_root_groups();
425
 
  keyfile.set_list_value(this->name, "root-groups",
426
 
                         root_groups.begin(), root_groups.end());
427
 
 
428
 
  if (get_active())
429
 
    keyfile.set_value(this->name, "mount-location",
430
 
                      get_mount_location());
431
 
 
432
 
  if (get_active())
433
 
    keyfile.set_value(this->name, "mount-device",
434
 
                      get_mount_device());
435
 
 
436
 
  string_list const& command_prefix = get_command_prefix();
437
 
  keyfile.set_list_value(this->name, "command-prefix",
438
 
                         command_prefix.begin(), command_prefix.end());
439
 
 
440
 
  keyfile.set_value(this->name, "personality",
441
 
                    get_persona().get_name());
442
 
}
443
 
 
444
 
void
445
 
sbuild::chroot::set_keyfile (keyfile const& keyfile)
446
 
{
447
 
  // This is set not in the configuration file, but set in the keyfile
448
 
  // manually.  The user must not have the ability to set this option.
449
 
  bool active(false);
450
 
  if (keyfile.get_value(this->name, "active",
451
 
                        keyfile::PRIORITY_REQUIRED, active))
452
 
    set_active(active);
453
 
 
454
 
  bool run_setup_scripts(false);
455
 
  if (keyfile.get_value(this->name, "run-setup-scripts",
456
 
                        keyfile::PRIORITY_OPTIONAL, run_setup_scripts))
457
 
    set_run_setup_scripts(run_setup_scripts);
458
 
 
459
 
  bool run_exec_scripts(false);
460
 
  if (keyfile.get_value(this->name, "run-session-scripts",
461
 
                        keyfile::PRIORITY_DEPRECATED, run_exec_scripts))
462
 
    set_run_exec_scripts(run_exec_scripts);
463
 
  if (keyfile.get_value(this->name, "run-exec-scripts",
464
 
                        keyfile::PRIORITY_OPTIONAL, run_exec_scripts))
465
 
    set_run_exec_scripts(run_exec_scripts);
466
 
 
467
 
  int priority(0);
468
 
  if (keyfile.get_value(this->name, "priority",
469
 
                        keyfile::PRIORITY_OPTIONAL, priority))
470
 
    set_priority(priority);
471
 
 
472
 
  string_list aliases;
473
 
  if (keyfile.get_list_value(this->name, "aliases",
474
 
                             keyfile::PRIORITY_OPTIONAL,
475
 
                             aliases))
476
 
    set_aliases(aliases);
477
 
 
478
 
  std::string description;
479
 
  if (keyfile.get_locale_string(this->name, "description",
480
 
                                keyfile::PRIORITY_OPTIONAL, description))
481
 
    set_description(description);
482
 
 
483
 
  string_list groups;
484
 
  if (keyfile.get_list_value(this->name, "groups",
485
 
                             keyfile::PRIORITY_REQUIRED,
486
 
                             groups))
487
 
    set_groups(groups);
488
 
 
489
 
  string_list root_groups;
490
 
  if (keyfile.get_list_value(this->name, "root-groups",
491
 
                             keyfile::PRIORITY_OPTIONAL,
492
 
                             root_groups))
493
 
    set_root_groups(root_groups);
494
 
 
495
 
  std::string mount_location;
496
 
  if (keyfile.get_value(this->name, "mount-location",
497
 
                        get_active() ?
498
 
                        keyfile::PRIORITY_REQUIRED : keyfile::PRIORITY_DISALLOWED,
499
 
                        mount_location))
500
 
    set_mount_location(mount_location);
501
 
 
502
 
  std::string mount_device;
503
 
  if (keyfile.get_value(this->name, "mount-device",
504
 
                        get_active() ?
505
 
                        keyfile::PRIORITY_OPTIONAL : keyfile::PRIORITY_DISALLOWED,
506
 
                        mount_device))
507
 
    set_mount_device(mount_device);
508
 
 
509
 
  string_list command_prefix;
510
 
  if (keyfile.get_list_value(this->name, "command-prefix",
511
 
                             keyfile::PRIORITY_OPTIONAL,
512
 
                             command_prefix))
513
 
    set_command_prefix(command_prefix);
514
 
 
515
 
  std::string persona_name;
516
 
  if (keyfile.get_value(this->name, "personality",
517
 
                        keyfile::PRIORITY_OPTIONAL,
518
 
                        persona_name))
519
 
    {
520
 
      personality persona (persona_name);
521
 
 
522
 
      if (persona.get_name() == "undefined" &&
523
 
          persona.get_name() != persona_name)
524
 
        {
525
 
          std::ostringstream plist;
526
 
          personality::print_personalities(plist);
527
 
 
528
 
          log_warning()
529
 
            << format(_("%1% chroot: personality \"%2%\" is unknown.\n"))
530
 
            % this->name % persona_name;
531
 
          log_info()
532
 
            << format(_("Valid personalities: %1%\n")) % plist.str();
533
 
        }
534
 
 
535
 
      set_persona(persona);
536
 
    }
537
 
}
538
 
 
539
 
/*
540
 
 * Local Variables:
541
 
 * mode:C++
542
 
 * End:
543
 
 */