Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | ** file: parent.c |
michael@0 | 8 | ** description: test the process machinery |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "prmem.h" |
michael@0 | 12 | #include "prprf.h" |
michael@0 | 13 | #include "prinit.h" |
michael@0 | 14 | #include "prproces.h" |
michael@0 | 15 | #include "prinrval.h" |
michael@0 | 16 | |
michael@0 | 17 | typedef struct Child |
michael@0 | 18 | { |
michael@0 | 19 | const char *name; |
michael@0 | 20 | char **argv; |
michael@0 | 21 | PRProcess *process; |
michael@0 | 22 | PRProcessAttr *attr; |
michael@0 | 23 | } Child; |
michael@0 | 24 | |
michael@0 | 25 | /* for the default test 'cvar -c 2000' */ |
michael@0 | 26 | static char *default_argv[] = {"cvar", "-c", "2000", NULL}; |
michael@0 | 27 | |
michael@0 | 28 | static void PrintUsage(void) |
michael@0 | 29 | { |
michael@0 | 30 | PR_fprintf(PR_GetSpecialFD(PR_StandardError), |
michael@0 | 31 | "Usage: parent [-d] child [options]\n"); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | int main(int argc, char **argv) |
michael@0 | 35 | { |
michael@0 | 36 | PRStatus rv; |
michael@0 | 37 | PRInt32 test_status = 1; |
michael@0 | 38 | PRIntervalTime t_start, t_elapsed; |
michael@0 | 39 | PRFileDesc *debug = NULL; |
michael@0 | 40 | Child *child = PR_NEWZAP(Child); |
michael@0 | 41 | |
michael@0 | 42 | if (1 == argc) |
michael@0 | 43 | { |
michael@0 | 44 | /* no command-line arguments: run the default test */ |
michael@0 | 45 | child->argv = default_argv; |
michael@0 | 46 | } |
michael@0 | 47 | else |
michael@0 | 48 | { |
michael@0 | 49 | argv += 1; /* don't care about our program name */ |
michael@0 | 50 | while (*argv != NULL && argv[0][0] == '-') |
michael@0 | 51 | { |
michael@0 | 52 | if (argv[0][1] == 'd') |
michael@0 | 53 | debug = PR_GetSpecialFD(PR_StandardError); |
michael@0 | 54 | else |
michael@0 | 55 | { |
michael@0 | 56 | PrintUsage(); |
michael@0 | 57 | return 2; /* not sufficient */ |
michael@0 | 58 | } |
michael@0 | 59 | argv += 1; |
michael@0 | 60 | } |
michael@0 | 61 | child->argv = argv; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (NULL == *child->argv) |
michael@0 | 65 | { |
michael@0 | 66 | PrintUsage(); |
michael@0 | 67 | return 2; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | child->name = *child->argv; |
michael@0 | 71 | if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name); |
michael@0 | 72 | |
michael@0 | 73 | child->attr = PR_NewProcessAttr(); |
michael@0 | 74 | PR_ProcessAttrSetStdioRedirect( |
michael@0 | 75 | child->attr, PR_StandardOutput, |
michael@0 | 76 | PR_GetSpecialFD(PR_StandardOutput)); |
michael@0 | 77 | PR_ProcessAttrSetStdioRedirect( |
michael@0 | 78 | child->attr, PR_StandardError, |
michael@0 | 79 | PR_GetSpecialFD(PR_StandardError)); |
michael@0 | 80 | |
michael@0 | 81 | t_start = PR_IntervalNow(); |
michael@0 | 82 | child->process = PR_CreateProcess( |
michael@0 | 83 | child->name, child->argv, NULL, child->attr); |
michael@0 | 84 | t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start); |
michael@0 | 85 | |
michael@0 | 86 | PR_DestroyProcessAttr(child->attr); |
michael@0 | 87 | |
michael@0 | 88 | test_status = (NULL == child->process) ? 1 : 0; |
michael@0 | 89 | if (NULL != debug) |
michael@0 | 90 | { |
michael@0 | 91 | PR_fprintf( |
michael@0 | 92 | debug, "Child was %sforked\n", |
michael@0 | 93 | (0 == test_status) ? "" : "NOT "); |
michael@0 | 94 | if (0 == test_status) |
michael@0 | 95 | PR_fprintf( |
michael@0 | 96 | debug, "PR_CreateProcess took %lu microseconds\n", |
michael@0 | 97 | PR_IntervalToMicroseconds(t_elapsed)); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | if (0 == test_status) |
michael@0 | 101 | { |
michael@0 | 102 | if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n"); |
michael@0 | 103 | rv = PR_WaitProcess(child->process, &test_status); |
michael@0 | 104 | if (PR_SUCCESS == rv) |
michael@0 | 105 | { |
michael@0 | 106 | if (NULL != debug) |
michael@0 | 107 | PR_fprintf( |
michael@0 | 108 | debug, "Child exited %s\n", |
michael@0 | 109 | (0 == test_status) ? "successfully" : "with error"); |
michael@0 | 110 | } |
michael@0 | 111 | else |
michael@0 | 112 | { |
michael@0 | 113 | test_status = 1; |
michael@0 | 114 | if (NULL != debug) |
michael@0 | 115 | PR_fprintf(debug, "PR_WaitProcess failed\n"); |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | PR_DELETE(child); |
michael@0 | 119 | PR_Cleanup(); |
michael@0 | 120 | return test_status; |
michael@0 | 121 | |
michael@0 | 122 | } /* main */ |
michael@0 | 123 | |
michael@0 | 124 | /* parent.c */ |
michael@0 | 125 |