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"
28 char dev[sizeof("32768\0")];
31 void *port_init(char *str, int device, struct chan_opts *opts)
33 struct port_chan *data;
39 printk("port_init : channel type 'port' must specify a "
44 port = strtoul(str, &end, 0);
45 if((*end != '\0') || (end == str)){
46 printk("port_init : couldn't parse port '%s'\n", str);
50 kern_data = port_data(port);
54 data = um_kmalloc(sizeof(*data));
58 *data = ((struct port_chan) { .raw = opts->raw,
59 .kernel_data = kern_data });
60 sprintf(data->dev, "%d", port);
64 port_kern_free(kern_data);
68 void port_free(void *d)
70 struct port_chan *data = d;
72 port_kern_free(data->kernel_data);
76 int port_open(int input, int output, int primary, void *d, char **dev_out)
78 struct port_chan *data = d;
81 fd = port_wait(data->kernel_data);
82 if((fd >= 0) && data->raw){
83 tcgetattr(fd, &data->tt);
90 void port_close(int fd, void *d)
92 struct port_chan *data = d;
94 port_remove_dev(data->kernel_data);
98 int port_console_write(int fd, const char *buf, int n, void *d)
100 struct port_chan *data = d;
102 return(generic_console_write(fd, buf, n, &data->tt));
105 struct chan_ops port_ops = {
110 .read = generic_read,
111 .write = generic_write,
112 .console_write = port_console_write,
113 .window_size = generic_window_size,
118 int port_listen_fd(int port)
120 struct sockaddr_in addr;
123 fd = socket(PF_INET, SOCK_STREAM, 0);
127 addr.sin_family = AF_INET;
128 addr.sin_port = htons(port);
129 addr.sin_addr.s_addr = htonl(INADDR_ANY);
130 if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
135 if(listen(fd, 1) < 0){
140 err = os_set_fd_block(fd, 0);
150 struct port_pre_exec_data {
155 void port_pre_exec(void *arg)
157 struct port_pre_exec_data *data = arg;
159 dup2(data->sock_fd, 0);
160 dup2(data->sock_fd, 1);
161 dup2(data->sock_fd, 2);
162 os_close_file(data->sock_fd);
163 dup2(data->pipe_fd, 3);
164 os_shutdown_socket(3, 1, 0);
165 os_close_file(data->pipe_fd);
168 int port_connection(int fd, int *socket, int *pid_out)
171 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
172 "/usr/lib/uml/port-helper", NULL };
173 struct port_pre_exec_data data;
175 new = os_accept_connection(fd);
179 err = os_pipe(socket, 0, 0);
183 data = ((struct port_pre_exec_data)
185 .pipe_fd = socket[1] });
187 err = run_helper(port_pre_exec, &data, argv, NULL);
195 os_shutdown_socket(socket[0], 1, 1);
196 os_close_file(socket[0]);
197 os_shutdown_socket(socket[1], 1, 1);
198 os_close_file(socket[1]);
205 * Overrides for Emacs so that we follow Linus's tabbing style.
206 * Emacs will notice this stuff at the end of the file and automatically
207 * adjust the settings for this buffer only. This must remain at the end
209 * ---------------------------------------------------------------------------
211 * c-file-style: "linux"