sbuild-chroot.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2008  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
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         EXEC_START,    
00055         EXEC_STOP      
00056       };
00057 
00059     enum session_flags
00060       {
00061         SESSION_NOFLAGS = 0,      
00062         SESSION_CREATE  = 1 << 0, 
00063         SESSION_CLONE   = 1 << 1, 
00064         SESSION_PURGE   = 1 << 2  
00065       };
00066 
00068     enum error_code
00069       {
00070         CHROOT_CREATE,   
00071         CHROOT_DEVICE,   
00072         CHROOT_TYPE,     
00073         DEVICE_ABS,      
00074         DEVICE_LOCK,     
00075         DEVICE_NOTBLOCK, 
00076         DEVICE_UNLOCK,   
00077         DIRECTORY_ABS,   
00078         FACET_INVALID,   
00079         FACET_PRESENT,   
00080         FILE_ABS,        
00081         FILE_LOCK,       
00082         FILE_NOTREG,     
00083         FILE_OWNER,      
00084         FILE_PERMS,      
00085         FILE_UNLOCK,     
00086         LOCATION_ABS,    
00087         SESSION_UNLINK,  
00088         SESSION_WRITE    
00089       };
00090 
00092     typedef custom_error<error_code> error;
00093 
00095     typedef std::tr1::shared_ptr<chroot> ptr;
00096 
00098     typedef std::tr1::shared_ptr<const chroot> const_ptr;
00099 
00100   protected:
00102     chroot ();
00103 
00105     chroot (const chroot& rhs);
00106 
00107   public:
00109     virtual ~chroot ();
00110 
00117     static ptr
00118     create (std::string const& type);
00119 
00125     virtual ptr
00126     clone () const = 0;
00127 
00136     virtual chroot::ptr
00137     clone_session (std::string const& session_id,
00138                    std::string const& user,
00139                    bool               root) const = 0;
00140 
00146     virtual chroot::ptr
00147     clone_source () const = 0;
00148 
00154     std::string const&
00155     get_name () const;
00156 
00162     void
00163     set_name (std::string const& name);
00164 
00170     std::string const&
00171     get_session_id () const;
00172 
00179     void
00180     set_session_id (std::string const& session_id);
00181 
00189     std::string const&
00190     get_keyfile_name () const;
00191 
00197     std::string const&
00198     get_description () const;
00199 
00205     void
00206     set_description (std::string const& description);
00207 
00213     std::string const&
00214     get_mount_location () const;
00215 
00221     void
00222     set_mount_location (std::string const& location);
00223 
00224   public:
00233     virtual std::string
00234     get_path () const = 0;
00235 
00242     unsigned int
00243     get_priority () const;
00244 
00254     void
00255     set_priority (unsigned int priority);
00256 
00262     string_list const&
00263     get_users () const;
00264 
00270     void
00271     set_users (string_list const& users);
00272 
00278     string_list const&
00279     get_groups () const;
00280 
00286     void
00287     set_groups (string_list const& groups);
00288 
00296     string_list const&
00297     get_root_users () const;
00298 
00306     void
00307     set_root_users (string_list const& users);
00308 
00316     string_list const&
00317     get_root_groups () const;
00318 
00326     void
00327     set_root_groups (string_list const& groups);
00328 
00335     string_list const&
00336     get_aliases () const;
00337 
00344     void
00345     set_aliases (string_list const& aliases);
00346 
00354     regex const&
00355     get_environment_filter () const;
00356 
00364     void
00365     set_environment_filter (regex const& environment_filter);
00366 
00373     bool
00374     get_active () const;
00375 
00381     bool
00382     get_original () const;
00383 
00389     void
00390     set_original (bool original);
00391 
00397     bool
00398     get_run_setup_scripts () const;
00399 
00400   protected:
00407     void
00408     set_run_setup_scripts (bool run_setup_scripts);
00409 
00410   public:
00418     std::string const&
00419     get_script_config () const;
00420 
00428     void
00429     set_script_config (std::string const& script_config);
00430 
00437     string_list const&
00438     get_command_prefix () const;
00439 
00446     void
00447     set_command_prefix (string_list const& command_prefix);
00448 
00454     virtual std::string const&
00455     get_chroot_type () const = 0;
00456 
00463     void
00464     setup_env (environment& env) const;
00465 
00473     virtual void
00474     setup_env (chroot const& chroot,
00475                environment& env) const = 0;
00476 
00488     void
00489     lock (setup_type type);
00490 
00504     void
00505     unlock (setup_type type,
00506             int        status);
00507 
00508   protected:
00514     virtual void
00515     setup_session_info (bool start);
00516 
00531     virtual void
00532     setup_lock(setup_type type,
00533                bool       lock,
00534                int        status) = 0;
00535 
00536   public:
00537     template <typename T>
00538     std::tr1::shared_ptr<T>
00539     get_facet ();
00540 
00541     template <typename T>
00542     const std::tr1::shared_ptr<const T>
00543     get_facet () const;
00544 
00545     template <typename T>
00546     void
00547     add_facet (std::tr1::shared_ptr<T> facet);
00548 
00549     template <typename T>
00550     void
00551     remove_facet ();
00552 
00553     template <typename T>
00554     void
00555     remove_facet (std::tr1::shared_ptr<T> facet);
00556 
00557     template <typename T>
00558     void
00559     replace_facet (std::tr1::shared_ptr<T> facet);
00560 
00561     string_list
00562     list_facets () const;
00563 
00570     session_flags
00571     get_session_flags () const;
00572 
00580     virtual chroot::session_flags
00581     get_session_flags (chroot const& chroot) const = 0;
00582 
00592     friend std::ostream&
00593     operator << (std::ostream& stream,
00594                  ptr const&    rhs)
00595     {
00596       rhs->print_details(stream);
00597       return stream;
00598     }
00599 
00603     friend
00604     keyfile const&
00605     operator >> (keyfile const& keyfile,
00606                  ptr&           rhs)
00607     {
00608       string_list used;
00609       rhs->set_keyfile(keyfile, used);
00610       keyfile.check_keys(rhs->get_name(), used);
00611       return keyfile;
00612     }
00613 
00617     friend
00618     keyfile&
00619     operator << (keyfile&   keyfile,
00620                  ptr const& rhs)
00621     {
00622       rhs->get_keyfile(keyfile);
00623       return keyfile;
00624     }
00625 
00631     void
00632     get_details (format_detail& detail) const;
00633 
00640     virtual void
00641     get_details (chroot const&  chroot,
00642                  format_detail& detail) const = 0;
00643 
00651     void
00652     print_details (std::ostream& stream) const;
00653 
00661     void
00662     get_keyfile (keyfile& keyfile) const;
00663 
00664   protected:
00673     virtual void
00674     get_keyfile (chroot const& chroot,
00675                  keyfile&      keyfile) const = 0;
00676 
00677   public:
00686     void
00687     set_keyfile (keyfile const& keyfile,
00688                  string_list&   used_keys);
00689 
00690   protected:
00700     virtual void
00701     set_keyfile (chroot&        chroot,
00702                  keyfile const& keyfile,
00703                  string_list&   used_keys) = 0;
00704 
00705   private:
00707     std::string   name;
00709     std::string   session_id;
00711     std::string   description;
00713     unsigned int  priority;
00715     string_list   users;
00717     string_list   groups;
00719     string_list   root_users;
00721     string_list   root_groups;
00723     string_list   aliases;
00725     regex         environment_filter;
00727     std::string   mount_location;
00729     bool          original;
00731     bool          run_setup_scripts;
00733     std::string   script_config;
00735     string_list   command_prefix;
00736 
00737     typedef std::tr1::shared_ptr<chroot_facet> facet_ptr;
00738     typedef std::list<facet_ptr> facet_list;
00739     facet_list facets;
00740   };
00741 
00748   chroot::session_flags
00749   inline operator | (chroot::session_flags const& lhs,
00750                      chroot::session_flags const& rhs)
00751   {
00752     return static_cast<chroot::session_flags>
00753       (static_cast<int>(lhs) | static_cast<int>(rhs));
00754   }
00755 
00762   chroot::session_flags
00763   inline operator & (chroot::session_flags const& lhs,
00764                      chroot::session_flags const& rhs)
00765   {
00766     return static_cast<chroot::session_flags>
00767       (static_cast<int>(lhs) & static_cast<int>(rhs));
00768   }
00769 
00770 }
00771 
00772 #include <sbuild/sbuild-chroot-facet.h>
00773 
00774 namespace sbuild
00775 {
00776 
00777   template <typename T>
00778   std::tr1::shared_ptr<T>
00779   chroot::get_facet ()
00780   {
00781     std::tr1::shared_ptr<T> ret;
00782 
00783     for (facet_list::const_iterator pos = facets.begin();
00784          pos != facets.end();
00785          ++pos)
00786       {
00787         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00788           break;
00789       }
00790 
00791     return ret;
00792   }
00793 
00794   template <typename T>
00795   const std::tr1::shared_ptr<const T>
00796   chroot::get_facet () const
00797   {
00798     std::tr1::shared_ptr<T> ret;
00799 
00800     for (facet_list::const_iterator pos = facets.begin();
00801          pos != facets.end();
00802          ++pos)
00803       {
00804         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00805           break;
00806       }
00807 
00808     return std::tr1::const_pointer_cast<T>(ret);
00809   }
00810 
00811   template <typename T>
00812   void
00813   chroot::add_facet (std::tr1::shared_ptr<T> facet)
00814   {
00815     facet_ptr new_facet = std::tr1::dynamic_pointer_cast<chroot_facet>(facet);
00816     if (!new_facet)
00817       throw error(FACET_INVALID);
00818 
00819     for (facet_list::const_iterator pos = facets.begin();
00820          pos != facets.end();
00821          ++pos)
00822       {
00823         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00824           throw error(FACET_PRESENT);
00825       }
00826 
00827     new_facet->set_chroot(*this);
00828     facets.push_back(new_facet);
00829   }
00830 
00831   template <typename T>
00832   void
00833   chroot::remove_facet ()
00834   {
00835     for (facet_list::iterator pos = facets.begin();
00836          pos != facets.end();
00837          ++pos)
00838       {
00839         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00840           {
00841             facets.erase(pos);
00842             break;
00843           }
00844       }
00845   }
00846 
00847   template <typename T>
00848   void
00849   chroot::remove_facet (std::tr1::shared_ptr<T> facet)
00850   {
00851     remove_facet<T>();
00852   }
00853 
00854   template <typename T>
00855   void
00856   chroot::replace_facet (std::tr1::shared_ptr<T> facet)
00857   {
00858     remove_facet<T>();
00859     add_facet(facet);
00860   }
00861 
00862 }
00863 
00864 #endif /* SBUILD_CHROOT_H */
00865 
00866 /*
00867  * Local Variables:
00868  * mode:C++
00869  * End:
00870  */

Generated on Sat Oct 24 07:32:32 2009 for sbuild by  doxygen 1.5.8