select.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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/fs.h>
  24. #include <linux/rcupdate.h>
  25. #include <asm/uaccess.h>
  26. #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
  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. #define FDS_IN(fds, n) (fds->in + n)
  116. #define FDS_OUT(fds, n) (fds->out + n)
  117. #define FDS_EX(fds, n) (fds->ex + n)
  118. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  119. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  120. {
  121. unsigned long *open_fds;
  122. unsigned long set;
  123. int max;
  124. struct fdtable *fdt;
  125. /* handle last in-complete long-word first */
  126. set = ~(~0UL << (n & (__NFDBITS-1)));
  127. n /= __NFDBITS;
  128. fdt = files_fdtable(current->files);
  129. open_fds = fdt->open_fds->fds_bits+n;
  130. max = 0;
  131. if (set) {
  132. set &= BITS(fds, n);
  133. if (set) {
  134. if (!(set & ~*open_fds))
  135. goto get_max;
  136. return -EBADF;
  137. }
  138. }
  139. while (n) {
  140. open_fds--;
  141. n--;
  142. set = BITS(fds, n);
  143. if (!set)
  144. continue;
  145. if (set & ~*open_fds)
  146. return -EBADF;
  147. if (max)
  148. continue;
  149. get_max:
  150. do {
  151. max++;
  152. set >>= 1;
  153. } while (set);
  154. max += n * __NFDBITS;
  155. }
  156. return max;
  157. }
  158. #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
  159. #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
  160. #define ISSET(i,m) (((i)&*(m)) != 0)
  161. #define SET(i,m) (*(m) |= (i))
  162. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  163. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  164. #define POLLEX_SET (POLLPRI)
  165. int do_select(int n, fd_set_bits *fds, s64 *timeout)
  166. {
  167. struct poll_wqueues table;
  168. poll_table *wait;
  169. int retval, i;
  170. rcu_read_lock();
  171. retval = max_select_fd(n, fds);
  172. rcu_read_unlock();
  173. if (retval < 0)
  174. return retval;
  175. n = retval;
  176. poll_initwait(&table);
  177. wait = &table.pt;
  178. if (!*timeout)
  179. wait = NULL;
  180. retval = 0;
  181. for (;;) {
  182. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  183. long __timeout;
  184. set_current_state(TASK_INTERRUPTIBLE);
  185. inp = fds->in; outp = fds->out; exp = fds->ex;
  186. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  187. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  188. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  189. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  190. const struct file_operations *f_op = NULL;
  191. struct file *file = NULL;
  192. in = *inp++; out = *outp++; ex = *exp++;
  193. all_bits = in | out | ex;
  194. if (all_bits == 0) {
  195. i += __NFDBITS;
  196. continue;
  197. }
  198. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  199. int fput_needed;
  200. if (i >= n)
  201. break;
  202. if (!(bit & all_bits))
  203. continue;
  204. file = fget_light(i, &fput_needed);
  205. if (file) {
  206. f_op = file->f_op;
  207. mask = DEFAULT_POLLMASK;
  208. if (f_op && f_op->poll)
  209. mask = (*f_op->poll)(file, retval ? NULL : wait);
  210. fput_light(file, fput_needed);
  211. if ((mask & POLLIN_SET) && (in & bit)) {
  212. res_in |= bit;
  213. retval++;
  214. }
  215. if ((mask & POLLOUT_SET) && (out & bit)) {
  216. res_out |= bit;
  217. retval++;
  218. }
  219. if ((mask & POLLEX_SET) && (ex & bit)) {
  220. res_ex |= bit;
  221. retval++;
  222. }
  223. }
  224. cond_resched();
  225. }
  226. if (res_in)
  227. *rinp = res_in;
  228. if (res_out)
  229. *routp = res_out;
  230. if (res_ex)
  231. *rexp = res_ex;
  232. }
  233. wait = NULL;
  234. if (retval || !*timeout || signal_pending(current))
  235. break;
  236. if(table.error) {
  237. retval = table.error;
  238. break;
  239. }
  240. if (*timeout < 0) {
  241. /* Wait indefinitely */
  242. __timeout = MAX_SCHEDULE_TIMEOUT;
  243. } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
  244. /* Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in a loop */
  245. __timeout = MAX_SCHEDULE_TIMEOUT - 1;
  246. *timeout -= __timeout;
  247. } else {
  248. __timeout = *timeout;
  249. *timeout = 0;
  250. }
  251. __timeout = schedule_timeout(__timeout);
  252. if (*timeout >= 0)
  253. *timeout += __timeout;
  254. }
  255. __set_current_state(TASK_RUNNING);
  256. poll_freewait(&table);
  257. return retval;
  258. }
  259. /*
  260. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  261. * like to be certain this leads to no problems. So I return
  262. * EINTR just for safety.
  263. *
  264. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  265. * I'm trying ERESTARTNOHAND which restart only when you want to.
  266. */
  267. #define MAX_SELECT_SECONDS \
  268. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  269. static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  270. fd_set __user *exp, s64 *timeout)
  271. {
  272. fd_set_bits fds;
  273. void *bits;
  274. int ret, max_fds;
  275. unsigned int size;
  276. struct fdtable *fdt;
  277. /* Allocate small arguments on the stack to save memory and be faster */
  278. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  279. ret = -EINVAL;
  280. if (n < 0)
  281. goto out_nofds;
  282. /* max_fds can increase, so grab it once to avoid race */
  283. rcu_read_lock();
  284. fdt = files_fdtable(current->files);
  285. max_fds = fdt->max_fds;
  286. rcu_read_unlock();
  287. if (n > max_fds)
  288. n = max_fds;
  289. /*
  290. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  291. * since we used fdset we need to allocate memory in units of
  292. * long-words.
  293. */
  294. size = FDS_BYTES(n);
  295. bits = stack_fds;
  296. if (size > sizeof(stack_fds) / 6) {
  297. /* Not enough space in on-stack array; must use kmalloc */
  298. ret = -ENOMEM;
  299. bits = kmalloc(6 * size, GFP_KERNEL);
  300. if (!bits)
  301. goto out_nofds;
  302. }
  303. fds.in = bits;
  304. fds.out = bits + size;
  305. fds.ex = bits + 2*size;
  306. fds.res_in = bits + 3*size;
  307. fds.res_out = bits + 4*size;
  308. fds.res_ex = bits + 5*size;
  309. if ((ret = get_fd_set(n, inp, fds.in)) ||
  310. (ret = get_fd_set(n, outp, fds.out)) ||
  311. (ret = get_fd_set(n, exp, fds.ex)))
  312. goto out;
  313. zero_fd_set(n, fds.res_in);
  314. zero_fd_set(n, fds.res_out);
  315. zero_fd_set(n, fds.res_ex);
  316. ret = do_select(n, &fds, timeout);
  317. if (ret < 0)
  318. goto out;
  319. if (!ret) {
  320. ret = -ERESTARTNOHAND;
  321. if (signal_pending(current))
  322. goto out;
  323. ret = 0;
  324. }
  325. if (set_fd_set(n, inp, fds.res_in) ||
  326. set_fd_set(n, outp, fds.res_out) ||
  327. set_fd_set(n, exp, fds.res_ex))
  328. ret = -EFAULT;
  329. out:
  330. if (bits != stack_fds)
  331. kfree(bits);
  332. out_nofds:
  333. return ret;
  334. }
  335. asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  336. fd_set __user *exp, struct timeval __user *tvp)
  337. {
  338. s64 timeout = -1;
  339. struct timeval tv;
  340. int ret;
  341. if (tvp) {
  342. if (copy_from_user(&tv, tvp, sizeof(tv)))
  343. return -EFAULT;
  344. if (tv.tv_sec < 0 || tv.tv_usec < 0)
  345. return -EINVAL;
  346. /* Cast to u64 to make GCC stop complaining */
  347. if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
  348. timeout = -1; /* infinite */
  349. else {
  350. timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
  351. timeout += tv.tv_sec * HZ;
  352. }
  353. }
  354. ret = core_sys_select(n, inp, outp, exp, &timeout);
  355. if (tvp) {
  356. struct timeval rtv;
  357. if (current->personality & STICKY_TIMEOUTS)
  358. goto sticky;
  359. rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
  360. rtv.tv_sec = timeout;
  361. if (timeval_compare(&rtv, &tv) >= 0)
  362. rtv = tv;
  363. if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
  364. sticky:
  365. /*
  366. * If an application puts its timeval in read-only
  367. * memory, we don't want the Linux-specific update to
  368. * the timeval to cause a fault after the select has
  369. * completed successfully. However, because we're not
  370. * updating the timeval, we can't restart the system
  371. * call.
  372. */
  373. if (ret == -ERESTARTNOHAND)
  374. ret = -EINTR;
  375. }
  376. }
  377. return ret;
  378. }
  379. #ifdef TIF_RESTORE_SIGMASK
  380. asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
  381. fd_set __user *exp, struct timespec __user *tsp,
  382. const sigset_t __user *sigmask, size_t sigsetsize)
  383. {
  384. s64 timeout = MAX_SCHEDULE_TIMEOUT;
  385. sigset_t ksigmask, sigsaved;
  386. struct timespec ts;
  387. int ret;
  388. if (tsp) {
  389. if (copy_from_user(&ts, tsp, sizeof(ts)))
  390. return -EFAULT;
  391. if (ts.tv_sec < 0 || ts.tv_nsec < 0)
  392. return -EINVAL;
  393. /* Cast to u64 to make GCC stop complaining */
  394. if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
  395. timeout = -1; /* infinite */
  396. else {
  397. timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
  398. timeout += ts.tv_sec * HZ;
  399. }
  400. }
  401. if (sigmask) {
  402. /* XXX: Don't preclude handling different sized sigset_t's. */
  403. if (sigsetsize != sizeof(sigset_t))
  404. return -EINVAL;
  405. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  406. return -EFAULT;
  407. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  408. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  409. }
  410. ret = core_sys_select(n, inp, outp, exp, &timeout);
  411. if (tsp) {
  412. struct timespec rts;
  413. if (current->personality & STICKY_TIMEOUTS)
  414. goto sticky;
  415. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  416. 1000;
  417. rts.tv_sec = timeout;
  418. if (timespec_compare(&rts, &ts) >= 0)
  419. rts = ts;
  420. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  421. sticky:
  422. /*
  423. * If an application puts its timeval in read-only
  424. * memory, we don't want the Linux-specific update to
  425. * the timeval to cause a fault after the select has
  426. * completed successfully. However, because we're not
  427. * updating the timeval, we can't restart the system
  428. * call.
  429. */
  430. if (ret == -ERESTARTNOHAND)
  431. ret = -EINTR;
  432. }
  433. }
  434. if (ret == -ERESTARTNOHAND) {
  435. /*
  436. * Don't restore the signal mask yet. Let do_signal() deliver
  437. * the signal on the way back to userspace, before the signal
  438. * mask is restored.
  439. */
  440. if (sigmask) {
  441. memcpy(&current->saved_sigmask, &sigsaved,
  442. sizeof(sigsaved));
  443. set_thread_flag(TIF_RESTORE_SIGMASK);
  444. }
  445. } else if (sigmask)
  446. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  447. return ret;
  448. }
  449. /*
  450. * Most architectures can't handle 7-argument syscalls. So we provide a
  451. * 6-argument version where the sixth argument is a pointer to a structure
  452. * which has a pointer to the sigset_t itself followed by a size_t containing
  453. * the sigset size.
  454. */
  455. asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
  456. fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
  457. {
  458. size_t sigsetsize = 0;
  459. sigset_t __user *up = NULL;
  460. if (sig) {
  461. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  462. || __get_user(up, (sigset_t __user * __user *)sig)
  463. || __get_user(sigsetsize,
  464. (size_t __user *)(sig+sizeof(void *))))
  465. return -EFAULT;
  466. }
  467. return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
  468. }
  469. #endif /* TIF_RESTORE_SIGMASK */
  470. struct poll_list {
  471. struct poll_list *next;
  472. int len;
  473. struct pollfd entries[0];
  474. };
  475. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  476. /*
  477. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  478. * interested in events matching the pollfd->events mask, and the result
  479. * matching that mask is both recorded in pollfd->revents and returned. The
  480. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  481. * if non-NULL.
  482. */
  483. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  484. {
  485. unsigned int mask;
  486. int fd;
  487. mask = 0;
  488. fd = pollfd->fd;
  489. if (fd >= 0) {
  490. int fput_needed;
  491. struct file * file;
  492. file = fget_light(fd, &fput_needed);
  493. mask = POLLNVAL;
  494. if (file != NULL) {
  495. mask = DEFAULT_POLLMASK;
  496. if (file->f_op && file->f_op->poll)
  497. mask = file->f_op->poll(file, pwait);
  498. /* Mask out unneeded events. */
  499. mask &= pollfd->events | POLLERR | POLLHUP;
  500. fput_light(file, fput_needed);
  501. }
  502. }
  503. pollfd->revents = mask;
  504. return mask;
  505. }
  506. static int do_poll(unsigned int nfds, struct poll_list *list,
  507. struct poll_wqueues *wait, s64 *timeout)
  508. {
  509. int count = 0;
  510. poll_table* pt = &wait->pt;
  511. /* Optimise the no-wait case */
  512. if (!(*timeout))
  513. pt = NULL;
  514. for (;;) {
  515. struct poll_list *walk;
  516. long __timeout;
  517. set_current_state(TASK_INTERRUPTIBLE);
  518. for (walk = list; walk != NULL; walk = walk->next) {
  519. struct pollfd * pfd, * pfd_end;
  520. pfd = walk->entries;
  521. pfd_end = pfd + walk->len;
  522. for (; pfd != pfd_end; pfd++) {
  523. /*
  524. * Fish for events. If we found one, record it
  525. * and kill the poll_table, so we don't
  526. * needlessly register any other waiters after
  527. * this. They'll get immediately deregistered
  528. * when we break out and return.
  529. */
  530. if (do_pollfd(pfd, pt)) {
  531. count++;
  532. pt = NULL;
  533. }
  534. }
  535. }
  536. /*
  537. * All waiters have already been registered, so don't provide
  538. * a poll_table to them on the next loop iteration.
  539. */
  540. pt = NULL;
  541. if (count || !*timeout || signal_pending(current))
  542. break;
  543. count = wait->error;
  544. if (count)
  545. break;
  546. if (*timeout < 0) {
  547. /* Wait indefinitely */
  548. __timeout = MAX_SCHEDULE_TIMEOUT;
  549. } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
  550. /*
  551. * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
  552. * a loop
  553. */
  554. __timeout = MAX_SCHEDULE_TIMEOUT - 1;
  555. *timeout -= __timeout;
  556. } else {
  557. __timeout = *timeout;
  558. *timeout = 0;
  559. }
  560. __timeout = schedule_timeout(__timeout);
  561. if (*timeout >= 0)
  562. *timeout += __timeout;
  563. }
  564. __set_current_state(TASK_RUNNING);
  565. return count;
  566. }
  567. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  568. sizeof(struct pollfd))
  569. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
  570. {
  571. struct poll_wqueues table;
  572. int fdcount, err;
  573. unsigned int i;
  574. struct poll_list *head;
  575. struct poll_list *walk;
  576. /* Allocate small arguments on the stack to save memory and be
  577. faster - use long to make sure the buffer is aligned properly
  578. on 64 bit archs to avoid unaligned access */
  579. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  580. struct poll_list *stack_pp = NULL;
  581. /* Do a sanity check on nfds ... */
  582. if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  583. return -EINVAL;
  584. poll_initwait(&table);
  585. head = NULL;
  586. walk = NULL;
  587. i = nfds;
  588. err = -ENOMEM;
  589. while(i!=0) {
  590. struct poll_list *pp;
  591. int num, size;
  592. if (stack_pp == NULL)
  593. num = N_STACK_PPS;
  594. else
  595. num = POLLFD_PER_PAGE;
  596. if (num > i)
  597. num = i;
  598. size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
  599. if (!stack_pp)
  600. stack_pp = pp = (struct poll_list *)stack_pps;
  601. else {
  602. pp = kmalloc(size, GFP_KERNEL);
  603. if (!pp)
  604. goto out_fds;
  605. }
  606. pp->next=NULL;
  607. pp->len = num;
  608. if (head == NULL)
  609. head = pp;
  610. else
  611. walk->next = pp;
  612. walk = pp;
  613. if (copy_from_user(pp->entries, ufds + nfds-i,
  614. sizeof(struct pollfd)*num)) {
  615. err = -EFAULT;
  616. goto out_fds;
  617. }
  618. i -= pp->len;
  619. }
  620. fdcount = do_poll(nfds, head, &table, timeout);
  621. /* OK, now copy the revents fields back to user space. */
  622. walk = head;
  623. err = -EFAULT;
  624. while(walk != NULL) {
  625. struct pollfd *fds = walk->entries;
  626. int j;
  627. for (j=0; j < walk->len; j++, ufds++) {
  628. if(__put_user(fds[j].revents, &ufds->revents))
  629. goto out_fds;
  630. }
  631. walk = walk->next;
  632. }
  633. err = fdcount;
  634. if (!fdcount && signal_pending(current))
  635. err = -EINTR;
  636. out_fds:
  637. walk = head;
  638. while(walk!=NULL) {
  639. struct poll_list *pp = walk->next;
  640. if (walk != stack_pp)
  641. kfree(walk);
  642. walk = pp;
  643. }
  644. poll_freewait(&table);
  645. return err;
  646. }
  647. asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  648. long timeout_msecs)
  649. {
  650. s64 timeout_jiffies;
  651. if (timeout_msecs > 0) {
  652. #if HZ > 1000
  653. /* We can only overflow if HZ > 1000 */
  654. if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
  655. timeout_jiffies = -1;
  656. else
  657. #endif
  658. timeout_jiffies = msecs_to_jiffies(timeout_msecs);
  659. } else {
  660. /* Infinite (< 0) or no (0) timeout */
  661. timeout_jiffies = timeout_msecs;
  662. }
  663. return do_sys_poll(ufds, nfds, &timeout_jiffies);
  664. }
  665. #ifdef TIF_RESTORE_SIGMASK
  666. asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
  667. struct timespec __user *tsp, const sigset_t __user *sigmask,
  668. size_t sigsetsize)
  669. {
  670. sigset_t ksigmask, sigsaved;
  671. struct timespec ts;
  672. s64 timeout = -1;
  673. int ret;
  674. if (tsp) {
  675. if (copy_from_user(&ts, tsp, sizeof(ts)))
  676. return -EFAULT;
  677. /* Cast to u64 to make GCC stop complaining */
  678. if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
  679. timeout = -1; /* infinite */
  680. else {
  681. timeout = DIV_ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
  682. timeout += ts.tv_sec * HZ;
  683. }
  684. }
  685. if (sigmask) {
  686. /* XXX: Don't preclude handling different sized sigset_t's. */
  687. if (sigsetsize != sizeof(sigset_t))
  688. return -EINVAL;
  689. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  690. return -EFAULT;
  691. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  692. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  693. }
  694. ret = do_sys_poll(ufds, nfds, &timeout);
  695. /* We can restart this syscall, usually */
  696. if (ret == -EINTR) {
  697. /*
  698. * Don't restore the signal mask yet. Let do_signal() deliver
  699. * the signal on the way back to userspace, before the signal
  700. * mask is restored.
  701. */
  702. if (sigmask) {
  703. memcpy(&current->saved_sigmask, &sigsaved,
  704. sizeof(sigsaved));
  705. set_thread_flag(TIF_RESTORE_SIGMASK);
  706. }
  707. ret = -ERESTARTNOHAND;
  708. } else if (sigmask)
  709. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  710. if (tsp && timeout >= 0) {
  711. struct timespec rts;
  712. if (current->personality & STICKY_TIMEOUTS)
  713. goto sticky;
  714. /* Yes, we know it's actually an s64, but it's also positive. */
  715. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  716. 1000;
  717. rts.tv_sec = timeout;
  718. if (timespec_compare(&rts, &ts) >= 0)
  719. rts = ts;
  720. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  721. sticky:
  722. /*
  723. * If an application puts its timeval in read-only
  724. * memory, we don't want the Linux-specific update to
  725. * the timeval to cause a fault after the select has
  726. * completed successfully. However, because we're not
  727. * updating the timeval, we can't restart the system
  728. * call.
  729. */
  730. if (ret == -ERESTARTNOHAND && timeout >= 0)
  731. ret = -EINTR;
  732. }
  733. }
  734. return ret;
  735. }
  736. #endif /* TIF_RESTORE_SIGMASK */