nsprpub/pr/tests/parent.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 /*
     7 ** file:        parent.c
     8 ** description: test the process machinery
     9 */
    11 #include "prmem.h"
    12 #include "prprf.h"
    13 #include "prinit.h"
    14 #include "prproces.h"
    15 #include "prinrval.h"
    17 typedef struct Child
    18 {
    19     const char *name;
    20     char **argv;
    21     PRProcess *process;
    22     PRProcessAttr *attr;
    23 } Child;
    25 /* for the default test 'cvar -c 2000' */
    26 static char *default_argv[] = {"cvar", "-c", "2000", NULL};
    28 static void PrintUsage(void)
    29 {
    30     PR_fprintf(PR_GetSpecialFD(PR_StandardError),
    31         "Usage: parent [-d] child [options]\n");
    32 }
    34 int main(int argc, char **argv)
    35 {
    36     PRStatus rv;
    37     PRInt32 test_status = 1;
    38     PRIntervalTime t_start, t_elapsed;
    39     PRFileDesc *debug = NULL;
    40     Child *child = PR_NEWZAP(Child);
    42     if (1 == argc)
    43     {
    44         /* no command-line arguments: run the default test */
    45         child->argv = default_argv;
    46     }
    47     else
    48     {
    49         argv += 1;  /* don't care about our program name */
    50         while (*argv != NULL && argv[0][0] == '-')
    51         {
    52             if (argv[0][1] == 'd')
    53                 debug = PR_GetSpecialFD(PR_StandardError);
    54             else
    55             {
    56                 PrintUsage();
    57                 return 2;  /* not sufficient */
    58             }
    59             argv += 1;
    60         }
    61         child->argv = argv;
    62     }
    64     if (NULL == *child->argv)
    65     {
    66         PrintUsage();
    67         return 2;
    68     }
    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;
    89     if (NULL != debug)
    90     {
    91         PR_fprintf(
    92             debug, "Child was %sforked\n",
    93             (0 == test_status) ? "" : "NOT ");
    94         if (0 == test_status)
    95             PR_fprintf(
    96                 debug, "PR_CreateProcess took %lu microseconds\n",
    97                 PR_IntervalToMicroseconds(t_elapsed));
    98     }
   100     if (0 == test_status)
   101     {
   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)
   105         {
   106             if (NULL != debug)
   107                 PR_fprintf(
   108                     debug, "Child exited %s\n",
   109                     (0 == test_status) ? "successfully" : "with error");
   110         }
   111         else
   112         {
   113             test_status = 1;
   114             if (NULL != debug)
   115                 PR_fprintf(debug, "PR_WaitProcess failed\n");
   116         }
   117     }
   118     PR_DELETE(child);
   119     PR_Cleanup();
   120     return test_status;
   122 }  /* main */
   124 /* parent.c */

mercurial