Back to index

lightning-sunbird  0.9+nobinonly
sigpipe.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  *************************************************************************
00040  *
00041  * Test: sigpipe.c
00042  *
00043  *     Test the SIGPIPE handler in NSPR.  This test applies to Unix only.
00044  *
00045  *************************************************************************
00046  */
00047 
00048 #if !defined(XP_UNIX) && !defined(XP_OS2)
00049 
00050 int main(void)
00051 {
00052     /* This test applies to Unix and OS/2 (emx build). */
00053     return 0;
00054 }
00055 
00056 #else /* XP_UNIX && OS/2 */
00057 
00058 #include "nspr.h"
00059 
00060 #ifdef XP_OS2
00061 #define INCL_DOSQUEUES
00062 #define INCL_DOSERRORS
00063 #include <os2.h>
00064 #endif
00065 
00066 #include <stdio.h>
00067 #ifdef XP_OS2_VACPP
00068 #define EPIPE EBADF  /* IBM's write() doesn't return EPIPE */
00069 #include <io.h>
00070 #else
00071 #include <unistd.h>
00072 #endif
00073 #include <errno.h>
00074 
00075 static void Test(void *arg)
00076 {
00077 #ifdef XP_OS2
00078     HFILE pipefd[2];
00079 #else
00080     int pipefd[2];
00081 #endif
00082     int rv;
00083     char c = '\0';
00084 
00085 #ifdef XP_OS2
00086     if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) {
00087 #else
00088     if (pipe(pipefd) == -1) {
00089 #endif
00090         fprintf(stderr, "cannot create pipe: %d\n", errno);
00091         exit(1);
00092     }
00093     close(pipefd[0]);
00094 
00095     rv = write(pipefd[1], &c, 1);
00096     if (rv != -1) {
00097         fprintf(stderr, "write to broken pipe should have failed with EPIPE but returned %d\n", rv);
00098         exit(1);
00099     }
00100     if (errno != EPIPE) {
00101         fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n", errno);
00102         exit(1);
00103     }
00104     close(pipefd[1]);
00105     printf("write to broken pipe failed with EPIPE, as expected\n");
00106 }
00107 
00108 int main(void)
00109 {
00110     PRThread *thread;
00111 
00112     /* This initializes NSPR. */
00113     PR_SetError(0, 0);
00114 
00115     thread = PR_CreateThread(PR_USER_THREAD, Test, NULL, PR_PRIORITY_NORMAL,
00116             PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00117     if (thread == NULL) {
00118         fprintf(stderr, "PR_CreateThread failed\n");
00119         exit(1);
00120     }
00121     if (PR_JoinThread(thread) == PR_FAILURE) {
00122         fprintf(stderr, "PR_JoinThread failed\n");
00123         exit(1);
00124     }
00125     Test(NULL);
00126 
00127     printf("PASSED\n");
00128     return 0;
00129 }
00130 
00131 #endif /* XP_UNIX */