]> git.ipfire.org Git - thirdparty/glibc.git/blob - io/tst-fcntl.c
8814b17ad3341ece4bf8c58642411a9dddb5b622
[thirdparty/glibc.git] / io / tst-fcntl.c
1 /* Tests for fcntl.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <paths.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26
27
28 /* Prototype for our test function. */
29 extern void do_prepare (int argc, char *argv[]);
30 extern int do_test (int argc, char *argv[]);
31
32 /* We have a preparation function. */
33 #define PREPARE do_prepare
34
35 #include "../test-skeleton.c"
36
37
38 /* Name of the temporary files. */
39 static char *name;
40
41 /* File descriptor to temporary file. */
42 static int fd;
43
44 void
45 do_prepare (int argc, char *argv[])
46 {
47 size_t name_len;
48
49 name_len = strlen (test_dir);
50 name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
51 mempcpy (mempcpy (name, test_dir, name_len),
52 "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
53 /* Create the temporary file. */
54 fd = mkstemp (name);
55 if (fd == -1)
56 {
57 printf ("cannot open temporary file: %m\n");
58 exit (1);
59 }
60 add_temp_file (name);
61 }
62
63
64 int
65 do_test (int argc, char *argv[])
66 {
67 int fd2;
68 int fd3;
69 struct stat64 st;
70 int val;
71 int result = 0;
72
73 if (fstat64 (fd, &st) != 0)
74 {
75 printf ("cannot stat test file: %m\n");
76 return 1;
77 }
78 if (! S_ISREG (st.st_mode) || st.st_size != 0)
79 {
80 puts ("file not created correctly");
81 return 1;
82 }
83
84 /* Get the flags with fcntl(). */
85 val = fcntl (fd, F_GETFL);
86 if (val == -1)
87 {
88 printf ("fcntl(fd, F_GETFL) failed: %m\n");
89 result = 1;
90 }
91 else if ((val & O_ACCMODE) != O_RDWR)
92 {
93 puts ("temporary file not opened for read and write");
94 result = 1;
95 }
96
97 /* Set the flags to something else. */
98 if (fcntl (fd, F_SETFL, O_RDONLY) == -1)
99 {
100 printf ("fcntl(fd, F_SETFL, O_RDONLY) failed: %m\n");
101 result = 1;
102 }
103
104 val = fcntl (fd, F_GETFL);
105 if (val == -1)
106 {
107 printf ("fcntl(fd, F_GETFL) after F_SETFL failed: %m\n");
108 result = 1;
109 }
110 else if ((val & O_ACCMODE) != O_RDWR)
111 {
112 puts ("temporary file access mode changed");
113 result = 1;
114 }
115
116 /* Set the flags to something else. */
117 if (fcntl (fd, F_SETFL, O_APPEND) == -1)
118 {
119 printf ("fcntl(fd, F_SETFL, O_APPEND) failed: %m\n");
120 result = 1;
121 }
122
123 val = fcntl (fd, F_GETFL);
124 if (val == -1)
125 {
126 printf ("fcntl(fd, F_GETFL) after second F_SETFL failed: %m\n");
127 result = 1;
128 }
129 else if ((val & O_APPEND) == 0)
130 {
131 puts ("O_APPEND not set");
132 result = 1;
133 }
134
135 val = fcntl (fd, F_GETFD);
136 if (val == -1)
137 {
138 printf ("fcntl(fd, F_GETFD) failed: %m\n");
139 result = 1;
140 }
141 else if (fcntl (fd, F_SETFD, val | FD_CLOEXEC) == -1)
142 {
143 printf ("fcntl(fd, F_SETFD, FD_CLOEXEC) failed: %m\n");
144 result = 1;
145 }
146 else
147 {
148 val = fcntl (fd, F_GETFD);
149 if (val == -1)
150 {
151 printf ("fcntl(fd, F_GETFD) after F_SETFD failed: %m\n");
152 result = 1;
153 }
154 else if ((val & FD_CLOEXEC) == 0)
155 {
156 puts ("FD_CLOEXEC not set");
157 result = 1;
158 }
159 }
160
161 /* Get a number of a free descriptor. If /dev/null is not available
162 don't continue testing. */
163 fd2 = open (_PATH_DEVNULL, O_RDWR);
164 if (fd2 == -1)
165 return result;
166 close (fd2);
167
168 fd3 = fcntl (fd, F_DUPFD, fd2 + 1);
169 if (fd3 == -1)
170 {
171 printf ("fcntl(fd, F_DUPFD, %d) failed: %m\n", fd2 + 1);
172 result = 1;
173 }
174 else if (fd3 <= fd2)
175 {
176 printf ("F_DUPFD returned %d which is not larger than %d\n", fd3, fd2);
177 result = 1;
178 }
179
180 if (fd3 != -1)
181 {
182 val = fcntl (fd3, F_GETFD);
183 if (val == -1)
184 {
185 printf ("fcntl(fd3, F_GETFD) after F_DUPFD failed: %m\n");
186 result = 1;
187 }
188 else if ((val & FD_CLOEXEC) != 0)
189 {
190 puts ("FD_CLOEXEC still set");
191 result = 1;
192 }
193
194 close (fd3);
195 }
196
197 return result;
198 }