tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / gem_fd_exhaustion.c
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Daniel Vetter <daniel.vetter@ffwll.ch>
25  *
26  */
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/resource.h>
31 #include <sys/time.h>
32 #include <fcntl.h>
33 #include <limits.h>
34
35 #include "drmtest.h"
36 #include "ioctl_wrappers.h"
37 #include "igt_aux.h"
38
39
40 #define FD_ARR_SZ 100
41 int fd_arr[FD_ARR_SZ];
42
43 igt_simple_main
44 {
45         int fd, i;
46         struct rlimit rlim;
47         unsigned nofile_rlim;
48         FILE *file_max = fopen("/proc/sys/fs/file-max", "r");
49         igt_assert(fscanf(file_max, "%u", &nofile_rlim) == 1);
50         fclose(file_max);
51
52         igt_info("System limit for open files is %u\n", nofile_rlim);
53
54         igt_assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
55         rlim.rlim_cur = nofile_rlim;
56         rlim.rlim_max = nofile_rlim;
57         igt_assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
58
59         fd = drm_open_any();
60
61         igt_assert(open("/dev/null", O_RDONLY) >= 0);
62
63         igt_fork(n, 1) {
64                 igt_drop_root();
65
66                 for (i = 0; ; i++) {
67                         int tmp_fd = open("/dev/null", O_RDONLY);
68                         uint32_t handle;
69
70                         if (tmp_fd >= 0 && i < FD_ARR_SZ)
71                                 fd_arr[i] = tmp_fd;
72
73                         handle = __gem_create(fd, 4096);
74                         if (handle)
75                                 gem_close(fd, handle);
76
77
78                         if (tmp_fd < 0) {
79                                 /* Ensure we actually hit the failure path ... */
80                                 igt_assert(handle == 0);
81                                 igt_info("fd exhaustion after %i rounds.\n", i);
82                                 break;
83                         }
84                 }
85
86                 /* The child will free all the fds when exiting, so no need to
87                  * clean up to mess to ensure that the parent can at least run
88                  * the exit handlers. */
89         }
90
91         igt_waitchildren();
92
93         close(fd);
94 }