pipepong2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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: pipepong2.c
  7. *
  8. * Description:
  9. * This test runs in conjunction with the pipeping2 test.
  10. * The pipeping2 test creates two pipes and passes two
  11. * pipe fd's to this test. Then the pipeping2 test writes
  12. * "ping" to this test and this test writes "pong" back.
  13. * To run this pair of tests, just invoke pipeping2.
  14. *
  15. * Tested areas: process creation, pipes, file descriptor
  16. * inheritance.
  17. */
  18. #include "prerror.h"
  19. #include "prio.h"
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #define NUM_ITERATIONS 10
  24. int main(int argc, char **argv)
  25. {
  26. PRFileDesc *pipe_read, *pipe_write;
  27. PRStatus status;
  28. char buf[1024];
  29. PRInt32 nBytes;
  30. int idx;
  31. pipe_read = PR_GetInheritedFD("PIPE_READ");
  32. if (pipe_read == NULL) {
  33. fprintf(stderr, "PR_GetInheritedFD failed\n");
  34. exit(1);
  35. }
  36. status = PR_SetFDInheritable(pipe_read, PR_FALSE);
  37. if (status == PR_FAILURE) {
  38. fprintf(stderr, "PR_SetFDInheritable failed\n");
  39. exit(1);
  40. }
  41. pipe_write = PR_GetInheritedFD("PIPE_WRITE");
  42. if (pipe_write == NULL) {
  43. fprintf(stderr, "PR_GetInheritedFD failed\n");
  44. exit(1);
  45. }
  46. status = PR_SetFDInheritable(pipe_write, PR_FALSE);
  47. if (status == PR_FAILURE) {
  48. fprintf(stderr, "PR_SetFDInheritable failed\n");
  49. exit(1);
  50. }
  51. for (idx = 0; idx < NUM_ITERATIONS; idx++) {
  52. memset(buf, 0, sizeof(buf));
  53. nBytes = PR_Read(pipe_read, buf, sizeof(buf));
  54. if (nBytes == -1) {
  55. fprintf(stderr, "PR_Read failed: (%d, %d)\n",
  56. PR_GetError(), PR_GetOSError());
  57. exit(1);
  58. }
  59. printf("pong process: received \"%s\"\n", buf);
  60. if (nBytes != 5) {
  61. fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
  62. nBytes);
  63. exit(1);
  64. }
  65. if (strcmp(buf, "ping") != 0) {
  66. fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
  67. buf);
  68. exit(1);
  69. }
  70. strcpy(buf, "pong");
  71. printf("pong process: sending \"%s\"\n", buf);
  72. nBytes = PR_Write(pipe_write, buf, 5);
  73. if (nBytes == -1) {
  74. fprintf(stderr, "PR_Write failed\n");
  75. exit(1);
  76. }
  77. }
  78. status = PR_Close(pipe_read);
  79. if (status == PR_FAILURE) {
  80. fprintf(stderr, "PR_Close failed\n");
  81. exit(1);
  82. }
  83. status = PR_Close(pipe_write);
  84. if (status == PR_FAILURE) {
  85. fprintf(stderr, "PR_Close failed\n");
  86. exit(1);
  87. }
  88. return 0;
  89. }