blob: a11573be0961fd7b233a02ff40421aa3a53940c7 [file] [log] [blame]
Jeff Dike67608e02007-02-10 01:44:02 -08001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dikee99525f2007-10-16 01:26:41 -07006#include "linux/completion.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/interrupt.h"
Jeff Dikee99525f2007-10-16 01:26:41 -07008#include "linux/list.h"
Daniel Walker2aa9c5d2008-02-04 22:31:27 -08009#include "linux/mutex.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include "linux/slab.h"
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040011#include "linux/workqueue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/atomic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "init.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070014#include "irq_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "os.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070016#include "port.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18struct 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
30struct port_dev {
31 struct port_list *port;
32 int helper_pid;
33 int telnetd_pid;
34};
35
36struct 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 Viro7bea96f2006-10-08 22:49:34 +010045static irqreturn_t pipe_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct connection *conn = data;
48 int fd;
49
50 fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
Jeff Dikee99525f2007-10-16 01:26:41 -070051 if (fd < 0) {
52 if (fd == -EAGAIN)
Jeff Dike67608e02007-02-10 01:44:02 -080053 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Dike67608e02007-02-10 01:44:02 -080055 printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 -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 Dike67608e02007-02-10 01:44:02 -080066 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
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
76static int port_accept(struct port_list *port)
77{
78 struct connection *conn;
Jeff Dikee99525f2007-10-16 01:26:41 -070079 int fd, socket[2], pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 fd = port_connection(port->fd, socket, &pid);
Jeff Dikee99525f2007-10-16 01:26:41 -070082 if (fd < 0) {
83 if (fd != -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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 Dikee99525f2007-10-16 01:26:41 -070090 if (conn == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 printk(KERN_ERR "port_accept : failed to allocate "
92 "connection\n");
93 goto out_close;
94 }
Jeff Dike67608e02007-02-10 01:44:02 -080095 *conn = ((struct connection)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 { .list = LIST_HEAD_INIT(conn->list),
97 .fd = fd,
98 .socket = { socket[0], socket[1] },
99 .telnetd_pid = pid,
100 .port = port });
101
Jeff Dikee99525f2007-10-16 01:26:41 -0700102 if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700103 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dikee99525f2007-10-16 01:26:41 -0700104 "telnetd", conn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 printk(KERN_ERR "port_accept : failed to get IRQ for "
106 "telnetd\n");
107 goto out_free;
108 }
109
Jeff Dikee99525f2007-10-16 01:26:41 -0700110 if (atomic_read(&port->wait_count) == 0) {
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700111 os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
Jeff Dikee99525f2007-10-16 01:26:41 -0700112 printk(KERN_ERR "No one waiting for port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 list_add(&conn->list, &port->pending);
Jeff Dike67608e02007-02-10 01:44:02 -0800115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 out_free:
118 kfree(conn);
119 out_close:
120 os_close_file(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -0700121 os_kill_process(pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700123 return 0;
Jeff Dike67608e02007-02-10 01:44:02 -0800124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800126static DEFINE_MUTEX(ports_mutex);
Jeff Dikec59bce62007-02-10 01:44:04 -0800127static LIST_HEAD(ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jeff Dikee99525f2007-10-16 01:26:41 -0700129static void port_work_proc(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct port_list *port;
132 struct list_head *ele;
133 unsigned long flags;
134
135 local_irq_save(flags);
Jeff Dikee99525f2007-10-16 01:26:41 -0700136 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 port = list_entry(ele, struct port_list, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700138 if (!port->has_connection)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 reactivate_fd(port->fd, ACCEPT_IRQ);
Jeff Dikee99525f2007-10-16 01:26:41 -0700142 while (port_accept(port))
143 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 port->has_connection = 0;
145 }
146 local_irq_restore(flags);
147}
148
David Howells6d5aefb2006-12-05 19:36:26 +0000149DECLARE_WORK(port_work, port_work_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Al Viro7bea96f2006-10-08 22:49:34 +0100151static irqreturn_t port_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct port_list *port = data;
154
155 port->has_connection = 1;
156 schedule_work(&port_work);
Jeff Dike67608e02007-02-10 01:44:02 -0800157 return IRQ_HANDLED;
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160void *port_data(int port_num)
161{
162 struct list_head *ele;
163 struct port_list *port;
164 struct port_dev *dev = NULL;
165 int fd;
166
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800167 mutex_lock(&ports_mutex);
Jeff Dikee99525f2007-10-16 01:26:41 -0700168 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 port = list_entry(ele, struct port_list, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700170 if (port->port == port_num)
171 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -0700174 if (port == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 printk(KERN_ERR "Allocation of port list failed\n");
176 goto out;
177 }
178
179 fd = port_listen_fd(port_num);
Jeff Dikee99525f2007-10-16 01:26:41 -0700180 if (fd < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 printk(KERN_ERR "binding to port %d failed, errno = %d\n",
182 port_num, -fd);
183 goto out_free;
184 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700185
186 if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
Jeff Dike67608e02007-02-10 01:44:02 -0800187 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dikee99525f2007-10-16 01:26:41 -0700188 "port", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
190 goto out_close;
191 }
192
Jeff Dike67608e02007-02-10 01:44:02 -0800193 *port = ((struct port_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 { .list = LIST_HEAD_INIT(port->list),
195 .wait_count = ATOMIC_INIT(0),
196 .has_connection = 0,
197 .port = port_num,
198 .fd = fd,
199 .pending = LIST_HEAD_INIT(port->pending),
200 .connections = LIST_HEAD_INIT(port->connections) });
201 spin_lock_init(&port->lock);
202 init_completion(&port->done);
203 list_add(&port->list, &ports);
204
205 found:
206 dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -0700207 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 printk(KERN_ERR "Allocation of port device entry failed\n");
209 goto out;
210 }
211
212 *dev = ((struct port_dev) { .port = port,
213 .helper_pid = -1,
214 .telnetd_pid = -1 });
215 goto out;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 out_close:
218 os_close_file(fd);
Jeff Dike79f66232007-10-16 01:26:40 -0700219 out_free:
220 kfree(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 out:
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800222 mutex_unlock(&ports_mutex);
Jeff Dike67608e02007-02-10 01:44:02 -0800223 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226int port_wait(void *data)
227{
228 struct port_dev *dev = data;
229 struct connection *conn;
230 struct port_list *port = dev->port;
231 int fd;
232
Jeff Dike67608e02007-02-10 01:44:02 -0800233 atomic_inc(&port->wait_count);
Jeff Dikee99525f2007-10-16 01:26:41 -0700234 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 fd = -ERESTARTSYS;
Jeff Dikee99525f2007-10-16 01:26:41 -0700236 if (wait_for_completion_interruptible(&port->done))
Jeff Dike67608e02007-02-10 01:44:02 -0800237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 spin_lock(&port->lock);
240
Jeff Dike67608e02007-02-10 01:44:02 -0800241 conn = list_entry(port->connections.next, struct connection,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 list);
243 list_del(&conn->list);
244 spin_unlock(&port->lock);
245
246 os_shutdown_socket(conn->socket[0], 1, 1);
247 os_close_file(conn->socket[0]);
248 os_shutdown_socket(conn->socket[1], 1, 1);
Jeff Dike67608e02007-02-10 01:44:02 -0800249 os_close_file(conn->socket[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* This is done here because freeing an IRQ can't be done
252 * within the IRQ handler. So, pipe_interrupt always ups
253 * the semaphore regardless of whether it got a successful
Jeff Dike67608e02007-02-10 01:44:02 -0800254 * connection. Then we loop here throwing out failed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * connections until a good one is found.
256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 free_irq(TELNETD_IRQ, conn);
258
Jeff Dikee99525f2007-10-16 01:26:41 -0700259 if (conn->fd >= 0)
260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 os_close_file(conn->fd);
262 kfree(conn);
263 }
264
265 fd = conn->fd;
266 dev->helper_pid = conn->helper_pid;
267 dev->telnetd_pid = conn->telnetd_pid;
268 kfree(conn);
269 out:
270 atomic_dec(&port->wait_count);
271 return fd;
272}
273
274void port_remove_dev(void *d)
275{
276 struct port_dev *dev = d;
277
Jeff Dikee99525f2007-10-16 01:26:41 -0700278 if (dev->helper_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 os_kill_process(dev->helper_pid, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700280 if (dev->telnetd_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 os_kill_process(dev->telnetd_pid, 1);
282 dev->helper_pid = -1;
283 dev->telnetd_pid = -1;
284}
285
286void port_kern_free(void *d)
287{
288 struct port_dev *dev = d;
289
290 port_remove_dev(dev);
291 kfree(dev);
292}
293
294static void free_port(void)
295{
296 struct list_head *ele;
297 struct port_list *port;
298
Jeff Dikee99525f2007-10-16 01:26:41 -0700299 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 port = list_entry(ele, struct port_list, list);
301 free_irq_by_fd(port->fd);
302 os_close_file(port->fd);
303 }
304}
305
306__uml_exitcall(free_port);