1 /*
2  * Copyright (C) 2016 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 #include <stdio.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <signal.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/errno.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27 #include "ioshark.h"
28 
29 /*
30  * Real simple utility that just extracts and dumps the IOshark filenames
31  * one per line from the ioshark_filenames file. Useful in debugging.
32  */
33 int
main(int argc,char ** argv)34 main(int argc __attribute__((unused)), char **argv)
35 {
36 	char *progname;
37 	static FILE *filename_cache_fp;
38 	struct stat st;
39 	struct ioshark_filename_struct *filename_cache;
40 	int filename_cache_num_entries;
41 	size_t filename_cache_size;
42 	int i;
43 
44 	progname = argv[0];
45 	if (stat("ioshark_filenames", &st) < 0) {
46 		fprintf(stderr, "%s Can't stat ioshark_filenames file\n",
47 			progname);
48 			exit(EXIT_FAILURE);
49 	}
50 	filename_cache_num_entries = st.st_size /
51 		sizeof(struct ioshark_filename_struct);
52 	filename_cache_fp = fopen("ioshark_filenames", "r");
53 	if (filename_cache_fp == NULL) {
54 		fprintf(stderr, "%s Cannot open ioshark_filenames file\n",
55 			progname);
56 		exit(EXIT_FAILURE);
57 	}
58 	/* Preallocate a fixed size of entries */
59 	filename_cache_size = filename_cache_num_entries + 1024;
60 	filename_cache = calloc(filename_cache_size,
61 				sizeof(struct ioshark_filename_struct));
62 	if (filename_cache == NULL) {
63 		fprintf(stderr, "%s Can't allocate memory - this is fatal\n",
64 			__func__);
65 		exit(EXIT_FAILURE);
66 	}
67 	if (fread(filename_cache,
68 		  sizeof(struct ioshark_filename_struct),
69 		  filename_cache_num_entries,
70 		  filename_cache_fp) != (size_t)filename_cache_num_entries) {
71 		fprintf(stderr, "%s Can't read ioshark_filenames file\n",
72 			progname);
73 		exit(EXIT_FAILURE);
74 	}
75        for (i = 0 ; i < filename_cache_num_entries ; i++) {
76 	       printf("%s\n", filename_cache[i].path);
77        }
78        free(filename_cache);
79        fclose(filename_cache_fp);
80 }
81