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
*********************************************************************/
29
#include <ext/stdio_filebuf.h>
31
#include <boost/format.hpp>
34
using namespace sbuild;
36
sbuild::chroot::chroot ():
47
run_setup_scripts(false),
48
run_exec_scripts(false),
54
personality("undefined")
60
sbuild::chroot::~chroot ()
65
sbuild::chroot::create (std::string const& type)
67
chroot *new_chroot = 0;
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();
79
format fmt(_("unknown chroot type \"%1%\""));
85
throw error(_("chroot creation failed"));
87
return ptr(new_chroot);
91
sbuild::chroot::get_name () const
97
sbuild::chroot::set_name (std::string const& name)
103
sbuild::chroot::get_description () const
105
return this->description;
109
sbuild::chroot::set_description (std::string const& description)
111
this->description = description;
115
sbuild::chroot::get_mount_location () const
117
return this->mount_location;
121
sbuild::chroot::set_mount_location (std::string const& location)
123
this->mount_location = location;
127
sbuild::chroot::get_location () const
129
return this->location;
133
sbuild::chroot::set_location (std::string const& location)
135
this->location = location;
139
sbuild::chroot::get_path () const
141
return get_mount_location() + get_location();
145
sbuild::chroot::get_mount_device () const
147
return this->mount_device;
151
sbuild::chroot::set_mount_device (std::string const& device)
153
this->mount_device = device;
157
sbuild::chroot::get_priority () const
159
return this->priority;
163
sbuild::chroot::set_priority (unsigned int priority)
165
this->priority = priority;
169
sbuild::chroot::get_groups () const
175
sbuild::chroot::set_groups (string_list const& groups)
177
this->groups = groups;
181
sbuild::chroot::get_root_groups () const
183
return this->root_groups;
187
sbuild::chroot::set_root_groups (string_list const& groups)
189
this->root_groups = groups;
193
sbuild::chroot::get_aliases () const
195
return this->aliases;
199
sbuild::chroot::set_aliases (string_list const& aliases)
201
this->aliases = aliases;
205
sbuild::chroot::get_active () const
211
sbuild::chroot::set_active (bool active)
213
this->active = active;
217
sbuild::chroot::get_run_setup_scripts () const
219
return this->run_setup_scripts;
223
sbuild::chroot::set_run_setup_scripts (bool run_setup_scripts)
225
this->run_setup_scripts = run_setup_scripts;
229
sbuild::chroot::get_run_exec_scripts () const
231
return this->run_exec_scripts;
235
sbuild::chroot::set_run_exec_scripts (bool run_exec_scripts)
237
this->run_exec_scripts = run_exec_scripts;
241
sbuild::chroot::get_command_prefix () const
243
return this->command_prefix;
247
sbuild::chroot::set_command_prefix (string_list const& command_prefix)
249
this->command_prefix = command_prefix;
253
sbuild::chroot::get_persona () const
255
return this->persona;
259
sbuild::chroot::set_persona (personality const& persona)
261
this->persona = persona;
265
sbuild::chroot::setup_env (environment& env)
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());
277
sbuild::chroot::setup_session_info (bool start)
279
/* Create or unlink session information. */
280
std::string file = std::string(SCHROOT_SESSION_DIR) + "/" + get_name();
284
int fd = open(file.c_str(), O_CREAT|O_EXCL|O_WRONLY, 0664);
287
format fmt(_("%1%: failed to create session file: %2%\n"));
288
fmt % file % strerror(errno);
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);
297
__gnu_cxx::stdio_filebuf<char> fdbuf(fd, std::ios::out);
299
std::ostream output(&fdbuf);
300
output.imbue(std::locale("C"));
302
sbuild::file_lock lock(fd);
305
lock.set_lock(lock::LOCK_EXCLUSIVE, 2);
307
catch (lock::error const& e)
309
format fmt(_("%1%: lock acquisition failure: %2%\n"));
310
fmt % file % e.what();
315
get_keyfile(details);
322
catch (lock::error const& e)
324
format fmt(_("%1%: lock discard failure: %2%\n"));
325
fmt % file % e.what();
329
else /* start == false */
331
if (unlink(file.c_str()) != 0)
333
format fmt(_("%1%: failed to unlink session file: %2%\n"));
334
fmt % file % strerror(errno);
341
sbuild::chroot::lock (setup_type type)
343
setup_lock(type, true, 0);
347
sbuild::chroot::unlock (setup_type type,
350
setup_lock(type, false, status);
354
sbuild::chroot::print_details (std::ostream& stream) const
356
if (this->active == true)
357
stream << _(" --- Session ---\n");
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));
374
if (!get_command_prefix().empty())
375
stream << format_details(_("Command Prefix"), get_command_prefix());
377
stream << format_details(_("Personality"), get_persona().get_name());
379
/* Non user-settable properties are listed last. */
380
if (!get_location().empty())
381
stream << format_details(_("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"),
389
if (!get_mount_device().empty())
390
stream << format_details(_("Mount Device"), get_mount_device());
394
sbuild::chroot::get_keyfile (keyfile& keyfile) const
396
keyfile.remove_group(this->name);
398
keyfile.set_value(this->name, "type",
401
keyfile.set_value(this->name, "active",
404
keyfile.set_value(this->name, "run-setup-scripts",
405
get_run_setup_scripts());
407
keyfile.set_value(this->name, "run-exec-scripts",
408
get_run_exec_scripts());
410
keyfile.set_value(this->name, "priority",
413
string_list const& aliases = get_aliases();
414
keyfile.set_list_value(this->name, "aliases",
415
aliases.begin(), aliases.end());
417
keyfile.set_value(this->name, "description",
420
string_list const& groups = get_groups();
421
keyfile.set_list_value(this->name, "groups",
422
groups.begin(), groups.end());
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());
429
keyfile.set_value(this->name, "mount-location",
430
get_mount_location());
433
keyfile.set_value(this->name, "mount-device",
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());
440
keyfile.set_value(this->name, "personality",
441
get_persona().get_name());
445
sbuild::chroot::set_keyfile (keyfile const& keyfile)
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.
450
if (keyfile.get_value(this->name, "active",
451
keyfile::PRIORITY_REQUIRED, active))
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);
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);
468
if (keyfile.get_value(this->name, "priority",
469
keyfile::PRIORITY_OPTIONAL, priority))
470
set_priority(priority);
473
if (keyfile.get_list_value(this->name, "aliases",
474
keyfile::PRIORITY_OPTIONAL,
476
set_aliases(aliases);
478
std::string description;
479
if (keyfile.get_locale_string(this->name, "description",
480
keyfile::PRIORITY_OPTIONAL, description))
481
set_description(description);
484
if (keyfile.get_list_value(this->name, "groups",
485
keyfile::PRIORITY_REQUIRED,
489
string_list root_groups;
490
if (keyfile.get_list_value(this->name, "root-groups",
491
keyfile::PRIORITY_OPTIONAL,
493
set_root_groups(root_groups);
495
std::string mount_location;
496
if (keyfile.get_value(this->name, "mount-location",
498
keyfile::PRIORITY_REQUIRED : keyfile::PRIORITY_DISALLOWED,
500
set_mount_location(mount_location);
502
std::string mount_device;
503
if (keyfile.get_value(this->name, "mount-device",
505
keyfile::PRIORITY_OPTIONAL : keyfile::PRIORITY_DISALLOWED,
507
set_mount_device(mount_device);
509
string_list command_prefix;
510
if (keyfile.get_list_value(this->name, "command-prefix",
511
keyfile::PRIORITY_OPTIONAL,
513
set_command_prefix(command_prefix);
515
std::string persona_name;
516
if (keyfile.get_value(this->name, "personality",
517
keyfile::PRIORITY_OPTIONAL,
520
personality persona (persona_name);
522
if (persona.get_name() == "undefined" &&
523
persona.get_name() != persona_name)
525
std::ostringstream plist;
526
personality::print_personalities(plist);
529
<< format(_("%1% chroot: personality \"%2%\" is unknown.\n"))
530
% this->name % persona_name;
532
<< format(_("Valid personalities: %1%\n")) % plist.str();
535
set_persona(persona);