1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* $Id: NetworkServiceRunner.cpp $ */
/** @file
* VirtualBox Main - interface for VBox DHCP server
*/
/*
* Copyright (C) 2009-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <map>
#include <string>
#include "NetworkServiceRunner.h"
#include <iprt/process.h>
#include <iprt/param.h>
#include <iprt/env.h>
#include <iprt/log.h>
#include <iprt/thread.h>
const std::string NetworkServiceRunner::kNsrKeyName = "--name";
const std::string NetworkServiceRunner::kNsrKeyNetwork = "--network";
const std::string NetworkServiceRunner::kNsrKeyTrunkType = "--trunk-type";
const std::string NetworkServiceRunner::kNsrTrunkName = "--trunk-name";
const std::string NetworkServiceRunner::kNsrMacAddress = "--mac-address";
const std::string NetworkServiceRunner::kNsrIpAddress = "--ip-address";
const std::string NetworkServiceRunner::kNsrIpNetmask = "--netmask";
const std::string NetworkServiceRunner::kNsrKeyNeedMain = "--need-main";
struct NetworkServiceRunner::Data
{
Data(const char* aProcName)
: mProcName(aProcName)
, mProcess(NIL_RTPROCESS)
, mKillProcOnStop(false)
{}
const char *mProcName;
RTPROCESS mProcess;
std::map<std::string, std::string> mOptions;
bool mKillProcOnStop;
};
NetworkServiceRunner::NetworkServiceRunner(const char *aProcName)
{
m = new NetworkServiceRunner::Data(aProcName);
}
NetworkServiceRunner::~NetworkServiceRunner()
{
stop();
delete m;
m = NULL;
}
int NetworkServiceRunner::setOption(const std::string& key, const std::string& val)
{
m->mOptions.insert(std::map<std::string, std::string>::value_type(key, val));
return VINF_SUCCESS;
}
void NetworkServiceRunner::clearOptions()
{
m->mOptions.clear();
}
void NetworkServiceRunner::detachFromServer()
{
m->mProcess = NIL_RTPROCESS;
}
int NetworkServiceRunner::start(bool aKillProcOnStop)
{
if (isRunning())
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;
m->mKillProcOnStop = aKillProcOnStop;
return rc;
}
int NetworkServiceRunner::stop()
{
/*
* If the process already terminated, this function will also grab the exit
* status and transition the process out of zombie status.
*/
if (!isRunning())
return VINF_OBJECT_DESTROYED;
bool fDoKillProc = true;
if (!m->mKillProcOnStop)
{
/*
* This is a VBoxSVC Main client. Do NOT kill it but assume it was shut
* down politely. Wait up to 1 second until the process is killed before
* doing the final hard kill.
*/
int rc = VINF_SUCCESS;
for (unsigned int i = 0; i < 100; i++)
{
rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL);
if (RT_SUCCESS(rc))
break;
RTThreadSleep(10);
}
if (rc != VERR_PROCESS_RUNNING)
fDoKillProc = false;
}
if (fDoKillProc)
{
RTProcTerminate(m->mProcess);
int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL);
NOREF(rc);
}
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;
}
|