select.c 24 KB

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