This source file includes following definitions.
- add_wait
- free_one_table
- free_wait
- get_tty
- check_in
- check_out
- check_ex
- do_select
- sys_select
1
2
3
4
5
6
7
8 #include <linux/fs.h>
9 #include <linux/kernel.h>
10 #include <linux/tty.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13
14 #include <asm/segment.h>
15 #include <asm/system.h>
16
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20
21 #include <const.h>
22 #include <errno.h>
23 #include <signal.h>
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 static select_table * sel_tables = NULL;
39
40 static void add_wait(struct task_struct ** wait_address, select_table * p)
41 {
42 int i;
43
44 if (!wait_address)
45 return;
46 for (i = 0 ; i < p->nr ; i++)
47 if (p->entry[i].wait_address == wait_address)
48 return;
49 current->next_wait = NULL;
50 p->entry[p->nr].wait_address = wait_address;
51 p->entry[p->nr].old_task = *wait_address;
52 *wait_address = current;
53 p->nr++;
54 }
55
56
57
58
59
60 static void free_one_table(select_table * p)
61 {
62 int i;
63 struct task_struct ** tpp;
64
65 for(tpp = &LAST_TASK ; tpp > &FIRST_TASK ; --tpp)
66 if (*tpp && ((*tpp)->next_wait == p->current))
67 (*tpp)->next_wait = NULL;
68 if (!p->nr)
69 return;
70 for (i = 0; i < p->nr ; i++) {
71 wake_up(p->entry[i].wait_address);
72 wake_up(&p->entry[i].old_task);
73 }
74 p->nr = 0;
75 }
76
77 static void free_wait(select_table * p)
78 {
79 select_table * tmp;
80
81 if (p->woken)
82 return;
83 p = sel_tables;
84 sel_tables = NULL;
85 while (p) {
86 wake_up(&p->current);
87 p->woken = 1;
88 tmp = p->next_table;
89 p->next_table = NULL;
90 free_one_table(p);
91 p = tmp;
92 }
93 }
94
95 static struct tty_struct * get_tty(struct inode * inode)
96 {
97 int major, minor;
98
99 if (!S_ISCHR(inode->i_mode))
100 return NULL;
101 if ((major = MAJOR(inode->i_rdev)) != 5 && major != 4)
102 return NULL;
103 if (major == 5)
104 minor = current->tty;
105 else
106 minor = MINOR(inode->i_rdev);
107 if (minor < 0)
108 return NULL;
109 return TTY_TABLE(minor);
110 }
111
112
113
114
115
116 static int check_in(select_table * wait, struct inode * inode)
117 {
118 struct tty_struct * tty;
119
120 if (tty = get_tty(inode))
121 if (!EMPTY(tty->secondary))
122 return 1;
123 else
124 add_wait(&tty->secondary->proc_list, wait);
125 else if (inode->i_pipe)
126 if (!PIPE_EMPTY(*inode) || inode->i_count < 2)
127 return 1;
128 else
129 add_wait(&inode->i_wait, wait);
130 return 0;
131 }
132
133 static int check_out(select_table * wait, struct inode * inode)
134 {
135 struct tty_struct * tty;
136
137 if (tty = get_tty(inode))
138 if (!FULL(tty->write_q))
139 return 1;
140 else
141 add_wait(&tty->write_q->proc_list, wait);
142 else if (inode->i_pipe)
143 if (!PIPE_FULL(*inode))
144 return 1;
145 else
146 add_wait(&inode->i_wait, wait);
147 return 0;
148 }
149
150 static int check_ex(select_table * wait, struct inode * inode)
151 {
152 struct tty_struct * tty;
153
154 if (tty = get_tty(inode))
155 if (!FULL(tty->write_q))
156 return 0;
157 else
158 return 0;
159 else if (inode->i_pipe)
160 if (inode->i_count < 2)
161 return 1;
162 else
163 add_wait(&inode->i_wait,wait);
164 return 0;
165 }
166
167 int do_select(fd_set in, fd_set out, fd_set ex,
168 fd_set *inp, fd_set *outp, fd_set *exp)
169 {
170 int count;
171 select_table wait_table;
172 int i;
173 fd_set mask;
174
175 mask = in | out | ex;
176 for (i = 0 ; i < NR_OPEN ; i++,mask >>= 1) {
177 if (!(mask & 1))
178 continue;
179 if (!current->filp[i])
180 return -EBADF;
181 if (!current->filp[i]->f_inode)
182 return -EBADF;
183 if (current->filp[i]->f_inode->i_pipe)
184 continue;
185 if (S_ISCHR(current->filp[i]->f_inode->i_mode))
186 continue;
187 if (S_ISFIFO(current->filp[i]->f_inode->i_mode))
188 continue;
189 return -EBADF;
190 }
191 repeat:
192 wait_table.nr = 0;
193 wait_table.woken = 0;
194 wait_table.current = current;
195 wait_table.next_table = sel_tables;
196 sel_tables = &wait_table;
197 *inp = *outp = *exp = 0;
198 count = 0;
199 mask = 1;
200 for (i = 0 ; i < NR_OPEN ; i++, mask += mask) {
201 if (mask & in)
202 if (check_in(&wait_table,current->filp[i]->f_inode)) {
203 *inp |= mask;
204 count++;
205 }
206 if (mask & out)
207 if (check_out(&wait_table,current->filp[i]->f_inode)) {
208 *outp |= mask;
209 count++;
210 }
211 if (mask & ex)
212 if (check_ex(&wait_table,current->filp[i]->f_inode)) {
213 *exp |= mask;
214 count++;
215 }
216 }
217 if (!(current->signal & ~current->blocked) &&
218 current->timeout && !count) {
219 current->state = TASK_INTERRUPTIBLE;
220 sti();
221 schedule();
222 cli();
223 free_wait(&wait_table);
224 goto repeat;
225 }
226 free_wait(&wait_table);
227 return count;
228 }
229
230
231
232
233
234
235 int sys_select( unsigned long *buffer )
236 {
237
238 int i;
239 fd_set res_in, in = 0, *inp;
240 fd_set res_out, out = 0, *outp;
241 fd_set res_ex, ex = 0, *exp;
242 fd_set mask;
243 struct timeval *tvp;
244 unsigned long timeout;
245
246 mask = get_fs_long(buffer++);
247 if (mask >= 32)
248 mask = ~0;
249 else
250 mask = ~((~0) << mask);
251 inp = (fd_set *) get_fs_long(buffer++);
252 outp = (fd_set *) get_fs_long(buffer++);
253 exp = (fd_set *) get_fs_long(buffer++);
254 tvp = (struct timeval *) get_fs_long(buffer);
255
256 if (inp)
257 in = mask & get_fs_long(inp);
258 if (outp)
259 out = mask & get_fs_long(outp);
260 if (exp)
261 ex = mask & get_fs_long(exp);
262 timeout = 0xffffffff;
263 if (tvp) {
264 timeout = get_fs_long((unsigned long *)&tvp->tv_usec)/(1000000/HZ);
265 timeout += get_fs_long((unsigned long *)&tvp->tv_sec) * HZ;
266 timeout += jiffies;
267 }
268 current->timeout = timeout;
269 cli();
270 i = do_select(in, out, ex, &res_in, &res_out, &res_ex);
271 if (current->timeout > jiffies)
272 timeout = current->timeout - jiffies;
273 else
274 timeout = 0;
275 sti();
276 current->timeout = 0;
277 if (i < 0)
278 return i;
279 if (inp) {
280 verify_area(inp, 4);
281 put_fs_long(res_in,inp);
282 }
283 if (outp) {
284 verify_area(outp,4);
285 put_fs_long(res_out,outp);
286 }
287 if (exp) {
288 verify_area(exp,4);
289 put_fs_long(res_ex,exp);
290 }
291 if (tvp) {
292 verify_area(tvp, sizeof(*tvp));
293 put_fs_long(timeout/HZ, (unsigned long *) &tvp->tv_sec);
294 timeout %= HZ;
295 timeout *= (1000000/HZ);
296 put_fs_long(timeout, (unsigned long *) &tvp->tv_usec);
297 }
298 if (i)
299 return i;
300 if (current->signal & ~current->blocked)
301 return -EINTR;
302 return 0;
303 }