blob: bc6afaf74c1a8123ace69f94c5d2d9a340bc6347 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <stddef.h>
8#include <stdlib.h>
9#include <string.h>
10#include <errno.h>
11#include <unistd.h>
12#include <termios.h>
13#include <sys/socket.h>
14#include <sys/un.h>
15#include <netinet/in.h>
16#include "user_util.h"
17#include "kern_util.h"
18#include "user.h"
19#include "chan_user.h"
20#include "port.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070022#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24struct port_chan {
25 int raw;
26 struct termios tt;
27 void *kernel_data;
28 char dev[sizeof("32768\0")];
29};
30
Jeff Dike5e7672e2006-09-27 01:50:33 -070031static void *port_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct port_chan *data;
34 void *kern_data;
35 char *end;
36 int port;
37
38 if(*str != ':'){
39 printk("port_init : channel type 'port' must specify a "
40 "port number\n");
41 return(NULL);
42 }
43 str++;
44 port = strtoul(str, &end, 0);
45 if((*end != '\0') || (end == str)){
46 printk("port_init : couldn't parse port '%s'\n", str);
47 return(NULL);
48 }
49
50 kern_data = port_data(port);
51 if(kern_data == NULL)
52 return(NULL);
53
54 data = um_kmalloc(sizeof(*data));
55 if(data == NULL)
56 goto err;
57
58 *data = ((struct port_chan) { .raw = opts->raw,
59 .kernel_data = kern_data });
60 sprintf(data->dev, "%d", port);
61
62 return(data);
63 err:
64 port_kern_free(kern_data);
65 return(NULL);
66}
67
68static void port_free(void *d)
69{
70 struct port_chan *data = d;
71
72 port_kern_free(data->kernel_data);
73 kfree(data);
74}
75
76static int port_open(int input, int output, int primary, void *d,
77 char **dev_out)
78{
79 struct port_chan *data = d;
80 int fd, err;
81
82 fd = port_wait(data->kernel_data);
83 if((fd >= 0) && data->raw){
84 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
85 if(err)
86 return(err);
87
88 err = raw(fd);
89 if(err)
90 return(err);
91 }
92 *dev_out = data->dev;
93 return(fd);
94}
95
96static void port_close(int fd, void *d)
97{
98 struct port_chan *data = d;
99
100 port_remove_dev(data->kernel_data);
101 os_close_file(fd);
102}
103
Jeff Dike5e7672e2006-09-27 01:50:33 -0700104const struct chan_ops port_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .type = "port",
106 .init = port_init,
107 .open = port_open,
108 .close = port_close,
109 .read = generic_read,
110 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -0800111 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .window_size = generic_window_size,
113 .free = port_free,
114 .winch = 1,
115};
116
117int port_listen_fd(int port)
118{
119 struct sockaddr_in addr;
120 int fd, err, arg;
121
122 fd = socket(PF_INET, SOCK_STREAM, 0);
123 if(fd == -1)
124 return(-errno);
125
126 arg = 1;
127 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
128 err = -errno;
129 goto out;
130 }
131
132 addr.sin_family = AF_INET;
133 addr.sin_port = htons(port);
134 addr.sin_addr.s_addr = htonl(INADDR_ANY);
135 if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
136 err = -errno;
137 goto out;
138 }
139
140 if(listen(fd, 1) < 0){
141 err = -errno;
142 goto out;
143 }
144
145 err = os_set_fd_block(fd, 0);
146 if(err < 0)
147 goto out;
148
149 return(fd);
150 out:
151 os_close_file(fd);
152 return(err);
153}
154
155struct port_pre_exec_data {
156 int sock_fd;
157 int pipe_fd;
158};
159
160void port_pre_exec(void *arg)
161{
162 struct port_pre_exec_data *data = arg;
163
164 dup2(data->sock_fd, 0);
165 dup2(data->sock_fd, 1);
166 dup2(data->sock_fd, 2);
167 os_close_file(data->sock_fd);
168 dup2(data->pipe_fd, 3);
169 os_shutdown_socket(3, 1, 0);
170 os_close_file(data->pipe_fd);
171}
172
173int port_connection(int fd, int *socket, int *pid_out)
174{
175 int new, err;
176 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
177 "/usr/lib/uml/port-helper", NULL };
178 struct port_pre_exec_data data;
179
180 new = os_accept_connection(fd);
181 if(new < 0)
182 return(new);
183
184 err = os_pipe(socket, 0, 0);
185 if(err < 0)
186 goto out_close;
187
188 data = ((struct port_pre_exec_data)
189 { .sock_fd = new,
190 .pipe_fd = socket[1] });
191
192 err = run_helper(port_pre_exec, &data, argv, NULL);
193 if(err < 0)
194 goto out_shutdown;
195
196 *pid_out = err;
197 return(new);
198
199 out_shutdown:
200 os_shutdown_socket(socket[0], 1, 1);
201 os_close_file(socket[0]);
202 os_shutdown_socket(socket[1], 1, 1);
203 os_close_file(socket[1]);
204 out_close:
205 os_close_file(new);
206 return(err);
207}
208
209/*
210 * Overrides for Emacs so that we follow Linus's tabbing style.
211 * Emacs will notice this stuff at the end of the file and automatically
212 * adjust the settings for this buffer only. This must remain at the end
213 * of the file.
214 * ---------------------------------------------------------------------------
215 * Local variables:
216 * c-file-style: "linux"
217 * End:
218 */