select.c 19 KB

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