VirtualBox

source: vbox/trunk/src/apps/adpctl/VBoxNetAdpCtl.cpp@ 23048

Last change on this file since 23048 was 23048, checked in by vboxsync, 15 years ago

VBoxNetAdpCtl.cpp: Fix for execv/dup2 failure path in the child.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: VBoxNetAdpCtl.cpp 23048 2009-09-15 23:21:32Z vboxsync $ */
2/** @file
3 * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <assert.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/wait.h>
33#include <sys/ioctl.h>
34#include <fcntl.h>
35#ifdef RT_OS_SOLARIS
36# include <sys/ioccom.h>
37#endif
38
39/* @todo Error codes must be moved to some header file */
40#define ADPCTLERR_NO_CTL_DEV 3
41#define ADPCTLERR_IOCTL_FAILED 4
42
43/* @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
44#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
45#define VBOXNETADP_NAME "vboxnet"
46#define VBOXNETADP_MAX_NAME_LEN 32
47#define VBOXNETADP_CTL_ADD _IOR('v', 1, VBOXNETADPREQ)
48#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
49typedef struct VBoxNetAdpReq
50{
51 char szName[VBOXNETADP_MAX_NAME_LEN];
52} VBOXNETADPREQ;
53typedef VBOXNETADPREQ *PVBOXNETADPREQ;
54
55
56#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
57
58#ifdef RT_OS_LINUX
59#define VBOXADPCTL_DEL_CMD "del"
60#else
61#define VBOXADPCTL_DEL_CMD "delete"
62#endif
63
64static void showUsage(void)
65{
66 fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
67 fprintf(stderr, " | VBoxNetAdpCtl add\n");
68 fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
69}
70
71static int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
72 const char *pcszArg2 = NULL,
73 const char *pcszArg3 = NULL,
74 const char *pcszArg4 = NULL,
75 const char *pcszArg5 = NULL)
76{
77 const char * const argv[] =
78 {
79 VBOXADPCTL_IFCONFIG_PATH,
80 pcszAdapterName,
81 pcszArg1, /* [address family] */
82 pcszArg2, /* address */
83 pcszArg3, /* ['netmask'] */
84 pcszArg4, /* [network mask] */
85 pcszArg5, /* [network mask] */
86 NULL /* terminator */
87 };
88 int rc = EXIT_SUCCESS;
89 pid_t childPid = fork();
90 switch (childPid)
91 {
92 case -1: /* Something went wrong. */
93 perror("fork() failed");
94 rc = EXIT_FAILURE;
95 break;
96 case 0: /* Child process. */
97 if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
98 rc = EXIT_FAILURE;
99 break;
100 default: /* Parent process. */
101 waitpid(childPid, &rc, 0);
102 break;
103 }
104
105 return rc;
106}
107
108#define MAX_ADDRESSES 128
109#define MAX_ADDRLEN 64
110
111static bool removeAddresses(char *pszAdapterName)
112{
113 char szBuf[1024];
114 char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
115 int rc;
116 int fds[2];
117 char * const argv[] = { pszAdapterName, NULL };
118 char * const envp[] = { (char*)"LC_ALL=C", NULL };
119
120 memset(aszAddresses, 0, sizeof(aszAddresses));
121
122 rc = pipe(fds);
123 if (rc < 0)
124 return false;
125
126 pid_t pid = fork();
127 if (pid < 0)
128 return false;
129
130 if (pid == 0)
131 {
132 /* child */
133 close(fds[0]);
134 close(STDOUT_FILENO);
135 rc = dup2(fds[1], STDOUT_FILENO);
136 if (rc >= 0)
137 execve(VBOXADPCTL_IFCONFIG_PATH, argv, envp);
138 return false;
139 }
140
141 /* parent */
142 close(fds[1]);
143 FILE *fp = fdopen(fds[0], "r");
144 if (!fp)
145 return false;
146
147 int cAddrs;
148 for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
149 {
150 int cbSkipWS = strspn(szBuf, " \t");
151#if 0 /* Don't use this! assert() breaks the mac build. Use IPRT or be a rectangular building thing. */
152 assert(cbSkipWS < 20);
153#endif
154 char *pszWord = strtok(szBuf + cbSkipWS, " ");
155 /* We are concerned with IPv6 address lines only. */
156 if (!pszWord || strcmp(pszWord, "inet6"))
157 continue;
158#ifdef RT_OS_LINUX
159 pszWord = strtok(NULL, " ");
160 /* Skip "addr:". */
161 if (!pszWord || strcmp(pszWord, "addr:"))
162 continue;
163#endif
164 pszWord = strtok(NULL, " ");
165 /* Skip link-local addresses. */
166 if (!pszWord || !strncmp(pszWord, "fe80", 4))
167 continue;
168 strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
169 }
170 fclose(fp);
171
172 for (int i = 0; i < cAddrs; i++)
173 {
174 if (executeIfconfig(pszAdapterName, "inet6",
175 VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
176 return false;
177 }
178
179 return true;
180}
181
182int doIOCtl(unsigned long uCmd, void *pData)
183{
184 int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
185 if (fd == -1)
186 {
187 perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
188 return ADPCTLERR_NO_CTL_DEV;
189 }
190
191 int rc = ioctl(fd, uCmd, pData);
192 if (rc == -1)
193 {
194 perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
195 rc = ADPCTLERR_IOCTL_FAILED;
196 }
197
198 close(fd);
199
200 return rc;
201}
202
203int main(int argc, char *argv[])
204
205{
206 char *pszAdapterName;
207 const char *pszAddress;
208 const char *pszNetworkMask = NULL;
209 const char *pszOption = NULL;
210 int rc = EXIT_SUCCESS;
211 bool fRemove = false;
212 VBOXNETADPREQ Req;
213
214 switch (argc)
215 {
216 case 5:
217 if (strcmp("netmask", argv[3]))
218 {
219 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
220 showUsage();
221 return 1;
222 }
223 pszOption = "netmask";
224 pszNetworkMask = argv[4];
225 pszAdapterName = argv[1];
226 pszAddress = argv[2];
227 break;
228 case 4:
229 if (strcmp("remove", argv[3]))
230 {
231 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
232 showUsage();
233 return 1;
234 }
235 fRemove = true;
236 pszAdapterName = argv[1];
237 pszAddress = argv[2];
238 break;
239 case 3:
240 pszAdapterName = argv[1];
241 pszAddress = argv[2];
242 if (strcmp("remove", pszAddress) == 0)
243 {
244 snprintf(Req.szName, sizeof(Req.szName), "%s", pszAdapterName);
245 return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
246 }
247 break;
248 case 2:
249 if (strcmp("add", argv[1]) == 0)
250 {
251 rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
252 if (rc == 0)
253 puts(Req.szName);
254 return rc;
255 }
256 /* Fall through */
257 default:
258 fprintf(stderr, "Invalid number of arguments.\n\n");
259 /* Fall through */
260 case 1:
261 showUsage();
262 return 1;
263 }
264
265 if (strncmp("vboxnet", pszAdapterName, 7))
266 {
267 fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
268 return 2;
269 }
270
271 if (fRemove)
272 {
273 if (strchr(pszAddress, ':'))
274 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
275 else
276 {
277#ifdef RT_OS_LINUX
278 rc = executeIfconfig(pszAdapterName, "0.0.0.0");
279#else
280 rc = executeIfconfig(pszAdapterName, "delete", pszAddress);
281#endif
282 }
283 }
284 else
285 {
286 /* We are setting/replacing address. */
287 if (strchr(pszAddress, ':'))
288 {
289 /*
290 * Before we set IPv6 address we'd like to remove
291 * all previously assigned addresses except the
292 * self-assigned one.
293 */
294 if (!removeAddresses(pszAdapterName))
295 rc = EXIT_FAILURE;
296 else
297 rc = executeIfconfig(pszAdapterName, "inet6", "add", pszAddress, pszOption, pszNetworkMask);
298 }
299 else
300 rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
301 }
302 return rc;
303}
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