2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/list.h"
7 #include "linux/sched.h"
8 #include "linux/slab.h"
9 #include "linux/interrupt.h"
10 #include "linux/irq.h"
11 #include "linux/spinlock.h"
12 #include "linux/errno.h"
13 #include "asm/atomic.h"
14 #include "asm/semaphore.h"
15 #include "asm/errno.h"
16 #include "kern_util.h"
25 struct list_head list
;
28 struct completion done
;
32 struct list_head pending
;
33 struct list_head connections
;
37 struct port_list
*port
;
43 struct list_head list
;
48 struct port_list
*port
;
51 static irqreturn_t
pipe_interrupt(int irq
, void *data
, struct pt_regs
*regs
)
53 struct connection
*conn
= data
;
56 fd
= os_rcv_fd(conn
->socket
[0], &conn
->helper_pid
);
61 printk(KERN_ERR
"pipe_interrupt : os_rcv_fd returned %d\n",
63 os_close_file(conn
->fd
);
66 list_del(&conn
->list
);
69 list_add(&conn
->list
, &conn
->port
->connections
);
71 complete(&conn
->port
->done
);
75 #define NO_WAITER_MSG \
77 "There are currently no UML consoles waiting for port connections.\n" \
78 "Either disconnect from one to make it available or activate some more\n" \
79 "by enabling more consoles in the UML /etc/inittab.\n" \
82 static int port_accept(struct port_list
*port
)
84 struct connection
*conn
;
85 int fd
, socket
[2], pid
, ret
= 0;
87 fd
= port_connection(port
->fd
, socket
, &pid
);
90 printk(KERN_ERR
"port_accept : port_connection "
91 "returned %d\n", -fd
);
95 conn
= kmalloc(sizeof(*conn
), GFP_ATOMIC
);
97 printk(KERN_ERR
"port_accept : failed to allocate "
101 *conn
= ((struct connection
)
102 { .list
= LIST_HEAD_INIT(conn
->list
),
104 .socket
= { socket
[0], socket
[1] },
108 if(um_request_irq(TELNETD_IRQ
, socket
[0], IRQ_READ
, pipe_interrupt
,
109 SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
,
111 printk(KERN_ERR
"port_accept : failed to get IRQ for "
116 if(atomic_read(&port
->wait_count
) == 0){
117 os_write_file(fd
, NO_WAITER_MSG
, sizeof(NO_WAITER_MSG
));
118 printk("No one waiting for port\n");
120 list_add(&conn
->list
, &port
->pending
);
128 os_kill_process(pid
, 1);
133 DECLARE_MUTEX(ports_sem
);
134 struct list_head ports
= LIST_HEAD_INIT(ports
);
136 void port_work_proc(void *unused
)
138 struct port_list
*port
;
139 struct list_head
*ele
;
142 local_irq_save(flags
);
143 list_for_each(ele
, &ports
){
144 port
= list_entry(ele
, struct port_list
, list
);
145 if(!port
->has_connection
)
147 reactivate_fd(port
->fd
, ACCEPT_IRQ
);
148 while(port_accept(port
)) ;
149 port
->has_connection
= 0;
151 local_irq_restore(flags
);
154 DECLARE_WORK(port_work
, port_work_proc
, NULL
);
156 static irqreturn_t
port_interrupt(int irq
, void *data
, struct pt_regs
*regs
)
158 struct port_list
*port
= data
;
160 port
->has_connection
= 1;
161 schedule_work(&port_work
);
165 void *port_data(int port_num
)
167 struct list_head
*ele
;
168 struct port_list
*port
;
169 struct port_dev
*dev
= NULL
;
173 list_for_each(ele
, &ports
){
174 port
= list_entry(ele
, struct port_list
, list
);
175 if(port
->port
== port_num
) goto found
;
177 port
= kmalloc(sizeof(struct port_list
), GFP_KERNEL
);
179 printk(KERN_ERR
"Allocation of port list failed\n");
183 fd
= port_listen_fd(port_num
);
185 printk(KERN_ERR
"binding to port %d failed, errno = %d\n",
189 if(um_request_irq(ACCEPT_IRQ
, fd
, IRQ_READ
, port_interrupt
,
190 SA_INTERRUPT
| SA_SHIRQ
| SA_SAMPLE_RANDOM
, "port",
192 printk(KERN_ERR
"Failed to get IRQ for port %d\n", port_num
);
196 *port
= ((struct port_list
)
197 { .list
= LIST_HEAD_INIT(port
->list
),
198 .wait_count
= ATOMIC_INIT(0),
202 .pending
= LIST_HEAD_INIT(port
->pending
),
203 .connections
= LIST_HEAD_INIT(port
->connections
) });
204 spin_lock_init(&port
->lock
);
205 init_completion(&port
->done
);
206 list_add(&port
->list
, &ports
);
209 dev
= kmalloc(sizeof(struct port_dev
), GFP_KERNEL
);
211 printk(KERN_ERR
"Allocation of port device entry failed\n");
215 *dev
= ((struct port_dev
) { .port
= port
,
217 .telnetd_pid
= -1 });
229 int port_wait(void *data
)
231 struct port_dev
*dev
= data
;
232 struct connection
*conn
;
233 struct port_list
*port
= dev
->port
;
236 atomic_inc(&port
->wait_count
);
239 if(wait_for_completion_interruptible(&port
->done
))
242 spin_lock(&port
->lock
);
244 conn
= list_entry(port
->connections
.next
, struct connection
,
246 list_del(&conn
->list
);
247 spin_unlock(&port
->lock
);
249 os_shutdown_socket(conn
->socket
[0], 1, 1);
250 os_close_file(conn
->socket
[0]);
251 os_shutdown_socket(conn
->socket
[1], 1, 1);
252 os_close_file(conn
->socket
[1]);
254 /* This is done here because freeing an IRQ can't be done
255 * within the IRQ handler. So, pipe_interrupt always ups
256 * the semaphore regardless of whether it got a successful
257 * connection. Then we loop here throwing out failed
258 * connections until a good one is found.
260 free_irq_by_irq_and_dev(TELNETD_IRQ
, conn
);
261 free_irq(TELNETD_IRQ
, conn
);
263 if(conn
->fd
>= 0) break;
264 os_close_file(conn
->fd
);
269 dev
->helper_pid
= conn
->helper_pid
;
270 dev
->telnetd_pid
= conn
->telnetd_pid
;
273 atomic_dec(&port
->wait_count
);
277 void port_remove_dev(void *d
)
279 struct port_dev
*dev
= d
;
281 if(dev
->helper_pid
!= -1)
282 os_kill_process(dev
->helper_pid
, 0);
283 if(dev
->telnetd_pid
!= -1)
284 os_kill_process(dev
->telnetd_pid
, 1);
285 dev
->helper_pid
= -1;
286 dev
->telnetd_pid
= -1;
289 void port_kern_free(void *d
)
291 struct port_dev
*dev
= d
;
293 port_remove_dev(dev
);
297 static void free_port(void)
299 struct list_head
*ele
;
300 struct port_list
*port
;
302 list_for_each(ele
, &ports
){
303 port
= list_entry(ele
, struct port_list
, list
);
304 free_irq_by_fd(port
->fd
);
305 os_close_file(port
->fd
);
309 __uml_exitcall(free_port
);