VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstVMMFork.cpp@ 93435

Last change on this file since 93435 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.2 KB
Line 
1/* $Id: tstVMMFork.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VMM Fork Test.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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 <iprt/errcore.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
41VMMR3DECL(int) VMMDoTest(PVM pVM);
42
43
44int main(int argc, char* argv[])
45{
46 int rcErrors = 0;
47
48 /*
49 * Initialize the runtime.
50 */
51 RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_TRY_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 PUVM pUVM;
70 int rc = VMR3Create(1, NULL, NULL, NULL, NULL, NULL, &pVM, &pUVM);
71 if (RT_SUCCESS(rc))
72 {
73 /*
74 * Do testing.
75 */
76 int iCowTester = 0;
77 char cCowTester = 'a';
78
79#ifndef AUTO_TEST_ARGS
80 int cArgs = argc - 1;
81 char **ppszArgs = &argv[1];
82#else
83 int cArgs = 2;
84 char *ppszArgs[3];
85 ppszArgs[0] = (char *)"/bin/sleep";
86 ppszArgs[1] = (char *)"3";
87 ppszArgs[2] = NULL;
88#endif
89
90 RTPrintf(TESTCASE ": forking current process...\n");
91 pid_t pid = fork();
92 if (pid < 0)
93 {
94 /* Bad. fork() failed! */
95 RTPrintf(TESTCASE ": error: fork() failed.\n");
96 rcErrors++;
97 }
98 else if (pid == 0)
99 {
100 /*
101 * The child process.
102 * Write to some local variables to trigger copy-on-write if it's used.
103 */
104 RTPrintf(TESTCASE ": running child process...\n");
105 RTPrintf(TESTCASE ": writing local variables...\n");
106 iCowTester = 2;
107 cCowTester = 'z';
108
109 RTPrintf(TESTCASE ": calling execv() with command-line:\n");
110 for (int i = 0; i < cArgs; i++)
111 RTPrintf(TESTCASE ": ppszArgs[%d]=%s\n", i, ppszArgs[i]);
112 execv(ppszArgs[0], ppszArgs);
113 RTPrintf(TESTCASE ": error: execv() returned to caller. errno=%d.\n", errno);
114 _exit(-1);
115 }
116 else
117 {
118 /*
119 * The parent process.
120 * Wait for child & run VMM test to ensure things are fine.
121 */
122 int result;
123 while (waitpid(pid, &result, 0) < 0)
124 ;
125 if (!WIFEXITED(result) || WEXITSTATUS(result) != 0)
126 {
127 RTPrintf(TESTCASE ": error: failed to run child process. errno=%d\n", errno);
128 rcErrors++;
129 }
130
131 if (rcErrors == 0)
132 {
133 RTPrintf(TESTCASE ": fork() returned fine.\n");
134 RTPrintf(TESTCASE ": testing VM after fork.\n");
135 VMR3ReqCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)VMMDoTest, 1, pVM);
136
137 STAMR3Dump(pUVM, "*");
138 }
139 }
140
141 if (rcErrors > 0)
142 RTPrintf(TESTCASE ": error: %d error(s) during fork(). Cannot proceed to test the VM.\n", rcErrors);
143 else
144 RTPrintf(TESTCASE ": fork() and VM test, SUCCESS.\n");
145
146 /*
147 * Cleanup.
148 */
149 rc = VMR3PowerOff(pUVM);
150 if (!RT_SUCCESS(rc))
151 {
152 RTPrintf(TESTCASE ": error: failed to power off vm! rc=%Rrc\n", rc);
153 rcErrors++;
154 }
155 rc = VMR3Destroy(pUVM);
156 if (!RT_SUCCESS(rc))
157 {
158 RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%Rrc\n", rc);
159 rcErrors++;
160 }
161 VMR3ReleaseUVM(pUVM);
162 }
163 else
164 {
165 RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%Rrc\n", rc);
166 rcErrors++;
167 }
168
169 return rcErrors;
170}
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