VirtualBox

Changeset 79644 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jul 9, 2019 2:01:06 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131950
Message:

Main/NetworkServiceRunner: eliminated std::string and replaced the std::map option/value map with a C array that can be passed directly to RTProcCreate. Changed the term 'options' to 'arguments' to avoid DHCP option confusion. bugref:9288

Location:
trunk/src/VBox/Main
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/DHCPServerImpl.h

    r79621 r79644  
    2323
    2424#include "DHCPServerWrap.h"
     25#include <map>
    2526
    2627namespace settings
     
    3132}
    3233
    33 
    34 #ifdef VBOX_WITH_HOSTNETIF_API
    35 struct NETIFINFO;
    36 #endif
    37 
    38 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
    39 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe"
    40 #else
    41 # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP"
    42 #endif
    43 
    44 class DHCPServerRunner : public NetworkServiceRunner
    45 {
    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 };
    5634
    5735/**
     
    6745 *  the middle.
    6846 */
    69 
    7047class ATL_NO_VTABLE DHCPServer
    7148    : public DHCPServerWrap
     
    8663    // Public internal methids.
    8764    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);
    9066
    9167private:
    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);
    9670
    9771    // wrapped IDHCPServer properties
  • trunk/src/VBox/Main/include/NetworkServiceRunner.h

    r76562 r79644  
    2222#endif
    2323
    24 #include <iprt/err.h>
    25 #include <iprt/types.h>
    26 #include <iprt/string.h>
    27 #include <iprt/mem.h>
    2824#include <VBox/com/string.h>
    2925
    30 #include <string>
    3126
     27/** @name Internal networking trunk type option values (NetworkServiceRunner::kpszKeyTrunkType)
     28 *  @{ */
    3229#define TRUNKTYPE_WHATEVER "whatever"
    3330#define TRUNKTYPE_NETFLT   "netflt"
    3431#define TRUNKTYPE_NETADP   "netadp"
    3532#define TRUNKTYPE_SRVNAT   "srvnat"
     33/** @} */
    3634
     35/**
     36 * Network service runner.
     37 *
     38 * Build arguments, starts and stops network service processes.
     39 */
    3740class NetworkServiceRunner
    3841{
     
    4144    virtual ~NetworkServiceRunner();
    4245
    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    /** @} */
    4552
    46     int  start(bool aKillProcOnStop);
     53    int  start(bool aKillProcessOnStop);
    4754    int  stop();
    4855    bool isRunning();
    4956    void detachFromServer();
    5057
    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    /** @} */
    5968
    6069private:
  • trunk/src/VBox/Main/src-server/DHCPServerImpl.cpp

    r79623 r79644  
    1616 */
    1717
     18
     19/*********************************************************************************************************************************
     20*   Header Files                                                                                                                 *
     21*********************************************************************************************************************************/
    1822#define LOG_GROUP LOG_GROUP_MAIN_DHCPSERVER
    1923#include "NetworkServiceRunner.h"
     
    2327
    2428#include <iprt/asm.h>
     29#include <iprt/err.h>
    2530#include <iprt/file.h>
    2631#include <iprt/net.h>
     
    3540#include "VirtualBoxImpl.h"
    3641
    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 */
     58class DHCPServerRunner : public NetworkServiceRunner
     59{
     60public:
     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 */
    4783struct DHCPServer::Data
    4884{
     
    5187        , router(false)
    5288    {
    53         tempConfigFileName[0] = '\0';
     89        szTempConfigFileName[0] = '\0';
    5490    }
    5591
     
    65101    settings::VmSlot2OptionsMap VmSlot2Options;
    66102
    67     char tempConfigFileName[RTPATH_MAX];
     103    char szTempConfigFileName[RTPATH_MAX];
    68104    com::Utf8Str strLeasesFilename;
    69105    com::Utf8Str networkName;
     
    73109
    74110
     111// constructor / destructor
     112/////////////////////////////////////////////////////////////////////////////
     113
     114
    75115DHCPServer::DHCPServer()
    76   : m(NULL)
    77   , mVirtualBox(NULL)
     116    : m(NULL)
     117    , mVirtualBox(NULL)
    78118{
    79119    m = new DHCPServer::Data();
     
    144184
    145185
    146 HRESULT DHCPServer::init(VirtualBox *aVirtualBox,
    147                          const settings::DHCPServer &data)
     186HRESULT DHCPServer::init(VirtualBox *aVirtualBox, const settings::DHCPServer &data)
    148187{
    149188    /* Enclose the state transition NotReady->InInit->Ready */
     
    161200
    162201    m->GlobalDhcpOptions.clear();
    163     m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(),
    164                                data.GlobalDhcpOptions.end());
     202    m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(), data.GlobalDhcpOptions.end());
    165203
    166204    m->VmSlot2Options.clear();
    167     m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(),
    168                             data.VmSlot2OptionsM.end());
     205    m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(), data.VmSlot2OptionsM.end());
    169206
    170207    autoInitSpan.setSucceeded();
     
    700737        return hrc;
    701738
    702     m->dhcp.clearOptions(); /* (Not DHCP options, but command line options for the service) */
     739    m->dhcp.resetArguments();
    703740
    704741#ifdef VBOX_WITH_DHCPD
     
    708745     */
    709746    /** @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));
    711748    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");
    713750    if (RT_SUCCESS(rc))
    714         rc = RTFileCreateTemp(m->tempConfigFileName, 0600);
     751        rc = RTFileCreateTemp(m->szTempConfigFileName, 0600);
    715752    if (RT_FAILURE(rc))
    716753    {
    717         m->tempConfigFileName[0] = '\0';
     754        m->szTempConfigFileName[0] = '\0';
    718755        return E_FAIL;
    719756    }
     
    791828
    792829    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());
    797834
    798835#else /* !VBOX_WITH_DHCPD */
    799836    /* Main is needed for NATNetwork */
    800837    if (m->router)
    801         m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNeedMain, "on");
     838        m->dhcp.addArgPair(NetworkServiceRunner::kpszKeyNeedMain, "on");
    802839
    803840    /* Commmon Network Settings */
    804     m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNetwork, aNetworkName.c_str());
    805 
     841    m->dhcp.addArgPair(NetworkServiceRunner::kpszKeyNetwork, aNetworkName.c_str());
    806842    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());
    810845
    811846    /* XXX: should this MAC default initialization moved to NetworkServiceRunner? */
     
    814849    guid.create();
    815850    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());
    824857#endif /* !VBOX_WITH_DHCPD */
    825858
     
    830863
    831864
    832 HRESULT DHCPServer::stop (void)
     865HRESULT DHCPServer::stop(void)
    833866{
    834867#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;
    839872    }
    840873#endif /* VBOX_WITH_DHCPD */
  • trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp

    r77436 r79644  
    760760    AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
    761761
    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());
    766766
    767767    /* No portforwarding rules from command-line, all will be fetched via API */
  • trunk/src/VBox/Main/src-server/NetworkServiceRunner.cpp

    r79610 r79644  
    2020*   Header Files                                                                                                                 *
    2121*********************************************************************************************************************************/
    22 #include <map>
    23 #include <string>
    2422#include "NetworkServiceRunner.h"
    2523
     24#include <iprt/env.h>
     25#include <iprt/err.h>
     26#include <iprt/log.h>
    2627#include <iprt/process.h>
    2728#include <iprt/path.h>
    2829#include <iprt/param.h>
    29 #include <iprt/env.h>
    30 #include <iprt/log.h>
    3130#include <iprt/thread.h>
    3231
     
    3534*   Global Variables                                                                                                             *
    3635*********************************************************************************************************************************/
    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";
    4643
    4744
     
    4946*   Structures and Typedefs                                                                                                      *
    5047*********************************************************************************************************************************/
     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 */
    5153struct NetworkServiceRunner::Data
    5254{
     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
    5370    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)
    5777    {}
    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    }
    6393};
    6494
     
    79109
    80110
    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 */
     117int 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 */
     160int 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
     169void NetworkServiceRunner::resetArguments()
     170{
     171    m->resetArguments();
    91172}
    92173
     
    94175void NetworkServiceRunner::detachFromServer()
    95176{
    96     m->mProcess = NIL_RTPROCESS;
    97 }
    98 
    99 
    100 int NetworkServiceRunner::start(bool aKillProcOnStop)
     177    m->Process = NIL_RTPROCESS;
     178}
     179
     180
     181int NetworkServiceRunner::start(bool aKillProcessOnStop)
    101182{
    102183    if (isRunning())
     
    104185
    105186    /*
    106      * Construct the path to the executable.  ASSUME it is relative to the
    107      * 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.
    108189     */
    109190    char szExePath[RTPATH_MAX];
    110191    AssertReturn(RTProcGetExecutablePath(szExePath, RTPATH_MAX), VERR_FILENAME_TOO_LONG);
    111192    RTPathStripFilename(szExePath);
    112     int vrc = RTPathAppend(szExePath, sizeof(szExePath), m->mProcName);
     193    int vrc = RTPathAppend(szExePath, sizeof(szExePath), m->pszProcName);
    113194    AssertLogRelRCReturn(vrc, vrc);
    114195
    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;
    131209
    132210    /*
    133211     * Start the process:
    134212     */
    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
    142221    return rc;
    143222}
     
    155234    bool fDoKillProc = true;
    156235
    157     if (!m->mKillProcOnStop)
     236    if (!m->fKillProcessOnStop)
    158237    {
    159238        /*
     
    162241         * doing the final hard kill.
    163242         */
    164         int rc = VINF_SUCCESS;
    165243        for (unsigned int i = 0; i < 100; i++)
    166244        {
    167             rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL);
    168             if (RT_SUCCESS(rc))
     245            if (!isRunning())
     246            {
     247                fDoKillProc = false;
    169248                break;
     249            }
    170250            RTThreadSleep(10);
    171251        }
    172         if (rc != VERR_PROCESS_RUNNING)
    173             fDoKillProc = false;
    174252    }
    175253
    176254    if (fDoKillProc)
    177255    {
    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);
    180260        NOREF(rc);
    181261    }
    182262
    183     m->mProcess = NIL_RTPROCESS;
     263    m->Process = NIL_RTPROCESS;
    184264    return VINF_SUCCESS;
    185265}
    186266
     267
     268/**
     269 * Checks if the service process is still running.
     270 *
     271 * @returns true if running, false if not.
     272 */
    187273bool NetworkServiceRunner::isRunning()
    188274{
    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    }
    199286    return false;
    200287}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette