7 #include <sys/eventfd.h>
19 /* Pipe FD related functions. */
20 static void open_pipes(void)
25 for (i
= 0; i
< MAX_PIPE_FDS
; i
+=2) {
26 if (pipe(pipes
) < 0) {
27 perror("pipe fail.\n");
30 shm
->pipe_fds
[i
] = pipes
[0];
31 shm
->pipe_fds
[i
+1] = pipes
[1];
33 output(2, "fd[%d] = pipe\n", shm
->pipe_fds
[i
]);
34 output(2, "fd[%d] = pipe\n", shm
->pipe_fds
[i
+1]);
38 static int rand_pipe_fd(void)
40 return shm
->pipe_fds
[rand() % MAX_PIPE_FDS
];
43 /* perf related fds (see also perf.c & syscalls/perf_event_open.c) */
44 static int rand_perf_fd(void)
46 return shm
->perf_fds
[rand() % MAX_PERF_FDS
];
49 /* epoll related fds */
50 static void open_epoll_fds(void)
55 while (i
< MAX_EPOLL_FDS
) {
57 switch (rand_bool()) {
58 case 0: fd
= epoll_create(1);
60 case 1: fd
= epoll_create1(EPOLL_CLOEXEC
);
67 shm
->epoll_fds
[i
] = fd
;
68 output(2, "fd[%d] = epoll\n", shm
->epoll_fds
[i
]);
74 static int rand_epoll_fd(void)
76 return shm
->epoll_fds
[rand() % MAX_EPOLL_FDS
];
80 static void open_eventfd_fds(void)
84 shm
->eventfd_fds
[0] = eventfd(rand32(), 0);
85 shm
->eventfd_fds
[1] = eventfd(rand32(), EFD_CLOEXEC
);
86 shm
->eventfd_fds
[2] = eventfd(rand32(), EFD_NONBLOCK
);
87 shm
->eventfd_fds
[3] = eventfd(rand32(), EFD_SEMAPHORE
);
88 shm
->eventfd_fds
[4] = eventfd(rand32(), EFD_CLOEXEC
| EFD_NONBLOCK
);
89 shm
->eventfd_fds
[5] = eventfd(rand32(), EFD_CLOEXEC
| EFD_SEMAPHORE
);
90 shm
->eventfd_fds
[6] = eventfd(rand32(), EFD_NONBLOCK
| EFD_SEMAPHORE
);
91 shm
->eventfd_fds
[7] = eventfd(rand32(), EFD_CLOEXEC
| EFD_NONBLOCK
| EFD_SEMAPHORE
);
93 for (i
= 0; i
< MAX_EVENTFD_FDS
; i
++)
94 output(2, "fd[%d] = eventfd\n", shm
->eventfd_fds
[i
]);
97 static int rand_eventfd_fd(void)
99 return shm
->eventfd_fds
[rand() % MAX_EVENTFD_FDS
];
102 /* regular file FDs */
103 unsigned int nr_file_fds
= 0;
105 static int rand_file_fd(void)
107 unsigned int fd_index
;
109 fd_index
= rand() % nr_file_fds
;
110 return shm
->file_fds
[fd_index
];
114 static int get_new_random_fd(void)
122 if (do_specific_proto
== TRUE
)
125 if (no_files
== TRUE
)
128 /* Ugly special case.
129 * Sometimes, we can get here without any fd's setup.
130 * If this happens, we divide by zero if we pick case 0 because
131 * nr_file_fds is zero
133 * When this circumstance occurs, we just force it to use another network socket.
135 * FIXME: A better solution would be to like, actually open an fd. duh.
137 if (nr_file_fds
== 0)
146 /* When using victim files, sockets can be 0.
147 * Use files as a fallback, or pipes if no files are open.
149 if (nr_sockets
== 0) {
156 fd
= shm
->socket_fds
[rand() % nr_sockets
];
164 if (shm
->perf_fds
[0] == 0) /* perf unavailable/disabled. */
171 fd
= rand_epoll_fd();
175 fd
= rand_eventfd_fd();
185 int get_random_fd(void)
187 /* 25% chance of returning something new. */
188 if ((rand() % 4) == 0)
189 return get_new_random_fd();
191 /* the rest of the time, return the same fd as last time. */
193 if (shm
->fd_lifetime
== 0) {
194 shm
->current_fd
= get_new_random_fd();
195 shm
->fd_lifetime
= (rand() % shm
->max_children
) + 5;
199 if (shm
->current_fd
== 0) {
200 shm
->fd_lifetime
= 0;
204 return shm
->current_fd
;
219 if (no_files
== FALSE
) {
221 if (files_in_index
== 0) /* Something bad happened. Crappy -V maybe? */
222 return; // FIXME: We should log something here probably.
228 void regenerate_fds(void)
230 if (no_files
== TRUE
)