VirtualBox

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

Last change on this file since 20903 was 20903, checked in by vboxsync, 16 years ago

SUP: Added a testcase to make sure threads exit when the process exits.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette