os161-1.99
 All Data Structures
bad_dup2.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Invalid calls to dup2
00032  */
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <limits.h>
00041 #include <errno.h>
00042 #include <err.h>
00043 
00044 #include "config.h"
00045 #include "test.h"
00046 
00047 static
00048 void
00049 dup2_fd2(int fd, const char *desc)
00050 {
00051         int rv;
00052 
00053         rv = dup2(STDIN_FILENO, fd);
00054         report_test(rv, errno, EBADF, desc);
00055 
00056         if (rv != -1) {
00057                 close(fd);      /* just in case */
00058         }
00059 }
00060 
00061 static
00062 void
00063 dup2_self(void)
00064 {
00065         struct stat sb;
00066         int rv;
00067         int testfd;
00068 
00069         /* use fd that isn't in use */
00070         testfd = CLOSED_FD;
00071 
00072         rv = dup2(STDIN_FILENO, testfd);
00073         if (rv == -1) {
00074                 warn("UH-OH: couldn't copy stdin");
00075                 return;
00076         }
00077 
00078         rv = dup2(testfd, testfd);
00079         if (rv == testfd) {
00080                 warnx("passed: dup2 to same fd");
00081         }
00082         else if (rv<0) {
00083                 warn("FAILURE: dup2 to same fd: error");
00084         }
00085         else {
00086                 warnx("FAILURE: dup2 to same fd: returned %d instead", rv);
00087         }
00088 
00089         rv = fstat(testfd, &sb);
00090         if (rv==0) {
00091                 warnx("passed: fstat fd after dup2 to itself");
00092         }
00093         else if (errno!=EUNIMP && errno!=ENOSYS) {
00094                 warn("FAILURE: fstat fd after dup2 to itself");
00095         }
00096         else {
00097                 /* no support for fstat; try lseek */
00098                 rv = lseek(testfd, 0, SEEK_CUR);
00099                 if (rv==0 || (rv==-1 && errno==ESPIPE)) {
00100                         warnx("passed: lseek fd after dup2 to itself");
00101                 }
00102                 else {
00103                         warn("FAILURE: lseek fd after dup2 to itself");
00104                 }
00105         }
00106 
00107         close(testfd);
00108 }
00109 
00110 void
00111 test_dup2(void)
00112 {
00113         /* This does the first fd. */
00114         test_dup2_fd();
00115 
00116         /* Any interesting cases added here should also go in common_fds.c */
00117         dup2_fd2(-1, "dup2 to -1");
00118         dup2_fd2(-5, "dup2 to -5");
00119         dup2_fd2(IMPOSSIBLE_FD, "dup2 to impossible fd");
00120 #ifdef OPEN_MAX
00121         dup2_fd2(OPEN_MAX, "dup2 to OPEN_MAX");
00122 #else
00123         warnx("Warning: OPEN_MAX not defined - test skipped");
00124 #endif
00125 
00126         dup2_self();
00127 }
 All Data Structures