nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / tests / parent.c
bloba81d5ec668c8a03c0fd6d7402cac95a7b3558e24
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
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/
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.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
39 ** file: parent.c
40 ** description: test the process machinery
43 #include "prmem.h"
44 #include "prprf.h"
45 #include "prinit.h"
46 #include "prproces.h"
47 #include "prinrval.h"
49 typedef struct Child
51 const char *name;
52 char **argv;
53 PRProcess *process;
54 PRProcessAttr *attr;
55 } Child;
57 /* for the default test 'cvar -c 2000' */
58 static char *default_argv[] = {"cvar", "-c", "2000", NULL};
60 static void PrintUsage(void)
62 PR_fprintf(PR_GetSpecialFD(PR_StandardError),
63 "Usage: parent [-d] child [options]\n");
66 PRIntn main (PRIntn argc, char **argv)
68 PRStatus rv;
69 PRInt32 test_status = 1;
70 PRIntervalTime t_start, t_elapsed;
71 PRFileDesc *debug = NULL;
72 Child *child = PR_NEWZAP(Child);
74 if (1 == argc)
76 /* no command-line arguments: run the default test */
77 child->argv = default_argv;
79 else
81 argv += 1; /* don't care about our program name */
82 while (*argv != NULL && argv[0][0] == '-')
84 if (argv[0][1] == 'd')
85 debug = PR_GetSpecialFD(PR_StandardError);
86 else
88 PrintUsage();
89 return 2; /* not sufficient */
91 argv += 1;
93 child->argv = argv;
96 if (NULL == *child->argv)
98 PrintUsage();
99 return 2;
102 child->name = *child->argv;
103 if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name);
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));
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);
118 PR_DestroyProcessAttr(child->attr);
120 test_status = (NULL == child->process) ? 1 : 0;
121 if (NULL != debug)
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));
132 if (0 == test_status)
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)
138 if (NULL != debug)
139 PR_fprintf(
140 debug, "Child exited %s\n",
141 (0 == test_status) ? "successfully" : "with error");
143 else
145 test_status = 1;
146 if (NULL != debug)
147 PR_fprintf(debug, "PR_WaitProcess failed\n");
150 PR_DELETE(child);
151 PR_Cleanup();
152 return test_status;
154 } /* main */
156 /* parent.c */