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 #ifdef SYMBIAN
64     /* Have mercy on the unknown 142 errno, it seems ok */
65     if (errno != EPIPE && errno != 142) {
66 #else
67     if (errno != EPIPE) {
68 #endif
69         fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n", errno);
70         exit(1);
71     }
72     close(pipefd[1]);
73     printf("write to broken pipe failed with EPIPE, as expected\n");
74 }
75 
76 int main(int argc, char **argv)
77 {
78     PRThread *thread;
79 
80     /* This initializes NSPR. */
81     PR_SetError(0, 0);
82 
83     thread = PR_CreateThread(PR_USER_THREAD, Test, NULL, PR_PRIORITY_NORMAL,
84             PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
85     if (thread == NULL) {
86         fprintf(stderr, "PR_CreateThread failed\n");
87         exit(1);
88     }
89     if (PR_JoinThread(thread) == PR_FAILURE) {
90         fprintf(stderr, "PR_JoinThread failed\n");
91         exit(1);
92     }
93     Test(NULL);
94 
95     printf("PASSED\n");
96     return 0;
97 }
98 
99 #endif /* XP_UNIX */
100