michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ************************************************************************* michael@0: * michael@0: * Test: sigpipe.c michael@0: * michael@0: * Test the SIGPIPE handler in NSPR. This test applies to Unix only. michael@0: * michael@0: ************************************************************************* michael@0: */ michael@0: michael@0: #if !defined(XP_UNIX) && !defined(XP_OS2) michael@0: michael@0: int main(void) michael@0: { michael@0: /* This test applies to Unix and OS/2. */ michael@0: return 0; michael@0: } michael@0: michael@0: #else /* XP_UNIX && OS/2 */ michael@0: michael@0: #include "nspr.h" michael@0: michael@0: #ifdef XP_OS2 michael@0: #define INCL_DOSQUEUES michael@0: #define INCL_DOSERRORS michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static void Test(void *arg) michael@0: { michael@0: #ifdef XP_OS2 michael@0: HFILE pipefd[2]; michael@0: #else michael@0: int pipefd[2]; michael@0: #endif michael@0: int rv; michael@0: char c = '\0'; michael@0: michael@0: #ifdef XP_OS2 michael@0: if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) { michael@0: #else michael@0: if (pipe(pipefd) == -1) { michael@0: #endif michael@0: fprintf(stderr, "cannot create pipe: %d\n", errno); michael@0: exit(1); michael@0: } michael@0: close(pipefd[0]); michael@0: michael@0: rv = write(pipefd[1], &c, 1); michael@0: if (rv != -1) { michael@0: fprintf(stderr, "write to broken pipe should have failed with EPIPE but returned %d\n", rv); michael@0: exit(1); michael@0: } michael@0: #ifdef SYMBIAN michael@0: /* Have mercy on the unknown 142 errno, it seems ok */ michael@0: if (errno != EPIPE && errno != 142) { michael@0: #else michael@0: if (errno != EPIPE) { michael@0: #endif michael@0: fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n", errno); michael@0: exit(1); michael@0: } michael@0: close(pipefd[1]); michael@0: printf("write to broken pipe failed with EPIPE, as expected\n"); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRThread *thread; michael@0: michael@0: /* This initializes NSPR. */ michael@0: PR_SetError(0, 0); michael@0: michael@0: thread = PR_CreateThread(PR_USER_THREAD, Test, NULL, PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (thread == NULL) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(thread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: Test(NULL); michael@0: michael@0: printf("PASSED\n"); michael@0: return 0; michael@0: } michael@0: michael@0: #endif /* XP_UNIX */