1 | /* $Id: tstVMMFork.cpp 11822 2008-08-29 14:21:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM Fork Test.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <VBox/vm.h>
|
---|
27 | #include <VBox/vmm.h>
|
---|
28 | #include <VBox/err.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/runtime.h>
|
---|
32 | #include <iprt/stream.h>
|
---|
33 |
|
---|
34 | #include <errno.h>
|
---|
35 | #include <sys/wait.h>
|
---|
36 | #include <unistd.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *******************************************************************************/
|
---|
42 | #define TESTCASE "tstVMMFork"
|
---|
43 | #define AUTO_TEST_ARGS 1
|
---|
44 |
|
---|
45 | VMMR3DECL(int) VMMDoTest(PVM pVM);
|
---|
46 |
|
---|
47 |
|
---|
48 | int main(int argc, char* argv[])
|
---|
49 | {
|
---|
50 | int rcErrors = 0;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Initialize the runtime.
|
---|
54 | */
|
---|
55 | RTR3InitAndSUPLib();
|
---|
56 |
|
---|
57 | #ifndef AUTO_TEST_ARGS
|
---|
58 | if (argc < 2)
|
---|
59 | {
|
---|
60 | RTPrintf("syntax: %s command [args]\n"
|
---|
61 | "\n"
|
---|
62 | "command Command to run under child process in fork.\n"
|
---|
63 | "[args] Arguments to command.\n", argv[0]);
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Create empty VM.
|
---|
70 | */
|
---|
71 | RTPrintf(TESTCASE ": Initializing...\n");
|
---|
72 | PVM pVM;
|
---|
73 | int rc = VMR3Create(NULL, NULL, NULL, NULL, &pVM);
|
---|
74 | if (VBOX_SUCCESS(rc))
|
---|
75 | {
|
---|
76 | /*
|
---|
77 | * Do testing.
|
---|
78 | */
|
---|
79 | int iCowTester = 0;
|
---|
80 | char cCowTester = 'a';
|
---|
81 |
|
---|
82 | #ifndef AUTO_TEST_ARGS
|
---|
83 | int cArgs = argc - 1;
|
---|
84 | char **ppszArgs = &argv[1];
|
---|
85 | #else
|
---|
86 | int cArgs = 2;
|
---|
87 | char *ppszArgs[3];
|
---|
88 | ppszArgs[0] = (char *)"/bin/sleep";
|
---|
89 | ppszArgs[1] = (char *)"3";
|
---|
90 | ppszArgs[2] = NULL;
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | RTPrintf(TESTCASE ": forking current process...\n");
|
---|
94 | pid_t pid = fork();
|
---|
95 | if (pid < 0)
|
---|
96 | {
|
---|
97 | /* Bad. fork() failed! */
|
---|
98 | RTPrintf(TESTCASE ": error: fork() failed.\n");
|
---|
99 | rcErrors++;
|
---|
100 | }
|
---|
101 | else if (pid == 0)
|
---|
102 | {
|
---|
103 | /*
|
---|
104 | * The child process.
|
---|
105 | * Write to some local variables to trigger copy-on-write if it's used.
|
---|
106 | */
|
---|
107 | RTPrintf(TESTCASE ": running child process...\n");
|
---|
108 | RTPrintf(TESTCASE ": writing local variables...\n");
|
---|
109 | iCowTester = 2;
|
---|
110 | cCowTester = 'z';
|
---|
111 |
|
---|
112 | RTPrintf(TESTCASE ": calling execv() with command-line:\n");
|
---|
113 | for (int i = 0; i < cArgs; i++)
|
---|
114 | RTPrintf(TESTCASE ": ppszArgs[%d]=%s\n", i, ppszArgs[i]);
|
---|
115 | execv(ppszArgs[0], ppszArgs);
|
---|
116 | RTPrintf(TESTCASE ": error: execv() returned to caller. errno=%d.\n", errno);
|
---|
117 | _exit(-1);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | /*
|
---|
122 | * The parent process.
|
---|
123 | * Wait for child & run VMM test to ensure things are fine.
|
---|
124 | */
|
---|
125 | int result;
|
---|
126 | while (waitpid(pid, &result, 0) < 0)
|
---|
127 | ;
|
---|
128 | if (!WIFEXITED(result) || WEXITSTATUS(result) != 0)
|
---|
129 | {
|
---|
130 | RTPrintf(TESTCASE ": error: failed to run child process. errno=%d\n", errno);
|
---|
131 | rcErrors++;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (rcErrors == 0)
|
---|
135 | {
|
---|
136 | RTPrintf(TESTCASE ": fork() returned fine.\n");
|
---|
137 | RTPrintf(TESTCASE ": testing VM after fork.\n");
|
---|
138 | PVMREQ pReq1 = NULL;
|
---|
139 | rc = VMR3ReqCall(pVM, &pReq1, RT_INDEFINITE_WAIT, (PFNRT)VMMDoTest, 1, pVM);
|
---|
140 | AssertRC(rc);
|
---|
141 | VMR3ReqFree(pReq1);
|
---|
142 |
|
---|
143 | STAMR3Dump(pVM, "*");
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (rcErrors > 0)
|
---|
148 | RTPrintf(TESTCASE ": error: %d error(s) during fork(). Cannot proceed to test the VM.\n");
|
---|
149 | else
|
---|
150 | RTPrintf(TESTCASE ": fork() and VM test, SUCCESS.\n");
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Cleanup.
|
---|
154 | */
|
---|
155 | rc = VMR3Destroy(pVM);
|
---|
156 | if (!VBOX_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%d\n", rc);
|
---|
159 | rcErrors++;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%d\n", rc);
|
---|
165 | rcErrors++;
|
---|
166 | }
|
---|
167 |
|
---|
168 | return rcErrors;
|
---|
169 | }
|
---|