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 if((kern_data = port_data(port)) == NULL)
53 if((data = um_kmalloc(sizeof(*data))) == NULL)
56 *data = ((struct port_chan) { .raw = opts->raw,
57 .kernel_data = kern_data });
58 sprintf(data->dev, "%d", port);
62 port_kern_free(kern_data);
66 void port_free(void *d)
68 struct port_chan *data = d;
70 port_kern_free(data->kernel_data);
74 int port_open(int input, int output, int primary, void *d, char **dev_out)
76 struct port_chan *data = d;
79 fd = port_wait(data->kernel_data);
80 if((fd >= 0) && data->raw){
81 tcgetattr(fd, &data->tt);
88 void port_close(int fd, void *d)
90 struct port_chan *data = d;
92 port_remove_dev(data->kernel_data);
96 int port_console_write(int fd, const char *buf, int n, void *d)
98 struct port_chan *data = d;
100 return(generic_console_write(fd, buf, n, &data->tt));
103 struct chan_ops port_ops = {
108 .read = generic_read,
109 .write = generic_write,
110 .console_write = port_console_write,
111 .window_size = generic_window_size,
116 int port_listen_fd(int port)
118 struct sockaddr_in addr;
121 fd = socket(PF_INET, SOCK_STREAM, 0);
125 addr.sin_family = AF_INET;
126 addr.sin_port = htons(port);
127 addr.sin_addr.s_addr = htonl(INADDR_ANY);
128 if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
133 if((listen(fd, 1) < 0) || (os_set_fd_block(fd, 0))){
144 struct port_pre_exec_data {
149 void port_pre_exec(void *arg)
151 struct port_pre_exec_data *data = arg;
153 dup2(data->sock_fd, 0);
154 dup2(data->sock_fd, 1);
155 dup2(data->sock_fd, 2);
156 close(data->sock_fd);
157 dup2(data->pipe_fd, 3);
158 os_shutdown_socket(3, 1, 0);
159 close(data->pipe_fd);
162 int port_connection(int fd, int *socket, int *pid_out)
165 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
166 "/usr/lib/uml/port-helper", NULL };
167 struct port_pre_exec_data data;
169 if((new = os_accept_connection(fd)) < 0)
172 err = os_pipe(socket, 0, 0);
176 data = ((struct port_pre_exec_data)
178 .pipe_fd = socket[1] });
180 err = run_helper(port_pre_exec, &data, argv, NULL);
188 os_shutdown_socket(socket[0], 1, 1);
190 os_shutdown_socket(socket[1], 1, 1);
198 * Overrides for Emacs so that we follow Linus's tabbing style.
199 * Emacs will notice this stuff at the end of the file and automatically
200 * adjust the settings for this buffer only. This must remain at the end
202 * ---------------------------------------------------------------------------
204 * c-file-style: "linux"