thinkpad-acpi: R52 brightness_mode has been confirmed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / select.c
blobd575e4fb495ce705cc8cdd9a7593bf8baa3ee42d
1 /*
2 * This file contains the procedures for the handling of select and poll
4 * Created for Linux based loosely upon Mathius Lattner's minix
5 * patches by Peter MacDonald. Heavily edited by Linus.
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.
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).
17 #include <linux/kernel.h>
18 #include <linux/syscalls.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/poll.h>
22 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
23 #include <linux/file.h>
24 #include <linux/fdtable.h>
25 #include <linux/fs.h>
26 #include <linux/rcupdate.h>
28 #include <asm/uaccess.h>
30 struct poll_table_page {
31 struct poll_table_page * next;
32 struct poll_table_entry * entry;
33 struct poll_table_entry entries[0];
36 #define POLL_TABLE_FULL(table) \
37 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
40 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
41 * I have rewritten this, taking some shortcuts: This code may not be easy to
42 * follow, but it should be free of race-conditions, and it's practical. If you
43 * understand what I'm doing here, then you understand how the linux
44 * sleep/wakeup mechanism works.
46 * Two very simple procedures, poll_wait() and poll_freewait() make all the
47 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
48 * as all select/poll functions have to call it to add an entry to the
49 * poll table.
51 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
52 poll_table *p);
54 void poll_initwait(struct poll_wqueues *pwq)
56 init_poll_funcptr(&pwq->pt, __pollwait);
57 pwq->error = 0;
58 pwq->table = NULL;
59 pwq->inline_index = 0;
62 EXPORT_SYMBOL(poll_initwait);
64 static void free_poll_entry(struct poll_table_entry *entry)
66 remove_wait_queue(entry->wait_address, &entry->wait);
67 fput(entry->filp);
70 void poll_freewait(struct poll_wqueues *pwq)
72 struct poll_table_page * p = pwq->table;
73 int i;
74 for (i = 0; i < pwq->inline_index; i++)
75 free_poll_entry(pwq->inline_entries + i);
76 while (p) {
77 struct poll_table_entry * entry;
78 struct poll_table_page *old;
80 entry = p->entry;
81 do {
82 entry--;
83 free_poll_entry(entry);
84 } while (entry > p->entries);
85 old = p;
86 p = p->next;
87 free_page((unsigned long) old);
91 EXPORT_SYMBOL(poll_freewait);
93 static struct poll_table_entry *poll_get_entry(poll_table *_p)
95 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96 struct poll_table_page *table = p->table;
98 if (p->inline_index < N_INLINE_POLL_ENTRIES)
99 return p->inline_entries + p->inline_index++;
101 if (!table || POLL_TABLE_FULL(table)) {
102 struct poll_table_page *new_table;
104 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
105 if (!new_table) {
106 p->error = -ENOMEM;
107 __set_current_state(TASK_RUNNING);
108 return NULL;
110 new_table->entry = new_table->entries;
111 new_table->next = table;
112 p->table = new_table;
113 table = new_table;
116 return table->entry++;
119 /* Add a new entry */
120 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
121 poll_table *p)
123 struct poll_table_entry *entry = poll_get_entry(p);
124 if (!entry)
125 return;
126 get_file(filp);
127 entry->filp = filp;
128 entry->wait_address = wait_address;
129 init_waitqueue_entry(&entry->wait, current);
130 add_wait_queue(wait_address, &entry->wait);
133 #define FDS_IN(fds, n) (fds->in + n)
134 #define FDS_OUT(fds, n) (fds->out + n)
135 #define FDS_EX(fds, n) (fds->ex + n)
137 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
139 static int max_select_fd(unsigned long n, fd_set_bits *fds)
141 unsigned long *open_fds;
142 unsigned long set;
143 int max;
144 struct fdtable *fdt;
146 /* handle last in-complete long-word first */
147 set = ~(~0UL << (n & (__NFDBITS-1)));
148 n /= __NFDBITS;
149 fdt = files_fdtable(current->files);
150 open_fds = fdt->open_fds->fds_bits+n;
151 max = 0;
152 if (set) {
153 set &= BITS(fds, n);
154 if (set) {
155 if (!(set & ~*open_fds))
156 goto get_max;
157 return -EBADF;
160 while (n) {
161 open_fds--;
162 n--;
163 set = BITS(fds, n);
164 if (!set)
165 continue;
166 if (set & ~*open_fds)
167 return -EBADF;
168 if (max)
169 continue;
170 get_max:
171 do {
172 max++;
173 set >>= 1;
174 } while (set);
175 max += n * __NFDBITS;
178 return max;
181 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
182 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
183 #define POLLEX_SET (POLLPRI)
185 int do_select(int n, fd_set_bits *fds, s64 *timeout)
187 struct poll_wqueues table;
188 poll_table *wait;
189 int retval, i;
191 rcu_read_lock();
192 retval = max_select_fd(n, fds);
193 rcu_read_unlock();
195 if (retval < 0)
196 return retval;
197 n = retval;
199 poll_initwait(&table);
200 wait = &table.pt;
201 if (!*timeout)
202 wait = NULL;
203 retval = 0;
204 for (;;) {
205 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
206 long __timeout;
208 set_current_state(TASK_INTERRUPTIBLE);
210 inp = fds->in; outp = fds->out; exp = fds->ex;
211 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
213 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
214 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
215 unsigned long res_in = 0, res_out = 0, res_ex = 0;
216 const struct file_operations *f_op = NULL;
217 struct file *file = NULL;
219 in = *inp++; out = *outp++; ex = *exp++;
220 all_bits = in | out | ex;
221 if (all_bits == 0) {
222 i += __NFDBITS;
223 continue;
226 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
227 int fput_needed;
228 if (i >= n)
229 break;
230 if (!(bit & all_bits))
231 continue;
232 file = fget_light(i, &fput_needed);
233 if (file) {
234 f_op = file->f_op;
235 mask = DEFAULT_POLLMASK;
236 if (f_op && f_op->poll)
237 mask = (*f_op->poll)(file, retval ? NULL : wait);
238 fput_light(file, fput_needed);
239 if ((mask & POLLIN_SET) && (in & bit)) {
240 res_in |= bit;
241 retval++;
243 if ((mask & POLLOUT_SET) && (out & bit)) {
244 res_out |= bit;
245 retval++;
247 if ((mask & POLLEX_SET) && (ex & bit)) {
248 res_ex |= bit;
249 retval++;
253 if (res_in)
254 *rinp = res_in;
255 if (res_out)
256 *routp = res_out;
257 if (res_ex)
258 *rexp = res_ex;
259 cond_resched();
261 wait = NULL;
262 if (retval || !*timeout || signal_pending(current))
263 break;
264 if (table.error) {
265 retval = table.error;
266 break;
269 if (*timeout < 0) {
270 /* Wait indefinitely */
271 __timeout = MAX_SCHEDULE_TIMEOUT;
272 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
273 /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
274 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
275 *timeout -= __timeout;
276 } else {
277 __timeout = *timeout;
278 *timeout = 0;
280 __timeout = schedule_timeout(__timeout);
281 if (*timeout >= 0)
282 *timeout += __timeout;
284 __set_current_state(TASK_RUNNING);
286 poll_freewait(&table);
288 return retval;
292 * We can actually return ERESTARTSYS instead of EINTR, but I'd
293 * like to be certain this leads to no problems. So I return
294 * EINTR just for safety.
296 * Update: ERESTARTSYS breaks at least the xview clock binary, so
297 * I'm trying ERESTARTNOHAND which restart only when you want to.
299 #define MAX_SELECT_SECONDS \
300 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
302 int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
303 fd_set __user *exp, s64 *timeout)
305 fd_set_bits fds;
306 void *bits;
307 int ret, max_fds;
308 unsigned int size;
309 struct fdtable *fdt;
310 /* Allocate small arguments on the stack to save memory and be faster */
311 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
313 ret = -EINVAL;
314 if (n < 0)
315 goto out_nofds;
317 /* max_fds can increase, so grab it once to avoid race */
318 rcu_read_lock();
319 fdt = files_fdtable(current->files);
320 max_fds = fdt->max_fds;
321 rcu_read_unlock();
322 if (n > max_fds)
323 n = max_fds;
326 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
327 * since we used fdset we need to allocate memory in units of
328 * long-words.
330 size = FDS_BYTES(n);
331 bits = stack_fds;
332 if (size > sizeof(stack_fds) / 6) {
333 /* Not enough space in on-stack array; must use kmalloc */
334 ret = -ENOMEM;
335 bits = kmalloc(6 * size, GFP_KERNEL);
336 if (!bits)
337 goto out_nofds;
339 fds.in = bits;
340 fds.out = bits + size;
341 fds.ex = bits + 2*size;
342 fds.res_in = bits + 3*size;
343 fds.res_out = bits + 4*size;
344 fds.res_ex = bits + 5*size;
346 if ((ret = get_fd_set(n, inp, fds.in)) ||
347 (ret = get_fd_set(n, outp, fds.out)) ||
348 (ret = get_fd_set(n, exp, fds.ex)))
349 goto out;
350 zero_fd_set(n, fds.res_in);
351 zero_fd_set(n, fds.res_out);
352 zero_fd_set(n, fds.res_ex);
354 ret = do_select(n, &fds, timeout);
356 if (ret < 0)
357 goto out;
358 if (!ret) {
359 ret = -ERESTARTNOHAND;
360 if (signal_pending(current))
361 goto out;
362 ret = 0;
365 if (set_fd_set(n, inp, fds.res_in) ||
366 set_fd_set(n, outp, fds.res_out) ||
367 set_fd_set(n, exp, fds.res_ex))
368 ret = -EFAULT;
370 out:
371 if (bits != stack_fds)
372 kfree(bits);
373 out_nofds:
374 return ret;
377 SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
378 fd_set __user *, exp, struct timeval __user *, tvp)
380 s64 timeout = -1;
381 struct timeval tv;
382 int ret;
384 if (tvp) {
385 if (copy_from_user(&tv, tvp, sizeof(tv)))
386 return -EFAULT;
388 if (tv.tv_sec < 0 || tv.tv_usec < 0)
389 return -EINVAL;
391 /* Cast to u64 to make GCC stop complaining */
392 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
393 timeout = -1; /* infinite */
394 else {
395 timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
396 timeout += tv.tv_sec * HZ;
400 ret = core_sys_select(n, inp, outp, exp, &timeout);
402 if (tvp) {
403 struct timeval rtv;
405 if (current->personality & STICKY_TIMEOUTS)
406 goto sticky;
407 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
408 rtv.tv_sec = timeout;
409 if (timeval_compare(&rtv, &tv) >= 0)
410 rtv = tv;
411 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
412 sticky:
414 * If an application puts its timeval in read-only
415 * memory, we don't want the Linux-specific update to
416 * the timeval to cause a fault after the select has
417 * completed successfully. However, because we're not
418 * updating the timeval, we can't restart the system
419 * call.
421 if (ret == -ERESTARTNOHAND)
422 ret = -EINTR;
426 return ret;
429 #ifdef HAVE_SET_RESTORE_SIGMASK
430 static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
431 fd_set __user *exp, struct timespec __user *tsp,
432 const sigset_t __user *sigmask, size_t sigsetsize)
434 s64 timeout = MAX_SCHEDULE_TIMEOUT;
435 sigset_t ksigmask, sigsaved;
436 struct timespec ts;
437 int ret;
439 if (tsp) {
440 if (copy_from_user(&ts, tsp, sizeof(ts)))
441 return -EFAULT;
443 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
444 return -EINVAL;
446 /* Cast to u64 to make GCC stop complaining */
447 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
448 timeout = -1; /* infinite */
449 else {
450 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
451 timeout += ts.tv_sec * HZ;
455 if (sigmask) {
456 /* XXX: Don't preclude handling different sized sigset_t's. */
457 if (sigsetsize != sizeof(sigset_t))
458 return -EINVAL;
459 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
460 return -EFAULT;
462 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
463 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
466 ret = core_sys_select(n, inp, outp, exp, &timeout);
468 if (tsp) {
469 struct timespec rts;
471 if (current->personality & STICKY_TIMEOUTS)
472 goto sticky;
473 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
474 1000;
475 rts.tv_sec = timeout;
476 if (timespec_compare(&rts, &ts) >= 0)
477 rts = ts;
478 if (copy_to_user(tsp, &rts, sizeof(rts))) {
479 sticky:
481 * If an application puts its timeval in read-only
482 * memory, we don't want the Linux-specific update to
483 * the timeval to cause a fault after the select has
484 * completed successfully. However, because we're not
485 * updating the timeval, we can't restart the system
486 * call.
488 if (ret == -ERESTARTNOHAND)
489 ret = -EINTR;
493 if (ret == -ERESTARTNOHAND) {
495 * Don't restore the signal mask yet. Let do_signal() deliver
496 * the signal on the way back to userspace, before the signal
497 * mask is restored.
499 if (sigmask) {
500 memcpy(&current->saved_sigmask, &sigsaved,
501 sizeof(sigsaved));
502 set_restore_sigmask();
504 } else if (sigmask)
505 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
507 return ret;
511 * Most architectures can't handle 7-argument syscalls. So we provide a
512 * 6-argument version where the sixth argument is a pointer to a structure
513 * which has a pointer to the sigset_t itself followed by a size_t containing
514 * the sigset size.
516 SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
517 fd_set __user *, exp, struct timespec __user *, tsp,
518 void __user *, sig)
520 size_t sigsetsize = 0;
521 sigset_t __user *up = NULL;
523 if (sig) {
524 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
525 || __get_user(up, (sigset_t __user * __user *)sig)
526 || __get_user(sigsetsize,
527 (size_t __user *)(sig+sizeof(void *))))
528 return -EFAULT;
531 return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
533 #endif /* HAVE_SET_RESTORE_SIGMASK */
535 struct poll_list {
536 struct poll_list *next;
537 int len;
538 struct pollfd entries[0];
541 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
544 * Fish for pollable events on the pollfd->fd file descriptor. We're only
545 * interested in events matching the pollfd->events mask, and the result
546 * matching that mask is both recorded in pollfd->revents and returned. The
547 * pwait poll_table will be used by the fd-provided poll handler for waiting,
548 * if non-NULL.
550 static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
552 unsigned int mask;
553 int fd;
555 mask = 0;
556 fd = pollfd->fd;
557 if (fd >= 0) {
558 int fput_needed;
559 struct file * file;
561 file = fget_light(fd, &fput_needed);
562 mask = POLLNVAL;
563 if (file != NULL) {
564 mask = DEFAULT_POLLMASK;
565 if (file->f_op && file->f_op->poll)
566 mask = file->f_op->poll(file, pwait);
567 /* Mask out unneeded events. */
568 mask &= pollfd->events | POLLERR | POLLHUP;
569 fput_light(file, fput_needed);
572 pollfd->revents = mask;
574 return mask;
577 static int do_poll(unsigned int nfds, struct poll_list *list,
578 struct poll_wqueues *wait, s64 *timeout)
580 int count = 0;
581 poll_table* pt = &wait->pt;
583 /* Optimise the no-wait case */
584 if (!(*timeout))
585 pt = NULL;
587 for (;;) {
588 struct poll_list *walk;
589 long __timeout;
591 set_current_state(TASK_INTERRUPTIBLE);
592 for (walk = list; walk != NULL; walk = walk->next) {
593 struct pollfd * pfd, * pfd_end;
595 pfd = walk->entries;
596 pfd_end = pfd + walk->len;
597 for (; pfd != pfd_end; pfd++) {
599 * Fish for events. If we found one, record it
600 * and kill the poll_table, so we don't
601 * needlessly register any other waiters after
602 * this. They'll get immediately deregistered
603 * when we break out and return.
605 if (do_pollfd(pfd, pt)) {
606 count++;
607 pt = NULL;
612 * All waiters have already been registered, so don't provide
613 * a poll_table to them on the next loop iteration.
615 pt = NULL;
616 if (!count) {
617 count = wait->error;
618 if (signal_pending(current))
619 count = -EINTR;
621 if (count || !*timeout)
622 break;
624 if (*timeout < 0) {
625 /* Wait indefinitely */
626 __timeout = MAX_SCHEDULE_TIMEOUT;
627 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
629 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
630 * a loop
632 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
633 *timeout -= __timeout;
634 } else {
635 __timeout = *timeout;
636 *timeout = 0;
639 __timeout = schedule_timeout(__timeout);
640 if (*timeout >= 0)
641 *timeout += __timeout;
643 __set_current_state(TASK_RUNNING);
644 return count;
647 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
648 sizeof(struct pollfd))
650 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
652 struct poll_wqueues table;
653 int err = -EFAULT, fdcount, len, size;
654 /* Allocate small arguments on the stack to save memory and be
655 faster - use long to make sure the buffer is aligned properly
656 on 64 bit archs to avoid unaligned access */
657 long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
658 struct poll_list *const head = (struct poll_list *)stack_pps;
659 struct poll_list *walk = head;
660 unsigned long todo = nfds;
662 if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
663 return -EINVAL;
665 len = min_t(unsigned int, nfds, N_STACK_PPS);
666 for (;;) {
667 walk->next = NULL;
668 walk->len = len;
669 if (!len)
670 break;
672 if (copy_from_user(walk->entries, ufds + nfds-todo,
673 sizeof(struct pollfd) * walk->len))
674 goto out_fds;
676 todo -= walk->len;
677 if (!todo)
678 break;
680 len = min(todo, POLLFD_PER_PAGE);
681 size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
682 walk = walk->next = kmalloc(size, GFP_KERNEL);
683 if (!walk) {
684 err = -ENOMEM;
685 goto out_fds;
689 poll_initwait(&table);
690 fdcount = do_poll(nfds, head, &table, timeout);
691 poll_freewait(&table);
693 for (walk = head; walk; walk = walk->next) {
694 struct pollfd *fds = walk->entries;
695 int j;
697 for (j = 0; j < walk->len; j++, ufds++)
698 if (__put_user(fds[j].revents, &ufds->revents))
699 goto out_fds;
702 err = fdcount;
703 out_fds:
704 walk = head->next;
705 while (walk) {
706 struct poll_list *pos = walk;
707 walk = walk->next;
708 kfree(pos);
711 return err;
714 static long do_restart_poll(struct restart_block *restart_block)
716 struct pollfd __user *ufds = (struct pollfd __user*)restart_block->arg0;
717 int nfds = restart_block->arg1;
718 s64 timeout = ((s64)restart_block->arg3<<32) | (s64)restart_block->arg2;
719 int ret;
721 ret = do_sys_poll(ufds, nfds, &timeout);
722 if (ret == -EINTR) {
723 restart_block->fn = do_restart_poll;
724 restart_block->arg2 = timeout & 0xFFFFFFFF;
725 restart_block->arg3 = (u64)timeout >> 32;
726 ret = -ERESTART_RESTARTBLOCK;
728 return ret;
731 SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
732 long, timeout_msecs)
734 s64 timeout_jiffies;
735 int ret;
737 if (timeout_msecs > 0) {
738 #if HZ > 1000
739 /* We can only overflow if HZ > 1000 */
740 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
741 timeout_jiffies = -1;
742 else
743 #endif
744 timeout_jiffies = msecs_to_jiffies(timeout_msecs) + 1;
745 } else {
746 /* Infinite (< 0) or no (0) timeout */
747 timeout_jiffies = timeout_msecs;
750 ret = do_sys_poll(ufds, nfds, &timeout_jiffies);
751 if (ret == -EINTR) {
752 struct restart_block *restart_block;
753 restart_block = &current_thread_info()->restart_block;
754 restart_block->fn = do_restart_poll;
755 restart_block->arg0 = (unsigned long)ufds;
756 restart_block->arg1 = nfds;
757 restart_block->arg2 = timeout_jiffies & 0xFFFFFFFF;
758 restart_block->arg3 = (u64)timeout_jiffies >> 32;
759 ret = -ERESTART_RESTARTBLOCK;
761 return ret;
764 #ifdef HAVE_SET_RESTORE_SIGMASK
765 SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
766 struct timespec __user *, tsp, const sigset_t __user *, sigmask,
767 size_t, sigsetsize)
769 sigset_t ksigmask, sigsaved;
770 struct timespec ts;
771 s64 timeout = -1;
772 int ret;
774 if (tsp) {
775 if (copy_from_user(&ts, tsp, sizeof(ts)))
776 return -EFAULT;
778 /* Cast to u64 to make GCC stop complaining */
779 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
780 timeout = -1; /* infinite */
781 else {
782 timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
783 timeout += ts.tv_sec * HZ;
787 if (sigmask) {
788 /* XXX: Don't preclude handling different sized sigset_t's. */
789 if (sigsetsize != sizeof(sigset_t))
790 return -EINVAL;
791 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
792 return -EFAULT;
794 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
795 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
798 ret = do_sys_poll(ufds, nfds, &timeout);
800 /* We can restart this syscall, usually */
801 if (ret == -EINTR) {
803 * Don't restore the signal mask yet. Let do_signal() deliver
804 * the signal on the way back to userspace, before the signal
805 * mask is restored.
807 if (sigmask) {
808 memcpy(&current->saved_sigmask, &sigsaved,
809 sizeof(sigsaved));
810 set_restore_sigmask();
812 ret = -ERESTARTNOHAND;
813 } else if (sigmask)
814 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
816 if (tsp && timeout >= 0) {
817 struct timespec rts;
819 if (current->personality & STICKY_TIMEOUTS)
820 goto sticky;
821 /* Yes, we know it's actually an s64, but it's also positive. */
822 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
823 1000;
824 rts.tv_sec = timeout;
825 if (timespec_compare(&rts, &ts) >= 0)
826 rts = ts;
827 if (copy_to_user(tsp, &rts, sizeof(rts))) {
828 sticky:
830 * If an application puts its timeval in read-only
831 * memory, we don't want the Linux-specific update to
832 * the timeval to cause a fault after the select has
833 * completed successfully. However, because we're not
834 * updating the timeval, we can't restart the system
835 * call.
837 if (ret == -ERESTARTNOHAND && timeout >= 0)
838 ret = -EINTR;
842 return ret;
844 #endif /* HAVE_SET_RESTORE_SIGMASK */