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 : public std::enable_shared_from_this<chroot>
46  {
47  public:
50  {
54  };
55 
58  {
60  SESSION_CREATE = 1 << 0,
61  SESSION_CLONE = 1 << 1,
62  SESSION_PURGE = 1 << 2,
63  SESSION_SOURCE = 1 << 3
64  };
65 
67  enum verbosity
68  {
72  };
73 
76  {
99  };
100 
103 
105  typedef std::shared_ptr<chroot> ptr;
106 
108  typedef std::shared_ptr<const chroot> const_ptr;
109 
110  protected:
112  chroot ();
113 
115  chroot (const chroot& rhs);
116 
117  public:
119  virtual ~chroot ();
120 
127  static ptr
128  create (std::string const& type);
129 
135  virtual ptr
136  clone () const = 0;
137 
147  virtual chroot::ptr
148  clone_session (std::string const& session_id,
149  std::string const& alias,
150  std::string const& user,
151  bool root) const = 0;
152 
158  virtual chroot::ptr
159  clone_source () const = 0;
160 
166  std::string const&
167  get_name () const;
168 
174  void
175  set_name (std::string const& name);
176 
182  std::string const&
183  get_description () const;
184 
190  void
191  set_description (std::string const& description);
192 
198  std::string const&
199  get_mount_location () const;
200 
206  void
207  set_mount_location (std::string const& location);
208 
209  public:
218  virtual std::string
219  get_path () const = 0;
220 
226  string_list const&
227  get_users () const;
228 
234  void
235  set_users (string_list const& users);
236 
242  string_list const&
243  get_groups () const;
244 
250  void
251  set_groups (string_list const& groups);
252 
260  string_list const&
261  get_root_users () const;
262 
270  void
272 
280  string_list const&
281  get_root_groups () const;
282 
290  void
292 
299  string_list const&
300  get_aliases () const;
301 
308  void
310 
316  bool
317  get_preserve_environment () const;
318 
324  void
326 
332  std::string const&
333  get_default_shell () const;
334 
340  void
341  set_default_shell (std::string const& default_shell);
342 
350  regex const&
351  get_environment_filter () const;
352 
360  void
362 
369  bool
370  get_active () const;
371 
377  bool
378  get_original () const;
379 
385  void
386  set_original (bool original);
387 
393  bool
394  get_run_setup_scripts () const;
395 
396  protected:
403  void
405 
406  public:
414  std::string const&
415  get_script_config () const;
416 
424  void
425  set_script_config (std::string const& script_config);
426 
434  std::string const&
435  get_profile () const;
436 
444  void
445  set_profile (std::string const& profile);
446 
453  string_list const&
454  get_command_prefix () const;
455 
462  void
464 
470  verbosity
471  get_verbosity () const;
472 
478  const char *
479  get_verbosity_string () const;
480 
486  void
488 
494  void
495  set_verbosity (std::string const& verbosity);
496 
502  virtual std::string const&
503  get_chroot_type () const = 0;
504 
511  void
512  setup_env (environment& env) const;
513 
521  virtual void
522  setup_env (chroot const& chroot,
523  environment& env) const = 0;
524 
536  void
537  lock (setup_type type);
538 
552  void
553  unlock (setup_type type,
554  int status);
555 
556  protected:
562  virtual void
563  setup_session_info (bool start);
564 
579  virtual void
580  setup_lock(setup_type type,
581  bool lock,
582  int status) = 0;
583 
584  public:
592  template <typename T>
593  std::shared_ptr<T>
594  get_facet ();
595 
603  template <typename T>
604  const std::shared_ptr<const T>
605  get_facet () const;
606 
612  template <typename T>
613  void
614  add_facet (std::shared_ptr<T> facet);
615 
620  template <typename T>
621  void
622  remove_facet ();
623 
629  template <typename T>
630  void
631  remove_facet (std::shared_ptr<T> facet);
632 
638  template <typename T>
639  void
640  replace_facet (std::shared_ptr<T> facet);
641 
648  list_facets () const;
649 
657  get_session_flags () const;
658 
666  virtual chroot::session_flags
667  get_session_flags (chroot const& chroot) const = 0;
668 
678  friend std::ostream&
679  operator << (std::ostream& stream,
680  ptr const& rhs)
681  {
682  rhs->print_details(stream);
683  return stream;
684  }
685 
693  friend
694  keyfile const&
696  ptr& rhs)
697  {
698  rhs->set_keyfile(keyfile);
699  return keyfile;
700  }
701 
709  friend
710  keyfile&
712  ptr const& rhs)
713  {
714  rhs->get_keyfile(keyfile);
715  return keyfile;
716  }
717 
723  void
724  get_details (format_detail& detail) const;
725 
732  virtual void
733  get_details (chroot const& chroot,
734  format_detail& detail) const = 0;
735 
743  void
744  print_details (std::ostream& stream) const;
745 
753  void
754  get_keyfile (keyfile& keyfile) const;
755 
756  protected:
765  virtual void
766  get_keyfile (chroot const& chroot,
767  keyfile& keyfile) const = 0;
768 
769  public:
777  void
778  set_keyfile (keyfile const& keyfile);
779 
780  protected:
790  virtual void
791  set_keyfile (chroot& chroot,
792  keyfile const& keyfile,
793  string_list& used_keys) = 0;
794 
795  private:
797  std::string name;
799  std::string description;
813  std::string default_shell;
817  std::string mount_location;
819  bool original;
823  std::string script_config;
825  std::string profile;
830 
832  typedef std::shared_ptr<chroot_facet> facet_ptr;
834  typedef std::list<facet_ptr> facet_list;
836  facet_list facets;
837  };
838 
847  chroot::session_flags const& rhs)
848  {
849  return static_cast<chroot::session_flags>
850  (static_cast<int>(lhs) | static_cast<int>(rhs));
851  }
852 
861  chroot::session_flags const& rhs)
862  {
863  return static_cast<chroot::session_flags>
864  (static_cast<int>(lhs) & static_cast<int>(rhs));
865  }
866 
867 }
868 
869 #include <sbuild/sbuild-chroot-facet.h>
870 
871 namespace sbuild
872 {
873 
874  template <typename T>
875  std::shared_ptr<T>
877  {
878  std::shared_ptr<T> ret;
879 
880  for (facet_list::const_iterator pos = facets.begin();
881  pos != facets.end();
882  ++pos)
883  {
884  if (ret = std::dynamic_pointer_cast<T>(*pos))
885  break;
886  }
887 
888  return ret;
889  }
890 
891  template <typename T>
892  const std::shared_ptr<const T>
894  {
895  std::shared_ptr<T> ret;
896 
897  for (facet_list::const_iterator pos = facets.begin();
898  pos != facets.end();
899  ++pos)
900  {
901  if (ret = std::dynamic_pointer_cast<T>(*pos))
902  break;
903  }
904 
905  return std::const_pointer_cast<T>(ret);
906  }
907 
908  template <typename T>
909  void
910  chroot::add_facet (std::shared_ptr<T> facet)
911  {
912  facet_ptr new_facet = std::dynamic_pointer_cast<chroot_facet>(facet);
913  if (!new_facet)
914  throw error(FACET_INVALID);
915 
916  for (facet_list::const_iterator pos = facets.begin();
917  pos != facets.end();
918  ++pos)
919  {
920  if (std::dynamic_pointer_cast<T>(*pos))
921  throw error(FACET_PRESENT);
922  }
923 
924  new_facet->set_chroot(*this);
925  facets.push_back(new_facet);
926  }
927 
928  template <typename T>
929  void
931  {
932  for (facet_list::iterator pos = facets.begin();
933  pos != facets.end();
934  ++pos)
935  {
936  if (std::dynamic_pointer_cast<T>(*pos))
937  {
938  facets.erase(pos);
939  break;
940  }
941  }
942  }
943 
944  template <typename T>
945  void
946  chroot::remove_facet (std::shared_ptr<T> facet)
947  {
948  remove_facet<T>();
949  }
950 
951  template <typename T>
952  void
953  chroot::replace_facet (std::shared_ptr<T> facet)
954  {
955  remove_facet<T>();
956  add_facet(facet);
957  }
958 
959 }
960 
961 #endif /* SBUILD_CHROOT_H */
962 
963 /*
964  * Local Variables:
965  * mode:C++
966  * End:
967  */
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:172
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:810
std::string const & get_description() const
Get the description of the chroot.
Definition: sbuild-chroot.cc:240
Could not set profile from script configuration path.
Definition: sbuild-chroot.h:95
regex const & get_environment_filter() const
Get the environment filter of the chroot.
Definition: sbuild-chroot.cc:372
Device must have an absolute path.
Definition: sbuild-chroot.h:80
void set_verbosity(verbosity verbosity)
Set the message verbosity.
Definition: sbuild-chroot.cc:500
void replace_facet(std::shared_ptr< T > facet)
Replace an existing chroot facet with a new facet.
Definition: sbuild-chroot.h:953
Activate a chroot.
Definition: sbuild-chroot.h:51
Only print essential messages.
Definition: sbuild-chroot.h:69
TR1 type substitution.
File is not a block device.
Definition: sbuild-chroot.h:82
Debian source builder components.
Definition: sbuild-auth-null.h:24
string_list users
Users allowed to access the chroot.
Definition: sbuild-chroot.h:801
virtual ~chroot()
The destructor.
Definition: sbuild-chroot.cc:167
std::string const & get_script_config() const
Get the script configuration file for the chroot.
Definition: sbuild-chroot.cc:408
void set_environment_filter(regex const &environment_filter)
Set the environment filter of the chroot.
Definition: sbuild-chroot.cc:378
std::shared_ptr< chroot_facet > facet_ptr
A shared pointer to a chroot facet.
Definition: sbuild-chroot.h:832
chroot()
The constructor.
Definition: sbuild-chroot.cc:107
Message verbosity is invalid.
Definition: sbuild-chroot.h:98
void set_aliases(string_list const &aliases)
Set the aliases of the chroot.
Definition: sbuild-chroot.cc:320
std::list< facet_ptr > facet_list
A list of chroot facets.
Definition: sbuild-chroot.h:834
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:67
Attempt to add facet which is already in use.
Definition: sbuild-chroot.h:86
Failed to lock device.
Definition: sbuild-chroot.h:81
friend std::ostream & operator<<(std::ostream &stream, ptr const &rhs)
Print detailed information about the chroot to a stream.
Definition: sbuild-chroot.h:679
Directory must have an absolute path.
Definition: sbuild-chroot.h:84
std::string default_shell
Default shell.
Definition: sbuild-chroot.h:813
chroot::session_flags operator&(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-AND of specifed session properties.
Definition: sbuild-chroot.h:860
The chroot is a source chroot.
Definition: sbuild-chroot.h:63
bool get_original() const
Get the originality of the chroot.
Definition: sbuild-chroot.cc:384
Chroot device name not set.
Definition: sbuild-chroot.h:78
verbosity get_verbosity() const
Get the message verbosity.
Definition: sbuild-chroot.cc:467
void setup_env(environment &env) const
Set environment.
Definition: sbuild-chroot.cc:534
bool preserve_environment
Preserve environment?
Definition: sbuild-chroot.h:811
Container of environment variables.
Definition: sbuild-environment.h:38
Attempt to add object which is not a facet.
Definition: sbuild-chroot.h:85
Unknown chroot type.
Definition: sbuild-chroot.h:79
friend keyfile const & operator>>(keyfile const &keyfile, ptr &rhs)
Chroot initialisation from a keyfile.
Definition: sbuild-chroot.h:695
verbosity message_verbosity
The message verbosity.
Definition: sbuild-chroot.h:829
void set_original(bool original)
Set the originality of the chroot.
Definition: sbuild-chroot.cc:390
File has write permissions for others.
Definition: sbuild-chroot.h:91
void set_preserve_environment(bool preserve_environment)
Set if the environment should be preserved in the chroot.
Definition: sbuild-chroot.cc:354
std::string const & get_name() const
Get the name of the chroot.
Definition: sbuild-chroot.cc:210
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:87
File is not a regular file.
Definition: sbuild-chroot.h:89
Failed to unlink session file.
Definition: sbuild-chroot.h:96
regex environment_filter
Environment filter regex.
Definition: sbuild-chroot.h:815
string_list root_users
Users allowed to access the chroot as root.
Definition: sbuild-chroot.h:805
std::string const & get_default_shell() const
Get default shell.
Definition: sbuild-chroot.cc:360
string_list root_groups
Groups allowed to access the chroot as root.
Definition: sbuild-chroot.h:807
Chroot creation failed.
Definition: sbuild-chroot.h:77
string_list list_facets() const
List all registered chroot facets.
Definition: sbuild-chroot.cc:519
std::string name
Chroot name.
Definition: sbuild-chroot.h:797
chroot::session_flags operator|(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-OR of specifed session properties.
Definition: sbuild-chroot.h:846
string_list const & get_command_prefix() const
Get the command_prefix for the chroot.
Definition: sbuild-chroot.cc:455
string_list aliases
Alternative names for the chroot.
Definition: sbuild-chroot.h:809
void lock(setup_type type)
Lock a chroot during setup.
Definition: sbuild-chroot.cc:625
bool get_run_setup_scripts() const
Check if chroot setup scripts will be run.
Definition: sbuild-chroot.cc:396
string_list const & get_groups() const
Get the groups allowed to access the chroot.
Definition: sbuild-chroot.cc:278
void set_users(string_list const &users)
Set the users allowed to access the chroot.
Definition: sbuild-chroot.cc:272
Invalid name.
Definition: sbuild-chroot.h:94
virtual void setup_session_info(bool start)
Set up persistent session information.
Definition: sbuild-chroot.cc:574
string_list const & get_root_users() const
Get the users allowed to access the chroot as root.
Definition: sbuild-chroot.cc:290
void set_script_config(std::string const &script_config)
Set the script configuration file for the chroot.
Definition: sbuild-chroot.cc:414
Failed to unlock device.
Definition: sbuild-chroot.h:83
File is not owned by user root.
Definition: sbuild-chroot.h:90
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:803
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:638
string_list const & get_root_groups() const
Get the groups allowed to access the chroot as root.
Definition: sbuild-chroot.cc:302
bool original
Was the chroot automatically generated?
Definition: sbuild-chroot.h:819
void set_root_groups(string_list const &groups)
Set the groups allowed to access the chroot as root.
Definition: sbuild-chroot.cc:308
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:88
void set_mount_location(std::string const &location)
Set the mount location of the chroot.
Definition: sbuild-chroot.cc:258
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:314
void set_run_setup_scripts(bool run_setup_scripts)
Set whether chroot setup scripts will be run.
Definition: sbuild-chroot.cc:402
bool run_setup_scripts
Run chroot setup scripts?
Definition: sbuild-chroot.h:821
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:876
Print all messages.
Definition: sbuild-chroot.h:71
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:631
custom_error< error_code > error
Exception type.
Definition: sbuild-chroot.h:102
void remove_facet()
Remove a chroot facet.
Definition: sbuild-chroot.h:930
Reactivate a chroot.
Definition: sbuild-chroot.h:52
std::string description
Chroot description.
Definition: sbuild-chroot.h:799
std::string profile
Configuration profile for setup scripts (replaces script_config).
Definition: sbuild-chroot.h:825
std::shared_ptr< const chroot > const_ptr
A shared_ptr to a const chroot object.
Definition: sbuild-chroot.h:108
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:348
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:75
facet_list facets
Contained chroot facets.
Definition: sbuild-chroot.h:836
std::string const & get_profile() const
Get the configuration profile for the chroot.
Definition: sbuild-chroot.cc:433
void set_profile(std::string const &profile)
Set configuration profile for the chroot.
Definition: sbuild-chroot.cc:439
Failed to write session file.
Definition: sbuild-chroot.h:97
std::string mount_location
Location to mount chroot in the filesystem (if any).
Definition: sbuild-chroot.h:817
void get_keyfile(keyfile &keyfile) const
Copy the chroot properties into a keyfile.
Definition: sbuild-chroot.cc:721
void set_command_prefix(string_list const &command_prefix)
Set the command_prefix for the chroot.
Definition: sbuild-chroot.cc:461
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:366
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:910
Failed to discard lock.
Definition: sbuild-chroot.h:92
void get_details(format_detail &detail) const
Get detailed information about the chroot for output.
Definition: sbuild-chroot.cc:653
std::string script_config
Configuration of the setup and exec scripts.
Definition: sbuild-chroot.h:823
const char * get_verbosity_string() const
Get the message verbosity as a readable string.
Definition: sbuild-chroot.cc:473
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:105
std::string const & get_mount_location() const
Get the mount location of the chroot.
Definition: sbuild-chroot.cc:252
void set_name(std::string const &name)
Set the name of the chroot.
Definition: sbuild-chroot.cc:216
Print messages (the default).
Definition: sbuild-chroot.h:70
void print_details(std::ostream &stream) const
Print detailed information about the chroot to a stream.
Definition: sbuild-chroot.cc:704
void set_root_users(string_list const &users)
Set the users allowed to access the chroot as root.
Definition: sbuild-chroot.cc:296
No flags are set.
Definition: sbuild-chroot.h:59
Location must have an absolute path.
Definition: sbuild-chroot.h:93
void set_description(std::string const &description)
Set the description of the chroot.
Definition: sbuild-chroot.cc:246
string_list const & get_users() const
Get the users allowed to access the chroot.
Definition: sbuild-chroot.cc:266
void set_groups(string_list const &groups)
Set the users allowed to access the chroot.
Definition: sbuild-chroot.cc:284
string_list command_prefix
Command prefix.
Definition: sbuild-chroot.h:827