irq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <string.h>
  10. #include <sys/poll.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include "user_util.h"
  14. #include "kern_util.h"
  15. #include "user.h"
  16. #include "process.h"
  17. #include "sigio.h"
  18. #include "irq_user.h"
  19. #include "os.h"
  20. static struct pollfd *pollfds = NULL;
  21. static int pollfds_num = 0;
  22. static int pollfds_size = 0;
  23. int os_waiting_for_events(struct irq_fd *active_fds)
  24. {
  25. struct irq_fd *irq_fd;
  26. int i, n, err;
  27. n = poll(pollfds, pollfds_num, 0);
  28. if(n < 0){
  29. err = -errno;
  30. if(errno != EINTR)
  31. printk("sigio_handler: os_waiting_for_events:"
  32. " poll returned %d, errno = %d\n", n, errno);
  33. return err;
  34. }
  35. if(n == 0)
  36. return 0;
  37. irq_fd = active_fds;
  38. for(i = 0; i < pollfds_num; i++){
  39. if(pollfds[i].revents != 0){
  40. irq_fd->current_events = pollfds[i].revents;
  41. pollfds[i].fd = -1;
  42. }
  43. irq_fd = irq_fd->next;
  44. }
  45. return n;
  46. }
  47. int os_isatty(int fd)
  48. {
  49. return(isatty(fd));
  50. }
  51. int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
  52. {
  53. if (pollfds_num == pollfds_size) {
  54. if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
  55. /* return min size needed for new pollfds area */
  56. return((pollfds_size + 1) * sizeof(pollfds[0]));
  57. }
  58. if(pollfds != NULL){
  59. memcpy(tmp_pfd, pollfds,
  60. sizeof(pollfds[0]) * pollfds_size);
  61. /* remove old pollfds */
  62. kfree(pollfds);
  63. }
  64. pollfds = tmp_pfd;
  65. pollfds_size++;
  66. } else {
  67. /* remove not used tmp_pfd */
  68. if (tmp_pfd != NULL)
  69. kfree(tmp_pfd);
  70. }
  71. pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
  72. .events = events,
  73. .revents = 0 });
  74. pollfds_num++;
  75. return(0);
  76. }
  77. void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
  78. struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
  79. {
  80. struct irq_fd **prev;
  81. int i = 0;
  82. prev = &active_fds;
  83. while(*prev != NULL){
  84. if((*test)(*prev, arg)){
  85. struct irq_fd *old_fd = *prev;
  86. if((pollfds[i].fd != -1) &&
  87. (pollfds[i].fd != (*prev)->fd)){
  88. printk("os_free_irq_by_cb - mismatch between "
  89. "active_fds and pollfds, fd %d vs %d\n",
  90. (*prev)->fd, pollfds[i].fd);
  91. goto out;
  92. }
  93. pollfds_num--;
  94. /* This moves the *whole* array after pollfds[i]
  95. * (though it doesn't spot as such)!
  96. */
  97. memmove(&pollfds[i], &pollfds[i + 1],
  98. (pollfds_num - i) * sizeof(pollfds[0]));
  99. if(*last_irq_ptr2 == &old_fd->next)
  100. *last_irq_ptr2 = prev;
  101. *prev = (*prev)->next;
  102. if(old_fd->type == IRQ_WRITE)
  103. ignore_sigio_fd(old_fd->fd);
  104. kfree(old_fd);
  105. continue;
  106. }
  107. prev = &(*prev)->next;
  108. i++;
  109. }
  110. out:
  111. return;
  112. }
  113. int os_get_pollfd(int i)
  114. {
  115. return(pollfds[i].fd);
  116. }
  117. void os_set_pollfd(int i, int fd)
  118. {
  119. pollfds[i].fd = fd;
  120. }
  121. void os_set_ioignore(void)
  122. {
  123. set_handler(SIGIO, SIG_IGN, 0, -1);
  124. }
  125. void init_irq_signals(int on_sigstack)
  126. {
  127. __sighandler_t h;
  128. int flags;
  129. flags = on_sigstack ? SA_ONSTACK : 0;
  130. if(timer_irq_inited) h = (__sighandler_t) alarm_handler;
  131. else h = boot_timer_handler;
  132. set_handler(SIGVTALRM, h, flags | SA_RESTART,
  133. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
  134. set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
  135. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  136. signal(SIGWINCH, SIG_IGN);
  137. }