Whamcloud - gitweb
LU-16915 tests: except sanity-sec test_51
[fs/lustre-release.git] / lustre / tests / badarea_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, Intel Corporation.
24  * Use is subject to license terms.
25  */
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #undef perror
33 #define perror(str) ((void)0)
34
35 int main(int argc, char **argv)
36 {
37         ssize_t rc;
38         int fd;
39         void *volatile fd_ptr;
40         void *volatile buf = (void *)0x4096000;
41
42         fd = open(argv[1], O_WRONLY);
43         if (fd == -1) {
44                 perror(argv[1]);
45                 goto read;
46         }
47
48         /* We need rc because Sles11 compiler warns against unchecked
49          * return value of read and write
50          */
51         rc = write(fd, buf, 5);
52         if (rc != 5)
53                 perror("write badarea (Should have failed)");
54
55         fd_ptr = (void *)&fd;
56         rc = write(fd, fd_ptr, 0);
57         if (rc != 0)
58                 perror("write zero bytes");
59
60         rc = write(fd, fd_ptr, 1);
61         if (rc != 1)
62                 perror("write one byte");
63
64         rc = write(fd, fd_ptr, 2UL*1024*1024);
65         if (rc != 2UL*1024*1024)
66                 perror("write 2M");
67
68         rc = write(fd, fd_ptr, 1UL*(1024+512)*1024*1024);
69         if (rc != 1UL*(1024+512)*1024*1024)
70                 perror("write 1.5G");
71
72         rc = write(fd, fd_ptr, -2);
73         if (rc != -2)
74                 perror("write -2");
75
76         close(fd);
77
78 read:
79         fd = open(argv[1], O_RDONLY);
80         if (fd == -1)
81                 return 0;
82         rc = read(fd, buf, 5);
83         perror("read");
84
85         close(fd);
86
87         /* Tame the compiler spooked about rc assigned, but not used */
88         if (!rc)
89                 return -1; /* Not really important. */
90
91         return 0;
92 }