sbuild  1.6.10
sbuild-chroot.h
1 /* Copyright © 2005-2008 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 3 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, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_CHROOT_H
20 #define SBUILD_CHROOT_H
21 
22 #include <sbuild/sbuild-custom-error.h>
23 #include <sbuild/sbuild-environment.h>
24 #include <sbuild/sbuild-format-detail.h>
25 #include <sbuild/sbuild-keyfile.h>
26 #include <sbuild/sbuild-regex.h>
27 #include <sbuild/sbuild-tr1types.h>
28 
29 #include <list>
30 #include <ostream>
31 #include <string>
32 
33 namespace sbuild
34 {
35 
36  class chroot_facet;
37 
45  class chroot
46  {
47  public:
50  {
54  };
55 
58  {
60  SESSION_CREATE = 1 << 0,
61  SESSION_CLONE = 1 << 1,
62  SESSION_PURGE = 1 << 2
63  };
64 
66  enum verbosity
67  {
71  };
72 
75  {
98  };
99 
102 
104  typedef std::shared_ptr<chroot> ptr;
105 
107  typedef std::shared_ptr<const chroot> const_ptr;
108 
109  protected:
111  chroot ();
112 
114  chroot (const chroot& rhs);
115 
116  public:
118  virtual ~chroot ();
119 
126  static ptr
127  create (std::string const& type);
128 
134  virtual ptr
135  clone () const = 0;
136 
146  virtual chroot::ptr
147  clone_session (std::string const& session_id,
148  std::string const& alias,
149  std::string const& user,
150  bool root) const = 0;
151 
157  virtual chroot::ptr
158  clone_source () const = 0;
159 
165  std::string const&
166  get_name () const;
167 
173  void
174  set_name (std::string const& name);
175 
181  std::string const&
182  get_description () const;
183 
189  void
190  set_description (std::string const& description);
191 
197  std::string const&
198  get_mount_location () const;
199 
205  void
206  set_mount_location (std::string const& location);
207 
208  public:
217  virtual std::string
218  get_path () const = 0;
219 
225  string_list const&
226  get_users () const;
227 
233  void
234  set_users (string_list const& users);
235 
241  string_list const&
242  get_groups () const;
243 
249  void
250  set_groups (string_list const& groups);
251 
259  string_list const&
260  get_root_users () const;
261 
269  void
271 
279  string_list const&
280  get_root_groups () const;
281 
289  void
291 
298  string_list const&
299  get_aliases () const;
300 
307  void
309 
315  bool
316  get_preserve_environment () const;
317 
323  void
325 
331  std::string const&
332  get_default_shell () const;
333 
339  void
340  set_default_shell (std::string const& default_shell);
341 
349  regex const&
350  get_environment_filter () const;
351 
359  void
361 
368  bool
369  get_active () const;
370 
376  bool
377  get_original () const;
378 
384  void
385  set_original (bool original);
386 
392  bool
393  get_run_setup_scripts () const;
394 
395  protected:
402  void
404 
405  public:
413  std::string const&
414  get_script_config () const;
415 
423  void
424  set_script_config (std::string const& script_config);
425 
433  std::string const&
434  get_profile () const;
435 
443  void
444  set_profile (std::string const& profile);
445 
452  string_list const&
453  get_command_prefix () const;
454 
461  void
463 
469  verbosity
470  get_verbosity () const;
471 
477  const char *
478  get_verbosity_string () const;
479 
485  void
487 
493  void
494  set_verbosity (std::string const& verbosity);
495 
501  virtual std::string const&
502  get_chroot_type () const = 0;
503 
510  void
511  setup_env (environment& env) const;
512 
520  virtual void
521  setup_env (chroot const& chroot,
522  environment& env) const = 0;
523 
535  void
536  lock (setup_type type);
537 
551  void
552  unlock (setup_type type,
553  int status);
554 
555  protected:
561  virtual void
562  setup_session_info (bool start);
563 
578  virtual void
579  setup_lock(setup_type type,
580  bool lock,
581  int status) = 0;
582 
583  public:
591  template <typename T>
592  std::shared_ptr<T>
593  get_facet ();
594 
602  template <typename T>
603  const std::shared_ptr<const T>
604  get_facet () const;
605 
611  template <typename T>
612  void
613  add_facet (std::shared_ptr<T> facet);
614 
619  template <typename T>
620  void
621  remove_facet ();
622 
628  template <typename T>
629  void
630  remove_facet (std::shared_ptr<T> facet);
631 
637  template <typename T>
638  void
639  replace_facet (std::shared_ptr<T> facet);
640 
647  list_facets () const;
648 
656  get_session_flags () const;
657 
665  virtual chroot::session_flags
666  get_session_flags (chroot const& chroot) const = 0;
667 
677  friend std::ostream&
678  operator << (std::ostream& stream,
679  ptr const& rhs)
680  {
681  rhs->print_details(stream);
682  return stream;
683  }
684 
692  friend
693  keyfile const&
695  ptr& rhs)
696  {
697  rhs->set_keyfile(keyfile);
698  return keyfile;
699  }
700 
708  friend
709  keyfile&
711  ptr const& rhs)
712  {
713  rhs->get_keyfile(keyfile);
714  return keyfile;
715  }
716 
722  void
723  get_details (format_detail& detail) const;
724 
731  virtual void
732  get_details (chroot const& chroot,
733  format_detail& detail) const = 0;
734 
742  void
743  print_details (std::ostream& stream) const;
744 
752  void
753  get_keyfile (keyfile& keyfile) const;
754 
755  protected:
764  virtual void
765  get_keyfile (chroot const& chroot,
766  keyfile& keyfile) const = 0;
767 
768  public:
776  void
777  set_keyfile (keyfile const& keyfile);
778 
779  protected:
789  virtual void
791  keyfile const& keyfile,
792  string_list& used_keys) = 0;
793 
794  private:
796  std::string name;
798  std::string description;
812  std::string default_shell;
816  std::string mount_location;
818  bool original;
822  std::string script_config;
824  std::string profile;
829 
831  typedef std::shared_ptr<chroot_facet> facet_ptr;
833  typedef std::list<facet_ptr> facet_list;
835  facet_list facets;
836  };
837 
846  chroot::session_flags const& rhs)
847  {
848  return static_cast<chroot::session_flags>
849  (static_cast<int>(lhs) | static_cast<int>(rhs));
850  }
851 
860  chroot::session_flags const& rhs)
861  {
862  return static_cast<chroot::session_flags>
863  (static_cast<int>(lhs) & static_cast<int>(rhs));
864  }
865 
866 }
867 
868 #include <sbuild/sbuild-chroot-facet.h>
869 
870 namespace sbuild
871 {
872 
873  template <typename T>
874  std::shared_ptr<T>
876  {
877  std::shared_ptr<T> ret;
878 
879  for (facet_list::const_iterator pos = facets.begin();
880  pos != facets.end();
881  ++pos)
882  {
883  if (ret = std::dynamic_pointer_cast<T>(*pos))
884  break;
885  }
886 
887  return ret;
888  }
889 
890  template <typename T>
891  const std::shared_ptr<const T>
893  {
894  std::shared_ptr<T> ret;
895 
896  for (facet_list::const_iterator pos = facets.begin();
897  pos != facets.end();
898  ++pos)
899  {
900  if (ret = std::dynamic_pointer_cast<T>(*pos))
901  break;
902  }
903 
904  return std::const_pointer_cast<T>(ret);
905  }
906 
907  template <typename T>
908  void
909  chroot::add_facet (std::shared_ptr<T> facet)
910  {
911  facet_ptr new_facet = std::dynamic_pointer_cast<chroot_facet>(facet);
912  if (!new_facet)
913  throw error(FACET_INVALID);
914 
915  for (facet_list::const_iterator pos = facets.begin();
916  pos != facets.end();
917  ++pos)
918  {
919  if (std::dynamic_pointer_cast<T>(*pos))
920  throw error(FACET_PRESENT);
921  }
922 
923  new_facet->set_chroot(*this);
924  facets.push_back(new_facet);
925  }
926 
927  template <typename T>
928  void
930  {
931  for (facet_list::iterator pos = facets.begin();
932  pos != facets.end();
933  ++pos)
934  {
935  if (std::dynamic_pointer_cast<T>(*pos))
936  {
937  facets.erase(pos);
938  break;
939  }
940  }
941  }
942 
943  template <typename T>
944  void
945  chroot::remove_facet (std::shared_ptr<T> facet)
946  {
947  remove_facet<T>();
948  }
949 
950  template <typename T>
951  void
952  chroot::replace_facet (std::shared_ptr<T> facet)
953  {
954  remove_facet<T>();
955  add_facet(facet);
956  }
957 
958 }
959 
960 #endif /* SBUILD_CHROOT_H */
961 
962 /*
963  * Local Variables:
964  * mode:C++
965  * End:
966  */
virtual std::string const & get_chroot_type() const =0
Get the type of the chroot.
POSIX extended regular expression.
Definition: sbuild-regex.h:66
static ptr create(std::string const &type)
Create a chroot.
Definition: sbuild-chroot.cc:170
virtual ptr clone() const =0
Copy the chroot.
void set_keyfile(keyfile const &keyfile)
Set the chroot properties from a keyfile.
Definition: sbuild-chroot.cc:806
std::string const & get_description() const
Get the description of the chroot.
Definition: sbuild-chroot.cc:238
Could not set profile from script configuration path.
Definition: sbuild-chroot.h:94
regex const & get_environment_filter() const
Get the environment filter of the chroot.
Definition: sbuild-chroot.cc:370
Device must have an absolute path.
Definition: sbuild-chroot.h:79
void set_verbosity(verbosity verbosity)
Set the message verbosity.
Definition: sbuild-chroot.cc:498
void replace_facet(std::shared_ptr< T > facet)
Replace an existing chroot facet with a new facet.
Definition: sbuild-chroot.h:952
Activate a chroot.
Definition: sbuild-chroot.h:51
Only print essential messages.
Definition: sbuild-chroot.h:68
TR1 type substitution.
File is not a block device.
Definition: sbuild-chroot.h:81
Debian source builder components.
Definition: sbuild-auth-null.h:24
string_list users
Users allowed to access the chroot.
Definition: sbuild-chroot.h:800
virtual ~chroot()
The destructor.
Definition: sbuild-chroot.cc:165
std::string const & get_script_config() const
Get the script configuration file for the chroot.
Definition: sbuild-chroot.cc:406
void set_environment_filter(regex const &environment_filter)
Set the environment filter of the chroot.
Definition: sbuild-chroot.cc:376
std::shared_ptr< chroot_facet > facet_ptr
A shared pointer to a chroot facet.
Definition: sbuild-chroot.h:831
chroot()
The constructor.
Definition: sbuild-chroot.cc:107
Message verbosity is invalid.
Definition: sbuild-chroot.h:97
void set_aliases(string_list const &aliases)
Set the aliases of the chroot.
Definition: sbuild-chroot.cc:318
std::list< facet_ptr > facet_list
A list of chroot facets.
Definition: sbuild-chroot.h:833
virtual chroot::ptr clone_session(std::string const &session_id, std::string const &alias, std::string const &user, bool root) const =0
Create a session chroot.
verbosity
Message verbosity.
Definition: sbuild-chroot.h:66
Attempt to add facet which is already in use.
Definition: sbuild-chroot.h:85
Failed to lock device.
Definition: sbuild-chroot.h:80
friend std::ostream & operator<<(std::ostream &stream, ptr const &rhs)
Print detailed information about the chroot to a stream.
Definition: sbuild-chroot.h:678
Directory must have an absolute path.
Definition: sbuild-chroot.h:83
std::string default_shell
Default shell.
Definition: sbuild-chroot.h:812
chroot::session_flags operator&(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-AND of specifed session properties.
Definition: sbuild-chroot.h:859
bool get_original() const
Get the originality of the chroot.
Definition: sbuild-chroot.cc:382
Chroot device name not set.
Definition: sbuild-chroot.h:77
verbosity get_verbosity() const
Get the message verbosity.
Definition: sbuild-chroot.cc:465
void setup_env(environment &env) const
Set environment.
Definition: sbuild-chroot.cc:532
bool preserve_environment
Preserve environment?
Definition: sbuild-chroot.h:810
Container of environment variables.
Definition: sbuild-environment.h:38
Attempt to add object which is not a facet.
Definition: sbuild-chroot.h:84
Unknown chroot type.
Definition: sbuild-chroot.h:78
friend keyfile const & operator>>(keyfile const &keyfile, ptr &rhs)
Chroot initialisation from a keyfile.
Definition: sbuild-chroot.h:694
verbosity message_verbosity
The message verbosity.
Definition: sbuild-chroot.h:828
void set_original(bool original)
Set the originality of the chroot.
Definition: sbuild-chroot.cc:388
File has write permissions for others.
Definition: sbuild-chroot.h:90
void set_preserve_environment(bool preserve_environment)
Set if the environment should be preserved in the chroot.
Definition: sbuild-chroot.cc:352
std::string const & get_name() const
Get the name of the chroot.
Definition: sbuild-chroot.cc:208
Advisory locking.
Definition: sbuild-lock.h:38
virtual void setup_lock(setup_type type, bool lock, int status)=0
Unlock a chroot during setup.
Configuration file parser.
Definition: sbuild-basic-keyfile.h:138
File must have an absolute path.
Definition: sbuild-chroot.h:86
File is not a regular file.
Definition: sbuild-chroot.h:88
Failed to unlink session file.
Definition: sbuild-chroot.h:95
regex environment_filter
Environment filter regex.
Definition: sbuild-chroot.h:814
string_list root_users
Users allowed to access the chroot as root.
Definition: sbuild-chroot.h:804
std::string const & get_default_shell() const
Get default shell.
Definition: sbuild-chroot.cc:358
string_list root_groups
Groups allowed to access the chroot as root.
Definition: sbuild-chroot.h:806
Chroot creation failed.
Definition: sbuild-chroot.h:76
string_list list_facets() const
List all registered chroot facets.
Definition: sbuild-chroot.cc:517
std::string name
Chroot name.
Definition: sbuild-chroot.h:796
chroot::session_flags operator|(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-OR of specifed session properties.
Definition: sbuild-chroot.h:845
string_list const & get_command_prefix() const
Get the command_prefix for the chroot.
Definition: sbuild-chroot.cc:453
string_list aliases
Alternative names for the chroot.
Definition: sbuild-chroot.h:808
void lock(setup_type type)
Lock a chroot during setup.
Definition: sbuild-chroot.cc:621
bool get_run_setup_scripts() const
Check if chroot setup scripts will be run.
Definition: sbuild-chroot.cc:394
string_list const & get_groups() const
Get the groups allowed to access the chroot.
Definition: sbuild-chroot.cc:276
void set_users(string_list const &users)
Set the users allowed to access the chroot.
Definition: sbuild-chroot.cc:270
Invalid name.
Definition: sbuild-chroot.h:93
virtual void setup_session_info(bool start)
Set up persistent session information.
Definition: sbuild-chroot.cc:570
string_list const & get_root_users() const
Get the users allowed to access the chroot as root.
Definition: sbuild-chroot.cc:288
void set_script_config(std::string const &script_config)
Set the script configuration file for the chroot.
Definition: sbuild-chroot.cc:412
Failed to unlock device.
Definition: sbuild-chroot.h:82
File is not owned by user root.
Definition: sbuild-chroot.h:89
std::vector< std::string > string_list
A string vector.
Definition: sbuild-types.h:38
string_list groups
Groups allowed to access the chroot.
Definition: sbuild-chroot.h:802
The chroot supports session creation.
Definition: sbuild-chroot.h:60
Custom error.
Definition: sbuild-custom-error.h:32
session_flags get_session_flags() const
Get the session flags of the chroot.
Definition: sbuild-chroot.cc:634
string_list const & get_root_groups() const
Get the groups allowed to access the chroot as root.
Definition: sbuild-chroot.cc:300
bool original
Was the chroot automatically generated?
Definition: sbuild-chroot.h:818
void set_root_groups(string_list const &groups)
Set the groups allowed to access the chroot as root.
Definition: sbuild-chroot.cc:306
basic_keyfile< keyfile_traits, keyfile_parser< keyfile_traits > > keyfile
Configuration file parser.
Definition: sbuild-keyfile.h:159
Failed to acquire lock.
Definition: sbuild-chroot.h:87
void set_mount_location(std::string const &location)
Set the mount location of the chroot.
Definition: sbuild-chroot.cc:256
session_flags
Chroot session properties.
Definition: sbuild-chroot.h:57
Format names and values for output.
Definition: sbuild-format-detail.h:38
virtual std::string get_path() const =0
Get the path to the chroot.
string_list const & get_aliases() const
Get the aliases of the chroot.
Definition: sbuild-chroot.cc:312
void set_run_setup_scripts(bool run_setup_scripts)
Set whether chroot setup scripts will be run.
Definition: sbuild-chroot.cc:400
bool run_setup_scripts
Run chroot setup scripts?
Definition: sbuild-chroot.h:820
setup_type
Type of setup to perform.
Definition: sbuild-chroot.h:49
std::shared_ptr< T > get_facet()
Get a chroot facet.
Definition: sbuild-chroot.h:875
Print all messages.
Definition: sbuild-chroot.h:70
bool get_active() const
Get the activity status of the chroot.
void unlock(setup_type type, int status)
Unlock a chroot during setup.
Definition: sbuild-chroot.cc:627
custom_error< error_code > error
Exception type.
Definition: sbuild-chroot.h:101
void remove_facet()
Remove a chroot facet.
Definition: sbuild-chroot.h:929
Reactivate a chroot.
Definition: sbuild-chroot.h:52
std::string description
Chroot description.
Definition: sbuild-chroot.h:798
std::string profile
Configuration profile for setup scripts (replaces script_config).
Definition: sbuild-chroot.h:824
std::shared_ptr< const chroot > const_ptr
A shared_ptr to a const chroot object.
Definition: sbuild-chroot.h:107
virtual chroot::ptr clone_source() const =0
Create a source chroot.
bool get_preserve_environment() const
Check if the environment should be preserved in the chroot.
Definition: sbuild-chroot.cc:346
The chroot supports cloning.
Definition: sbuild-chroot.h:61
Common chroot data.
Definition: sbuild-chroot.h:45
error_code
Error codes.
Definition: sbuild-chroot.h:74
facet_list facets
Contained chroot facets.
Definition: sbuild-chroot.h:835
std::string const & get_profile() const
Get the configuration profile for the chroot.
Definition: sbuild-chroot.cc:431
void set_profile(std::string const &profile)
Set configuration profile for the chroot.
Definition: sbuild-chroot.cc:437
Failed to write session file.
Definition: sbuild-chroot.h:96
std::string mount_location
Location to mount chroot in the filesystem (if any).
Definition: sbuild-chroot.h:816
void get_keyfile(keyfile &keyfile) const
Copy the chroot properties into a keyfile.
Definition: sbuild-chroot.cc:717
void set_command_prefix(string_list const &command_prefix)
Set the command_prefix for the chroot.
Definition: sbuild-chroot.cc:459
Deactivate a chroot.
Definition: sbuild-chroot.h:53
void set_default_shell(std::string const &default_shell)
Set the default shell.
Definition: sbuild-chroot.cc:364
The chroot should be purged.
Definition: sbuild-chroot.h:62
void add_facet(std::shared_ptr< T > facet)
Add a chroot facet.
Definition: sbuild-chroot.h:909
Failed to discard lock.
Definition: sbuild-chroot.h:91
void get_details(format_detail &detail) const
Get detailed information about the chroot for output.
Definition: sbuild-chroot.cc:649
std::string script_config
Configuration of the setup and exec scripts.
Definition: sbuild-chroot.h:822
const char * get_verbosity_string() const
Get the message verbosity as a readable string.
Definition: sbuild-chroot.cc:471
Common chroot data.
Definition: sbuild-chroot-facet.h:40
std::shared_ptr< chroot > ptr
A shared_ptr to a chroot object.
Definition: sbuild-chroot.h:104
std::string const & get_mount_location() const
Get the mount location of the chroot.
Definition: sbuild-chroot.cc:250
void set_name(std::string const &name)
Set the name of the chroot.
Definition: sbuild-chroot.cc:214
Print messages (the default).
Definition: sbuild-chroot.h:69
void print_details(std::ostream &stream) const
Print detailed information about the chroot to a stream.
Definition: sbuild-chroot.cc:700
void set_root_users(string_list const &users)
Set the users allowed to access the chroot as root.
Definition: sbuild-chroot.cc:294
No flags are set.
Definition: sbuild-chroot.h:59
Location must have an absolute path.
Definition: sbuild-chroot.h:92
void set_description(std::string const &description)
Set the description of the chroot.
Definition: sbuild-chroot.cc:244
string_list const & get_users() const
Get the users allowed to access the chroot.
Definition: sbuild-chroot.cc:264
void set_groups(string_list const &groups)
Set the users allowed to access the chroot.
Definition: sbuild-chroot.cc:282
string_list command_prefix
Command prefix.
Definition: sbuild-chroot.h:826