/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- dup2_fd2
- dup2_self
- test_dup2
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Invalid calls to dup2
32 */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <limits.h>
41 #include <errno.h>
42 #include <err.h>
43
44 #include "config.h"
45 #include "test.h"
46
47 static
48 void
49 dup2_fd2(int fd, const char *desc)
50 {
51 int rv;
52
53 rv = dup2(STDIN_FILENO, fd);
54 report_test(rv, errno, EBADF, desc);
55
56 if (rv != -1) {
57 close(fd); /* just in case */
58 }
59 }
60
61 static
62 void
63 dup2_self(void)
64 {
65 struct stat sb;
66 int rv;
67 int testfd;
68
69 /* use fd that isn't in use */
70 testfd = CLOSED_FD;
71
72 rv = dup2(STDIN_FILENO, testfd);
73 if (rv == -1) {
74 warn("UH-OH: couldn't copy stdin");
75 return;
76 }
77
78 rv = dup2(testfd, testfd);
79 if (rv == testfd) {
80 warnx("passed: dup2 to same fd");
81 }
82 else if (rv<0) {
83 warn("FAILURE: dup2 to same fd: error");
84 }
85 else {
86 warnx("FAILURE: dup2 to same fd: returned %d instead", rv);
87 }
88
89 rv = fstat(testfd, &sb);
90 if (rv==0) {
91 warnx("passed: fstat fd after dup2 to itself");
92 }
93 else if (errno!=EUNIMP && errno!=ENOSYS) {
94 warn("FAILURE: fstat fd after dup2 to itself");
95 }
96 else {
97 /* no support for fstat; try lseek */
98 rv = lseek(testfd, 0, SEEK_CUR);
99 if (rv==0 || (rv==-1 && errno==ESPIPE)) {
100 warnx("passed: lseek fd after dup2 to itself");
101 }
102 else {
103 warn("FAILURE: lseek fd after dup2 to itself");
104 }
105 }
106
107 close(testfd);
108 }
109
110 void
111 test_dup2(void)
112 {
113 /* This does the first fd. */
114 test_dup2_fd();
115
116 /* Any interesting cases added here should also go in common_fds.c */
117 dup2_fd2(-1, "dup2 to -1");
118 dup2_fd2(-5, "dup2 to -5");
119 dup2_fd2(IMPOSSIBLE_FD, "dup2 to impossible fd");
120 #ifdef OPEN_MAX
121 dup2_fd2(OPEN_MAX, "dup2 to OPEN_MAX");
122 #else
123 warnx("Warning: OPEN_MAX not defined - test skipped");
124 #endif
125
126 dup2_self();
127 }