Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / select.c
1 /*
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>
25 #include <linux/rcupdate.h>
26 #include <linux/kallsyms.h>
27
28 #include <asm/uaccess.h>
29
30 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
31 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
32
33 struct poll_table_page {
34         struct poll_table_page * next;
35         struct poll_table_entry * entry;
36         struct poll_table_entry entries[0];
37 };
38
39 #define POLL_TABLE_FULL(table) \
40         ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
41
42 /*
43  * Ok, Peter made a complicated, but straightforward multiple_wait() function.
44  * I have rewritten this, taking some shortcuts: This code may not be easy to
45  * follow, but it should be free of race-conditions, and it's practical. If you
46  * understand what I'm doing here, then you understand how the linux
47  * sleep/wakeup mechanism works.
48  *
49  * Two very simple procedures, poll_wait() and poll_freewait() make all the
50  * work.  poll_wait() is an inline-function defined in <linux/poll.h>,
51  * as all select/poll functions have to call it to add an entry to the
52  * poll table.
53  */
54 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
55                        poll_table *p);
56
57 void poll_initwait(struct poll_wqueues *pwq)
58 {
59         init_poll_funcptr(&pwq->pt, __pollwait);
60         pwq->error = 0;
61         pwq->table = NULL;
62         pwq->inline_index = 0;
63 }
64
65 EXPORT_SYMBOL(poll_initwait);
66
67 static void free_poll_entry(struct poll_table_entry *entry)
68 {
69         if (remove_wait_queue(entry->wait_address,&entry->wait) < 0)
70                 print_symbol("bad poll-entry for %s", (unsigned long) entry->filp->f_op->poll);
71         fput(entry->filp);
72 }
73
74 void poll_freewait(struct poll_wqueues *pwq)
75 {
76         struct poll_table_page * p = pwq->table;
77         int i;
78         for (i = 0; i < pwq->inline_index; i++)
79                 free_poll_entry(pwq->inline_entries + i);
80         while (p) {
81                 struct poll_table_entry * entry;
82                 struct poll_table_page *old;
83
84                 entry = p->entry;
85                 do {
86                         entry--;
87                         free_poll_entry(entry);
88                 } while (entry > p->entries);
89                 old = p;
90                 p = p->next;
91                 free_page((unsigned long) old);
92         }
93 }
94
95 EXPORT_SYMBOL(poll_freewait);
96
97 static struct poll_table_entry *poll_get_entry(poll_table *_p)
98 {
99         struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
100         struct poll_table_page *table = p->table;
101
102         if (p->inline_index < N_INLINE_POLL_ENTRIES)
103                 return p->inline_entries + p->inline_index++;
104
105         if (!table || POLL_TABLE_FULL(table)) {
106                 struct poll_table_page *new_table;
107
108                 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
109                 if (!new_table) {
110                         p->error = -ENOMEM;
111                         __set_current_state(TASK_RUNNING);
112                         return NULL;
113                 }
114                 new_table->entry = new_table->entries;
115                 new_table->next = table;
116                 p->table = new_table;
117                 table = new_table;
118         }
119
120         return table->entry++;
121 }
122
123 /* Add a new entry */
124 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
125                                 poll_table *p)
126 {
127         struct poll_table_entry *entry = poll_get_entry(p);
128         if (!entry)
129                 return;
130         get_file(filp);
131         entry->filp = filp;
132         entry->wait_address = wait_address;
133         init_waitqueue_entry(&entry->wait, current);
134         add_wait_queue(wait_address,&entry->wait);
135 }
136
137 #define FDS_IN(fds, n)          (fds->in + n)
138 #define FDS_OUT(fds, n)         (fds->out + n)
139 #define FDS_EX(fds, n)          (fds->ex + n)
140
141 #define BITS(fds, n)    (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
142
143 static int max_select_fd(unsigned long n, fd_set_bits *fds)
144 {
145         unsigned long *open_fds;
146         unsigned long set;
147         int max;
148         struct fdtable *fdt;
149
150         /* handle last in-complete long-word first */
151         set = ~(~0UL << (n & (__NFDBITS-1)));
152         n /= __NFDBITS;
153         fdt = files_fdtable(current->files);
154         open_fds = fdt->open_fds->fds_bits+n;
155         max = 0;
156         if (set) {
157                 set &= BITS(fds, n);
158                 if (set) {
159                         if (!(set & ~*open_fds))
160                                 goto get_max;
161                         return -EBADF;
162                 }
163         }
164         while (n) {
165                 open_fds--;
166                 n--;
167                 set = BITS(fds, n);
168                 if (!set)
169                         continue;
170                 if (set & ~*open_fds)
171                         return -EBADF;
172                 if (max)
173                         continue;
174 get_max:
175                 do {
176                         max++;
177                         set >>= 1;
178                 } while (set);
179                 max += n * __NFDBITS;
180         }
181
182         return max;
183 }
184
185 #define BIT(i)          (1UL << ((i)&(__NFDBITS-1)))
186 #define MEM(i,m)        ((m)+(unsigned)(i)/__NFDBITS)
187 #define ISSET(i,m)      (((i)&*(m)) != 0)
188 #define SET(i,m)        (*(m) |= (i))
189
190 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
191 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
192 #define POLLEX_SET (POLLPRI)
193
194 int do_select(int n, fd_set_bits *fds, s64 *timeout)
195 {
196         struct poll_wqueues table;
197         poll_table *wait;
198         int retval, i;
199
200         rcu_read_lock();
201         retval = max_select_fd(n, fds);
202         rcu_read_unlock();
203
204         if (retval < 0)
205                 return retval;
206         n = retval;
207
208         poll_initwait(&table);
209         wait = &table.pt;
210         if (!*timeout)
211                 wait = NULL;
212         retval = 0;
213         for (;;) {
214                 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
215                 long __timeout;
216
217                 set_current_state(TASK_INTERRUPTIBLE);
218
219                 inp = fds->in; outp = fds->out; exp = fds->ex;
220                 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
221
222                 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
223                         unsigned long in, out, ex, all_bits, bit = 1, mask, j;
224                         unsigned long res_in = 0, res_out = 0, res_ex = 0;
225                         const struct file_operations *f_op = NULL;
226                         struct file *file = NULL;
227
228                         in = *inp++; out = *outp++; ex = *exp++;
229                         all_bits = in | out | ex;
230                         if (all_bits == 0) {
231                                 i += __NFDBITS;
232                                 continue;
233                         }
234
235                         for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
236                                 int fput_needed;
237                                 if (i >= n)
238                                         break;
239                                 if (!(bit & all_bits))
240                                         continue;
241                                 file = fget_light(i, &fput_needed);
242                                 if (file) {
243                                         f_op = file->f_op;
244                                         mask = DEFAULT_POLLMASK;
245                                         if (f_op && f_op->poll)
246                                                 mask = (*f_op->poll)(file, retval ? NULL : wait);
247                                         fput_light(file, fput_needed);
248                                         if ((mask & POLLIN_SET) && (in & bit)) {
249                                                 res_in |= bit;
250                                                 retval++;
251                                         }
252                                         if ((mask & POLLOUT_SET) && (out & bit)) {
253                                                 res_out |= bit;
254                                                 retval++;
255                                         }
256                                         if ((mask & POLLEX_SET) && (ex & bit)) {
257                                                 res_ex |= bit;
258                                                 retval++;
259                                         }
260                                 }
261                                 cond_resched();
262                         }
263                         if (res_in)
264                                 *rinp = res_in;
265                         if (res_out)
266                                 *routp = res_out;
267                         if (res_ex)
268                                 *rexp = res_ex;
269                 }
270                 wait = NULL;
271                 if (retval || !*timeout || signal_pending(current))
272                         break;
273                 if(table.error) {
274                         retval = table.error;
275                         break;
276                 }
277
278                 if (*timeout < 0) {
279                         /* Wait indefinitely */
280                         __timeout = MAX_SCHEDULE_TIMEOUT;
281                 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
282                         /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
283                         __timeout = MAX_SCHEDULE_TIMEOUT - 1;
284                         *timeout -= __timeout;
285                 } else {
286                         __timeout = *timeout;
287                         *timeout = 0;
288                 }
289                 __timeout = schedule_timeout(__timeout);
290                 if (*timeout >= 0)
291                         *timeout += __timeout;
292         }
293         __set_current_state(TASK_RUNNING);
294
295         poll_freewait(&table);
296
297         return retval;
298 }
299
300 /*
301  * We can actually return ERESTARTSYS instead of EINTR, but I'd
302  * like to be certain this leads to no problems. So I return
303  * EINTR just for safety.
304  *
305  * Update: ERESTARTSYS breaks at least the xview clock binary, so
306  * I'm trying ERESTARTNOHAND which restart only when you want to.
307  */
308 #define MAX_SELECT_SECONDS \
309         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
310
311 static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
312                            fd_set __user *exp, s64 *timeout)
313 {
314         fd_set_bits fds;
315         void *bits;
316         int ret, max_fdset;
317         unsigned int size;
318         struct fdtable *fdt;
319         /* Allocate small arguments on the stack to save memory and be faster */
320         long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
321
322         ret = -EINVAL;
323         if (n < 0)
324                 goto out_nofds;
325
326         /* max_fdset can increase, so grab it once to avoid race */
327         rcu_read_lock();
328         fdt = files_fdtable(current->files);
329         max_fdset = fdt->max_fdset;
330         rcu_read_unlock();
331         if (n > max_fdset)
332                 n = max_fdset;
333
334         /*
335          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
336          * since we used fdset we need to allocate memory in units of
337          * long-words. 
338          */
339         size = FDS_BYTES(n);
340         bits = stack_fds;
341         if (size > sizeof(stack_fds) / 6) {
342                 /* Not enough space in on-stack array; must use kmalloc */
343                 ret = -ENOMEM;
344                 bits = kmalloc(6 * size, GFP_KERNEL);
345                 if (!bits)
346                         goto out_nofds;
347         }
348         fds.in      = bits;
349         fds.out     = bits +   size;
350         fds.ex      = bits + 2*size;
351         fds.res_in  = bits + 3*size;
352         fds.res_out = bits + 4*size;
353         fds.res_ex  = bits + 5*size;
354
355         if ((ret = get_fd_set(n, inp, fds.in)) ||
356             (ret = get_fd_set(n, outp, fds.out)) ||
357             (ret = get_fd_set(n, exp, fds.ex)))
358                 goto out;
359         zero_fd_set(n, fds.res_in);
360         zero_fd_set(n, fds.res_out);
361         zero_fd_set(n, fds.res_ex);
362
363         ret = do_select(n, &fds, timeout);
364
365         if (ret < 0)
366                 goto out;
367         if (!ret) {
368                 ret = -ERESTARTNOHAND;
369                 if (signal_pending(current))
370                         goto out;
371                 ret = 0;
372         }
373
374         if (set_fd_set(n, inp, fds.res_in) ||
375             set_fd_set(n, outp, fds.res_out) ||
376             set_fd_set(n, exp, fds.res_ex))
377                 ret = -EFAULT;
378
379 out:
380         if (bits != stack_fds)
381                 kfree(bits);
382 out_nofds:
383         return ret;
384 }
385
386 asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
387                         fd_set __user *exp, struct timeval __user *tvp)
388 {
389         s64 timeout = -1;
390         struct timeval tv;
391         int ret;
392
393         if (tvp) {
394                 if (copy_from_user(&tv, tvp, sizeof(tv)))
395                         return -EFAULT;
396
397                 if (tv.tv_sec < 0 || tv.tv_usec < 0)
398                         return -EINVAL;
399
400                 /* Cast to u64 to make GCC stop complaining */
401                 if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
402                         timeout = -1;   /* infinite */
403                 else {
404                         timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
405                         timeout += tv.tv_sec * HZ;
406                 }
407         }
408
409         ret = core_sys_select(n, inp, outp, exp, &timeout);
410
411         if (tvp) {
412                 struct timeval rtv;
413
414                 if (current->personality & STICKY_TIMEOUTS)
415                         goto sticky;
416                 rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
417                 rtv.tv_sec = timeout;
418                 if (timeval_compare(&rtv, &tv) >= 0)
419                         rtv = tv;
420                 if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
421 sticky:
422                         /*
423                          * If an application puts its timeval in read-only
424                          * memory, we don't want the Linux-specific update to
425                          * the timeval to cause a fault after the select has
426                          * completed successfully. However, because we're not
427                          * updating the timeval, we can't restart the system
428                          * call.
429                          */
430                         if (ret == -ERESTARTNOHAND)
431                                 ret = -EINTR;
432                 }
433         }
434
435         return ret;
436 }
437
438 #ifdef TIF_RESTORE_SIGMASK
439 asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
440                 fd_set __user *exp, struct timespec __user *tsp,
441                 const sigset_t __user *sigmask, size_t sigsetsize)
442 {
443         s64 timeout = MAX_SCHEDULE_TIMEOUT;
444         sigset_t ksigmask, sigsaved;
445         struct timespec ts;
446         int ret;
447
448         if (tsp) {
449                 if (copy_from_user(&ts, tsp, sizeof(ts)))
450                         return -EFAULT;
451
452                 if (ts.tv_sec < 0 || ts.tv_nsec < 0)
453                         return -EINVAL;
454
455                 /* Cast to u64 to make GCC stop complaining */
456                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
457                         timeout = -1;   /* infinite */
458                 else {
459                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
460                         timeout += ts.tv_sec * HZ;
461                 }
462         }
463
464         if (sigmask) {
465                 /* XXX: Don't preclude handling different sized sigset_t's.  */
466                 if (sigsetsize != sizeof(sigset_t))
467                         return -EINVAL;
468                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
469                         return -EFAULT;
470
471                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
472                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
473         }
474
475         ret = core_sys_select(n, inp, outp, exp, &timeout);
476
477         if (tsp) {
478                 struct timespec rts;
479
480                 if (current->personality & STICKY_TIMEOUTS)
481                         goto sticky;
482                 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
483                                                 1000;
484                 rts.tv_sec = timeout;
485                 if (timespec_compare(&rts, &ts) >= 0)
486                         rts = ts;
487                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
488 sticky:
489                         /*
490                          * If an application puts its timeval in read-only
491                          * memory, we don't want the Linux-specific update to
492                          * the timeval to cause a fault after the select has
493                          * completed successfully. However, because we're not
494                          * updating the timeval, we can't restart the system
495                          * call.
496                          */
497                         if (ret == -ERESTARTNOHAND)
498                                 ret = -EINTR;
499                 }
500         }
501
502         if (ret == -ERESTARTNOHAND) {
503                 /*
504                  * Don't restore the signal mask yet. Let do_signal() deliver
505                  * the signal on the way back to userspace, before the signal
506                  * mask is restored.
507                  */
508                 if (sigmask) {
509                         memcpy(&current->saved_sigmask, &sigsaved,
510                                         sizeof(sigsaved));
511                         set_thread_flag(TIF_RESTORE_SIGMASK);
512                 }
513         } else if (sigmask)
514                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
515
516         return ret;
517 }
518
519 /*
520  * Most architectures can't handle 7-argument syscalls. So we provide a
521  * 6-argument version where the sixth argument is a pointer to a structure
522  * which has a pointer to the sigset_t itself followed by a size_t containing
523  * the sigset size.
524  */
525 asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
526         fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
527 {
528         size_t sigsetsize = 0;
529         sigset_t __user *up = NULL;
530
531         if (sig) {
532                 if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
533                     || __get_user(up, (sigset_t __user * __user *)sig)
534                     || __get_user(sigsetsize,
535                                 (size_t __user *)(sig+sizeof(void *))))
536                         return -EFAULT;
537         }
538
539         return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
540 }
541 #endif /* TIF_RESTORE_SIGMASK */
542
543 struct poll_list {
544         struct poll_list *next;
545         int len;
546         struct pollfd entries[0];
547 };
548
549 #define POLLFD_PER_PAGE  ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
550
551 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
552         poll_table ** pwait, int *count)
553 {
554         int i;
555
556         for (i = 0; i < num; i++) {
557                 int fd;
558                 unsigned int mask;
559                 struct pollfd *fdp;
560
561                 mask = 0;
562                 fdp = fdpage+i;
563                 fd = fdp->fd;
564                 if (fd >= 0) {
565                         int fput_needed;
566                         struct file * 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 &= fdp->events | POLLERR | POLLHUP;
573                                 fput_light(file, fput_needed);
574                         }
575                         if (mask) {
576                                 *pwait = NULL;
577                                 (*count)++;
578                         }
579                 }
580                 fdp->revents = mask;
581         }
582 }
583
584 static int do_poll(unsigned int nfds,  struct poll_list *list,
585                    struct poll_wqueues *wait, s64 *timeout)
586 {
587         int count = 0;
588         poll_table* pt = &wait->pt;
589
590         /* Optimise the no-wait case */
591         if (!(*timeout))
592                 pt = NULL;
593  
594         for (;;) {
595                 struct poll_list *walk;
596                 long __timeout;
597
598                 set_current_state(TASK_INTERRUPTIBLE);
599                 walk = list;
600                 while(walk != NULL) {
601                         do_pollfd( walk->len, walk->entries, &pt, &count);
602                         walk = walk->next;
603                 }
604                 pt = NULL;
605                 if (count || !*timeout || signal_pending(current))
606                         break;
607                 count = wait->error;
608                 if (count)
609                         break;
610
611                 if (*timeout < 0) {
612                         /* Wait indefinitely */
613                         __timeout = MAX_SCHEDULE_TIMEOUT;
614                 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
615                         /*
616                          * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
617                          * a loop
618                          */
619                         __timeout = MAX_SCHEDULE_TIMEOUT - 1;
620                         *timeout -= __timeout;
621                 } else {
622                         __timeout = *timeout;
623                         *timeout = 0;
624                 }
625
626                 __timeout = schedule_timeout(__timeout);
627                 if (*timeout >= 0)
628                         *timeout += __timeout;
629         }
630         __set_current_state(TASK_RUNNING);
631         return count;
632 }
633
634 #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list))  / \
635                         sizeof(struct pollfd))
636
637 int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
638 {
639         struct poll_wqueues table;
640         int fdcount, err;
641         unsigned int i;
642         struct poll_list *head;
643         struct poll_list *walk;
644         struct fdtable *fdt;
645         int max_fdset;
646         /* Allocate small arguments on the stack to save memory and be
647            faster - use long to make sure the buffer is aligned properly
648            on 64 bit archs to avoid unaligned access */
649         long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
650         struct poll_list *stack_pp = NULL;
651
652         /* Do a sanity check on nfds ... */
653         rcu_read_lock();
654         fdt = files_fdtable(current->files);
655         max_fdset = fdt->max_fdset;
656         rcu_read_unlock();
657         if (nfds > max_fdset && nfds > OPEN_MAX)
658                 return -EINVAL;
659
660         poll_initwait(&table);
661
662         head = NULL;
663         walk = NULL;
664         i = nfds;
665         err = -ENOMEM;
666         while(i!=0) {
667                 struct poll_list *pp;
668                 int num, size;
669                 if (stack_pp == NULL)
670                         num = N_STACK_PPS;
671                 else
672                         num = POLLFD_PER_PAGE;
673                 if (num > i)
674                         num = i;
675                 size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
676                 if (!stack_pp)
677                         stack_pp = pp = (struct poll_list *)stack_pps;
678                 else {
679                         pp = kmalloc(size, GFP_KERNEL);
680                         if (!pp)
681                                 goto out_fds;
682                 }
683                 pp->next=NULL;
684                 pp->len = num;
685                 if (head == NULL)
686                         head = pp;
687                 else
688                         walk->next = pp;
689
690                 walk = pp;
691                 if (copy_from_user(pp->entries, ufds + nfds-i, 
692                                 sizeof(struct pollfd)*num)) {
693                         err = -EFAULT;
694                         goto out_fds;
695                 }
696                 i -= pp->len;
697         }
698
699         fdcount = do_poll(nfds, head, &table, timeout);
700
701         /* OK, now copy the revents fields back to user space. */
702         walk = head;
703         err = -EFAULT;
704         while(walk != NULL) {
705                 struct pollfd *fds = walk->entries;
706                 int j;
707
708                 for (j=0; j < walk->len; j++, ufds++) {
709                         if(__put_user(fds[j].revents, &ufds->revents))
710                                 goto out_fds;
711                 }
712                 walk = walk->next;
713         }
714         err = fdcount;
715         if (!fdcount && signal_pending(current))
716                 err = -EINTR;
717 out_fds:
718         walk = head;
719         while(walk!=NULL) {
720                 struct poll_list *pp = walk->next;
721                 if (walk != stack_pp)
722                         kfree(walk);
723                 walk = pp;
724         }
725         poll_freewait(&table);
726         return err;
727 }
728
729 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
730                         long timeout_msecs)
731 {
732         s64 timeout_jiffies = 0;
733
734         if (timeout_msecs) {
735 #if HZ > 1000
736                 /* We can only overflow if HZ > 1000 */
737                 if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
738                         timeout_jiffies = -1;
739                 else
740 #endif
741                         timeout_jiffies = msecs_to_jiffies(timeout_msecs);
742         }
743
744         return do_sys_poll(ufds, nfds, &timeout_jiffies);
745 }
746
747 #ifdef TIF_RESTORE_SIGMASK
748 asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
749         struct timespec __user *tsp, const sigset_t __user *sigmask,
750         size_t sigsetsize)
751 {
752         sigset_t ksigmask, sigsaved;
753         struct timespec ts;
754         s64 timeout = -1;
755         int ret;
756
757         if (tsp) {
758                 if (copy_from_user(&ts, tsp, sizeof(ts)))
759                         return -EFAULT;
760
761                 /* Cast to u64 to make GCC stop complaining */
762                 if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
763                         timeout = -1;   /* infinite */
764                 else {
765                         timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
766                         timeout += ts.tv_sec * HZ;
767                 }
768         }
769
770         if (sigmask) {
771                 /* XXX: Don't preclude handling different sized sigset_t's.  */
772                 if (sigsetsize != sizeof(sigset_t))
773                         return -EINVAL;
774                 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
775                         return -EFAULT;
776
777                 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
778                 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
779         }
780
781         ret = do_sys_poll(ufds, nfds, &timeout);
782
783         /* We can restart this syscall, usually */
784         if (ret == -EINTR) {
785                 /*
786                  * Don't restore the signal mask yet. Let do_signal() deliver
787                  * the signal on the way back to userspace, before the signal
788                  * mask is restored.
789                  */
790                 if (sigmask) {
791                         memcpy(&current->saved_sigmask, &sigsaved,
792                                         sizeof(sigsaved));
793                         set_thread_flag(TIF_RESTORE_SIGMASK);
794                 }
795                 ret = -ERESTARTNOHAND;
796         } else if (sigmask)
797                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
798
799         if (tsp && timeout >= 0) {
800                 struct timespec rts;
801
802                 if (current->personality & STICKY_TIMEOUTS)
803                         goto sticky;
804                 /* Yes, we know it's actually an s64, but it's also positive. */
805                 rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
806                                                 1000;
807                 rts.tv_sec = timeout;
808                 if (timespec_compare(&rts, &ts) >= 0)
809                         rts = ts;
810                 if (copy_to_user(tsp, &rts, sizeof(rts))) {
811                 sticky:
812                         /*
813                          * If an application puts its timeval in read-only
814                          * memory, we don't want the Linux-specific update to
815                          * the timeval to cause a fault after the select has
816                          * completed successfully. However, because we're not
817                          * updating the timeval, we can't restart the system
818                          * call.
819                          */
820                         if (ret == -ERESTARTNOHAND && timeout >= 0)
821                                 ret = -EINTR;
822                 }
823         }
824
825         return ret;
826 }
827 #endif /* TIF_RESTORE_SIGMASK */