00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SBUILD_CHROOT_H
00020 #define SBUILD_CHROOT_H
00021
00022 #include <sbuild/sbuild-custom-error.h>
00023 #include <sbuild/sbuild-environment.h>
00024 #include <sbuild/sbuild-format-detail.h>
00025 #include <sbuild/sbuild-keyfile.h>
00026 #include <sbuild/sbuild-regex.h>
00027 #include <sbuild/sbuild-tr1types.h>
00028
00029 #include <list>
00030 #include <ostream>
00031 #include <string>
00032
00033 namespace sbuild
00034 {
00035
00036 class chroot_facet;
00037
00045 class chroot
00046 {
00047 public:
00049 enum setup_type
00050 {
00051 SETUP_START,
00052 SETUP_RECOVER,
00053 SETUP_STOP
00054 };
00055
00057 enum session_flags
00058 {
00059 SESSION_NOFLAGS = 0,
00060 SESSION_CREATE = 1 << 0,
00061 SESSION_CLONE = 1 << 1,
00062 SESSION_PURGE = 1 << 2
00063 };
00064
00066 enum verbosity
00067 {
00068 VERBOSITY_QUIET,
00069 VERBOSITY_NORMAL,
00070 VERBOSITY_VERBOSE
00071 };
00072
00074 enum error_code
00075 {
00076 CHROOT_CREATE,
00077 CHROOT_DEVICE,
00078 CHROOT_TYPE,
00079 DEVICE_ABS,
00080 DEVICE_LOCK,
00081 DEVICE_NOTBLOCK,
00082 DEVICE_UNLOCK,
00083 DIRECTORY_ABS,
00084 FACET_INVALID,
00085 FACET_PRESENT,
00086 FILE_ABS,
00087 FILE_LOCK,
00088 FILE_NOTREG,
00089 FILE_OWNER,
00090 FILE_PERMS,
00091 FILE_UNLOCK,
00092 LOCATION_ABS,
00093 NAME_INVALID,
00094 SCRIPT_CONFIG_CV,
00095 SESSION_UNLINK,
00096 SESSION_WRITE,
00097 VERBOSITY_INVALID
00098 };
00099
00101 typedef custom_error<error_code> error;
00102
00104 typedef std::shared_ptr<chroot> ptr;
00105
00107 typedef std::shared_ptr<const chroot> const_ptr;
00108
00109 protected:
00111 chroot ();
00112
00114 chroot (const chroot& rhs);
00115
00116 public:
00118 virtual ~chroot ();
00119
00126 static ptr
00127 create (std::string const& type);
00128
00134 virtual ptr
00135 clone () const = 0;
00136
00146 virtual chroot::ptr
00147 clone_session (std::string const& session_id,
00148 std::string const& alias,
00149 std::string const& user,
00150 bool root) const = 0;
00151
00157 virtual chroot::ptr
00158 clone_source () const = 0;
00159
00165 std::string const&
00166 get_name () const;
00167
00173 void
00174 set_name (std::string const& name);
00175
00181 std::string const&
00182 get_description () const;
00183
00189 void
00190 set_description (std::string const& description);
00191
00197 std::string const&
00198 get_mount_location () const;
00199
00205 void
00206 set_mount_location (std::string const& location);
00207
00208 public:
00217 virtual std::string
00218 get_path () const = 0;
00219
00225 string_list const&
00226 get_users () const;
00227
00233 void
00234 set_users (string_list const& users);
00235
00241 string_list const&
00242 get_groups () const;
00243
00249 void
00250 set_groups (string_list const& groups);
00251
00259 string_list const&
00260 get_root_users () const;
00261
00269 void
00270 set_root_users (string_list const& users);
00271
00279 string_list const&
00280 get_root_groups () const;
00281
00289 void
00290 set_root_groups (string_list const& groups);
00291
00298 string_list const&
00299 get_aliases () const;
00300
00307 void
00308 set_aliases (string_list const& aliases);
00309
00315 bool
00316 get_preserve_environment () const;
00317
00323 void
00324 set_preserve_environment (bool preserve_environment);
00325
00331 std::string const&
00332 get_default_shell () const;
00333
00339 void
00340 set_default_shell (std::string const& default_shell);
00341
00349 regex const&
00350 get_environment_filter () const;
00351
00359 void
00360 set_environment_filter (regex const& environment_filter);
00361
00368 bool
00369 get_active () const;
00370
00376 bool
00377 get_original () const;
00378
00384 void
00385 set_original (bool original);
00386
00392 bool
00393 get_run_setup_scripts () const;
00394
00395 protected:
00402 void
00403 set_run_setup_scripts (bool run_setup_scripts);
00404
00405 public:
00413 std::string const&
00414 get_script_config () const;
00415
00423 void
00424 set_script_config (std::string const& script_config);
00425
00433 std::string const&
00434 get_profile () const;
00435
00443 void
00444 set_profile (std::string const& profile);
00445
00452 string_list const&
00453 get_command_prefix () const;
00454
00461 void
00462 set_command_prefix (string_list const& command_prefix);
00463
00469 verbosity
00470 get_verbosity () const;
00471
00477 const char *
00478 get_verbosity_string () const;
00479
00485 void
00486 set_verbosity (verbosity verbosity);
00487
00493 void
00494 set_verbosity (std::string const& verbosity);
00495
00501 virtual std::string const&
00502 get_chroot_type () const = 0;
00503
00510 void
00511 setup_env (environment& env) const;
00512
00520 virtual void
00521 setup_env (chroot const& chroot,
00522 environment& env) const = 0;
00523
00535 void
00536 lock (setup_type type);
00537
00551 void
00552 unlock (setup_type type,
00553 int status);
00554
00555 protected:
00561 virtual void
00562 setup_session_info (bool start);
00563
00578 virtual void
00579 setup_lock(setup_type type,
00580 bool lock,
00581 int status) = 0;
00582
00583 public:
00591 template <typename T>
00592 std::shared_ptr<T>
00593 get_facet ();
00594
00602 template <typename T>
00603 const std::shared_ptr<const T>
00604 get_facet () const;
00605
00611 template <typename T>
00612 void
00613 add_facet (std::shared_ptr<T> facet);
00614
00619 template <typename T>
00620 void
00621 remove_facet ();
00622
00628 template <typename T>
00629 void
00630 remove_facet (std::shared_ptr<T> facet);
00631
00637 template <typename T>
00638 void
00639 replace_facet (std::shared_ptr<T> facet);
00640
00646 string_list
00647 list_facets () const;
00648
00655 session_flags
00656 get_session_flags () const;
00657
00665 virtual chroot::session_flags
00666 get_session_flags (chroot const& chroot) const = 0;
00667
00677 friend std::ostream&
00678 operator << (std::ostream& stream,
00679 ptr const& rhs)
00680 {
00681 rhs->print_details(stream);
00682 return stream;
00683 }
00684
00692 friend
00693 keyfile const&
00694 operator >> (keyfile const& keyfile,
00695 ptr& rhs)
00696 {
00697 rhs->set_keyfile(keyfile);
00698 return keyfile;
00699 }
00700
00708 friend
00709 keyfile&
00710 operator << (keyfile& keyfile,
00711 ptr const& rhs)
00712 {
00713 rhs->get_keyfile(keyfile);
00714 return keyfile;
00715 }
00716
00722 void
00723 get_details (format_detail& detail) const;
00724
00731 virtual void
00732 get_details (chroot const& chroot,
00733 format_detail& detail) const = 0;
00734
00742 void
00743 print_details (std::ostream& stream) const;
00744
00752 void
00753 get_keyfile (keyfile& keyfile) const;
00754
00755 protected:
00764 virtual void
00765 get_keyfile (chroot const& chroot,
00766 keyfile& keyfile) const = 0;
00767
00768 public:
00776 void
00777 set_keyfile (keyfile const& keyfile);
00778
00779 protected:
00789 virtual void
00790 set_keyfile (chroot& chroot,
00791 keyfile const& keyfile,
00792 string_list& used_keys) = 0;
00793
00794 private:
00796 std::string name;
00798 std::string description;
00800 string_list users;
00802 string_list groups;
00804 string_list root_users;
00806 string_list root_groups;
00808 string_list aliases;
00810 bool preserve_environment;
00812 std::string default_shell;
00814 regex environment_filter;
00816 std::string mount_location;
00818 bool original;
00820 bool run_setup_scripts;
00822 std::string script_config;
00824 std::string profile;
00826 string_list command_prefix;
00828 verbosity message_verbosity;
00829
00831 typedef std::shared_ptr<chroot_facet> facet_ptr;
00833 typedef std::list<facet_ptr> facet_list;
00835 facet_list facets;
00836 };
00837
00844 chroot::session_flags
00845 inline operator | (chroot::session_flags const& lhs,
00846 chroot::session_flags const& rhs)
00847 {
00848 return static_cast<chroot::session_flags>
00849 (static_cast<int>(lhs) | static_cast<int>(rhs));
00850 }
00851
00858 chroot::session_flags
00859 inline operator & (chroot::session_flags const& lhs,
00860 chroot::session_flags const& rhs)
00861 {
00862 return static_cast<chroot::session_flags>
00863 (static_cast<int>(lhs) & static_cast<int>(rhs));
00864 }
00865
00866 }
00867
00868 #include <sbuild/sbuild-chroot-facet.h>
00869
00870 namespace sbuild
00871 {
00872
00873 template <typename T>
00874 std::shared_ptr<T>
00875 chroot::get_facet ()
00876 {
00877 std::shared_ptr<T> ret;
00878
00879 for (facet_list::const_iterator pos = facets.begin();
00880 pos != facets.end();
00881 ++pos)
00882 {
00883 if (ret = std::dynamic_pointer_cast<T>(*pos))
00884 break;
00885 }
00886
00887 return ret;
00888 }
00889
00890 template <typename T>
00891 const std::shared_ptr<const T>
00892 chroot::get_facet () const
00893 {
00894 std::shared_ptr<T> ret;
00895
00896 for (facet_list::const_iterator pos = facets.begin();
00897 pos != facets.end();
00898 ++pos)
00899 {
00900 if (ret = std::dynamic_pointer_cast<T>(*pos))
00901 break;
00902 }
00903
00904 return std::const_pointer_cast<T>(ret);
00905 }
00906
00907 template <typename T>
00908 void
00909 chroot::add_facet (std::shared_ptr<T> facet)
00910 {
00911 facet_ptr new_facet = std::dynamic_pointer_cast<chroot_facet>(facet);
00912 if (!new_facet)
00913 throw error(FACET_INVALID);
00914
00915 for (facet_list::const_iterator pos = facets.begin();
00916 pos != facets.end();
00917 ++pos)
00918 {
00919 if (std::dynamic_pointer_cast<T>(*pos))
00920 throw error(FACET_PRESENT);
00921 }
00922
00923 new_facet->set_chroot(*this);
00924 facets.push_back(new_facet);
00925 }
00926
00927 template <typename T>
00928 void
00929 chroot::remove_facet ()
00930 {
00931 for (facet_list::iterator pos = facets.begin();
00932 pos != facets.end();
00933 ++pos)
00934 {
00935 if (std::dynamic_pointer_cast<T>(*pos))
00936 {
00937 facets.erase(pos);
00938 break;
00939 }
00940 }
00941 }
00942
00943 template <typename T>
00944 void
00945 chroot::remove_facet (std::shared_ptr<T> facet)
00946 {
00947 remove_facet<T>();
00948 }
00949
00950 template <typename T>
00951 void
00952 chroot::replace_facet (std::shared_ptr<T> facet)
00953 {
00954 remove_facet<T>();
00955 add_facet(facet);
00956 }
00957
00958 }
00959
00960 #endif
00961
00962
00963
00964
00965
00966