select.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. 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. if (i >= n)
  201. break;
  202. if (!(bit & all_bits))
  203. continue;
  204. file = fget(i);
  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(file);
  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. char *bits;
  274. int ret, size, max_fdset;
  275. struct fdtable *fdt;
  276. /* Allocate small arguments on the stack to save memory and be faster */
  277. char stack_fds[SELECT_STACK_ALLOC];
  278. ret = -EINVAL;
  279. if (n < 0)
  280. goto out_nofds;
  281. /* max_fdset can increase, so grab it once to avoid race */
  282. rcu_read_lock();
  283. fdt = files_fdtable(current->files);
  284. max_fdset = fdt->max_fdset;
  285. rcu_read_unlock();
  286. if (n > max_fdset)
  287. n = max_fdset;
  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. ret = -ENOMEM;
  294. size = FDS_BYTES(n);
  295. if (6*size < SELECT_STACK_ALLOC)
  296. bits = stack_fds;
  297. else
  298. bits = kmalloc(6 * size, GFP_KERNEL);
  299. if (!bits)
  300. goto out_nofds;
  301. fds.in = (unsigned long *) bits;
  302. fds.out = (unsigned long *) (bits + size);
  303. fds.ex = (unsigned long *) (bits + 2*size);
  304. fds.res_in = (unsigned long *) (bits + 3*size);
  305. fds.res_out = (unsigned long *) (bits + 4*size);
  306. fds.res_ex = (unsigned long *) (bits + 5*size);
  307. if ((ret = get_fd_set(n, inp, fds.in)) ||
  308. (ret = get_fd_set(n, outp, fds.out)) ||
  309. (ret = get_fd_set(n, exp, fds.ex)))
  310. goto out;
  311. zero_fd_set(n, fds.res_in);
  312. zero_fd_set(n, fds.res_out);
  313. zero_fd_set(n, fds.res_ex);
  314. ret = do_select(n, &fds, timeout);
  315. if (ret < 0)
  316. goto out;
  317. if (!ret) {
  318. ret = -ERESTARTNOHAND;
  319. if (signal_pending(current))
  320. goto out;
  321. ret = 0;
  322. }
  323. if (set_fd_set(n, inp, fds.res_in) ||
  324. set_fd_set(n, outp, fds.res_out) ||
  325. set_fd_set(n, exp, fds.res_ex))
  326. ret = -EFAULT;
  327. out:
  328. if (bits != stack_fds)
  329. kfree(bits);
  330. out_nofds:
  331. return ret;
  332. }
  333. asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  334. fd_set __user *exp, struct timeval __user *tvp)
  335. {
  336. s64 timeout = -1;
  337. struct timeval tv;
  338. int ret;
  339. if (tvp) {
  340. if (copy_from_user(&tv, tvp, sizeof(tv)))
  341. return -EFAULT;
  342. if (tv.tv_sec < 0 || tv.tv_usec < 0)
  343. return -EINVAL;
  344. /* Cast to u64 to make GCC stop complaining */
  345. if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
  346. timeout = -1; /* infinite */
  347. else {
  348. timeout = ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
  349. timeout += tv.tv_sec * HZ;
  350. }
  351. }
  352. ret = core_sys_select(n, inp, outp, exp, &timeout);
  353. if (tvp) {
  354. struct timeval rtv;
  355. if (current->personality & STICKY_TIMEOUTS)
  356. goto sticky;
  357. rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
  358. rtv.tv_sec = timeout;
  359. if (timeval_compare(&rtv, &tv) >= 0)
  360. rtv = tv;
  361. if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
  362. sticky:
  363. /*
  364. * If an application puts its timeval in read-only
  365. * memory, we don't want the Linux-specific update to
  366. * the timeval to cause a fault after the select has
  367. * completed successfully. However, because we're not
  368. * updating the timeval, we can't restart the system
  369. * call.
  370. */
  371. if (ret == -ERESTARTNOHAND)
  372. ret = -EINTR;
  373. }
  374. }
  375. return ret;
  376. }
  377. #ifdef TIF_RESTORE_SIGMASK
  378. asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp,
  379. fd_set __user *exp, struct timespec __user *tsp,
  380. const sigset_t __user *sigmask, size_t sigsetsize)
  381. {
  382. s64 timeout = MAX_SCHEDULE_TIMEOUT;
  383. sigset_t ksigmask, sigsaved;
  384. struct timespec ts;
  385. int ret;
  386. if (tsp) {
  387. if (copy_from_user(&ts, tsp, sizeof(ts)))
  388. return -EFAULT;
  389. if (ts.tv_sec < 0 || ts.tv_nsec < 0)
  390. return -EINVAL;
  391. /* Cast to u64 to make GCC stop complaining */
  392. if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
  393. timeout = -1; /* infinite */
  394. else {
  395. timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
  396. timeout += ts.tv_sec * HZ;
  397. }
  398. }
  399. if (sigmask) {
  400. /* XXX: Don't preclude handling different sized sigset_t's. */
  401. if (sigsetsize != sizeof(sigset_t))
  402. return -EINVAL;
  403. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  404. return -EFAULT;
  405. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  406. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  407. }
  408. ret = core_sys_select(n, inp, outp, exp, &timeout);
  409. if (tsp) {
  410. struct timespec rts;
  411. if (current->personality & STICKY_TIMEOUTS)
  412. goto sticky;
  413. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  414. 1000;
  415. rts.tv_sec = timeout;
  416. if (timespec_compare(&rts, &ts) >= 0)
  417. rts = ts;
  418. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  419. sticky:
  420. /*
  421. * If an application puts its timeval in read-only
  422. * memory, we don't want the Linux-specific update to
  423. * the timeval to cause a fault after the select has
  424. * completed successfully. However, because we're not
  425. * updating the timeval, we can't restart the system
  426. * call.
  427. */
  428. if (ret == -ERESTARTNOHAND)
  429. ret = -EINTR;
  430. }
  431. }
  432. if (ret == -ERESTARTNOHAND) {
  433. /*
  434. * Don't restore the signal mask yet. Let do_signal() deliver
  435. * the signal on the way back to userspace, before the signal
  436. * mask is restored.
  437. */
  438. if (sigmask) {
  439. memcpy(&current->saved_sigmask, &sigsaved,
  440. sizeof(sigsaved));
  441. set_thread_flag(TIF_RESTORE_SIGMASK);
  442. }
  443. } else if (sigmask)
  444. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  445. return ret;
  446. }
  447. /*
  448. * Most architectures can't handle 7-argument syscalls. So we provide a
  449. * 6-argument version where the sixth argument is a pointer to a structure
  450. * which has a pointer to the sigset_t itself followed by a size_t containing
  451. * the sigset size.
  452. */
  453. asmlinkage long sys_pselect6(int n, fd_set __user *inp, fd_set __user *outp,
  454. fd_set __user *exp, struct timespec __user *tsp, void __user *sig)
  455. {
  456. size_t sigsetsize = 0;
  457. sigset_t __user *up = NULL;
  458. if (sig) {
  459. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  460. || __get_user(up, (sigset_t __user * __user *)sig)
  461. || __get_user(sigsetsize,
  462. (size_t __user *)(sig+sizeof(void *))))
  463. return -EFAULT;
  464. }
  465. return sys_pselect7(n, inp, outp, exp, tsp, up, sigsetsize);
  466. }
  467. #endif /* TIF_RESTORE_SIGMASK */
  468. struct poll_list {
  469. struct poll_list *next;
  470. int len;
  471. struct pollfd entries[0];
  472. };
  473. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  474. static void do_pollfd(unsigned int num, struct pollfd * fdpage,
  475. poll_table ** pwait, int *count)
  476. {
  477. int i;
  478. for (i = 0; i < num; i++) {
  479. int fd;
  480. unsigned int mask;
  481. struct pollfd *fdp;
  482. mask = 0;
  483. fdp = fdpage+i;
  484. fd = fdp->fd;
  485. if (fd >= 0) {
  486. struct file * file = fget(fd);
  487. mask = POLLNVAL;
  488. if (file != NULL) {
  489. mask = DEFAULT_POLLMASK;
  490. if (file->f_op && file->f_op->poll)
  491. mask = file->f_op->poll(file, *pwait);
  492. mask &= fdp->events | POLLERR | POLLHUP;
  493. fput(file);
  494. }
  495. if (mask) {
  496. *pwait = NULL;
  497. (*count)++;
  498. }
  499. }
  500. fdp->revents = mask;
  501. }
  502. }
  503. static int do_poll(unsigned int nfds, struct poll_list *list,
  504. struct poll_wqueues *wait, s64 *timeout)
  505. {
  506. int count = 0;
  507. poll_table* pt = &wait->pt;
  508. /* Optimise the no-wait case */
  509. if (!(*timeout))
  510. pt = NULL;
  511. for (;;) {
  512. struct poll_list *walk;
  513. long __timeout;
  514. set_current_state(TASK_INTERRUPTIBLE);
  515. walk = list;
  516. while(walk != NULL) {
  517. do_pollfd( walk->len, walk->entries, &pt, &count);
  518. walk = walk->next;
  519. }
  520. pt = NULL;
  521. if (count || !*timeout || signal_pending(current))
  522. break;
  523. count = wait->error;
  524. if (count)
  525. break;
  526. if (*timeout < 0) {
  527. /* Wait indefinitely */
  528. __timeout = MAX_SCHEDULE_TIMEOUT;
  529. } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
  530. /*
  531. * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
  532. * a loop
  533. */
  534. __timeout = MAX_SCHEDULE_TIMEOUT - 1;
  535. *timeout -= __timeout;
  536. } else {
  537. __timeout = *timeout;
  538. *timeout = 0;
  539. }
  540. __timeout = schedule_timeout(__timeout);
  541. if (*timeout >= 0)
  542. *timeout += __timeout;
  543. }
  544. __set_current_state(TASK_RUNNING);
  545. return count;
  546. }
  547. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  548. sizeof(struct pollfd))
  549. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
  550. {
  551. struct poll_wqueues table;
  552. int fdcount, err;
  553. unsigned int i;
  554. struct poll_list *head;
  555. struct poll_list *walk;
  556. struct fdtable *fdt;
  557. int max_fdset;
  558. /* Allocate small arguments on the stack to save memory and be faster */
  559. char stack_pps[POLL_STACK_ALLOC];
  560. struct poll_list *stack_pp = NULL;
  561. /* Do a sanity check on nfds ... */
  562. rcu_read_lock();
  563. fdt = files_fdtable(current->files);
  564. max_fdset = fdt->max_fdset;
  565. rcu_read_unlock();
  566. if (nfds > max_fdset && nfds > OPEN_MAX)
  567. return -EINVAL;
  568. poll_initwait(&table);
  569. head = NULL;
  570. walk = NULL;
  571. i = nfds;
  572. err = -ENOMEM;
  573. while(i!=0) {
  574. struct poll_list *pp;
  575. int num, size;
  576. if (stack_pp == NULL)
  577. num = N_STACK_PPS;
  578. else
  579. num = POLLFD_PER_PAGE;
  580. if (num > i)
  581. num = i;
  582. size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
  583. if (!stack_pp)
  584. stack_pp = pp = (struct poll_list *)stack_pps;
  585. else {
  586. pp = kmalloc(size, GFP_KERNEL);
  587. if (!pp)
  588. goto out_fds;
  589. }
  590. pp->next=NULL;
  591. pp->len = num;
  592. if (head == NULL)
  593. head = pp;
  594. else
  595. walk->next = pp;
  596. walk = pp;
  597. if (copy_from_user(pp->entries, ufds + nfds-i,
  598. sizeof(struct pollfd)*num)) {
  599. err = -EFAULT;
  600. goto out_fds;
  601. }
  602. i -= pp->len;
  603. }
  604. fdcount = do_poll(nfds, head, &table, timeout);
  605. /* OK, now copy the revents fields back to user space. */
  606. walk = head;
  607. err = -EFAULT;
  608. while(walk != NULL) {
  609. struct pollfd *fds = walk->entries;
  610. int j;
  611. for (j=0; j < walk->len; j++, ufds++) {
  612. if(__put_user(fds[j].revents, &ufds->revents))
  613. goto out_fds;
  614. }
  615. walk = walk->next;
  616. }
  617. err = fdcount;
  618. if (!fdcount && signal_pending(current))
  619. err = -EINTR;
  620. out_fds:
  621. walk = head;
  622. while(walk!=NULL) {
  623. struct poll_list *pp = walk->next;
  624. if (walk != stack_pp)
  625. kfree(walk);
  626. walk = pp;
  627. }
  628. poll_freewait(&table);
  629. return err;
  630. }
  631. asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  632. long timeout_msecs)
  633. {
  634. s64 timeout_jiffies = 0;
  635. if (timeout_msecs) {
  636. #if HZ > 1000
  637. /* We can only overflow if HZ > 1000 */
  638. if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
  639. timeout_jiffies = -1;
  640. else
  641. #endif
  642. timeout_jiffies = msecs_to_jiffies(timeout_msecs);
  643. }
  644. return do_sys_poll(ufds, nfds, &timeout_jiffies);
  645. }
  646. #ifdef TIF_RESTORE_SIGMASK
  647. asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds,
  648. struct timespec __user *tsp, const sigset_t __user *sigmask,
  649. size_t sigsetsize)
  650. {
  651. sigset_t ksigmask, sigsaved;
  652. struct timespec ts;
  653. s64 timeout = -1;
  654. int ret;
  655. if (tsp) {
  656. if (copy_from_user(&ts, tsp, sizeof(ts)))
  657. return -EFAULT;
  658. /* Cast to u64 to make GCC stop complaining */
  659. if ((u64)ts.tv_sec >= (u64)MAX_INT64_SECONDS)
  660. timeout = -1; /* infinite */
  661. else {
  662. timeout = ROUND_UP(ts.tv_nsec, NSEC_PER_SEC/HZ);
  663. timeout += ts.tv_sec * HZ;
  664. }
  665. }
  666. if (sigmask) {
  667. /* XXX: Don't preclude handling different sized sigset_t's. */
  668. if (sigsetsize != sizeof(sigset_t))
  669. return -EINVAL;
  670. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  671. return -EFAULT;
  672. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  673. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  674. }
  675. ret = do_sys_poll(ufds, nfds, &timeout);
  676. /* We can restart this syscall, usually */
  677. if (ret == -EINTR) {
  678. /*
  679. * Don't restore the signal mask yet. Let do_signal() deliver
  680. * the signal on the way back to userspace, before the signal
  681. * mask is restored.
  682. */
  683. if (sigmask) {
  684. memcpy(&current->saved_sigmask, &sigsaved,
  685. sizeof(sigsaved));
  686. set_thread_flag(TIF_RESTORE_SIGMASK);
  687. }
  688. ret = -ERESTARTNOHAND;
  689. } else if (sigmask)
  690. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  691. if (tsp && timeout >= 0) {
  692. struct timespec rts;
  693. if (current->personality & STICKY_TIMEOUTS)
  694. goto sticky;
  695. /* Yes, we know it's actually an s64, but it's also positive. */
  696. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  697. 1000;
  698. rts.tv_sec = timeout;
  699. if (timespec_compare(&rts, &ts) >= 0)
  700. rts = ts;
  701. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  702. sticky:
  703. /*
  704. * If an application puts its timeval in read-only
  705. * memory, we don't want the Linux-specific update to
  706. * the timeval to cause a fault after the select has
  707. * completed successfully. However, because we're not
  708. * updating the timeval, we can't restart the system
  709. * call.
  710. */
  711. if (ret == -ERESTARTNOHAND && timeout >= 0)
  712. ret = -EINTR;
  713. }
  714. }
  715. return ret;
  716. }
  717. #endif /* TIF_RESTORE_SIGMASK */