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> |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 27 | #include <linux/hrtimer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/uaccess.h> |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct poll_table_page { |
| 32 | struct poll_table_page * next; |
| 33 | struct poll_table_entry * entry; |
| 34 | struct poll_table_entry entries[0]; |
| 35 | }; |
| 36 | |
| 37 | #define POLL_TABLE_FULL(table) \ |
| 38 | ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table)) |
| 39 | |
| 40 | /* |
| 41 | * Ok, Peter made a complicated, but straightforward multiple_wait() function. |
| 42 | * I have rewritten this, taking some shortcuts: This code may not be easy to |
| 43 | * follow, but it should be free of race-conditions, and it's practical. If you |
| 44 | * understand what I'm doing here, then you understand how the linux |
| 45 | * sleep/wakeup mechanism works. |
| 46 | * |
| 47 | * Two very simple procedures, poll_wait() and poll_freewait() make all the |
| 48 | * work. poll_wait() is an inline-function defined in <linux/poll.h>, |
| 49 | * as all select/poll functions have to call it to add an entry to the |
| 50 | * poll table. |
| 51 | */ |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 52 | static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, |
| 53 | poll_table *p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | void poll_initwait(struct poll_wqueues *pwq) |
| 56 | { |
| 57 | init_poll_funcptr(&pwq->pt, __pollwait); |
| 58 | pwq->error = 0; |
| 59 | pwq->table = NULL; |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 60 | pwq->inline_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | EXPORT_SYMBOL(poll_initwait); |
| 64 | |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 65 | static void free_poll_entry(struct poll_table_entry *entry) |
| 66 | { |
WANG Cong | ccf6780 | 2007-05-09 07:10:02 +0200 | [diff] [blame] | 67 | remove_wait_queue(entry->wait_address, &entry->wait); |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 68 | fput(entry->filp); |
| 69 | } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | void poll_freewait(struct poll_wqueues *pwq) |
| 72 | { |
| 73 | struct poll_table_page * p = pwq->table; |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 74 | int i; |
| 75 | for (i = 0; i < pwq->inline_index; i++) |
| 76 | free_poll_entry(pwq->inline_entries + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | while (p) { |
| 78 | struct poll_table_entry * entry; |
| 79 | struct poll_table_page *old; |
| 80 | |
| 81 | entry = p->entry; |
| 82 | do { |
| 83 | entry--; |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 84 | free_poll_entry(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } while (entry > p->entries); |
| 86 | old = p; |
| 87 | p = p->next; |
| 88 | free_page((unsigned long) old); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | EXPORT_SYMBOL(poll_freewait); |
| 93 | |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 94 | static struct poll_table_entry *poll_get_entry(poll_table *_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt); |
| 97 | struct poll_table_page *table = p->table; |
| 98 | |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 99 | if (p->inline_index < N_INLINE_POLL_ENTRIES) |
| 100 | return p->inline_entries + p->inline_index++; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (!table || POLL_TABLE_FULL(table)) { |
| 103 | struct poll_table_page *new_table; |
| 104 | |
| 105 | new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL); |
| 106 | if (!new_table) { |
| 107 | p->error = -ENOMEM; |
| 108 | __set_current_state(TASK_RUNNING); |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 109 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | new_table->entry = new_table->entries; |
| 112 | new_table->next = table; |
| 113 | p->table = new_table; |
| 114 | table = new_table; |
| 115 | } |
| 116 | |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 117 | return table->entry++; |
| 118 | } |
| 119 | |
| 120 | /* Add a new entry */ |
| 121 | static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, |
| 122 | poll_table *p) |
| 123 | { |
| 124 | struct poll_table_entry *entry = poll_get_entry(p); |
| 125 | if (!entry) |
| 126 | return; |
| 127 | get_file(filp); |
| 128 | entry->filp = filp; |
| 129 | entry->wait_address = wait_address; |
| 130 | init_waitqueue_entry(&entry->wait, current); |
WANG Cong | ccf6780 | 2007-05-09 07:10:02 +0200 | [diff] [blame] | 131 | add_wait_queue(wait_address, &entry->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Thomas Gleixner | b773ad4 | 2008-08-31 08:16:57 -0700 | [diff] [blame] | 134 | /** |
| 135 | * poll_select_set_timeout - helper function to setup the timeout value |
| 136 | * @to: pointer to timespec variable for the final timeout |
| 137 | * @sec: seconds (from user space) |
| 138 | * @nsec: nanoseconds (from user space) |
| 139 | * |
| 140 | * Note, we do not use a timespec for the user space value here, That |
| 141 | * way we can use the function for timeval and compat interfaces as well. |
| 142 | * |
| 143 | * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0. |
| 144 | */ |
| 145 | int poll_select_set_timeout(struct timespec *to, long sec, long nsec) |
| 146 | { |
| 147 | struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec}; |
| 148 | |
| 149 | if (!timespec_valid(&ts)) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | /* Optimize for the zero timeout value here */ |
| 153 | if (!sec && !nsec) { |
| 154 | to->tv_sec = to->tv_nsec = 0; |
| 155 | } else { |
| 156 | ktime_get_ts(to); |
| 157 | *to = timespec_add_safe(*to, ts); |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int poll_select_copy_remaining(struct timespec *end_time, void __user *p, |
| 163 | int timeval, int ret) |
| 164 | { |
| 165 | struct timespec rts; |
| 166 | struct timeval rtv; |
| 167 | |
| 168 | if (!p) |
| 169 | return ret; |
| 170 | |
| 171 | if (current->personality & STICKY_TIMEOUTS) |
| 172 | goto sticky; |
| 173 | |
| 174 | /* No update for zero timeout */ |
| 175 | if (!end_time->tv_sec && !end_time->tv_nsec) |
| 176 | return ret; |
| 177 | |
| 178 | ktime_get_ts(&rts); |
| 179 | rts = timespec_sub(*end_time, rts); |
| 180 | if (rts.tv_sec < 0) |
| 181 | rts.tv_sec = rts.tv_nsec = 0; |
| 182 | |
| 183 | if (timeval) { |
| 184 | rtv.tv_sec = rts.tv_sec; |
| 185 | rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC; |
| 186 | |
| 187 | if (!copy_to_user(p, &rtv, sizeof(rtv))) |
| 188 | return ret; |
| 189 | |
| 190 | } else if (!copy_to_user(p, &rts, sizeof(rts))) |
| 191 | return ret; |
| 192 | |
| 193 | /* |
| 194 | * If an application puts its timeval in read-only memory, we |
| 195 | * don't want the Linux-specific update to the timeval to |
| 196 | * cause a fault after the select has completed |
| 197 | * successfully. However, because we're not updating the |
| 198 | * timeval, we can't restart the system call. |
| 199 | */ |
| 200 | |
| 201 | sticky: |
| 202 | if (ret == -ERESTARTNOHAND) |
| 203 | ret = -EINTR; |
| 204 | return ret; |
| 205 | } |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #define FDS_IN(fds, n) (fds->in + n) |
| 208 | #define FDS_OUT(fds, n) (fds->out + n) |
| 209 | #define FDS_EX(fds, n) (fds->ex + n) |
| 210 | |
| 211 | #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n)) |
| 212 | |
| 213 | static int max_select_fd(unsigned long n, fd_set_bits *fds) |
| 214 | { |
| 215 | unsigned long *open_fds; |
| 216 | unsigned long set; |
| 217 | int max; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 218 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /* handle last in-complete long-word first */ |
| 221 | set = ~(~0UL << (n & (__NFDBITS-1))); |
| 222 | n /= __NFDBITS; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 223 | fdt = files_fdtable(current->files); |
| 224 | open_fds = fdt->open_fds->fds_bits+n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | max = 0; |
| 226 | if (set) { |
| 227 | set &= BITS(fds, n); |
| 228 | if (set) { |
| 229 | if (!(set & ~*open_fds)) |
| 230 | goto get_max; |
| 231 | return -EBADF; |
| 232 | } |
| 233 | } |
| 234 | while (n) { |
| 235 | open_fds--; |
| 236 | n--; |
| 237 | set = BITS(fds, n); |
| 238 | if (!set) |
| 239 | continue; |
| 240 | if (set & ~*open_fds) |
| 241 | return -EBADF; |
| 242 | if (max) |
| 243 | continue; |
| 244 | get_max: |
| 245 | do { |
| 246 | max++; |
| 247 | set >>= 1; |
| 248 | } while (set); |
| 249 | max += n * __NFDBITS; |
| 250 | } |
| 251 | |
| 252 | return max; |
| 253 | } |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR) |
| 256 | #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR) |
| 257 | #define POLLEX_SET (POLLPRI) |
| 258 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 259 | int do_select(int n, fd_set_bits *fds, struct timespec *end_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 261 | ktime_t expire, *to = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | struct poll_wqueues table; |
| 263 | poll_table *wait; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 264 | int retval, i, timed_out = 0; |
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; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 276 | if (end_time && !end_time->tv_sec && !end_time->tv_nsec) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | wait = NULL; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 278 | timed_out = 1; |
| 279 | } |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | retval = 0; |
| 282 | for (;;) { |
| 283 | unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp; |
| 284 | |
| 285 | set_current_state(TASK_INTERRUPTIBLE); |
| 286 | |
| 287 | inp = fds->in; outp = fds->out; exp = fds->ex; |
| 288 | rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex; |
| 289 | |
| 290 | for (i = 0; i < n; ++rinp, ++routp, ++rexp) { |
| 291 | unsigned long in, out, ex, all_bits, bit = 1, mask, j; |
| 292 | 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] | 293 | const struct file_operations *f_op = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | struct file *file = NULL; |
| 295 | |
| 296 | in = *inp++; out = *outp++; ex = *exp++; |
| 297 | all_bits = in | out | ex; |
| 298 | if (all_bits == 0) { |
| 299 | i += __NFDBITS; |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) { |
Eric Dumazet | e4a1f12 | 2006-03-28 01:56:34 -0800 | [diff] [blame] | 304 | int fput_needed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (i >= n) |
| 306 | break; |
| 307 | if (!(bit & all_bits)) |
| 308 | continue; |
Eric Dumazet | e4a1f12 | 2006-03-28 01:56:34 -0800 | [diff] [blame] | 309 | file = fget_light(i, &fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (file) { |
| 311 | f_op = file->f_op; |
| 312 | mask = DEFAULT_POLLMASK; |
| 313 | if (f_op && f_op->poll) |
| 314 | mask = (*f_op->poll)(file, retval ? NULL : wait); |
Eric Dumazet | e4a1f12 | 2006-03-28 01:56:34 -0800 | [diff] [blame] | 315 | fput_light(file, fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if ((mask & POLLIN_SET) && (in & bit)) { |
| 317 | res_in |= bit; |
| 318 | retval++; |
| 319 | } |
| 320 | if ((mask & POLLOUT_SET) && (out & bit)) { |
| 321 | res_out |= bit; |
| 322 | retval++; |
| 323 | } |
| 324 | if ((mask & POLLEX_SET) && (ex & bit)) { |
| 325 | res_ex |= bit; |
| 326 | retval++; |
| 327 | } |
| 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | if (res_in) |
| 331 | *rinp = res_in; |
| 332 | if (res_out) |
| 333 | *routp = res_out; |
| 334 | if (res_ex) |
| 335 | *rexp = res_ex; |
Linus Torvalds | 55d8538 | 2008-06-22 12:23:15 -0700 | [diff] [blame] | 336 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | wait = NULL; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 339 | if (retval || timed_out || signal_pending(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | break; |
Pavel Machek | f526448 | 2008-04-21 22:15:06 +0000 | [diff] [blame] | 341 | if (table.error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | retval = table.error; |
| 343 | break; |
| 344 | } |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 345 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 346 | /* |
| 347 | * If this is the first loop and we have a timeout |
| 348 | * given, then we convert to ktime_t and set the to |
| 349 | * pointer to the expiry value. |
| 350 | */ |
| 351 | if (end_time && !to) { |
| 352 | expire = timespec_to_ktime(*end_time); |
| 353 | to = &expire; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 354 | } |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 355 | |
| 356 | if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS)) |
| 357 | timed_out = 1; |
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, |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 378 | fd_set __user *exp, struct timespec *end_time) |
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 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 429 | ret = do_select(n, &fds, end_time); |
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 | { |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 455 | struct timespec end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 456 | struct timeval tv; |
| 457 | int ret; |
| 458 | |
| 459 | if (tvp) { |
| 460 | if (copy_from_user(&tv, tvp, sizeof(tv))) |
| 461 | return -EFAULT; |
| 462 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 463 | to = &end_time; |
| 464 | if (poll_select_set_timeout(to, tv.tv_sec, |
| 465 | tv.tv_usec * NSEC_PER_USEC)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 466 | return -EINVAL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 469 | ret = core_sys_select(n, inp, outp, exp, to); |
| 470 | ret = poll_select_copy_remaining(&end_time, tvp, 1, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 475 | #ifdef HAVE_SET_RESTORE_SIGMASK |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 476 | asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp, |
| 477 | fd_set __user *exp, struct timespec __user *tsp, |
| 478 | const sigset_t __user *sigmask, size_t sigsetsize) |
| 479 | { |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 480 | sigset_t ksigmask, sigsaved; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 481 | struct timespec ts, end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 482 | int ret; |
| 483 | |
| 484 | if (tsp) { |
| 485 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 486 | return -EFAULT; |
| 487 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 488 | to = &end_time; |
| 489 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 490 | return -EINVAL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (sigmask) { |
| 494 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 495 | if (sigsetsize != sizeof(sigset_t)) |
| 496 | return -EINVAL; |
| 497 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) |
| 498 | return -EFAULT; |
| 499 | |
| 500 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 501 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 502 | } |
| 503 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 504 | ret = core_sys_select(n, inp, outp, exp, &end_time); |
| 505 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 506 | |
| 507 | if (ret == -ERESTARTNOHAND) { |
| 508 | /* |
| 509 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 510 | * the signal on the way back to userspace, before the signal |
| 511 | * mask is restored. |
| 512 | */ |
| 513 | if (sigmask) { |
| 514 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 515 | sizeof(sigsaved)); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 516 | set_restore_sigmask(); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 517 | } |
| 518 | } else if (sigmask) |
| 519 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Most architectures can't handle 7-argument syscalls. So we provide a |
| 526 | * 6-argument version where the sixth argument is a pointer to a structure |
| 527 | * which has a pointer to the sigset_t itself followed by a size_t containing |
| 528 | * the sigset size. |
| 529 | */ |
| 530 | asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp, |
| 531 | fd_set __user *exp, struct timespec __user *tsp, void __user *sig) |
| 532 | { |
| 533 | size_t sigsetsize = 0; |
| 534 | sigset_t __user *up = NULL; |
| 535 | |
| 536 | if (sig) { |
| 537 | if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t)) |
Al Viro | e110ab9 | 2006-02-01 05:26:09 -0500 | [diff] [blame] | 538 | || __get_user(up, (sigset_t __user * __user *)sig) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 539 | || __get_user(sigsetsize, |
Al Viro | e110ab9 | 2006-02-01 05:26:09 -0500 | [diff] [blame] | 540 | (size_t __user *)(sig+sizeof(void *)))) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 541 | return -EFAULT; |
| 542 | } |
| 543 | |
| 544 | return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize); |
| 545 | } |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 546 | #endif /* HAVE_SET_RESTORE_SIGMASK */ |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | struct poll_list { |
| 549 | struct poll_list *next; |
| 550 | int len; |
| 551 | struct pollfd entries[0]; |
| 552 | }; |
| 553 | |
| 554 | #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd)) |
| 555 | |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 556 | /* |
| 557 | * Fish for pollable events on the pollfd->fd file descriptor. We're only |
| 558 | * interested in events matching the pollfd->events mask, and the result |
| 559 | * matching that mask is both recorded in pollfd->revents and returned. The |
| 560 | * pwait poll_table will be used by the fd-provided poll handler for waiting, |
| 561 | * if non-NULL. |
| 562 | */ |
| 563 | static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 565 | unsigned int mask; |
| 566 | int fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 568 | mask = 0; |
| 569 | fd = pollfd->fd; |
| 570 | if (fd >= 0) { |
| 571 | int fput_needed; |
| 572 | struct file * file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 574 | file = fget_light(fd, &fput_needed); |
| 575 | mask = POLLNVAL; |
| 576 | if (file != NULL) { |
| 577 | mask = DEFAULT_POLLMASK; |
| 578 | if (file->f_op && file->f_op->poll) |
| 579 | mask = file->f_op->poll(file, pwait); |
| 580 | /* Mask out unneeded events. */ |
| 581 | mask &= pollfd->events | POLLERR | POLLHUP; |
| 582 | fput_light(file, fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 585 | pollfd->revents = mask; |
| 586 | |
| 587 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static int do_poll(unsigned int nfds, struct poll_list *list, |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 591 | struct poll_wqueues *wait, struct timespec *end_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | poll_table* pt = &wait->pt; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 594 | ktime_t expire, *to = NULL; |
| 595 | int timed_out = 0, count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 597 | /* Optimise the no-wait case */ |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 598 | if (end_time && !end_time->tv_sec && !end_time->tv_nsec) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | pt = NULL; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 600 | timed_out = 1; |
| 601 | } |
Oleg Nesterov | 9bf084f | 2007-10-16 23:26:18 -0700 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | for (;;) { |
| 604 | struct poll_list *walk; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | set_current_state(TASK_INTERRUPTIBLE); |
Vadim Lobanov | 4a4b69f | 2006-06-23 02:05:16 -0700 | [diff] [blame] | 607 | for (walk = list; walk != NULL; walk = walk->next) { |
| 608 | struct pollfd * pfd, * pfd_end; |
| 609 | |
| 610 | pfd = walk->entries; |
| 611 | pfd_end = pfd + walk->len; |
| 612 | for (; pfd != pfd_end; pfd++) { |
| 613 | /* |
| 614 | * Fish for events. If we found one, record it |
| 615 | * and kill the poll_table, so we don't |
| 616 | * needlessly register any other waiters after |
| 617 | * this. They'll get immediately deregistered |
| 618 | * when we break out and return. |
| 619 | */ |
| 620 | if (do_pollfd(pfd, pt)) { |
| 621 | count++; |
| 622 | pt = NULL; |
| 623 | } |
| 624 | } |
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 | /* |
| 627 | * All waiters have already been registered, so don't provide |
| 628 | * a poll_table to them on the next loop iteration. |
| 629 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | pt = NULL; |
Oleg Nesterov | 9bf084f | 2007-10-16 23:26:18 -0700 | [diff] [blame] | 631 | if (!count) { |
| 632 | count = wait->error; |
| 633 | if (signal_pending(current)) |
| 634 | count = -EINTR; |
| 635 | } |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 636 | if (count || timed_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | break; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 638 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 639 | /* |
| 640 | * If this is the first loop and we have a timeout |
| 641 | * given, then we convert to ktime_t and set the to |
| 642 | * pointer to the expiry value. |
| 643 | */ |
| 644 | if (end_time && !to) { |
| 645 | expire = timespec_to_ktime(*end_time); |
| 646 | to = &expire; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 649 | if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS)) |
| 650 | timed_out = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | __set_current_state(TASK_RUNNING); |
| 653 | return count; |
| 654 | } |
| 655 | |
Andi Kleen | 70674f9 | 2006-03-28 01:56:33 -0800 | [diff] [blame] | 656 | #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \ |
| 657 | sizeof(struct pollfd)) |
| 658 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 659 | int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, |
| 660 | struct timespec *end_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
| 662 | struct poll_wqueues table; |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 663 | int err = -EFAULT, fdcount, len, size; |
Jes Sorensen | 30c14e4 | 2006-03-31 11:18:57 -0500 | [diff] [blame] | 664 | /* Allocate small arguments on the stack to save memory and be |
| 665 | faster - use long to make sure the buffer is aligned properly |
| 666 | on 64 bit archs to avoid unaligned access */ |
| 667 | long stack_pps[POLL_STACK_ALLOC/sizeof(long)]; |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 668 | struct poll_list *const head = (struct poll_list *)stack_pps; |
| 669 | struct poll_list *walk = head; |
| 670 | unsigned long todo = nfds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Chris Snook | 4e6fd33 | 2006-09-29 02:01:33 -0700 | [diff] [blame] | 672 | if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 675 | len = min_t(unsigned int, nfds, N_STACK_PPS); |
| 676 | for (;;) { |
| 677 | walk->next = NULL; |
| 678 | walk->len = len; |
| 679 | if (!len) |
| 680 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 682 | if (copy_from_user(walk->entries, ufds + nfds-todo, |
| 683 | sizeof(struct pollfd) * walk->len)) |
| 684 | goto out_fds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 686 | todo -= walk->len; |
| 687 | if (!todo) |
| 688 | break; |
| 689 | |
| 690 | len = min(todo, POLLFD_PER_PAGE); |
| 691 | size = sizeof(struct poll_list) + sizeof(struct pollfd) * len; |
| 692 | walk = walk->next = kmalloc(size, GFP_KERNEL); |
| 693 | if (!walk) { |
| 694 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | goto out_fds; |
| 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 698 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 699 | poll_initwait(&table); |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 700 | fdcount = do_poll(nfds, head, &table, end_time); |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 701 | poll_freewait(&table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 703 | for (walk = head; walk; walk = walk->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | struct pollfd *fds = walk->entries; |
| 705 | int j; |
| 706 | |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 707 | for (j = 0; j < walk->len; j++, ufds++) |
| 708 | if (__put_user(fds[j].revents, &ufds->revents)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | goto out_fds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | err = fdcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | out_fds: |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 714 | walk = head->next; |
| 715 | while (walk) { |
| 716 | struct poll_list *pos = walk; |
| 717 | walk = walk->next; |
| 718 | kfree(pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
Oleg Nesterov | 252e572 | 2007-10-16 23:26:17 -0700 | [diff] [blame] | 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | return err; |
| 722 | } |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 723 | |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 724 | static long do_restart_poll(struct restart_block *restart_block) |
| 725 | { |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 726 | struct pollfd __user *ufds = restart_block->poll.ufds; |
| 727 | int nfds = restart_block->poll.nfds; |
| 728 | struct timespec *to = NULL, end_time; |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 729 | int ret; |
| 730 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 731 | if (restart_block->poll.has_timeout) { |
| 732 | end_time.tv_sec = restart_block->poll.tv_sec; |
| 733 | end_time.tv_nsec = restart_block->poll.tv_nsec; |
| 734 | to = &end_time; |
| 735 | } |
| 736 | |
| 737 | ret = do_sys_poll(ufds, nfds, to); |
| 738 | |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 739 | if (ret == -EINTR) { |
| 740 | restart_block->fn = do_restart_poll; |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 741 | ret = -ERESTART_RESTARTBLOCK; |
| 742 | } |
| 743 | return ret; |
| 744 | } |
| 745 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 746 | asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds, |
| 747 | long timeout_msecs) |
| 748 | { |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 749 | struct timespec end_time, *to = NULL; |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 750 | int ret; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 751 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 752 | if (timeout_msecs >= 0) { |
| 753 | to = &end_time; |
| 754 | poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC, |
| 755 | NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC)); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 758 | ret = do_sys_poll(ufds, nfds, to); |
| 759 | |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 760 | if (ret == -EINTR) { |
| 761 | struct restart_block *restart_block; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 762 | |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 763 | restart_block = ¤t_thread_info()->restart_block; |
| 764 | restart_block->fn = do_restart_poll; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 765 | restart_block->poll.ufds = ufds; |
| 766 | restart_block->poll.nfds = nfds; |
| 767 | |
| 768 | if (timeout_msecs >= 0) { |
| 769 | restart_block->poll.tv_sec = end_time.tv_sec; |
| 770 | restart_block->poll.tv_nsec = end_time.tv_nsec; |
| 771 | restart_block->poll.has_timeout = 1; |
| 772 | } else |
| 773 | restart_block->poll.has_timeout = 0; |
| 774 | |
Chris Wright | 3075d9d | 2007-10-16 23:27:18 -0700 | [diff] [blame] | 775 | ret = -ERESTART_RESTARTBLOCK; |
| 776 | } |
| 777 | return ret; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 780 | #ifdef HAVE_SET_RESTORE_SIGMASK |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 781 | asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds, |
| 782 | struct timespec __user *tsp, const sigset_t __user *sigmask, |
| 783 | size_t sigsetsize) |
| 784 | { |
| 785 | sigset_t ksigmask, sigsaved; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 786 | struct timespec ts, end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 787 | int ret; |
| 788 | |
| 789 | if (tsp) { |
| 790 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 791 | return -EFAULT; |
| 792 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 793 | to = &end_time; |
| 794 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) |
| 795 | return -EINVAL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (sigmask) { |
| 799 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 800 | if (sigsetsize != sizeof(sigset_t)) |
| 801 | return -EINVAL; |
| 802 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) |
| 803 | return -EFAULT; |
| 804 | |
| 805 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 806 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 807 | } |
| 808 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 809 | ret = do_sys_poll(ufds, nfds, to); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 810 | |
| 811 | /* We can restart this syscall, usually */ |
| 812 | if (ret == -EINTR) { |
| 813 | /* |
| 814 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 815 | * the signal on the way back to userspace, before the signal |
| 816 | * mask is restored. |
| 817 | */ |
| 818 | if (sigmask) { |
| 819 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 820 | sizeof(sigsaved)); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 821 | set_restore_sigmask(); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 822 | } |
| 823 | ret = -ERESTARTNOHAND; |
| 824 | } else if (sigmask) |
| 825 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 826 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame^] | 827 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 828 | |
| 829 | return ret; |
| 830 | } |
Roland McGrath | f3de272 | 2008-04-30 00:53:09 -0700 | [diff] [blame] | 831 | #endif /* HAVE_SET_RESTORE_SIGMASK */ |