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