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