select.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 <linux/hrtimer.h>
  27. #include <asm/uaccess.h>
  28. struct poll_table_page {
  29. struct poll_table_page * next;
  30. struct poll_table_entry * entry;
  31. struct poll_table_entry entries[0];
  32. };
  33. #define POLL_TABLE_FULL(table) \
  34. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  35. /*
  36. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  37. * I have rewritten this, taking some shortcuts: This code may not be easy to
  38. * follow, but it should be free of race-conditions, and it's practical. If you
  39. * understand what I'm doing here, then you understand how the linux
  40. * sleep/wakeup mechanism works.
  41. *
  42. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  43. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  44. * as all select/poll functions have to call it to add an entry to the
  45. * poll table.
  46. */
  47. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  48. poll_table *p);
  49. void poll_initwait(struct poll_wqueues *pwq)
  50. {
  51. init_poll_funcptr(&pwq->pt, __pollwait);
  52. pwq->error = 0;
  53. pwq->table = NULL;
  54. pwq->inline_index = 0;
  55. }
  56. EXPORT_SYMBOL(poll_initwait);
  57. static void free_poll_entry(struct poll_table_entry *entry)
  58. {
  59. remove_wait_queue(entry->wait_address, &entry->wait);
  60. fput(entry->filp);
  61. }
  62. void poll_freewait(struct poll_wqueues *pwq)
  63. {
  64. struct poll_table_page * p = pwq->table;
  65. int i;
  66. for (i = 0; i < pwq->inline_index; i++)
  67. free_poll_entry(pwq->inline_entries + i);
  68. while (p) {
  69. struct poll_table_entry * entry;
  70. struct poll_table_page *old;
  71. entry = p->entry;
  72. do {
  73. entry--;
  74. free_poll_entry(entry);
  75. } while (entry > p->entries);
  76. old = p;
  77. p = p->next;
  78. free_page((unsigned long) old);
  79. }
  80. }
  81. EXPORT_SYMBOL(poll_freewait);
  82. static struct poll_table_entry *poll_get_entry(poll_table *_p)
  83. {
  84. struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
  85. struct poll_table_page *table = p->table;
  86. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  87. return p->inline_entries + p->inline_index++;
  88. if (!table || POLL_TABLE_FULL(table)) {
  89. struct poll_table_page *new_table;
  90. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  91. if (!new_table) {
  92. p->error = -ENOMEM;
  93. __set_current_state(TASK_RUNNING);
  94. return NULL;
  95. }
  96. new_table->entry = new_table->entries;
  97. new_table->next = table;
  98. p->table = new_table;
  99. table = new_table;
  100. }
  101. return table->entry++;
  102. }
  103. /* Add a new entry */
  104. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  105. poll_table *p)
  106. {
  107. struct poll_table_entry *entry = poll_get_entry(p);
  108. if (!entry)
  109. return;
  110. get_file(filp);
  111. entry->filp = filp;
  112. entry->wait_address = wait_address;
  113. init_waitqueue_entry(&entry->wait, current);
  114. add_wait_queue(wait_address, &entry->wait);
  115. }
  116. /**
  117. * poll_select_set_timeout - helper function to setup the timeout value
  118. * @to: pointer to timespec variable for the final timeout
  119. * @sec: seconds (from user space)
  120. * @nsec: nanoseconds (from user space)
  121. *
  122. * Note, we do not use a timespec for the user space value here, That
  123. * way we can use the function for timeval and compat interfaces as well.
  124. *
  125. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  126. */
  127. int poll_select_set_timeout(struct timespec *to, long sec, long nsec)
  128. {
  129. struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec};
  130. if (!timespec_valid(&ts))
  131. return -EINVAL;
  132. /* Optimize for the zero timeout value here */
  133. if (!sec && !nsec) {
  134. to->tv_sec = to->tv_nsec = 0;
  135. } else {
  136. ktime_get_ts(to);
  137. *to = timespec_add_safe(*to, ts);
  138. }
  139. return 0;
  140. }
  141. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  142. int timeval, int ret)
  143. {
  144. struct timespec rts;
  145. struct timeval rtv;
  146. if (!p)
  147. return ret;
  148. if (current->personality & STICKY_TIMEOUTS)
  149. goto sticky;
  150. /* No update for zero timeout */
  151. if (!end_time->tv_sec && !end_time->tv_nsec)
  152. return ret;
  153. ktime_get_ts(&rts);
  154. rts = timespec_sub(*end_time, rts);
  155. if (rts.tv_sec < 0)
  156. rts.tv_sec = rts.tv_nsec = 0;
  157. if (timeval) {
  158. rtv.tv_sec = rts.tv_sec;
  159. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  160. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  161. return ret;
  162. } else if (!copy_to_user(p, &rts, sizeof(rts)))
  163. return ret;
  164. /*
  165. * If an application puts its timeval in read-only memory, we
  166. * don't want the Linux-specific update to the timeval to
  167. * cause a fault after the select has completed
  168. * successfully. However, because we're not updating the
  169. * timeval, we can't restart the system call.
  170. */
  171. sticky:
  172. if (ret == -ERESTARTNOHAND)
  173. ret = -EINTR;
  174. return ret;
  175. }
  176. #define FDS_IN(fds, n) (fds->in + n)
  177. #define FDS_OUT(fds, n) (fds->out + n)
  178. #define FDS_EX(fds, n) (fds->ex + n)
  179. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  180. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  181. {
  182. unsigned long *open_fds;
  183. unsigned long set;
  184. int max;
  185. struct fdtable *fdt;
  186. /* handle last in-complete long-word first */
  187. set = ~(~0UL << (n & (__NFDBITS-1)));
  188. n /= __NFDBITS;
  189. fdt = files_fdtable(current->files);
  190. open_fds = fdt->open_fds->fds_bits+n;
  191. max = 0;
  192. if (set) {
  193. set &= BITS(fds, n);
  194. if (set) {
  195. if (!(set & ~*open_fds))
  196. goto get_max;
  197. return -EBADF;
  198. }
  199. }
  200. while (n) {
  201. open_fds--;
  202. n--;
  203. set = BITS(fds, n);
  204. if (!set)
  205. continue;
  206. if (set & ~*open_fds)
  207. return -EBADF;
  208. if (max)
  209. continue;
  210. get_max:
  211. do {
  212. max++;
  213. set >>= 1;
  214. } while (set);
  215. max += n * __NFDBITS;
  216. }
  217. return max;
  218. }
  219. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  220. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  221. #define POLLEX_SET (POLLPRI)
  222. int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  223. {
  224. ktime_t expire, *to = NULL;
  225. struct poll_wqueues table;
  226. poll_table *wait;
  227. int retval, i, timed_out = 0;
  228. rcu_read_lock();
  229. retval = max_select_fd(n, fds);
  230. rcu_read_unlock();
  231. if (retval < 0)
  232. return retval;
  233. n = retval;
  234. poll_initwait(&table);
  235. wait = &table.pt;
  236. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  237. wait = NULL;
  238. timed_out = 1;
  239. }
  240. retval = 0;
  241. for (;;) {
  242. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  243. set_current_state(TASK_INTERRUPTIBLE);
  244. inp = fds->in; outp = fds->out; exp = fds->ex;
  245. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  246. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  247. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  248. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  249. const struct file_operations *f_op = NULL;
  250. struct file *file = NULL;
  251. in = *inp++; out = *outp++; ex = *exp++;
  252. all_bits = in | out | ex;
  253. if (all_bits == 0) {
  254. i += __NFDBITS;
  255. continue;
  256. }
  257. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  258. int fput_needed;
  259. if (i >= n)
  260. break;
  261. if (!(bit & all_bits))
  262. continue;
  263. file = fget_light(i, &fput_needed);
  264. if (file) {
  265. f_op = file->f_op;
  266. mask = DEFAULT_POLLMASK;
  267. if (f_op && f_op->poll)
  268. mask = (*f_op->poll)(file, retval ? NULL : wait);
  269. fput_light(file, fput_needed);
  270. if ((mask & POLLIN_SET) && (in & bit)) {
  271. res_in |= bit;
  272. retval++;
  273. }
  274. if ((mask & POLLOUT_SET) && (out & bit)) {
  275. res_out |= bit;
  276. retval++;
  277. }
  278. if ((mask & POLLEX_SET) && (ex & bit)) {
  279. res_ex |= bit;
  280. retval++;
  281. }
  282. }
  283. }
  284. if (res_in)
  285. *rinp = res_in;
  286. if (res_out)
  287. *routp = res_out;
  288. if (res_ex)
  289. *rexp = res_ex;
  290. cond_resched();
  291. }
  292. wait = NULL;
  293. if (retval || timed_out || signal_pending(current))
  294. break;
  295. if (table.error) {
  296. retval = table.error;
  297. break;
  298. }
  299. /*
  300. * If this is the first loop and we have a timeout
  301. * given, then we convert to ktime_t and set the to
  302. * pointer to the expiry value.
  303. */
  304. if (end_time && !to) {
  305. expire = timespec_to_ktime(*end_time);
  306. to = &expire;
  307. }
  308. if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS))
  309. timed_out = 1;
  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, struct timespec *end_time)
  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, end_time);
  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. struct timespec end_time, *to = NULL;
  395. struct timeval tv;
  396. int ret;
  397. if (tvp) {
  398. if (copy_from_user(&tv, tvp, sizeof(tv)))
  399. return -EFAULT;
  400. to = &end_time;
  401. if (poll_select_set_timeout(to, tv.tv_sec,
  402. tv.tv_usec * NSEC_PER_USEC))
  403. return -EINVAL;
  404. }
  405. ret = core_sys_select(n, inp, outp, exp, to);
  406. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  407. return ret;
  408. }
  409. #ifdef HAVE_SET_RESTORE_SIGMASK
  410. asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
  411. fd_set __user *exp, struct timespec __user *tsp,
  412. const sigset_t __user *sigmask, size_t sigsetsize)
  413. {
  414. sigset_t ksigmask, sigsaved;
  415. struct timespec ts, end_time, *to = NULL;
  416. int ret;
  417. if (tsp) {
  418. if (copy_from_user(&ts, tsp, sizeof(ts)))
  419. return -EFAULT;
  420. to = &end_time;
  421. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  422. return -EINVAL;
  423. }
  424. if (sigmask) {
  425. /* XXX: Don't preclude handling different sized sigset_t's. */
  426. if (sigsetsize != sizeof(sigset_t))
  427. return -EINVAL;
  428. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  429. return -EFAULT;
  430. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  431. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  432. }
  433. ret = core_sys_select(n, inp, outp, exp, &end_time);
  434. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  435. if (ret == -ERESTARTNOHAND) {
  436. /*
  437. * Don't restore the signal mask yet. Let do_signal() deliver
  438. * the signal on the way back to userspace, before the signal
  439. * mask is restored.
  440. */
  441. if (sigmask) {
  442. memcpy(&current->saved_sigmask, &sigsaved,
  443. sizeof(sigsaved));
  444. set_restore_sigmask();
  445. }
  446. } else if (sigmask)
  447. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  448. return ret;
  449. }
  450. /*
  451. * Most architectures can't handle 7-argument syscalls. So we provide a
  452. * 6-argument version where the sixth argument is a pointer to a structure
  453. * which has a pointer to the sigset_t itself followed by a size_t containing
  454. * the sigset size.
  455. */
  456. asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
  457. fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
  458. {
  459. size_t sigsetsize = 0;
  460. sigset_t __user *up = NULL;
  461. if (sig) {
  462. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  463. || __get_user(up, (sigset_t __user * __user *)sig)
  464. || __get_user(sigsetsize,
  465. (size_t __user *)(sig+sizeof(void *))))
  466. return -EFAULT;
  467. }
  468. return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
  469. }
  470. #endif /* HAVE_SET_RESTORE_SIGMASK */
  471. struct poll_list {
  472. struct poll_list *next;
  473. int len;
  474. struct pollfd entries[0];
  475. };
  476. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  477. /*
  478. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  479. * interested in events matching the pollfd->events mask, and the result
  480. * matching that mask is both recorded in pollfd->revents and returned. The
  481. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  482. * if non-NULL.
  483. */
  484. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  485. {
  486. unsigned int mask;
  487. int fd;
  488. mask = 0;
  489. fd = pollfd->fd;
  490. if (fd >= 0) {
  491. int fput_needed;
  492. struct file * file;
  493. file = fget_light(fd, &fput_needed);
  494. mask = POLLNVAL;
  495. if (file != NULL) {
  496. mask = DEFAULT_POLLMASK;
  497. if (file->f_op && file->f_op->poll)
  498. mask = file->f_op->poll(file, pwait);
  499. /* Mask out unneeded events. */
  500. mask &= pollfd->events | POLLERR | POLLHUP;
  501. fput_light(file, fput_needed);
  502. }
  503. }
  504. pollfd->revents = mask;
  505. return mask;
  506. }
  507. static int do_poll(unsigned int nfds, struct poll_list *list,
  508. struct poll_wqueues *wait, struct timespec *end_time)
  509. {
  510. poll_table* pt = &wait->pt;
  511. ktime_t expire, *to = NULL;
  512. int timed_out = 0, count = 0;
  513. /* Optimise the no-wait case */
  514. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  515. pt = NULL;
  516. timed_out = 1;
  517. }
  518. for (;;) {
  519. struct poll_list *walk;
  520. set_current_state(TASK_INTERRUPTIBLE);
  521. for (walk = list; walk != NULL; walk = walk->next) {
  522. struct pollfd * pfd, * pfd_end;
  523. pfd = walk->entries;
  524. pfd_end = pfd + walk->len;
  525. for (; pfd != pfd_end; pfd++) {
  526. /*
  527. * Fish for events. If we found one, record it
  528. * and kill the poll_table, so we don't
  529. * needlessly register any other waiters after
  530. * this. They'll get immediately deregistered
  531. * when we break out and return.
  532. */
  533. if (do_pollfd(pfd, pt)) {
  534. count++;
  535. pt = NULL;
  536. }
  537. }
  538. }
  539. /*
  540. * All waiters have already been registered, so don't provide
  541. * a poll_table to them on the next loop iteration.
  542. */
  543. pt = NULL;
  544. if (!count) {
  545. count = wait->error;
  546. if (signal_pending(current))
  547. count = -EINTR;
  548. }
  549. if (count || timed_out)
  550. break;
  551. /*
  552. * If this is the first loop and we have a timeout
  553. * given, then we convert to ktime_t and set the to
  554. * pointer to the expiry value.
  555. */
  556. if (end_time && !to) {
  557. expire = timespec_to_ktime(*end_time);
  558. to = &expire;
  559. }
  560. if (!schedule_hrtimeout(to, HRTIMER_MODE_ABS))
  561. timed_out = 1;
  562. }
  563. __set_current_state(TASK_RUNNING);
  564. return count;
  565. }
  566. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  567. sizeof(struct pollfd))
  568. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  569. struct timespec *end_time)
  570. {
  571. struct poll_wqueues table;
  572. int err = -EFAULT, fdcount, len, size;
  573. /* Allocate small arguments on the stack to save memory and be
  574. faster - use long to make sure the buffer is aligned properly
  575. on 64 bit archs to avoid unaligned access */
  576. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  577. struct poll_list *const head = (struct poll_list *)stack_pps;
  578. struct poll_list *walk = head;
  579. unsigned long todo = nfds;
  580. if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  581. return -EINVAL;
  582. len = min_t(unsigned int, nfds, N_STACK_PPS);
  583. for (;;) {
  584. walk->next = NULL;
  585. walk->len = len;
  586. if (!len)
  587. break;
  588. if (copy_from_user(walk->entries, ufds + nfds-todo,
  589. sizeof(struct pollfd) * walk->len))
  590. goto out_fds;
  591. todo -= walk->len;
  592. if (!todo)
  593. break;
  594. len = min(todo, POLLFD_PER_PAGE);
  595. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  596. walk = walk->next = kmalloc(size, GFP_KERNEL);
  597. if (!walk) {
  598. err = -ENOMEM;
  599. goto out_fds;
  600. }
  601. }
  602. poll_initwait(&table);
  603. fdcount = do_poll(nfds, head, &table, end_time);
  604. poll_freewait(&table);
  605. for (walk = head; walk; walk = walk->next) {
  606. struct pollfd *fds = walk->entries;
  607. int j;
  608. for (j = 0; j < walk->len; j++, ufds++)
  609. if (__put_user(fds[j].revents, &ufds->revents))
  610. goto out_fds;
  611. }
  612. err = fdcount;
  613. out_fds:
  614. walk = head->next;
  615. while (walk) {
  616. struct poll_list *pos = walk;
  617. walk = walk->next;
  618. kfree(pos);
  619. }
  620. return err;
  621. }
  622. static long do_restart_poll(struct restart_block *restart_block)
  623. {
  624. struct pollfd __user *ufds = restart_block->poll.ufds;
  625. int nfds = restart_block->poll.nfds;
  626. struct timespec *to = NULL, end_time;
  627. int ret;
  628. if (restart_block->poll.has_timeout) {
  629. end_time.tv_sec = restart_block->poll.tv_sec;
  630. end_time.tv_nsec = restart_block->poll.tv_nsec;
  631. to = &end_time;
  632. }
  633. ret = do_sys_poll(ufds, nfds, to);
  634. if (ret == -EINTR) {
  635. restart_block->fn = do_restart_poll;
  636. ret = -ERESTART_RESTARTBLOCK;
  637. }
  638. return ret;
  639. }
  640. asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  641. long timeout_msecs)
  642. {
  643. struct timespec end_time, *to = NULL;
  644. int ret;
  645. if (timeout_msecs >= 0) {
  646. to = &end_time;
  647. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  648. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  649. }
  650. ret = do_sys_poll(ufds, nfds, to);
  651. if (ret == -EINTR) {
  652. struct restart_block *restart_block;
  653. restart_block = &current_thread_info()->restart_block;
  654. restart_block->fn = do_restart_poll;
  655. restart_block->poll.ufds = ufds;
  656. restart_block->poll.nfds = nfds;
  657. if (timeout_msecs >= 0) {
  658. restart_block->poll.tv_sec = end_time.tv_sec;
  659. restart_block->poll.tv_nsec = end_time.tv_nsec;
  660. restart_block->poll.has_timeout = 1;
  661. } else
  662. restart_block->poll.has_timeout = 0;
  663. ret = -ERESTART_RESTARTBLOCK;
  664. }
  665. return ret;
  666. }
  667. #ifdef HAVE_SET_RESTORE_SIGMASK
  668. asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
  669. struct timespec __user *tsp, const sigset_t __user *sigmask,
  670. size_t sigsetsize)
  671. {
  672. sigset_t ksigmask, sigsaved;
  673. struct timespec ts, end_time, *to = NULL;
  674. int ret;
  675. if (tsp) {
  676. if (copy_from_user(&ts, tsp, sizeof(ts)))
  677. return -EFAULT;
  678. to = &end_time;
  679. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  680. return -EINVAL;
  681. }
  682. if (sigmask) {
  683. /* XXX: Don't preclude handling different sized sigset_t's. */
  684. if (sigsetsize != sizeof(sigset_t))
  685. return -EINVAL;
  686. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  687. return -EFAULT;
  688. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  689. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  690. }
  691. ret = do_sys_poll(ufds, nfds, to);
  692. /* We can restart this syscall, usually */
  693. if (ret == -EINTR) {
  694. /*
  695. * Don't restore the signal mask yet. Let do_signal() deliver
  696. * the signal on the way back to userspace, before the signal
  697. * mask is restored.
  698. */
  699. if (sigmask) {
  700. memcpy(&current->saved_sigmask, &sigsaved,
  701. sizeof(sigsaved));
  702. set_restore_sigmask();
  703. }
  704. ret = -ERESTARTNOHAND;
  705. } else if (sigmask)
  706. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  707. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  708. return ret;
  709. }
  710. #endif /* HAVE_SET_RESTORE_SIGMASK */