system.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2006, 2007, 2008 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_SYSTEM_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_SYSTEM_HH 1
00022 
00023 #include <paludis/util/exception.hh>
00024 #include <paludis/util/private_implementation_pattern.hh>
00025 #include <paludis/util/wrapped_forward_iterator-fwd.hh>
00026 #include <tr1/memory>
00027 #include <tr1/functional>
00028 #include <string>
00029 #include <sys/types.h>
00030 
00031 /** \file
00032  * Various system utilities.
00033  *
00034  * \ingroup g_system
00035  *
00036  * \section Examples
00037  *
00038  * - None at this time.
00039  */
00040 
00041 namespace paludis
00042 {
00043     class FSEntry;
00044 
00045     /**
00046      * Thrown if getenv_or_error fails.
00047      *
00048      * \ingroup g_exceptions
00049      * \ingroup g_system
00050      * \nosubgrouping
00051      */
00052     class PALUDIS_VISIBLE GetenvError : public Exception
00053     {
00054         public:
00055             ///\name Basic operations
00056             ///\{
00057 
00058             GetenvError(const std::string & key) throw ();
00059 
00060             ///\}
00061     };
00062 
00063     /**
00064      * Thrown if fork, wait or chdir fail when running a command.
00065      *
00066      * \ingroup g_exceptions
00067      * \ingroup g_system
00068      * \nosubgrouping
00069      */
00070     class PALUDIS_VISIBLE RunCommandError : public Exception
00071     {
00072         public:
00073             /**
00074              * Constructor.
00075              */
00076             RunCommandError(const std::string & message) throw ();
00077     };
00078 
00079     /**
00080      * Fetch the value of environment variable key, or def if the variable is
00081      * not defined.
00082      *
00083      * \ingroup g_system
00084      */
00085     std::string getenv_with_default(const std::string & key, const std::string & def) PALUDIS_VISIBLE;
00086 
00087     /**
00088      * Fetch the value of environment variable key, or throw a GetenvError if
00089      * the variable is not defined.
00090      *
00091      * \ingroup g_system
00092      */
00093     std::string getenv_or_error(const std::string & key) PALUDIS_VISIBLE;
00094 
00095     /**
00096      * Fetch the kernel version, for $KV.
00097      *
00098      * \ingroup g_system
00099      */
00100     std::string kernel_version() PALUDIS_VISIBLE;
00101 
00102     /**
00103      * A command to be run.
00104      *
00105      * \see PStream
00106      * \see run_command
00107      * \ingroup g_system
00108      * \nosubgrouping
00109      */
00110     class PALUDIS_VISIBLE Command :
00111         private PrivateImplementationPattern<Command>
00112     {
00113         public:
00114             ///\name Basic operations
00115             ///\{
00116 
00117             Command(const std::string &);
00118             Command(const char * const);
00119             Command(const Command &);
00120             const Command & operator= (const Command &);
00121             ~Command();
00122 
00123             ///\}
00124 
00125             ///\name Change command execution options
00126             ///\{
00127 
00128             /**
00129              * Include a chdir before we run our command.
00130              */
00131             Command & with_chdir(const FSEntry &);
00132 
00133             /**
00134              * Add a setenv before we run our command.
00135              */
00136             Command & with_setenv(const std::string &, const std::string &);
00137 
00138             /**
00139              * Run our command sandboxed.
00140              */
00141             Command & with_sandbox();
00142 
00143             /**
00144              * Echo the command to be run to stderr before running it.
00145              */
00146             Command & with_echo_to_stderr();
00147 
00148             /**
00149              * Add a setuid and setgid before running our command.
00150              */
00151             Command & with_uid_gid(const uid_t, const gid_t);
00152 
00153             /**
00154              * Prefix stdout output with this.
00155              */
00156             Command & with_stdout_prefix(const std::string &);
00157 
00158             /**
00159              * Prefix stderr output with this.
00160              */
00161             Command & with_stderr_prefix(const std::string &);
00162 
00163             /**
00164              * If prefixing, and if the output contains only blanks, don't display
00165              * any output.
00166              */
00167             Command & with_prefix_discard_blank_output();
00168 
00169             /**
00170              * If prefixing, prefix blank lines too.
00171              */
00172             Command & with_prefix_blank_lines();
00173 
00174             /**
00175              * Specify a pipe command handler.
00176              */
00177             Command & with_pipe_command_handler(const std::tr1::function<std::string (const std::string &)> &);
00178 
00179             /**
00180              * Specify a stream to which stdout is captured and written.
00181              */
00182             Command & with_captured_stdout_stream(std::ostream * const);
00183 
00184             /**
00185              * Specify a stream to which stderr is captured and written.
00186              *
00187              * \since 0.30
00188              */
00189             Command & with_captured_stderr_stream(std::ostream * const);
00190 
00191             ///\}
00192 
00193             ///\name Fetch command execution options
00194             ///\{
00195 
00196             /**
00197              * Our command, as a string.
00198              */
00199             std::string command() const;
00200 
00201             /**
00202              * Where to chdir, as a string.
00203              */
00204             std::string chdir() const;
00205 
00206             /**
00207              * Echo ourself to stderr.
00208              */
00209             void echo_to_stderr() const;
00210 
00211             /**
00212              * The uid for setuid.
00213              */
00214             std::tr1::shared_ptr<const uid_t> uid() const;
00215 
00216             /**
00217              * The gid for setgid.
00218              */
00219             std::tr1::shared_ptr<const gid_t> gid() const;
00220 
00221             /**
00222              * The stdout prefix.
00223              */
00224             std::string stdout_prefix() const;
00225 
00226             /**
00227              * The stderr prefix.
00228              */
00229             std::string stderr_prefix() const;
00230 
00231             /**
00232              * If prefixing, and if the output contains only blanks, don't display
00233              * any output?
00234              */
00235             bool prefix_discard_blank_output() const;
00236 
00237             /**
00238              * Prefix blank lines?
00239              */
00240             bool prefix_blank_lines() const;
00241 
00242             /**
00243              * The pipe command handler.
00244              */
00245             const std::tr1::function<std::string (const std::string &)> & pipe_command_handler() const;
00246 
00247             /**
00248              * The captured stdout stream, or null.
00249              */
00250             std::ostream * captured_stdout_stream() const;
00251 
00252             /**
00253              * The captured stderr stream, or null.
00254              *
00255              * \since 0.30
00256              */
00257             std::ostream * captured_stderr_stream() const;
00258 
00259             ///\}
00260 
00261             ///\name Iterate over our setenvs.
00262             ///\{
00263 
00264             struct ConstIteratorTag;
00265             typedef WrappedForwardIterator<ConstIteratorTag, const std::pair<const std::string, std::string> > ConstIterator;
00266             ConstIterator begin_setenvs() const;
00267             ConstIterator end_setenvs() const;
00268 
00269             ///\}
00270     };
00271 
00272     /**
00273      * Run a command, wait for it to terminate and return its exit status.
00274      *
00275      * \ingroup g_system
00276      */
00277     int run_command(const Command & cmd) PALUDIS_VISIBLE
00278         PALUDIS_ATTRIBUTE((warn_unused_result));
00279 
00280     /**
00281      * Set the stderr and close for stdout fds used by run_command and
00282      * run_command_in_directory.
00283      *
00284      * \ingroup g_system
00285      */
00286     void set_run_command_stdout_fds(const int, const int) PALUDIS_VISIBLE;
00287 
00288     /**
00289      * Set the stderr and close for stderr fds used by run_command and
00290      * run_command_in_directory.
00291      *
00292      * \ingroup g_system
00293      */
00294     void set_run_command_stderr_fds(const int, const int) PALUDIS_VISIBLE;
00295 
00296     /**
00297      * Fetch the username for a uid, or the uid as a string if not available.
00298      *
00299      * \ingroup g_system
00300      */
00301     std::string get_user_name(const uid_t) PALUDIS_VISIBLE;
00302 
00303     /**
00304      * Fetch the group name for a gid, or the gid as a string if not available.
00305      *
00306      * \ingroup g_system
00307      */
00308     std::string get_group_name(const gid_t) PALUDIS_VISIBLE;
00309 }
00310 
00311 #endif
00312 

Generated on Mon Dec 22 19:43:51 2008 for paludis by  doxygen 1.5.7.1