select.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/sched.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/export.h>
  20. #include <linux/slab.h>
  21. #include <linux/poll.h>
  22. #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  23. #include <linux/file.h>
  24. #include <linux/fdtable.h>
  25. #include <linux/fs.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/hrtimer.h>
  28. #include <asm/uaccess.h>
  29. /*
  30. * Estimate expected accuracy in ns from a timeval.
  31. *
  32. * After quite a bit of churning around, we've settled on
  33. * a simple thing of taking 0.1% of the timeout as the
  34. * slack, with a cap of 100 msec.
  35. * "nice" tasks get a 0.5% slack instead.
  36. *
  37. * Consider this comment an open invitation to come up with even
  38. * better solutions..
  39. */
  40. #define MAX_SLACK (100 * NSEC_PER_MSEC)
  41. static long __estimate_accuracy(struct timespec *tv)
  42. {
  43. long slack;
  44. int divfactor = 1000;
  45. if (tv->tv_sec < 0)
  46. return 0;
  47. if (task_nice(current) > 0)
  48. divfactor = divfactor / 5;
  49. if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
  50. return MAX_SLACK;
  51. slack = tv->tv_nsec / divfactor;
  52. slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
  53. if (slack > MAX_SLACK)
  54. return MAX_SLACK;
  55. return slack;
  56. }
  57. long select_estimate_accuracy(struct timespec *tv)
  58. {
  59. unsigned long ret;
  60. struct timespec now;
  61. /*
  62. * Realtime tasks get a slack of 0 for obvious reasons.
  63. */
  64. if (rt_task(current))
  65. return 0;
  66. ktime_get_ts(&now);
  67. now = timespec_sub(*tv, now);
  68. ret = __estimate_accuracy(&now);
  69. if (ret < current->timer_slack_ns)
  70. return current->timer_slack_ns;
  71. return ret;
  72. }
  73. struct poll_table_page {
  74. struct poll_table_page * next;
  75. struct poll_table_entry * entry;
  76. struct poll_table_entry entries[0];
  77. };
  78. #define POLL_TABLE_FULL(table) \
  79. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  80. /*
  81. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  82. * I have rewritten this, taking some shortcuts: This code may not be easy to
  83. * follow, but it should be free of race-conditions, and it's practical. If you
  84. * understand what I'm doing here, then you understand how the linux
  85. * sleep/wakeup mechanism works.
  86. *
  87. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  88. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  89. * as all select/poll functions have to call it to add an entry to the
  90. * poll table.
  91. */
  92. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  93. poll_table *p);
  94. void poll_initwait(struct poll_wqueues *pwq)
  95. {
  96. init_poll_funcptr(&pwq->pt, __pollwait);
  97. pwq->polling_task = current;
  98. pwq->triggered = 0;
  99. pwq->error = 0;
  100. pwq->table = NULL;
  101. pwq->inline_index = 0;
  102. }
  103. EXPORT_SYMBOL(poll_initwait);
  104. static void free_poll_entry(struct poll_table_entry *entry)
  105. {
  106. remove_wait_queue(entry->wait_address, &entry->wait);
  107. fput(entry->filp);
  108. }
  109. void poll_freewait(struct poll_wqueues *pwq)
  110. {
  111. struct poll_table_page * p = pwq->table;
  112. int i;
  113. for (i = 0; i < pwq->inline_index; i++)
  114. free_poll_entry(pwq->inline_entries + i);
  115. while (p) {
  116. struct poll_table_entry * entry;
  117. struct poll_table_page *old;
  118. entry = p->entry;
  119. do {
  120. entry--;
  121. free_poll_entry(entry);
  122. } while (entry > p->entries);
  123. old = p;
  124. p = p->next;
  125. free_page((unsigned long) old);
  126. }
  127. }
  128. EXPORT_SYMBOL(poll_freewait);
  129. static struct poll_table_entry *poll_get_entry(struct poll_wqueues *p)
  130. {
  131. struct poll_table_page *table = p->table;
  132. if (p->inline_index < N_INLINE_POLL_ENTRIES)
  133. return p->inline_entries + p->inline_index++;
  134. if (!table || POLL_TABLE_FULL(table)) {
  135. struct poll_table_page *new_table;
  136. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  137. if (!new_table) {
  138. p->error = -ENOMEM;
  139. return NULL;
  140. }
  141. new_table->entry = new_table->entries;
  142. new_table->next = table;
  143. p->table = new_table;
  144. table = new_table;
  145. }
  146. return table->entry++;
  147. }
  148. static int __pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
  149. {
  150. struct poll_wqueues *pwq = wait->private;
  151. DECLARE_WAITQUEUE(dummy_wait, pwq->polling_task);
  152. /*
  153. * Although this function is called under waitqueue lock, LOCK
  154. * doesn't imply write barrier and the users expect write
  155. * barrier semantics on wakeup functions. The following
  156. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  157. * and is paired with set_mb() in poll_schedule_timeout.
  158. */
  159. smp_wmb();
  160. pwq->triggered = 1;
  161. /*
  162. * Perform the default wake up operation using a dummy
  163. * waitqueue.
  164. *
  165. * TODO: This is hacky but there currently is no interface to
  166. * pass in @sync. @sync is scheduled to be removed and once
  167. * that happens, wake_up_process() can be used directly.
  168. */
  169. return default_wake_function(&dummy_wait, mode, sync, key);
  170. }
  171. static int pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
  172. {
  173. struct poll_table_entry *entry;
  174. entry = container_of(wait, struct poll_table_entry, wait);
  175. if (key && !((unsigned long)key & entry->key))
  176. return 0;
  177. return __pollwake(wait, mode, sync, key);
  178. }
  179. /* Add a new entry */
  180. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  181. poll_table *p)
  182. {
  183. struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);
  184. struct poll_table_entry *entry = poll_get_entry(pwq);
  185. if (!entry)
  186. return;
  187. entry->filp = get_file(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. if (sizeof(rtv) > sizeof(rtv.tv_sec) + sizeof(rtv.tv_usec))
  260. memset(&rtv, 0, sizeof(rtv));
  261. rtv.tv_sec = rts.tv_sec;
  262. rtv.tv_usec = rts.tv_nsec / NSEC_PER_USEC;
  263. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  264. return ret;
  265. } else if (!copy_to_user(p, &rts, sizeof(rts)))
  266. return ret;
  267. /*
  268. * If an application puts its timeval in read-only memory, we
  269. * don't want the Linux-specific update to the timeval to
  270. * cause a fault after the select has completed
  271. * successfully. However, because we're not updating the
  272. * timeval, we can't restart the system call.
  273. */
  274. sticky:
  275. if (ret == -ERESTARTNOHAND)
  276. ret = -EINTR;
  277. return ret;
  278. }
  279. #define FDS_IN(fds, n) (fds->in + n)
  280. #define FDS_OUT(fds, n) (fds->out + n)
  281. #define FDS_EX(fds, n) (fds->ex + n)
  282. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  283. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  284. {
  285. unsigned long *open_fds;
  286. unsigned long set;
  287. int max;
  288. struct fdtable *fdt;
  289. /* handle last in-complete long-word first */
  290. set = ~(~0UL << (n & (BITS_PER_LONG-1)));
  291. n /= BITS_PER_LONG;
  292. fdt = files_fdtable(current->files);
  293. open_fds = fdt->open_fds + n;
  294. max = 0;
  295. if (set) {
  296. set &= BITS(fds, n);
  297. if (set) {
  298. if (!(set & ~*open_fds))
  299. goto get_max;
  300. return -EBADF;
  301. }
  302. }
  303. while (n) {
  304. open_fds--;
  305. n--;
  306. set = BITS(fds, n);
  307. if (!set)
  308. continue;
  309. if (set & ~*open_fds)
  310. return -EBADF;
  311. if (max)
  312. continue;
  313. get_max:
  314. do {
  315. max++;
  316. set >>= 1;
  317. } while (set);
  318. max += n * BITS_PER_LONG;
  319. }
  320. return max;
  321. }
  322. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  323. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  324. #define POLLEX_SET (POLLPRI)
  325. static inline void wait_key_set(poll_table *wait, unsigned long in,
  326. unsigned long out, unsigned long bit)
  327. {
  328. wait->_key = POLLEX_SET;
  329. if (in & bit)
  330. wait->_key |= POLLIN_SET;
  331. if (out & bit)
  332. wait->_key |= POLLOUT_SET;
  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->_qproc = NULL;
  351. timed_out = 1;
  352. }
  353. if (end_time && !timed_out)
  354. slack = select_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. in = *inp++; out = *outp++; ex = *exp++;
  364. all_bits = in | out | ex;
  365. if (all_bits == 0) {
  366. i += BITS_PER_LONG;
  367. continue;
  368. }
  369. for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
  370. struct fd f;
  371. if (i >= n)
  372. break;
  373. if (!(bit & all_bits))
  374. continue;
  375. f = fdget(i);
  376. if (f.file) {
  377. const struct file_operations *f_op;
  378. f_op = f.file->f_op;
  379. mask = DEFAULT_POLLMASK;
  380. if (f_op && f_op->poll) {
  381. wait_key_set(wait, in, out, bit);
  382. mask = (*f_op->poll)(f.file, wait);
  383. }
  384. fdput(f);
  385. if ((mask & POLLIN_SET) && (in & bit)) {
  386. res_in |= bit;
  387. retval++;
  388. wait->_qproc = NULL;
  389. }
  390. if ((mask & POLLOUT_SET) && (out & bit)) {
  391. res_out |= bit;
  392. retval++;
  393. wait->_qproc = NULL;
  394. }
  395. if ((mask & POLLEX_SET) && (ex & bit)) {
  396. res_ex |= bit;
  397. retval++;
  398. wait->_qproc = NULL;
  399. }
  400. }
  401. }
  402. if (res_in)
  403. *rinp = res_in;
  404. if (res_out)
  405. *routp = res_out;
  406. if (res_ex)
  407. *rexp = res_ex;
  408. cond_resched();
  409. }
  410. wait->_qproc = NULL;
  411. if (retval || timed_out || signal_pending(current))
  412. break;
  413. if (table.error) {
  414. retval = table.error;
  415. break;
  416. }
  417. /*
  418. * If this is the first loop and we have a timeout
  419. * given, then we convert to ktime_t and set the to
  420. * pointer to the expiry value.
  421. */
  422. if (end_time && !to) {
  423. expire = timespec_to_ktime(*end_time);
  424. to = &expire;
  425. }
  426. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  427. to, slack))
  428. timed_out = 1;
  429. }
  430. poll_freewait(&table);
  431. return retval;
  432. }
  433. /*
  434. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  435. * like to be certain this leads to no problems. So I return
  436. * EINTR just for safety.
  437. *
  438. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  439. * I'm trying ERESTARTNOHAND which restart only when you want to.
  440. */
  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. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  527. fd_set __user *exp, struct timespec __user *tsp,
  528. const sigset_t __user *sigmask, size_t sigsetsize)
  529. {
  530. sigset_t ksigmask, sigsaved;
  531. struct timespec ts, end_time, *to = NULL;
  532. int ret;
  533. if (tsp) {
  534. if (copy_from_user(&ts, tsp, sizeof(ts)))
  535. return -EFAULT;
  536. to = &end_time;
  537. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  538. return -EINVAL;
  539. }
  540. if (sigmask) {
  541. /* XXX: Don't preclude handling different sized sigset_t's. */
  542. if (sigsetsize != sizeof(sigset_t))
  543. return -EINVAL;
  544. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  545. return -EFAULT;
  546. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  547. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  548. }
  549. ret = core_sys_select(n, inp, outp, exp, to);
  550. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  551. if (ret == -ERESTARTNOHAND) {
  552. /*
  553. * Don't restore the signal mask yet. Let do_signal() deliver
  554. * the signal on the way back to userspace, before the signal
  555. * mask is restored.
  556. */
  557. if (sigmask) {
  558. memcpy(&current->saved_sigmask, &sigsaved,
  559. sizeof(sigsaved));
  560. set_restore_sigmask();
  561. }
  562. } else if (sigmask)
  563. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  564. return ret;
  565. }
  566. /*
  567. * Most architectures can't handle 7-argument syscalls. So we provide a
  568. * 6-argument version where the sixth argument is a pointer to a structure
  569. * which has a pointer to the sigset_t itself followed by a size_t containing
  570. * the sigset size.
  571. */
  572. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  573. fd_set __user *, exp, struct timespec __user *, tsp,
  574. void __user *, sig)
  575. {
  576. size_t sigsetsize = 0;
  577. sigset_t __user *up = NULL;
  578. if (sig) {
  579. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  580. || __get_user(up, (sigset_t __user * __user *)sig)
  581. || __get_user(sigsetsize,
  582. (size_t __user *)(sig+sizeof(void *))))
  583. return -EFAULT;
  584. }
  585. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  586. }
  587. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  588. struct sel_arg_struct {
  589. unsigned long n;
  590. fd_set __user *inp, *outp, *exp;
  591. struct timeval __user *tvp;
  592. };
  593. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  594. {
  595. struct sel_arg_struct a;
  596. if (copy_from_user(&a, arg, sizeof(a)))
  597. return -EFAULT;
  598. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  599. }
  600. #endif
  601. struct poll_list {
  602. struct poll_list *next;
  603. int len;
  604. struct pollfd entries[0];
  605. };
  606. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  607. /*
  608. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  609. * interested in events matching the pollfd->events mask, and the result
  610. * matching that mask is both recorded in pollfd->revents and returned. The
  611. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  612. * if pwait->_qproc is non-NULL.
  613. */
  614. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  615. {
  616. unsigned int mask;
  617. int fd;
  618. mask = 0;
  619. fd = pollfd->fd;
  620. if (fd >= 0) {
  621. struct fd f = fdget(fd);
  622. mask = POLLNVAL;
  623. if (f.file) {
  624. mask = DEFAULT_POLLMASK;
  625. if (f.file->f_op && f.file->f_op->poll) {
  626. pwait->_key = pollfd->events|POLLERR|POLLHUP;
  627. mask = f.file->f_op->poll(f.file, pwait);
  628. }
  629. /* Mask out unneeded events. */
  630. mask &= pollfd->events | POLLERR | POLLHUP;
  631. fdput(f);
  632. }
  633. }
  634. pollfd->revents = mask;
  635. return mask;
  636. }
  637. static int do_poll(unsigned int nfds, struct poll_list *list,
  638. struct poll_wqueues *wait, struct timespec *end_time)
  639. {
  640. poll_table* pt = &wait->pt;
  641. ktime_t expire, *to = NULL;
  642. int timed_out = 0, count = 0;
  643. unsigned long slack = 0;
  644. /* Optimise the no-wait case */
  645. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  646. pt->_qproc = NULL;
  647. timed_out = 1;
  648. }
  649. if (end_time && !timed_out)
  650. slack = select_estimate_accuracy(end_time);
  651. for (;;) {
  652. struct poll_list *walk;
  653. for (walk = list; walk != NULL; walk = walk->next) {
  654. struct pollfd * pfd, * pfd_end;
  655. pfd = walk->entries;
  656. pfd_end = pfd + walk->len;
  657. for (; pfd != pfd_end; pfd++) {
  658. /*
  659. * Fish for events. If we found one, record it
  660. * and kill poll_table->_qproc, so we don't
  661. * needlessly register any other waiters after
  662. * this. They'll get immediately deregistered
  663. * when we break out and return.
  664. */
  665. if (do_pollfd(pfd, pt)) {
  666. count++;
  667. pt->_qproc = NULL;
  668. }
  669. }
  670. }
  671. /*
  672. * All waiters have already been registered, so don't provide
  673. * a poll_table->_qproc to them on the next loop iteration.
  674. */
  675. pt->_qproc = NULL;
  676. if (!count) {
  677. count = wait->error;
  678. if (signal_pending(current))
  679. count = -EINTR;
  680. }
  681. if (count || timed_out)
  682. break;
  683. /*
  684. * If this is the first loop and we have a timeout
  685. * given, then we convert to ktime_t and set the to
  686. * pointer to the expiry value.
  687. */
  688. if (end_time && !to) {
  689. expire = timespec_to_ktime(*end_time);
  690. to = &expire;
  691. }
  692. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  693. timed_out = 1;
  694. }
  695. return count;
  696. }
  697. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  698. sizeof(struct pollfd))
  699. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  700. struct timespec *end_time)
  701. {
  702. struct poll_wqueues table;
  703. int err = -EFAULT, fdcount, len, size;
  704. /* Allocate small arguments on the stack to save memory and be
  705. faster - use long to make sure the buffer is aligned properly
  706. on 64 bit archs to avoid unaligned access */
  707. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  708. struct poll_list *const head = (struct poll_list *)stack_pps;
  709. struct poll_list *walk = head;
  710. unsigned long todo = nfds;
  711. if (nfds > rlimit(RLIMIT_NOFILE))
  712. return -EINVAL;
  713. len = min_t(unsigned int, nfds, N_STACK_PPS);
  714. for (;;) {
  715. walk->next = NULL;
  716. walk->len = len;
  717. if (!len)
  718. break;
  719. if (copy_from_user(walk->entries, ufds + nfds-todo,
  720. sizeof(struct pollfd) * walk->len))
  721. goto out_fds;
  722. todo -= walk->len;
  723. if (!todo)
  724. break;
  725. len = min(todo, POLLFD_PER_PAGE);
  726. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  727. walk = walk->next = kmalloc(size, GFP_KERNEL);
  728. if (!walk) {
  729. err = -ENOMEM;
  730. goto out_fds;
  731. }
  732. }
  733. poll_initwait(&table);
  734. fdcount = do_poll(nfds, head, &table, end_time);
  735. poll_freewait(&table);
  736. for (walk = head; walk; walk = walk->next) {
  737. struct pollfd *fds = walk->entries;
  738. int j;
  739. for (j = 0; j < walk->len; j++, ufds++)
  740. if (__put_user(fds[j].revents, &ufds->revents))
  741. goto out_fds;
  742. }
  743. err = fdcount;
  744. out_fds:
  745. walk = head->next;
  746. while (walk) {
  747. struct poll_list *pos = walk;
  748. walk = walk->next;
  749. kfree(pos);
  750. }
  751. return err;
  752. }
  753. static long do_restart_poll(struct restart_block *restart_block)
  754. {
  755. struct pollfd __user *ufds = restart_block->poll.ufds;
  756. int nfds = restart_block->poll.nfds;
  757. struct timespec *to = NULL, end_time;
  758. int ret;
  759. if (restart_block->poll.has_timeout) {
  760. end_time.tv_sec = restart_block->poll.tv_sec;
  761. end_time.tv_nsec = restart_block->poll.tv_nsec;
  762. to = &end_time;
  763. }
  764. ret = do_sys_poll(ufds, nfds, to);
  765. if (ret == -EINTR) {
  766. restart_block->fn = do_restart_poll;
  767. ret = -ERESTART_RESTARTBLOCK;
  768. }
  769. return ret;
  770. }
  771. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  772. int, timeout_msecs)
  773. {
  774. struct timespec end_time, *to = NULL;
  775. int ret;
  776. if (timeout_msecs >= 0) {
  777. to = &end_time;
  778. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  779. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  780. }
  781. ret = do_sys_poll(ufds, nfds, to);
  782. if (ret == -EINTR) {
  783. struct restart_block *restart_block;
  784. restart_block = &current_thread_info()->restart_block;
  785. restart_block->fn = do_restart_poll;
  786. restart_block->poll.ufds = ufds;
  787. restart_block->poll.nfds = nfds;
  788. if (timeout_msecs >= 0) {
  789. restart_block->poll.tv_sec = end_time.tv_sec;
  790. restart_block->poll.tv_nsec = end_time.tv_nsec;
  791. restart_block->poll.has_timeout = 1;
  792. } else
  793. restart_block->poll.has_timeout = 0;
  794. ret = -ERESTART_RESTARTBLOCK;
  795. }
  796. return ret;
  797. }
  798. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  799. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  800. size_t, sigsetsize)
  801. {
  802. sigset_t ksigmask, sigsaved;
  803. struct timespec ts, end_time, *to = NULL;
  804. int ret;
  805. if (tsp) {
  806. if (copy_from_user(&ts, tsp, sizeof(ts)))
  807. return -EFAULT;
  808. to = &end_time;
  809. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  810. return -EINVAL;
  811. }
  812. if (sigmask) {
  813. /* XXX: Don't preclude handling different sized sigset_t's. */
  814. if (sigsetsize != sizeof(sigset_t))
  815. return -EINVAL;
  816. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  817. return -EFAULT;
  818. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  819. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  820. }
  821. ret = do_sys_poll(ufds, nfds, to);
  822. /* We can restart this syscall, usually */
  823. if (ret == -EINTR) {
  824. /*
  825. * Don't restore the signal mask yet. Let do_signal() deliver
  826. * the signal on the way back to userspace, before the signal
  827. * mask is restored.
  828. */
  829. if (sigmask) {
  830. memcpy(&current->saved_sigmask, &sigsaved,
  831. sizeof(sigsaved));
  832. set_restore_sigmask();
  833. }
  834. ret = -ERESTARTNOHAND;
  835. } else if (sigmask)
  836. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  837. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  838. return ret;
  839. }