Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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" | |
21 | #include "helper.h" | |
22 | #include "os.h" | |
23 | ||
24 | struct port_chan { | |
25 | int raw; | |
26 | struct termios tt; | |
27 | void *kernel_data; | |
28 | char dev[sizeof("32768\0")]; | |
29 | }; | |
30 | ||
31 | static void *port_init(char *str, int device, struct chan_opts *opts) | |
32 | { | |
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 | ||
68 | static 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 | ||
76 | static 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 | ||
96 | static 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 | ||
104 | static int port_console_write(int fd, const char *buf, int n, void *d) | |
105 | { | |
106 | struct port_chan *data = d; | |
107 | ||
108 | return(generic_console_write(fd, buf, n, &data->tt)); | |
109 | } | |
110 | ||
111 | struct chan_ops port_ops = { | |
112 | .type = "port", | |
113 | .init = port_init, | |
114 | .open = port_open, | |
115 | .close = port_close, | |
116 | .read = generic_read, | |
117 | .write = generic_write, | |
118 | .console_write = port_console_write, | |
119 | .window_size = generic_window_size, | |
120 | .free = port_free, | |
121 | .winch = 1, | |
122 | }; | |
123 | ||
124 | int port_listen_fd(int port) | |
125 | { | |
126 | struct sockaddr_in addr; | |
127 | int fd, err, arg; | |
128 | ||
129 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
130 | if(fd == -1) | |
131 | return(-errno); | |
132 | ||
133 | arg = 1; | |
134 | if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){ | |
135 | err = -errno; | |
136 | goto out; | |
137 | } | |
138 | ||
139 | addr.sin_family = AF_INET; | |
140 | addr.sin_port = htons(port); | |
141 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
142 | if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){ | |
143 | err = -errno; | |
144 | goto out; | |
145 | } | |
146 | ||
147 | if(listen(fd, 1) < 0){ | |
148 | err = -errno; | |
149 | goto out; | |
150 | } | |
151 | ||
152 | err = os_set_fd_block(fd, 0); | |
153 | if(err < 0) | |
154 | goto out; | |
155 | ||
156 | return(fd); | |
157 | out: | |
158 | os_close_file(fd); | |
159 | return(err); | |
160 | } | |
161 | ||
162 | struct port_pre_exec_data { | |
163 | int sock_fd; | |
164 | int pipe_fd; | |
165 | }; | |
166 | ||
167 | void port_pre_exec(void *arg) | |
168 | { | |
169 | struct port_pre_exec_data *data = arg; | |
170 | ||
171 | dup2(data->sock_fd, 0); | |
172 | dup2(data->sock_fd, 1); | |
173 | dup2(data->sock_fd, 2); | |
174 | os_close_file(data->sock_fd); | |
175 | dup2(data->pipe_fd, 3); | |
176 | os_shutdown_socket(3, 1, 0); | |
177 | os_close_file(data->pipe_fd); | |
178 | } | |
179 | ||
180 | int port_connection(int fd, int *socket, int *pid_out) | |
181 | { | |
182 | int new, err; | |
183 | char *argv[] = { "/usr/sbin/in.telnetd", "-L", | |
184 | "/usr/lib/uml/port-helper", NULL }; | |
185 | struct port_pre_exec_data data; | |
186 | ||
187 | new = os_accept_connection(fd); | |
188 | if(new < 0) | |
189 | return(new); | |
190 | ||
191 | err = os_pipe(socket, 0, 0); | |
192 | if(err < 0) | |
193 | goto out_close; | |
194 | ||
195 | data = ((struct port_pre_exec_data) | |
196 | { .sock_fd = new, | |
197 | .pipe_fd = socket[1] }); | |
198 | ||
199 | err = run_helper(port_pre_exec, &data, argv, NULL); | |
200 | if(err < 0) | |
201 | goto out_shutdown; | |
202 | ||
203 | *pid_out = err; | |
204 | return(new); | |
205 | ||
206 | out_shutdown: | |
207 | os_shutdown_socket(socket[0], 1, 1); | |
208 | os_close_file(socket[0]); | |
209 | os_shutdown_socket(socket[1], 1, 1); | |
210 | os_close_file(socket[1]); | |
211 | out_close: | |
212 | os_close_file(new); | |
213 | return(err); | |
214 | } | |
215 | ||
216 | /* | |
217 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
218 | * Emacs will notice this stuff at the end of the file and automatically | |
219 | * adjust the settings for this buffer only. This must remain at the end | |
220 | * of the file. | |
221 | * --------------------------------------------------------------------------- | |
222 | * Local variables: | |
223 | * c-file-style: "linux" | |
224 | * End: | |
225 | */ |