port_kern.c 6.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 
 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

#include "linux/list.h"
#include "linux/sched.h"
#include "linux/slab.h"
#include "linux/irq.h"
#include "linux/spinlock.h"
#include "linux/errno.h"
#include "asm/semaphore.h"
#include "asm/errno.h"
#include "kern_util.h"
#include "kern.h"
#include "irq_user.h"
#include "port.h"
#include "init.h"
#include "os.h"

struct port_list {
	struct list_head list;
23
	int has_connection;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	struct semaphore sem;
	int port;
	int fd;
	spinlock_t lock;
	struct list_head pending;
	struct list_head connections;
};

struct port_dev {
	struct port_list *port;
	int fd;
 	int helper_pid;
 	int telnetd_pid;
};

struct connection {
	struct list_head list;
	int fd;
 	int helper_pid;
	int socket[2];
	int telnetd_pid;
	struct port_list *port;
};

static void pipe_interrupt(int irq, void *data, struct pt_regs *regs)
{
	struct connection *conn = data;
	int fd;

 	fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
	if(fd < 0){
Jeff Dike's avatar
Jeff Dike committed
55 56 57
		if(fd == -EAGAIN)
			return;

58
		printk("os_rcv_fd returned %d\n", -fd);
59 60
		os_close_file(conn->fd);
	}
Jeff Dike's avatar
Jeff Dike committed
61 62 63

	list_del(&conn->list);

64 65 66 67 68 69
	conn->fd = fd;
	list_add(&conn->list, &conn->port->connections);

	up(&conn->port->sem);
}

70
static int port_accept(struct port_list *port)
71 72
{
	struct connection *conn;
73
	int fd, socket[2], pid, ret = 0;
74 75 76

	fd = port_connection(port->fd, socket, &pid);
	if(fd < 0){
77 78
		if(fd != -EAGAIN)
			printk("port_connection returned %d\n", -fd);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		goto out;
	}

	conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
	if(conn == NULL){
		printk("port_interrupt : failed to allocate connection\n");
		goto out_close;
	}
	*conn = ((struct connection) 
		{ list :	LIST_HEAD_INIT(conn->list),
		  fd :		fd,
		  socket : 	{ socket[0], socket[1] },
		  telnetd_pid :	pid,
		  port :	port });

	if(um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt, 
			  SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, 
			  "telnetd", conn)){
		printk(KERN_ERR "Failed to get IRQ for telnetd\n");
		goto out_free;
	}

	list_add(&conn->list, &port->pending);
102
	ret = 1;
103 104 105 106 107 108
	goto out;

 out_free:
	kfree(conn);
 out_close:
	os_close_file(fd);
109
	if(pid != -1) os_kill_process(pid, 0);
110
 out:
111
	return(ret);
112 113
} 

114 115 116
DECLARE_MUTEX(ports_sem);
struct list_head ports = LIST_HEAD_INIT(ports);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
void port_work_proc(void *unused)
{
	struct port_list *port;
	struct list_head *ele;
	unsigned long flags;

	local_irq_save(flags);
	list_for_each(ele, &ports){
		port = list_entry(ele, struct port_list, list);
		if(!port->has_connection)
			continue;
		reactivate_fd(port->fd, ACCEPT_IRQ);
		while(port_accept(port)) ;
		port->has_connection = 0;
	}
	local_irq_restore(flags);
}

DECLARE_WORK(port_work, port_work_proc, NULL);

static void port_interrupt(int irq, void *data, struct pt_regs *regs)
{
	struct port_list *port = data;

	port->has_connection = 1;
	schedule_work(&port_work);
} 

145 146 147 148
void *port_data(int port_num)
{
	struct list_head *ele;
	struct port_list *port;
149
	struct port_dev *dev = NULL;
150 151
	int fd;

152
	down(&ports_sem);
153 154 155 156 157 158 159
	list_for_each(ele, &ports){
		port = list_entry(ele, struct port_list, list);
		if(port->port == port_num) goto found;
	}
	port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
	if(port == NULL){
		printk(KERN_ERR "Allocation of port list failed\n");
160
		goto out;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	}

	fd = port_listen_fd(port_num);
	if(fd < 0){
		printk(KERN_ERR "binding to port %d failed, errno = %d\n",
		       port_num, -fd);
		goto out_free;
	}
	if(um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt, 
			  SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, "port",
			  port)){
		printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
		goto out_close;
	}

	*port = ((struct port_list) 
177 178 179 180 181 182 183 184
		{ list :	 	LIST_HEAD_INIT(port->list),
		  has_connection :	0,
		  sem :			__SEMAPHORE_INITIALIZER(port->sem, 0),
		  lock :		SPIN_LOCK_UNLOCKED,
		  port :	 	port_num,
		  fd : 			fd,
		  pending :		LIST_HEAD_INIT(port->pending),
		  connections :		LIST_HEAD_INIT(port->connections) });
185 186 187 188 189 190
	list_add(&port->list, &ports);

 found:
	dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
	if(dev == NULL){
		printk(KERN_ERR "Allocation of port device entry failed\n");
191
		goto out;
192 193
	}

194 195 196
	*dev = ((struct port_dev) { port : 		port,
				    fd :		-1,
				    helper_pid : 	-1 });
197
	goto out;
198 199 200 201 202

 out_free:
	kfree(port);
 out_close:
	os_close_file(fd);
203 204
 out:
	up(&ports_sem);
205
	return(dev);
206 207 208 209 210 211 212
}

void port_remove_dev(void *d)
{
	struct port_dev *dev = d;

  	if(dev->helper_pid != -1)
213
 		os_kill_process(dev->helper_pid, 0);
214
 	if(dev->telnetd_pid != -1)
215
 		os_kill_process(dev->telnetd_pid, 0);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
 	dev->helper_pid = -1;
}

static void free_port(void)
{
	struct list_head *ele;
	struct port_list *port;

	list_for_each(ele, &ports){
		port = list_entry(ele, struct port_list, list);
		os_close_file(port->fd);
	}
}

__uml_exitcall(free_port);

int port_wait(void *data)
{
	struct port_dev *dev = data;
	struct connection *conn;
	struct port_list *port = dev->port;

	while(1){
		if(down_interruptible(&port->sem)) return(-ERESTARTSYS);

		spin_lock(&port->lock);

		conn = list_entry(port->connections.next, struct connection, 
				  list);
		list_del(&conn->list);
		spin_unlock(&port->lock);

		os_shutdown_socket(conn->socket[0], 1, 1);
		os_close_file(conn->socket[0]);
		os_shutdown_socket(conn->socket[1], 1, 1);
		os_close_file(conn->socket[1]);	

		/* This is done here because freeing an IRQ can't be done
		 * within the IRQ handler.  So, pipe_interrupt always ups
		 * the semaphore regardless of whether it got a successful
		 * connection.  Then we loop here throwing out failed 
		 * connections until a good one is found.
		 */
		free_irq(TELNETD_IRQ, conn);

		if(conn->fd >= 0) break;
		os_close_file(conn->fd);
		kfree(conn);
	}

	dev->fd = conn->fd;
	dev->helper_pid = conn->helper_pid;
	dev->telnetd_pid = conn->telnetd_pid;
	kfree(conn);

	return(dev->fd);
}

void port_kern_free(void *d)
{
	struct port_dev *dev = d;

	kfree(dev);
}

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */