1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/parent.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** file: parent.c 1.11 +** description: test the process machinery 1.12 +*/ 1.13 + 1.14 +#include "prmem.h" 1.15 +#include "prprf.h" 1.16 +#include "prinit.h" 1.17 +#include "prproces.h" 1.18 +#include "prinrval.h" 1.19 + 1.20 +typedef struct Child 1.21 +{ 1.22 + const char *name; 1.23 + char **argv; 1.24 + PRProcess *process; 1.25 + PRProcessAttr *attr; 1.26 +} Child; 1.27 + 1.28 +/* for the default test 'cvar -c 2000' */ 1.29 +static char *default_argv[] = {"cvar", "-c", "2000", NULL}; 1.30 + 1.31 +static void PrintUsage(void) 1.32 +{ 1.33 + PR_fprintf(PR_GetSpecialFD(PR_StandardError), 1.34 + "Usage: parent [-d] child [options]\n"); 1.35 +} 1.36 + 1.37 +int main(int argc, char **argv) 1.38 +{ 1.39 + PRStatus rv; 1.40 + PRInt32 test_status = 1; 1.41 + PRIntervalTime t_start, t_elapsed; 1.42 + PRFileDesc *debug = NULL; 1.43 + Child *child = PR_NEWZAP(Child); 1.44 + 1.45 + if (1 == argc) 1.46 + { 1.47 + /* no command-line arguments: run the default test */ 1.48 + child->argv = default_argv; 1.49 + } 1.50 + else 1.51 + { 1.52 + argv += 1; /* don't care about our program name */ 1.53 + while (*argv != NULL && argv[0][0] == '-') 1.54 + { 1.55 + if (argv[0][1] == 'd') 1.56 + debug = PR_GetSpecialFD(PR_StandardError); 1.57 + else 1.58 + { 1.59 + PrintUsage(); 1.60 + return 2; /* not sufficient */ 1.61 + } 1.62 + argv += 1; 1.63 + } 1.64 + child->argv = argv; 1.65 + } 1.66 + 1.67 + if (NULL == *child->argv) 1.68 + { 1.69 + PrintUsage(); 1.70 + return 2; 1.71 + } 1.72 + 1.73 + child->name = *child->argv; 1.74 + if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name); 1.75 + 1.76 + child->attr = PR_NewProcessAttr(); 1.77 + PR_ProcessAttrSetStdioRedirect( 1.78 + child->attr, PR_StandardOutput, 1.79 + PR_GetSpecialFD(PR_StandardOutput)); 1.80 + PR_ProcessAttrSetStdioRedirect( 1.81 + child->attr, PR_StandardError, 1.82 + PR_GetSpecialFD(PR_StandardError)); 1.83 + 1.84 + t_start = PR_IntervalNow(); 1.85 + child->process = PR_CreateProcess( 1.86 + child->name, child->argv, NULL, child->attr); 1.87 + t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start); 1.88 + 1.89 + PR_DestroyProcessAttr(child->attr); 1.90 + 1.91 + test_status = (NULL == child->process) ? 1 : 0; 1.92 + if (NULL != debug) 1.93 + { 1.94 + PR_fprintf( 1.95 + debug, "Child was %sforked\n", 1.96 + (0 == test_status) ? "" : "NOT "); 1.97 + if (0 == test_status) 1.98 + PR_fprintf( 1.99 + debug, "PR_CreateProcess took %lu microseconds\n", 1.100 + PR_IntervalToMicroseconds(t_elapsed)); 1.101 + } 1.102 + 1.103 + if (0 == test_status) 1.104 + { 1.105 + if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n"); 1.106 + rv = PR_WaitProcess(child->process, &test_status); 1.107 + if (PR_SUCCESS == rv) 1.108 + { 1.109 + if (NULL != debug) 1.110 + PR_fprintf( 1.111 + debug, "Child exited %s\n", 1.112 + (0 == test_status) ? "successfully" : "with error"); 1.113 + } 1.114 + else 1.115 + { 1.116 + test_status = 1; 1.117 + if (NULL != debug) 1.118 + PR_fprintf(debug, "PR_WaitProcess failed\n"); 1.119 + } 1.120 + } 1.121 + PR_DELETE(child); 1.122 + PR_Cleanup(); 1.123 + return test_status; 1.124 + 1.125 +} /* main */ 1.126 + 1.127 +/* parent.c */ 1.128 +