VirtualBox

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

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

tstSupSem-Zombie: minor adjustments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: tstSupSem-Zombie.cpp 20905 2009-06-24 21:38:42Z 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 int rc = SUPSemEventWaitNoResume(g_pSession, hEvent, 120*1000);
62 AssertReleaseMsgFailed(("%Rrc\n", rc));
63 return rc;
64}
65
66
67static DECLCALLBACK(int) tstSupSemMRETimed(RTTHREAD hSelf, void *pvUser)
68{
69 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
70 RTThreadUserSignal(hSelf);
71 int rc = SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, 120*1000);
72 AssertReleaseMsgFailed(("%Rrc\n", rc));
73 return rc;
74}
75
76
77static DECLCALLBACK(int) tstSupSemSREInf(RTTHREAD hSelf, void *pvUser)
78{
79 SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
80 RTThreadUserSignal(hSelf);
81 int rc = SUPSemEventWaitNoResume(g_pSession, hEvent, RT_INDEFINITE_WAIT);
82 AssertReleaseMsgFailed(("%Rrc\n", rc));
83 return rc;
84}
85
86
87static DECLCALLBACK(int) tstSupSemMREInf(RTTHREAD hSelf, void *pvUser)
88{
89 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
90 RTThreadUserSignal(hSelf);
91 int rc = SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, RT_INDEFINITE_WAIT);
92 AssertReleaseMsgFailed(("%Rrc\n", rc));
93 return rc;
94}
95
96static int mainChild(void)
97{
98 /*
99 * Init.
100 */
101 int rc = RTR3InitAndSUPLib();
102 if (RT_FAILURE(rc))
103 {
104 RTPrintf("tstSupSem-Zombie-Child: fatal error: RTR3InitAndSUPLib failed with rc=%Rrc\n", rc);
105 return 1;
106 }
107
108 RTTEST hTest;
109 rc = RTTestCreate("tstSupSem-Zombie-Child", &hTest);
110 if (RT_FAILURE(rc))
111 {
112 RTPrintf("tstSupSem-Zombie-Child: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
113 return 1;
114 }
115 g_hTest = hTest;
116
117 PSUPDRVSESSION pSession;
118 rc = SUPR3Init(&pSession);
119 if (RT_FAILURE(rc))
120 {
121 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
122 return RTTestSummaryAndDestroy(hTest);
123 }
124 g_pSession = pSession;
125
126 /*
127 * A semaphore of each kind and throw a bunch of threads on them.
128 */
129 SUPSEMEVENT hEvent = NIL_SUPSEMEVENT;
130 RTTESTI_CHECK_RC(rc = SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
131 if (RT_SUCCESS(rc))
132 {
133 SUPSEMEVENTMULTI hEventMulti = NIL_SUPSEMEVENT;
134 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
135 if (RT_SUCCESS(rc))
136 {
137 for (uint32_t cThreads = 0; cThreads < 5; cThreads++)
138 {
139 RTTHREAD hThread;
140 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSRETimed, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
141 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMRETimed, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
142 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemSREInf, (void *)hEvent, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntSRE"), VINF_SUCCESS);
143 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemMREInf, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, 0 /*fFlags*/, "IntMRE"), VINF_SUCCESS);
144 RTThreadSleep(2);
145 }
146 RTThreadSleep(50);
147
148 /*
149 * This is where the test really starts...
150 */
151 return 0;
152 }
153 }
154
155 return RTTestSummaryAndDestroy(hTest);
156}
157
158
159/**
160 * The parent main routine.
161 * @param argv0 The executable name (or whatever).
162 */
163static int mainParent(const char *argv0)
164{
165 /*
166 * Init.
167 */
168 RTTEST hTest;
169 int rc = RTTestInitAndCreate("tstSupSem-Zombie", &hTest);
170 if (rc)
171 return rc;
172 RTTestBanner(hTest);
173
174 /*
175 * Spin of the child process which may or may not turn into a zombie
176 */
177 for (uint32_t iPass = 0; iPass < 32; iPass++)
178 {
179 RTTestSubF(hTest, "Pass %u", iPass);
180
181 RTPROCESS hProcess;
182 const char *apszArgs[3] = { argv0, "--child", NULL };
183 RTTESTI_CHECK_RC_OK(rc = RTProcCreate(argv0, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProcess));
184 if (RT_SUCCESS(rc))
185 {
186 /*
187 * Wait for 60 seconds then give up.
188 */
189 RTPROCSTATUS Status;
190 uint64_t StartTS = RTTimeMilliTS();
191 for (;;)
192 {
193 rc = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &Status);
194 if (RT_SUCCESS(rc))
195 break;
196 uint64_t cElapsed = RTTimeMilliTS() - StartTS;
197 if (cElapsed > 60*1000)
198 break;
199 RTThreadSleep(cElapsed < 60 ? 30 : cElapsed < 200 ? 10 : 100);
200 }
201 RTTESTI_CHECK_RC_OK(rc);
202 if ( RT_SUCCESS(rc)
203 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
204 || Status.iStatus != 0))
205 {
206 RTTestIFailed("child %d (%#x) reason %d\n", Status.iStatus, Status.iStatus, Status.enmReason);
207 rc = VERR_PERMISSION_DENIED;
208 }
209 }
210 /* one zombie process is enough. */
211 if (RT_FAILURE(rc))
212 break;
213 }
214
215 return RTTestSummaryAndDestroy(hTest);
216}
217
218
219int main(int argc, char **argv)
220{
221 if ( argc == 2
222 && !strcmp(argv[1], "--child"))
223 return mainChild();
224 return mainParent(argv[0]);
225}
226
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