2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3 * Licensed under the GPL
6 #include "linux/completion.h"
7 #include "linux/interrupt.h"
8 #include "linux/list.h"
9 #include "asm/atomic.h"
16 struct list_head list
;
19 struct completion done
;
23 struct list_head pending
;
24 struct list_head connections
;
28 struct port_list
*port
;
34 struct list_head list
;
39 struct port_list
*port
;
42 static irqreturn_t
pipe_interrupt(int irq
, void *data
)
44 struct connection
*conn
= data
;
47 fd
= os_rcv_fd(conn
->socket
[0], &conn
->helper_pid
);
52 printk(KERN_ERR
"pipe_interrupt : os_rcv_fd returned %d\n",
54 os_close_file(conn
->fd
);
57 list_del(&conn
->list
);
60 list_add(&conn
->list
, &conn
->port
->connections
);
62 complete(&conn
->port
->done
);
66 #define NO_WAITER_MSG \
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" \
73 static int port_accept(struct port_list
*port
)
75 struct connection
*conn
;
76 int fd
, socket
[2], pid
;
78 fd
= port_connection(port
->fd
, socket
, &pid
);
81 printk(KERN_ERR
"port_accept : port_connection "
82 "returned %d\n", -fd
);
86 conn
= kmalloc(sizeof(*conn
), GFP_ATOMIC
);
88 printk(KERN_ERR
"port_accept : failed to allocate "
92 *conn
= ((struct connection
)
93 { .list
= LIST_HEAD_INIT(conn
->list
),
95 .socket
= { socket
[0], socket
[1] },
99 if (um_request_irq(TELNETD_IRQ
, socket
[0], IRQ_READ
, pipe_interrupt
,
100 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
102 printk(KERN_ERR
"port_accept : failed to get IRQ for "
107 if (atomic_read(&port
->wait_count
) == 0) {
108 os_write_file(fd
, NO_WAITER_MSG
, sizeof(NO_WAITER_MSG
));
109 printk(KERN_ERR
"No one waiting for port\n");
111 list_add(&conn
->list
, &port
->pending
);
118 os_kill_process(pid
, 1);
123 static DECLARE_MUTEX(ports_sem
);
124 static LIST_HEAD(ports
);
126 static void port_work_proc(struct work_struct
*unused
)
128 struct port_list
*port
;
129 struct list_head
*ele
;
132 local_irq_save(flags
);
133 list_for_each(ele
, &ports
) {
134 port
= list_entry(ele
, struct port_list
, list
);
135 if (!port
->has_connection
)
138 reactivate_fd(port
->fd
, ACCEPT_IRQ
);
139 while (port_accept(port
))
141 port
->has_connection
= 0;
143 local_irq_restore(flags
);
146 DECLARE_WORK(port_work
, port_work_proc
);
148 static irqreturn_t
port_interrupt(int irq
, void *data
)
150 struct port_list
*port
= data
;
152 port
->has_connection
= 1;
153 schedule_work(&port_work
);
157 void *port_data(int port_num
)
159 struct list_head
*ele
;
160 struct port_list
*port
;
161 struct port_dev
*dev
= NULL
;
165 list_for_each(ele
, &ports
) {
166 port
= list_entry(ele
, struct port_list
, list
);
167 if (port
->port
== port_num
)
170 port
= kmalloc(sizeof(struct port_list
), GFP_KERNEL
);
172 printk(KERN_ERR
"Allocation of port list failed\n");
176 fd
= port_listen_fd(port_num
);
178 printk(KERN_ERR
"binding to port %d failed, errno = %d\n",
183 if (um_request_irq(ACCEPT_IRQ
, fd
, IRQ_READ
, port_interrupt
,
184 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
186 printk(KERN_ERR
"Failed to get IRQ for port %d\n", port_num
);
190 *port
= ((struct port_list
)
191 { .list
= LIST_HEAD_INIT(port
->list
),
192 .wait_count
= ATOMIC_INIT(0),
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
);
203 dev
= kmalloc(sizeof(struct port_dev
), GFP_KERNEL
);
205 printk(KERN_ERR
"Allocation of port device entry failed\n");
209 *dev
= ((struct port_dev
) { .port
= port
,
211 .telnetd_pid
= -1 });
223 int port_wait(void *data
)
225 struct port_dev
*dev
= data
;
226 struct connection
*conn
;
227 struct port_list
*port
= dev
->port
;
230 atomic_inc(&port
->wait_count
);
233 if (wait_for_completion_interruptible(&port
->done
))
236 spin_lock(&port
->lock
);
238 conn
= list_entry(port
->connections
.next
, struct connection
,
240 list_del(&conn
->list
);
241 spin_unlock(&port
->lock
);
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);
246 os_close_file(conn
->socket
[1]);
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
251 * connection. Then we loop here throwing out failed
252 * connections until a good one is found.
254 free_irq(TELNETD_IRQ
, conn
);
258 os_close_file(conn
->fd
);
263 dev
->helper_pid
= conn
->helper_pid
;
264 dev
->telnetd_pid
= conn
->telnetd_pid
;
267 atomic_dec(&port
->wait_count
);
271 void port_remove_dev(void *d
)
273 struct port_dev
*dev
= d
;
275 if (dev
->helper_pid
!= -1)
276 os_kill_process(dev
->helper_pid
, 0);
277 if (dev
->telnetd_pid
!= -1)
278 os_kill_process(dev
->telnetd_pid
, 1);
279 dev
->helper_pid
= -1;
280 dev
->telnetd_pid
= -1;
283 void port_kern_free(void *d
)
285 struct port_dev
*dev
= d
;
287 port_remove_dev(dev
);
291 static void free_port(void)
293 struct list_head
*ele
;
294 struct port_list
*port
;
296 list_for_each(ele
, &ports
) {
297 port
= list_entry(ele
, struct port_list
, list
);
298 free_irq_by_fd(port
->fd
);
299 os_close_file(port
->fd
);
303 __uml_exitcall(free_port
);