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 "linux/mutex.h"
10 #include "asm/atomic.h"
17 struct list_head list
;
20 struct completion done
;
24 struct list_head pending
;
25 struct list_head connections
;
29 struct port_list
*port
;
35 struct list_head list
;
40 struct port_list
*port
;
43 static irqreturn_t
pipe_interrupt(int irq
, void *data
)
45 struct connection
*conn
= data
;
48 fd
= os_rcv_fd(conn
->socket
[0], &conn
->helper_pid
);
53 printk(KERN_ERR
"pipe_interrupt : os_rcv_fd returned %d\n",
55 os_close_file(conn
->fd
);
58 list_del(&conn
->list
);
61 list_add(&conn
->list
, &conn
->port
->connections
);
63 complete(&conn
->port
->done
);
67 #define NO_WAITER_MSG \
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" \
74 static int port_accept(struct port_list
*port
)
76 struct connection
*conn
;
77 int fd
, socket
[2], pid
;
79 fd
= port_connection(port
->fd
, socket
, &pid
);
82 printk(KERN_ERR
"port_accept : port_connection "
83 "returned %d\n", -fd
);
87 conn
= kmalloc(sizeof(*conn
), GFP_ATOMIC
);
89 printk(KERN_ERR
"port_accept : failed to allocate "
93 *conn
= ((struct connection
)
94 { .list
= LIST_HEAD_INIT(conn
->list
),
96 .socket
= { socket
[0], socket
[1] },
100 if (um_request_irq(TELNETD_IRQ
, socket
[0], IRQ_READ
, pipe_interrupt
,
101 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
103 printk(KERN_ERR
"port_accept : failed to get IRQ for "
108 if (atomic_read(&port
->wait_count
) == 0) {
109 os_write_file(fd
, NO_WAITER_MSG
, sizeof(NO_WAITER_MSG
));
110 printk(KERN_ERR
"No one waiting for port\n");
112 list_add(&conn
->list
, &port
->pending
);
119 os_kill_process(pid
, 1);
124 static DEFINE_MUTEX(ports_mutex
);
125 static LIST_HEAD(ports
);
127 static void port_work_proc(struct work_struct
*unused
)
129 struct port_list
*port
;
130 struct list_head
*ele
;
133 local_irq_save(flags
);
134 list_for_each(ele
, &ports
) {
135 port
= list_entry(ele
, struct port_list
, list
);
136 if (!port
->has_connection
)
139 reactivate_fd(port
->fd
, ACCEPT_IRQ
);
140 while (port_accept(port
))
142 port
->has_connection
= 0;
144 local_irq_restore(flags
);
147 DECLARE_WORK(port_work
, port_work_proc
);
149 static irqreturn_t
port_interrupt(int irq
, void *data
)
151 struct port_list
*port
= data
;
153 port
->has_connection
= 1;
154 schedule_work(&port_work
);
158 void *port_data(int port_num
)
160 struct list_head
*ele
;
161 struct port_list
*port
;
162 struct port_dev
*dev
= NULL
;
165 mutex_lock(&ports_mutex
);
166 list_for_each(ele
, &ports
) {
167 port
= list_entry(ele
, struct port_list
, list
);
168 if (port
->port
== port_num
)
171 port
= kmalloc(sizeof(struct port_list
), GFP_KERNEL
);
173 printk(KERN_ERR
"Allocation of port list failed\n");
177 fd
= port_listen_fd(port_num
);
179 printk(KERN_ERR
"binding to port %d failed, errno = %d\n",
184 if (um_request_irq(ACCEPT_IRQ
, fd
, IRQ_READ
, port_interrupt
,
185 IRQF_DISABLED
| IRQF_SHARED
| IRQF_SAMPLE_RANDOM
,
187 printk(KERN_ERR
"Failed to get IRQ for port %d\n", port_num
);
191 *port
= ((struct port_list
)
192 { .list
= LIST_HEAD_INIT(port
->list
),
193 .wait_count
= ATOMIC_INIT(0),
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
);
204 dev
= kmalloc(sizeof(struct port_dev
), GFP_KERNEL
);
206 printk(KERN_ERR
"Allocation of port device entry failed\n");
210 *dev
= ((struct port_dev
) { .port
= port
,
212 .telnetd_pid
= -1 });
220 mutex_unlock(&ports_mutex
);
224 int port_wait(void *data
)
226 struct port_dev
*dev
= data
;
227 struct connection
*conn
;
228 struct port_list
*port
= dev
->port
;
231 atomic_inc(&port
->wait_count
);
234 if (wait_for_completion_interruptible(&port
->done
))
237 spin_lock(&port
->lock
);
239 conn
= list_entry(port
->connections
.next
, struct connection
,
241 list_del(&conn
->list
);
242 spin_unlock(&port
->lock
);
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);
247 os_close_file(conn
->socket
[1]);
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
252 * connection. Then we loop here throwing out failed
253 * connections until a good one is found.
255 free_irq(TELNETD_IRQ
, conn
);
259 os_close_file(conn
->fd
);
264 dev
->helper_pid
= conn
->helper_pid
;
265 dev
->telnetd_pid
= conn
->telnetd_pid
;
268 atomic_dec(&port
->wait_count
);
272 void port_remove_dev(void *d
)
274 struct port_dev
*dev
= d
;
276 if (dev
->helper_pid
!= -1)
277 os_kill_process(dev
->helper_pid
, 0);
278 if (dev
->telnetd_pid
!= -1)
279 os_kill_process(dev
->telnetd_pid
, 1);
280 dev
->helper_pid
= -1;
281 dev
->telnetd_pid
= -1;
284 void port_kern_free(void *d
)
286 struct port_dev
*dev
= d
;
288 port_remove_dev(dev
);
292 static void free_port(void)
294 struct list_head
*ele
;
295 struct port_list
*port
;
297 list_for_each(ele
, &ports
) {
298 port
= list_entry(ele
, struct port_list
, list
);
299 free_irq_by_fd(port
->fd
);
300 os_close_file(port
->fd
);
304 __uml_exitcall(free_port
);