select.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * This file contains the procedures for the handling of select and poll
  3. *
  4. * Created for Linux based loosely upon Mathius Lattner's minix
  5. * patches by Peter MacDonald. Heavily edited by Linus.
  6. *
  7. * 4 February 1994
  8. * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
  9. * flag set in its personality we do *not* modify the given timeout
  10. * parameter to reflect time remaining.
  11. *
  12. * 24 January 2000
  13. * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
  14. * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21. #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  22. #include <linux/file.h>
  23. #include <linux/fdtable.h>
  24. #include <linux/fs.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/hrtimer.h>
  27. #include <asm/uaccess.h>
  28. /*
  29. * Estimate expected accuracy in ns from a timeval.
  30. *
  31. * After quite a bit of churning around, we've settled on
  32. * a simple thing of taking 0.1% of the timeout as the
  33. * slack, with a cap of 100 msec.
  34. * "nice" tasks get a 0.5% slack instead.
  35. *
  36. * Consider this comment an open invitation to come up with even
  37. * better solutions..
  38. */
  39. #define MAX_SLACK (100 * NSEC_PER_MSEC)
  40. static long __estimate_accuracy(struct timespec *tv)
  41. {
  42. long slack;
  43. int divfactor = 1000;
  44. if (tv->tv_sec < 0)
  45. return 0;
  46. if (task_nice(current) > 0)
  47. divfactor = divfactor / 5;
  48. if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
  49. return MAX_SLACK;
  50. slack = tv->tv_nsec / divfactor;
  51. slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
  52. if (slack > MAX_SLACK)
  53. return MAX_SLACK;
  54. return slack;
  55. }
  56. static long estimate_accuracy(struct timespec *tv)
  57. {
  58. unsigned long ret;
  59. struct timespec now;
  60. /*
  61. * Realtime tasks get a slack of 0 for obvious reasons.
  62. */
  63. if (rt_task(current))
  64. return 0;
  65. ktime_get_ts(&now);
  66. now = timespec_sub(*tv, now);
  67. ret = __estimate_accuracy(&now);
  68. if (ret < current->timer_slack_ns)
  69. return current->timer_slack_ns;
  70. return ret;
  71. }
  72. struct poll_table_page {
  73. struct poll_table_page * next;
  74. struct poll_table_entry * entry;
  75. struct poll_table_entry entries[0];
  76. };
  77. #define POLL_TABLE_FULL(table) \
  78. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  79. /*
  80. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  81. * I have rewritten this, taking some shortcuts: This code may not be easy to
  82. * follow, but it should be free of race-conditions, and it's practical. If you
  83. * understand what I'm doing here, then you understand how the linux
  84. * sleep/wakeup mechanism works.
  85. *
  86. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  87. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  88. * as all select/poll functions have to call it to add an entry to the
  89. * poll table.
  90. */
  91. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  92. poll_table *p);
  93. void poll_initwait(struct poll_wqueues *pwq)
  94. {
  95. init_poll_funcptr(&pwq->pt, __pollwait);
  96. pwq->polling_task = current;
  97. pwq->triggered = 0;
  98. pwq->error = 0;
  99. pwq->table = NULL;
  100. pwq->inline_index = 0;
  101. }
  102. EXPORT_SYMBOL(poll_initwait);
  103. static void free_poll_entry(struct poll_table_entry *entry)
  104. {
  105. remove_wait_queue(entry->wait_address, &entry->wait);
  106. fput(entry->filp);
  107. }
  108. void poll_freewait(struct poll_wqueues *pwq)
  109. {
  110. struct poll_table_page * p = pwq->table;
  111. int i;
  112. for (i = 0; i < pwq->inline_index; i++)
  113. free_poll_entry(pwq->inline_entries + i);
  114. while (p) {
  115. struct poll_table_entry * entry;
  116. struct poll_table_page *old;
  117. entry = p->entry;
  118. do {
  119. entry--;
  120. free_poll_entry(entry);
  121. } while (entry > p->entries);
  122. old = p;
  123. p = p->next;
  124. free_page((unsigned long) old);
  125. }
  126. }
  127. EXPORT_SYMBOL(poll_freewait);
  128. static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
  129. {
  130. struct poll_table_page *table = p->table;
  131. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  132. return p->inline_entries + p->inline_index++;
  133. if (!table || POLL_TABLE_FULL(table)) {
  134. struct poll_table_page *new_table;
  135. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  136. if (!new_table) {
  137. p->error = -ENOMEM;
  138. return NULL;
  139. }
  140. new_table->entry = new_table->entries;
  141. new_table->next = table;
  142. p->table = new_table;
  143. table = new_table;
  144. }
  145. return table->entry++;
  146. }
  147. static int __pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
  148. {
  149. struct poll_wqueues *pwq = wait->private;
  150. DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
  151. /*
  152. * Although this function is called under waitqueue lock, LOCK
  153. * doesn't imply write barrier and the users expect write
  154. * barrier semantics on wakeup functions. The following
  155. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  156. * and is paired with set_mb() in poll_schedule_timeout.
  157. */
  158. smp_wmb();
  159. pwq->triggered = 1;
  160. /*
  161. * Perform the default wake up operation using a dummy
  162. * waitqueue.
  163. *
  164. * TODO: This is hacky but there currently is no interface to
  165. * pass in @sync. @sync is scheduled to be removed and once
  166. * that happens, wake_up_process() can be used directly.
  167. */
  168. return default_wake_function(&dummy_wait, mode, sync, key);
  169. }
  170. static int pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
  171. {
  172. struct poll_table_entry *entry;
  173. entry = container_of(wait, struct poll_table_entry, wait);
  174. if (key && !((unsigned long)key & entry->key))
  175. return 0;
  176. return __pollwake(wait, mode, sync, key);
  177. }
  178. /* Add a new entry */
  179. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  180. poll_table *p)
  181. {
  182. struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
  183. struct poll_table_entry *entry = poll_get_entry(pwq);
  184. if (!entry)
  185. return;
  186. get_file(filp);
  187. entry->filp = filp;
  188. entry->wait_address = wait_address;
  189. entry->key = p->key;
  190. init_waitqueue_func_entry(&entry->wait, pollwake);
  191. entry->wait.private = pwq;
  192. add_wait_queue(wait_address, &entry->wait);
  193. }
  194. int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
  195. ktime_t *expires, unsigned long slack)
  196. {
  197. int rc = -EINTR;
  198. set_current_state(state);
  199. if (!pwq->triggered)
  200. rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
  201. __set_current_state(TASK_RUNNING);
  202. /*
  203. * Prepare for the next iteration.
  204. *
  205. * The following set_mb() serves two purposes. First, it's
  206. * the counterpart rmb of the wmb in pollwake() such that data
  207. * written before wake up is always visible after wake up.
  208. * Second, the full barrier guarantees that triggered clearing
  209. * doesn't pass event check of the next iteration. Note that
  210. * this problem doesn't exist for the first iteration as
  211. * add_wait_queue() has full barrier semantics.
  212. */
  213. set_mb(pwq->triggered, 0);
  214. return rc;
  215. }
  216. EXPORT_SYMBOL(poll_schedule_timeout);
  217. /**
  218. * poll_select_set_timeout - helper function to setup the timeout value
  219. * @to: pointer to timespec variable for the final timeout
  220. * @sec: seconds (from user space)
  221. * @nsec: nanoseconds (from user space)
  222. *
  223. * Note, we do not use a timespec for the user space value here, That
  224. * way we can use the function for timeval and compat interfaces as well.
  225. *
  226. * Returns -EINVAL if sec/nsec are not normalized. Otherwise 0.
  227. */
  228. int poll_select_set_timeout(struct timespec *to, long sec, long nsec)
  229. {
  230. struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec};
  231. if (!timespec_valid(&ts))
  232. return -EINVAL;
  233. /* Optimize for the zero timeout value here */
  234. if (!sec && !nsec) {
  235. to->tv_sec = to->tv_nsec = 0;
  236. } else {
  237. ktime_get_ts(to);
  238. *to = timespec_add_safe(*to, ts);
  239. }
  240. return 0;
  241. }
  242. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  243. int timeval, int ret)
  244. {
  245. struct timespec rts;
  246. struct timeval rtv;
  247. if (!p)
  248. return ret;
  249. if (current->personality & STICKY_TIMEOUTS)
  250. goto sticky;
  251. /* No update for zero timeout */
  252. if (!end_time->tv_sec && !end_time->tv_nsec)
  253. return ret;
  254. ktime_get_ts(&rts);
  255. rts = timespec_sub(*end_time, rts);
  256. if (rts.tv_sec < 0)
  257. rts.tv_sec = rts.tv_nsec = 0;
  258. if (timeval) {
  259. rtv.tv_sec = rts.tv_sec;
  260. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  261. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  262. return ret;
  263. } else if (!copy_to_user(p, &rts, sizeof(rts)))
  264. return ret;
  265. /*
  266. * If an application puts its timeval in read-only memory, we
  267. * don't want the Linux-specific update to the timeval to
  268. * cause a fault after the select has completed
  269. * successfully. However, because we're not updating the
  270. * timeval, we can't restart the system call.
  271. */
  272. sticky:
  273. if (ret == -ERESTARTNOHAND)
  274. ret = -EINTR;
  275. return ret;
  276. }
  277. #define FDS_IN(fds, n) (fds->in + n)
  278. #define FDS_OUT(fds, n) (fds->out + n)
  279. #define FDS_EX(fds, n) (fds->ex + n)
  280. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  281. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  282. {
  283. unsigned long *open_fds;
  284. unsigned long set;
  285. int max;
  286. struct fdtable *fdt;
  287. /* handle last in-complete long-word first */
  288. set = ~(~0UL << (n & (__NFDBITS-1)));
  289. n /= __NFDBITS;
  290. fdt = files_fdtable(current->files);
  291. open_fds = fdt->open_fds->fds_bits+n;
  292. max = 0;
  293. if (set) {
  294. set &= BITS(fds, n);
  295. if (set) {
  296. if (!(set & ~*open_fds))
  297. goto get_max;
  298. return -EBADF;
  299. }
  300. }
  301. while (n) {
  302. open_fds--;
  303. n--;
  304. set = BITS(fds, n);
  305. if (!set)
  306. continue;
  307. if (set & ~*open_fds)
  308. return -EBADF;
  309. if (max)
  310. continue;
  311. get_max:
  312. do {
  313. max++;
  314. set >>= 1;
  315. } while (set);
  316. max += n * __NFDBITS;
  317. }
  318. return max;
  319. }
  320. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  321. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  322. #define POLLEX_SET (POLLPRI)
  323. static inline void wait_key_set(poll_table *wait, unsigned long in,
  324. unsigned long out, unsigned long bit)
  325. {
  326. if (wait) {
  327. wait->key = POLLEX_SET;
  328. if (in & bit)
  329. wait->key |= POLLIN_SET;
  330. if (out & bit)
  331. wait->key |= POLLOUT_SET;
  332. }
  333. }
  334. int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  335. {
  336. ktime_t expire, *to = NULL;
  337. struct poll_wqueues table;
  338. poll_table *wait;
  339. int retval, i, timed_out = 0;
  340. unsigned long slack = 0;
  341. rcu_read_lock();
  342. retval = max_select_fd(n, fds);
  343. rcu_read_unlock();
  344. if (retval < 0)
  345. return retval;
  346. n = retval;
  347. poll_initwait(&table);
  348. wait = &table.pt;
  349. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  350. wait = NULL;
  351. timed_out = 1;
  352. }
  353. if (end_time && !timed_out)
  354. slack = estimate_accuracy(end_time);
  355. retval = 0;
  356. for (;;) {
  357. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  358. inp = fds->in; outp = fds->out; exp = fds->ex;
  359. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  360. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  361. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  362. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  363. const struct file_operations *f_op = NULL;
  364. struct file *file = NULL;
  365. in = *inp++; out = *outp++; ex = *exp++;
  366. all_bits = in | out | ex;
  367. if (all_bits == 0) {
  368. i += __NFDBITS;
  369. continue;
  370. }
  371. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  372. int fput_needed;
  373. if (i >= n)
  374. break;
  375. if (!(bit & all_bits))
  376. continue;
  377. file = fget_light(i, &fput_needed);
  378. if (file) {
  379. f_op = file->f_op;
  380. mask = DEFAULT_POLLMASK;
  381. if (f_op && f_op->poll) {
  382. wait_key_set(wait, in, out, bit);
  383. mask = (*f_op->poll)(file, wait);
  384. }
  385. fput_light(file, fput_needed);
  386. if ((mask & POLLIN_SET) && (in & bit)) {
  387. res_in |= bit;
  388. retval++;
  389. wait = NULL;
  390. }
  391. if ((mask & POLLOUT_SET) && (out & bit)) {
  392. res_out |= bit;
  393. retval++;
  394. wait = NULL;
  395. }
  396. if ((mask & POLLEX_SET) && (ex & bit)) {
  397. res_ex |= bit;
  398. retval++;
  399. wait = NULL;
  400. }
  401. }
  402. }
  403. if (res_in)
  404. *rinp = res_in;
  405. if (res_out)
  406. *routp = res_out;
  407. if (res_ex)
  408. *rexp = res_ex;
  409. cond_resched();
  410. }
  411. wait = NULL;
  412. if (retval || timed_out || signal_pending(current))
  413. break;
  414. if (table.error) {
  415. retval = table.error;
  416. break;
  417. }
  418. /*
  419. * If this is the first loop and we have a timeout
  420. * given, then we convert to ktime_t and set the to
  421. * pointer to the expiry value.
  422. */
  423. if (end_time && !to) {
  424. expire = timespec_to_ktime(*end_time);
  425. to = &expire;
  426. }
  427. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  428. to, slack))
  429. timed_out = 1;
  430. }
  431. poll_freewait(&table);
  432. return retval;
  433. }
  434. /*
  435. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  436. * like to be certain this leads to no problems. So I return
  437. * EINTR just for safety.
  438. *
  439. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  440. * I'm trying ERESTARTNOHAND which restart only when you want to.
  441. */
  442. #define MAX_SELECT_SECONDS \
  443. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  444. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  445. fd_set __user *exp, struct timespec *end_time)
  446. {
  447. fd_set_bits fds;
  448. void *bits;
  449. int ret, max_fds;
  450. unsigned int size;
  451. struct fdtable *fdt;
  452. /* Allocate small arguments on the stack to save memory and be faster */
  453. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  454. ret = -EINVAL;
  455. if (n < 0)
  456. goto out_nofds;
  457. /* max_fds can increase, so grab it once to avoid race */
  458. rcu_read_lock();
  459. fdt = files_fdtable(current->files);
  460. max_fds = fdt->max_fds;
  461. rcu_read_unlock();
  462. if (n > max_fds)
  463. n = max_fds;
  464. /*
  465. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  466. * since we used fdset we need to allocate memory in units of
  467. * long-words.
  468. */
  469. size = FDS_BYTES(n);
  470. bits = stack_fds;
  471. if (size > sizeof(stack_fds) / 6) {
  472. /* Not enough space in on-stack array; must use kmalloc */
  473. ret = -ENOMEM;
  474. bits = kmalloc(6 * size, GFP_KERNEL);
  475. if (!bits)
  476. goto out_nofds;
  477. }
  478. fds.in = bits;
  479. fds.out = bits + size;
  480. fds.ex = bits + 2*size;
  481. fds.res_in = bits + 3*size;
  482. fds.res_out = bits + 4*size;
  483. fds.res_ex = bits + 5*size;
  484. if ((ret = get_fd_set(n, inp, fds.in)) ||
  485. (ret = get_fd_set(n, outp, fds.out)) ||
  486. (ret = get_fd_set(n, exp, fds.ex)))
  487. goto out;
  488. zero_fd_set(n, fds.res_in);
  489. zero_fd_set(n, fds.res_out);
  490. zero_fd_set(n, fds.res_ex);
  491. ret = do_select(n, &fds, end_time);
  492. if (ret < 0)
  493. goto out;
  494. if (!ret) {
  495. ret = -ERESTARTNOHAND;
  496. if (signal_pending(current))
  497. goto out;
  498. ret = 0;
  499. }
  500. if (set_fd_set(n, inp, fds.res_in) ||
  501. set_fd_set(n, outp, fds.res_out) ||
  502. set_fd_set(n, exp, fds.res_ex))
  503. ret = -EFAULT;
  504. out:
  505. if (bits != stack_fds)
  506. kfree(bits);
  507. out_nofds:
  508. return ret;
  509. }
  510. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  511. fd_set __user *, exp, struct timeval __user *, tvp)
  512. {
  513. struct timespec end_time, *to = NULL;
  514. struct timeval tv;
  515. int ret;
  516. if (tvp) {
  517. if (copy_from_user(&tv, tvp, sizeof(tv)))
  518. return -EFAULT;
  519. to = &end_time;
  520. if (poll_select_set_timeout(to,
  521. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  522. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  523. return -EINVAL;
  524. }
  525. ret = core_sys_select(n, inp, outp, exp, to);
  526. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  527. return ret;
  528. }
  529. #ifdef HAVE_SET_RESTORE_SIGMASK
  530. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  531. fd_set __user *exp, struct timespec __user *tsp,
  532. const sigset_t __user *sigmask, size_t sigsetsize)
  533. {
  534. sigset_t ksigmask, sigsaved;
  535. struct timespec ts, end_time, *to = NULL;
  536. int ret;
  537. if (tsp) {
  538. if (copy_from_user(&ts, tsp, sizeof(ts)))
  539. return -EFAULT;
  540. to = &end_time;
  541. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  542. return -EINVAL;
  543. }
  544. if (sigmask) {
  545. /* XXX: Don't preclude handling different sized sigset_t's. */
  546. if (sigsetsize != sizeof(sigset_t))
  547. return -EINVAL;
  548. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  549. return -EFAULT;
  550. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  551. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  552. }
  553. ret = core_sys_select(n, inp, outp, exp, to);
  554. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  555. if (ret == -ERESTARTNOHAND) {
  556. /*
  557. * Don't restore the signal mask yet. Let do_signal() deliver
  558. * the signal on the way back to userspace, before the signal
  559. * mask is restored.
  560. */
  561. if (sigmask) {
  562. memcpy(&current->saved_sigmask, &sigsaved,
  563. sizeof(sigsaved));
  564. set_restore_sigmask();
  565. }
  566. } else if (sigmask)
  567. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  568. return ret;
  569. }
  570. /*
  571. * Most architectures can't handle 7-argument syscalls. So we provide a
  572. * 6-argument version where the sixth argument is a pointer to a structure
  573. * which has a pointer to the sigset_t itself followed by a size_t containing
  574. * the sigset size.
  575. */
  576. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  577. fd_set __user *, exp, struct timespec __user *, tsp,
  578. void __user *, sig)
  579. {
  580. size_t sigsetsize = 0;
  581. sigset_t __user *up = NULL;
  582. if (sig) {
  583. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  584. || __get_user(up, (sigset_t __user * __user *)sig)
  585. || __get_user(sigsetsize,
  586. (size_t __user *)(sig+sizeof(void *))))
  587. return -EFAULT;
  588. }
  589. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  590. }
  591. #endif /* HAVE_SET_RESTORE_SIGMASK */
  592. struct poll_list {
  593. struct poll_list *next;
  594. int len;
  595. struct pollfd entries[0];
  596. };
  597. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  598. /*
  599. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  600. * interested in events matching the pollfd->events mask, and the result
  601. * matching that mask is both recorded in pollfd->revents and returned. The
  602. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  603. * if non-NULL.
  604. */
  605. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  606. {
  607. unsigned int mask;
  608. int fd;
  609. mask = 0;
  610. fd = pollfd->fd;
  611. if (fd >= 0) {
  612. int fput_needed;
  613. struct file * file;
  614. file = fget_light(fd, &fput_needed);
  615. mask = POLLNVAL;
  616. if (file != NULL) {
  617. mask = DEFAULT_POLLMASK;
  618. if (file->f_op && file->f_op->poll) {
  619. if (pwait)
  620. pwait->key = pollfd->events |
  621. POLLERR | POLLHUP;
  622. mask = file->f_op->poll(file, pwait);
  623. }
  624. /* Mask out unneeded events. */
  625. mask &= pollfd->events | POLLERR | POLLHUP;
  626. fput_light(file, fput_needed);
  627. }
  628. }
  629. pollfd->revents = mask;
  630. return mask;
  631. }
  632. static int do_poll(unsigned int nfds, struct poll_list *list,
  633. struct poll_wqueues *wait, struct timespec *end_time)
  634. {
  635. poll_table* pt = &wait->pt;
  636. ktime_t expire, *to = NULL;
  637. int timed_out = 0, count = 0;
  638. unsigned long slack = 0;
  639. /* Optimise the no-wait case */
  640. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  641. pt = NULL;
  642. timed_out = 1;
  643. }
  644. if (end_time && !timed_out)
  645. slack = estimate_accuracy(end_time);
  646. for (;;) {
  647. struct poll_list *walk;
  648. for (walk = list; walk != NULL; walk = walk->next) {
  649. struct pollfd * pfd, * pfd_end;
  650. pfd = walk->entries;
  651. pfd_end = pfd + walk->len;
  652. for (; pfd != pfd_end; pfd++) {
  653. /*
  654. * Fish for events. If we found one, record it
  655. * and kill the poll_table, so we don't
  656. * needlessly register any other waiters after
  657. * this. They'll get immediately deregistered
  658. * when we break out and return.
  659. */
  660. if (do_pollfd(pfd, pt)) {
  661. count++;
  662. pt = NULL;
  663. }
  664. }
  665. }
  666. /*
  667. * All waiters have already been registered, so don't provide
  668. * a poll_table to them on the next loop iteration.
  669. */
  670. pt = NULL;
  671. if (!count) {
  672. count = wait->error;
  673. if (signal_pending(current))
  674. count = -EINTR;
  675. }
  676. if (count || timed_out)
  677. break;
  678. /*
  679. * If this is the first loop and we have a timeout
  680. * given, then we convert to ktime_t and set the to
  681. * pointer to the expiry value.
  682. */
  683. if (end_time && !to) {
  684. expire = timespec_to_ktime(*end_time);
  685. to = &expire;
  686. }
  687. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  688. timed_out = 1;
  689. }
  690. return count;
  691. }
  692. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  693. sizeof(struct pollfd))
  694. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  695. struct timespec *end_time)
  696. {
  697. struct poll_wqueues table;
  698. int err = -EFAULT, fdcount, len, size;
  699. /* Allocate small arguments on the stack to save memory and be
  700. faster - use long to make sure the buffer is aligned properly
  701. on 64 bit archs to avoid unaligned access */
  702. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  703. struct poll_list *const head = (struct poll_list *)stack_pps;
  704. struct poll_list *walk = head;
  705. unsigned long todo = nfds;
  706. if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  707. return -EINVAL;
  708. len = min_t(unsigned int, nfds, N_STACK_PPS);
  709. for (;;) {
  710. walk->next = NULL;
  711. walk->len = len;
  712. if (!len)
  713. break;
  714. if (copy_from_user(walk->entries, ufds + nfds-todo,
  715. sizeof(struct pollfd) * walk->len))
  716. goto out_fds;
  717. todo -= walk->len;
  718. if (!todo)
  719. break;
  720. len = min(todo, POLLFD_PER_PAGE);
  721. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  722. walk = walk->next = kmalloc(size, GFP_KERNEL);
  723. if (!walk) {
  724. err = -ENOMEM;
  725. goto out_fds;
  726. }
  727. }
  728. poll_initwait(&table);
  729. fdcount = do_poll(nfds, head, &table, end_time);
  730. poll_freewait(&table);
  731. for (walk = head; walk; walk = walk->next) {
  732. struct pollfd *fds = walk->entries;
  733. int j;
  734. for (j = 0; j < walk->len; j++, ufds++)
  735. if (__put_user(fds[j].revents, &ufds->revents))
  736. goto out_fds;
  737. }
  738. err = fdcount;
  739. out_fds:
  740. walk = head->next;
  741. while (walk) {
  742. struct poll_list *pos = walk;
  743. walk = walk->next;
  744. kfree(pos);
  745. }
  746. return err;
  747. }
  748. static long do_restart_poll(struct restart_block *restart_block)
  749. {
  750. struct pollfd __user *ufds = restart_block->poll.ufds;
  751. int nfds = restart_block->poll.nfds;
  752. struct timespec *to = NULL, end_time;
  753. int ret;
  754. if (restart_block->poll.has_timeout) {
  755. end_time.tv_sec = restart_block->poll.tv_sec;
  756. end_time.tv_nsec = restart_block->poll.tv_nsec;
  757. to = &end_time;
  758. }
  759. ret = do_sys_poll(ufds, nfds, to);
  760. if (ret == -EINTR) {
  761. restart_block->fn = do_restart_poll;
  762. ret = -ERESTART_RESTARTBLOCK;
  763. }
  764. return ret;
  765. }
  766. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  767. long, timeout_msecs)
  768. {
  769. struct timespec end_time, *to = NULL;
  770. int ret;
  771. if (timeout_msecs >= 0) {
  772. to = &end_time;
  773. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  774. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  775. }
  776. ret = do_sys_poll(ufds, nfds, to);
  777. if (ret == -EINTR) {
  778. struct restart_block *restart_block;
  779. restart_block = &current_thread_info()->restart_block;
  780. restart_block->fn = do_restart_poll;
  781. restart_block->poll.ufds = ufds;
  782. restart_block->poll.nfds = nfds;
  783. if (timeout_msecs >= 0) {
  784. restart_block->poll.tv_sec = end_time.tv_sec;
  785. restart_block->poll.tv_nsec = end_time.tv_nsec;
  786. restart_block->poll.has_timeout = 1;
  787. } else
  788. restart_block->poll.has_timeout = 0;
  789. ret = -ERESTART_RESTARTBLOCK;
  790. }
  791. return ret;
  792. }
  793. #ifdef HAVE_SET_RESTORE_SIGMASK
  794. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  795. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  796. size_t, sigsetsize)
  797. {
  798. sigset_t ksigmask, sigsaved;
  799. struct timespec ts, end_time, *to = NULL;
  800. int ret;
  801. if (tsp) {
  802. if (copy_from_user(&ts, tsp, sizeof(ts)))
  803. return -EFAULT;
  804. to = &end_time;
  805. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  806. return -EINVAL;
  807. }
  808. if (sigmask) {
  809. /* XXX: Don't preclude handling different sized sigset_t's. */
  810. if (sigsetsize != sizeof(sigset_t))
  811. return -EINVAL;
  812. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  813. return -EFAULT;
  814. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  815. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  816. }
  817. ret = do_sys_poll(ufds, nfds, to);
  818. /* We can restart this syscall, usually */
  819. if (ret == -EINTR) {
  820. /*
  821. * Don't restore the signal mask yet. Let do_signal() deliver
  822. * the signal on the way back to userspace, before the signal
  823. * mask is restored.
  824. */
  825. if (sigmask) {
  826. memcpy(&current->saved_sigmask, &sigsaved,
  827. sizeof(sigsaved));
  828. set_restore_sigmask();
  829. }
  830. ret = -ERESTARTNOHAND;
  831. } else if (sigmask)
  832. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  833. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  834. return ret;
  835. }
  836. #endif /* HAVE_SET_RESTORE_SIGMASK */