Use common bits/shm.h for more architectures.
[glibc.git] / io / test-lfs.c
blob52120e9bca19e675d93d85402bafbd49fdfe4917
1 /* Some basic tests for LFS.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>, 2000.
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.
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.
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/>. */
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <error.h>
26 #include <errno.h>
27 #include <sys/resource.h>
28 #include <support/check.h>
30 /* Prototype for our test function. */
31 extern void do_prepare (int argc, char *argv[]);
32 extern int do_test (int argc, char *argv[]);
34 /* We have a preparation function. */
35 #define PREPARE do_prepare
37 /* We might need a bit longer timeout. */
38 #define TIMEOUT 20 /* sec */
40 /* This defines the `main' function and some more. */
41 #include <test-skeleton.c>
43 /* These are for the temporary file we generate. */
44 char *name;
45 int fd;
47 /* 2^31 = 2GB. */
48 #define TWO_GB 2147483648LL
50 void
51 do_prepare (int argc, char *argv[])
53 size_t name_len;
54 struct rlimit64 rlim;
56 name_len = strlen (test_dir);
57 name = xmalloc (name_len + sizeof ("/lfsXXXXXX"));
58 mempcpy (mempcpy (name, test_dir, name_len),
59 "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
61 /* Open our test file. */
62 fd = mkstemp64 (name);
63 if (fd == -1)
65 if (errno == ENOSYS)
67 /* Fail silently. */
68 error (0, 0, "open64 is not supported");
69 exit (EXIT_SUCCESS);
71 else
72 error (EXIT_FAILURE, errno, "cannot create temporary file");
74 if (!support_descriptor_supports_holes (fd))
75 FAIL_UNSUPPORTED ("File %s does not support holes", name);
76 add_temp_file (name);
78 if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
80 error (0, errno, "cannot get resource limit");
81 exit (0);
83 if (rlim.rlim_cur < TWO_GB + 200)
85 rlim.rlim_cur = TWO_GB + 200;
86 if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
88 error (0, errno, "cannot reset file size limits");
89 exit (0);
94 static void
95 test_ftello (void)
97 FILE *f;
98 int ret;
99 off64_t pos;
101 f = fopen64 (name, "w");
103 ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
104 if (ret == -1 && errno == ENOSYS)
106 error (0, 0, "fseeko64 is not supported.");
107 exit (EXIT_SUCCESS);
109 if (ret == -1 && errno == EINVAL)
111 error (0, 0, "LFS seems not to be supported");
112 exit (EXIT_SUCCESS);
114 if (ret == -1)
116 error (0, errno, "fseeko64 failed with error");
117 exit (EXIT_FAILURE);
120 ret = fwrite ("Hello", 1, 5, f);
121 if (ret == -1 && errno == EFBIG)
123 error (0, errno, "LFS seems not to be supported");
124 exit (EXIT_SUCCESS);
127 if (ret == -1 && errno == ENOSPC)
129 error (0, 0, "Not enough space to write file.");
130 exit (EXIT_SUCCESS);
133 if (ret != 5)
134 error (EXIT_FAILURE, errno, "Cannot write test string to large file");
136 pos = ftello64 (f);
138 if (pos != TWO_GB+105)
140 error (0, 0, "ftello64 gives wrong result.");
141 exit (EXIT_FAILURE);
144 fclose (f);
148 do_test (int argc, char *argv[])
150 int ret, fd2;
151 struct stat64 statbuf;
153 ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
154 if (ret == -1 && errno == ENOSYS)
156 error (0, 0, "lseek64 is not supported.");
157 exit (EXIT_SUCCESS);
159 if (ret == -1 && errno == EINVAL)
161 error (0, 0, "LFS seems not to be supported.");
162 exit (EXIT_SUCCESS);
164 if (ret == -1)
166 error (0, errno, "lseek64 failed with error");
167 exit (EXIT_FAILURE);
169 off64_t offset64 = lseek64 (fd, 0, SEEK_CUR);
170 if (offset64 != TWO_GB + 100)
172 error (0, 0, "lseek64 did not return expected offset");
173 exit (EXIT_FAILURE);
175 off_t offset = lseek (fd, 0, SEEK_CUR);
176 if (sizeof (off_t) < sizeof (off64_t))
178 if (offset != -1 || errno != EOVERFLOW)
180 error (0, 0, "lseek did not fail with EOVERFLOW");
181 exit (EXIT_FAILURE);
184 else
185 if (offset != TWO_GB + 100)
187 error (0, 0, "lseek did not return expected offset");
188 exit (EXIT_FAILURE);
191 ret = write (fd, "Hello", 5);
192 if (ret == -1 && errno == EFBIG)
194 error (0, 0, "LFS seems not to be supported.");
195 exit (EXIT_SUCCESS);
198 if (ret == -1 && errno == ENOSPC)
200 error (0, 0, "Not enough space to write file.");
201 exit (EXIT_SUCCESS);
204 if (ret != 5)
205 error (EXIT_FAILURE, errno, "cannot write test string to large file");
207 ret = close (fd);
209 if (ret == -1)
210 error (EXIT_FAILURE, errno, "error closing file");
212 ret = stat64 (name, &statbuf);
214 if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
215 error (0, 0, "stat64 is not supported.");
216 else if (ret == -1)
217 error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
218 else if (statbuf.st_size != (TWO_GB + 100 + 5))
219 error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
220 (long long int) statbuf.st_size, (TWO_GB + 100 + 5));
222 fd2 = openat64 (AT_FDCWD, name, O_RDWR);
223 if (fd2 == -1)
225 if (errno == ENOSYS)
227 /* Silently ignore this test. */
228 error (0, 0, "openat64 is not supported");
230 else
231 error (EXIT_FAILURE, errno, "openat64 failed to open big file");
233 else
235 ret = close (fd2);
237 if (ret == -1)
238 error (EXIT_FAILURE, errno, "error closing file");
241 test_ftello ();
243 return 0;