00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00018 #ifndef BOOST_PROCESS_SELF_HPP
00019 #define BOOST_PROCESS_SELF_HPP
00020
00021 #include <boost/process/config.hpp>
00022
00023 #if defined(BOOST_POSIX_API)
00024 # include <unistd.h>
00025 #elif defined(BOOST_WINDOWS_API)
00026 # include <windows.h>
00027 #else
00028 # error "Unsupported platform."
00029 #endif
00030
00031 #include <boost/process/process.hpp>
00032 #include <boost/process/environment.hpp>
00033 #include <boost/system/system_error.hpp>
00034 #include <boost/throw_exception.hpp>
00035 #include <boost/noncopyable.hpp>
00036 #include <string>
00037
00038 #if defined(BOOST_POSIX_API)
00039 extern "C"
00040 {
00041 extern char **environ;
00042 }
00043 #endif
00044
00045 namespace boost {
00046 namespace process {
00047
00053 class self : public process, boost::noncopyable
00054 {
00055 public:
00059 static self &get_instance()
00060 {
00061 static self *instance = 0;
00062 if (!instance)
00063 instance = new self;
00064 return *instance;
00065 }
00066
00073 static environment get_environment()
00074 {
00075 environment e;
00076
00077 #if defined(BOOST_POSIX_API)
00078 char **env = ::environ;
00079 while (*env)
00080 {
00081 std::string s = *env;
00082 std::string::size_type pos = s.find('=');
00083 e.insert(boost::process::environment::value_type(s.substr(0, pos), s.substr(pos + 1)));
00084 ++env;
00085 }
00086 #elif defined(BOOST_WINDOWS_API)
00087 #ifdef GetEnvironmentStrings
00088 #undef GetEnvironmentStrings
00089 #endif
00090 char *environ = ::GetEnvironmentStrings();
00091 if (!environ)
00092 boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::self::get_environment: GetEnvironmentStrings failed"));
00093 try
00094 {
00095 char *env = environ;
00096 while (*env)
00097 {
00098 std::string s = env;
00099 std::string::size_type pos = s.find('=');
00100 e.insert(boost::process::environment::value_type(s.substr(0, pos), s.substr(pos + 1)));
00101 env += s.size() + 1;
00102 }
00103 }
00104 catch (...)
00105 {
00106 ::FreeEnvironmentStringsA(environ);
00107 throw;
00108 }
00109 ::FreeEnvironmentStringsA(environ);
00110 #endif
00111
00112 return e;
00113 }
00114
00115 private:
00121 self() :
00122 #if defined(BOOST_POSIX_API)
00123 process(::getpid())
00124 #elif defined(BOOST_WINDOWS_API)
00125 process(::GetCurrentProcessId())
00126 #endif
00127 {
00128 }
00129 };
00130
00131 }
00132 }
00133
00134 #endif