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