select.c 24 KB

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