port_kern.c 6.5 KB

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