port_kern.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  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 n_fds = 1, fd = -1;
  44. ssize_t ret;
  45. ret = os_rcv_fd_msg(conn->socket[0], &fd, n_fds, &conn->helper_pid,
  46. sizeof(conn->helper_pid));
  47. if (ret != sizeof(conn->helper_pid)) {
  48. if (ret == -EAGAIN)
  49. return IRQ_NONE;
  50. printk(KERN_ERR "pipe_interrupt : os_rcv_fd_msg returned %zd\n",
  51. ret);
  52. os_close_file(conn->fd);
  53. }
  54. list_del(&conn->list);
  55. conn->fd = fd;
  56. list_add(&conn->list, &conn->port->connections);
  57. complete(&conn->port->done);
  58. return IRQ_HANDLED;
  59. }
  60. #define NO_WAITER_MSG \
  61. "****\n" \
  62. "There are currently no UML consoles waiting for port connections.\n" \
  63. "Either disconnect from one to make it available or activate some more\n" \
  64. "by enabling more consoles in the UML /etc/inittab.\n" \
  65. "****\n"
  66. static int port_accept(struct port_list *port)
  67. {
  68. struct connection *conn;
  69. int fd, socket[2], pid;
  70. fd = port_connection(port->fd, socket, &pid);
  71. if (fd < 0) {
  72. if (fd != -EAGAIN)
  73. printk(KERN_ERR "port_accept : port_connection "
  74. "returned %d\n", -fd);
  75. goto out;
  76. }
  77. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  78. if (conn == NULL) {
  79. printk(KERN_ERR "port_accept : failed to allocate "
  80. "connection\n");
  81. goto out_close;
  82. }
  83. *conn = ((struct connection)
  84. { .list = LIST_HEAD_INIT(conn->list),
  85. .fd = fd,
  86. .socket = { socket[0], socket[1] },
  87. .telnetd_pid = pid,
  88. .port = port });
  89. if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
  90. IRQF_SHARED, "telnetd", conn) < 0) {
  91. printk(KERN_ERR "port_accept : failed to get IRQ for "
  92. "telnetd\n");
  93. goto out_free;
  94. }
  95. if (atomic_read(&port->wait_count) == 0) {
  96. os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
  97. printk(KERN_ERR "No one waiting for port\n");
  98. }
  99. list_add(&conn->list, &port->pending);
  100. return 1;
  101. out_free:
  102. kfree(conn);
  103. out_close:
  104. os_close_file(fd);
  105. os_kill_process(pid, 1);
  106. out:
  107. return 0;
  108. }
  109. static DEFINE_MUTEX(ports_mutex);
  110. static LIST_HEAD(ports);
  111. static void port_work_proc(struct work_struct *unused)
  112. {
  113. struct port_list *port;
  114. struct list_head *ele;
  115. unsigned long flags;
  116. local_irq_save(flags);
  117. list_for_each(ele, &ports) {
  118. port = list_entry(ele, struct port_list, list);
  119. if (!port->has_connection)
  120. continue;
  121. while (port_accept(port))
  122. ;
  123. port->has_connection = 0;
  124. }
  125. local_irq_restore(flags);
  126. }
  127. static 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_SHARED, "port", port) < 0) {
  160. printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
  161. goto out_close;
  162. }
  163. *port = ((struct port_list)
  164. { .list = LIST_HEAD_INIT(port->list),
  165. .wait_count = ATOMIC_INIT(0),
  166. .has_connection = 0,
  167. .port = port_num,
  168. .fd = fd,
  169. .pending = LIST_HEAD_INIT(port->pending),
  170. .connections = LIST_HEAD_INIT(port->connections) });
  171. spin_lock_init(&port->lock);
  172. init_completion(&port->done);
  173. list_add(&port->list, &ports);
  174. found:
  175. dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
  176. if (dev == NULL) {
  177. printk(KERN_ERR "Allocation of port device entry failed\n");
  178. goto out;
  179. }
  180. *dev = ((struct port_dev) { .port = port,
  181. .helper_pid = -1,
  182. .telnetd_pid = -1 });
  183. goto out;
  184. out_close:
  185. os_close_file(fd);
  186. out_free:
  187. kfree(port);
  188. out:
  189. mutex_unlock(&ports_mutex);
  190. return dev;
  191. }
  192. int port_wait(void *data)
  193. {
  194. struct port_dev *dev = data;
  195. struct connection *conn;
  196. struct port_list *port = dev->port;
  197. int fd;
  198. atomic_inc(&port->wait_count);
  199. while (1) {
  200. fd = -ERESTARTSYS;
  201. if (wait_for_completion_interruptible(&port->done))
  202. goto out;
  203. spin_lock(&port->lock);
  204. conn = list_entry(port->connections.next, struct connection,
  205. list);
  206. list_del(&conn->list);
  207. spin_unlock(&port->lock);
  208. os_shutdown_socket(conn->socket[0], 1, 1);
  209. os_close_file(conn->socket[0]);
  210. os_shutdown_socket(conn->socket[1], 1, 1);
  211. os_close_file(conn->socket[1]);
  212. /* This is done here because freeing an IRQ can't be done
  213. * within the IRQ handler. So, pipe_interrupt always ups
  214. * the semaphore regardless of whether it got a successful
  215. * connection. Then we loop here throwing out failed
  216. * connections until a good one is found.
  217. */
  218. um_free_irq(TELNETD_IRQ, conn);
  219. if (conn->fd >= 0)
  220. break;
  221. os_close_file(conn->fd);
  222. kfree(conn);
  223. }
  224. fd = conn->fd;
  225. dev->helper_pid = conn->helper_pid;
  226. dev->telnetd_pid = conn->telnetd_pid;
  227. kfree(conn);
  228. out:
  229. atomic_dec(&port->wait_count);
  230. return fd;
  231. }
  232. void port_remove_dev(void *d)
  233. {
  234. struct port_dev *dev = d;
  235. if (dev->helper_pid != -1)
  236. os_kill_process(dev->helper_pid, 0);
  237. if (dev->telnetd_pid != -1)
  238. os_kill_process(dev->telnetd_pid, 1);
  239. dev->helper_pid = -1;
  240. dev->telnetd_pid = -1;
  241. }
  242. void port_kern_free(void *d)
  243. {
  244. struct port_dev *dev = d;
  245. port_remove_dev(dev);
  246. kfree(dev);
  247. }
  248. static void free_port(void)
  249. {
  250. struct list_head *ele;
  251. struct port_list *port;
  252. list_for_each(ele, &ports) {
  253. port = list_entry(ele, struct port_list, list);
  254. free_irq_by_fd(port->fd);
  255. os_close_file(port->fd);
  256. }
  257. }
  258. __uml_exitcall(free_port);