VirtualBox

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

Last change on this file since 49494 was 49494, checked in by vboxsync, 12 years ago

Main/NetServiceRunner: Make command line more flexible for adding service specific parameters.

XXX: creating final array parameter to RTProcCreate(array,) need further improvements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/* $Id: NetworkServiceRunner.cpp 49494 2013-11-15 10:32:10Z 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.215389.xyz. 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";
33
34struct NetworkServiceRunner::Data
35{
36 Data(const char* aProcName):mProcName(aProcName), mProcess(NIL_RTPROCESS){}
37 const char *mProcName;
38 RTPROCESS mProcess;
39 std::map<std::string, std::string> mOptions;
40};
41
42NetworkServiceRunner::NetworkServiceRunner(const char *aProcName)
43{
44 m = new NetworkServiceRunner::Data(aProcName);
45
46}
47
48
49NetworkServiceRunner::~NetworkServiceRunner()
50{
51 stop();
52 delete m;
53 m = NULL;
54}
55
56
57int NetworkServiceRunner::setOption(const std::string& key, const std::string& val)
58{
59 m->mOptions.insert(std::map<std::string, std::string>::value_type(key, val));
60 return VINF_SUCCESS;
61}
62
63
64void NetworkServiceRunner::detachFromServer()
65{
66 m->mProcess = NIL_RTPROCESS;
67}
68
69
70int NetworkServiceRunner::start()
71{
72 if (isRunning())
73 return VINF_ALREADY_INITIALIZED;
74
75 const char * args[10*2];
76
77 AssertReturn(m->mOptions.size() < 10, VERR_INTERNAL_ERROR);
78
79 /* get the path to the executable */
80 char exePathBuf[RTPATH_MAX];
81 const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
82 char *substrSl = strrchr(exePathBuf, '/');
83 char *substrBs = strrchr(exePathBuf, '\\');
84 char *suffix = substrSl ? substrSl : substrBs;
85
86 if (suffix)
87 {
88 suffix++;
89 strcpy(suffix, m->mProcName);
90 }
91
92 int index = 0;
93
94 args[index++] = exePath;
95
96 std::map<std::string, std::string>::const_iterator it;
97 for(it = m->mOptions.begin(); it != m->mOptions.end(); ++it)
98 {
99 args[index++] = it->first.c_str();
100 args[index++] = it->second.c_str();
101 }
102
103 args[index++] = NULL;
104
105 int rc = RTProcCreate(suffix ? exePath : m->mProcName, args, RTENV_DEFAULT, 0, &m->mProcess);
106 if (RT_FAILURE(rc))
107 m->mProcess = NIL_RTPROCESS;
108
109 return rc;
110}
111
112
113int NetworkServiceRunner::stop()
114{
115 if (!isRunning())
116 return VINF_OBJECT_DESTROYED;
117
118 int rc = RTProcTerminate(m->mProcess);
119 RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL);
120 m->mProcess = NIL_RTPROCESS;
121 return rc;
122}
123
124bool NetworkServiceRunner::isRunning()
125{
126 if (m->mProcess == NIL_RTPROCESS)
127 return false;
128
129 RTPROCSTATUS status;
130 int rc = RTProcWait(m->mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
131
132 if (rc == VERR_PROCESS_RUNNING)
133 return true;
134
135 m->mProcess = NIL_RTPROCESS;
136 return false;
137}
Note: See TracBrowser for help on using the repository browser.

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