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/spinlock.h"
11 #include "linux/errno.h"
12 #include "asm/atomic.h"
13 #include "asm/semaphore.h"
14 #include "asm/errno.h"
15 #include "kern_util.h"
24 struct list_head list
;
27 struct completion done
;
31 struct list_head pending
;
32 struct list_head connections
;
36 struct port_list
*port
;
42 struct list_head list
;
47 struct port_list
*port
;
50 static irqreturn_t
pipe_interrupt(int irq
, void *data
)
52 struct connection
*conn
= data
;
55 fd
= os_rcv_fd(conn
->socket
[0], &conn
->helper_pid
);
60 printk(KERN_ERR
"pipe_interrupt : os_rcv_fd returned %d\n",
62 os_close_file(conn
->fd
);
65 list_del(&conn
->list
);
68 list_add(&conn
->list
, &conn
->port
->connections
);
70 complete(&conn
->port
->done
);
74 #define NO_WAITER_MSG \
76 "There are currently no UML consoles waiting for port connections.\n" \
77 "Either disconnect from one to make it available or activate some more\n" \
78 "by enabling more consoles in the UML /etc/inittab.\n" \
81 static int port_accept(struct port_list
*port
)
83 struct connection
*conn
;
84 int fd
, socket
[2], pid
, ret
= 0;
86 fd
= port_connection(port
->fd
, socket
, &pid
);
89 printk(KERN_ERR
"port_accept : port_connection "
90 "returned %d\n", -fd
);
94 conn
= kmalloc(sizeof(*conn
), GFP_ATOMIC
);
96 printk(KERN_ERR
"port_accept : failed to allocate "
100 *conn
= ((struct connection
)
101 { .list
= LIST_HEAD_INIT(conn
->list
),
103 .socket
= { socket
[0], socket
[1] },
107 if(um_request_irq(TELNETD_IRQ
, socket
[0], IRQ_READ
, pipe_interrupt
,
108 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
110 printk(KERN_ERR
"port_accept : failed to get IRQ for "
115 if(atomic_read(&port
->wait_count
) == 0){
116 os_write_file(fd
, NO_WAITER_MSG
, sizeof(NO_WAITER_MSG
));
117 printk("No one waiting for port\n");
119 list_add(&conn
->list
, &port
->pending
);
127 os_kill_process(pid
, 1);
132 static DECLARE_MUTEX(ports_sem
);
133 static LIST_HEAD(ports
);
135 void port_work_proc(struct work_struct
*unused
)
137 struct port_list
*port
;
138 struct list_head
*ele
;
141 local_irq_save(flags
);
142 list_for_each(ele
, &ports
){
143 port
= list_entry(ele
, struct port_list
, list
);
144 if(!port
->has_connection
)
146 reactivate_fd(port
->fd
, ACCEPT_IRQ
);
147 while(port_accept(port
)) ;
148 port
->has_connection
= 0;
150 local_irq_restore(flags
);
153 DECLARE_WORK(port_work
, port_work_proc
);
155 static irqreturn_t
port_interrupt(int irq
, void *data
)
157 struct port_list
*port
= data
;
159 port
->has_connection
= 1;
160 schedule_work(&port_work
);
164 void *port_data(int port_num
)
166 struct list_head
*ele
;
167 struct port_list
*port
;
168 struct port_dev
*dev
= NULL
;
172 list_for_each(ele
, &ports
){
173 port
= list_entry(ele
, struct port_list
, list
);
174 if(port
->port
== port_num
) goto found
;
176 port
= kmalloc(sizeof(struct port_list
), GFP_KERNEL
);
178 printk(KERN_ERR
"Allocation of port list failed\n");
182 fd
= port_listen_fd(port_num
);
184 printk(KERN_ERR
"binding to port %d failed, errno = %d\n",
188 if(um_request_irq(ACCEPT_IRQ
, fd
, IRQ_READ
, port_interrupt
,
189 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
191 printk(KERN_ERR
"Failed to get IRQ for port %d\n", port_num
);
195 *port
= ((struct port_list
)
196 { .list
= LIST_HEAD_INIT(port
->list
),
197 .wait_count
= ATOMIC_INIT(0),
201 .pending
= LIST_HEAD_INIT(port
->pending
),
202 .connections
= LIST_HEAD_INIT(port
->connections
) });
203 spin_lock_init(&port
->lock
);
204 init_completion(&port
->done
);
205 list_add(&port
->list
, &ports
);
208 dev
= kmalloc(sizeof(struct port_dev
), GFP_KERNEL
);
210 printk(KERN_ERR
"Allocation of port device entry failed\n");
214 *dev
= ((struct port_dev
) { .port
= port
,
216 .telnetd_pid
= -1 });
228 int port_wait(void *data
)
230 struct port_dev
*dev
= data
;
231 struct connection
*conn
;
232 struct port_list
*port
= dev
->port
;
235 atomic_inc(&port
->wait_count
);
238 if(wait_for_completion_interruptible(&port
->done
))
241 spin_lock(&port
->lock
);
243 conn
= list_entry(port
->connections
.next
, struct connection
,
245 list_del(&conn
->list
);
246 spin_unlock(&port
->lock
);
248 os_shutdown_socket(conn
->socket
[0], 1, 1);
249 os_close_file(conn
->socket
[0]);
250 os_shutdown_socket(conn
->socket
[1], 1, 1);
251 os_close_file(conn
->socket
[1]);
253 /* This is done here because freeing an IRQ can't be done
254 * within the IRQ handler. So, pipe_interrupt always ups
255 * the semaphore regardless of whether it got a successful
256 * connection. Then we loop here throwing out failed
257 * connections until a good one is found.
259 free_irq(TELNETD_IRQ
, conn
);
261 if(conn
->fd
>= 0) break;
262 os_close_file(conn
->fd
);
267 dev
->helper_pid
= conn
->helper_pid
;
268 dev
->telnetd_pid
= conn
->telnetd_pid
;
271 atomic_dec(&port
->wait_count
);
275 void port_remove_dev(void *d
)
277 struct port_dev
*dev
= d
;
279 if(dev
->helper_pid
!= -1)
280 os_kill_process(dev
->helper_pid
, 0);
281 if(dev
->telnetd_pid
!= -1)
282 os_kill_process(dev
->telnetd_pid
, 1);
283 dev
->helper_pid
= -1;
284 dev
->telnetd_pid
= -1;
287 void port_kern_free(void *d
)
289 struct port_dev
*dev
= d
;
291 port_remove_dev(dev
);
295 static void free_port(void)
297 struct list_head
*ele
;
298 struct port_list
*port
;
300 list_for_each(ele
, &ports
){
301 port
= list_entry(ele
, struct port_list
, list
);
302 free_irq_by_fd(port
->fd
);
303 os_close_file(port
->fd
);
307 __uml_exitcall(free_port
);