Back to index

lightning-sunbird  0.9+nobinonly
parent.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** file:        parent.c
00040 ** description: test the process machinery
00041 */
00042 
00043 #include "prmem.h"
00044 #include "prprf.h"
00045 #include "prinit.h"
00046 #include "prproces.h"
00047 #include "prinrval.h"
00048 
00049 typedef struct Child
00050 {
00051     const char *name;
00052     char **argv;
00053     PRProcess *process;
00054     PRProcessAttr *attr;
00055 } Child;
00056 
00057 /* for the default test 'cvar -c 2000' */
00058 static char *default_argv[] = {"cvar", "-c", "2000", NULL};
00059 
00060 static void PrintUsage(void)
00061 {
00062     PR_fprintf(PR_GetSpecialFD(PR_StandardError),
00063         "Usage: parent [-d] child [options]\n");
00064 }
00065 
00066 PRIntn main (PRIntn argc, char **argv)
00067 {
00068     PRStatus rv;
00069     PRInt32 test_status = 1;
00070     PRIntervalTime t_start, t_elapsed;
00071     PRFileDesc *debug = NULL;
00072     Child *child = PR_NEWZAP(Child);
00073 
00074     if (1 == argc)
00075     {
00076         /* no command-line arguments: run the default test */
00077         child->argv = default_argv;
00078     }
00079     else
00080     {
00081         argv += 1;  /* don't care about our program name */
00082         while (*argv != NULL && argv[0][0] == '-')
00083         {
00084             if (argv[0][1] == 'd')
00085                 debug = PR_GetSpecialFD(PR_StandardError);
00086             else
00087             {
00088                 PrintUsage();
00089                 return 2;  /* not sufficient */
00090             }
00091             argv += 1;
00092         }
00093         child->argv = argv;
00094     }
00095 
00096     if (NULL == *child->argv)
00097     {
00098         PrintUsage();
00099         return 2;
00100     }
00101 
00102     child->name = *child->argv;
00103     if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name);
00104 
00105     child->attr = PR_NewProcessAttr();
00106     PR_ProcessAttrSetStdioRedirect(
00107         child->attr, PR_StandardOutput,
00108         PR_GetSpecialFD(PR_StandardOutput));
00109     PR_ProcessAttrSetStdioRedirect(
00110         child->attr, PR_StandardError,
00111         PR_GetSpecialFD(PR_StandardError));
00112 
00113     t_start = PR_IntervalNow();
00114     child->process = PR_CreateProcess(
00115         child->name, child->argv, NULL, child->attr);
00116     t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start);
00117 
00118     PR_DestroyProcessAttr(child->attr);
00119 
00120     test_status = (NULL == child->process) ? 1 : 0;
00121     if (NULL != debug)
00122     {
00123         PR_fprintf(
00124             debug, "Child was %sforked\n",
00125             (0 == test_status) ? "" : "NOT ");
00126         if (0 == test_status)
00127             PR_fprintf(
00128                 debug, "PR_CreateProcess took %lu microseconds\n",
00129                 PR_IntervalToMicroseconds(t_elapsed));
00130     }
00131 
00132     if (0 == test_status)
00133     {
00134         if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n");
00135         rv = PR_WaitProcess(child->process, &test_status);
00136         if (PR_SUCCESS == rv)
00137         {
00138             if (NULL != debug)
00139                 PR_fprintf(
00140                     debug, "Child exited %s\n",
00141                     (0 == test_status) ? "successfully" : "with error");
00142         }
00143         else
00144         {
00145             test_status = 1;
00146             if (NULL != debug)
00147                 PR_fprintf(debug, "PR_WaitProcess failed\n");
00148         }
00149     }
00150     PR_DELETE(child);
00151     PR_Cleanup();
00152     return test_status;
00153     
00154 }  /* main */
00155 
00156 /* parent.c */
00157