xref: /aosp_15_r20/system/extras/tests/storage/opentest.c (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
18*288bf522SAndroid Build Coastguard Worker #include <sys/stat.h>
19*288bf522SAndroid Build Coastguard Worker #include <fcntl.h>
20*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
21*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
22*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
23*288bf522SAndroid Build Coastguard Worker #include <sys/time.h>
24*288bf522SAndroid Build Coastguard Worker #include <sys/resource.h>
25*288bf522SAndroid Build Coastguard Worker 
main(int argc,char * argv[])26*288bf522SAndroid Build Coastguard Worker int main(int argc, char *argv[])
27*288bf522SAndroid Build Coastguard Worker {
28*288bf522SAndroid Build Coastguard Worker     int i;
29*288bf522SAndroid Build Coastguard Worker     int nfiles;
30*288bf522SAndroid Build Coastguard Worker     int *fds;
31*288bf522SAndroid Build Coastguard Worker     char *dir;
32*288bf522SAndroid Build Coastguard Worker     struct stat statbuf;
33*288bf522SAndroid Build Coastguard Worker     char name[16];
34*288bf522SAndroid Build Coastguard Worker     struct rlimit rlim;
35*288bf522SAndroid Build Coastguard Worker 
36*288bf522SAndroid Build Coastguard Worker     if (argc != 3) {
37*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "Usage: opentest <directory> <num_files>\n");
38*288bf522SAndroid Build Coastguard Worker         exit(1);
39*288bf522SAndroid Build Coastguard Worker     }
40*288bf522SAndroid Build Coastguard Worker 
41*288bf522SAndroid Build Coastguard Worker     dir = argv[1];
42*288bf522SAndroid Build Coastguard Worker 
43*288bf522SAndroid Build Coastguard Worker     nfiles = atoi(argv[2]);
44*288bf522SAndroid Build Coastguard Worker     if ((nfiles <= 0) || (nfiles > 65536)) {
45*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "num_files must be between 1 and 65536\n");
46*288bf522SAndroid Build Coastguard Worker         exit(1);
47*288bf522SAndroid Build Coastguard Worker     }
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker     if (stat(dir, &statbuf)) {
50*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "Cannot stat %s\n", dir);
51*288bf522SAndroid Build Coastguard Worker         exit(1);
52*288bf522SAndroid Build Coastguard Worker     }
53*288bf522SAndroid Build Coastguard Worker 
54*288bf522SAndroid Build Coastguard Worker     if (! S_ISDIR(statbuf.st_mode)) {
55*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "%s is not a directory!\n", dir);
56*288bf522SAndroid Build Coastguard Worker         exit(1);
57*288bf522SAndroid Build Coastguard Worker     }
58*288bf522SAndroid Build Coastguard Worker 
59*288bf522SAndroid Build Coastguard Worker     if (access(dir, R_OK | W_OK)) {
60*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "No access to %s\n", dir);
61*288bf522SAndroid Build Coastguard Worker         exit(1);
62*288bf522SAndroid Build Coastguard Worker     }
63*288bf522SAndroid Build Coastguard Worker 
64*288bf522SAndroid Build Coastguard Worker     fds = malloc(nfiles * sizeof(int));
65*288bf522SAndroid Build Coastguard Worker     if (fds == 0) {
66*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "Unable to malloc array of %d fds\n", nfiles);
67*288bf522SAndroid Build Coastguard Worker         exit(1);
68*288bf522SAndroid Build Coastguard Worker     }
69*288bf522SAndroid Build Coastguard Worker 
70*288bf522SAndroid Build Coastguard Worker     if (chdir(dir)) {
71*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "Cannot chdir to %s\n", dir);
72*288bf522SAndroid Build Coastguard Worker         exit(1);
73*288bf522SAndroid Build Coastguard Worker     }
74*288bf522SAndroid Build Coastguard Worker 
75*288bf522SAndroid Build Coastguard Worker     rlim.rlim_cur = nfiles + 10;
76*288bf522SAndroid Build Coastguard Worker     rlim.rlim_max = nfiles + 10;
77*288bf522SAndroid Build Coastguard Worker     if (setrlimit(RLIMIT_NOFILE, &rlim)) {
78*288bf522SAndroid Build Coastguard Worker         fprintf(stderr, "Unable to raise RLIMIT_NOFILE to %ld\n", rlim.rlim_cur);
79*288bf522SAndroid Build Coastguard Worker         exit(1);
80*288bf522SAndroid Build Coastguard Worker     }
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker     for (i = 0; i < nfiles; i++) {
83*288bf522SAndroid Build Coastguard Worker         snprintf(name, sizeof(name), "%d", i);
84*288bf522SAndroid Build Coastguard Worker         fds[i] = open(name, O_WRONLY | O_CREAT, 0666);
85*288bf522SAndroid Build Coastguard Worker         if (fds[i] < 0) {
86*288bf522SAndroid Build Coastguard Worker             fprintf(stderr, "Unable to open %d fd\n", i);
87*288bf522SAndroid Build Coastguard Worker             exit(1);
88*288bf522SAndroid Build Coastguard Worker         }
89*288bf522SAndroid Build Coastguard Worker     }
90*288bf522SAndroid Build Coastguard Worker 
91*288bf522SAndroid Build Coastguard Worker     /* Rely upon exit to cleanup! */
92*288bf522SAndroid Build Coastguard Worker     exit(0);
93*288bf522SAndroid Build Coastguard Worker }
94*288bf522SAndroid Build Coastguard Worker 
95*288bf522SAndroid Build Coastguard Worker 
96