This source file includes following definitions.
- free_wait
- check
- do_select
- __get_fd_set
- __set_fd_set
- sys_select
1
2
3
4
5
6
7
8 #include <linux/types.h>
9 #include <linux/time.h>
10 #include <linux/fs.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17
18 #include <asm/segment.h>
19 #include <asm/system.h>
20
21 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 static void free_wait(select_table * p)
42 {
43 struct select_table_entry * entry = p->entry + p->nr;
44
45 while (p->nr > 0) {
46 p->nr--;
47 entry--;
48 remove_wait_queue(entry->wait_address,&entry->wait);
49 }
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63 static int check(int flag, select_table * wait, struct file * file)
64 {
65 struct inode * inode;
66 struct file_operations *fops;
67 int (*select) (struct inode *, struct file *, int, select_table *);
68
69 inode = file->f_inode;
70 if ((fops = file->f_op) && (select = fops->select))
71 return select(inode, file, flag, wait)
72 || (wait && select(inode, file, flag, NULL));
73 if (S_ISREG(inode->i_mode))
74 return 1;
75 return 0;
76 }
77
78 int do_select(int n, fd_set *in, fd_set *out, fd_set *ex,
79 fd_set *res_in, fd_set *res_out, fd_set *res_ex)
80 {
81 int count;
82 select_table wait_table, *wait;
83 struct select_table_entry *entry;
84 int i;
85 int max;
86
87 max = -1;
88 for (i = 0 ; i < n ; i++) {
89 if (!FD_ISSET(i, in) &&
90 !FD_ISSET(i, out) &&
91 !FD_ISSET(i, ex))
92 continue;
93 if (!current->filp[i])
94 return -EBADF;
95 if (!current->filp[i]->f_inode)
96 return -EBADF;
97 max = i;
98 }
99 n = max + 1;
100 entry = (struct select_table_entry *) get_free_page(GFP_KERNEL);
101 if (!entry)
102 return -ENOMEM;
103 FD_ZERO(res_in);
104 FD_ZERO(res_out);
105 FD_ZERO(res_ex);
106 count = 0;
107 wait_table.nr = 0;
108 wait_table.entry = entry;
109 wait = &wait_table;
110 repeat:
111 current->state = TASK_INTERRUPTIBLE;
112 for (i = 0 ; i < n ; i++) {
113 if (FD_ISSET(i,in) && check(SEL_IN,wait,current->filp[i])) {
114 FD_SET(i, res_in);
115 count++;
116 wait = NULL;
117 }
118 if (FD_ISSET(i,out) && check(SEL_OUT,wait,current->filp[i])) {
119 FD_SET(i, res_out);
120 count++;
121 wait = NULL;
122 }
123 if (FD_ISSET(i,ex) && check(SEL_EX,wait,current->filp[i])) {
124 FD_SET(i, res_ex);
125 count++;
126 wait = NULL;
127 }
128 }
129 wait = NULL;
130 if (!count && current->timeout && !(current->signal & ~current->blocked)) {
131 schedule();
132 goto repeat;
133 }
134 free_wait(&wait_table);
135 free_page((unsigned long) entry);
136 current->state = TASK_RUNNING;
137 return count;
138 }
139
140
141
142
143
144 static int __get_fd_set(int nr, unsigned long * fs_pointer, unsigned long * fdset)
145 {
146 int error;
147
148 FD_ZERO(fdset);
149 if (!fs_pointer)
150 return 0;
151 error = verify_area(VERIFY_WRITE,fs_pointer,sizeof(fd_set));
152 if (error)
153 return error;
154 while (nr > 0) {
155 *fdset = get_fs_long(fs_pointer);
156 fdset++;
157 fs_pointer++;
158 nr -= 32;
159 }
160 return 0;
161 }
162
163 static void __set_fd_set(int nr, unsigned long * fs_pointer, unsigned long * fdset)
164 {
165 if (!fs_pointer)
166 return;
167 while (nr > 0) {
168 put_fs_long(*fdset, fs_pointer);
169 fdset++;
170 fs_pointer++;
171 nr -= 32;
172 }
173 }
174
175 #define get_fd_set(nr,fsp,fdp) \
176 __get_fd_set(nr, (unsigned long *) (fsp), (unsigned long *) (fdp))
177
178 #define set_fd_set(nr,fsp,fdp) \
179 __set_fd_set(nr, (unsigned long *) (fsp), (unsigned long *) (fdp))
180
181
182
183
184
185
186 int sys_select( unsigned long *buffer )
187 {
188
189 int i;
190 fd_set res_in, in, *inp;
191 fd_set res_out, out, *outp;
192 fd_set res_ex, ex, *exp;
193 int n;
194 struct timeval *tvp;
195 unsigned long timeout;
196
197 i = verify_area(VERIFY_READ, buffer, 20);
198 if (i)
199 return i;
200 n = get_fs_long(buffer++);
201 if (n < 0)
202 return -EINVAL;
203 if (n > NR_OPEN)
204 n = NR_OPEN;
205 inp = (fd_set *) get_fs_long(buffer++);
206 outp = (fd_set *) get_fs_long(buffer++);
207 exp = (fd_set *) get_fs_long(buffer++);
208 tvp = (struct timeval *) get_fs_long(buffer);
209 if ((i = get_fd_set(n, inp, &in)) ||
210 (i = get_fd_set(n, outp, &out)) ||
211 (i = get_fd_set(n, exp, &ex))) return i;
212 timeout = 0xffffffff;
213 if (tvp) {
214 i = verify_area(VERIFY_WRITE, tvp, sizeof(*tvp));
215 if (i)
216 return i;
217 timeout = jiffies;
218 timeout += ROUND_UP(get_fs_long((unsigned long *)&tvp->tv_usec),(1000000/HZ));
219 timeout += get_fs_long((unsigned long *)&tvp->tv_sec) * HZ;
220 if (timeout <= jiffies)
221 timeout = 0;
222 }
223 current->timeout = timeout;
224 i = do_select(n, &in, &out, &ex, &res_in, &res_out, &res_ex);
225 if (current->timeout > jiffies)
226 timeout = current->timeout - jiffies;
227 else
228 timeout = 0;
229 current->timeout = 0;
230 if (tvp) {
231 put_fs_long(timeout/HZ, (unsigned long *) &tvp->tv_sec);
232 timeout %= HZ;
233 timeout *= (1000000/HZ);
234 put_fs_long(timeout, (unsigned long *) &tvp->tv_usec);
235 }
236 if (i < 0)
237 return i;
238 if (!i && (current->signal & ~current->blocked))
239 return -EINTR;
240 set_fd_set(n, inp, &res_in);
241 set_fd_set(n, outp, &res_out);
242 set_fd_set(n, exp, &res_ex);
243 return i;
244 }