select.c 20 KB

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