blob: fce0fd1bb1d1210feeda5fbb328c064c5ca007f9 [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;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800223 const struct file_operations *f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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) {
Eric Dumazete4a1f122006-03-28 01:56:34 -0800234 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (i >= n)
236 break;
237 if (!(bit & all_bits))
238 continue;
Eric Dumazete4a1f122006-03-28 01:56:34 -0800239 file = fget_light(i, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (file) {
241 f_op = file->f_op;
242 mask = DEFAULT_POLLMASK;
243 if (f_op && f_op->poll)
244 mask = (*f_op->poll)(file, retval ? NULL : wait);
Eric Dumazete4a1f122006-03-28 01:56:34 -0800245 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if ((mask & POLLIN_SET) && (in & bit)) {
247 res_in |= bit;
248 retval++;
249 }
250 if ((mask & POLLOUT_SET) && (out & bit)) {
251 res_out |= bit;
252 retval++;
253 }
254 if ((mask & POLLEX_SET) && (ex & bit)) {
255 res_ex |= bit;
256 retval++;
257 }
258 }
259 cond_resched();
260 }
261 if (res_in)
262 *rinp = res_in;
263 if (res_out)
264 *routp = res_out;
265 if (res_ex)
266 *rexp = res_ex;
267 }
268 wait = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800269 if (retval || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 if(table.error) {
272 retval = table.error;
273 break;
274 }
David Woodhouse9f729492006-01-18 17:44:05 -0800275
276 if (*timeout < 0) {
277 /* Wait indefinitely */
278 __timeout = MAX_SCHEDULE_TIMEOUT;
279 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
280 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
281 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
282 *timeout -= __timeout;
283 } else {
284 __timeout = *timeout;
285 *timeout = 0;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __timeout = schedule_timeout(__timeout);
David Woodhouse9f729492006-01-18 17:44:05 -0800288 if (*timeout >= 0)
289 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 __set_current_state(TASK_RUNNING);
292
293 poll_freewait(&table);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return retval;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * We can actually return ERESTARTSYS instead of EINTR, but I'd
300 * like to be certain this leads to no problems. So I return
301 * EINTR just for safety.
302 *
303 * Update: ERESTARTSYS breaks at least the xview clock binary, so
304 * I'm trying ERESTARTNOHAND which restart only when you want to.
305 */
306#define MAX_SELECT_SECONDS \
307 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
308
David Woodhouse9f729492006-01-18 17:44:05 -0800309static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
310 fd_set __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 fd_set_bits fds;
Andrew Morton29ff2db2006-04-10 22:52:46 -0700313 void *bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int ret, size, max_fdset;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700315 struct fdtable *fdt;
Andi Kleen70674f92006-03-28 01:56:33 -0800316 /* Allocate small arguments on the stack to save memory and be faster */
Jes Sorensen30c14e42006-03-31 11:18:57 -0500317 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 ret = -EINVAL;
320 if (n < 0)
321 goto out_nofds;
322
323 /* max_fdset can increase, so grab it once to avoid race */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700324 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700325 fdt = files_fdtable(current->files);
326 max_fdset = fdt->max_fdset;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700327 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (n > max_fdset)
329 n = max_fdset;
330
331 /*
332 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
333 * since we used fdset we need to allocate memory in units of
334 * long-words.
335 */
336 ret = -ENOMEM;
337 size = FDS_BYTES(n);
Andi Kleen70674f92006-03-28 01:56:33 -0800338 if (6*size < SELECT_STACK_ALLOC)
339 bits = stack_fds;
340 else
341 bits = kmalloc(6 * size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!bits)
343 goto out_nofds;
Andrew Morton29ff2db2006-04-10 22:52:46 -0700344 fds.in = bits;
345 fds.out = bits + size;
346 fds.ex = bits + 2*size;
347 fds.res_in = bits + 3*size;
348 fds.res_out = bits + 4*size;
349 fds.res_ex = bits + 5*size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if ((ret = get_fd_set(n, inp, fds.in)) ||
352 (ret = get_fd_set(n, outp, fds.out)) ||
353 (ret = get_fd_set(n, exp, fds.ex)))
354 goto out;
355 zero_fd_set(n, fds.res_in);
356 zero_fd_set(n, fds.res_out);
357 zero_fd_set(n, fds.res_ex);
358
David Woodhouse9f729492006-01-18 17:44:05 -0800359 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (ret < 0)
362 goto out;
363 if (!ret) {
364 ret = -ERESTARTNOHAND;
365 if (signal_pending(current))
366 goto out;
367 ret = 0;
368 }
369
370 if (set_fd_set(n, inp, fds.res_in) ||
371 set_fd_set(n, outp, fds.res_out) ||
372 set_fd_set(n, exp, fds.res_ex))
373 ret = -EFAULT;
374
375out:
Andi Kleen70674f92006-03-28 01:56:33 -0800376 if (bits != stack_fds)
377 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378out_nofds:
379 return ret;
380}
381
David Woodhouse9f729492006-01-18 17:44:05 -0800382asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
383 fd_set __user *exp, struct timeval __user *tvp)
384{
385 s64 timeout = -1;
386 struct timeval tv;
387 int ret;
388
389 if (tvp) {
390 if (copy_from_user(&tv, tvp, sizeof(tv)))
391 return -EFAULT;
392
393 if (tv.tv_sec < 0 || tv.tv_usec < 0)
394 return -EINVAL;
395
396 /* Cast to u64 to make GCC stop complaining */
397 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
398 timeout = -1; /* infinite */
399 else {
400 timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
401 timeout += tv.tv_sec * HZ;
402 }
403 }
404
405 ret = core_sys_select(n, inp, outp, exp, &timeout);
406
407 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800408 struct timeval rtv;
409
David Woodhouse9f729492006-01-18 17:44:05 -0800410 if (current->personality & STICKY_TIMEOUTS)
411 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800412 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
413 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800414 if (timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800415 rtv = tv;
416 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800417sticky:
418 /*
419 * If an application puts its timeval in read-only
420 * memory, we don't want the Linux-specific update to
421 * the timeval to cause a fault after the select has
422 * completed successfully. However, because we're not
423 * updating the timeval, we can't restart the system
424 * call.
425 */
426 if (ret == -ERESTARTNOHAND)
427 ret = -EINTR;
428 }
429 }
430
431 return ret;
432}
433
434#ifdef TIF_RESTORE_SIGMASK
435asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
436 fd_set __user *exp, struct timespec __user *tsp,
437 const sigset_t __user *sigmask, size_t sigsetsize)
438{
439 s64 timeout = MAX_SCHEDULE_TIMEOUT;
440 sigset_t ksigmask, sigsaved;
441 struct timespec ts;
442 int ret;
443
444 if (tsp) {
445 if (copy_from_user(&ts, tsp, sizeof(ts)))
446 return -EFAULT;
447
448 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
449 return -EINVAL;
450
451 /* Cast to u64 to make GCC stop complaining */
452 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
453 timeout = -1; /* infinite */
454 else {
455 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
456 timeout += ts.tv_sec * HZ;
457 }
458 }
459
460 if (sigmask) {
461 /* XXX: Don't preclude handling different sized sigset_t's. */
462 if (sigsetsize != sizeof(sigset_t))
463 return -EINVAL;
464 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
465 return -EFAULT;
466
467 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
468 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
469 }
470
471 ret = core_sys_select(n, inp, outp, exp, &timeout);
472
473 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800474 struct timespec rts;
475
David Woodhouse9f729492006-01-18 17:44:05 -0800476 if (current->personality & STICKY_TIMEOUTS)
477 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800478 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
479 1000;
480 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800481 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800482 rts = ts;
483 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800484sticky:
485 /*
486 * If an application puts its timeval in read-only
487 * memory, we don't want the Linux-specific update to
488 * the timeval to cause a fault after the select has
489 * completed successfully. However, because we're not
490 * updating the timeval, we can't restart the system
491 * call.
492 */
493 if (ret == -ERESTARTNOHAND)
494 ret = -EINTR;
495 }
496 }
497
498 if (ret == -ERESTARTNOHAND) {
499 /*
500 * Don't restore the signal mask yet. Let do_signal() deliver
501 * the signal on the way back to userspace, before the signal
502 * mask is restored.
503 */
504 if (sigmask) {
505 memcpy(&current->saved_sigmask, &sigsaved,
506 sizeof(sigsaved));
507 set_thread_flag(TIF_RESTORE_SIGMASK);
508 }
509 } else if (sigmask)
510 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
511
512 return ret;
513}
514
515/*
516 * Most architectures can't handle 7-argument syscalls. So we provide a
517 * 6-argument version where the sixth argument is a pointer to a structure
518 * which has a pointer to the sigset_t itself followed by a size_t containing
519 * the sigset size.
520 */
521asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
522 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
523{
524 size_t sigsetsize = 0;
525 sigset_t __user *up = NULL;
526
527 if (sig) {
528 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
Al Viroe110ab92006-02-01 05:26:09 -0500529 || __get_user(up, (sigset_t __user * __user *)sig)
David Woodhouse9f729492006-01-18 17:44:05 -0800530 || __get_user(sigsetsize,
Al Viroe110ab92006-02-01 05:26:09 -0500531 (size_t __user *)(sig+sizeof(void *))))
David Woodhouse9f729492006-01-18 17:44:05 -0800532 return -EFAULT;
533 }
534
535 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
536}
537#endif /* TIF_RESTORE_SIGMASK */
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539struct poll_list {
540 struct poll_list *next;
541 int len;
542 struct pollfd entries[0];
543};
544
545#define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
546
547static void do_pollfd(unsigned int num, struct pollfd * fdpage,
548 poll_table ** pwait, int *count)
549{
550 int i;
551
552 for (i = 0; i < num; i++) {
553 int fd;
554 unsigned int mask;
555 struct pollfd *fdp;
556
557 mask = 0;
558 fdp = fdpage+i;
559 fd = fdp->fd;
560 if (fd >= 0) {
Eric Dumazete4a1f122006-03-28 01:56:34 -0800561 int fput_needed;
562 struct file * file = fget_light(fd, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 mask = POLLNVAL;
564 if (file != NULL) {
565 mask = DEFAULT_POLLMASK;
566 if (file->f_op && file->f_op->poll)
567 mask = file->f_op->poll(file, *pwait);
568 mask &= fdp->events | POLLERR | POLLHUP;
Eric Dumazete4a1f122006-03-28 01:56:34 -0800569 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 if (mask) {
572 *pwait = NULL;
573 (*count)++;
574 }
575 }
576 fdp->revents = mask;
577 }
578}
579
580static int do_poll(unsigned int nfds, struct poll_list *list,
David Woodhouse9f729492006-01-18 17:44:05 -0800581 struct poll_wqueues *wait, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int count = 0;
584 poll_table* pt = &wait->pt;
585
David Woodhouse9f729492006-01-18 17:44:05 -0800586 /* Optimise the no-wait case */
587 if (!(*timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 pt = NULL;
589
590 for (;;) {
591 struct poll_list *walk;
David Woodhouse9f729492006-01-18 17:44:05 -0800592 long __timeout;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 set_current_state(TASK_INTERRUPTIBLE);
595 walk = list;
596 while(walk != NULL) {
597 do_pollfd( walk->len, walk->entries, &pt, &count);
598 walk = walk->next;
599 }
600 pt = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800601 if (count || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 count = wait->error;
604 if (count)
605 break;
David Woodhouse9f729492006-01-18 17:44:05 -0800606
607 if (*timeout < 0) {
608 /* Wait indefinitely */
609 __timeout = MAX_SCHEDULE_TIMEOUT;
610 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
611 /*
612 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
613 * a loop
614 */
615 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
616 *timeout -= __timeout;
617 } else {
618 __timeout = *timeout;
619 *timeout = 0;
620 }
621
622 __timeout = schedule_timeout(__timeout);
623 if (*timeout >= 0)
624 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 __set_current_state(TASK_RUNNING);
627 return count;
628}
629
Andi Kleen70674f92006-03-28 01:56:33 -0800630#define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
631 sizeof(struct pollfd))
632
David Woodhouse9f729492006-01-18 17:44:05 -0800633int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct poll_wqueues table;
636 int fdcount, err;
637 unsigned int i;
638 struct poll_list *head;
639 struct poll_list *walk;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700640 struct fdtable *fdt;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700641 int max_fdset;
Jes Sorensen30c14e42006-03-31 11:18:57 -0500642 /* Allocate small arguments on the stack to save memory and be
643 faster - use long to make sure the buffer is aligned properly
644 on 64 bit archs to avoid unaligned access */
645 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
Andi Kleen70674f92006-03-28 01:56:33 -0800646 struct poll_list *stack_pp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* Do a sanity check on nfds ... */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700649 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700650 fdt = files_fdtable(current->files);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700651 max_fdset = fdt->max_fdset;
652 rcu_read_unlock();
653 if (nfds > max_fdset && nfds > OPEN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 poll_initwait(&table);
657
658 head = NULL;
659 walk = NULL;
660 i = nfds;
661 err = -ENOMEM;
662 while(i!=0) {
663 struct poll_list *pp;
Andi Kleen70674f92006-03-28 01:56:33 -0800664 int num, size;
665 if (stack_pp == NULL)
666 num = N_STACK_PPS;
667 else
668 num = POLLFD_PER_PAGE;
669 if (num > i)
670 num = i;
671 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
672 if (!stack_pp)
673 stack_pp = pp = (struct poll_list *)stack_pps;
674 else {
675 pp = kmalloc(size, GFP_KERNEL);
676 if (!pp)
677 goto out_fds;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pp->next=NULL;
Andi Kleen70674f92006-03-28 01:56:33 -0800680 pp->len = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (head == NULL)
682 head = pp;
683 else
684 walk->next = pp;
685
686 walk = pp;
687 if (copy_from_user(pp->entries, ufds + nfds-i,
Andi Kleen70674f92006-03-28 01:56:33 -0800688 sizeof(struct pollfd)*num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 err = -EFAULT;
690 goto out_fds;
691 }
692 i -= pp->len;
693 }
David Woodhouse9f729492006-01-18 17:44:05 -0800694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 fdcount = do_poll(nfds, head, &table, timeout);
696
697 /* OK, now copy the revents fields back to user space. */
698 walk = head;
699 err = -EFAULT;
700 while(walk != NULL) {
701 struct pollfd *fds = walk->entries;
702 int j;
703
704 for (j=0; j < walk->len; j++, ufds++) {
705 if(__put_user(fds[j].revents, &ufds->revents))
706 goto out_fds;
707 }
708 walk = walk->next;
709 }
710 err = fdcount;
711 if (!fdcount && signal_pending(current))
712 err = -EINTR;
713out_fds:
714 walk = head;
715 while(walk!=NULL) {
716 struct poll_list *pp = walk->next;
Andi Kleen70674f92006-03-28 01:56:33 -0800717 if (walk != stack_pp)
718 kfree(walk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 walk = pp;
720 }
721 poll_freewait(&table);
722 return err;
723}
David Woodhouse9f729492006-01-18 17:44:05 -0800724
725asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
726 long timeout_msecs)
727{
728 s64 timeout_jiffies = 0;
729
730 if (timeout_msecs) {
731#if HZ > 1000
732 /* We can only overflow if HZ > 1000 */
733 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
734 timeout_jiffies = -1;
735 else
736#endif
737 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
738 }
739
740 return do_sys_poll(ufds, nfds, &timeout_jiffies);
741}
742
743#ifdef TIF_RESTORE_SIGMASK
744asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
745 struct timespec __user *tsp, const sigset_t __user *sigmask,
746 size_t sigsetsize)
747{
748 sigset_t ksigmask, sigsaved;
749 struct timespec ts;
750 s64 timeout = -1;
751 int ret;
752
753 if (tsp) {
754 if (copy_from_user(&ts, tsp, sizeof(ts)))
755 return -EFAULT;
756
757 /* Cast to u64 to make GCC stop complaining */
758 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
759 timeout = -1; /* infinite */
760 else {
761 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
762 timeout += ts.tv_sec * HZ;
763 }
764 }
765
766 if (sigmask) {
767 /* XXX: Don't preclude handling different sized sigset_t's. */
768 if (sigsetsize != sizeof(sigset_t))
769 return -EINVAL;
770 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
771 return -EFAULT;
772
773 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
774 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
775 }
776
777 ret = do_sys_poll(ufds, nfds, &timeout);
778
779 /* We can restart this syscall, usually */
780 if (ret == -EINTR) {
781 /*
782 * Don't restore the signal mask yet. Let do_signal() deliver
783 * the signal on the way back to userspace, before the signal
784 * mask is restored.
785 */
786 if (sigmask) {
787 memcpy(&current->saved_sigmask, &sigsaved,
788 sizeof(sigsaved));
789 set_thread_flag(TIF_RESTORE_SIGMASK);
790 }
791 ret = -ERESTARTNOHAND;
792 } else if (sigmask)
793 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
794
795 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -0800796 struct timespec rts;
797
David Woodhouse9f729492006-01-18 17:44:05 -0800798 if (current->personality & STICKY_TIMEOUTS)
799 goto sticky;
800 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -0800801 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
802 1000;
803 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800804 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800805 rts = ts;
806 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800807 sticky:
808 /*
809 * If an application puts its timeval in read-only
810 * memory, we don't want the Linux-specific update to
811 * the timeval to cause a fault after the select has
812 * completed successfully. However, because we're not
813 * updating the timeval, we can't restart the system
814 * call.
815 */
816 if (ret == -ERESTARTNOHAND && timeout >= 0)
817 ret = -EINTR;
818 }
819 }
820
821 return ret;
822}
823#endif /* TIF_RESTORE_SIGMASK */