~ubuntu-branches/debian/sid/trinity/sid

« back to all changes in this revision

Viewing changes to fds.c

  • Committer: Package Import Robot
  • Author(s): gustavo panizzo
  • Date: 2014-01-17 21:10:15 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140117211015-k2qbnpu0osa5mlil
Tags: 1.3-1
* New upstream version 1.3.
* Removed wrong dependency on linux-headers. (Closes: #733771).

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <stdlib.h>
4
4
#include <string.h>
5
5
#include <unistd.h>
 
6
#include <sys/epoll.h>
 
7
#include <sys/eventfd.h>
6
8
 
 
9
#include "perf.h"
 
10
#include "random.h"
7
11
#include "shm.h"
8
12
#include "files.h"
9
13
#include "pids.h"
12
16
#include "sanitise.h"
13
17
#include "params.h"
14
18
 
15
 
unsigned int nr_file_fds = 0;
16
 
 
 
19
/* Pipe FD related functions. */
17
20
static void open_pipes(void)
18
21
{
19
22
        int pipes[2];
32
35
        }
33
36
}
34
37
 
 
38
static int rand_pipe_fd(void)
 
39
{
 
40
        return shm->pipe_fds[rand() % MAX_PIPE_FDS];
 
41
}
 
42
 
 
43
/* perf related fds (see also perf.c & syscalls/perf_event_open.c) */
 
44
static int rand_perf_fd(void)
 
45
{
 
46
        return shm->perf_fds[rand() % MAX_PERF_FDS];
 
47
}
 
48
 
 
49
/* epoll related fds */
 
50
static void open_epoll_fds(void)
 
51
{
 
52
        unsigned int i = 0;
 
53
        int fd = -1;
 
54
 
 
55
        while (i < MAX_EPOLL_FDS) {
 
56
 
 
57
                switch (rand_bool()) {
 
58
                case 0: fd = epoll_create(1);
 
59
                        break;
 
60
                case 1: fd = epoll_create1(EPOLL_CLOEXEC);
 
61
                        break;
 
62
                default:
 
63
                         break;
 
64
                }
 
65
 
 
66
                if (fd != -1) {
 
67
                        shm->epoll_fds[i] = fd;
 
68
                        output(2, "fd[%d] = epoll\n", shm->epoll_fds[i]);
 
69
                        i++;
 
70
                }
 
71
        }
 
72
}
 
73
 
 
74
static int rand_epoll_fd(void)
 
75
{
 
76
        return shm->epoll_fds[rand() % MAX_EPOLL_FDS];
 
77
}
 
78
 
 
79
/* eventfd FDs */
 
80
static void open_eventfd_fds(void)
 
81
{
 
82
        unsigned int i;
 
83
 
 
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);
 
92
 
 
93
        for (i = 0; i < MAX_EVENTFD_FDS; i++)
 
94
                output(2, "fd[%d] = eventfd\n", shm->eventfd_fds[i]);
 
95
}
 
96
 
 
97
static int rand_eventfd_fd(void)
 
98
{
 
99
        return shm->eventfd_fds[rand() % MAX_EVENTFD_FDS];
 
100
}
 
101
 
 
102
/* regular file FDs  */
 
103
unsigned int nr_file_fds = 0;
 
104
 
35
105
static int rand_file_fd(void)
36
106
{
37
107
        unsigned int fd_index;
40
110
        return shm->file_fds[fd_index];
41
111
}
42
112
 
43
 
static int rand_pipe_fd(void)
44
 
{
45
 
        return shm->pipe_fds[rand() % MAX_PIPE_FDS];
46
 
}
47
113
 
48
114
static int get_new_random_fd(void)
49
115
{
50
116
        unsigned int i;
51
117
        int fd = 0;
52
118
 
53
 
        i = rand() % 3;
 
119
retry:
 
120
        i = rand() % 6;
54
121
 
55
122
        if (do_specific_proto == TRUE)
56
123
                i = 1;
86
153
                                fd = rand_pipe_fd();
87
154
                        return fd;
88
155
                }
89
 
                fd = shm->socket_fds[rand() % nr_sockets];
 
156
                fd = shm->sockets[rand() % nr_sockets].fd;
90
157
                break;
91
158
 
92
159
        case 2:
93
160
                fd = rand_pipe_fd();
94
161
                break;
 
162
 
 
163
        case 3:
 
164
                if (shm->perf_fds[0] == 0)      /* perf unavailable/disabled. */
 
165
                        goto retry;
 
166
 
 
167
                fd = rand_perf_fd();
 
168
                break;
 
169
 
 
170
        case 4:
 
171
                fd = rand_epoll_fd();
 
172
                break;
 
173
 
 
174
        case 5:
 
175
                fd = rand_eventfd_fd();
 
176
                break;
 
177
 
95
178
        default:
96
179
                break;
97
180
        }
123
206
 
124
207
void setup_fds(void)
125
208
{
126
 
        open_sockets();
127
 
        if (no_files == TRUE)
128
 
                return;
 
209
        /* If we have victim files, don't worry about sockets. */
 
210
        if (victim_path == NULL)
 
211
                open_sockets();
129
212
 
130
213
        open_pipes();
131
214
 
132
 
        generate_filelist();
133
 
        if (files_in_index == 0)
134
 
                return;
135
 
 
136
 
        open_files();
 
215
        open_perf_fds();
 
216
 
 
217
        open_epoll_fds();
 
218
 
 
219
        open_eventfd_fds();
 
220
 
 
221
        if (no_files == FALSE) {
 
222
                generate_filelist();
 
223
                if (files_in_index == 0)        /* Something bad happened. Crappy -V maybe? */
 
224
                        return;                 // FIXME: We should log something here probably.
 
225
 
 
226
                open_files();
 
227
        }
137
228
}
138
229
 
139
230
void regenerate_fds(void)