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