select.c 20 KB

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