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