port_kern.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 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_SHARED, "telnetd", conn)) {
  89. printk(KERN_ERR "port_accept : failed to get IRQ for "
  90. "telnetd\n");
  91. goto out_free;
  92. }
  93. if (atomic_read(&port->wait_count) == 0) {
  94. os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
  95. printk(KERN_ERR "No one waiting for port\n");
  96. }
  97. list_add(&conn->list, &port->pending);
  98. return 1;
  99. out_free:
  100. kfree(conn);
  101. out_close:
  102. os_close_file(fd);
  103. os_kill_process(pid, 1);
  104. out:
  105. return 0;
  106. }
  107. static DEFINE_MUTEX(ports_mutex);
  108. static LIST_HEAD(ports);
  109. static void port_work_proc(struct work_struct *unused)
  110. {
  111. struct port_list *port;
  112. struct list_head *ele;
  113. unsigned long flags;
  114. local_irq_save(flags);
  115. list_for_each(ele, &ports) {
  116. port = list_entry(ele, struct port_list, list);
  117. if (!port->has_connection)
  118. continue;
  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_SHARED, "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. mutex_unlock(&ports_mutex);
  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. um_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);