select.c 23 KB

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