select.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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/module.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->fds_bits+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. if (wait) {
  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. }
  337. int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
  338. {
  339. ktime_t expire, *to = NULL;
  340. struct poll_wqueues table;
  341. poll_table *wait;
  342. int retval, i, timed_out = 0;
  343. unsigned long slack = 0;
  344. rcu_read_lock();
  345. retval = max_select_fd(n, fds);
  346. rcu_read_unlock();
  347. if (retval < 0)
  348. return retval;
  349. n = retval;
  350. poll_initwait(&table);
  351. wait = &table.pt;
  352. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  353. wait = NULL;
  354. timed_out = 1;
  355. }
  356. if (end_time && !timed_out)
  357. slack = select_estimate_accuracy(end_time);
  358. retval = 0;
  359. for (;;) {
  360. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  361. inp = fds->in; outp = fds->out; exp = fds->ex;
  362. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  363. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  364. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  365. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  366. const struct file_operations *f_op = NULL;
  367. struct file *file = NULL;
  368. in = *inp++; out = *outp++; ex = *exp++;
  369. all_bits = in | out | ex;
  370. if (all_bits == 0) {
  371. i += __NFDBITS;
  372. continue;
  373. }
  374. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  375. int fput_needed;
  376. if (i >= n)
  377. break;
  378. if (!(bit & all_bits))
  379. continue;
  380. file = fget_light(i, &fput_needed);
  381. if (file) {
  382. f_op = file->f_op;
  383. mask = DEFAULT_POLLMASK;
  384. if (f_op && f_op->poll) {
  385. wait_key_set(wait, in, out, bit);
  386. mask = (*f_op->poll)(file, wait);
  387. }
  388. fput_light(file, fput_needed);
  389. if ((mask & POLLIN_SET) && (in & bit)) {
  390. res_in |= bit;
  391. retval++;
  392. wait = NULL;
  393. }
  394. if ((mask & POLLOUT_SET) && (out & bit)) {
  395. res_out |= bit;
  396. retval++;
  397. wait = NULL;
  398. }
  399. if ((mask & POLLEX_SET) && (ex & bit)) {
  400. res_ex |= bit;
  401. retval++;
  402. wait = NULL;
  403. }
  404. }
  405. }
  406. if (res_in)
  407. *rinp = res_in;
  408. if (res_out)
  409. *routp = res_out;
  410. if (res_ex)
  411. *rexp = res_ex;
  412. cond_resched();
  413. }
  414. wait = NULL;
  415. if (retval || timed_out || signal_pending(current))
  416. break;
  417. if (table.error) {
  418. retval = table.error;
  419. break;
  420. }
  421. /*
  422. * If this is the first loop and we have a timeout
  423. * given, then we convert to ktime_t and set the to
  424. * pointer to the expiry value.
  425. */
  426. if (end_time && !to) {
  427. expire = timespec_to_ktime(*end_time);
  428. to = &expire;
  429. }
  430. if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
  431. to, slack))
  432. timed_out = 1;
  433. }
  434. poll_freewait(&table);
  435. return retval;
  436. }
  437. /*
  438. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  439. * like to be certain this leads to no problems. So I return
  440. * EINTR just for safety.
  441. *
  442. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  443. * I'm trying ERESTARTNOHAND which restart only when you want to.
  444. */
  445. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
  446. fd_set __user *exp, struct timespec *end_time)
  447. {
  448. fd_set_bits fds;
  449. void *bits;
  450. int ret, max_fds;
  451. unsigned int size;
  452. struct fdtable *fdt;
  453. /* Allocate small arguments on the stack to save memory and be faster */
  454. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  455. ret = -EINVAL;
  456. if (n < 0)
  457. goto out_nofds;
  458. /* max_fds can increase, so grab it once to avoid race */
  459. rcu_read_lock();
  460. fdt = files_fdtable(current->files);
  461. max_fds = fdt->max_fds;
  462. rcu_read_unlock();
  463. if (n > max_fds)
  464. n = max_fds;
  465. /*
  466. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  467. * since we used fdset we need to allocate memory in units of
  468. * long-words.
  469. */
  470. size = FDS_BYTES(n);
  471. bits = stack_fds;
  472. if (size > sizeof(stack_fds) / 6) {
  473. /* Not enough space in on-stack array; must use kmalloc */
  474. ret = -ENOMEM;
  475. bits = kmalloc(6 * size, GFP_KERNEL);
  476. if (!bits)
  477. goto out_nofds;
  478. }
  479. fds.in = bits;
  480. fds.out = bits + size;
  481. fds.ex = bits + 2*size;
  482. fds.res_in = bits + 3*size;
  483. fds.res_out = bits + 4*size;
  484. fds.res_ex = bits + 5*size;
  485. if ((ret = get_fd_set(n, inp, fds.in)) ||
  486. (ret = get_fd_set(n, outp, fds.out)) ||
  487. (ret = get_fd_set(n, exp, fds.ex)))
  488. goto out;
  489. zero_fd_set(n, fds.res_in);
  490. zero_fd_set(n, fds.res_out);
  491. zero_fd_set(n, fds.res_ex);
  492. ret = do_select(n, &fds, end_time);
  493. if (ret < 0)
  494. goto out;
  495. if (!ret) {
  496. ret = -ERESTARTNOHAND;
  497. if (signal_pending(current))
  498. goto out;
  499. ret = 0;
  500. }
  501. if (set_fd_set(n, inp, fds.res_in) ||
  502. set_fd_set(n, outp, fds.res_out) ||
  503. set_fd_set(n, exp, fds.res_ex))
  504. ret = -EFAULT;
  505. out:
  506. if (bits != stack_fds)
  507. kfree(bits);
  508. out_nofds:
  509. return ret;
  510. }
  511. SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,
  512. fd_set __user *, exp, struct timeval __user *, tvp)
  513. {
  514. struct timespec end_time, *to = NULL;
  515. struct timeval tv;
  516. int ret;
  517. if (tvp) {
  518. if (copy_from_user(&tv, tvp, sizeof(tv)))
  519. return -EFAULT;
  520. to = &end_time;
  521. if (poll_select_set_timeout(to,
  522. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  523. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  524. return -EINVAL;
  525. }
  526. ret = core_sys_select(n, inp, outp, exp, to);
  527. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  528. return ret;
  529. }
  530. #ifdef HAVE_SET_RESTORE_SIGMASK
  531. static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
  532. fd_set __user *exp, struct timespec __user *tsp,
  533. const sigset_t __user *sigmask, size_t sigsetsize)
  534. {
  535. sigset_t ksigmask, sigsaved;
  536. struct timespec ts, end_time, *to = NULL;
  537. int ret;
  538. if (tsp) {
  539. if (copy_from_user(&ts, tsp, sizeof(ts)))
  540. return -EFAULT;
  541. to = &end_time;
  542. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  543. return -EINVAL;
  544. }
  545. if (sigmask) {
  546. /* XXX: Don't preclude handling different sized sigset_t's. */
  547. if (sigsetsize != sizeof(sigset_t))
  548. return -EINVAL;
  549. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  550. return -EFAULT;
  551. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  552. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  553. }
  554. ret = core_sys_select(n, inp, outp, exp, to);
  555. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  556. if (ret == -ERESTARTNOHAND) {
  557. /*
  558. * Don't restore the signal mask yet. Let do_signal() deliver
  559. * the signal on the way back to userspace, before the signal
  560. * mask is restored.
  561. */
  562. if (sigmask) {
  563. memcpy(&current->saved_sigmask, &sigsaved,
  564. sizeof(sigsaved));
  565. set_restore_sigmask();
  566. }
  567. } else if (sigmask)
  568. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  569. return ret;
  570. }
  571. /*
  572. * Most architectures can't handle 7-argument syscalls. So we provide a
  573. * 6-argument version where the sixth argument is a pointer to a structure
  574. * which has a pointer to the sigset_t itself followed by a size_t containing
  575. * the sigset size.
  576. */
  577. SYSCALL_DEFINE6(pselect6, int, n, fd_set __user *, inp, fd_set __user *, outp,
  578. fd_set __user *, exp, struct timespec __user *, tsp,
  579. void __user *, sig)
  580. {
  581. size_t sigsetsize = 0;
  582. sigset_t __user *up = NULL;
  583. if (sig) {
  584. if (!access_ok(VERIFY_READ, sig, sizeof(void *)+sizeof(size_t))
  585. || __get_user(up, (sigset_t __user * __user *)sig)
  586. || __get_user(sigsetsize,
  587. (size_t __user *)(sig+sizeof(void *))))
  588. return -EFAULT;
  589. }
  590. return do_pselect(n, inp, outp, exp, tsp, up, sigsetsize);
  591. }
  592. #endif /* HAVE_SET_RESTORE_SIGMASK */
  593. #ifdef __ARCH_WANT_SYS_OLD_SELECT
  594. struct sel_arg_struct {
  595. unsigned long n;
  596. fd_set __user *inp, *outp, *exp;
  597. struct timeval __user *tvp;
  598. };
  599. SYSCALL_DEFINE1(old_select, struct sel_arg_struct __user *, arg)
  600. {
  601. struct sel_arg_struct a;
  602. if (copy_from_user(&a, arg, sizeof(a)))
  603. return -EFAULT;
  604. return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
  605. }
  606. #endif
  607. struct poll_list {
  608. struct poll_list *next;
  609. int len;
  610. struct pollfd entries[0];
  611. };
  612. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  613. /*
  614. * Fish for pollable events on the pollfd->fd file descriptor. We're only
  615. * interested in events matching the pollfd->events mask, and the result
  616. * matching that mask is both recorded in pollfd->revents and returned. The
  617. * pwait poll_table will be used by the fd-provided poll handler for waiting,
  618. * if non-NULL.
  619. */
  620. static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
  621. {
  622. unsigned int mask;
  623. int fd;
  624. mask = 0;
  625. fd = pollfd->fd;
  626. if (fd >= 0) {
  627. int fput_needed;
  628. struct file * file;
  629. file = fget_light(fd, &fput_needed);
  630. mask = POLLNVAL;
  631. if (file != NULL) {
  632. mask = DEFAULT_POLLMASK;
  633. if (file->f_op && file->f_op->poll) {
  634. if (pwait)
  635. pwait->key = pollfd->events |
  636. POLLERR | POLLHUP;
  637. mask = file->f_op->poll(file, pwait);
  638. }
  639. /* Mask out unneeded events. */
  640. mask &= pollfd->events | POLLERR | POLLHUP;
  641. fput_light(file, fput_needed);
  642. }
  643. }
  644. pollfd->revents = mask;
  645. return mask;
  646. }
  647. static int do_poll(unsigned int nfds, struct poll_list *list,
  648. struct poll_wqueues *wait, struct timespec *end_time)
  649. {
  650. poll_table* pt = &wait->pt;
  651. ktime_t expire, *to = NULL;
  652. int timed_out = 0, count = 0;
  653. unsigned long slack = 0;
  654. /* Optimise the no-wait case */
  655. if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
  656. pt = NULL;
  657. timed_out = 1;
  658. }
  659. if (end_time && !timed_out)
  660. slack = select_estimate_accuracy(end_time);
  661. for (;;) {
  662. struct poll_list *walk;
  663. for (walk = list; walk != NULL; walk = walk->next) {
  664. struct pollfd * pfd, * pfd_end;
  665. pfd = walk->entries;
  666. pfd_end = pfd + walk->len;
  667. for (; pfd != pfd_end; pfd++) {
  668. /*
  669. * Fish for events. If we found one, record it
  670. * and kill the poll_table, so we don't
  671. * needlessly register any other waiters after
  672. * this. They'll get immediately deregistered
  673. * when we break out and return.
  674. */
  675. if (do_pollfd(pfd, pt)) {
  676. count++;
  677. pt = NULL;
  678. }
  679. }
  680. }
  681. /*
  682. * All waiters have already been registered, so don't provide
  683. * a poll_table to them on the next loop iteration.
  684. */
  685. pt = NULL;
  686. if (!count) {
  687. count = wait->error;
  688. if (signal_pending(current))
  689. count = -EINTR;
  690. }
  691. if (count || timed_out)
  692. break;
  693. /*
  694. * If this is the first loop and we have a timeout
  695. * given, then we convert to ktime_t and set the to
  696. * pointer to the expiry value.
  697. */
  698. if (end_time && !to) {
  699. expire = timespec_to_ktime(*end_time);
  700. to = &expire;
  701. }
  702. if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
  703. timed_out = 1;
  704. }
  705. return count;
  706. }
  707. #define N_STACK_PPS ((sizeof(stack_pps) - sizeof(struct poll_list)) / \
  708. sizeof(struct pollfd))
  709. int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  710. struct timespec *end_time)
  711. {
  712. struct poll_wqueues table;
  713. int err = -EFAULT, fdcount, len, size;
  714. /* Allocate small arguments on the stack to save memory and be
  715. faster - use long to make sure the buffer is aligned properly
  716. on 64 bit archs to avoid unaligned access */
  717. long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
  718. struct poll_list *const head = (struct poll_list *)stack_pps;
  719. struct poll_list *walk = head;
  720. unsigned long todo = nfds;
  721. if (nfds > rlimit(RLIMIT_NOFILE))
  722. return -EINVAL;
  723. len = min_t(unsigned int, nfds, N_STACK_PPS);
  724. for (;;) {
  725. walk->next = NULL;
  726. walk->len = len;
  727. if (!len)
  728. break;
  729. if (copy_from_user(walk->entries, ufds + nfds-todo,
  730. sizeof(struct pollfd) * walk->len))
  731. goto out_fds;
  732. todo -= walk->len;
  733. if (!todo)
  734. break;
  735. len = min(todo, POLLFD_PER_PAGE);
  736. size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
  737. walk = walk->next = kmalloc(size, GFP_KERNEL);
  738. if (!walk) {
  739. err = -ENOMEM;
  740. goto out_fds;
  741. }
  742. }
  743. poll_initwait(&table);
  744. fdcount = do_poll(nfds, head, &table, end_time);
  745. poll_freewait(&table);
  746. for (walk = head; walk; walk = walk->next) {
  747. struct pollfd *fds = walk->entries;
  748. int j;
  749. for (j = 0; j < walk->len; j++, ufds++)
  750. if (__put_user(fds[j].revents, &ufds->revents))
  751. goto out_fds;
  752. }
  753. err = fdcount;
  754. out_fds:
  755. walk = head->next;
  756. while (walk) {
  757. struct poll_list *pos = walk;
  758. walk = walk->next;
  759. kfree(pos);
  760. }
  761. return err;
  762. }
  763. static long do_restart_poll(struct restart_block *restart_block)
  764. {
  765. struct pollfd __user *ufds = restart_block->poll.ufds;
  766. int nfds = restart_block->poll.nfds;
  767. struct timespec *to = NULL, end_time;
  768. int ret;
  769. if (restart_block->poll.has_timeout) {
  770. end_time.tv_sec = restart_block->poll.tv_sec;
  771. end_time.tv_nsec = restart_block->poll.tv_nsec;
  772. to = &end_time;
  773. }
  774. ret = do_sys_poll(ufds, nfds, to);
  775. if (ret == -EINTR) {
  776. restart_block->fn = do_restart_poll;
  777. ret = -ERESTART_RESTARTBLOCK;
  778. }
  779. return ret;
  780. }
  781. SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
  782. long, timeout_msecs)
  783. {
  784. struct timespec end_time, *to = NULL;
  785. int ret;
  786. if (timeout_msecs >= 0) {
  787. to = &end_time;
  788. poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC,
  789. NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));
  790. }
  791. ret = do_sys_poll(ufds, nfds, to);
  792. if (ret == -EINTR) {
  793. struct restart_block *restart_block;
  794. restart_block = &current_thread_info()->restart_block;
  795. restart_block->fn = do_restart_poll;
  796. restart_block->poll.ufds = ufds;
  797. restart_block->poll.nfds = nfds;
  798. if (timeout_msecs >= 0) {
  799. restart_block->poll.tv_sec = end_time.tv_sec;
  800. restart_block->poll.tv_nsec = end_time.tv_nsec;
  801. restart_block->poll.has_timeout = 1;
  802. } else
  803. restart_block->poll.has_timeout = 0;
  804. ret = -ERESTART_RESTARTBLOCK;
  805. }
  806. return ret;
  807. }
  808. #ifdef HAVE_SET_RESTORE_SIGMASK
  809. SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
  810. struct timespec __user *, tsp, const sigset_t __user *, sigmask,
  811. size_t, sigsetsize)
  812. {
  813. sigset_t ksigmask, sigsaved;
  814. struct timespec ts, end_time, *to = NULL;
  815. int ret;
  816. if (tsp) {
  817. if (copy_from_user(&ts, tsp, sizeof(ts)))
  818. return -EFAULT;
  819. to = &end_time;
  820. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  821. return -EINVAL;
  822. }
  823. if (sigmask) {
  824. /* XXX: Don't preclude handling different sized sigset_t's. */
  825. if (sigsetsize != sizeof(sigset_t))
  826. return -EINVAL;
  827. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  828. return -EFAULT;
  829. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  830. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  831. }
  832. ret = do_sys_poll(ufds, nfds, to);
  833. /* We can restart this syscall, usually */
  834. if (ret == -EINTR) {
  835. /*
  836. * Don't restore the signal mask yet. Let do_signal() deliver
  837. * the signal on the way back to userspace, before the signal
  838. * mask is restored.
  839. */
  840. if (sigmask) {
  841. memcpy(&current->saved_sigmask, &sigsaved,
  842. sizeof(sigsaved));
  843. set_restore_sigmask();
  844. }
  845. ret = -ERESTARTNOHAND;
  846. } else if (sigmask)
  847. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  848. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  849. return ret;
  850. }
  851. #endif /* HAVE_SET_RESTORE_SIGMASK */