1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ** file: parent.c
|
---|
40 | ** description: test the process machinery
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include "prmem.h"
|
---|
44 | #include "prprf.h"
|
---|
45 | #include "prinit.h"
|
---|
46 | #include "prproces.h"
|
---|
47 | #include "prinrval.h"
|
---|
48 |
|
---|
49 | typedef struct Child
|
---|
50 | {
|
---|
51 | const char *name;
|
---|
52 | char **argv;
|
---|
53 | PRProcess *process;
|
---|
54 | PRProcessAttr *attr;
|
---|
55 | } Child;
|
---|
56 |
|
---|
57 | /* for the default test 'cvar -c 2000' */
|
---|
58 | static char *default_argv[] = {"cvar", "-c", "2000", NULL};
|
---|
59 |
|
---|
60 | static void PrintUsage(void)
|
---|
61 | {
|
---|
62 | PR_fprintf(PR_GetSpecialFD(PR_StandardError),
|
---|
63 | "Usage: parent [-d] child [options]\n");
|
---|
64 | }
|
---|
65 |
|
---|
66 | PRIntn main (PRIntn argc, char **argv)
|
---|
67 | {
|
---|
68 | PRStatus rv;
|
---|
69 | PRInt32 test_status = 1;
|
---|
70 | PRIntervalTime t_start, t_elapsed;
|
---|
71 | PRFileDesc *debug = NULL;
|
---|
72 | Child *child = PR_NEWZAP(Child);
|
---|
73 |
|
---|
74 | if (1 == argc)
|
---|
75 | {
|
---|
76 | /* no command-line arguments: run the default test */
|
---|
77 | child->argv = default_argv;
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | argv += 1; /* don't care about our program name */
|
---|
82 | while (*argv != NULL && argv[0][0] == '-')
|
---|
83 | {
|
---|
84 | if (argv[0][1] == 'd')
|
---|
85 | debug = PR_GetSpecialFD(PR_StandardError);
|
---|
86 | else
|
---|
87 | {
|
---|
88 | PrintUsage();
|
---|
89 | return 2; /* not sufficient */
|
---|
90 | }
|
---|
91 | argv += 1;
|
---|
92 | }
|
---|
93 | child->argv = argv;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (NULL == *child->argv)
|
---|
97 | {
|
---|
98 | PrintUsage();
|
---|
99 | return 2;
|
---|
100 | }
|
---|
101 |
|
---|
102 | child->name = *child->argv;
|
---|
103 | if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name);
|
---|
104 |
|
---|
105 | child->attr = PR_NewProcessAttr();
|
---|
106 | PR_ProcessAttrSetStdioRedirect(
|
---|
107 | child->attr, PR_StandardOutput,
|
---|
108 | PR_GetSpecialFD(PR_StandardOutput));
|
---|
109 | PR_ProcessAttrSetStdioRedirect(
|
---|
110 | child->attr, PR_StandardError,
|
---|
111 | PR_GetSpecialFD(PR_StandardError));
|
---|
112 |
|
---|
113 | t_start = PR_IntervalNow();
|
---|
114 | child->process = PR_CreateProcess(
|
---|
115 | child->name, child->argv, NULL, child->attr);
|
---|
116 | t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start);
|
---|
117 |
|
---|
118 | PR_DestroyProcessAttr(child->attr);
|
---|
119 |
|
---|
120 | test_status = (NULL == child->process) ? 1 : 0;
|
---|
121 | if (NULL != debug)
|
---|
122 | {
|
---|
123 | PR_fprintf(
|
---|
124 | debug, "Child was %sforked\n",
|
---|
125 | (0 == test_status) ? "" : "NOT ");
|
---|
126 | if (0 == test_status)
|
---|
127 | PR_fprintf(
|
---|
128 | debug, "PR_CreateProcess took %lu microseconds\n",
|
---|
129 | PR_IntervalToMicroseconds(t_elapsed));
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (0 == test_status)
|
---|
133 | {
|
---|
134 | if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n");
|
---|
135 | rv = PR_WaitProcess(child->process, &test_status);
|
---|
136 | if (PR_SUCCESS == rv)
|
---|
137 | {
|
---|
138 | if (NULL != debug)
|
---|
139 | PR_fprintf(
|
---|
140 | debug, "Child exited %s\n",
|
---|
141 | (0 == test_status) ? "successfully" : "with error");
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | test_status = 1;
|
---|
146 | if (NULL != debug)
|
---|
147 | PR_fprintf(debug, "PR_WaitProcess failed\n");
|
---|
148 | }
|
---|
149 | }
|
---|
150 | PR_DELETE(child);
|
---|
151 | PR_Cleanup();
|
---|
152 | return test_status;
|
---|
153 |
|
---|
154 | } /* main */
|
---|
155 |
|
---|
156 | /* parent.c */
|
---|
157 |
|
---|