parent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/. */
  5. /*
  6. ** file: parent.c
  7. ** description: test the process machinery
  8. */
  9. #include "prmem.h"
  10. #include "prprf.h"
  11. #include "prinit.h"
  12. #include "prproces.h"
  13. #include "prinrval.h"
  14. typedef struct Child
  15. {
  16. const char *name;
  17. char **argv;
  18. PRProcess *process;
  19. PRProcessAttr *attr;
  20. } Child;
  21. /* for the default test 'cvar -c 2000' */
  22. static char *default_argv[] = {"cvar", "-c", "2000", NULL};
  23. static void PrintUsage(void)
  24. {
  25. PR_fprintf(PR_GetSpecialFD(PR_StandardError),
  26. "Usage: parent [-d] child [options]\n");
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. PRStatus rv;
  31. PRInt32 test_status = 1;
  32. PRIntervalTime t_start, t_elapsed;
  33. PRFileDesc *debug = NULL;
  34. Child *child = PR_NEWZAP(Child);
  35. if (1 == argc)
  36. {
  37. /* no command-line arguments: run the default test */
  38. child->argv = default_argv;
  39. }
  40. else
  41. {
  42. argv += 1; /* don't care about our program name */
  43. while (*argv != NULL && argv[0][0] == '-')
  44. {
  45. if (argv[0][1] == 'd') {
  46. debug = PR_GetSpecialFD(PR_StandardError);
  47. }
  48. else
  49. {
  50. PrintUsage();
  51. return 2; /* not sufficient */
  52. }
  53. argv += 1;
  54. }
  55. child->argv = argv;
  56. }
  57. if (NULL == *child->argv)
  58. {
  59. PrintUsage();
  60. return 2;
  61. }
  62. child->name = *child->argv;
  63. if (NULL != debug) {
  64. PR_fprintf(debug, "Forking %s\n", child->name);
  65. }
  66. child->attr = PR_NewProcessAttr();
  67. PR_ProcessAttrSetStdioRedirect(
  68. child->attr, PR_StandardOutput,
  69. PR_GetSpecialFD(PR_StandardOutput));
  70. PR_ProcessAttrSetStdioRedirect(
  71. child->attr, PR_StandardError,
  72. PR_GetSpecialFD(PR_StandardError));
  73. t_start = PR_IntervalNow();
  74. child->process = PR_CreateProcess(
  75. child->name, child->argv, NULL, child->attr);
  76. t_elapsed = (PRIntervalTime) (PR_IntervalNow() - t_start);
  77. PR_DestroyProcessAttr(child->attr);
  78. test_status = (NULL == child->process) ? 1 : 0;
  79. if (NULL != debug)
  80. {
  81. PR_fprintf(
  82. debug, "Child was %sforked\n",
  83. (0 == test_status) ? "" : "NOT ");
  84. if (0 == test_status)
  85. PR_fprintf(
  86. debug, "PR_CreateProcess took %lu microseconds\n",
  87. PR_IntervalToMicroseconds(t_elapsed));
  88. }
  89. if (0 == test_status)
  90. {
  91. if (NULL != debug) {
  92. PR_fprintf(debug, "Waiting for child to exit\n");
  93. }
  94. rv = PR_WaitProcess(child->process, &test_status);
  95. if (PR_SUCCESS == rv)
  96. {
  97. if (NULL != debug)
  98. PR_fprintf(
  99. debug, "Child exited %s\n",
  100. (0 == test_status) ? "successfully" : "with error");
  101. }
  102. else
  103. {
  104. test_status = 1;
  105. if (NULL != debug) {
  106. PR_fprintf(debug, "PR_WaitProcess failed\n");
  107. }
  108. }
  109. }
  110. PR_DELETE(child);
  111. PR_Cleanup();
  112. return test_status;
  113. } /* main */
  114. /* parent.c */