OSDN Git Service

ANRdaemon: move trace result from /sdcard to /data
[android-x86/system-extras.git] / puncture_fs / puncture_fs.c
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <assert.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <logwrap/logwrap.h>
27 #include <sys/stat.h>
28 #include <sys/statvfs.h>
29 #include <utils/Log.h>
30
31 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32
33 #define MAX_IO_WRITE_CHUNK_SIZE 0x100000
34
35 #ifndef min
36 #define min(a,b) ((a) < (b) ? (a) : (b))
37 #endif
38
39 typedef unsigned long u64;
40
41 static void usage(const char * const progname) {
42     fprintf(stderr,
43             "Usage: %s [-s <seed>] -h <hole size in bytes> -t <total hole size in bytes> "
44             "path\n",
45             progname);
46 }
47
48 static u64 get_free_space(const char * const path) {
49     struct statvfs s;
50
51     if (statvfs(path, &s) < 0) {
52         fprintf(stderr, "\nerrno: %d. Failed to get free disk space on %s\n",
53                 errno, path);
54         return 0;
55     } else {
56         return (u64)s.f_bsize * (u64)s.f_bfree;
57     }
58 }
59
60 static u64 get_random_num(const u64 start, const u64 end) {
61     if (end - start <= 0)
62         return start;
63     assert(RAND_MAX >= 0x7FFFFFFF);
64     if ((end - start) > 0x7FFFFFFF)
65         return start + (((u64)random() << 31) | (u64)random()) % (end - start);
66     return start + (random() % (end - start));
67 }
68
69 static char get_random_char() {
70     return 'A' + random() % ('Z' - 'A');
71 }
72
73 static bool create_unique_file(const char * const dir_path, const u64 size,
74                                const u64 id, char * const base,
75                                const u64 base_length) {
76     u64 length = 0;
77     int fd;
78     char file_path[FILENAME_MAX];
79     bool ret = true;
80
81     base[random() % min(base_length, size)] = get_random_char();
82
83     sprintf(file_path, "%s/file_%lu", dir_path, id);
84     fd = open(file_path, O_WRONLY | O_CREAT | O_SYNC, 0777);
85     if (fd < 0) {
86         // We suppress ENOSPC erros as that is common as we approach the
87         // last few MBs of the fs as we don't account for the size of the newly
88         // added meta data after the initial free space computation.
89         if (errno != 28) {
90             fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, file_path);
91         }
92         return false;
93     }
94     while (length + base_length < size) {
95         if (write(fd, base, base_length) < 0) {
96             if (errno != 28) {
97                 fprintf(stderr, "\nerrno: %d. Failed to write %lu bytes to %s\n",
98                         errno, base_length, file_path);
99             }
100             ret = false;
101             goto done;
102         }
103         length += base_length;
104     }
105     if (write(fd, base, size - length) < 0) {
106         if (errno != 28) {
107             fprintf(stderr, "\nerrno: %d. Failed to write last %lu bytes to %s\n",
108                     errno, size - length, file_path);
109         }
110         ret = false;
111         goto done;
112     }
113 done:
114     if (close(fd) < 0) {
115         fprintf(stderr, "\nFailed to close %s\n", file_path);
116         ret = false;
117     }
118     return ret;
119 }
120
121 static bool create_unique_dir(char *dir, const char * const root_path) {
122     char random_string[15];
123     int i;
124
125     for (i = 0; i < 14; ++i) {
126         random_string[i] = get_random_char();
127     }
128     random_string[14] = '\0';
129
130     sprintf(dir, "%s/%s", root_path, random_string);
131
132     if (mkdir(dir, 0777) < 0) {
133         fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, dir);
134         return false;
135     }
136     return true;
137 }
138
139 static bool puncture_fs (const char * const path, const u64 total_size,
140                          const u64 hole_size, const u64 total_hole_size) {
141     u64 increments = (hole_size * total_size) / total_hole_size;
142     u64 hole_max;
143     u64 starting_max = 0;
144     u64 ending_max = increments;
145     char stay_dir[FILENAME_MAX], delete_dir[FILENAME_MAX];
146     char *rm_bin_argv[] = { "/system/bin/rm", "-rf", ""};
147     u64 file_id = 1;
148     char *base_file_data;
149     u64 i = 0;
150
151     if (!create_unique_dir(stay_dir, path) ||
152         !create_unique_dir(delete_dir, path)) {
153         return false;
154     }
155
156     base_file_data = (char*) malloc(MAX_IO_WRITE_CHUNK_SIZE);
157     for (i = 0; i < MAX_IO_WRITE_CHUNK_SIZE; ++i) {
158         base_file_data[i] = get_random_char();
159     }
160     fprintf(stderr, "\n");
161     while (ending_max <= total_size) {
162         fprintf(stderr, "\rSTAGE 1/2: %d%% Complete",
163                 (int) (100.0 * starting_max / total_size));
164         hole_max = get_random_num(starting_max, ending_max);
165
166         create_unique_file(stay_dir,
167                            hole_max - starting_max,
168                            file_id++,
169                            base_file_data,
170                            MAX_IO_WRITE_CHUNK_SIZE);
171         create_unique_file(delete_dir,
172                            hole_size,
173                            file_id++,
174                            base_file_data,
175                            MAX_IO_WRITE_CHUNK_SIZE);
176
177         starting_max = hole_max + hole_size;
178         ending_max += increments;
179     }
180     create_unique_file(stay_dir,
181                        (ending_max - increments - starting_max),
182                        file_id++,
183                        base_file_data,
184                        MAX_IO_WRITE_CHUNK_SIZE);
185     fprintf(stderr, "\rSTAGE 1/2: 100%% Complete\n");
186     fprintf(stderr, "\rSTAGE 2/2: 0%% Complete");
187     free(base_file_data);
188     rm_bin_argv[2] = delete_dir;
189     if (android_fork_execvp_ext(ARRAY_SIZE(rm_bin_argv), rm_bin_argv,
190                                 NULL, 1, LOG_KLOG, 0, NULL) < 0) {
191         fprintf(stderr, "\nFailed to delete %s\n", rm_bin_argv[2]);
192         return false;
193     }
194     fprintf(stderr, "\rSTAGE 2/2: 100%% Complete\n");
195     return true;
196 }
197
198 int main (const int argc, char ** const argv) {
199     int opt;
200     int mandatory_opt;
201     char *path = NULL;
202     int seed = time(NULL);
203
204     u64 total_size = 0;
205     u64 hole_size = 0;
206     u64 total_hole_size = 0;
207
208     mandatory_opt = 2;
209     while ((opt = getopt(argc, argv, "s:h:t:")) != -1) {
210         switch(opt) {
211             case 's':
212                 seed = atoi(optarg);
213                 break;
214             case 'h':
215                 hole_size = atoll(optarg);
216                 mandatory_opt--;
217                 break;
218             case 't':
219                 total_hole_size = atoll(optarg);
220                 mandatory_opt--;
221                 break;
222             default:
223                 usage(argv[0]);
224                 exit(EXIT_FAILURE);
225         }
226     }
227     if (mandatory_opt) {
228         usage(argv[0]);
229         exit(EXIT_FAILURE);
230     }
231     if (optind >= argc) {
232         fprintf(stderr, "\nExpected path name after options.\n");
233         usage(argv[0]);
234         exit(EXIT_FAILURE);
235     }
236     path = argv[optind++];
237
238     if (optind < argc) {
239         fprintf(stderr, "\nUnexpected argument: %s\n", argv[optind]);
240         usage(argv[0]);
241         exit(EXIT_FAILURE);
242     }
243
244     srandom(seed);
245     fprintf(stderr, "\nRandom seed is: %d\n", seed);
246
247     total_size = get_free_space(path);
248     if (!total_size) {
249         exit(EXIT_FAILURE);
250     }
251     if (total_size < total_hole_size || total_hole_size < hole_size) {
252         fprintf(stderr, "\nInvalid sizes: total available size should be "
253                         "larger than total hole size which is larger than "
254                         "hole size\n");
255         exit(EXIT_FAILURE);
256     }
257
258     if (!puncture_fs(path, total_size, hole_size, total_hole_size)) {
259         exit(EXIT_FAILURE);
260     }
261     return 0;
262 }