1#   Copyright 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
2#   Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# This file was written by Corinna Vinschen <vinschen@redhat.com>
18
19if [target_info exists gdb,nofileio] {
20    verbose "Skipping fileio.exp because of no fileio capabilities."
21    continue
22}
23
24if $tracelevel then {
25        strace $tracelevel
26}
27
28
29set testfile "fileio"
30set srcfile ${testfile}.c
31set binfile ${objdir}/${subdir}/${testfile}
32
33if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34    untested fileio.exp
35    return -1
36}
37
38# Create and source the file that provides information about the compiler
39# used to compile the test case.
40
41if [get_compiler_info ${binfile}] {
42    return -1;
43}
44
45remote_exec build {sh -xc test\ -r\ dir2.fileio.test\ &&\ chmod\ -f\ +w\ dir2.fileio.test}
46remote_exec build {sh -xc rm\ -rf\ *.fileio.test}
47
48set oldtimeout $timeout
49set timeout [expr "$timeout + 60"]
50
51# Start with a fresh gdb.
52
53gdb_exit
54gdb_start
55gdb_reinitialize_dir $srcdir/$subdir
56gdb_load ${binfile}
57gdb_test_no_output "set print sevenbit-strings"
58gdb_test_no_output "set print address off"
59gdb_test_no_output "set width 0"
60
61if ![runto_main] then {
62    perror "couldn't run to breakpoint"
63    continue
64}
65
66gdb_test "break stop" "Breakpoint .*$srcfile.*"
67
68set stop_msg ".*Breakpoint .* stop \\(\\) at.*$srcfile:.*static void stop \\(\\) {}.*"
69
70gdb_test continue \
71"Continuing\\..*open 1:.*OK$stop_msg" \
72"Open a file"
73
74gdb_test continue \
75"Continuing\\..*open 2:.*EEXIST$stop_msg" \
76"Creating already existing file returns EEXIST"
77
78gdb_test continue \
79"Continuing\\..*open 3:.*EISDIR$stop_msg" \
80"Open directory for writing returns EISDIR"
81
82gdb_test continue \
83"Continuing\\..*open 4:.*ENOENT$stop_msg" \
84"Opening nonexistant file returns ENOENT"
85
86gdb_test "continue" ".*" ""
87
88catch "system \"chmod -f -w nowrt.fileio.test\""
89
90gdb_test continue \
91"Continuing\\..*open 5:.*EACCES$stop_msg" \
92"Open for write but no write permission returns EACCES"
93
94gdb_test continue \
95"Continuing\\..*write 1:.*OK$stop_msg" \
96"Writing to a file"
97
98gdb_test continue \
99"Continuing\\..*write 2:.*EBADF$stop_msg" \
100"Write using invalid file descriptor returns EBADF"
101
102gdb_test continue \
103"Continuing\\..*write 3:.*EBADF$stop_msg" \
104"Writing to a read-only file returns EBADF"
105
106gdb_test continue \
107"Continuing\\..*read 1:.*OK$stop_msg" \
108"Reading from a file"
109
110gdb_test continue \
111"Continuing\\..*read 2:.*EBADF$stop_msg" \
112"Read using invalid file descriptor returns EBADF"
113
114gdb_test continue \
115"Continuing\\..*lseek 1:.*OK$stop_msg" \
116"Lseeking CUR a file"
117
118gdb_test continue \
119"Continuing\\..*lseek 2:.*OK$stop_msg" \
120"Lseeking END a file"
121
122gdb_test continue \
123"Continuing\\..*lseek 3:.*OK$stop_msg" \
124"Lseeking SET a file"
125
126
127gdb_test continue \
128"Continuing\\..*close 1:.*OK$stop_msg" \
129"Closing a file"
130
131gdb_test continue \
132"Continuing\\..*close 2:.*EBADF$stop_msg" \
133"Closing an invalid file descriptor returns EBADF"
134
135gdb_test continue \
136"Continuing\\..*stat 1:.*OK$stop_msg" \
137"Stat a file"
138
139gdb_test continue \
140	"Continuing\\..*stat 2:.*(ENOENT|EFAULT)$stop_msg" \
141"Stat a NULL pathname returns ENOENT or EFAULT"
142
143gdb_test continue \
144"Continuing\\..*stat 3:.*ENOENT$stop_msg" \
145"Stat an empty pathname returns ENOENT"
146
147gdb_test continue \
148"Continuing\\..*stat 4:.*ENOENT$stop_msg" \
149"Stat a nonexistant file returns ENOENT"
150
151gdb_test continue \
152"Continuing\\..*fstat 1:.*OK$stop_msg" \
153"Fstat an open file"
154
155gdb_test continue \
156"Continuing\\..*fstat 2:.*EBADF$stop_msg" \
157"Fstat an invalid file descriptor returns EBADF"
158
159gdb_test continue \
160"Continuing\\..*isatty 1:.*OK$stop_msg" \
161"Isatty (stdin)"
162
163gdb_test continue \
164"Continuing\\..*isatty 2:.*OK$stop_msg" \
165"Isatty (stdout)"
166
167gdb_test continue \
168"Continuing\\..*isatty 3:.*OK$stop_msg" \
169"Isatty (stderr)"
170
171gdb_test continue \
172"Continuing\\..*isatty 4:.*OK$stop_msg" \
173"Isatty (invalid fd)"
174
175gdb_test continue \
176"Continuing\\..*isatty 5:.*OK$stop_msg" \
177"Isatty (open file)"
178
179gdb_test continue \
180"Continuing\\..*system 1:.*OK$stop_msg" \
181"System says shell is available"
182
183gdb_test_no_output "set remote system-call-allowed 1"
184
185gdb_test continue \
186"Continuing\\..*system 2:.*OK$stop_msg" \
187"System(3) call"
188
189# Is this ok?  POSIX says system returns a waitpid status?
190gdb_test continue \
191"Continuing\\..*system 3:.*OK$stop_msg" \
192"System with invalid command returns 127"
193
194gdb_test continue \
195"Continuing\\..*rename 1:.*OK$stop_msg" \
196"Rename a file"
197
198gdb_test continue \
199"Continuing\\..*rename 2:.*EISDIR$stop_msg" \
200"Renaming a file to existing directory returns EISDIR"
201
202set test "Renaming a directory to a non-empty directory returns ENOTEMPTY or EEXIST"
203gdb_test_multiple continue "${test}" {
204    -re "Continuing\\..*rename 3:.*(ENOTEMPTY|EEXIST)$stop_msg$gdb_prompt $" {
205	pass "${test}"
206    }
207    -re "Continuing\\..*rename 3:.*EBUSY$stop_msg$gdb_prompt $" {
208	# At least version <= 2.6/2004-01-08 of the Linux Kernel gets
209	# this wrong (reporting EBUSY) when the file system is NFS
210	# mounted.
211	setup_xfail *-*-*linux* gdb/1502
212	fail "${test}"
213    }
214}
215
216gdb_test continue \
217"Continuing\\..*rename 4:.*EINVAL$stop_msg" \
218"Renaming a directory to a subdir of itself returns EINVAL"
219
220gdb_test continue \
221"Continuing\\..*rename 5:.*ENOENT$stop_msg" \
222"Renaming a nonexistant file returns ENOENT"
223
224gdb_test continue \
225"Continuing\\..*unlink 1:.*OK$stop_msg" \
226"Unlink a file"
227
228# This test fails on Cygwin because unlink() succeeds on Win32 systems
229# in that situation.
230if [ishost *cygwin*] {
231    setup_xfail "*-*-*"
232}
233gdb_test continue \
234"Continuing\\..*unlink 2:.*EACCES$stop_msg" \
235"Unlinking a file in a directory w/o write access returns EACCES"
236
237gdb_test continue \
238"Continuing\\..*unlink 3:.*ENOENT$stop_msg" \
239"Unlinking a nonexistant file returns ENOENT"
240
241gdb_test continue \
242"Continuing\\..*time 1:.*OK$stop_msg" \
243"Time(2) call returns the same value as in parameter"
244
245sleep 2
246gdb_test continue \
247"Continuing\\..*time 2:.*OK$stop_msg" \
248"Time(2) returns feasible values"
249
250send_gdb "quit\n"
251send_gdb "y\n"
252
253remote_exec build {sh -xc test\ -r\ dir2.fileio.test\ &&\ chmod\ -f\ +w\ dir2.fileio.test}
254remote_exec build {sh -xc rm\ -rf\ *.fileio.test}
255
256set timeout $oldtimeout
257return 0
258