00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00018 #ifndef BOOST_PROCESS_PROCESS_HPP
00019 #define BOOST_PROCESS_PROCESS_HPP
00020
00021 #include <boost/process/config.hpp>
00022
00023 #if defined(BOOST_POSIX_API)
00024 # include <cerrno>
00025 # include <signal.h>
00026 #elif defined(BOOST_WINDOWS_API)
00027 # include <cstdlib>
00028 # include <windows.h>
00029 #else
00030 # error "Unsupported platform."
00031 #endif
00032
00033 #include <boost/system/system_error.hpp>
00034 #include <boost/throw_exception.hpp>
00035
00036 namespace boost {
00037 namespace process {
00038
00045 class process
00046 {
00047 public:
00048 #if defined(BOOST_PROCESS_DOXYGEN)
00049
00059 typedef NativeProcessId id_type;
00060 #elif defined(BOOST_POSIX_API)
00061 typedef pid_t id_type;
00062 #elif defined(BOOST_WINDOWS_API)
00063 typedef DWORD id_type;
00064 #endif
00065
00072 process(id_type id)
00073 : id_(id)
00074 {
00075 }
00076
00080 id_type get_id() const
00081 {
00082 return id_;
00083 }
00084
00101 void terminate(bool force = false) const
00102 {
00103 #if defined(BOOST_POSIX_API)
00104 if (::kill(id_, force ? SIGKILL : SIGTERM) == -1)
00105 boost::throw_exception(boost::system::system_error(boost::system::error_code(errno, boost::system::get_system_category()), "boost::process::process::terminate: kill(2) failed"));
00106 #elif defined(BOOST_WINDOWS_API)
00107 HANDLE h = ::OpenProcess(PROCESS_TERMINATE, FALSE, id_);
00108 if (h == NULL)
00109 boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::process::terminate: OpenProcess failed"));
00110 if (!::TerminateProcess(h, EXIT_FAILURE))
00111 {
00112 ::CloseHandle(h);
00113 boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::process::terminate: TerminateProcess failed"));
00114 }
00115 if (!::CloseHandle(h))
00116 boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::process::terminate: CloseHandle failed"));
00117 #endif
00118 }
00119
00120 private:
00124 id_type id_;
00125 };
00126
00127 }
00128 }
00129
00130 #endif