port_kern.c 6.5 KB

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