1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
| typedef struct {
unsigned long *in, *out, *ex;
unsigned long *res_in, *res_out, *res_ex;
} fd_set_bits;
// long sys_select(int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval *tvp)
SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
fd_set __user *, exp, struct timeval __user *, tvp)
{
struct timespec end_time, *to = NULL;
struct timeval tv;
int ret;
if (tvp) {
if (copy_from_user(&tv, tvp, sizeof(tv))) {
return -EFAULT;
}
// 计算超时时间
to = &end_time;
if (poll_select_set_timeout(to,
tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
(tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC)) {
return -EINVAL;
}
}
ret = core_sys_select(n, inp, outp, exp, to);
// 复制剩余时间到用户空间
ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
return ret;
}
int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
fd_set __user *exp, struct timespec *end_time)
{
fd_set_bits fds;
void *bits;
int ret, max_fds;
unsigned int size;
struct fdtable *fdt;
//小对象使用栈上的空间,节约内存, 加快访问速度
long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
ret = -EINVAL;
if (n < 0) {
goto out_nofds;
}
rcu_read_lock();
// 取得进程对应的 fdtable
fdt = files_fdtable(current->files);
max_fds = fdt->max_fds;
rcu_read_unlock();
if (n > max_fds) {
n = max_fds;
}
size = FDS_BYTES(n);
bits = stack_fds;
if (size > sizeof(stack_fds) / 6) {
// 栈上的空间不够, 申请内存, 全部使用堆上的空间
ret = -ENOMEM;
bits = kmalloc(6 * size, GFP_KERNEL);
if (!bits) {
goto out_nofds;
}
}
fds.in = bits;
fds.out = bits + size;
fds.ex = bits + 2*size;
fds.res_in = bits + 3*size;
fds.res_out = bits + 4*size;
fds.res_ex = bits + 5*size;
// 复制用户空间到内核
if ((ret = get_fd_set(n, inp, fds.in)) ||
(ret = get_fd_set(n, outp, fds.out)) ||
(ret = get_fd_set(n, exp, fds.ex))) {
goto out;
}
// 初始化fd set
zero_fd_set(n, fds.res_in);
zero_fd_set(n, fds.res_out);
zero_fd_set(n, fds.res_ex);
ret = do_select(n, &fds, end_time);
if (ret < 0) {
goto out;
}
if (!ret) {
// 该返回值会被系统捕获, 并以同样的参数重新调用sys_select()
ret = -ERESTARTNOHAND;
if (signal_pending(current)) {
goto out;
}
ret = 0;
}
// 复制到用户空间
if (set_fd_set(n, inp, fds.res_in) ||
set_fd_set(n, outp, fds.res_out) ||
set_fd_set(n, exp, fds.res_ex)) {
ret = -EFAULT;
}
out:
if (bits != stack_fds) {
kfree(bits);
}
out_nofds:
return ret;
}
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
{
ktime_t expire, *to = NULL;
struct poll_wqueues table;
poll_table *wait;
int retval, i, timed_out = 0;
unsigned long slack = 0;
rcu_read_lock();
// 检查fds中fd的有效性, 并获取当前最大的fd
retval = max_select_fd(n, fds);
rcu_read_unlock();
if (retval < 0) {
return retval;
}
n = retval;
// 初始化 poll_wqueues 结构, 设置函数指针_qproc 为__pollwait
poll_initwait(&table);
wait = &table.pt;
if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
wait = NULL;
timed_out = 1;
}
if (end_time && !timed_out) {
// 估计需要等待的时间.
slack = select_estimate_accuracy(end_time);
}
retval = 0;
for (;;) {
unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
inp = fds->in;
outp = fds->out;
exp = fds->ex;
rinp = fds->res_in;
routp = fds->res_out;
rexp = fds->res_ex;
// 遍历所有的描述符, i 文件描述符
for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
unsigned long in, out, ex, all_bits, bit = 1, mask, j;
unsigned long res_in = 0, res_out = 0, res_ex = 0;
const struct file_operations *f_op = NULL;
struct file *file = NULL;
// 检查当前的 slot 中的描述符
in = *inp++;
out = *outp++;
ex = *exp++;
all_bits = in | out | ex;
if (all_bits == 0) { // 没有需要监听的描述符, 下一个slot
i += __NFDBITS;
continue;
}
for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
int fput_needed;
if (i >= n) {
break;
}
// 不需要监听描述符 i
if (!(bit & all_bits)) {
continue;
}
// 取得文件结构
file = fget_light(i, &fput_needed);
if (file) {
f_op = file->f_op;
// 没有 f_op 的话就认为一直处于就绪状态
mask = DEFAULT_POLLMASK;
if (f_op && f_op->poll) {
// 设置等待事件的掩码
wait_key_set(wait, in, out, bit);
/*
static inline void wait_key_set(poll_table *wait, unsigned long in,
unsigned long out, unsigned long bit)
{
wait->_key = POLLEX_SET;// (POLLPRI)
if (in & bit)
wait->_key |= POLLIN_SET;//(POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
if (out & bit)
wait->_key |= POLLOUT_SET;//POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
}
*/
// 获取当前的就绪状态, 并添加到文件的对应等待队列中
mask = (*f_op->poll)(file, wait);
// 和poll完全一样
}
fput_light(file, fput_needed);
// 释放文件
// 检查文件 i 是否已有事件就绪,
if ((mask & POLLIN_SET) && (in & bit)) {
res_in |= bit;
retval++;
// 如果已有就绪事件就不再向其他文件的
// 等待队列中添加回调函数
wait = NULL;
}
if ((mask & POLLOUT_SET) && (out & bit)) {
res_out |= bit;
retval++;
wait = NULL;
}
if ((mask & POLLEX_SET) && (ex & bit)) {
res_ex |= bit;
retval++;
wait = NULL;
}
}
}
if (res_in) {
*rinp = res_in;
}
if (res_out) {
*routp = res_out;
}
if (res_ex) {
*rexp = res_ex;
}
cond_resched();
}
wait = NULL; // 该添加回调函数的都已经添加了
if (retval || timed_out || signal_pending(current)) {
break; // 信号发生,监听事件就绪或超时
}
if (table.error) {
retval = table.error; // 产生错误了
break;
}
// 转换到内核时间
if (end_time && !to) {
expire = timespec_to_ktime(*end_time);
to = &expire;
}
// 等待直到超时, 或由回调函数唤醒, 超时后会再次遍历文件描述符
if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
to, slack)) {
timed_out = 1;
}
}
poll_freewait(&table);
return retval;
}
|