select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. /* handle last in-complete long-word first */
  117. set = ~(~0UL << (n & (__NFDBITS-1)));
  118. n /= __NFDBITS;
  119. open_fds = current->files->open_fds->fds_bits+n;
  120. max = 0;
  121. if (set) {
  122. set &= BITS(fds, n);
  123. if (set) {
  124. if (!(set & ~*open_fds))
  125. goto get_max;
  126. return -EBADF;
  127. }
  128. }
  129. while (n) {
  130. open_fds--;
  131. n--;
  132. set = BITS(fds, n);
  133. if (!set)
  134. continue;
  135. if (set & ~*open_fds)
  136. return -EBADF;
  137. if (max)
  138. continue;
  139. get_max:
  140. do {
  141. max++;
  142. set >>= 1;
  143. } while (set);
  144. max += n * __NFDBITS;
  145. }
  146. return max;
  147. }
  148. #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
  149. #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
  150. #define ISSET(i,m) (((i)&*(m)) != 0)
  151. #define SET(i,m) (*(m) |= (i))
  152. #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
  153. #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
  154. #define POLLEX_SET (POLLPRI)
  155. int do_select(int n, fd_set_bits *fds, long *timeout)
  156. {
  157. struct poll_wqueues table;
  158. poll_table *wait;
  159. int retval, i;
  160. long __timeout = *timeout;
  161. spin_lock(&current->files->file_lock);
  162. retval = max_select_fd(n, fds);
  163. spin_unlock(&current->files->file_lock);
  164. if (retval < 0)
  165. return retval;
  166. n = retval;
  167. poll_initwait(&table);
  168. wait = &table.pt;
  169. if (!__timeout)
  170. wait = NULL;
  171. retval = 0;
  172. for (;;) {
  173. unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
  174. set_current_state(TASK_INTERRUPTIBLE);
  175. inp = fds->in; outp = fds->out; exp = fds->ex;
  176. rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
  177. for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
  178. unsigned long in, out, ex, all_bits, bit = 1, mask, j;
  179. unsigned long res_in = 0, res_out = 0, res_ex = 0;
  180. struct file_operations *f_op = NULL;
  181. struct file *file = NULL;
  182. in = *inp++; out = *outp++; ex = *exp++;
  183. all_bits = in | out | ex;
  184. if (all_bits == 0) {
  185. i += __NFDBITS;
  186. continue;
  187. }
  188. for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
  189. if (i >= n)
  190. break;
  191. if (!(bit & all_bits))
  192. continue;
  193. file = fget(i);
  194. if (file) {
  195. f_op = file->f_op;
  196. mask = DEFAULT_POLLMASK;
  197. if (f_op && f_op->poll)
  198. mask = (*f_op->poll)(file, retval ? NULL : wait);
  199. fput(file);
  200. if ((mask & POLLIN_SET) && (in & bit)) {
  201. res_in |= bit;
  202. retval++;
  203. }
  204. if ((mask & POLLOUT_SET) && (out & bit)) {
  205. res_out |= bit;
  206. retval++;
  207. }
  208. if ((mask & POLLEX_SET) && (ex & bit)) {
  209. res_ex |= bit;
  210. retval++;
  211. }
  212. }
  213. cond_resched();
  214. }
  215. if (res_in)
  216. *rinp = res_in;
  217. if (res_out)
  218. *routp = res_out;
  219. if (res_ex)
  220. *rexp = res_ex;
  221. }
  222. wait = NULL;
  223. if (retval || !__timeout || signal_pending(current))
  224. break;
  225. if(table.error) {
  226. retval = table.error;
  227. break;
  228. }
  229. __timeout = schedule_timeout(__timeout);
  230. }
  231. __set_current_state(TASK_RUNNING);
  232. poll_freewait(&table);
  233. /*
  234. * Up-to-date the caller timeout.
  235. */
  236. *timeout = __timeout;
  237. return retval;
  238. }
  239. static void *select_bits_alloc(int size)
  240. {
  241. return kmalloc(6 * size, GFP_KERNEL);
  242. }
  243. static void select_bits_free(void *bits, int size)
  244. {
  245. kfree(bits);
  246. }
  247. /*
  248. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  249. * like to be certain this leads to no problems. So I return
  250. * EINTR just for safety.
  251. *
  252. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  253. * I'm trying ERESTARTNOHAND which restart only when you want to.
  254. */
  255. #define MAX_SELECT_SECONDS \
  256. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  257. asmlinkage long
  258. sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
  259. {
  260. fd_set_bits fds;
  261. char *bits;
  262. long timeout;
  263. int ret, size, max_fdset;
  264. timeout = MAX_SCHEDULE_TIMEOUT;
  265. if (tvp) {
  266. time_t sec, usec;
  267. if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
  268. || __get_user(sec, &tvp->tv_sec)
  269. || __get_user(usec, &tvp->tv_usec)) {
  270. ret = -EFAULT;
  271. goto out_nofds;
  272. }
  273. ret = -EINVAL;
  274. if (sec < 0 || usec < 0)
  275. goto out_nofds;
  276. if ((unsigned long) sec < MAX_SELECT_SECONDS) {
  277. timeout = ROUND_UP(usec, 1000000/HZ);
  278. timeout += sec * (unsigned long) HZ;
  279. }
  280. }
  281. ret = -EINVAL;
  282. if (n < 0)
  283. goto out_nofds;
  284. /* max_fdset can increase, so grab it once to avoid race */
  285. max_fdset = current->files->max_fdset;
  286. if (n > max_fdset)
  287. n = max_fdset;
  288. /*
  289. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  290. * since we used fdset we need to allocate memory in units of
  291. * long-words.
  292. */
  293. ret = -ENOMEM;
  294. size = FDS_BYTES(n);
  295. bits = select_bits_alloc(size);
  296. if (!bits)
  297. goto out_nofds;
  298. fds.in = (unsigned long *) bits;
  299. fds.out = (unsigned long *) (bits + size);
  300. fds.ex = (unsigned long *) (bits + 2*size);
  301. fds.res_in = (unsigned long *) (bits + 3*size);
  302. fds.res_out = (unsigned long *) (bits + 4*size);
  303. fds.res_ex = (unsigned long *) (bits + 5*size);
  304. if ((ret = get_fd_set(n, inp, fds.in)) ||
  305. (ret = get_fd_set(n, outp, fds.out)) ||
  306. (ret = get_fd_set(n, exp, fds.ex)))
  307. goto out;
  308. zero_fd_set(n, fds.res_in);
  309. zero_fd_set(n, fds.res_out);
  310. zero_fd_set(n, fds.res_ex);
  311. ret = do_select(n, &fds, &timeout);
  312. if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
  313. time_t sec = 0, usec = 0;
  314. if (timeout) {
  315. sec = timeout / HZ;
  316. usec = timeout % HZ;
  317. usec *= (1000000/HZ);
  318. }
  319. put_user(sec, &tvp->tv_sec);
  320. put_user(usec, &tvp->tv_usec);
  321. }
  322. if (ret < 0)
  323. goto out;
  324. if (!ret) {
  325. ret = -ERESTARTNOHAND;
  326. if (signal_pending(current))
  327. goto out;
  328. ret = 0;
  329. }
  330. if (set_fd_set(n, inp, fds.res_in) ||
  331. set_fd_set(n, outp, fds.res_out) ||
  332. set_fd_set(n, exp, fds.res_ex))
  333. ret = -EFAULT;
  334. out:
  335. select_bits_free(bits, size);
  336. out_nofds:
  337. return ret;
  338. }
  339. struct poll_list {
  340. struct poll_list *next;
  341. int len;
  342. struct pollfd entries[0];
  343. };
  344. #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
  345. static void do_pollfd(unsigned int num, struct pollfd * fdpage,
  346. poll_table ** pwait, int *count)
  347. {
  348. int i;
  349. for (i = 0; i < num; i++) {
  350. int fd;
  351. unsigned int mask;
  352. struct pollfd *fdp;
  353. mask = 0;
  354. fdp = fdpage+i;
  355. fd = fdp->fd;
  356. if (fd >= 0) {
  357. struct file * file = fget(fd);
  358. mask = POLLNVAL;
  359. if (file != NULL) {
  360. mask = DEFAULT_POLLMASK;
  361. if (file->f_op && file->f_op->poll)
  362. mask = file->f_op->poll(file, *pwait);
  363. mask &= fdp->events | POLLERR | POLLHUP;
  364. fput(file);
  365. }
  366. if (mask) {
  367. *pwait = NULL;
  368. (*count)++;
  369. }
  370. }
  371. fdp->revents = mask;
  372. }
  373. }
  374. static int do_poll(unsigned int nfds, struct poll_list *list,
  375. struct poll_wqueues *wait, long timeout)
  376. {
  377. int count = 0;
  378. poll_table* pt = &wait->pt;
  379. if (!timeout)
  380. pt = NULL;
  381. for (;;) {
  382. struct poll_list *walk;
  383. set_current_state(TASK_INTERRUPTIBLE);
  384. walk = list;
  385. while(walk != NULL) {
  386. do_pollfd( walk->len, walk->entries, &pt, &count);
  387. walk = walk->next;
  388. }
  389. pt = NULL;
  390. if (count || !timeout || signal_pending(current))
  391. break;
  392. count = wait->error;
  393. if (count)
  394. break;
  395. timeout = schedule_timeout(timeout);
  396. }
  397. __set_current_state(TASK_RUNNING);
  398. return count;
  399. }
  400. asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
  401. {
  402. struct poll_wqueues table;
  403. int fdcount, err;
  404. unsigned int i;
  405. struct poll_list *head;
  406. struct poll_list *walk;
  407. /* Do a sanity check on nfds ... */
  408. if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
  409. return -EINVAL;
  410. if (timeout) {
  411. /* Careful about overflow in the intermediate values */
  412. if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
  413. timeout = (unsigned long)(timeout*HZ+999)/1000+1;
  414. else /* Negative or overflow */
  415. timeout = MAX_SCHEDULE_TIMEOUT;
  416. }
  417. poll_initwait(&table);
  418. head = NULL;
  419. walk = NULL;
  420. i = nfds;
  421. err = -ENOMEM;
  422. while(i!=0) {
  423. struct poll_list *pp;
  424. pp = kmalloc(sizeof(struct poll_list)+
  425. sizeof(struct pollfd)*
  426. (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
  427. GFP_KERNEL);
  428. if(pp==NULL)
  429. goto out_fds;
  430. pp->next=NULL;
  431. pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
  432. if (head == NULL)
  433. head = pp;
  434. else
  435. walk->next = pp;
  436. walk = pp;
  437. if (copy_from_user(pp->entries, ufds + nfds-i,
  438. sizeof(struct pollfd)*pp->len)) {
  439. err = -EFAULT;
  440. goto out_fds;
  441. }
  442. i -= pp->len;
  443. }
  444. fdcount = do_poll(nfds, head, &table, timeout);
  445. /* OK, now copy the revents fields back to user space. */
  446. walk = head;
  447. err = -EFAULT;
  448. while(walk != NULL) {
  449. struct pollfd *fds = walk->entries;
  450. int j;
  451. for (j=0; j < walk->len; j++, ufds++) {
  452. if(__put_user(fds[j].revents, &ufds->revents))
  453. goto out_fds;
  454. }
  455. walk = walk->next;
  456. }
  457. err = fdcount;
  458. if (!fdcount && signal_pending(current))
  459. err = -EINTR;
  460. out_fds:
  461. walk = head;
  462. while(walk!=NULL) {
  463. struct poll_list *pp = walk->next;
  464. kfree(walk);
  465. walk = pp;
  466. }
  467. poll_freewait(&table);
  468. return err;
  469. }