select.c 12 KB

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