]>
Commit | Line | Data |
---|---|---|
67608e0c | 1 | /* |
e99525f9 | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com) |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
e99525f9 | 6 | #include "linux/completion.h" |
1da177e4 | 7 | #include "linux/interrupt.h" |
e99525f9 | 8 | #include "linux/list.h" |
2aa9c5db | 9 | #include "linux/mutex.h" |
5a0e3ad6 | 10 | #include "linux/slab.h" |
d43c36dc | 11 | #include "linux/workqueue.h" |
1da177e4 | 12 | #include "asm/atomic.h" |
1da177e4 | 13 | #include "init.h" |
e99525f9 | 14 | #include "irq_kern.h" |
1da177e4 | 15 | #include "os.h" |
e99525f9 | 16 | #include "port.h" |
1da177e4 LT |
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 | ||
7bea96fd | 45 | static irqreturn_t pipe_interrupt(int irq, void *data) |
1da177e4 LT |
46 | { |
47 | struct connection *conn = data; | |
48 | int fd; | |
49 | ||
50 | fd = os_rcv_fd(conn->socket[0], &conn->helper_pid); | |
e99525f9 JD |
51 | if (fd < 0) { |
52 | if (fd == -EAGAIN) | |
67608e0c | 53 | return IRQ_NONE; |
1da177e4 | 54 | |
67608e0c | 55 | printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n", |
1da177e4 LT |
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); | |
67608e0c | 66 | return IRQ_HANDLED; |
1da177e4 LT |
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; | |
e99525f9 | 79 | int fd, socket[2], pid; |
1da177e4 LT |
80 | |
81 | fd = port_connection(port->fd, socket, &pid); | |
e99525f9 JD |
82 | if (fd < 0) { |
83 | if (fd != -EAGAIN) | |
1da177e4 LT |
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); | |
e99525f9 | 90 | if (conn == NULL) { |
1da177e4 LT |
91 | printk(KERN_ERR "port_accept : failed to allocate " |
92 | "connection\n"); | |
93 | goto out_close; | |
94 | } | |
67608e0c | 95 | *conn = ((struct connection) |
1da177e4 LT |
96 | { .list = LIST_HEAD_INIT(conn->list), |
97 | .fd = fd, | |
98 | .socket = { socket[0], socket[1] }, | |
99 | .telnetd_pid = pid, | |
100 | .port = port }); | |
101 | ||
e99525f9 | 102 | if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt, |
bd6aa650 | 103 | IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
e99525f9 | 104 | "telnetd", conn)) { |
1da177e4 LT |
105 | printk(KERN_ERR "port_accept : failed to get IRQ for " |
106 | "telnetd\n"); | |
107 | goto out_free; | |
108 | } | |
109 | ||
e99525f9 | 110 | if (atomic_read(&port->wait_count) == 0) { |
a6ea4cce | 111 | os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG)); |
e99525f9 | 112 | printk(KERN_ERR "No one waiting for port\n"); |
1da177e4 LT |
113 | } |
114 | list_add(&conn->list, &port->pending); | |
67608e0c | 115 | return 1; |
1da177e4 LT |
116 | |
117 | out_free: | |
118 | kfree(conn); | |
119 | out_close: | |
120 | os_close_file(fd); | |
e99525f9 | 121 | os_kill_process(pid, 1); |
1da177e4 | 122 | out: |
e99525f9 | 123 | return 0; |
67608e0c | 124 | } |
1da177e4 | 125 | |
2aa9c5db | 126 | static DEFINE_MUTEX(ports_mutex); |
c59bce62 | 127 | static LIST_HEAD(ports); |
1da177e4 | 128 | |
e99525f9 | 129 | static void port_work_proc(struct work_struct *unused) |
1da177e4 LT |
130 | { |
131 | struct port_list *port; | |
132 | struct list_head *ele; | |
133 | unsigned long flags; | |
134 | ||
135 | local_irq_save(flags); | |
e99525f9 | 136 | list_for_each(ele, &ports) { |
1da177e4 | 137 | port = list_entry(ele, struct port_list, list); |
e99525f9 | 138 | if (!port->has_connection) |
1da177e4 | 139 | continue; |
e99525f9 | 140 | |
1da177e4 | 141 | reactivate_fd(port->fd, ACCEPT_IRQ); |
e99525f9 JD |
142 | while (port_accept(port)) |
143 | ; | |
1da177e4 LT |
144 | port->has_connection = 0; |
145 | } | |
146 | local_irq_restore(flags); | |
147 | } | |
148 | ||
6d5aefb8 | 149 | DECLARE_WORK(port_work, port_work_proc); |
1da177e4 | 150 | |
7bea96fd | 151 | static irqreturn_t port_interrupt(int irq, void *data) |
1da177e4 LT |
152 | { |
153 | struct port_list *port = data; | |
154 | ||
155 | port->has_connection = 1; | |
156 | schedule_work(&port_work); | |
67608e0c JD |
157 | return IRQ_HANDLED; |
158 | } | |
1da177e4 LT |
159 | |
160 | void *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 | ||
2aa9c5db | 167 | mutex_lock(&ports_mutex); |
e99525f9 | 168 | list_for_each(ele, &ports) { |
1da177e4 | 169 | port = list_entry(ele, struct port_list, list); |
e99525f9 JD |
170 | if (port->port == port_num) |
171 | goto found; | |
1da177e4 LT |
172 | } |
173 | port = kmalloc(sizeof(struct port_list), GFP_KERNEL); | |
e99525f9 | 174 | if (port == NULL) { |
1da177e4 LT |
175 | printk(KERN_ERR "Allocation of port list failed\n"); |
176 | goto out; | |
177 | } | |
178 | ||
179 | fd = port_listen_fd(port_num); | |
e99525f9 | 180 | if (fd < 0) { |
1da177e4 LT |
181 | printk(KERN_ERR "binding to port %d failed, errno = %d\n", |
182 | port_num, -fd); | |
183 | goto out_free; | |
184 | } | |
e99525f9 JD |
185 | |
186 | if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt, | |
67608e0c | 187 | IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
e99525f9 | 188 | "port", port)) { |
1da177e4 LT |
189 | printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num); |
190 | goto out_close; | |
191 | } | |
192 | ||
67608e0c | 193 | *port = ((struct port_list) |
1da177e4 LT |
194 | { .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); | |
e99525f9 | 207 | if (dev == NULL) { |
1da177e4 LT |
208 | 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 | ||
1da177e4 LT |
217 | out_close: |
218 | os_close_file(fd); | |
79f66233 JD |
219 | out_free: |
220 | kfree(port); | |
1da177e4 | 221 | out: |
2aa9c5db | 222 | mutex_unlock(&ports_mutex); |
67608e0c | 223 | return dev; |
1da177e4 LT |
224 | } |
225 | ||
226 | int 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 | ||
67608e0c | 233 | atomic_inc(&port->wait_count); |
e99525f9 | 234 | while (1) { |
1da177e4 | 235 | fd = -ERESTARTSYS; |
e99525f9 | 236 | if (wait_for_completion_interruptible(&port->done)) |
67608e0c | 237 | goto out; |
1da177e4 LT |
238 | |
239 | spin_lock(&port->lock); | |
240 | ||
67608e0c | 241 | conn = list_entry(port->connections.next, struct connection, |
1da177e4 LT |
242 | 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); | |
67608e0c | 249 | os_close_file(conn->socket[1]); |
1da177e4 LT |
250 | |
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 | |
67608e0c | 254 | * connection. Then we loop here throwing out failed |
1da177e4 LT |
255 | * connections until a good one is found. |
256 | */ | |
1da177e4 LT |
257 | free_irq(TELNETD_IRQ, conn); |
258 | ||
e99525f9 JD |
259 | if (conn->fd >= 0) |
260 | break; | |
1da177e4 LT |
261 | 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 | ||
274 | void port_remove_dev(void *d) | |
275 | { | |
276 | struct port_dev *dev = d; | |
277 | ||
e99525f9 | 278 | if (dev->helper_pid != -1) |
1da177e4 | 279 | os_kill_process(dev->helper_pid, 0); |
e99525f9 | 280 | if (dev->telnetd_pid != -1) |
1da177e4 LT |
281 | os_kill_process(dev->telnetd_pid, 1); |
282 | dev->helper_pid = -1; | |
283 | dev->telnetd_pid = -1; | |
284 | } | |
285 | ||
286 | void port_kern_free(void *d) | |
287 | { | |
288 | struct port_dev *dev = d; | |
289 | ||
290 | port_remove_dev(dev); | |
291 | kfree(dev); | |
292 | } | |
293 | ||
294 | static void free_port(void) | |
295 | { | |
296 | struct list_head *ele; | |
297 | struct port_list *port; | |
298 | ||
e99525f9 | 299 | list_for_each(ele, &ports) { |
1da177e4 LT |
300 | 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); |