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/. */
8 ** description: test the process machinery
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
)
37 PRInt32 test_status
= 1;
38 PRIntervalTime t_start
, t_elapsed
;
39 PRFileDesc
*debug
= NULL
;
40 Child
*child
= PR_NEWZAP(Child
);
44 /* no command-line arguments: run the default test */
45 child
->argv
= default_argv
;
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
);
57 return 2; /* not sufficient */
64 if (NULL
== *child
->argv
)
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;
92 debug
, "Child was %sforked\n",
93 (0 == test_status
) ? "" : "NOT ");
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
)
108 debug
, "Child exited %s\n",
109 (0 == test_status
) ? "successfully" : "with error");
115 PR_fprintf(debug
, "PR_WaitProcess failed\n");