~ubuntu-branches/ubuntu/vivid/virtualbox-ose/vivid

« back to all changes in this revision

Viewing changes to src/apps/adpctl/VBoxNetAdpCtl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-10-13 23:06:00 UTC
  • mfrom: (0.3.2 upstream) (0.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20091013230600-xhu2pwizq0wo63l9
Tags: 3.0.8-dfsg-1ubuntu1
* Merge from debian unstable (LP: #444812), remaining changes:
  - Enable DKMS support on virtualbox host and guest modules (LP: #267097)
    - Drop virtualbox-ose{-guest,}-modules-* package templates
    - Recommend *-source instead of *-modules packages
    - Replace error messages related to missing/mismatched
      kernel module accordingly
  - Autoload kernel module
    - LOAD_VBOXDRV_MODULE=1 in virtualbox-ose.default
  - Disable update action
    - patches/u01-disable-update-action.dpatch
  - Virtualbox should go in Accessories, not in System tools (LP: #288590)
    - virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add apport hook
    - virtualbox-ose.files/source_virtualbox-ose.py
    - virtualbox-ose.install
  - Add launchpad integration
    - control
    - lpi-bug.xpm
    - patches/u02-lp-integration.dpatch
* Try to remove existing dkms modules before adding the new modules
  (LP: #434503)
  - debian/virtualbox-ose-source.postinst
  - debian/virtualbox-ose-guest-source.postinst
* Don't fail if dkms modules have already been removed
  - debian/virtualbox-ose-source.prerm
  - debian/virtualbox-ose-guest-source.prerm

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
/*******************************************************************************
25
25
*   Header Files                                                               *
26
26
*******************************************************************************/
27
 
#include <assert.h>
28
27
#include <stdio.h>
29
28
#include <stdlib.h>
30
29
#include <string.h>
36
35
# include <sys/ioccom.h>
37
36
#endif
38
37
 
39
 
/* @todo Error codes must be moved to some header file */
40
 
#define ADPCTLERR_NO_CTL_DEV 3
41
 
#define ADPCTLERR_IOCTL_FAILED 4
 
38
/** @todo Error codes must be moved to some header file */
 
39
#define ADPCTLERR_BAD_NAME         2
 
40
#define ADPCTLERR_NO_CTL_DEV       3
 
41
#define ADPCTLERR_IOCTL_FAILED     4
42
42
 
43
 
/* @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
 
43
/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
44
44
#define VBOXNETADP_CTL_DEV_NAME    "/dev/vboxnetctl"
45
45
#define VBOXNETADP_NAME            "vboxnet"
46
46
#define VBOXNETADP_MAX_NAME_LEN    32
85
85
        pcszArg5, /* [network mask] */
86
86
        NULL  /* terminator */
87
87
    };
 
88
    char * const envp[] = { (char*)"LC_ALL=C", NULL };
88
89
    int rc = EXIT_SUCCESS;
89
90
    pid_t childPid = fork();
90
91
    switch (childPid)
94
95
            rc = EXIT_FAILURE;
95
96
            break;
96
97
        case 0: /* Child process. */
97
 
            if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
 
98
            if (execve(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv, envp) == -1)
98
99
                rc = EXIT_FAILURE;
99
100
            break;
100
101
        default: /* Parent process. */
108
109
#define MAX_ADDRESSES 128
109
110
#define MAX_ADDRLEN   64
110
111
 
111
 
static bool removeAddresses(const char *pszAdapterName)
 
112
static bool removeAddresses(char *pszAdapterName)
112
113
{
113
 
    char szCmd[1024], szBuf[1024];
 
114
    char szBuf[1024];
114
115
    char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
 
116
    int rc;
 
117
    int fds[2];
 
118
    char * const argv[] = { VBOXADPCTL_IFCONFIG_PATH, pszAdapterName, NULL };
 
119
    char * const envp[] = { (char*)"LC_ALL=C", NULL };
115
120
 
116
121
    memset(aszAddresses, 0, sizeof(aszAddresses));
117
 
    snprintf(szCmd, sizeof(szCmd), VBOXADPCTL_IFCONFIG_PATH " %s", pszAdapterName);
118
 
    FILE *fp = popen(szCmd, "r");
119
 
 
 
122
 
 
123
    rc = pipe(fds);
 
124
    if (rc < 0)
 
125
        return false;
 
126
 
 
127
    pid_t pid = fork();
 
128
    if (pid < 0)
 
129
        return false;
 
130
 
 
131
    if (pid == 0)
 
132
    {
 
133
        /* child */
 
134
        close(fds[0]);
 
135
        close(STDOUT_FILENO);
 
136
        rc = dup2(fds[1], STDOUT_FILENO);
 
137
        if (rc >= 0)
 
138
            execve(VBOXADPCTL_IFCONFIG_PATH, argv, envp);
 
139
        return false;
 
140
    }
 
141
 
 
142
    /* parent */
 
143
    close(fds[1]);
 
144
    FILE *fp = fdopen(fds[0], "r");
120
145
    if (!fp)
121
146
        return false;
122
147
 
124
149
    for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
125
150
    {
126
151
        int cbSkipWS = strspn(szBuf, " \t");
127
 
#if 0 /* Don't use this! assert() breaks the mac build. Use IPRT or be a rectangular building thing. */
128
 
        assert(cbSkipWS < 20);
129
 
#endif
130
152
        char *pszWord = strtok(szBuf + cbSkipWS, " ");
131
153
        /* We are concerned with IPv6 address lines only. */
132
154
        if (!pszWord || strcmp(pszWord, "inet6"))
143
165
            continue;
144
166
        strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
145
167
    }
146
 
    pclose(fp);
 
168
    fclose(fp);
147
169
 
148
170
    for (int i = 0; i < cAddrs; i++)
149
171
    {
150
 
        if (executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
 
172
        if (executeIfconfig(pszAdapterName, "inet6",
 
173
                            VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
151
174
            return false;
152
175
    }
153
176
 
166
189
    int rc = ioctl(fd, uCmd, pData);
167
190
    if (rc == -1)
168
191
    {
169
 
        perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME); 
 
192
        perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
170
193
        rc = ADPCTLERR_IOCTL_FAILED;
171
194
    }
172
 
    
 
195
 
173
196
    close(fd);
174
 
 
 
197
 
175
198
    return rc;
176
199
}
177
200
 
 
201
int checkAdapterName(const char *pcszNameIn, char *pszNameOut)
 
202
{
 
203
    int iAdapterIndex = -1;
 
204
 
 
205
    if (   strlen(pcszNameIn) >= VBOXNETADP_MAX_NAME_LEN
 
206
        || sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
 
207
        || iAdapterIndex < 0 || iAdapterIndex > 99 )
 
208
    {
 
209
        fprintf(stderr, "Setting configuration for %s is not supported.\n", pcszNameIn);
 
210
        return ADPCTLERR_BAD_NAME;
 
211
    }
 
212
    sprintf(pszNameOut, "vboxnet%d", iAdapterIndex);
 
213
    if (strcmp(pszNameOut, pcszNameIn))
 
214
    {
 
215
        fprintf(stderr, "Invalid adapter name %s.\n", pcszNameIn);
 
216
        return ADPCTLERR_BAD_NAME;
 
217
    }
 
218
 
 
219
    return 0;
 
220
}
 
221
 
178
222
int main(int argc, char *argv[])
179
223
 
180
224
{
181
 
    const char *pszAdapterName;
 
225
    char szAdapterName[VBOXNETADP_MAX_NAME_LEN];
 
226
    char *pszAdapterName;
182
227
    const char *pszAddress;
183
228
    const char *pszNetworkMask = NULL;
184
229
    const char *pszOption = NULL;
216
261
            pszAddress = argv[2];
217
262
            if (strcmp("remove", pszAddress) == 0)
218
263
            {
219
 
                strncpy(Req.szName, pszAdapterName, sizeof(Req.szName));
 
264
                rc = checkAdapterName(pszAdapterName, szAdapterName);
 
265
                if (rc)
 
266
                    return rc;
 
267
                memset(&Req, '\0', sizeof(Req));
 
268
                snprintf(Req.szName, sizeof(Req.szName), "%s", szAdapterName);
220
269
                return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
221
270
            }
222
271
            break;
223
272
        case 2:
224
273
            if (strcmp("add", argv[1]) == 0)
225
274
            {
 
275
                memset(&Req, '\0', sizeof(Req));
226
276
                rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
227
277
                if (rc == 0)
228
278
                    puts(Req.szName);
237
287
            return 1;
238
288
    }
239
289
 
240
 
    if (strncmp("vboxnet", pszAdapterName, 7))
241
 
    {
242
 
        fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
243
 
        return 2;
244
 
    }
 
290
    rc = checkAdapterName(pszAdapterName, szAdapterName);
 
291
    if (rc)
 
292
        return rc;
 
293
 
 
294
    pszAdapterName = szAdapterName;
245
295
 
246
296
    if (fRemove)
247
297
    {
276
326
    }
277
327
    return rc;
278
328
}
 
329