1 | /* $Id: NetworkServiceRunner.cpp 47447 2013-07-29 04:16: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.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 | #include "NetworkServiceRunner.h"
|
---|
18 | #include <iprt/process.h>
|
---|
19 | #include <iprt/param.h>
|
---|
20 | #include <iprt/env.h>
|
---|
21 |
|
---|
22 | struct ARGDEF
|
---|
23 | {
|
---|
24 | NETCFG Type;
|
---|
25 | const char * Name;
|
---|
26 | };
|
---|
27 |
|
---|
28 | static const ARGDEF g_aArgDefs[] =
|
---|
29 | {
|
---|
30 | {NETCFG_NAME, "--name"},
|
---|
31 | {NETCFG_NETNAME, "--network"},
|
---|
32 | {NETCFG_TRUNKTYPE, "--trunk-type"},
|
---|
33 | {NETCFG_TRUNKNAME, "--trunk-name"},
|
---|
34 | {NETCFG_MACADDRESS, "--mac-address"},
|
---|
35 | {NETCFG_IPADDRESS, "--ip-address"},
|
---|
36 | {NETCFG_VERBOSE, "--verbose"},
|
---|
37 | {NETCFG_NETMASK, "--netmask"},
|
---|
38 | };
|
---|
39 |
|
---|
40 | static const ARGDEF * getArgDef(NETCFG type)
|
---|
41 | {
|
---|
42 | for (unsigned i = 0; i < RT_ELEMENTS(g_aArgDefs); i++)
|
---|
43 | if (g_aArgDefs[i].Type == type)
|
---|
44 | return &g_aArgDefs[i];
|
---|
45 |
|
---|
46 | return NULL;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void NetworkServiceRunner::detachFromServer()
|
---|
50 | {
|
---|
51 | mProcess = NIL_RTPROCESS;
|
---|
52 | }
|
---|
53 |
|
---|
54 | int NetworkServiceRunner::start()
|
---|
55 | {
|
---|
56 | if (isRunning())
|
---|
57 | return VINF_ALREADY_INITIALIZED;
|
---|
58 |
|
---|
59 | const char * args[NETCFG_NOTOPT_MAXVAL * 2];
|
---|
60 |
|
---|
61 | /* get the path to the executable */
|
---|
62 | char exePathBuf[RTPATH_MAX];
|
---|
63 | const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
|
---|
64 | char *substrSl = strrchr(exePathBuf, '/');
|
---|
65 | char *substrBs = strrchr(exePathBuf, '\\');
|
---|
66 | char *suffix = substrSl ? substrSl : substrBs;
|
---|
67 |
|
---|
68 | if (suffix)
|
---|
69 | {
|
---|
70 | suffix++;
|
---|
71 | strcpy(suffix, mProcName);
|
---|
72 | }
|
---|
73 |
|
---|
74 | int index = 0;
|
---|
75 |
|
---|
76 | args[index++] = exePath;
|
---|
77 |
|
---|
78 | for (unsigned i = 0; i < NETCFG_NOTOPT_MAXVAL; i++)
|
---|
79 | {
|
---|
80 | if (mOptionEnabled[i])
|
---|
81 | {
|
---|
82 | const ARGDEF *pArgDef = getArgDef((NETCFG)i);
|
---|
83 | if (!pArgDef)
|
---|
84 | continue;
|
---|
85 | args[index++] = pArgDef->Name;
|
---|
86 |
|
---|
87 | if (mOptions[i].length())
|
---|
88 | args[index++] = mOptions[i].c_str(); // value
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | args[index++] = NULL;
|
---|
93 |
|
---|
94 | int rc = RTProcCreate(suffix ? exePath : mProcName, args, RTENV_DEFAULT, 0, &mProcess);
|
---|
95 | if (RT_FAILURE(rc))
|
---|
96 | mProcess = NIL_RTPROCESS;
|
---|
97 |
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int NetworkServiceRunner::stop()
|
---|
102 | {
|
---|
103 | if (!isRunning())
|
---|
104 | return VINF_OBJECT_DESTROYED;
|
---|
105 |
|
---|
106 | int rc = RTProcTerminate(mProcess);
|
---|
107 | RTProcWait(mProcess, RTPROCWAIT_FLAGS_BLOCK, NULL);
|
---|
108 | mProcess = NIL_RTPROCESS;
|
---|
109 | return rc;
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool NetworkServiceRunner::isRunning()
|
---|
113 | {
|
---|
114 | if (mProcess == NIL_RTPROCESS)
|
---|
115 | return false;
|
---|
116 |
|
---|
117 | RTPROCSTATUS status;
|
---|
118 | int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
|
---|
119 |
|
---|
120 | if (rc == VERR_PROCESS_RUNNING)
|
---|
121 | return true;
|
---|
122 |
|
---|
123 | mProcess = NIL_RTPROCESS;
|
---|
124 | return false;
|
---|
125 | }
|
---|