blob: d8b4f0722b8d8cec0e58e048498ed9d366e28893 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file contains the procedures for the handling of select and poll
3 *
4 * Created for Linux based loosely upon Mathius Lattner's minix
5 * patches by Peter MacDonald. Heavily edited by Linus.
6 *
7 * 4 February 1994
8 * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
9 * flag set in its personality we do *not* modify the given timeout
10 * parameter to reflect time remaining.
11 *
12 * 24 January 2000
13 * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
14 * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
15 */
16
17#include <linux/syscalls.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/smp_lock.h>
21#include <linux/poll.h>
22#include <linux/personality.h> /* for STICKY_TIMEOUTS */
23#include <linux/file.h>
24#include <linux/fs.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070025#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/uaccess.h>
28
29#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
30#define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct poll_table_page {
33 struct poll_table_page * next;
34 struct poll_table_entry * entry;
35 struct poll_table_entry entries[0];
36};
37
38#define POLL_TABLE_FULL(table) \
39 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
40
41/*
42 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
43 * I have rewritten this, taking some shortcuts: This code may not be easy to
44 * follow, but it should be free of race-conditions, and it's practical. If you
45 * understand what I'm doing here, then you understand how the linux
46 * sleep/wakeup mechanism works.
47 *
48 * Two very simple procedures, poll_wait() and poll_freewait() make all the
49 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
50 * as all select/poll functions have to call it to add an entry to the
51 * poll table.
52 */
Adrian Bunk75c96f82005-05-05 16:16:09 -070053static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
54 poll_table *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56void poll_initwait(struct poll_wqueues *pwq)
57{
58 init_poll_funcptr(&pwq->pt, __pollwait);
59 pwq->error = 0;
60 pwq->table = NULL;
Andi Kleen70674f92006-03-28 01:56:33 -080061 pwq->inline_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64EXPORT_SYMBOL(poll_initwait);
65
Andi Kleen70674f92006-03-28 01:56:33 -080066static void free_poll_entry(struct poll_table_entry *entry)
67{
68 remove_wait_queue(entry->wait_address,&entry->wait);
69 fput(entry->filp);
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072void poll_freewait(struct poll_wqueues *pwq)
73{
74 struct poll_table_page * p = pwq->table;
Andi Kleen70674f92006-03-28 01:56:33 -080075 int i;
76 for (i = 0; i < pwq->inline_index; i++)
77 free_poll_entry(pwq->inline_entries + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 while (p) {
79 struct poll_table_entry * entry;
80 struct poll_table_page *old;
81
82 entry = p->entry;
83 do {
84 entry--;
Andi Kleen70674f92006-03-28 01:56:33 -080085 free_poll_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 } while (entry > p->entries);
87 old = p;
88 p = p->next;
89 free_page((unsigned long) old);
90 }
91}
92
93EXPORT_SYMBOL(poll_freewait);
94
Andi Kleen70674f92006-03-28 01:56:33 -080095static struct poll_table_entry *poll_get_entry(poll_table *_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
98 struct poll_table_page *table = p->table;
99
Andi Kleen70674f92006-03-28 01:56:33 -0800100 if (p->inline_index < N_INLINE_POLL_ENTRIES)
101 return p->inline_entries + p->inline_index++;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!table || POLL_TABLE_FULL(table)) {
104 struct poll_table_page *new_table;
105
106 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
107 if (!new_table) {
108 p->error = -ENOMEM;
109 __set_current_state(TASK_RUNNING);
Andi Kleen70674f92006-03-28 01:56:33 -0800110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 new_table->entry = new_table->entries;
113 new_table->next = table;
114 p->table = new_table;
115 table = new_table;
116 }
117
Andi Kleen70674f92006-03-28 01:56:33 -0800118 return table->entry++;
119}
120
121/* Add a new entry */
122static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
123 poll_table *p)
124{
125 struct poll_table_entry *entry = poll_get_entry(p);
126 if (!entry)
127 return;
128 get_file(filp);
129 entry->filp = filp;
130 entry->wait_address = wait_address;
131 init_waitqueue_entry(&entry->wait, current);
132 add_wait_queue(wait_address,&entry->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135#define FDS_IN(fds, n) (fds->in + n)
136#define FDS_OUT(fds, n) (fds->out + n)
137#define FDS_EX(fds, n) (fds->ex + n)
138
139#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
140
141static int max_select_fd(unsigned long n, fd_set_bits *fds)
142{
143 unsigned long *open_fds;
144 unsigned long set;
145 int max;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700146 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* handle last in-complete long-word first */
149 set = ~(~0UL << (n & (__NFDBITS-1)));
150 n /= __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700151 fdt = files_fdtable(current->files);
152 open_fds = fdt->open_fds->fds_bits+n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 max = 0;
154 if (set) {
155 set &= BITS(fds, n);
156 if (set) {
157 if (!(set & ~*open_fds))
158 goto get_max;
159 return -EBADF;
160 }
161 }
162 while (n) {
163 open_fds--;
164 n--;
165 set = BITS(fds, n);
166 if (!set)
167 continue;
168 if (set & ~*open_fds)
169 return -EBADF;
170 if (max)
171 continue;
172get_max:
173 do {
174 max++;
175 set >>= 1;
176 } while (set);
177 max += n * __NFDBITS;
178 }
179
180 return max;
181}
182
183#define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
184#define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
185#define ISSET(i,m) (((i)&*(m)) != 0)
186#define SET(i,m) (*(m) |= (i))
187
188#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
189#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
190#define POLLEX_SET (POLLPRI)
191
David Woodhouse9f729492006-01-18 17:44:05 -0800192int do_select(int n, fd_set_bits *fds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct poll_wqueues table;
195 poll_table *wait;
196 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dipankar Sarmab8359962005-09-09 13:04:14 -0700198 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 retval = max_select_fd(n, fds);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (retval < 0)
203 return retval;
204 n = retval;
205
206 poll_initwait(&table);
207 wait = &table.pt;
David Woodhouse9f729492006-01-18 17:44:05 -0800208 if (!*timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 wait = NULL;
210 retval = 0;
211 for (;;) {
212 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
David Woodhouse9f729492006-01-18 17:44:05 -0800213 long __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 set_current_state(TASK_INTERRUPTIBLE);
216
217 inp = fds->in; outp = fds->out; exp = fds->ex;
218 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
219
220 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
221 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
222 unsigned long res_in = 0, res_out = 0, res_ex = 0;
223 struct file_operations *f_op = NULL;
224 struct file *file = NULL;
225
226 in = *inp++; out = *outp++; ex = *exp++;
227 all_bits = in | out | ex;
228 if (all_bits == 0) {
229 i += __NFDBITS;
230 continue;
231 }
232
233 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
234 if (i >= n)
235 break;
236 if (!(bit & all_bits))
237 continue;
238 file = fget(i);
239 if (file) {
240 f_op = file->f_op;
241 mask = DEFAULT_POLLMASK;
242 if (f_op && f_op->poll)
243 mask = (*f_op->poll)(file, retval ? NULL : wait);
244 fput(file);
245 if ((mask & POLLIN_SET) && (in & bit)) {
246 res_in |= bit;
247 retval++;
248 }
249 if ((mask & POLLOUT_SET) && (out & bit)) {
250 res_out |= bit;
251 retval++;
252 }
253 if ((mask & POLLEX_SET) && (ex & bit)) {
254 res_ex |= bit;
255 retval++;
256 }
257 }
258 cond_resched();
259 }
260 if (res_in)
261 *rinp = res_in;
262 if (res_out)
263 *routp = res_out;
264 if (res_ex)
265 *rexp = res_ex;
266 }
267 wait = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800268 if (retval || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270 if(table.error) {
271 retval = table.error;
272 break;
273 }
David Woodhouse9f729492006-01-18 17:44:05 -0800274
275 if (*timeout < 0) {
276 /* Wait indefinitely */
277 __timeout = MAX_SCHEDULE_TIMEOUT;
278 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
279 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
280 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
281 *timeout -= __timeout;
282 } else {
283 __timeout = *timeout;
284 *timeout = 0;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 __timeout = schedule_timeout(__timeout);
David Woodhouse9f729492006-01-18 17:44:05 -0800287 if (*timeout >= 0)
288 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 __set_current_state(TASK_RUNNING);
291
292 poll_freewait(&table);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return retval;
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * We can actually return ERESTARTSYS instead of EINTR, but I'd
299 * like to be certain this leads to no problems. So I return
300 * EINTR just for safety.
301 *
302 * Update: ERESTARTSYS breaks at least the xview clock binary, so
303 * I'm trying ERESTARTNOHAND which restart only when you want to.
304 */
305#define MAX_SELECT_SECONDS \
306 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
307
David Woodhouse9f729492006-01-18 17:44:05 -0800308static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
309 fd_set __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 fd_set_bits fds;
312 char *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int ret, size, max_fdset;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700314 struct fdtable *fdt;
Andi Kleen70674f92006-03-28 01:56:33 -0800315 /* Allocate small arguments on the stack to save memory and be faster */
316 char stack_fds[SELECT_STACK_ALLOC];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ret = -EINVAL;
319 if (n < 0)
320 goto out_nofds;
321
322 /* max_fdset can increase, so grab it once to avoid race */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700323 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700324 fdt = files_fdtable(current->files);
325 max_fdset = fdt->max_fdset;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (n > max_fdset)
328 n = max_fdset;
329
330 /*
331 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
332 * since we used fdset we need to allocate memory in units of
333 * long-words.
334 */
335 ret = -ENOMEM;
336 size = FDS_BYTES(n);
Andi Kleen70674f92006-03-28 01:56:33 -0800337 if (6*size < SELECT_STACK_ALLOC)
338 bits = stack_fds;
339 else
340 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!bits)
342 goto out_nofds;
343 fds.in = (unsigned long *) bits;
344 fds.out = (unsigned long *) (bits + size);
345 fds.ex = (unsigned long *) (bits + 2*size);
346 fds.res_in = (unsigned long *) (bits + 3*size);
347 fds.res_out = (unsigned long *) (bits + 4*size);
348 fds.res_ex = (unsigned long *) (bits + 5*size);
349
350 if ((ret = get_fd_set(n, inp, fds.in)) ||
351 (ret = get_fd_set(n, outp, fds.out)) ||
352 (ret = get_fd_set(n, exp, fds.ex)))
353 goto out;
354 zero_fd_set(n, fds.res_in);
355 zero_fd_set(n, fds.res_out);
356 zero_fd_set(n, fds.res_ex);
357
David Woodhouse9f729492006-01-18 17:44:05 -0800358 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (ret < 0)
361 goto out;
362 if (!ret) {
363 ret = -ERESTARTNOHAND;
364 if (signal_pending(current))
365 goto out;
366 ret = 0;
367 }
368
369 if (set_fd_set(n, inp, fds.res_in) ||
370 set_fd_set(n, outp, fds.res_out) ||
371 set_fd_set(n, exp, fds.res_ex))
372 ret = -EFAULT;
373
374out:
Andi Kleen70674f92006-03-28 01:56:33 -0800375 if (bits != stack_fds)
376 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377out_nofds:
378 return ret;
379}
380
David Woodhouse9f729492006-01-18 17:44:05 -0800381asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
382 fd_set __user *exp, struct timeval __user *tvp)
383{
384 s64 timeout = -1;
385 struct timeval tv;
386 int ret;
387
388 if (tvp) {
389 if (copy_from_user(&tv, tvp, sizeof(tv)))
390 return -EFAULT;
391
392 if (tv.tv_sec < 0 || tv.tv_usec < 0)
393 return -EINVAL;
394
395 /* Cast to u64 to make GCC stop complaining */
396 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
397 timeout = -1; /* infinite */
398 else {
399 timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
400 timeout += tv.tv_sec * HZ;
401 }
402 }
403
404 ret = core_sys_select(n, inp, outp, exp, &timeout);
405
406 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800407 struct timeval rtv;
408
David Woodhouse9f729492006-01-18 17:44:05 -0800409 if (current->personality & STICKY_TIMEOUTS)
410 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800411 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
412 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800413 if (timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800414 rtv = tv;
415 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800416sticky:
417 /*
418 * If an application puts its timeval in read-only
419 * memory, we don't want the Linux-specific update to
420 * the timeval to cause a fault after the select has
421 * completed successfully. However, because we're not
422 * updating the timeval, we can't restart the system
423 * call.
424 */
425 if (ret == -ERESTARTNOHAND)
426 ret = -EINTR;
427 }
428 }
429
430 return ret;
431}
432
433#ifdef TIF_RESTORE_SIGMASK
434asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
435 fd_set __user *exp, struct timespec __user *tsp,
436 const sigset_t __user *sigmask, size_t sigsetsize)
437{
438 s64 timeout = MAX_SCHEDULE_TIMEOUT;
439 sigset_t ksigmask, sigsaved;
440 struct timespec ts;
441 int ret;
442
443 if (tsp) {
444 if (copy_from_user(&ts, tsp, sizeof(ts)))
445 return -EFAULT;
446
447 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
448 return -EINVAL;
449
450 /* Cast to u64 to make GCC stop complaining */
451 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
452 timeout = -1; /* infinite */
453 else {
454 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
455 timeout += ts.tv_sec * HZ;
456 }
457 }
458
459 if (sigmask) {
460 /* XXX: Don't preclude handling different sized sigset_t's. */
461 if (sigsetsize != sizeof(sigset_t))
462 return -EINVAL;
463 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
464 return -EFAULT;
465
466 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
467 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
468 }
469
470 ret = core_sys_select(n, inp, outp, exp, &timeout);
471
472 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800473 struct timespec rts;
474
David Woodhouse9f729492006-01-18 17:44:05 -0800475 if (current->personality & STICKY_TIMEOUTS)
476 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800477 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
478 1000;
479 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800480 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800481 rts = ts;
482 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800483sticky:
484 /*
485 * If an application puts its timeval in read-only
486 * memory, we don't want the Linux-specific update to
487 * the timeval to cause a fault after the select has
488 * completed successfully. However, because we're not
489 * updating the timeval, we can't restart the system
490 * call.
491 */
492 if (ret == -ERESTARTNOHAND)
493 ret = -EINTR;
494 }
495 }
496
497 if (ret == -ERESTARTNOHAND) {
498 /*
499 * Don't restore the signal mask yet. Let do_signal() deliver
500 * the signal on the way back to userspace, before the signal
501 * mask is restored.
502 */
503 if (sigmask) {
504 memcpy(&current->saved_sigmask, &sigsaved,
505 sizeof(sigsaved));
506 set_thread_flag(TIF_RESTORE_SIGMASK);
507 }
508 } else if (sigmask)
509 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
510
511 return ret;
512}
513
514/*
515 * Most architectures can't handle 7-argument syscalls. So we provide a
516 * 6-argument version where the sixth argument is a pointer to a structure
517 * which has a pointer to the sigset_t itself followed by a size_t containing
518 * the sigset size.
519 */
520asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
521 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
522{
523 size_t sigsetsize = 0;
524 sigset_t __user *up = NULL;
525
526 if (sig) {
527 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
Al Viroe110ab92006-02-01 05:26:09 -0500528 || __get_user(up, (sigset_t __user * __user *)sig)
David Woodhouse9f729492006-01-18 17:44:05 -0800529 || __get_user(sigsetsize,
Al Viroe110ab92006-02-01 05:26:09 -0500530 (size_t __user *)(sig+sizeof(void *))))
David Woodhouse9f729492006-01-18 17:44:05 -0800531 return -EFAULT;
532 }
533
534 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
535}
536#endif /* TIF_RESTORE_SIGMASK */
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538struct poll_list {
539 struct poll_list *next;
540 int len;
541 struct pollfd entries[0];
542};
543
544#define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
545
546static void do_pollfd(unsigned int num, struct pollfd * fdpage,
547 poll_table ** pwait, int *count)
548{
549 int i;
550
551 for (i = 0; i < num; i++) {
552 int fd;
553 unsigned int mask;
554 struct pollfd *fdp;
555
556 mask = 0;
557 fdp = fdpage+i;
558 fd = fdp->fd;
559 if (fd >= 0) {
560 struct file * file = fget(fd);
561 mask = POLLNVAL;
562 if (file != NULL) {
563 mask = DEFAULT_POLLMASK;
564 if (file->f_op && file->f_op->poll)
565 mask = file->f_op->poll(file, *pwait);
566 mask &= fdp->events | POLLERR | POLLHUP;
567 fput(file);
568 }
569 if (mask) {
570 *pwait = NULL;
571 (*count)++;
572 }
573 }
574 fdp->revents = mask;
575 }
576}
577
578static int do_poll(unsigned int nfds, struct poll_list *list,
David Woodhouse9f729492006-01-18 17:44:05 -0800579 struct poll_wqueues *wait, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 int count = 0;
582 poll_table* pt = &wait->pt;
583
David Woodhouse9f729492006-01-18 17:44:05 -0800584 /* Optimise the no-wait case */
585 if (!(*timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 pt = NULL;
587
588 for (;;) {
589 struct poll_list *walk;
David Woodhouse9f729492006-01-18 17:44:05 -0800590 long __timeout;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 set_current_state(TASK_INTERRUPTIBLE);
593 walk = list;
594 while(walk != NULL) {
595 do_pollfd( walk->len, walk->entries, &pt, &count);
596 walk = walk->next;
597 }
598 pt = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800599 if (count || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 count = wait->error;
602 if (count)
603 break;
David Woodhouse9f729492006-01-18 17:44:05 -0800604
605 if (*timeout < 0) {
606 /* Wait indefinitely */
607 __timeout = MAX_SCHEDULE_TIMEOUT;
608 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
609 /*
610 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
611 * a loop
612 */
613 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
614 *timeout -= __timeout;
615 } else {
616 __timeout = *timeout;
617 *timeout = 0;
618 }
619
620 __timeout = schedule_timeout(__timeout);
621 if (*timeout >= 0)
622 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 __set_current_state(TASK_RUNNING);
625 return count;
626}
627
Andi Kleen70674f92006-03-28 01:56:33 -0800628#define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
629 sizeof(struct pollfd))
630
David Woodhouse9f729492006-01-18 17:44:05 -0800631int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct poll_wqueues table;
634 int fdcount, err;
635 unsigned int i;
636 struct poll_list *head;
637 struct poll_list *walk;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700638 struct fdtable *fdt;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700639 int max_fdset;
Andi Kleen70674f92006-03-28 01:56:33 -0800640 /* Allocate small arguments on the stack to save memory and be faster */
641 char stack_pps[POLL_STACK_ALLOC];
642 struct poll_list *stack_pp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Do a sanity check on nfds ... */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700645 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700646 fdt = files_fdtable(current->files);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700647 max_fdset = fdt->max_fdset;
648 rcu_read_unlock();
649 if (nfds > max_fdset && nfds > OPEN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EINVAL;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 poll_initwait(&table);
653
654 head = NULL;
655 walk = NULL;
656 i = nfds;
657 err = -ENOMEM;
658 while(i!=0) {
659 struct poll_list *pp;
Andi Kleen70674f92006-03-28 01:56:33 -0800660 int num, size;
661 if (stack_pp == NULL)
662 num = N_STACK_PPS;
663 else
664 num = POLLFD_PER_PAGE;
665 if (num > i)
666 num = i;
667 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
668 if (!stack_pp)
669 stack_pp = pp = (struct poll_list *)stack_pps;
670 else {
671 pp = kmalloc(size, GFP_KERNEL);
672 if (!pp)
673 goto out_fds;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 pp->next=NULL;
Andi Kleen70674f92006-03-28 01:56:33 -0800676 pp->len = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (head == NULL)
678 head = pp;
679 else
680 walk->next = pp;
681
682 walk = pp;
683 if (copy_from_user(pp->entries, ufds + nfds-i,
Andi Kleen70674f92006-03-28 01:56:33 -0800684 sizeof(struct pollfd)*num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 err = -EFAULT;
686 goto out_fds;
687 }
688 i -= pp->len;
689 }
David Woodhouse9f729492006-01-18 17:44:05 -0800690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 fdcount = do_poll(nfds, head, &table, timeout);
692
693 /* OK, now copy the revents fields back to user space. */
694 walk = head;
695 err = -EFAULT;
696 while(walk != NULL) {
697 struct pollfd *fds = walk->entries;
698 int j;
699
700 for (j=0; j < walk->len; j++, ufds++) {
701 if(__put_user(fds[j].revents, &ufds->revents))
702 goto out_fds;
703 }
704 walk = walk->next;
705 }
706 err = fdcount;
707 if (!fdcount && signal_pending(current))
708 err = -EINTR;
709out_fds:
710 walk = head;
711 while(walk!=NULL) {
712 struct poll_list *pp = walk->next;
Andi Kleen70674f92006-03-28 01:56:33 -0800713 if (walk != stack_pp)
714 kfree(walk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 walk = pp;
716 }
717 poll_freewait(&table);
718 return err;
719}
David Woodhouse9f729492006-01-18 17:44:05 -0800720
721asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
722 long timeout_msecs)
723{
724 s64 timeout_jiffies = 0;
725
726 if (timeout_msecs) {
727#if HZ > 1000
728 /* We can only overflow if HZ > 1000 */
729 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
730 timeout_jiffies = -1;
731 else
732#endif
733 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
734 }
735
736 return do_sys_poll(ufds, nfds, &timeout_jiffies);
737}
738
739#ifdef TIF_RESTORE_SIGMASK
740asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
741 struct timespec __user *tsp, const sigset_t __user *sigmask,
742 size_t sigsetsize)
743{
744 sigset_t ksigmask, sigsaved;
745 struct timespec ts;
746 s64 timeout = -1;
747 int ret;
748
749 if (tsp) {
750 if (copy_from_user(&ts, tsp, sizeof(ts)))
751 return -EFAULT;
752
753 /* Cast to u64 to make GCC stop complaining */
754 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
755 timeout = -1; /* infinite */
756 else {
757 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
758 timeout += ts.tv_sec * HZ;
759 }
760 }
761
762 if (sigmask) {
763 /* XXX: Don't preclude handling different sized sigset_t's. */
764 if (sigsetsize != sizeof(sigset_t))
765 return -EINVAL;
766 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
767 return -EFAULT;
768
769 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
770 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
771 }
772
773 ret = do_sys_poll(ufds, nfds, &timeout);
774
775 /* We can restart this syscall, usually */
776 if (ret == -EINTR) {
777 /*
778 * Don't restore the signal mask yet. Let do_signal() deliver
779 * the signal on the way back to userspace, before the signal
780 * mask is restored.
781 */
782 if (sigmask) {
783 memcpy(&current->saved_sigmask, &sigsaved,
784 sizeof(sigsaved));
785 set_thread_flag(TIF_RESTORE_SIGMASK);
786 }
787 ret = -ERESTARTNOHAND;
788 } else if (sigmask)
789 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
790
791 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -0800792 struct timespec rts;
793
David Woodhouse9f729492006-01-18 17:44:05 -0800794 if (current->personality & STICKY_TIMEOUTS)
795 goto sticky;
796 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -0800797 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
798 1000;
799 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800800 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800801 rts = ts;
802 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800803 sticky:
804 /*
805 * If an application puts its timeval in read-only
806 * memory, we don't want the Linux-specific update to
807 * the timeval to cause a fault after the select has
808 * completed successfully. However, because we're not
809 * updating the timeval, we can't restart the system
810 * call.
811 */
812 if (ret == -ERESTARTNOHAND && timeout >= 0)
813 ret = -EINTR;
814 }
815 }
816
817 return ret;
818}
819#endif /* TIF_RESTORE_SIGMASK */