sigpipe.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. *************************************************************************
  7. *
  8. * Test: sigpipe.c
  9. *
  10. * Test the SIGPIPE handler in NSPR. This test applies to Unix only.
  11. *
  12. *************************************************************************
  13. */
  14. #if !defined(XP_UNIX) && !defined(XP_OS2)
  15. int main(void)
  16. {
  17. /* This test applies to Unix and OS/2. */
  18. return 0;
  19. }
  20. #else /* XP_UNIX && OS/2 */
  21. #include "nspr.h"
  22. #ifdef XP_OS2
  23. #define INCL_DOSQUEUES
  24. #define INCL_DOSERRORS
  25. #include <os2.h>
  26. #endif
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. static void Test(void *arg)
  31. {
  32. #ifdef XP_OS2
  33. HFILE pipefd[2];
  34. #else
  35. int pipefd[2];
  36. #endif
  37. int rv;
  38. char c = '\0';
  39. #ifdef XP_OS2
  40. if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) {
  41. #else
  42. if (pipe(pipefd) == -1) {
  43. #endif
  44. fprintf(stderr, "cannot create pipe: %d\n", errno);
  45. exit(1);
  46. }
  47. close(pipefd[0]);
  48. rv = write(pipefd[1], &c, 1);
  49. if (rv != -1) {
  50. fprintf(stderr, "write to broken pipe should have failed with EPIPE but returned %d\n", rv);
  51. exit(1);
  52. }
  53. if (errno != EPIPE) {
  54. fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n", errno);
  55. exit(1);
  56. }
  57. close(pipefd[1]);
  58. printf("write to broken pipe failed with EPIPE, as expected\n");
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. PRThread *thread;
  63. /* This initializes NSPR. */
  64. PR_SetError(0, 0);
  65. thread = PR_CreateThread(PR_USER_THREAD, Test, NULL, PR_PRIORITY_NORMAL,
  66. PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  67. if (thread == NULL) {
  68. fprintf(stderr, "PR_CreateThread failed\n");
  69. exit(1);
  70. }
  71. if (PR_JoinThread(thread) == PR_FAILURE) {
  72. fprintf(stderr, "PR_JoinThread failed\n");
  73. exit(1);
  74. }
  75. Test(NULL);
  76. printf("PASSED\n");
  77. return 0;
  78. }
  79. #endif /* XP_UNIX */