Debian Codegraph @

nsprpub/pr/tests/sigpipe.c

raw
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)
17
18 int main(void) {
19 /* This test applies to Unix and OS/2. */
20 return 0;
21 }
22
23 #else /* XP_UNIX && OS/2 */
24
25 # include "nspr.h"
26
27 # include <stdio.h>
28 # include <unistd.h>
29 # include <errno.h>
30
31 static void Test(void* arg) {
32 int pipefd[2];
33 int rv;
34 char c = '\0';
35
36 if (pipe(pipefd) == -1) {
37 fprintf(stderr, "cannot create pipe: %d\n", errno);
38 exit(1);
39 }
40 close(pipefd[0]);
41
42 rv = write(pipefd[1], &c, 1);
43 if (rv != -1) {
44 fprintf(
45 stderr,
46 "write to broken pipe should have failed with EPIPE but returned %d\n",
47 rv);
48 exit(1);
49 }
50 if (errno != EPIPE) {
51 fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n",
52 errno);
53 exit(1);
54 }
55 close(pipefd[1]);
56 printf("write to broken pipe failed with EPIPE, as expected\n");
57 }
58
59 int main(int argc, char** argv) {
60 PRThread* thread;
61
62 /* This initializes NSPR. */
63 PR_SetError(0, 0);
64
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
77 printf("PASSED\n");
78 return 0;
79 }
80
81 #endif /* XP_UNIX */