cmd/xfsprogs/libdm/dmapi_tests/README 1.1 Renamed to cmd/xfstests/dmapi/README
[xfstests-dev.git] / dmapi / src / suite1 / cmd / security_hole.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #ifdef linux
39 #include <string.h>
40 #endif
41
42 /*
43         To read unallocated disk blocks in a filesystem, do
44                 cc -o test thisfile.c
45                 ./test myfile
46                 cat myfile
47 */
48
49 #ifndef linux
50 extern  char    *sys_errlist[];
51 #endif
52 extern  int     optind;
53 extern  char    *optarg;
54
55 static  char    *Progname;
56
57
58 static void
59 usage(void)
60 {
61         int     i;
62
63         fprintf(stderr, "usage:\t%s [-s size] pathname\n", Progname);
64         fprintf(stderr, "\t-s size\t\tsize of file (default 10,000,000 bytes)\n");
65         exit(1);
66 }
67
68
69 int
70 main(
71         int     argc, 
72         char    **argv)
73 {
74         char    *pathname = NULL;
75         off_t   size = 10000000;
76         char    buff[1];
77         int     method = F_RESVSP;
78         flock_t flock;
79         int     opt;
80         int     fd;
81
82         if (Progname = strrchr(argv[0], '/')) {
83                 Progname++;
84         } else {
85                 Progname = argv[0];
86         }
87
88         /* Crack and validate the command line options. */
89
90         while ((opt = getopt(argc, argv, "s:")) != EOF) {
91                 switch (opt) {
92                 case 's':
93                         size = atol(optarg);
94                         break;
95                 case '?':
96                         usage();
97                 }
98         }
99         if (optind + 1 != argc)
100                 usage();
101         pathname = argv[optind];
102
103         /* Create the file and write one byte at a large offset to create a
104            big hole in the middle of the file.
105         */
106
107         if ((fd = open(pathname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
108                 perror("open failed");
109                 exit(1);
110         }
111         if (lseek(fd, size, 0) < 0) {
112                 perror("lseek failed");
113                 exit(1);
114         }
115         buff[0] = '\0';
116         if (write(fd, buff, 1) != 1) {
117                 perror("write failed");
118                 exit(1);
119         }
120
121         /* Now fill in the hole with uninitialized blocks. */
122
123         flock.l_whence = 0;
124         flock.l_start = 0;
125         flock.l_len = size;
126
127         if (fcntl(fd, method, &flock) < 0) {
128                 perror("fcntl failed");
129                 exit(1);
130         }
131         printf("%s created\n", pathname);
132 }