blob: 19930081d3d8e93c42c7c0cf3d8be783ede5c304 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "asm/atomic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "init.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070012#include "irq_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "os.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070014#include "port.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct 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
28struct port_dev {
29 struct port_list *port;
30 int helper_pid;
31 int telnetd_pid;
32};
33
34struct connection {
35 struct list_head list;
36 int fd;
37 int helper_pid;
38 int socket[2];
39 int telnetd_pid;
40 struct port_list *port;
41};
42
Al Viro7bea96f2006-10-08 22:49:34 +010043static irqreturn_t pipe_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct connection *conn = data;
46 int fd;
47
48 fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
Jeff Dikee99525f2007-10-16 01:26:41 -070049 if (fd < 0) {
50 if (fd == -EAGAIN)
Jeff Dike67608e02007-02-10 01:44:02 -080051 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jeff Dike67608e02007-02-10 01:44:02 -080053 printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 -fd);
55 os_close_file(conn->fd);
56 }
57
58 list_del(&conn->list);
59
60 conn->fd = fd;
61 list_add(&conn->list, &conn->port->connections);
62
63 complete(&conn->port->done);
Jeff Dike67608e02007-02-10 01:44:02 -080064 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67#define NO_WAITER_MSG \
68 "****\n" \
69 "There are currently no UML consoles waiting for port connections.\n" \
70 "Either disconnect from one to make it available or activate some more\n" \
71 "by enabling more consoles in the UML /etc/inittab.\n" \
72 "****\n"
73
74static int port_accept(struct port_list *port)
75{
76 struct connection *conn;
Jeff Dikee99525f2007-10-16 01:26:41 -070077 int fd, socket[2], pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 fd = port_connection(port->fd, socket, &pid);
Jeff Dikee99525f2007-10-16 01:26:41 -070080 if (fd < 0) {
81 if (fd != -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 printk(KERN_ERR "port_accept : port_connection "
83 "returned %d\n", -fd);
84 goto out;
85 }
86
87 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Jeff Dikee99525f2007-10-16 01:26:41 -070088 if (conn == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 printk(KERN_ERR "port_accept : failed to allocate "
90 "connection\n");
91 goto out_close;
92 }
Jeff Dike67608e02007-02-10 01:44:02 -080093 *conn = ((struct connection)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 { .list = LIST_HEAD_INIT(conn->list),
95 .fd = fd,
96 .socket = { socket[0], socket[1] },
97 .telnetd_pid = pid,
98 .port = port });
99
Jeff Dikee99525f2007-10-16 01:26:41 -0700100 if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700101 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dikee99525f2007-10-16 01:26:41 -0700102 "telnetd", conn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 printk(KERN_ERR "port_accept : failed to get IRQ for "
104 "telnetd\n");
105 goto out_free;
106 }
107
Jeff Dikee99525f2007-10-16 01:26:41 -0700108 if (atomic_read(&port->wait_count) == 0) {
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700109 os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
Jeff Dikee99525f2007-10-16 01:26:41 -0700110 printk(KERN_ERR "No one waiting for port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 list_add(&conn->list, &port->pending);
Jeff Dike67608e02007-02-10 01:44:02 -0800113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 out_free:
116 kfree(conn);
117 out_close:
118 os_close_file(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -0700119 os_kill_process(pid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700121 return 0;
Jeff Dike67608e02007-02-10 01:44:02 -0800122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800124static DEFINE_MUTEX(ports_mutex);
Jeff Dikec59bce62007-02-10 01:44:04 -0800125static LIST_HEAD(ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jeff Dikee99525f2007-10-16 01:26:41 -0700127static void port_work_proc(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct port_list *port;
130 struct list_head *ele;
131 unsigned long flags;
132
133 local_irq_save(flags);
Jeff Dikee99525f2007-10-16 01:26:41 -0700134 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 port = list_entry(ele, struct port_list, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700136 if (!port->has_connection)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 continue;
Jeff Dikee99525f2007-10-16 01:26:41 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 reactivate_fd(port->fd, ACCEPT_IRQ);
Jeff Dikee99525f2007-10-16 01:26:41 -0700140 while (port_accept(port))
141 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 port->has_connection = 0;
143 }
144 local_irq_restore(flags);
145}
146
David Howells6d5aefb2006-12-05 19:36:26 +0000147DECLARE_WORK(port_work, port_work_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Al Viro7bea96f2006-10-08 22:49:34 +0100149static irqreturn_t port_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct port_list *port = data;
152
153 port->has_connection = 1;
154 schedule_work(&port_work);
Jeff Dike67608e02007-02-10 01:44:02 -0800155 return IRQ_HANDLED;
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158void *port_data(int port_num)
159{
160 struct list_head *ele;
161 struct port_list *port;
162 struct port_dev *dev = NULL;
163 int fd;
164
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800165 mutex_lock(&ports_mutex);
Jeff Dikee99525f2007-10-16 01:26:41 -0700166 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 port = list_entry(ele, struct port_list, list);
Jeff Dikee99525f2007-10-16 01:26:41 -0700168 if (port->port == port_num)
169 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -0700172 if (port == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 printk(KERN_ERR "Allocation of port list failed\n");
174 goto out;
175 }
176
177 fd = port_listen_fd(port_num);
Jeff Dikee99525f2007-10-16 01:26:41 -0700178 if (fd < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk(KERN_ERR "binding to port %d failed, errno = %d\n",
180 port_num, -fd);
181 goto out_free;
182 }
Jeff Dikee99525f2007-10-16 01:26:41 -0700183
184 if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
Jeff Dike67608e02007-02-10 01:44:02 -0800185 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dikee99525f2007-10-16 01:26:41 -0700186 "port", port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
188 goto out_close;
189 }
190
Jeff Dike67608e02007-02-10 01:44:02 -0800191 *port = ((struct port_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 { .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 Dikee99525f2007-10-16 01:26:41 -0700205 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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 Torvalds1da177e2005-04-16 15:20:36 -0700215 out_close:
216 os_close_file(fd);
Jeff Dike79f66232007-10-16 01:26:40 -0700217 out_free:
218 kfree(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 out:
Daniel Walker2aa9c5d2008-02-04 22:31:27 -0800220 mutex_unlock(&ports_mutex);
Jeff Dike67608e02007-02-10 01:44:02 -0800221 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224int 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 Dike67608e02007-02-10 01:44:02 -0800231 atomic_inc(&port->wait_count);
Jeff Dikee99525f2007-10-16 01:26:41 -0700232 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 fd = -ERESTARTSYS;
Jeff Dikee99525f2007-10-16 01:26:41 -0700234 if (wait_for_completion_interruptible(&port->done))
Jeff Dike67608e02007-02-10 01:44:02 -0800235 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 spin_lock(&port->lock);
238
Jeff Dike67608e02007-02-10 01:44:02 -0800239 conn = list_entry(port->connections.next, struct connection,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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 Dike67608e02007-02-10 01:44:02 -0800247 os_close_file(conn->socket[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
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 Dike67608e02007-02-10 01:44:02 -0800252 * connection. Then we loop here throwing out failed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * connections until a good one is found.
254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 free_irq(TELNETD_IRQ, conn);
256
Jeff Dikee99525f2007-10-16 01:26:41 -0700257 if (conn->fd >= 0)
258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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
272void port_remove_dev(void *d)
273{
274 struct port_dev *dev = d;
275
Jeff Dikee99525f2007-10-16 01:26:41 -0700276 if (dev->helper_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 os_kill_process(dev->helper_pid, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700278 if (dev->telnetd_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 os_kill_process(dev->telnetd_pid, 1);
280 dev->helper_pid = -1;
281 dev->telnetd_pid = -1;
282}
283
284void port_kern_free(void *d)
285{
286 struct port_dev *dev = d;
287
288 port_remove_dev(dev);
289 kfree(dev);
290}
291
292static void free_port(void)
293{
294 struct list_head *ele;
295 struct port_list *port;
296
Jeff Dikee99525f2007-10-16 01:26:41 -0700297 list_for_each(ele, &ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 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);