Changeset 79644 in vbox for trunk/src/VBox
- Timestamp:
- Jul 9, 2019 2:01:06 PM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 131950
- Location:
- trunk/src/VBox/Main
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/include/DHCPServerImpl.h
r79621 r79644 23 23 24 24 #include "DHCPServerWrap.h" 25 #include <map> 25 26 26 27 namespace settings … … 31 32 } 32 33 33 34 #ifdef VBOX_WITH_HOSTNETIF_API35 struct NETIFINFO;36 #endif37 38 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)39 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe"40 #else41 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP"42 #endif43 44 class DHCPServerRunner : public NetworkServiceRunner45 {46 public:47 DHCPServerRunner() : NetworkServiceRunner(DHCP_EXECUTABLE_NAME) {}48 virtual ~DHCPServerRunner() {};49 50 static const std::string kDsrKeyGateway;51 static const std::string kDsrKeyLowerIp;52 static const std::string kDsrKeyUpperIp;53 static const std::string kDsrKeyConfig;54 static const std::string kDsrKeyComment;55 };56 34 57 35 /** … … 67 45 * the middle. 68 46 */ 69 70 47 class ATL_NO_VTABLE DHCPServer 71 48 : public DHCPServerWrap … … 86 63 // Public internal methids. 87 64 HRESULT i_saveSettings(settings::DHCPServer &data); 88 settings::DhcpOptionMap &i_findOptMapByVmNameSlot(const com::Utf8Str &aVmName, 89 LONG Slot); 65 settings::DhcpOptionMap &i_findOptMapByVmNameSlot(const com::Utf8Str &aVmName, LONG Slot); 90 66 91 67 private: 92 HRESULT encodeOption(com::Utf8Str &aEncoded, 93 uint32_t aOptCode, const settings::DhcpOptValue &aOptValue); 94 int addOption(settings::DhcpOptionMap &aMap, 95 DhcpOpt_T aOption, const com::Utf8Str &aValue); 68 HRESULT encodeOption(com::Utf8Str &aEncoded, uint32_t aOptCode, const settings::DhcpOptValue &aOptValue); 69 int addOption(settings::DhcpOptionMap &aMap, DhcpOpt_T aOption, const com::Utf8Str &aValue); 96 70 97 71 // wrapped IDHCPServer properties -
trunk/src/VBox/Main/include/NetworkServiceRunner.h
r76562 r79644 22 22 #endif 23 23 24 #include <iprt/err.h>25 #include <iprt/types.h>26 #include <iprt/string.h>27 #include <iprt/mem.h>28 24 #include <VBox/com/string.h> 29 25 30 #include <string>31 26 27 /** @name Internal networking trunk type option values (NetworkServiceRunner::kpszKeyTrunkType) 28 * @{ */ 32 29 #define TRUNKTYPE_WHATEVER "whatever" 33 30 #define TRUNKTYPE_NETFLT "netflt" 34 31 #define TRUNKTYPE_NETADP "netadp" 35 32 #define TRUNKTYPE_SRVNAT "srvnat" 33 /** @} */ 36 34 35 /** 36 * Network service runner. 37 * 38 * Build arguments, starts and stops network service processes. 39 */ 37 40 class NetworkServiceRunner 38 41 { … … 41 44 virtual ~NetworkServiceRunner(); 42 45 43 int setOption(const std::string& key, const std::string& val); 44 void clearOptions(); 46 /** @name Argument management 47 * @{ */ 48 int addArgument(const char *pszArgument); 49 int addArgPair(const char *pszOption, const char *pszValue); 50 void resetArguments(); 51 /** @} */ 45 52 46 int start(bool aKillProc OnStop);53 int start(bool aKillProcessOnStop); 47 54 int stop(); 48 55 bool isRunning(); 49 56 void detachFromServer(); 50 57 51 static const std::string kNsrKeyName; 52 static const std::string kNsrKeyNetwork; 53 static const std::string kNsrKeyTrunkType; 54 static const std::string kNsrTrunkName; 55 static const std::string kNsrMacAddress; 56 static const std::string kNsrIpAddress; 57 static const std::string kNsrIpNetmask; 58 static const std::string kNsrKeyNeedMain; 58 /** @name Common options 59 * @{ */ 60 static const char * const kpszKeyNetwork; 61 static const char * const kpszKeyTrunkType; 62 static const char * const kpszTrunkName; 63 static const char * const kpszMacAddress; 64 static const char * const kpszIpAddress; 65 static const char * const kpszIpNetmask; 66 static const char * const kpszKeyNeedMain; 67 /** @} */ 59 68 60 69 private: -
trunk/src/VBox/Main/src-server/DHCPServerImpl.cpp
r79623 r79644 16 16 */ 17 17 18 19 /********************************************************************************************************************************* 20 * Header Files * 21 *********************************************************************************************************************************/ 18 22 #define LOG_GROUP LOG_GROUP_MAIN_DHCPSERVER 19 23 #include "NetworkServiceRunner.h" … … 23 27 24 28 #include <iprt/asm.h> 29 #include <iprt/err.h> 25 30 #include <iprt/file.h> 26 31 #include <iprt/net.h> … … 35 40 #include "VirtualBoxImpl.h" 36 41 37 // constructor / destructor 38 ///////////////////////////////////////////////////////////////////////////// 39 /** @todo Convert to C strings as this is wastefull: */ 40 const std::string DHCPServerRunner::kDsrKeyGateway = "--gateway"; 41 const std::string DHCPServerRunner::kDsrKeyLowerIp = "--lower-ip"; 42 const std::string DHCPServerRunner::kDsrKeyUpperIp = "--upper-ip"; 43 const std::string DHCPServerRunner::kDsrKeyConfig = "--config"; 44 const std::string DHCPServerRunner::kDsrKeyComment = "--comment"; 45 46 42 43 /********************************************************************************************************************************* 44 * Defined Constants And Macros * 45 *********************************************************************************************************************************/ 46 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) 47 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe" 48 #else 49 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP" 50 #endif 51 52 53 /** 54 * DHCP server specialization of NetworkServiceRunner. 55 * 56 * Just defines the executable name and adds option constants. 57 */ 58 class DHCPServerRunner : public NetworkServiceRunner 59 { 60 public: 61 DHCPServerRunner() : NetworkServiceRunner(DHCP_EXECUTABLE_NAME) 62 {} 63 virtual ~DHCPServerRunner() 64 {} 65 66 static const char * const kDsrKeyGateway; 67 static const char * const kDsrKeyLowerIp; 68 static const char * const kDsrKeyUpperIp; 69 static const char * const kDsrKeyConfig; 70 static const char * const kDsrKeyComment; 71 }; 72 73 /*static*/ const char * const DHCPServerRunner::kDsrKeyGateway = "--gateway"; 74 /*static*/ const char * const DHCPServerRunner::kDsrKeyLowerIp = "--lower-ip"; 75 /*static*/ const char * const DHCPServerRunner::kDsrKeyUpperIp = "--upper-ip"; 76 /*static*/ const char * const DHCPServerRunner::kDsrKeyConfig = "--config"; 77 /*static*/ const char * const DHCPServerRunner::kDsrKeyComment = "--comment"; 78 79 80 /** 81 * Hidden private data of the DHCPServer class. 82 */ 47 83 struct DHCPServer::Data 48 84 { … … 51 87 , router(false) 52 88 { 53 tempConfigFileName[0] = '\0';89 szTempConfigFileName[0] = '\0'; 54 90 } 55 91 … … 65 101 settings::VmSlot2OptionsMap VmSlot2Options; 66 102 67 char tempConfigFileName[RTPATH_MAX];103 char szTempConfigFileName[RTPATH_MAX]; 68 104 com::Utf8Str strLeasesFilename; 69 105 com::Utf8Str networkName; … … 73 109 74 110 111 // constructor / destructor 112 ///////////////////////////////////////////////////////////////////////////// 113 114 75 115 DHCPServer::DHCPServer() 76 : m(NULL)77 , mVirtualBox(NULL)116 : m(NULL) 117 , mVirtualBox(NULL) 78 118 { 79 119 m = new DHCPServer::Data(); … … 144 184 145 185 146 HRESULT DHCPServer::init(VirtualBox *aVirtualBox, 147 const settings::DHCPServer &data) 186 HRESULT DHCPServer::init(VirtualBox *aVirtualBox, const settings::DHCPServer &data) 148 187 { 149 188 /* Enclose the state transition NotReady->InInit->Ready */ … … 161 200 162 201 m->GlobalDhcpOptions.clear(); 163 m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(), 164 data.GlobalDhcpOptions.end()); 202 m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(), data.GlobalDhcpOptions.end()); 165 203 166 204 m->VmSlot2Options.clear(); 167 m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(), 168 data.VmSlot2OptionsM.end()); 205 m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(), data.VmSlot2OptionsM.end()); 169 206 170 207 autoInitSpan.setSucceeded(); … … 700 737 return hrc; 701 738 702 m->dhcp. clearOptions(); /* (Not DHCP options, but command line options for the service) */739 m->dhcp.resetArguments(); 703 740 704 741 #ifdef VBOX_WITH_DHCPD … … 708 745 */ 709 746 /** @todo put this next to the leases file. */ 710 int rc = RTPathTemp(m-> tempConfigFileName, sizeof(m->tempConfigFileName));747 int rc = RTPathTemp(m->szTempConfigFileName, sizeof(m->szTempConfigFileName)); 711 748 if (RT_SUCCESS(rc)) 712 rc = RTPathAppend(m-> tempConfigFileName, sizeof(m->tempConfigFileName), "dhcp-config-XXXXX.xml");749 rc = RTPathAppend(m->szTempConfigFileName, sizeof(m->szTempConfigFileName), "dhcp-config-XXXXX.xml"); 713 750 if (RT_SUCCESS(rc)) 714 rc = RTFileCreateTemp(m-> tempConfigFileName, 0600);751 rc = RTFileCreateTemp(m->szTempConfigFileName, 0600); 715 752 if (RT_FAILURE(rc)) 716 753 { 717 m-> tempConfigFileName[0] = '\0';754 m->szTempConfigFileName[0] = '\0'; 718 755 return E_FAIL; 719 756 } … … 791 828 792 829 xml::XmlFileWriter writer(doc); 793 writer.write(m-> tempConfigFileName, false);794 795 m->dhcp. setOption(DHCPServerRunner::kDsrKeyConfig, m->tempConfigFileName); /* command line options, not dhcp ones. */796 m->dhcp. setOption(DHCPServerRunner::kDsrKeyComment, m->networkName.c_str());830 writer.write(m->szTempConfigFileName, false); 831 832 m->dhcp.addArgPair(DHCPServerRunner::kDsrKeyConfig, m->szTempConfigFileName); 833 m->dhcp.addArgPair(DHCPServerRunner::kDsrKeyComment, m->networkName.c_str()); 797 834 798 835 #else /* !VBOX_WITH_DHCPD */ 799 836 /* Main is needed for NATNetwork */ 800 837 if (m->router) 801 m->dhcp. setOption(NetworkServiceRunner::kNsrKeyNeedMain, "on");838 m->dhcp.addArgPair(NetworkServiceRunner::kpszKeyNeedMain, "on"); 802 839 803 840 /* Commmon Network Settings */ 804 m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNetwork, aNetworkName.c_str()); 805 841 m->dhcp.addArgPair(NetworkServiceRunner::kpszKeyNetwork, aNetworkName.c_str()); 806 842 if (!aTrunkName.isEmpty()) 807 m->dhcp.setOption(NetworkServiceRunner::kNsrTrunkName, aTrunkName.c_str()); 808 809 m->dhcp.setOption(NetworkServiceRunner::kNsrKeyTrunkType, aTrunkType.c_str()); 843 m->dhcp.addArgPair(NetworkServiceRunner::kpszTrunkName, aTrunkName.c_str()); 844 m->dhcp.addArgPair(NetworkServiceRunner::kpszKeyTrunkType, aTrunkType.c_str()); 810 845 811 846 /* XXX: should this MAC default initialization moved to NetworkServiceRunner? */ … … 814 849 guid.create(); 815 850 RTStrPrintf (strMAC, sizeof(strMAC), "08:00:27:%02X:%02X:%02X", 816 guid.raw()->au8[0], 817 guid.raw()->au8[1], 818 guid.raw()->au8[2]); 819 m->dhcp.setOption(NetworkServiceRunner::kNsrMacAddress, strMAC); 820 m->dhcp.setOption(NetworkServiceRunner::kNsrIpAddress, Utf8Str(m->IPAddress).c_str()); 821 m->dhcp.setOption(NetworkServiceRunner::kNsrIpNetmask, Utf8Str(m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text).c_str()); 822 m->dhcp.setOption(DHCPServerRunner::kDsrKeyLowerIp, Utf8Str(m->lowerIP).c_str()); 823 m->dhcp.setOption(DHCPServerRunner::kDsrKeyUpperIp, Utf8Str(m->upperIP).c_str()); 851 guid.raw()->au8[0], guid.raw()->au8[1], guid.raw()->au8[2]); 852 m->dhcp.addArgPair(NetworkServiceRunner::kpszMacAddress, strMAC); 853 m->dhcp.addArgPair(NetworkServiceRunner::kpszIpAddress, m->IPAddress.c_str()); 854 m->dhcp.addArgPair(NetworkServiceRunner::kpszIpNetmask, m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text.c_str()); 855 m->dhcp.addArgPair(DHCPServerRunner::kDsrKeyLowerIp, m->lowerIP.c_str()); 856 m->dhcp.addArgPair(DHCPServerRunner::kDsrKeyUpperIp, m->upperIP.c_str()); 824 857 #endif /* !VBOX_WITH_DHCPD */ 825 858 … … 830 863 831 864 832 HRESULT DHCPServer::stop 865 HRESULT DHCPServer::stop(void) 833 866 { 834 867 #ifdef VBOX_WITH_DHCPD 835 if (m-> tempConfigFileName[0])836 { 837 RTFileDelete(m-> tempConfigFileName);838 m-> tempConfigFileName[0] = 0;868 if (m->szTempConfigFileName[0]) 869 { 870 RTFileDelete(m->szTempConfigFileName); 871 m->szTempConfigFileName[0] = 0; 839 872 } 840 873 #endif /* VBOX_WITH_DHCPD */ -
trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp
r77436 r79644 760 760 AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL); 761 761 762 m->NATRunner. setOption(NetworkServiceRunner::kNsrKeyNetwork, Utf8Str(m->s.strNetworkName).c_str());763 m->NATRunner. setOption(NetworkServiceRunner::kNsrKeyTrunkType, Utf8Str(aTrunkType).c_str());764 m->NATRunner. setOption(NetworkServiceRunner::kNsrIpAddress, Utf8Str(m->IPv4Gateway).c_str());765 m->NATRunner. setOption(NetworkServiceRunner::kNsrIpNetmask, Utf8Str(m->IPv4NetworkMask).c_str());762 m->NATRunner.addArgPair(NetworkServiceRunner::kpszKeyNetwork, Utf8Str(m->s.strNetworkName).c_str()); 763 m->NATRunner.addArgPair(NetworkServiceRunner::kpszKeyTrunkType, Utf8Str(aTrunkType).c_str()); 764 m->NATRunner.addArgPair(NetworkServiceRunner::kpszIpAddress, Utf8Str(m->IPv4Gateway).c_str()); 765 m->NATRunner.addArgPair(NetworkServiceRunner::kpszIpNetmask, Utf8Str(m->IPv4NetworkMask).c_str()); 766 766 767 767 /* No portforwarding rules from command-line, all will be fetched via API */ -
trunk/src/VBox/Main/src-server/NetworkServiceRunner.cpp
r79610 r79644 20 20 * Header Files * 21 21 *********************************************************************************************************************************/ 22 #include <map>23 #include <string>24 22 #include "NetworkServiceRunner.h" 25 23 24 #include <iprt/env.h> 25 #include <iprt/err.h> 26 #include <iprt/log.h> 26 27 #include <iprt/process.h> 27 28 #include <iprt/path.h> 28 29 #include <iprt/param.h> 29 #include <iprt/env.h>30 #include <iprt/log.h>31 30 #include <iprt/thread.h> 32 31 … … 35 34 * Global Variables * 36 35 *********************************************************************************************************************************/ 37 /** @todo Convert to C strings as this is wastefull: */ 38 const std::string NetworkServiceRunner::kNsrKeyName = "--name"; 39 const std::string NetworkServiceRunner::kNsrKeyNetwork = "--network"; 40 const std::string NetworkServiceRunner::kNsrKeyTrunkType = "--trunk-type"; 41 const std::string NetworkServiceRunner::kNsrTrunkName = "--trunk-name"; 42 const std::string NetworkServiceRunner::kNsrMacAddress = "--mac-address"; 43 const std::string NetworkServiceRunner::kNsrIpAddress = "--ip-address"; 44 const std::string NetworkServiceRunner::kNsrIpNetmask = "--netmask"; 45 const std::string NetworkServiceRunner::kNsrKeyNeedMain = "--need-main"; 36 /*static*/ const char * const NetworkServiceRunner::kpszKeyNetwork = "--network"; 37 /*static*/ const char * const NetworkServiceRunner::kpszKeyTrunkType = "--trunk-type"; 38 /*static*/ const char * const NetworkServiceRunner::kpszTrunkName = "--trunk-name"; 39 /*static*/ const char * const NetworkServiceRunner::kpszMacAddress = "--mac-address"; 40 /*static*/ const char * const NetworkServiceRunner::kpszIpAddress = "--ip-address"; 41 /*static*/ const char * const NetworkServiceRunner::kpszIpNetmask = "--netmask"; 42 /*static*/ const char * const NetworkServiceRunner::kpszKeyNeedMain = "--need-main"; 46 43 47 44 … … 49 46 * Structures and Typedefs * 50 47 *********************************************************************************************************************************/ 48 /** 49 * Internal data the rest of the world does not need to be bothered with. 50 * 51 * @note no 'm' prefix here, as the runner is accessing it thru an 'm' member. 52 */ 51 53 struct NetworkServiceRunner::Data 52 54 { 55 /** The process filename. */ 56 const char *pszProcName; 57 /** Actual size of papszArgs. */ 58 size_t cArgs; 59 /** Number of entries allocated for papszArgs. */ 60 size_t cArgsAlloc; 61 /** The argument vector. 62 * Each entry is a string allocation via RTStrDup. The zero'th entry is 63 * filled in by start(). */ 64 char **papszArgs; 65 /** The process ID. */ 66 RTPROCESS Process; 67 /** Whether to kill the process on stopping. */ 68 bool fKillProcessOnStop; 69 53 70 Data(const char* aProcName) 54 : mProcName(aProcName) 55 , mProcess(NIL_RTPROCESS) 56 , mKillProcOnStop(false) 71 : pszProcName(aProcName) 72 , cArgs(0) 73 , cArgsAlloc(0) 74 , papszArgs(NULL) 75 , Process(NIL_RTPROCESS) 76 , fKillProcessOnStop(false) 57 77 {} 58 const char *mProcName; 59 RTPROCESS mProcess; 60 std::map<std::string, std::string> mOptions; /**< @todo r=bird: A map for command line option/value pairs? really? 61 * Wouldn't a simple argument list have done it much much more efficiently? */ 62 bool mKillProcOnStop; 78 79 ~Data() 80 { 81 resetArguments(); 82 } 83 84 void resetArguments() 85 { 86 for (size_t i = 0; i < cArgs; i++) 87 RTStrFree(papszArgs[i]); 88 RTMemFree(papszArgs); 89 cArgs = 0; 90 cArgsAlloc = 0; 91 papszArgs = NULL; 92 } 63 93 }; 64 94 … … 79 109 80 110 81 int NetworkServiceRunner::setOption(const std::string& key, const std::string& val) 82 { 83 m->mOptions.insert(std::map<std::string, std::string>::value_type(key, val)); 84 return VINF_SUCCESS; 85 } 86 87 88 void NetworkServiceRunner::clearOptions() 89 { 90 m->mOptions.clear(); 111 /** 112 * Adds one argument to the server command line. 113 * 114 * @returns IPRT status code. 115 * @param pszArgument The argument to add. 116 */ 117 int NetworkServiceRunner::addArgument(const char *pszArgument) 118 { 119 AssertPtr(pszArgument); 120 121 /* 122 * Grow the argument vector as needed. 123 * Make sure unused space is NULL'ed and that we've got an extra entry for 124 * the NULL terminator. Arguments starts at 1 of course, 0 being the executable. 125 */ 126 size_t const i = RT_MAX(m->cArgs, 1); 127 size_t const cAlloc = m->cArgsAlloc; 128 if (i + 1 /*NULL terminator*/ >= m->cArgsAlloc) 129 { 130 size_t cNewAlloc = cAlloc ? cAlloc : 2; 131 do 132 cNewAlloc *= 2; 133 while (cNewAlloc <= i + 1); 134 void *pvNew = RTMemRealloc(m->papszArgs, cNewAlloc * sizeof(m->papszArgs[0])); 135 AssertReturn(pvNew, VERR_NO_MEMORY); 136 m->papszArgs = (char **)pvNew; 137 RT_BZERO(&m->papszArgs[m->cArgsAlloc], (cNewAlloc - cAlloc) * sizeof(m->papszArgs[0])); 138 m->cArgsAlloc = cNewAlloc; 139 } 140 141 /* 142 * Add it. 143 */ 144 m->papszArgs[i] = RTStrDup(pszArgument); 145 if (m->papszArgs[i]) 146 { 147 m->cArgs = i + 1; 148 Assert(m->papszArgs[m->cArgs] == NULL); 149 return VINF_SUCCESS; 150 } 151 return VERR_NO_STR_MEMORY; 152 } 153 154 155 /** 156 * Adds a pair of arguments, e.g. option + value. 157 * 158 * @returns IPRT status code. 159 */ 160 int NetworkServiceRunner::addArgPair(const char *pszOption, const char *pszValue) 161 { 162 int rc = addArgument(pszOption); 163 if (RT_SUCCESS(rc)) 164 rc = addArgument(pszValue); 165 return rc; 166 } 167 168 169 void NetworkServiceRunner::resetArguments() 170 { 171 m->resetArguments(); 91 172 } 92 173 … … 94 175 void NetworkServiceRunner::detachFromServer() 95 176 { 96 m-> mProcess = NIL_RTPROCESS;97 } 98 99 100 int NetworkServiceRunner::start(bool aKillProc OnStop)177 m->Process = NIL_RTPROCESS; 178 } 179 180 181 int NetworkServiceRunner::start(bool aKillProcessOnStop) 101 182 { 102 183 if (isRunning()) … … 104 185 105 186 /* 106 * Construct the path to the executable . ASSUME it is relative to the107 * directory that holds VBoxSVC.187 * Construct the path to the executable and put in into the argument vector. 188 * ASSUME it is relative to the directory that holds VBoxSVC. 108 189 */ 109 190 char szExePath[RTPATH_MAX]; 110 191 AssertReturn(RTProcGetExecutablePath(szExePath, RTPATH_MAX), VERR_FILENAME_TOO_LONG); 111 192 RTPathStripFilename(szExePath); 112 int vrc = RTPathAppend(szExePath, sizeof(szExePath), m-> mProcName);193 int vrc = RTPathAppend(szExePath, sizeof(szExePath), m->pszProcName); 113 194 AssertLogRelRCReturn(vrc, vrc); 114 195 115 /* 116 * Allocate the argument array and construct the argument vector. 117 */ 118 size_t const cArgs = 1 + m->mOptions.size() * 2 + 1; 119 char const **papszArgs = (char const **)RTMemTmpAllocZ(sizeof(papszArgs[0]) * cArgs); 120 AssertReturn(papszArgs, VERR_NO_TMP_MEMORY); 121 122 size_t iArg = 0; 123 papszArgs[iArg++] = szExePath; 124 for (std::map<std::string, std::string>::const_iterator it = m->mOptions.begin(); it != m->mOptions.end(); ++it) 125 { 126 papszArgs[iArg++] = it->first.c_str(); 127 papszArgs[iArg++] = it->second.c_str(); 128 } 129 Assert(iArg + 1 == cArgs); 130 Assert(papszArgs[iArg] == NULL); 196 if (m->cArgs == 0 && m->cArgsAlloc == 0) 197 { 198 m->cArgsAlloc = 2; 199 m->papszArgs = (char **)RTMemAllocZ(sizeof(m->papszArgs[0]) * 2); 200 AssertReturn(m->papszArgs, VERR_NO_MEMORY); 201 } 202 else 203 Assert(m->cArgsAlloc >= 2); 204 RTStrFree(m->papszArgs[0]); 205 m->papszArgs[0] = RTStrDup(szExePath); 206 AssertReturn(m->papszArgs[0], VERR_NO_MEMORY); 207 if (m->cArgs == 0) 208 m->cArgs = 1; 131 209 132 210 /* 133 211 * Start the process: 134 212 */ 135 int rc = RTProcCreate(szExePath, papszArgs, RTENV_DEFAULT, 0, &m->mProcess); 136 if (RT_FAILURE(rc)) 137 m->mProcess = NIL_RTPROCESS; 138 139 m->mKillProcOnStop = aKillProcOnStop; 140 141 RTMemTmpFree(papszArgs); 213 int rc = RTProcCreate(szExePath, m->papszArgs, RTENV_DEFAULT, 0, &m->Process); 214 if (RT_SUCCESS(rc)) 215 LogRel(("NetworkServiceRunning: started '%s', pid %RTproc\n", m->pszProcName, m->Process)); 216 else 217 m->Process = NIL_RTPROCESS; 218 219 m->fKillProcessOnStop = aKillProcessOnStop; 220 142 221 return rc; 143 222 } … … 155 234 bool fDoKillProc = true; 156 235 157 if (!m-> mKillProcOnStop)236 if (!m->fKillProcessOnStop) 158 237 { 159 238 /* … … 162 241 * doing the final hard kill. 163 242 */ 164 int rc = VINF_SUCCESS;165 243 for (unsigned int i = 0; i < 100; i++) 166 244 { 167 rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL); 168 if (RT_SUCCESS(rc)) 245 if (!isRunning()) 246 { 247 fDoKillProc = false; 169 248 break; 249 } 170 250 RTThreadSleep(10); 171 251 } 172 if (rc != VERR_PROCESS_RUNNING)173 fDoKillProc = false;174 252 } 175 253 176 254 if (fDoKillProc) 177 255 { 178 RTProcTerminate(m->mProcess); 179 int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL); 256 LogRel(("NetworkServiceRunning: killing %s, pid %RTproc...\n", m->pszProcName, m->Process)); 257 RTProcTerminate(m->Process); 258 259 int rc = RTProcWait(m->Process, RTPROCWAIT_FLAGS_BLOCK, NULL); 180 260 NOREF(rc); 181 261 } 182 262 183 m-> mProcess = NIL_RTPROCESS;263 m->Process = NIL_RTPROCESS; 184 264 return VINF_SUCCESS; 185 265 } 186 266 267 268 /** 269 * Checks if the service process is still running. 270 * 271 * @returns true if running, false if not. 272 */ 187 273 bool NetworkServiceRunner::isRunning() 188 274 { 189 if (m->mProcess == NIL_RTPROCESS) 190 return false; 191 192 RTPROCSTATUS status; 193 int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status); 194 195 if (rc == VERR_PROCESS_RUNNING) 196 return true; 197 198 m->mProcess = NIL_RTPROCESS; 275 RTPROCESS Process = m->Process; 276 if (Process != NIL_RTPROCESS) 277 { 278 RTPROCSTATUS ExitStatus; 279 int rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ExitStatus); 280 if (rc == VERR_PROCESS_RUNNING) 281 return true; 282 LogRel(("NetworkServiceRunning: %s (pid %RTproc) stopped: iStatus=%u enmReason=%d\n", 283 m->pszProcName, m->Process, ExitStatus.iStatus, ExitStatus.enmReason)); 284 m->Process = NIL_RTPROCESS; 285 } 199 286 return false; 200 287 }
Note:
See TracChangeset
for help on using the changeset viewer.