select.c 24 KB

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