select.c 19 KB

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