backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / nsprpub / pr / tests / parent.c
blobe49af2a98904702788529a5420a6fdcff09eba59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 ** file: parent.c
8 ** description: test the process machinery
9 */
11 #include "prmem.h"
12 #include "prprf.h"
13 #include "prinit.h"
14 #include "prproces.h"
15 #include "prinrval.h"
17 typedef struct Child
19 const char *name;
20 char **argv;
21 PRProcess *process;
22 PRProcessAttr *attr;
23 } Child;
25 /* for the default test 'cvar -c 2000' */
26 static char *default_argv[] = {"cvar", "-c", "2000", NULL};
28 static void PrintUsage(void)
30 PR_fprintf(PR_GetSpecialFD(PR_StandardError),
31 "Usage: parent [-d] child [options]\n");
34 int main(int argc, char **argv)
36 PRStatus rv;
37 PRInt32 test_status = 1;
38 PRIntervalTime t_start, t_elapsed;
39 PRFileDesc *debug = NULL;
40 Child *child = PR_NEWZAP(Child);
42 if (1 == argc)
44 /* no command-line arguments: run the default test */
45 child->argv = default_argv;
47 else
49 argv += 1; /* don't care about our program name */
50 while (*argv != NULL && argv[0][0] == '-')
52 if (argv[0][1] == 'd')
53 debug = PR_GetSpecialFD(PR_StandardError);
54 else
56 PrintUsage();
57 return 2; /* not sufficient */
59 argv += 1;
61 child->argv = argv;
64 if (NULL == *child->argv)
66 PrintUsage();
67 return 2;
70 child->name = *child->argv;
71 if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name);
73 child->attr = PR_NewProcessAttr();
74 PR_ProcessAttrSetStdioRedirect(
75 child->attr, PR_StandardOutput,
76 PR_GetSpecialFD(PR_StandardOutput));
77 PR_ProcessAttrSetStdioRedirect(
78 child->attr, PR_StandardError,
79 PR_GetSpecialFD(PR_StandardError));
81 t_start = PR_IntervalNow();
82 child->process = PR_CreateProcess(
83 child->name, child->argv, NULL, child->attr);
84 t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start);
86 PR_DestroyProcessAttr(child->attr);
88 test_status = (NULL == child->process) ? 1 : 0;
89 if (NULL != debug)
91 PR_fprintf(
92 debug, "Child was %sforked\n",
93 (0 == test_status) ? "" : "NOT ");
94 if (0 == test_status)
95 PR_fprintf(
96 debug, "PR_CreateProcess took %lu microseconds\n",
97 PR_IntervalToMicroseconds(t_elapsed));
100 if (0 == test_status)
102 if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n");
103 rv = PR_WaitProcess(child->process, &test_status);
104 if (PR_SUCCESS == rv)
106 if (NULL != debug)
107 PR_fprintf(
108 debug, "Child exited %s\n",
109 (0 == test_status) ? "successfully" : "with error");
111 else
113 test_status = 1;
114 if (NULL != debug)
115 PR_fprintf(debug, "PR_WaitProcess failed\n");
118 PR_DELETE(child);
119 PR_Cleanup();
120 return test_status;
122 } /* main */
124 /* parent.c */