blob: 1815a57d225585122c22dd0a357dbdced59e1c40 [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
32struct poll_table_entry {
33 struct file * filp;
34 wait_queue_t wait;
35 wait_queue_head_t * wait_address;
36};
37
38struct poll_table_page {
39 struct poll_table_page * next;
40 struct poll_table_entry * entry;
41 struct poll_table_entry entries[0];
42};
43
44#define POLL_TABLE_FULL(table) \
45 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
46
47/*
48 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
49 * I have rewritten this, taking some shortcuts: This code may not be easy to
50 * follow, but it should be free of race-conditions, and it's practical. If you
51 * understand what I'm doing here, then you understand how the linux
52 * sleep/wakeup mechanism works.
53 *
54 * Two very simple procedures, poll_wait() and poll_freewait() make all the
55 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
56 * as all select/poll functions have to call it to add an entry to the
57 * poll table.
58 */
Adrian Bunk75c96f82005-05-05 16:16:09 -070059static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
60 poll_table *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62void poll_initwait(struct poll_wqueues *pwq)
63{
64 init_poll_funcptr(&pwq->pt, __pollwait);
65 pwq->error = 0;
66 pwq->table = NULL;
67}
68
69EXPORT_SYMBOL(poll_initwait);
70
71void poll_freewait(struct poll_wqueues *pwq)
72{
73 struct poll_table_page * p = pwq->table;
74 while (p) {
75 struct poll_table_entry * entry;
76 struct poll_table_page *old;
77
78 entry = p->entry;
79 do {
80 entry--;
81 remove_wait_queue(entry->wait_address,&entry->wait);
82 fput(entry->filp);
83 } 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
Adrian Bunk75c96f82005-05-05 16:16:09 -070092static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
93 poll_table *_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96 struct poll_table_page *table = p->table;
97
98 if (!table || POLL_TABLE_FULL(table)) {
99 struct poll_table_page *new_table;
100
101 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
102 if (!new_table) {
103 p->error = -ENOMEM;
104 __set_current_state(TASK_RUNNING);
105 return;
106 }
107 new_table->entry = new_table->entries;
108 new_table->next = table;
109 p->table = new_table;
110 table = new_table;
111 }
112
113 /* Add a new entry */
114 {
115 struct poll_table_entry * entry = table->entry;
116 table->entry = entry+1;
117 get_file(filp);
118 entry->filp = filp;
119 entry->wait_address = wait_address;
120 init_waitqueue_entry(&entry->wait, current);
121 add_wait_queue(wait_address,&entry->wait);
122 }
123}
124
125#define FDS_IN(fds, n) (fds->in + n)
126#define FDS_OUT(fds, n) (fds->out + n)
127#define FDS_EX(fds, n) (fds->ex + n)
128
129#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
130
131static int max_select_fd(unsigned long n, fd_set_bits *fds)
132{
133 unsigned long *open_fds;
134 unsigned long set;
135 int max;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700136 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* handle last in-complete long-word first */
139 set = ~(~0UL << (n & (__NFDBITS-1)));
140 n /= __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700141 fdt = files_fdtable(current->files);
142 open_fds = fdt->open_fds->fds_bits+n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 max = 0;
144 if (set) {
145 set &= BITS(fds, n);
146 if (set) {
147 if (!(set & ~*open_fds))
148 goto get_max;
149 return -EBADF;
150 }
151 }
152 while (n) {
153 open_fds--;
154 n--;
155 set = BITS(fds, n);
156 if (!set)
157 continue;
158 if (set & ~*open_fds)
159 return -EBADF;
160 if (max)
161 continue;
162get_max:
163 do {
164 max++;
165 set >>= 1;
166 } while (set);
167 max += n * __NFDBITS;
168 }
169
170 return max;
171}
172
173#define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
174#define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
175#define ISSET(i,m) (((i)&*(m)) != 0)
176#define SET(i,m) (*(m) |= (i))
177
178#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
179#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
180#define POLLEX_SET (POLLPRI)
181
David Woodhouse9f729492006-01-18 17:44:05 -0800182int do_select(int n, fd_set_bits *fds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct poll_wqueues table;
185 poll_table *wait;
186 int retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dipankar Sarmab8359962005-09-09 13:04:14 -0700188 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 retval = max_select_fd(n, fds);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (retval < 0)
193 return retval;
194 n = retval;
195
196 poll_initwait(&table);
197 wait = &table.pt;
David Woodhouse9f729492006-01-18 17:44:05 -0800198 if (!*timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 wait = NULL;
200 retval = 0;
201 for (;;) {
202 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
David Woodhouse9f729492006-01-18 17:44:05 -0800203 long __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 set_current_state(TASK_INTERRUPTIBLE);
206
207 inp = fds->in; outp = fds->out; exp = fds->ex;
208 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
209
210 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
211 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
212 unsigned long res_in = 0, res_out = 0, res_ex = 0;
213 struct file_operations *f_op = NULL;
214 struct file *file = NULL;
215
216 in = *inp++; out = *outp++; ex = *exp++;
217 all_bits = in | out | ex;
218 if (all_bits == 0) {
219 i += __NFDBITS;
220 continue;
221 }
222
223 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
224 if (i >= n)
225 break;
226 if (!(bit & all_bits))
227 continue;
228 file = fget(i);
229 if (file) {
230 f_op = file->f_op;
231 mask = DEFAULT_POLLMASK;
232 if (f_op && f_op->poll)
233 mask = (*f_op->poll)(file, retval ? NULL : wait);
234 fput(file);
235 if ((mask & POLLIN_SET) && (in & bit)) {
236 res_in |= bit;
237 retval++;
238 }
239 if ((mask & POLLOUT_SET) && (out & bit)) {
240 res_out |= bit;
241 retval++;
242 }
243 if ((mask & POLLEX_SET) && (ex & bit)) {
244 res_ex |= bit;
245 retval++;
246 }
247 }
248 cond_resched();
249 }
250 if (res_in)
251 *rinp = res_in;
252 if (res_out)
253 *routp = res_out;
254 if (res_ex)
255 *rexp = res_ex;
256 }
257 wait = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800258 if (retval || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
260 if(table.error) {
261 retval = table.error;
262 break;
263 }
David Woodhouse9f729492006-01-18 17:44:05 -0800264
265 if (*timeout < 0) {
266 /* Wait indefinitely */
267 __timeout = MAX_SCHEDULE_TIMEOUT;
268 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
269 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
270 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
271 *timeout -= __timeout;
272 } else {
273 __timeout = *timeout;
274 *timeout = 0;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 __timeout = schedule_timeout(__timeout);
David Woodhouse9f729492006-01-18 17:44:05 -0800277 if (*timeout >= 0)
278 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 __set_current_state(TASK_RUNNING);
281
282 poll_freewait(&table);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return retval;
285}
286
287static void *select_bits_alloc(int size)
288{
289 return kmalloc(6 * size, GFP_KERNEL);
290}
291
292static void select_bits_free(void *bits, int size)
293{
294 kfree(bits);
295}
296
297/*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 ret = -EINVAL;
317 if (n < 0)
318 goto out_nofds;
319
320 /* max_fdset can increase, so grab it once to avoid race */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700321 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700322 fdt = files_fdtable(current->files);
323 max_fdset = fdt->max_fdset;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (n > max_fdset)
326 n = max_fdset;
327
328 /*
329 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
330 * since we used fdset we need to allocate memory in units of
331 * long-words.
332 */
333 ret = -ENOMEM;
334 size = FDS_BYTES(n);
335 bits = select_bits_alloc(size);
336 if (!bits)
337 goto out_nofds;
338 fds.in = (unsigned long *) bits;
339 fds.out = (unsigned long *) (bits + size);
340 fds.ex = (unsigned long *) (bits + 2*size);
341 fds.res_in = (unsigned long *) (bits + 3*size);
342 fds.res_out = (unsigned long *) (bits + 4*size);
343 fds.res_ex = (unsigned long *) (bits + 5*size);
344
345 if ((ret = get_fd_set(n, inp, fds.in)) ||
346 (ret = get_fd_set(n, outp, fds.out)) ||
347 (ret = get_fd_set(n, exp, fds.ex)))
348 goto out;
349 zero_fd_set(n, fds.res_in);
350 zero_fd_set(n, fds.res_out);
351 zero_fd_set(n, fds.res_ex);
352
David Woodhouse9f729492006-01-18 17:44:05 -0800353 ret = do_select(n, &fds, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (ret < 0)
356 goto out;
357 if (!ret) {
358 ret = -ERESTARTNOHAND;
359 if (signal_pending(current))
360 goto out;
361 ret = 0;
362 }
363
364 if (set_fd_set(n, inp, fds.res_in) ||
365 set_fd_set(n, outp, fds.res_out) ||
366 set_fd_set(n, exp, fds.res_ex))
367 ret = -EFAULT;
368
369out:
370 select_bits_free(bits, size);
371out_nofds:
372 return ret;
373}
374
David Woodhouse9f729492006-01-18 17:44:05 -0800375asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
376 fd_set __user *exp, struct timeval __user *tvp)
377{
378 s64 timeout = -1;
379 struct timeval tv;
380 int ret;
381
382 if (tvp) {
383 if (copy_from_user(&tv, tvp, sizeof(tv)))
384 return -EFAULT;
385
386 if (tv.tv_sec < 0 || tv.tv_usec < 0)
387 return -EINVAL;
388
389 /* Cast to u64 to make GCC stop complaining */
390 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
391 timeout = -1; /* infinite */
392 else {
393 timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
394 timeout += tv.tv_sec * HZ;
395 }
396 }
397
398 ret = core_sys_select(n, inp, outp, exp, &timeout);
399
400 if (tvp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800401 struct timeval rtv;
402
David Woodhouse9f729492006-01-18 17:44:05 -0800403 if (current->personality & STICKY_TIMEOUTS)
404 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800405 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
406 rtv.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800407 if (timeval_compare(&rtv, &tv) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800408 rtv = tv;
409 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800410sticky:
411 /*
412 * If an application puts its timeval in read-only
413 * memory, we don't want the Linux-specific update to
414 * the timeval to cause a fault after the select has
415 * completed successfully. However, because we're not
416 * updating the timeval, we can't restart the system
417 * call.
418 */
419 if (ret == -ERESTARTNOHAND)
420 ret = -EINTR;
421 }
422 }
423
424 return ret;
425}
426
427#ifdef TIF_RESTORE_SIGMASK
428asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
429 fd_set __user *exp, struct timespec __user *tsp,
430 const sigset_t __user *sigmask, size_t sigsetsize)
431{
432 s64 timeout = MAX_SCHEDULE_TIMEOUT;
433 sigset_t ksigmask, sigsaved;
434 struct timespec ts;
435 int ret;
436
437 if (tsp) {
438 if (copy_from_user(&ts, tsp, sizeof(ts)))
439 return -EFAULT;
440
441 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
442 return -EINVAL;
443
444 /* Cast to u64 to make GCC stop complaining */
445 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
446 timeout = -1; /* infinite */
447 else {
448 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
449 timeout += ts.tv_sec * HZ;
450 }
451 }
452
453 if (sigmask) {
454 /* XXX: Don't preclude handling different sized sigset_t's. */
455 if (sigsetsize != sizeof(sigset_t))
456 return -EINVAL;
457 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
458 return -EFAULT;
459
460 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
461 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
462 }
463
464 ret = core_sys_select(n, inp, outp, exp, &timeout);
465
466 if (tsp) {
Andrew Morton643a6542006-02-11 17:55:52 -0800467 struct timespec rts;
468
David Woodhouse9f729492006-01-18 17:44:05 -0800469 if (current->personality & STICKY_TIMEOUTS)
470 goto sticky;
Andrew Morton643a6542006-02-11 17:55:52 -0800471 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
472 1000;
473 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800474 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800475 rts = ts;
476 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800477sticky:
478 /*
479 * If an application puts its timeval in read-only
480 * memory, we don't want the Linux-specific update to
481 * the timeval to cause a fault after the select has
482 * completed successfully. However, because we're not
483 * updating the timeval, we can't restart the system
484 * call.
485 */
486 if (ret == -ERESTARTNOHAND)
487 ret = -EINTR;
488 }
489 }
490
491 if (ret == -ERESTARTNOHAND) {
492 /*
493 * Don't restore the signal mask yet. Let do_signal() deliver
494 * the signal on the way back to userspace, before the signal
495 * mask is restored.
496 */
497 if (sigmask) {
498 memcpy(&current->saved_sigmask, &sigsaved,
499 sizeof(sigsaved));
500 set_thread_flag(TIF_RESTORE_SIGMASK);
501 }
502 } else if (sigmask)
503 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
504
505 return ret;
506}
507
508/*
509 * Most architectures can't handle 7-argument syscalls. So we provide a
510 * 6-argument version where the sixth argument is a pointer to a structure
511 * which has a pointer to the sigset_t itself followed by a size_t containing
512 * the sigset size.
513 */
514asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
515 fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
516{
517 size_t sigsetsize = 0;
518 sigset_t __user *up = NULL;
519
520 if (sig) {
521 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
Al Viroe110ab92006-02-01 05:26:09 -0500522 || __get_user(up, (sigset_t __user * __user *)sig)
David Woodhouse9f729492006-01-18 17:44:05 -0800523 || __get_user(sigsetsize,
Al Viroe110ab92006-02-01 05:26:09 -0500524 (size_t __user *)(sig+sizeof(void *))))
David Woodhouse9f729492006-01-18 17:44:05 -0800525 return -EFAULT;
526 }
527
528 return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
529}
530#endif /* TIF_RESTORE_SIGMASK */
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532struct poll_list {
533 struct poll_list *next;
534 int len;
535 struct pollfd entries[0];
536};
537
538#define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
539
540static void do_pollfd(unsigned int num, struct pollfd * fdpage,
541 poll_table ** pwait, int *count)
542{
543 int i;
544
545 for (i = 0; i < num; i++) {
546 int fd;
547 unsigned int mask;
548 struct pollfd *fdp;
549
550 mask = 0;
551 fdp = fdpage+i;
552 fd = fdp->fd;
553 if (fd >= 0) {
554 struct file * file = fget(fd);
555 mask = POLLNVAL;
556 if (file != NULL) {
557 mask = DEFAULT_POLLMASK;
558 if (file->f_op && file->f_op->poll)
559 mask = file->f_op->poll(file, *pwait);
560 mask &= fdp->events | POLLERR | POLLHUP;
561 fput(file);
562 }
563 if (mask) {
564 *pwait = NULL;
565 (*count)++;
566 }
567 }
568 fdp->revents = mask;
569 }
570}
571
572static int do_poll(unsigned int nfds, struct poll_list *list,
David Woodhouse9f729492006-01-18 17:44:05 -0800573 struct poll_wqueues *wait, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 int count = 0;
576 poll_table* pt = &wait->pt;
577
David Woodhouse9f729492006-01-18 17:44:05 -0800578 /* Optimise the no-wait case */
579 if (!(*timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pt = NULL;
581
582 for (;;) {
583 struct poll_list *walk;
David Woodhouse9f729492006-01-18 17:44:05 -0800584 long __timeout;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 set_current_state(TASK_INTERRUPTIBLE);
587 walk = list;
588 while(walk != NULL) {
589 do_pollfd( walk->len, walk->entries, &pt, &count);
590 walk = walk->next;
591 }
592 pt = NULL;
David Woodhouse9f729492006-01-18 17:44:05 -0800593 if (count || !*timeout || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595 count = wait->error;
596 if (count)
597 break;
David Woodhouse9f729492006-01-18 17:44:05 -0800598
599 if (*timeout < 0) {
600 /* Wait indefinitely */
601 __timeout = MAX_SCHEDULE_TIMEOUT;
602 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
603 /*
604 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
605 * a loop
606 */
607 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
608 *timeout -= __timeout;
609 } else {
610 __timeout = *timeout;
611 *timeout = 0;
612 }
613
614 __timeout = schedule_timeout(__timeout);
615 if (*timeout >= 0)
616 *timeout += __timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 __set_current_state(TASK_RUNNING);
619 return count;
620}
621
David Woodhouse9f729492006-01-18 17:44:05 -0800622int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct poll_wqueues table;
625 int fdcount, err;
626 unsigned int i;
627 struct poll_list *head;
628 struct poll_list *walk;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700629 struct fdtable *fdt;
Dipankar Sarmab8359962005-09-09 13:04:14 -0700630 int max_fdset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* Do a sanity check on nfds ... */
Dipankar Sarmab8359962005-09-09 13:04:14 -0700633 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700634 fdt = files_fdtable(current->files);
Dipankar Sarmab8359962005-09-09 13:04:14 -0700635 max_fdset = fdt->max_fdset;
636 rcu_read_unlock();
637 if (nfds > max_fdset && nfds > OPEN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -EINVAL;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 poll_initwait(&table);
641
642 head = NULL;
643 walk = NULL;
644 i = nfds;
645 err = -ENOMEM;
646 while(i!=0) {
647 struct poll_list *pp;
648 pp = kmalloc(sizeof(struct poll_list)+
649 sizeof(struct pollfd)*
650 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
651 GFP_KERNEL);
652 if(pp==NULL)
653 goto out_fds;
654 pp->next=NULL;
655 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
656 if (head == NULL)
657 head = pp;
658 else
659 walk->next = pp;
660
661 walk = pp;
662 if (copy_from_user(pp->entries, ufds + nfds-i,
663 sizeof(struct pollfd)*pp->len)) {
664 err = -EFAULT;
665 goto out_fds;
666 }
667 i -= pp->len;
668 }
David Woodhouse9f729492006-01-18 17:44:05 -0800669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 fdcount = do_poll(nfds, head, &table, timeout);
671
672 /* OK, now copy the revents fields back to user space. */
673 walk = head;
674 err = -EFAULT;
675 while(walk != NULL) {
676 struct pollfd *fds = walk->entries;
677 int j;
678
679 for (j=0; j < walk->len; j++, ufds++) {
680 if(__put_user(fds[j].revents, &ufds->revents))
681 goto out_fds;
682 }
683 walk = walk->next;
684 }
685 err = fdcount;
686 if (!fdcount && signal_pending(current))
687 err = -EINTR;
688out_fds:
689 walk = head;
690 while(walk!=NULL) {
691 struct poll_list *pp = walk->next;
692 kfree(walk);
693 walk = pp;
694 }
695 poll_freewait(&table);
696 return err;
697}
David Woodhouse9f729492006-01-18 17:44:05 -0800698
699asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
700 long timeout_msecs)
701{
702 s64 timeout_jiffies = 0;
703
704 if (timeout_msecs) {
705#if HZ > 1000
706 /* We can only overflow if HZ > 1000 */
707 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
708 timeout_jiffies = -1;
709 else
710#endif
711 timeout_jiffies = msecs_to_jiffies(timeout_msecs);
712 }
713
714 return do_sys_poll(ufds, nfds, &timeout_jiffies);
715}
716
717#ifdef TIF_RESTORE_SIGMASK
718asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
719 struct timespec __user *tsp, const sigset_t __user *sigmask,
720 size_t sigsetsize)
721{
722 sigset_t ksigmask, sigsaved;
723 struct timespec ts;
724 s64 timeout = -1;
725 int ret;
726
727 if (tsp) {
728 if (copy_from_user(&ts, tsp, sizeof(ts)))
729 return -EFAULT;
730
731 /* Cast to u64 to make GCC stop complaining */
732 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
733 timeout = -1; /* infinite */
734 else {
735 timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
736 timeout += ts.tv_sec * HZ;
737 }
738 }
739
740 if (sigmask) {
741 /* XXX: Don't preclude handling different sized sigset_t's. */
742 if (sigsetsize != sizeof(sigset_t))
743 return -EINVAL;
744 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
745 return -EFAULT;
746
747 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
748 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
749 }
750
751 ret = do_sys_poll(ufds, nfds, &timeout);
752
753 /* We can restart this syscall, usually */
754 if (ret == -EINTR) {
755 /*
756 * Don't restore the signal mask yet. Let do_signal() deliver
757 * the signal on the way back to userspace, before the signal
758 * mask is restored.
759 */
760 if (sigmask) {
761 memcpy(&current->saved_sigmask, &sigsaved,
762 sizeof(sigsaved));
763 set_thread_flag(TIF_RESTORE_SIGMASK);
764 }
765 ret = -ERESTARTNOHAND;
766 } else if (sigmask)
767 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
768
769 if (tsp && timeout >= 0) {
Andrew Morton643a6542006-02-11 17:55:52 -0800770 struct timespec rts;
771
David Woodhouse9f729492006-01-18 17:44:05 -0800772 if (current->personality & STICKY_TIMEOUTS)
773 goto sticky;
774 /* Yes, we know it's actually an s64, but it's also positive. */
Andrew Morton643a6542006-02-11 17:55:52 -0800775 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
776 1000;
777 rts.tv_sec = timeout;
Andrew Morton74910e62006-02-17 13:52:58 -0800778 if (timespec_compare(&rts, &ts) >= 0)
Andrew Morton643a6542006-02-11 17:55:52 -0800779 rts = ts;
780 if (copy_to_user(tsp, &rts, sizeof(rts))) {
David Woodhouse9f729492006-01-18 17:44:05 -0800781 sticky:
782 /*
783 * If an application puts its timeval in read-only
784 * memory, we don't want the Linux-specific update to
785 * the timeval to cause a fault after the select has
786 * completed successfully. However, because we're not
787 * updating the timeval, we can't restart the system
788 * call.
789 */
790 if (ret == -ERESTARTNOHAND && timeout >= 0)
791 ret = -EINTR;
792 }
793 }
794
795 return ret;
796}
797#endif /* TIF_RESTORE_SIGMASK */