VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstSupSem-Zombie.cpp@ 20904

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

build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: tstSupSem-Zombie.cpp 20904 2009-06-24 19:34:49Z vboxsync $ */
2/** @file
3 * Support Library Testcase - Ring-3 Semaphore interface - Zombie bugs.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <VBox/sup.h>
36
37#include <VBox/param.h>
38#include <iprt/env.h>
39#include <iprt/err.h>
40#include <iprt/initterm.h>
41#include <iprt/process.h>
42#include <iprt/stream.h>
43#include <iprt/string.h>
44#include <iprt/test.h>
45#include <iprt/time.h>
46#include <iprt/thread.h>
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52static PSUPDRVSESSION g_pSession;
53static RTTEST g_hTest;
54
55
56
57static DECLCALLBACK(int) tstSupSemSRETimed(RTTHREAD hSelf, void *pvUser)
58{
59 SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
60 RTThreadUserSignal(hSelf);
61 return SUPSemEventWaitNoResume(g_pSession, hEvent, 120*1000);
62}
63
64
65static DECLCALLBACK(int) tstSupSemMRETimed(RTTHREAD hSelf, void *pvUser)
66{
67 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
68 RTThreadUserSignal(hSelf);
69 return SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, 120*1000);
70}
71
72
73static DECLCALLBACK(int) tstSupSemSREInf(RTTHREAD hSelf, void *pvUser)
74{
75 SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
76 RTThreadUserSignal(hSelf);
77 return SUPSemEventWaitNoResume(g_pSession, hEvent, RT_INDEFINITE_WAIT);
78}
79
80
81static DECLCALLBACK(int) tstSupSemMREInf(RTTHREAD hSelf, void *pvUser)
82{
83 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
84 RTThreadUserSignal(hSelf);
85 return SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, RT_INDEFINITE_WAIT);
86}
87
88static int mainChild(void)
89{
90 /*
91 * Init.
92 */
93 int rc = RTR3InitAndSUPLib();
94 if (RT_FAILURE(rc))
95 {
96 RTPrintf("tstSupSem-Zombie-Child: fatal error: RTR3InitAndSUPLib failed with rc=%Rrc\n", rc);
97 return 1;
98 }
99
100 RTTEST hTest;
101 rc = RTTestCreate("tstSupSem-Zombie-Child", &hTest);
102 if (RT_FAILURE(rc))
103 {
104 RTPrintf("tstSupSem-Zombie-Child: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
105 return 1;
106 }
107 g_hTest = hTest;
108
109 PSUPDRVSESSION pSession;
110 rc = SUPR3Init(&pSession);
111 if (RT_FAILURE(rc))
112 {
113 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
114 return RTTestSummaryAndDestroy(hTest);
115 }
116 g_pSession = pSession;
117
118 /*
119 * A semaphore of each kind and throw a bunch of threads on them.
120 */
121 SUPSEMEVENT hEvent = NIL_SUPSEMEVENT;
122 RTTESTI_CHECK_RC(rc = SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
123 if (RT_SUCCESS(rc))
124 {
125 SUPSEMEVENTMULTI hEventMulti = NIL_SUPSEMEVENT;
126 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
127 if (RT_SUCCESS(rc))
128 {
129 for (uint32_t cThreads = 0; cThreads < 5; cThreads++)
130 {
131 RTTHREAD hThread;
132 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSRETimed, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
133 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMRETimed, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
134 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSREInf, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
135 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMREInf, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
136 RTThreadSleep(2);
137 }
138 RTThreadSleep(50);
139
140 /*
141 * This is where the test really starts...
142 */
143 return 0;
144 }
145 }
146
147 return RTTestSummaryAndDestroy(hTest);
148}
149
150
151/**
152 * The parent main routine.
153 * @param argv0 The executable name (or whatever).
154 */
155static int mainParent(const char *argv0)
156{
157 /*
158 * Init.
159 */
160 RTTEST hTest;
161 int rc = RTTestInitAndCreate("tstSupSem-Zombie", &hTest);
162 if (rc)
163 return rc;
164 RTTestBanner(hTest);
165
166 /*
167 * Spin of the child process which may or may not turn into a zombie
168 */
169 for (uint32_t iPass = 0; iPass < 32; iPass++)
170 {
171 RTTestSubF(hTest, "Pass %u", iPass);
172
173 RTPROCESS hProcess;
174 const char *apszArgs[3] = { argv0, "--child", NULL };
175 RTTESTI_CHECK_RC_OK(rc = RTProcCreate(argv0, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProcess));
176 if (RT_SUCCESS(rc))
177 {
178 /*
179 * Wait for 60 seconds then give up.
180 */
181 RTPROCSTATUS Status;
182 uint64_t StartTS = RTTimeMilliTS();
183 for (;;)
184 {
185 rc = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &Status);
186 if (RT_SUCCESS(rc))
187 break;
188 uint64_t cElapsed = RTTimeMilliTS() - StartTS;
189 if (cElapsed > 60*1000)
190 break;
191 RTThreadSleep(cElapsed < 60 ? 30 : cElapsed < 200 ? 10 : 100);
192 }
193 RTTESTI_CHECK_RC_OK(rc);
194 if ( RT_SUCCESS(rc)
195 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
196 || Status.iStatus != 0))
197 {
198 RTTestIFailed("child %u (%d) reason %d\n", Status.iStatus, Status.iStatus, Status.enmReason);
199 rc = VERR_PERMISSION_DENIED;
200 }
201 }
202 /* one zombie process is enough. */
203 if (RT_FAILURE(rc))
204 break;
205 }
206
207 return RTTestSummaryAndDestroy(hTest);
208}
209
210
211int main(int argc, char **argv)
212{
213 if ( argc == 2
214 && !strcmp(argv[1], "--child"))
215 return mainChild();
216 return mainParent(argv[0]);
217}
218
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