select.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. char *bits;
  275. int ret, size, max_fdset;
  276. struct fdtable *fdt;
  277. /* Allocate small arguments on the stack to save memory and be faster */
  278. char stack_fds[SELECT_STACK_ALLOC];
  279. ret = -EINVAL;
  280. if (n < 0)
  281. goto out_nofds;
  282. /* max_fdset can increase, so grab it once to avoid race */
  283. rcu_read_lock();
  284. fdt = files_fdtable(current->files);
  285. max_fdset = fdt->max_fdset;
  286. rcu_read_unlock();
  287. if (n > max_fdset)
  288. n = max_fdset;
  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. ret = -ENOMEM;
  295. size = FDS_BYTES(n);
  296. if (6*size < SELECT_STACK_ALLOC)
  297. bits = stack_fds;
  298. else
  299. bits = kmalloc(6 * size, GFP_KERNEL);
  300. if (!bits)
  301. goto out_nofds;
  302. fds.in = (unsigned long *) bits;
  303. fds.out = (unsigned long *) (bits + size);
  304. fds.ex = (unsigned long *) (bits + 2*size);
  305. fds.res_in = (unsigned long *) (bits + 3*size);
  306. fds.res_out = (unsigned long *) (bits + 4*size);
  307. fds.res_ex = (unsigned long *) (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 = 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 = 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. static void do_pollfd(unsigned int num, struct pollfd * fdpage,
  476. poll_table ** pwait, int *count)
  477. {
  478. int i;
  479. for (i = 0; i < num; i++) {
  480. int fd;
  481. unsigned int mask;
  482. struct pollfd *fdp;
  483. mask = 0;
  484. fdp = fdpage+i;
  485. fd = fdp->fd;
  486. if (fd >= 0) {
  487. int fput_needed;
  488. struct file * file = fget_light(fd, &fput_needed);
  489. mask = POLLNVAL;
  490. if (file != NULL) {
  491. mask = DEFAULT_POLLMASK;
  492. if (file->f_op && file->f_op->poll)
  493. mask = file->f_op->poll(file, *pwait);
  494. mask &= fdp->events | POLLERR | POLLHUP;
  495. fput_light(file, fput_needed);
  496. }
  497. if (mask) {
  498. *pwait = NULL;
  499. (*count)++;
  500. }
  501. }
  502. fdp->revents = mask;
  503. }
  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. walk = list;
  518. while(walk != NULL) {
  519. do_pollfd( walk->len, walk->entries, &pt, &count);
  520. walk = walk->next;
  521. }
  522. pt = NULL;
  523. if (count || !*timeout || signal_pending(current))
  524. break;
  525. count = wait->error;
  526. if (count)
  527. break;
  528. if (*timeout < 0) {
  529. /* Wait indefinitely */
  530. __timeout = MAX_SCHEDULE_TIMEOUT;
  531. } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
  532. /*
  533. * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
  534. * a loop
  535. */
  536. __timeout = MAX_SCHEDULE_TIMEOUT - 1;
  537. *timeout -= __timeout;
  538. } else {
  539. __timeout = *timeout;
  540. *timeout = 0;
  541. }
  542. __timeout = schedule_timeout(__timeout);
  543. if (*timeout >= 0)
  544. *timeout += __timeout;
  545. }
  546. __set_current_state(TASK_RUNNING);
  547. return count;
  548. }
  549. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  550. sizeof(struct pollfd))
  551. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
  552. {
  553. struct poll_wqueues table;
  554. int fdcount, err;
  555. unsigned int i;
  556. struct poll_list *head;
  557. struct poll_list *walk;
  558. struct fdtable *fdt;
  559. int max_fdset;
  560. /* Allocate small arguments on the stack to save memory and be faster */
  561. char stack_pps[POLL_STACK_ALLOC];
  562. struct poll_list *stack_pp = NULL;
  563. /* Do a sanity check on nfds ... */
  564. rcu_read_lock();
  565. fdt = files_fdtable(current->files);
  566. max_fdset = fdt->max_fdset;
  567. rcu_read_unlock();
  568. if (nfds > max_fdset && nfds > OPEN_MAX)
  569. return -EINVAL;
  570. poll_initwait(&table);
  571. head = NULL;
  572. walk = NULL;
  573. i = nfds;
  574. err = -ENOMEM;
  575. while(i!=0) {
  576. struct poll_list *pp;
  577. int num, size;
  578. if (stack_pp == NULL)
  579. num = N_STACK_PPS;
  580. else
  581. num = POLLFD_PER_PAGE;
  582. if (num > i)
  583. num = i;
  584. size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
  585. if (!stack_pp)
  586. stack_pp = pp = (struct poll_list *)stack_pps;
  587. else {
  588. pp = kmalloc(size, GFP_KERNEL);
  589. if (!pp)
  590. goto out_fds;
  591. }
  592. pp->next=NULL;
  593. pp->len = num;
  594. if (head == NULL)
  595. head = pp;
  596. else
  597. walk->next = pp;
  598. walk = pp;
  599. if (copy_from_user(pp->entries, ufds + nfds-i,
  600. sizeof(struct pollfd)*num)) {
  601. err = -EFAULT;
  602. goto out_fds;
  603. }
  604. i -= pp->len;
  605. }
  606. fdcount = do_poll(nfds, head, &table, timeout);
  607. /* OK, now copy the revents fields back to user space. */
  608. walk = head;
  609. err = -EFAULT;
  610. while(walk != NULL) {
  611. struct pollfd *fds = walk->entries;
  612. int j;
  613. for (j=0; j < walk->len; j++, ufds++) {
  614. if(__put_user(fds[j].revents, &ufds->revents))
  615. goto out_fds;
  616. }
  617. walk = walk->next;
  618. }
  619. err = fdcount;
  620. if (!fdcount && signal_pending(current))
  621. err = -EINTR;
  622. out_fds:
  623. walk = head;
  624. while(walk!=NULL) {
  625. struct poll_list *pp = walk->next;
  626. if (walk != stack_pp)
  627. kfree(walk);
  628. walk = pp;
  629. }
  630. poll_freewait(&table);
  631. return err;
  632. }
  633. asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  634. long timeout_msecs)
  635. {
  636. s64 timeout_jiffies = 0;
  637. if (timeout_msecs) {
  638. #if HZ > 1000
  639. /* We can only overflow if HZ > 1000 */
  640. if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
  641. timeout_jiffies = -1;
  642. else
  643. #endif
  644. timeout_jiffies = msecs_to_jiffies(timeout_msecs);
  645. }
  646. return do_sys_poll(ufds, nfds, &timeout_jiffies);
  647. }
  648. #ifdef TIF_RESTORE_SIGMASK
  649. asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
  650. struct timespec __user *tsp, const sigset_t __user *sigmask,
  651. size_t sigsetsize)
  652. {
  653. sigset_t ksigmask, sigsaved;
  654. struct timespec ts;
  655. s64 timeout = -1;
  656. int ret;
  657. if (tsp) {
  658. if (copy_from_user(&ts, tsp, sizeof(ts)))
  659. return -EFAULT;
  660. /* Cast to u64 to make GCC stop complaining */
  661. if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
  662. timeout = -1; /* infinite */
  663. else {
  664. timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
  665. timeout += ts.tv_sec * HZ;
  666. }
  667. }
  668. if (sigmask) {
  669. /* XXX: Don't preclude handling different sized sigset_t's. */
  670. if (sigsetsize != sizeof(sigset_t))
  671. return -EINVAL;
  672. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  673. return -EFAULT;
  674. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  675. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  676. }
  677. ret = do_sys_poll(ufds, nfds, &timeout);
  678. /* We can restart this syscall, usually */
  679. if (ret == -EINTR) {
  680. /*
  681. * Don't restore the signal mask yet. Let do_signal() deliver
  682. * the signal on the way back to userspace, before the signal
  683. * mask is restored.
  684. */
  685. if (sigmask) {
  686. memcpy(&current->saved_sigmask, &sigsaved,
  687. sizeof(sigsaved));
  688. set_thread_flag(TIF_RESTORE_SIGMASK);
  689. }
  690. ret = -ERESTARTNOHAND;
  691. } else if (sigmask)
  692. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  693. if (tsp && timeout >= 0) {
  694. struct timespec rts;
  695. if (current->personality & STICKY_TIMEOUTS)
  696. goto sticky;
  697. /* Yes, we know it's actually an s64, but it's also positive. */
  698. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  699. 1000;
  700. rts.tv_sec = timeout;
  701. if (timespec_compare(&rts, &ts) >= 0)
  702. rts = ts;
  703. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  704. sticky:
  705. /*
  706. * If an application puts its timeval in read-only
  707. * memory, we don't want the Linux-specific update to
  708. * the timeval to cause a fault after the select has
  709. * completed successfully. However, because we're not
  710. * updating the timeval, we can't restart the system
  711. * call.
  712. */
  713. if (ret == -ERESTARTNOHAND && timeout >= 0)
  714. ret = -EINTR;
  715. }
  716. }
  717. return ret;
  718. }
  719. #endif /* TIF_RESTORE_SIGMASK */