2 * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include "user_util.h"
17 #include "kern_util.h"
19 #include "chan_user.h"
27 char dev[sizeof("32768\0")];
30 static void *port_init(char *str, int device, struct chan_opts *opts)
32 struct port_chan *data;
38 printk("port_init : channel type 'port' must specify a "
43 port = strtoul(str, &end, 0);
44 if((*end != '\0') || (end == str)){
45 printk("port_init : couldn't parse port '%s'\n", str);
49 kern_data = port_data(port);
53 data = um_kmalloc(sizeof(*data));
57 *data = ((struct port_chan) { .raw = opts->raw,
58 .kernel_data = kern_data });
59 sprintf(data->dev, "%d", port);
63 port_kern_free(kern_data);
67 static void port_free(void *d)
69 struct port_chan *data = d;
71 port_kern_free(data->kernel_data);
75 static int port_open(int input, int output, int primary, void *d,
78 struct port_chan *data = d;
81 fd = port_wait(data->kernel_data);
82 if((fd >= 0) && data->raw){
83 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
95 static void port_close(int fd, void *d)
97 struct port_chan *data = d;
99 port_remove_dev(data->kernel_data);
103 static int port_console_write(int fd, const char *buf, int n, void *d)
105 struct port_chan *data = d;
107 return(generic_console_write(fd, buf, n, &data->tt));
110 struct chan_ops port_ops = {
115 .read = generic_read,
116 .write = generic_write,
117 .console_write = port_console_write,
118 .window_size = generic_window_size,
123 int port_listen_fd(int port)
125 struct sockaddr_in addr;
128 fd = socket(PF_INET, SOCK_STREAM, 0);
133 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
138 addr.sin_family = AF_INET;
139 addr.sin_port = htons(port);
140 addr.sin_addr.s_addr = htonl(INADDR_ANY);
141 if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
146 if(listen(fd, 1) < 0){
151 err = os_set_fd_block(fd, 0);
161 struct port_pre_exec_data {
166 void port_pre_exec(void *arg)
168 struct port_pre_exec_data *data = arg;
170 dup2(data->sock_fd, 0);
171 dup2(data->sock_fd, 1);
172 dup2(data->sock_fd, 2);
173 os_close_file(data->sock_fd);
174 dup2(data->pipe_fd, 3);
175 os_shutdown_socket(3, 1, 0);
176 os_close_file(data->pipe_fd);
179 int port_connection(int fd, int *socket, int *pid_out)
182 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
183 "/usr/lib/uml/port-helper", NULL };
184 struct port_pre_exec_data data;
186 new = os_accept_connection(fd);
190 err = os_pipe(socket, 0, 0);
194 data = ((struct port_pre_exec_data)
196 .pipe_fd = socket[1] });
198 err = run_helper(port_pre_exec, &data, argv, NULL);
206 os_shutdown_socket(socket[0], 1, 1);
207 os_close_file(socket[0]);
208 os_shutdown_socket(socket[1], 1, 1);
209 os_close_file(socket[1]);
216 * Overrides for Emacs so that we follow Linus's tabbing style.
217 * Emacs will notice this stuff at the end of the file and automatically
218 * adjust the settings for this buffer only. This must remain at the end
220 * ---------------------------------------------------------------------------
222 * c-file-style: "linux"