select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/syscalls.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/poll.h>
  21. #include <linux/personality.h> /* for STICKY_TIMEOUTS */
  22. #include <linux/file.h>
  23. #include <linux/fs.h>
  24. #include <linux/rcupdate.h>
  25. #include <asm/uaccess.h>
  26. #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  27. #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
  28. struct poll_table_entry {
  29. struct file * filp;
  30. wait_queue_t wait;
  31. wait_queue_head_t * wait_address;
  32. };
  33. struct poll_table_page {
  34. struct poll_table_page * next;
  35. struct poll_table_entry * entry;
  36. struct poll_table_entry entries[0];
  37. };
  38. #define POLL_TABLE_FULL(table) \
  39. ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
  40. /*
  41. * Ok, Peter made a complicated, but straightforward multiple_wait() function.
  42. * I have rewritten this, taking some shortcuts: This code may not be easy to
  43. * follow, but it should be free of race-conditions, and it's practical. If you
  44. * understand what I'm doing here, then you understand how the linux
  45. * sleep/wakeup mechanism works.
  46. *
  47. * Two very simple procedures, poll_wait() and poll_freewait() make all the
  48. * work. poll_wait() is an inline-function defined in <linux/poll.h>,
  49. * as all select/poll functions have to call it to add an entry to the
  50. * poll table.
  51. */
  52. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  53. poll_table *p);
  54. void poll_initwait(struct poll_wqueues *pwq)
  55. {
  56. init_poll_funcptr(&pwq->pt, __pollwait);
  57. pwq->error = 0;
  58. pwq->table = NULL;
  59. }
  60. EXPORT_SYMBOL(poll_initwait);
  61. void poll_freewait(struct poll_wqueues *pwq)
  62. {
  63. struct poll_table_page * p = pwq->table;
  64. while (p) {
  65. struct poll_table_entry * entry;
  66. struct poll_table_page *old;
  67. entry = p->entry;
  68. do {
  69. entry--;
  70. remove_wait_queue(entry->wait_address,&entry->wait);
  71. fput(entry->filp);
  72. } while (entry > p->entries);
  73. old = p;
  74. p = p->next;
  75. free_page((unsigned long) old);
  76. }
  77. }
  78. EXPORT_SYMBOL(poll_freewait);
  79. static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
  80. poll_table *_p)
  81. {
  82. struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
  83. struct poll_table_page *table = p->table;
  84. if (!table || POLL_TABLE_FULL(table)) {
  85. struct poll_table_page *new_table;
  86. new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
  87. if (!new_table) {
  88. p->error = -ENOMEM;
  89. __set_current_state(TASK_RUNNING);
  90. return;
  91. }
  92. new_table->entry = new_table->entries;
  93. new_table->next = table;
  94. p->table = new_table;
  95. table = new_table;
  96. }
  97. /* Add a new entry */
  98. {
  99. struct poll_table_entry * entry = table->entry;
  100. table->entry = entry+1;
  101. get_file(filp);
  102. entry->filp = filp;
  103. entry->wait_address = wait_address;
  104. init_waitqueue_entry(&entry->wait, current);
  105. add_wait_queue(wait_address,&entry->wait);
  106. }
  107. }
  108. #define FDS_IN(fds, n) (fds->in + n)
  109. #define FDS_OUT(fds, n) (fds->out + n)
  110. #define FDS_EX(fds, n) (fds->ex + n)
  111. #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
  112. static int max_select_fd(unsigned long n, fd_set_bits *fds)
  113. {
  114. unsigned long *open_fds;
  115. unsigned long set;
  116. int max;
  117. struct fdtable *fdt;
  118. /* handle last in-complete long-word first */
  119. set = ~(~0UL << (n & (__NFDBITS-1)));
  120. n /= __NFDBITS;
  121. fdt = files_fdtable(current->files);
  122. open_fds = fdt->open_fds->fds_bits+n;
  123. max = 0;
  124. if (set) {
  125. set &= BITS(fds, n);
  126. if (set) {
  127. if (!(set & ~*open_fds))
  128. goto get_max;
  129. return -EBADF;
  130. }
  131. }
  132. while (n) {
  133. open_fds--;
  134. n--;
  135. set = BITS(fds, n);
  136. if (!set)
  137. continue;
  138. if (set & ~*open_fds)
  139. return -EBADF;
  140. if (max)
  141. continue;
  142. get_max:
  143. do {
  144. max++;
  145. set >>= 1;
  146. } while (set);
  147. max += n * __NFDBITS;
  148. }
  149. return max;
  150. }
  151. #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
  152. #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
  153. #define ISSET(i,m) (((i)&*(m)) != 0)
  154. #define SET(i,m) (*(m) |= (i))
  155. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  156. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  157. #define POLLEX_SET (POLLPRI)
  158. int do_select(int n, fd_set_bits *fds, long *timeout)
  159. {
  160. struct poll_wqueues table;
  161. poll_table *wait;
  162. int retval, i;
  163. long __timeout = *timeout;
  164. rcu_read_lock();
  165. retval = max_select_fd(n, fds);
  166. rcu_read_unlock();
  167. if (retval < 0)
  168. return retval;
  169. n = retval;
  170. poll_initwait(&table);
  171. wait = &table.pt;
  172. if (!__timeout)
  173. wait = NULL;
  174. retval = 0;
  175. for (;;) {
  176. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  177. set_current_state(TASK_INTERRUPTIBLE);
  178. inp = fds->in; outp = fds->out; exp = fds->ex;
  179. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  180. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  181. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  182. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  183. struct file_operations *f_op = NULL;
  184. struct file *file = NULL;
  185. in = *inp++; out = *outp++; ex = *exp++;
  186. all_bits = in | out | ex;
  187. if (all_bits == 0) {
  188. i += __NFDBITS;
  189. continue;
  190. }
  191. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  192. if (i >= n)
  193. break;
  194. if (!(bit & all_bits))
  195. continue;
  196. file = fget(i);
  197. if (file) {
  198. f_op = file->f_op;
  199. mask = DEFAULT_POLLMASK;
  200. if (f_op && f_op->poll)
  201. mask = (*f_op->poll)(file, retval ? NULL : wait);
  202. fput(file);
  203. if ((mask & POLLIN_SET) && (in & bit)) {
  204. res_in |= bit;
  205. retval++;
  206. }
  207. if ((mask & POLLOUT_SET) && (out & bit)) {
  208. res_out |= bit;
  209. retval++;
  210. }
  211. if ((mask & POLLEX_SET) && (ex & bit)) {
  212. res_ex |= bit;
  213. retval++;
  214. }
  215. }
  216. cond_resched();
  217. }
  218. if (res_in)
  219. *rinp = res_in;
  220. if (res_out)
  221. *routp = res_out;
  222. if (res_ex)
  223. *rexp = res_ex;
  224. }
  225. wait = NULL;
  226. if (retval || !__timeout || signal_pending(current))
  227. break;
  228. if(table.error) {
  229. retval = table.error;
  230. break;
  231. }
  232. __timeout = schedule_timeout(__timeout);
  233. }
  234. __set_current_state(TASK_RUNNING);
  235. poll_freewait(&table);
  236. /*
  237. * Up-to-date the caller timeout.
  238. */
  239. *timeout = __timeout;
  240. return retval;
  241. }
  242. static void *select_bits_alloc(int size)
  243. {
  244. return kmalloc(6 * size, GFP_KERNEL);
  245. }
  246. static void select_bits_free(void *bits, int size)
  247. {
  248. kfree(bits);
  249. }
  250. /*
  251. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  252. * like to be certain this leads to no problems. So I return
  253. * EINTR just for safety.
  254. *
  255. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  256. * I'm trying ERESTARTNOHAND which restart only when you want to.
  257. */
  258. #define MAX_SELECT_SECONDS \
  259. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  260. asmlinkage long
  261. sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
  262. {
  263. fd_set_bits fds;
  264. char *bits;
  265. long timeout;
  266. int ret, size, max_fdset;
  267. struct fdtable *fdt;
  268. timeout = MAX_SCHEDULE_TIMEOUT;
  269. if (tvp) {
  270. time_t sec, usec;
  271. if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
  272. || __get_user(sec, &tvp->tv_sec)
  273. || __get_user(usec, &tvp->tv_usec)) {
  274. ret = -EFAULT;
  275. goto out_nofds;
  276. }
  277. ret = -EINVAL;
  278. if (sec < 0 || usec < 0)
  279. goto out_nofds;
  280. if ((unsigned long) sec < MAX_SELECT_SECONDS) {
  281. timeout = ROUND_UP(usec, 1000000/HZ);
  282. timeout += sec * (unsigned long) HZ;
  283. }
  284. }
  285. ret = -EINVAL;
  286. if (n < 0)
  287. goto out_nofds;
  288. /* max_fdset can increase, so grab it once to avoid race */
  289. rcu_read_lock();
  290. fdt = files_fdtable(current->files);
  291. max_fdset = fdt->max_fdset;
  292. rcu_read_unlock();
  293. if (n > max_fdset)
  294. n = max_fdset;
  295. /*
  296. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  297. * since we used fdset we need to allocate memory in units of
  298. * long-words.
  299. */
  300. ret = -ENOMEM;
  301. size = FDS_BYTES(n);
  302. bits = select_bits_alloc(size);
  303. if (!bits)
  304. goto out_nofds;
  305. fds.in = (unsigned long *) bits;
  306. fds.out = (unsigned long *) (bits + size);
  307. fds.ex = (unsigned long *) (bits + 2*size);
  308. fds.res_in = (unsigned long *) (bits + 3*size);
  309. fds.res_out = (unsigned long *) (bits + 4*size);
  310. fds.res_ex = (unsigned long *) (bits + 5*size);
  311. if ((ret = get_fd_set(n, inp, fds.in)) ||
  312. (ret = get_fd_set(n, outp, fds.out)) ||
  313. (ret = get_fd_set(n, exp, fds.ex)))
  314. goto out;
  315. zero_fd_set(n, fds.res_in);
  316. zero_fd_set(n, fds.res_out);
  317. zero_fd_set(n, fds.res_ex);
  318. ret = do_select(n, &fds, &timeout);
  319. if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
  320. time_t sec = 0, usec = 0;
  321. if (timeout) {
  322. sec = timeout / HZ;
  323. usec = timeout % HZ;
  324. usec *= (1000000/HZ);
  325. }
  326. put_user(sec, &tvp->tv_sec);
  327. put_user(usec, &tvp->tv_usec);
  328. }
  329. if (ret < 0)
  330. goto out;
  331. if (!ret) {
  332. ret = -ERESTARTNOHAND;
  333. if (signal_pending(current))
  334. goto out;
  335. ret = 0;
  336. }
  337. if (set_fd_set(n, inp, fds.res_in) ||
  338. set_fd_set(n, outp, fds.res_out) ||
  339. set_fd_set(n, exp, fds.res_ex))
  340. ret = -EFAULT;
  341. out:
  342. select_bits_free(bits, size);
  343. out_nofds:
  344. return ret;
  345. }
  346. struct poll_list {
  347. struct poll_list *next;
  348. int len;
  349. struct pollfd entries[0];
  350. };
  351. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  352. static void do_pollfd(unsigned int num, struct pollfd * fdpage,
  353. poll_table ** pwait, int *count)
  354. {
  355. int i;
  356. for (i = 0; i < num; i++) {
  357. int fd;
  358. unsigned int mask;
  359. struct pollfd *fdp;
  360. mask = 0;
  361. fdp = fdpage+i;
  362. fd = fdp->fd;
  363. if (fd >= 0) {
  364. struct file * file = fget(fd);
  365. mask = POLLNVAL;
  366. if (file != NULL) {
  367. mask = DEFAULT_POLLMASK;
  368. if (file->f_op && file->f_op->poll)
  369. mask = file->f_op->poll(file, *pwait);
  370. mask &= fdp->events | POLLERR | POLLHUP;
  371. fput(file);
  372. }
  373. if (mask) {
  374. *pwait = NULL;
  375. (*count)++;
  376. }
  377. }
  378. fdp->revents = mask;
  379. }
  380. }
  381. static int do_poll(unsigned int nfds, struct poll_list *list,
  382. struct poll_wqueues *wait, long timeout)
  383. {
  384. int count = 0;
  385. poll_table* pt = &wait->pt;
  386. if (!timeout)
  387. pt = NULL;
  388. for (;;) {
  389. struct poll_list *walk;
  390. set_current_state(TASK_INTERRUPTIBLE);
  391. walk = list;
  392. while(walk != NULL) {
  393. do_pollfd( walk->len, walk->entries, &pt, &count);
  394. walk = walk->next;
  395. }
  396. pt = NULL;
  397. if (count || !timeout || signal_pending(current))
  398. break;
  399. count = wait->error;
  400. if (count)
  401. break;
  402. timeout = schedule_timeout(timeout);
  403. }
  404. __set_current_state(TASK_RUNNING);
  405. return count;
  406. }
  407. asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
  408. {
  409. struct poll_wqueues table;
  410. int fdcount, err;
  411. unsigned int i;
  412. struct poll_list *head;
  413. struct poll_list *walk;
  414. struct fdtable *fdt;
  415. int max_fdset;
  416. /* Do a sanity check on nfds ... */
  417. rcu_read_lock();
  418. fdt = files_fdtable(current->files);
  419. max_fdset = fdt->max_fdset;
  420. rcu_read_unlock();
  421. if (nfds > max_fdset && nfds > OPEN_MAX)
  422. return -EINVAL;
  423. if (timeout) {
  424. /* Careful about overflow in the intermediate values */
  425. if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
  426. timeout = (unsigned long)(timeout*HZ+999)/1000+1;
  427. else /* Negative or overflow */
  428. timeout = MAX_SCHEDULE_TIMEOUT;
  429. }
  430. poll_initwait(&table);
  431. head = NULL;
  432. walk = NULL;
  433. i = nfds;
  434. err = -ENOMEM;
  435. while(i!=0) {
  436. struct poll_list *pp;
  437. pp = kmalloc(sizeof(struct poll_list)+
  438. sizeof(struct pollfd)*
  439. (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
  440. GFP_KERNEL);
  441. if(pp==NULL)
  442. goto out_fds;
  443. pp->next=NULL;
  444. pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
  445. if (head == NULL)
  446. head = pp;
  447. else
  448. walk->next = pp;
  449. walk = pp;
  450. if (copy_from_user(pp->entries, ufds + nfds-i,
  451. sizeof(struct pollfd)*pp->len)) {
  452. err = -EFAULT;
  453. goto out_fds;
  454. }
  455. i -= pp->len;
  456. }
  457. fdcount = do_poll(nfds, head, &table, timeout);
  458. /* OK, now copy the revents fields back to user space. */
  459. walk = head;
  460. err = -EFAULT;
  461. while(walk != NULL) {
  462. struct pollfd *fds = walk->entries;
  463. int j;
  464. for (j=0; j < walk->len; j++, ufds++) {
  465. if(__put_user(fds[j].revents, &ufds->revents))
  466. goto out_fds;
  467. }
  468. walk = walk->next;
  469. }
  470. err = fdcount;
  471. if (!fdcount && signal_pending(current))
  472. err = -EINTR;
  473. out_fds:
  474. walk = head;
  475. while(walk!=NULL) {
  476. struct poll_list *pp = walk->next;
  477. kfree(walk);
  478. walk = pp;
  479. }
  480. poll_freewait(&table);
  481. return err;
  482. }