1 | /* $Id: tstRTProcWait.cpp 13837 2008-11-05 02:54:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTProcWait.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 <iprt/runtime.h>
|
---|
36 | #include <iprt/process.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <iprt/stream.h>
|
---|
39 | #include <iprt/semaphore.h>
|
---|
40 | #include <iprt/env.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | typedef struct SpawnerArgs
|
---|
48 | {
|
---|
49 | RTPROCESS Process;
|
---|
50 | const char *pszExe;
|
---|
51 | } SPAWNERARGS, *PSPAWNERARGS;
|
---|
52 |
|
---|
53 |
|
---|
54 | DECLCALLBACK(int) SpawnerThread(RTTHREAD Thread, void *pvUser)
|
---|
55 | {
|
---|
56 | PSPAWNERARGS pArgs = (PSPAWNERARGS)pvUser;
|
---|
57 | pArgs->Process = NIL_RTPROCESS;
|
---|
58 | const char *apszArgs[3] = { pArgs->pszExe, "child", NULL };
|
---|
59 | return RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0, &pArgs->Process);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | int main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | RTR3Init();
|
---|
66 | if (argc == 2 && !strcmp(argv[1], "child"))
|
---|
67 | return 42;
|
---|
68 |
|
---|
69 | RTPrintf("tstRTWait: spawning a child in a separate thread and waits for it in the main thread...\n");
|
---|
70 | RTTHREAD Thread;
|
---|
71 | SPAWNERARGS Args = { NIL_RTPROCESS, argv[0] };
|
---|
72 | int rc = RTThreadCreate(&Thread, SpawnerThread, &Args, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "SPAWNER");
|
---|
73 | if (RT_SUCCESS(rc))
|
---|
74 | {
|
---|
75 | /* Wait for it to complete. */
|
---|
76 | int rc2;
|
---|
77 | int rc = RTThreadWait(Thread, RT_INDEFINITE_WAIT, &rc2);
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | rc = rc2;
|
---|
80 | if (RT_SUCCESS(rc))
|
---|
81 | {
|
---|
82 | /* wait for the process to complete */
|
---|
83 | RTPROCSTATUS Status;
|
---|
84 | rc = RTProcWait(Args.Process, 0, &Status);
|
---|
85 | if (RT_SUCCESS(rc))
|
---|
86 | {
|
---|
87 | if ( Status.enmReason == RTPROCEXITREASON_NORMAL
|
---|
88 | && Status.iStatus == 42)
|
---|
89 | RTPrintf("tstRTWait: Success!\n");
|
---|
90 | else
|
---|
91 | {
|
---|
92 | rc = VERR_GENERAL_FAILURE;
|
---|
93 | if (Status.enmReason != RTPROCEXITREASON_NORMAL)
|
---|
94 | RTPrintf("tstRTWait: Expected exit reason RTPROCEXITREASON_NORMAL, got %d.\n", Status.enmReason);
|
---|
95 | else
|
---|
96 | RTPrintf("tstRTWait: Expected exit status 42, got %d.\n", Status.iStatus);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else
|
---|
100 | RTPrintf("tstRTWait: RTProcWait failed with rc=%Rrc!\n", rc);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | RTPrintf("tstRTWait: RTThreadWait or SpawnerThread failed with rc=%Rrc!\n", rc);
|
---|
104 | }
|
---|
105 | else
|
---|
106 | RTPrintf("tstRTWait: RTThreadCreate failed with rc=%Rrc!\n", rc);
|
---|
107 |
|
---|
108 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
109 | }
|
---|