| 1 |
/* |
| 2 |
* Copyright (C) 2012 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 <sys/types.h> |
| 18 |
#include <sys/stat.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <sys/time.h> |
| 24 |
#include <sys/resource.h> |
| 25 |
|
| 26 |
int main(int argc, char *argv[]) |
| 27 |
{ |
| 28 |
int i; |
| 29 |
int nfiles; |
| 30 |
int *fds; |
| 31 |
char *dir; |
| 32 |
struct stat statbuf; |
| 33 |
char name[16]; |
| 34 |
struct rlimit rlim; |
| 35 |
|
| 36 |
if (argc != 3) { |
| 37 |
fprintf(stderr, "Usage: opentest <directory> <num_files>\n"); |
| 38 |
exit(1); |
| 39 |
} |
| 40 |
|
| 41 |
dir = argv[1]; |
| 42 |
|
| 43 |
nfiles = atoi(argv[2]); |
| 44 |
if ((nfiles <= 0) || (nfiles > 65536)) { |
| 45 |
fprintf(stderr, "num_files must be between 1 and 65536\n"); |
| 46 |
exit(1); |
| 47 |
} |
| 48 |
|
| 49 |
if (stat(dir, &statbuf)) { |
| 50 |
fprintf(stderr, "Cannot stat %s\n", dir); |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
if (! S_ISDIR(statbuf.st_mode)) { |
| 55 |
fprintf(stderr, "%s is not a directory!\n", dir); |
| 56 |
exit(1); |
| 57 |
} |
| 58 |
|
| 59 |
if (access(dir, R_OK | W_OK)) { |
| 60 |
fprintf(stderr, "No access to %s\n", dir); |
| 61 |
exit(1); |
| 62 |
} |
| 63 |
|
| 64 |
fds = malloc(nfiles * sizeof(int)); |
| 65 |
if (fds == 0) { |
| 66 |
fprintf(stderr, "Unable to malloc array of %d fds\n", nfiles); |
| 67 |
exit(1); |
| 68 |
} |
| 69 |
|
| 70 |
if (chdir(dir)) { |
| 71 |
fprintf(stderr, "Cannot chdir to %s\n", dir); |
| 72 |
exit(1); |
| 73 |
} |
| 74 |
|
| 75 |
rlim.rlim_cur = nfiles + 10; |
| 76 |
rlim.rlim_max = nfiles + 10; |
| 77 |
if (setrlimit(RLIMIT_NOFILE, &rlim)) { |
| 78 |
fprintf(stderr, "Unable to raise RLIMIT_NOFILE to %ld\n", rlim.rlim_cur); |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
|
| 82 |
for (i = 0; i < nfiles; i++) { |
| 83 |
snprintf(name, sizeof(name), "%d", i); |
| 84 |
fds[i] = open(name, O_WRONLY | O_CREAT, 0666); |
| 85 |
if (fds[i] < 0) { |
| 86 |
fprintf(stderr, "Unable to open %d fd\n", i); |
| 87 |
exit(1); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* Rely upon exit to cleanup! */ |
| 92 |
exit(0); |
| 93 |
} |
| 94 |
|
| 95 |
|