NetworkServiceRunner.cpp revision 8680dc7232f3eefc091f564093aaffd15075d898
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/* $Id$ */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/** @file
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * VirtualBox Main - interface for VBox DHCP server
59190ecd61435d19ba3515b876272aee7bd12298vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/*
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Copyright (C) 2009-2012 Oracle Corporation
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * available from http://www.virtualbox.org. This file is free software;
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * General Public License (GPL) as published by the Free Software
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <map>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <string>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include "NetworkServiceRunner.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <iprt/process.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <iprt/param.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#include <iprt/env.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrKeyName = "--name";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrKeyNetwork = "--network";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrKeyTrunkType = "--trunk-type";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrTrunkName = "--trunk-name";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrMacAddress = "--mac-address";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrIpAddress = "--ip-address";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrIpNetmask = "--netmask";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncconst std::string NetworkServiceRunner::kNsrKeyNeedMain = "--need-main";
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstruct NetworkServiceRunner::Data
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync Data(const char* aProcName):mProcName(aProcName), mProcess(NIL_RTPROCESS){}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync const char *mProcName;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync RTPROCESS mProcess;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync std::map<std::string, std::string> mOptions;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync};
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncNetworkServiceRunner::NetworkServiceRunner(const char *aProcName)
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync m = new NetworkServiceRunner::Data(aProcName);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncNetworkServiceRunner::~NetworkServiceRunner()
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync stop();
59190ecd61435d19ba3515b876272aee7bd12298vboxsync delete m;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync m = NULL;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncint NetworkServiceRunner::setOption(const std::string& key, const std::string& val)
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync m->mOptions.insert(std::map<std::string, std::string>::value_type(key, val));
59190ecd61435d19ba3515b876272aee7bd12298vboxsync return VINF_SUCCESS;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncvoid NetworkServiceRunner::detachFromServer()
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync m->mProcess = NIL_RTPROCESS;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncint NetworkServiceRunner::start()
59190ecd61435d19ba3515b876272aee7bd12298vboxsync{
59190ecd61435d19ba3515b876272aee7bd12298vboxsync if (isRunning())
59190ecd61435d19ba3515b876272aee7bd12298vboxsync return VINF_ALREADY_INITIALIZED;
const char * args[10*2];
AssertReturn(m->mOptions.size() < 10, VERR_INTERNAL_ERROR);
/* get the path to the executable */
char exePathBuf[RTPATH_MAX];
const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
char *substrSl = strrchr(exePathBuf, '/');
char *substrBs = strrchr(exePathBuf, '\\');
char *suffix = substrSl ? substrSl : substrBs;
if (suffix)
{
suffix++;
strcpy(suffix, m->mProcName);
}
int index = 0;
args[index++] = exePath;
std::map<std::string, std::string>::const_iterator it;
for(it = m->mOptions.begin(); it != m->mOptions.end(); ++it)
{
args[index++] = it->first.c_str();
args[index++] = it->second.c_str();
}
args[index++] = NULL;
int rc = RTProcCreate(suffix ? exePath : m->mProcName, args, RTENV_DEFAULT, 0, &m->mProcess);
if (RT_FAILURE(rc))
m->mProcess = NIL_RTPROCESS;
return rc;
}
int NetworkServiceRunner::stop()
{
if (!isRunning())
return VINF_OBJECT_DESTROYED;
m->mProcess = NIL_RTPROCESS;
return VINF_SUCCESS;
}
bool NetworkServiceRunner::isRunning()
{
if (m->mProcess == NIL_RTPROCESS)
return false;
RTPROCSTATUS status;
int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
if (rc == VERR_PROCESS_RUNNING)
return true;
m->mProcess = NIL_RTPROCESS;
return false;
}