select.c 12 KB

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