blob: 46dca31c607ad79fd2e857cb1e3b72a902ee6c0f [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 -070029struct poll_table_page {
30 struct poll_table_page * next;
31 struct poll_table_entry * entry;
32 struct poll_table_entry entries[0];
33};
34
35#define POLL_TABLE_FULL(table) \
36 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
37
38/*
39 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
40 * I have rewritten this, taking some shortcuts: This code may not be easy to
41 * follow, but it should be free of race-conditions, and it's practical. If you
42 * understand what I'm doing here, then you understand how the linux
43 * sleep/wakeup mechanism works.
44 *
45 * Two very simple procedures, poll_wait() and poll_freewait() make all the
46 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
47 * as all select/poll functions have to call it to add an entry to the
48 * poll table.
49 */
Adrian Bunk75c96f82005-05-05 16:16:09 -070050static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
51 poll_table *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53void poll_initwait(struct poll_wqueues *pwq)
54{
55 init_poll_funcptr(&pwq->pt, __pollwait);
56 pwq->error = 0;
57 pwq->table = NULL;
Andi Kleen70674f92006-03-28 01:56:33 -080058 pwq->inline_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61EXPORT_SYMBOL(poll_initwait);
62
Andi Kleen70674f92006-03-28 01:56:33 -080063static void free_poll_entry(struct poll_table_entry *entry)
64{
WANG Congccf67802007-05-09 07:10:02 +020065 remove_wait_queue(entry->wait_address, &entry->wait);
Andi Kleen70674f92006-03-28 01:56:33 -080066 fput(entry->filp);
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069void poll_freewait(struct poll_wqueues *pwq)
70{
71 struct poll_table_page * p = pwq->table;
Andi Kleen70674f92006-03-28 01:56:33 -080072 int i;
73 for (i = 0; i < pwq->inline_index; i++)
74 free_poll_entry(pwq->inline_entries + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 while (p) {
76 struct poll_table_entry * entry;
77 struct poll_table_page *old;
78
79 entry = p->entry;
80 do {
81 entry--;
Andi Kleen70674f92006-03-28 01:56:33 -080082 free_poll_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } while (entry > p->entries);
84 old = p;
85 p = p->next;
86 free_page((unsigned long) old);
87 }
88}
89
90EXPORT_SYMBOL(poll_freewait);
91
Andi Kleen70674f92006-03-28 01:56:33 -080092static struct poll_table_entry *poll_get_entry(poll_table *_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
95 struct poll_table_page *table = p->table;
96
Andi Kleen70674f92006-03-28 01:56:33 -080097 if (p->inline_index < N_INLINE_POLL_ENTRIES)
98 return p->inline_entries + p->inline_index++;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!table || POLL_TABLE_FULL(table)) {
101 struct poll_table_page *new_table;
102
103 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
104 if (!new_table) {
105 p->error = -ENOMEM;
106 __set_current_state(TASK_RUNNING);
Andi Kleen70674f92006-03-28 01:56:33 -0800107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 new_table->entry = new_table->entries;
110 new_table->next = table;
111 p->table = new_table;
112 table = new_table;
113 }
114
Andi Kleen70674f92006-03-28 01:56:33 -0800115 return table->entry++;
116}
117
118/* Add a new entry */
119static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
120 poll_table *p)
121{
122 struct poll_table_entry *entry = poll_get_entry(p);
123 if (!entry)
124 return;
125 get_file(filp);
126 entry->filp = filp;
127 entry->wait_address = wait_address;
128 init_waitqueue_entry(&entry->wait, current);
WANG Congccf67802007-05-09 07:10:02 +0200129 add_wait_queue(wait_address, &entry->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132#define FDS_IN(fds, n) (fds->in + n)
133#define FDS_OUT(fds, n) (fds->out + n)
134#define FDS_EX(fds, n) (fds->ex + n)
135
136#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
137
138static int max_select_fd(unsigned long n, fd_set_bits *fds)
139{
140 unsigned long *open_fds;
141 unsigned long set;
142 int max;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700143 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* handle last in-complete long-word first */
146 set = ~(~0UL << (n & (__NFDBITS-1)));
147 n /= __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700148 fdt = files_fdtable(current->files);
149 open_fds = fdt->open_fds->fds_bits+n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 max = 0;
151 if (set) {
152 set &= BITS(fds, n);
153 if (set) {
154 if (!(set & ~*open_fds))
155 goto get_max;
156 return -EBADF;
157 }
158 }
159 while (n) {
160 open_fds--;
161 n--;
162 set = BITS(fds, n);
163 if (!set)
164 continue;
165 if (set & ~*open_fds)
166 return -EBADF;
167 if (max)
168 continue;
169get_max:
170 do {
171 max++;
172 set >>= 1;
173 } while (set);
174 max += n * __NFDBITS;
175 }
176
177 return max;
178}
179
180#define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
181#define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
182#define ISSET(i,m) (((i)&*(m)) != 0)
183#define SET(i,m) (*(m) |= (i))
184
185#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
186#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
187#define POLLEX_SET (POLLPRI)
188
David Woodhouse9f729492006-01-18 17:44:05 -0800189int do_select(int n, fd_set_bits *fds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct poll_wqueues table;
192 poll_table *wait;
193 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dipankar Sarmab8359962005-09-09 13:04:14 -0700195 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 retval = max_select_fd(n, fds);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700197 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (retval < 0)
200 return retval;
201 n = retval;
202
203 poll_initwait(&table);
204 wait = &table.pt;
David Woodhouse9f729492006-01-18 17:44:05 -0800205 if (!*timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 wait = NULL;
207 retval = 0;
208 for (;;) {
209 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
David Woodhouse9f729492006-01-18 17:44:05 -0800210 long __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 set_current_state(TASK_INTERRUPTIBLE);
213
214 inp = fds->in; outp = fds->out; exp = fds->ex;
215 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
216
217 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
218 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
219 unsigned long res_in = 0, res_out = 0, res_ex = 0;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800220 const struct file_operations *f_op = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct file *file = NULL;
222
223 in = *inp++; out = *outp++; ex = *exp++;
224 all_bits = in | out | ex;
225 if (all_bits == 0) {
226 i += __NFDBITS;
227 continue;
228 }
229
230 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
Eric Dumazete4a1f122006-03-28 01:56:34 -0800231 int fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (i >= n)
233 break;
234 if (!(bit & all_bits))
235 continue;
Eric Dumazete4a1f122006-03-28 01:56:34 -0800236 file = fget_light(i, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (file) {
238 f_op = file->f_op;
239 mask = DEFAULT_POLLMASK;
240 if (f_op && f_op->poll)
241 mask = (*f_op->poll)(file, retval ? NULL : wait);
Eric Dumazete4a1f122006-03-28 01:56:34 -0800242 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if ((mask & POLLIN_SET) && (in & bit)) {
244 res_in |= bit;
245 retval++;
246 }
247 if ((mask & POLLOUT_SET) && (out & bit)) {
248 res_out |= bit;
249 retval++;
250 }
251 if ((mask & POLLEX_SET) && (ex & bit)) {
252 res_ex |= bit;
253 retval++;
254 }
255 }
256 cond_resched();
257 }
258 if (res_in)
259 *rinp = res_in;
260 if (res_out)
261 *routp = res_out;
262 if (res_ex)
263 *rexp = res_ex;
264 }
265 wait = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800266 if (retval || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 if(table.error) {
269 retval = table.error;
270 break;
271 }
David Woodhouse9f729492006-01-18 17:44:05 -0800272
273 if (*timeout < 0) {
274 /* Wait indefinitely */
275 __timeout = MAX_SCHEDULE_TIMEOUT;
276 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
277 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
278 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
279 *timeout -= __timeout;
280 } else {
281 __timeout = *timeout;
282 *timeout = 0;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 __timeout = schedule_timeout(__timeout);
David Woodhouse9f729492006-01-18 17:44:05 -0800285 if (*timeout >= 0)
286 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 __set_current_state(TASK_RUNNING);
289
290 poll_freewait(&table);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return retval;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * We can actually return ERESTARTSYS instead of EINTR, but I'd
297 * like to be certain this leads to no problems. So I return
298 * EINTR just for safety.
299 *
300 * Update: ERESTARTSYS breaks at least the xview clock binary, so
301 * I'm trying ERESTARTNOHAND which restart only when you want to.
302 */
303#define MAX_SELECT_SECONDS \
304 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
305
David Woodhouse9f729492006-01-18 17:44:05 -0800306static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
307 fd_set __user *exp, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 fd_set_bits fds;
Andrew Morton29ff2db2006-04-10 22:52:46 -0700310 void *bits;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800311 int ret, max_fds;
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700312 unsigned int size;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700313 struct fdtable *fdt;
Andi Kleen70674f92006-03-28 01:56:33 -0800314 /* Allocate small arguments on the stack to save memory and be faster */
Jes Sorensen30c14e42006-03-31 11:18:57 -0500315 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ret = -EINVAL;
318 if (n < 0)
319 goto out_nofds;
320
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800321 /* max_fds can increase, so grab it once to avoid race */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700322 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700323 fdt = files_fdtable(current->files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800324 max_fds = fdt->max_fds;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700325 rcu_read_unlock();
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800326 if (n > max_fds)
327 n = max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /*
330 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
331 * since we used fdset we need to allocate memory in units of
332 * long-words.
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 size = FDS_BYTES(n);
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700335 bits = stack_fds;
336 if (size > sizeof(stack_fds) / 6) {
337 /* Not enough space in on-stack array; must use kmalloc */
338 ret = -ENOMEM;
Andi Kleen70674f92006-03-28 01:56:33 -0800339 bits = kmalloc(6 * size, GFP_KERNEL);
Mitchell Blank Jrb04eb6a2006-04-10 22:54:08 -0700340 if (!bits)
341 goto out_nofds;
342 }
Andrew Morton29ff2db2006-04-10 22:52:46 -0700343 fds.in = bits;
344 fds.out = bits + size;
345 fds.ex = bits + 2*size;
346 fds.res_in = bits + 3*size;
347 fds.res_out = bits + 4*size;
348 fds.res_ex = bits + 5*size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
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 {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700399 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800400 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 {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700454 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800455 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
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700546/*
547 * Fish for pollable events on the pollfd->fd file descriptor. We're only
548 * interested in events matching the pollfd->events mask, and the result
549 * matching that mask is both recorded in pollfd->revents and returned. The
550 * pwait poll_table will be used by the fd-provided poll handler for waiting,
551 * if non-NULL.
552 */
553static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700555 unsigned int mask;
556 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700558 mask = 0;
559 fd = pollfd->fd;
560 if (fd >= 0) {
561 int fput_needed;
562 struct file * file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700564 file = fget_light(fd, &fput_needed);
565 mask = POLLNVAL;
566 if (file != NULL) {
567 mask = DEFAULT_POLLMASK;
568 if (file->f_op && file->f_op->poll)
569 mask = file->f_op->poll(file, pwait);
570 /* Mask out unneeded events. */
571 mask &= pollfd->events | POLLERR | POLLHUP;
572 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700575 pollfd->revents = mask;
576
577 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
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);
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700595 for (walk = list; walk != NULL; walk = walk->next) {
596 struct pollfd * pfd, * pfd_end;
597
598 pfd = walk->entries;
599 pfd_end = pfd + walk->len;
600 for (; pfd != pfd_end; pfd++) {
601 /*
602 * Fish for events. If we found one, record it
603 * and kill the poll_table, so we don't
604 * needlessly register any other waiters after
605 * this. They'll get immediately deregistered
606 * when we break out and return.
607 */
608 if (do_pollfd(pfd, pt)) {
609 count++;
610 pt = NULL;
611 }
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Vadim Lobanov4a4b69f2006-06-23 02:05:16 -0700614 /*
615 * All waiters have already been registered, so don't provide
616 * a poll_table to them on the next loop iteration.
617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 pt = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800619 if (count || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 count = wait->error;
622 if (count)
623 break;
David Woodhouse9f729492006-01-18 17:44:05 -0800624
625 if (*timeout < 0) {
626 /* Wait indefinitely */
627 __timeout = MAX_SCHEDULE_TIMEOUT;
628 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
629 /*
630 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
631 * a loop
632 */
633 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
634 *timeout -= __timeout;
635 } else {
636 __timeout = *timeout;
637 *timeout = 0;
638 }
639
640 __timeout = schedule_timeout(__timeout);
641 if (*timeout >= 0)
642 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 __set_current_state(TASK_RUNNING);
645 return count;
646}
647
Andi Kleen70674f92006-03-28 01:56:33 -0800648#define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
649 sizeof(struct pollfd))
650
David Woodhouse9f729492006-01-18 17:44:05 -0800651int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct poll_wqueues table;
654 int fdcount, err;
655 unsigned int i;
656 struct poll_list *head;
657 struct poll_list *walk;
Jes Sorensen30c14e42006-03-31 11:18:57 -0500658 /* Allocate small arguments on the stack to save memory and be
659 faster - use long to make sure the buffer is aligned properly
660 on 64 bit archs to avoid unaligned access */
661 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
Andi Kleen70674f92006-03-28 01:56:33 -0800662 struct poll_list *stack_pp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /* Do a sanity check on nfds ... */
Chris Snook4e6fd332006-09-29 02:01:33 -0700665 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 poll_initwait(&table);
669
670 head = NULL;
671 walk = NULL;
672 i = nfds;
673 err = -ENOMEM;
674 while(i!=0) {
675 struct poll_list *pp;
Andi Kleen70674f92006-03-28 01:56:33 -0800676 int num, size;
677 if (stack_pp == NULL)
678 num = N_STACK_PPS;
679 else
680 num = POLLFD_PER_PAGE;
681 if (num > i)
682 num = i;
683 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
684 if (!stack_pp)
685 stack_pp = pp = (struct poll_list *)stack_pps;
686 else {
687 pp = kmalloc(size, GFP_KERNEL);
688 if (!pp)
689 goto out_fds;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 pp->next=NULL;
Andi Kleen70674f92006-03-28 01:56:33 -0800692 pp->len = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (head == NULL)
694 head = pp;
695 else
696 walk->next = pp;
697
698 walk = pp;
699 if (copy_from_user(pp->entries, ufds + nfds-i,
Andi Kleen70674f92006-03-28 01:56:33 -0800700 sizeof(struct pollfd)*num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 err = -EFAULT;
702 goto out_fds;
703 }
704 i -= pp->len;
705 }
David Woodhouse9f729492006-01-18 17:44:05 -0800706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 fdcount = do_poll(nfds, head, &table, timeout);
708
709 /* OK, now copy the revents fields back to user space. */
710 walk = head;
711 err = -EFAULT;
712 while(walk != NULL) {
713 struct pollfd *fds = walk->entries;
714 int j;
715
716 for (j=0; j < walk->len; j++, ufds++) {
717 if(__put_user(fds[j].revents, &ufds->revents))
718 goto out_fds;
719 }
720 walk = walk->next;
721 }
722 err = fdcount;
723 if (!fdcount && signal_pending(current))
724 err = -EINTR;
725out_fds:
726 walk = head;
727 while(walk!=NULL) {
728 struct poll_list *pp = walk->next;
Andi Kleen70674f92006-03-28 01:56:33 -0800729 if (walk != stack_pp)
730 kfree(walk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 walk = pp;
732 }
733 poll_freewait(&table);
734 return err;
735}
David Woodhouse9f729492006-01-18 17:44:05 -0800736
737asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
738 long timeout_msecs)
739{
Frode Isaksen04a34462006-06-25 05:49:09 -0700740 s64 timeout_jiffies;
David Woodhouse9f729492006-01-18 17:44:05 -0800741
Frode Isaksen04a34462006-06-25 05:49:09 -0700742 if (timeout_msecs > 0) {
David Woodhouse9f729492006-01-18 17:44:05 -0800743#if HZ > 1000
744 /* We can only overflow if HZ > 1000 */
745 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
746 timeout_jiffies = -1;
747 else
748#endif
749 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
Frode Isaksen04a34462006-06-25 05:49:09 -0700750 } else {
751 /* Infinite (< 0) or no (0) timeout */
752 timeout_jiffies = timeout_msecs;
David Woodhouse9f729492006-01-18 17:44:05 -0800753 }
754
755 return do_sys_poll(ufds, nfds, &timeout_jiffies);
756}
757
758#ifdef TIF_RESTORE_SIGMASK
759asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
760 struct timespec __user *tsp, const sigset_t __user *sigmask,
761 size_t sigsetsize)
762{
763 sigset_t ksigmask, sigsaved;
764 struct timespec ts;
765 s64 timeout = -1;
766 int ret;
767
768 if (tsp) {
769 if (copy_from_user(&ts, tsp, sizeof(ts)))
770 return -EFAULT;
771
772 /* Cast to u64 to make GCC stop complaining */
773 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
774 timeout = -1; /* infinite */
775 else {
Milind Arun Choudhary022a1692007-05-08 00:29:02 -0700776 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
David Woodhouse9f729492006-01-18 17:44:05 -0800777 timeout += ts.tv_sec * HZ;
778 }
779 }
780
781 if (sigmask) {
782 /* XXX: Don't preclude handling different sized sigset_t's. */
783 if (sigsetsize != sizeof(sigset_t))
784 return -EINVAL;
785 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
786 return -EFAULT;
787
788 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
789 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
790 }
791
792 ret = do_sys_poll(ufds, nfds, &timeout);
793
794 /* We can restart this syscall, usually */
795 if (ret == -EINTR) {
796 /*
797 * Don't restore the signal mask yet. Let do_signal() deliver
798 * the signal on the way back to userspace, before the signal
799 * mask is restored.
800 */
801 if (sigmask) {
802 memcpy(&current->saved_sigmask, &sigsaved,
803 sizeof(sigsaved));
804 set_thread_flag(TIF_RESTORE_SIGMASK);
805 }
806 ret = -ERESTARTNOHAND;
807 } else if (sigmask)
808 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
809
810 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -0800811 struct timespec rts;
812
David Woodhouse9f729492006-01-18 17:44:05 -0800813 if (current->personality & STICKY_TIMEOUTS)
814 goto sticky;
815 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -0800816 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
817 1000;
818 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800819 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800820 rts = ts;
821 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800822 sticky:
823 /*
824 * If an application puts its timeval in read-only
825 * memory, we don't want the Linux-specific update to
826 * the timeval to cause a fault after the select has
827 * completed successfully. However, because we're not
828 * updating the timeval, we can't restart the system
829 * call.
830 */
831 if (ret == -ERESTARTNOHAND && timeout >= 0)
832 ret = -EINTR;
833 }
834 }
835
836 return ret;
837}
838#endif /* TIF_RESTORE_SIGMASK */