1 | /* $Id: tstRTProcWait.cpp 4475 2007-09-01 01:21:19Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime Testcase - RTProcWait.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/runtime.h>
|
---|
23 | #include <iprt/process.h>
|
---|
24 | #include <iprt/thread.h>
|
---|
25 | #include <iprt/stream.h>
|
---|
26 | #include <iprt/semaphore.h>
|
---|
27 | #include <iprt/env.h>
|
---|
28 | #include <iprt/err.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | typedef struct SpawnerArgs
|
---|
35 | {
|
---|
36 | RTPROCESS Process;
|
---|
37 | const char *pszExe;
|
---|
38 | } SPAWNERARGS, *PSPAWNERARGS;
|
---|
39 |
|
---|
40 |
|
---|
41 | DECLCALLBACK(int) SpawnerThread(RTTHREAD Thread, void *pvUser)
|
---|
42 | {
|
---|
43 | PSPAWNERARGS pArgs = (PSPAWNERARGS)pvUser;
|
---|
44 | pArgs->Process = NIL_RTPROCESS;
|
---|
45 | const char *apszArgs[3] = { pArgs->pszExe, "child", NULL };
|
---|
46 | return RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0, &pArgs->Process);
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | int main(int argc, char **argv)
|
---|
51 | {
|
---|
52 | RTR3Init();
|
---|
53 | if (argc == 2 && !strcmp(argv[1], "child"))
|
---|
54 | return 42;
|
---|
55 |
|
---|
56 | RTPrintf("tstRTWait: spawning a child in a separate thread and waits for it in the main thread...\n");
|
---|
57 | RTTHREAD Thread;
|
---|
58 | SPAWNERARGS Args = { NIL_RTPROCESS, argv[0] };
|
---|
59 | int rc = RTThreadCreate(&Thread, SpawnerThread, &Args, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "SPAWNER");
|
---|
60 | if (RT_SUCCESS(rc))
|
---|
61 | {
|
---|
62 | /* Wait for it to complete. */
|
---|
63 | int rc2;
|
---|
64 | int rc = RTThreadWait(Thread, RT_INDEFINITE_WAIT, &rc2);
|
---|
65 | if (RT_SUCCESS(rc))
|
---|
66 | rc = rc2;
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | /* wait for the process to complete */
|
---|
70 | RTPROCSTATUS Status;
|
---|
71 | rc = RTProcWait(Args.Process, 0, &Status);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | if ( Status.enmReason == RTPROCEXITREASON_NORMAL
|
---|
75 | && Status.iStatus == 42)
|
---|
76 | RTPrintf("tstRTWait: Success!\n");
|
---|
77 | else
|
---|
78 | {
|
---|
79 | rc = VERR_GENERAL_FAILURE;
|
---|
80 | if (Status.enmReason != RTPROCEXITREASON_NORMAL)
|
---|
81 | RTPrintf("tstRTWait: Expected exit reason RTPROCEXITREASON_NORMAL, got %d.\n", Status.enmReason);
|
---|
82 | else
|
---|
83 | RTPrintf("tstRTWait: Expected exit status 42, got %d.\n", Status.iStatus);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | else
|
---|
87 | RTPrintf("tstRTWait: RTProcWait failed with rc=%Vrc!\n", rc);
|
---|
88 | }
|
---|
89 | else
|
---|
90 | RTPrintf("tstRTWait: RTThreadWait or SpawnerThread failed with rc=%Vrc!\n", rc);
|
---|
91 | }
|
---|
92 | else
|
---|
93 | RTPrintf("tstRTWait: RTThreadCreate failed with rc=%Vrc!\n", rc);
|
---|
94 |
|
---|
95 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
96 | }
|
---|