port_kern.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/completion.h"
  6. #include "linux/interrupt.h"
  7. #include "linux/list.h"
  8. #include "asm/atomic.h"
  9. #include "init.h"
  10. #include "irq_kern.h"
  11. #include "os.h"
  12. #include "port.h"
  13. struct port_list {
  14. struct list_head list;
  15. atomic_t wait_count;
  16. int has_connection;
  17. struct completion done;
  18. int port;
  19. int fd;
  20. spinlock_t lock;
  21. struct list_head pending;
  22. struct list_head connections;
  23. };
  24. struct port_dev {
  25. struct port_list *port;
  26. int helper_pid;
  27. int telnetd_pid;
  28. };
  29. struct connection {
  30. struct list_head list;
  31. int fd;
  32. int helper_pid;
  33. int socket[2];
  34. int telnetd_pid;
  35. struct port_list *port;
  36. };
  37. static irqreturn_t pipe_interrupt(int irq, void *data)
  38. {
  39. struct connection *conn = data;
  40. int fd;
  41. fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
  42. if (fd < 0) {
  43. if (fd == -EAGAIN)
  44. return IRQ_NONE;
  45. printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n",
  46. -fd);
  47. os_close_file(conn->fd);
  48. }
  49. list_del(&conn->list);
  50. conn->fd = fd;
  51. list_add(&conn->list, &conn->port->connections);
  52. complete(&conn->port->done);
  53. return IRQ_HANDLED;
  54. }
  55. #define NO_WAITER_MSG \
  56. "****\n" \
  57. "There are currently no UML consoles waiting for port connections.\n" \
  58. "Either disconnect from one to make it available or activate some more\n" \
  59. "by enabling more consoles in the UML /etc/inittab.\n" \
  60. "****\n"
  61. static int port_accept(struct port_list *port)
  62. {
  63. struct connection *conn;
  64. int fd, socket[2], pid;
  65. fd = port_connection(port->fd, socket, &pid);
  66. if (fd < 0) {
  67. if (fd != -EAGAIN)
  68. printk(KERN_ERR "port_accept : port_connection "
  69. "returned %d\n", -fd);
  70. goto out;
  71. }
  72. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  73. if (conn == NULL) {
  74. printk(KERN_ERR "port_accept : failed to allocate "
  75. "connection\n");
  76. goto out_close;
  77. }
  78. *conn = ((struct connection)
  79. { .list = LIST_HEAD_INIT(conn->list),
  80. .fd = fd,
  81. .socket = { socket[0], socket[1] },
  82. .telnetd_pid = pid,
  83. .port = port });
  84. if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
  85. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  86. "telnetd", conn)) {
  87. printk(KERN_ERR "port_accept : failed to get IRQ for "
  88. "telnetd\n");
  89. goto out_free;
  90. }
  91. if (atomic_read(&port->wait_count) == 0) {
  92. os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
  93. printk(KERN_ERR "No one waiting for port\n");
  94. }
  95. list_add(&conn->list, &port->pending);
  96. return 1;
  97. out_free:
  98. kfree(conn);
  99. out_close:
  100. os_close_file(fd);
  101. os_kill_process(pid, 1);
  102. out:
  103. return 0;
  104. }
  105. static DECLARE_MUTEX(ports_sem);
  106. static LIST_HEAD(ports);
  107. static void port_work_proc(struct work_struct *unused)
  108. {
  109. struct port_list *port;
  110. struct list_head *ele;
  111. unsigned long flags;
  112. local_irq_save(flags);
  113. list_for_each(ele, &ports) {
  114. port = list_entry(ele, struct port_list, list);
  115. if (!port->has_connection)
  116. continue;
  117. reactivate_fd(port->fd, ACCEPT_IRQ);
  118. while (port_accept(port))
  119. ;
  120. port->has_connection = 0;
  121. }
  122. local_irq_restore(flags);
  123. }
  124. DECLARE_WORK(port_work, port_work_proc);
  125. static irqreturn_t port_interrupt(int irq, void *data)
  126. {
  127. struct port_list *port = data;
  128. port->has_connection = 1;
  129. schedule_work(&port_work);
  130. return IRQ_HANDLED;
  131. }
  132. void *port_data(int port_num)
  133. {
  134. struct list_head *ele;
  135. struct port_list *port;
  136. struct port_dev *dev = NULL;
  137. int fd;
  138. down(&ports_sem);
  139. list_for_each(ele, &ports) {
  140. port = list_entry(ele, struct port_list, list);
  141. if (port->port == port_num)
  142. goto found;
  143. }
  144. port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
  145. if (port == NULL) {
  146. printk(KERN_ERR "Allocation of port list failed\n");
  147. goto out;
  148. }
  149. fd = port_listen_fd(port_num);
  150. if (fd < 0) {
  151. printk(KERN_ERR "binding to port %d failed, errno = %d\n",
  152. port_num, -fd);
  153. goto out_free;
  154. }
  155. if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
  156. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  157. "port", port)) {
  158. printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
  159. goto out_close;
  160. }
  161. *port = ((struct port_list)
  162. { .list = LIST_HEAD_INIT(port->list),
  163. .wait_count = ATOMIC_INIT(0),
  164. .has_connection = 0,
  165. .port = port_num,
  166. .fd = fd,
  167. .pending = LIST_HEAD_INIT(port->pending),
  168. .connections = LIST_HEAD_INIT(port->connections) });
  169. spin_lock_init(&port->lock);
  170. init_completion(&port->done);
  171. list_add(&port->list, &ports);
  172. found:
  173. dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
  174. if (dev == NULL) {
  175. printk(KERN_ERR "Allocation of port device entry failed\n");
  176. goto out;
  177. }
  178. *dev = ((struct port_dev) { .port = port,
  179. .helper_pid = -1,
  180. .telnetd_pid = -1 });
  181. goto out;
  182. out_close:
  183. os_close_file(fd);
  184. out_free:
  185. kfree(port);
  186. out:
  187. up(&ports_sem);
  188. return dev;
  189. }
  190. int port_wait(void *data)
  191. {
  192. struct port_dev *dev = data;
  193. struct connection *conn;
  194. struct port_list *port = dev->port;
  195. int fd;
  196. atomic_inc(&port->wait_count);
  197. while (1) {
  198. fd = -ERESTARTSYS;
  199. if (wait_for_completion_interruptible(&port->done))
  200. goto out;
  201. spin_lock(&port->lock);
  202. conn = list_entry(port->connections.next, struct connection,
  203. list);
  204. list_del(&conn->list);
  205. spin_unlock(&port->lock);
  206. os_shutdown_socket(conn->socket[0], 1, 1);
  207. os_close_file(conn->socket[0]);
  208. os_shutdown_socket(conn->socket[1], 1, 1);
  209. os_close_file(conn->socket[1]);
  210. /* This is done here because freeing an IRQ can't be done
  211. * within the IRQ handler. So, pipe_interrupt always ups
  212. * the semaphore regardless of whether it got a successful
  213. * connection. Then we loop here throwing out failed
  214. * connections until a good one is found.
  215. */
  216. free_irq(TELNETD_IRQ, conn);
  217. if (conn->fd >= 0)
  218. break;
  219. os_close_file(conn->fd);
  220. kfree(conn);
  221. }
  222. fd = conn->fd;
  223. dev->helper_pid = conn->helper_pid;
  224. dev->telnetd_pid = conn->telnetd_pid;
  225. kfree(conn);
  226. out:
  227. atomic_dec(&port->wait_count);
  228. return fd;
  229. }
  230. void port_remove_dev(void *d)
  231. {
  232. struct port_dev *dev = d;
  233. if (dev->helper_pid != -1)
  234. os_kill_process(dev->helper_pid, 0);
  235. if (dev->telnetd_pid != -1)
  236. os_kill_process(dev->telnetd_pid, 1);
  237. dev->helper_pid = -1;
  238. dev->telnetd_pid = -1;
  239. }
  240. void port_kern_free(void *d)
  241. {
  242. struct port_dev *dev = d;
  243. port_remove_dev(dev);
  244. kfree(dev);
  245. }
  246. static void free_port(void)
  247. {
  248. struct list_head *ele;
  249. struct port_list *port;
  250. list_for_each(ele, &ports) {
  251. port = list_entry(ele, struct port_list, list);
  252. free_irq_by_fd(port->fd);
  253. os_close_file(port->fd);
  254. }
  255. }
  256. __uml_exitcall(free_port);