VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/NetworkServiceRunner.cpp@ 51574

Last change on this file since 51574 was 50213, checked in by vboxsync, 11 years ago

spaces

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: NetworkServiceRunner.cpp 50213 2014-01-24 08:23:12Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for VBox DHCP server
4 */
5
6/*
7 * Copyright (C) 2009-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <map>
19#include <string>
20#include "NetworkServiceRunner.h"
21#include <iprt/process.h>
22#include <iprt/param.h>
23#include <iprt/env.h>
24
25
26const std::string NetworkServiceRunner::kNsrKeyName = "--name";
27const std::string NetworkServiceRunner::kNsrKeyNetwork = "--network";
28const std::string NetworkServiceRunner::kNsrKeyTrunkType = "--trunk-type";
29const std::string NetworkServiceRunner::kNsrTrunkName = "--trunk-name";
30const std::string NetworkServiceRunner::kNsrMacAddress = "--mac-address";
31const std::string NetworkServiceRunner::kNsrIpAddress = "--ip-address";
32const std::string NetworkServiceRunner::kNsrIpNetmask = "--netmask";
33const std::string NetworkServiceRunner::kNsrKeyNeedMain = "--need-main";
34
35struct NetworkServiceRunner::Data
36{
37 Data(const char* aProcName):mProcName(aProcName), mProcess(NIL_RTPROCESS){}
38 const char *mProcName;
39 RTPROCESS mProcess;
40 std::map<std::string, std::string> mOptions;
41};
42
43NetworkServiceRunner::NetworkServiceRunner(const char *aProcName)
44{
45 m = new NetworkServiceRunner::Data(aProcName);
46
47}
48
49
50NetworkServiceRunner::~NetworkServiceRunner()
51{
52 stop();
53 delete m;
54 m = NULL;
55}
56
57
58int NetworkServiceRunner::setOption(const std::string& key, const std::string& val)
59{
60 m->mOptions.insert(std::map<std::string, std::string>::value_type(key, val));
61 return VINF_SUCCESS;
62}
63
64
65void NetworkServiceRunner::detachFromServer()
66{
67 m->mProcess = NIL_RTPROCESS;
68}
69
70
71int NetworkServiceRunner::start()
72{
73 if (isRunning())
74 return VINF_ALREADY_INITIALIZED;
75
76 const char * args[10*2];
77
78 AssertReturn(m->mOptions.size() < 10, VERR_INTERNAL_ERROR);
79
80 /* get the path to the executable */
81 char exePathBuf[RTPATH_MAX];
82 const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
83 char *substrSl = strrchr(exePathBuf, '/');
84 char *substrBs = strrchr(exePathBuf, '\\');
85 char *suffix = substrSl ? substrSl : substrBs;
86
87 if (suffix)
88 {
89 suffix++;
90 strcpy(suffix, m->mProcName);
91 }
92
93 int index = 0;
94
95 args[index++] = exePath;
96
97 std::map<std::string, std::string>::const_iterator it;
98 for(it = m->mOptions.begin(); it != m->mOptions.end(); ++it)
99 {
100 args[index++] = it->first.c_str();
101 args[index++] = it->second.c_str();
102 }
103
104 args[index++] = NULL;
105
106 int rc = RTProcCreate(suffix ? exePath : m->mProcName, args, RTENV_DEFAULT, 0, &m->mProcess);
107 if (RT_FAILURE(rc))
108 m->mProcess = NIL_RTPROCESS;
109
110 return rc;
111}
112
113
114int NetworkServiceRunner::stop()
115{
116 if (!isRunning())
117 return VINF_OBJECT_DESTROYED;
118
119 int rc = RTProcTerminate(m->mProcess);
120 RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL);
121 m->mProcess = NIL_RTPROCESS;
122 return rc;
123}
124
125bool NetworkServiceRunner::isRunning()
126{
127 if (m->mProcess == NIL_RTPROCESS)
128 return false;
129
130 RTPROCSTATUS status;
131 int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
132
133 if (rc == VERR_PROCESS_RUNNING)
134 return true;
135
136 m->mProcess = NIL_RTPROCESS;
137 return false;
138}
Note: See TracBrowser for help on using the repository browser.

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