blob: a974082b0824a8a9935abd42c3471b50a9755410 [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
Milind Arun Choudhary022a1692007-05-08 00:29:02 -070017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/syscalls.h>
19#include <linux/module.h>
20#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct poll_table_page {
32 struct poll_table_page * next;
33 struct poll_table_entry * entry;
34 struct poll_table_entry entries[0];
35};
36
37#define POLL_TABLE_FULL(table) \
38 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
39
40/*
41 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
42 * I have rewritten this, taking some shortcuts: This code may not be easy to
43 * follow, but it should be free of race-conditions, and it's practical. If you
44 * understand what I'm doing here, then you understand how the linux
45 * sleep/wakeup mechanism works.
46 *
47 * Two very simple procedures, poll_wait() and poll_freewait() make all the
48 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
49 * as all select/poll functions have to call it to add an entry to the
50 * poll table.
51 */
Adrian Bunk75c96f82005-05-05 16:16:09 -070052static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
53 poll_table *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55void poll_initwait(struct poll_wqueues *pwq)
56{
57 init_poll_funcptr(&pwq->pt, __pollwait);
58 pwq->error = 0;
59 pwq->table = NULL;
Andi Kleen70674f92006-03-28 01:56:33 -080060 pwq->inline_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63EXPORT_SYMBOL(poll_initwait);
64
Andi Kleen70674f92006-03-28 01:56:33 -080065static void free_poll_entry(struct poll_table_entry *entry)
66{
WANG Congccf67802007-05-09 07:10:02 +020067 remove_wait_queue(entry->wait_address, &entry->wait);
Andi Kleen70674f92006-03-28 01:56:33 -080068 fput(entry->filp);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071void poll_freewait(struct poll_wqueues *pwq)
72{
73 struct poll_table_page * p = pwq->table;
Andi Kleen70674f92006-03-28 01:56:33 -080074 int i;
75 for (i = 0; i < pwq->inline_index; i++)
76 free_poll_entry(pwq->inline_entries + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 while (p) {
78 struct poll_table_entry * entry;
79 struct poll_table_page *old;
80
81 entry = p->entry;
82 do {
83 entry--;
Andi Kleen70674f92006-03-28 01:56:33 -080084 free_poll_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 } while (entry > p->entries);
86 old = p;
87 p = p->next;
88 free_page((unsigned long) old);
89 }
90}
91
92EXPORT_SYMBOL(poll_freewait);
93
Andi Kleen70674f92006-03-28 01:56:33 -080094static struct poll_table_entry *poll_get_entry(poll_table *_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
97 struct poll_table_page *table = p->table;
98
Andi Kleen70674f92006-03-28 01:56:33 -080099 if (p->inline_index < N_INLINE_POLL_ENTRIES)
100 return p->inline_entries + p->inline_index++;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!table || POLL_TABLE_FULL(table)) {
103 struct poll_table_page *new_table;
104
105 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
106 if (!new_table) {
107 p->error = -ENOMEM;
108 __set_current_state(TASK_RUNNING);
Andi Kleen70674f92006-03-28 01:56:33 -0800109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 new_table->entry = new_table->entries;
112 new_table->next = table;
113 p->table = new_table;
114 table = new_table;
115 }
116
Andi Kleen70674f92006-03-28 01:56:33 -0800117 return table->entry++;
118}
119
120/* Add a new entry */
121static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
122 poll_table *p)
123{
124 struct poll_table_entry *entry = poll_get_entry(p);
125 if (!entry)
126 return;
127 get_file(filp);
128 entry->filp = filp;
129 entry->wait_address = wait_address;
130 init_waitqueue_entry(&entry->wait, current);
WANG Congccf67802007-05-09 07:10:02 +0200131 add_wait_queue(wait_address, &entry->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134#define FDS_IN(fds, n) (fds->in + n)
135#define FDS_OUT(fds, n) (fds->out + n)
136#define FDS_EX(fds, n) (fds->ex + n)
137
138#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
139
140static int max_select_fd(unsigned long n, fd_set_bits *fds)
141{
142 unsigned long *open_fds;
143 unsigned long set;
144 int max;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700145 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* handle last in-complete long-word first */
148 set = ~(~0UL << (n & (__NFDBITS-1)));
149 n /= __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700150 fdt = files_fdtable(current->files);
151 open_fds = fdt->open_fds->fds_bits+n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 max = 0;
153 if (set) {
154 set &= BITS(fds, n);
155 if (set) {
156 if (!(set & ~*open_fds))
157 goto get_max;
158 return -EBADF;
159 }
160 }
161 while (n) {
162 open_fds--;
163 n--;
164 set = BITS(fds, n);
165 if (!set)
166 continue;
167 if (set & ~*open_fds)
168 return -EBADF;
169 if (max)
170 continue;
171get_max:
172 do {
173 max++;
174 set >>= 1;
175 } while (set);
176 max += n * __NFDBITS;
177 }
178
179 return max;
180}
181
182#define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
183#define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
184#define ISSET(i,m) (((i)&*(m)) != 0)
185#define SET(i,m) (*(m) |= (i))
186
187#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
188#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
189#define POLLEX_SET (POLLPRI)
190
David Woodhouse9f729492006-01-18 17:44:05 -0800191int do_select(int n, fd_set_bits *fds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct poll_wqueues table;
194 poll_table *wait;
195 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Dipankar Sarmab8359962005-09-09 13:04:14 -0700197 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 retval = max_select_fd(n, fds);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700199 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (retval < 0)
202 return retval;
203 n = retval;
204
205 poll_initwait(&table);
206 wait = &table.pt;
David Woodhouse9f729492006-01-18 17:44:05 -0800207 if (!*timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 wait = NULL;
209 retval = 0;
210 for (;;) {
211 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
David Woodhouse9f729492006-01-18 17:44:05 -0800212 long __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 set_current_state(TASK_INTERRUPTIBLE);
215
216 inp = fds->in; outp = fds->out; exp = fds->ex;
217 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
218
219 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
220 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
221 unsigned long res_in = 0, res_out = 0, res_ex = 0;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800222 const struct file_operations *f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct file *file = NULL;
224
225 in = *inp++; out = *outp++; ex = *exp++;
226 all_bits = in | out | ex;
227 if (all_bits == 0) {
228 i += __NFDBITS;
229 continue;
230 }
231
232 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
Eric Dumazete4a1f122006-03-28 01:56:34 -0800233 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (i >= n)
235 break;
236 if (!(bit & all_bits))
237 continue;
Eric Dumazete4a1f122006-03-28 01:56:34 -0800238 file = fget_light(i, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 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);
Eric Dumazete4a1f122006-03-28 01:56:34 -0800244 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 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;
Andrew Morton29ff2db2006-04-10 22:52:46 -0700312 void *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800313 int ret, max_fds;
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700314 unsigned int size;
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
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800323 /* max_fds 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);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800326 max_fds = fdt->max_fds;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700327 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800328 if (n > max_fds)
329 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 size = FDS_BYTES(n);
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700337 bits = stack_fds;
338 if (size > sizeof(stack_fds) / 6) {
339 /* Not enough space in on-stack array; must use kmalloc */
340 ret = -ENOMEM;
Andi Kleen70674f92006-03-28 01:56:33 -0800341 bits = kmalloc(6 * size, GFP_KERNEL);
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700342 if (!bits)
343 goto out_nofds;
344 }
Andrew Morton29ff2db2006-04-10 22:52:46 -0700345 fds.in = bits;
346 fds.out = bits + size;
347 fds.ex = bits + 2*size;
348 fds.res_in = bits + 3*size;
349 fds.res_out = bits + 4*size;
350 fds.res_ex = bits + 5*size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if ((ret = get_fd_set(n, inp, fds.in)) ||
353 (ret = get_fd_set(n, outp, fds.out)) ||
354 (ret = get_fd_set(n, exp, fds.ex)))
355 goto out;
356 zero_fd_set(n, fds.res_in);
357 zero_fd_set(n, fds.res_out);
358 zero_fd_set(n, fds.res_ex);
359
David Woodhouse9f729492006-01-18 17:44:05 -0800360 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (ret < 0)
363 goto out;
364 if (!ret) {
365 ret = -ERESTARTNOHAND;
366 if (signal_pending(current))
367 goto out;
368 ret = 0;
369 }
370
371 if (set_fd_set(n, inp, fds.res_in) ||
372 set_fd_set(n, outp, fds.res_out) ||
373 set_fd_set(n, exp, fds.res_ex))
374 ret = -EFAULT;
375
376out:
Andi Kleen70674f92006-03-28 01:56:33 -0800377 if (bits != stack_fds)
378 kfree(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379out_nofds:
380 return ret;
381}
382
David Woodhouse9f729492006-01-18 17:44:05 -0800383asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
384 fd_set __user *exp, struct timeval __user *tvp)
385{
386 s64 timeout = -1;
387 struct timeval tv;
388 int ret;
389
390 if (tvp) {
391 if (copy_from_user(&tv, tvp, sizeof(tv)))
392 return -EFAULT;
393
394 if (tv.tv_sec < 0 || tv.tv_usec < 0)
395 return -EINVAL;
396
397 /* Cast to u64 to make GCC stop complaining */
398 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
399 timeout = -1; /* infinite */
400 else {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700401 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800402 timeout += tv.tv_sec * HZ;
403 }
404 }
405
406 ret = core_sys_select(n, inp, outp, exp, &timeout);
407
408 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800409 struct timeval rtv;
410
David Woodhouse9f729492006-01-18 17:44:05 -0800411 if (current->personality & STICKY_TIMEOUTS)
412 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800413 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
414 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800415 if (timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800416 rtv = tv;
417 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800418sticky:
419 /*
420 * If an application puts its timeval in read-only
421 * memory, we don't want the Linux-specific update to
422 * the timeval to cause a fault after the select has
423 * completed successfully. However, because we're not
424 * updating the timeval, we can't restart the system
425 * call.
426 */
427 if (ret == -ERESTARTNOHAND)
428 ret = -EINTR;
429 }
430 }
431
432 return ret;
433}
434
435#ifdef TIF_RESTORE_SIGMASK
436asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
437 fd_set __user *exp, struct timespec __user *tsp,
438 const sigset_t __user *sigmask, size_t sigsetsize)
439{
440 s64 timeout = MAX_SCHEDULE_TIMEOUT;
441 sigset_t ksigmask, sigsaved;
442 struct timespec ts;
443 int ret;
444
445 if (tsp) {
446 if (copy_from_user(&ts, tsp, sizeof(ts)))
447 return -EFAULT;
448
449 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
450 return -EINVAL;
451
452 /* Cast to u64 to make GCC stop complaining */
453 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
454 timeout = -1; /* infinite */
455 else {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700456 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800457 timeout += ts.tv_sec * HZ;
458 }
459 }
460
461 if (sigmask) {
462 /* XXX: Don't preclude handling different sized sigset_t's. */
463 if (sigsetsize != sizeof(sigset_t))
464 return -EINVAL;
465 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
466 return -EFAULT;
467
468 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
469 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
470 }
471
472 ret = core_sys_select(n, inp, outp, exp, &timeout);
473
474 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800475 struct timespec rts;
476
David Woodhouse9f729492006-01-18 17:44:05 -0800477 if (current->personality & STICKY_TIMEOUTS)
478 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800479 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
480 1000;
481 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800482 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800483 rts = ts;
484 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800485sticky:
486 /*
487 * If an application puts its timeval in read-only
488 * memory, we don't want the Linux-specific update to
489 * the timeval to cause a fault after the select has
490 * completed successfully. However, because we're not
491 * updating the timeval, we can't restart the system
492 * call.
493 */
494 if (ret == -ERESTARTNOHAND)
495 ret = -EINTR;
496 }
497 }
498
499 if (ret == -ERESTARTNOHAND) {
500 /*
501 * Don't restore the signal mask yet. Let do_signal() deliver
502 * the signal on the way back to userspace, before the signal
503 * mask is restored.
504 */
505 if (sigmask) {
506 memcpy(&current->saved_sigmask, &sigsaved,
507 sizeof(sigsaved));
508 set_thread_flag(TIF_RESTORE_SIGMASK);
509 }
510 } else if (sigmask)
511 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
512
513 return ret;
514}
515
516/*
517 * Most architectures can't handle 7-argument syscalls. So we provide a
518 * 6-argument version where the sixth argument is a pointer to a structure
519 * which has a pointer to the sigset_t itself followed by a size_t containing
520 * the sigset size.
521 */
522asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
523 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
524{
525 size_t sigsetsize = 0;
526 sigset_t __user *up = NULL;
527
528 if (sig) {
529 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
Al Viroe110ab92006-02-01 05:26:09 -0500530 || __get_user(up, (sigset_t __user * __user *)sig)
David Woodhouse9f729492006-01-18 17:44:05 -0800531 || __get_user(sigsetsize,
Al Viroe110ab92006-02-01 05:26:09 -0500532 (size_t __user *)(sig+sizeof(void *))))
David Woodhouse9f729492006-01-18 17:44:05 -0800533 return -EFAULT;
534 }
535
536 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
537}
538#endif /* TIF_RESTORE_SIGMASK */
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540struct poll_list {
541 struct poll_list *next;
542 int len;
543 struct pollfd entries[0];
544};
545
546#define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
547
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700548/*
549 * Fish for pollable events on the pollfd->fd file descriptor. We're only
550 * interested in events matching the pollfd->events mask, and the result
551 * matching that mask is both recorded in pollfd->revents and returned. The
552 * pwait poll_table will be used by the fd-provided poll handler for waiting,
553 * if non-NULL.
554 */
555static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700557 unsigned int mask;
558 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700560 mask = 0;
561 fd = pollfd->fd;
562 if (fd >= 0) {
563 int fput_needed;
564 struct file * file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700566 file = fget_light(fd, &fput_needed);
567 mask = POLLNVAL;
568 if (file != NULL) {
569 mask = DEFAULT_POLLMASK;
570 if (file->f_op && file->f_op->poll)
571 mask = file->f_op->poll(file, pwait);
572 /* Mask out unneeded events. */
573 mask &= pollfd->events | POLLERR | POLLHUP;
574 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700577 pollfd->revents = mask;
578
579 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582static int do_poll(unsigned int nfds, struct poll_list *list,
David Woodhouse9f729492006-01-18 17:44:05 -0800583 struct poll_wqueues *wait, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 int count = 0;
586 poll_table* pt = &wait->pt;
587
David Woodhouse9f729492006-01-18 17:44:05 -0800588 /* Optimise the no-wait case */
589 if (!(*timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 pt = NULL;
591
592 for (;;) {
593 struct poll_list *walk;
David Woodhouse9f729492006-01-18 17:44:05 -0800594 long __timeout;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 set_current_state(TASK_INTERRUPTIBLE);
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700597 for (walk = list; walk != NULL; walk = walk->next) {
598 struct pollfd * pfd, * pfd_end;
599
600 pfd = walk->entries;
601 pfd_end = pfd + walk->len;
602 for (; pfd != pfd_end; pfd++) {
603 /*
604 * Fish for events. If we found one, record it
605 * and kill the poll_table, so we don't
606 * needlessly register any other waiters after
607 * this. They'll get immediately deregistered
608 * when we break out and return.
609 */
610 if (do_pollfd(pfd, pt)) {
611 count++;
612 pt = NULL;
613 }
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700616 /*
617 * All waiters have already been registered, so don't provide
618 * a poll_table to them on the next loop iteration.
619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 pt = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800621 if (count || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
623 count = wait->error;
624 if (count)
625 break;
David Woodhouse9f729492006-01-18 17:44:05 -0800626
627 if (*timeout < 0) {
628 /* Wait indefinitely */
629 __timeout = MAX_SCHEDULE_TIMEOUT;
630 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
631 /*
632 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
633 * a loop
634 */
635 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
636 *timeout -= __timeout;
637 } else {
638 __timeout = *timeout;
639 *timeout = 0;
640 }
641
642 __timeout = schedule_timeout(__timeout);
643 if (*timeout >= 0)
644 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 __set_current_state(TASK_RUNNING);
647 return count;
648}
649
Andi Kleen70674f92006-03-28 01:56:33 -0800650#define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
651 sizeof(struct pollfd))
652
David Woodhouse9f729492006-01-18 17:44:05 -0800653int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct poll_wqueues table;
656 int fdcount, err;
657 unsigned int i;
658 struct poll_list *head;
659 struct poll_list *walk;
Jes Sorensen30c14e42006-03-31 11:18:57 -0500660 /* Allocate small arguments on the stack to save memory and be
661 faster - use long to make sure the buffer is aligned properly
662 on 64 bit archs to avoid unaligned access */
663 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
Andi Kleen70674f92006-03-28 01:56:33 -0800664 struct poll_list *stack_pp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* Do a sanity check on nfds ... */
Chris Snook4e6fd332006-09-29 02:01:33 -0700667 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EINVAL;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 poll_initwait(&table);
671
672 head = NULL;
673 walk = NULL;
674 i = nfds;
675 err = -ENOMEM;
676 while(i!=0) {
677 struct poll_list *pp;
Andi Kleen70674f92006-03-28 01:56:33 -0800678 int num, size;
679 if (stack_pp == NULL)
680 num = N_STACK_PPS;
681 else
682 num = POLLFD_PER_PAGE;
683 if (num > i)
684 num = i;
685 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
686 if (!stack_pp)
687 stack_pp = pp = (struct poll_list *)stack_pps;
688 else {
689 pp = kmalloc(size, GFP_KERNEL);
690 if (!pp)
691 goto out_fds;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pp->next=NULL;
Andi Kleen70674f92006-03-28 01:56:33 -0800694 pp->len = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (head == NULL)
696 head = pp;
697 else
698 walk->next = pp;
699
700 walk = pp;
701 if (copy_from_user(pp->entries, ufds + nfds-i,
Andi Kleen70674f92006-03-28 01:56:33 -0800702 sizeof(struct pollfd)*num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 err = -EFAULT;
704 goto out_fds;
705 }
706 i -= pp->len;
707 }
David Woodhouse9f729492006-01-18 17:44:05 -0800708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 fdcount = do_poll(nfds, head, &table, timeout);
710
711 /* OK, now copy the revents fields back to user space. */
712 walk = head;
713 err = -EFAULT;
714 while(walk != NULL) {
715 struct pollfd *fds = walk->entries;
716 int j;
717
718 for (j=0; j < walk->len; j++, ufds++) {
719 if(__put_user(fds[j].revents, &ufds->revents))
720 goto out_fds;
721 }
722 walk = walk->next;
723 }
724 err = fdcount;
725 if (!fdcount && signal_pending(current))
726 err = -EINTR;
727out_fds:
728 walk = head;
729 while(walk!=NULL) {
730 struct poll_list *pp = walk->next;
Andi Kleen70674f92006-03-28 01:56:33 -0800731 if (walk != stack_pp)
732 kfree(walk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 walk = pp;
734 }
735 poll_freewait(&table);
736 return err;
737}
David Woodhouse9f729492006-01-18 17:44:05 -0800738
739asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
740 long timeout_msecs)
741{
Frode Isaksen04a34462006-06-25 05:49:09 -0700742 s64 timeout_jiffies;
David Woodhouse9f729492006-01-18 17:44:05 -0800743
Frode Isaksen04a34462006-06-25 05:49:09 -0700744 if (timeout_msecs > 0) {
David Woodhouse9f729492006-01-18 17:44:05 -0800745#if HZ > 1000
746 /* We can only overflow if HZ > 1000 */
747 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
748 timeout_jiffies = -1;
749 else
750#endif
751 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
Frode Isaksen04a34462006-06-25 05:49:09 -0700752 } else {
753 /* Infinite (< 0) or no (0) timeout */
754 timeout_jiffies = timeout_msecs;
David Woodhouse9f729492006-01-18 17:44:05 -0800755 }
756
757 return do_sys_poll(ufds, nfds, &timeout_jiffies);
758}
759
760#ifdef TIF_RESTORE_SIGMASK
761asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
762 struct timespec __user *tsp, const sigset_t __user *sigmask,
763 size_t sigsetsize)
764{
765 sigset_t ksigmask, sigsaved;
766 struct timespec ts;
767 s64 timeout = -1;
768 int ret;
769
770 if (tsp) {
771 if (copy_from_user(&ts, tsp, sizeof(ts)))
772 return -EFAULT;
773
774 /* Cast to u64 to make GCC stop complaining */
775 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
776 timeout = -1; /* infinite */
777 else {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700778 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800779 timeout += ts.tv_sec * HZ;
780 }
781 }
782
783 if (sigmask) {
784 /* XXX: Don't preclude handling different sized sigset_t's. */
785 if (sigsetsize != sizeof(sigset_t))
786 return -EINVAL;
787 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
788 return -EFAULT;
789
790 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
791 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
792 }
793
794 ret = do_sys_poll(ufds, nfds, &timeout);
795
796 /* We can restart this syscall, usually */
797 if (ret == -EINTR) {
798 /*
799 * Don't restore the signal mask yet. Let do_signal() deliver
800 * the signal on the way back to userspace, before the signal
801 * mask is restored.
802 */
803 if (sigmask) {
804 memcpy(&current->saved_sigmask, &sigsaved,
805 sizeof(sigsaved));
806 set_thread_flag(TIF_RESTORE_SIGMASK);
807 }
808 ret = -ERESTARTNOHAND;
809 } else if (sigmask)
810 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
811
812 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -0800813 struct timespec rts;
814
David Woodhouse9f729492006-01-18 17:44:05 -0800815 if (current->personality & STICKY_TIMEOUTS)
816 goto sticky;
817 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -0800818 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
819 1000;
820 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800821 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800822 rts = ts;
823 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800824 sticky:
825 /*
826 * If an application puts its timeval in read-only
827 * memory, we don't want the Linux-specific update to
828 * the timeval to cause a fault after the select has
829 * completed successfully. However, because we're not
830 * updating the timeval, we can't restart the system
831 * call.
832 */
833 if (ret == -ERESTARTNOHAND && timeout >= 0)
834 ret = -EINTR;
835 }
836 }
837
838 return ret;
839}
840#endif /* TIF_RESTORE_SIGMASK */