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