Commit | Line | Data |
---|---|---|
67608e0c | 1 | /* |
e99525f9 | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com) |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
1da177e4 | 7 | #include <stdlib.h> |
1da177e4 | 8 | #include <errno.h> |
1da177e4 | 9 | #include <termios.h> |
e99525f9 | 10 | #include <unistd.h> |
1da177e4 | 11 | #include <netinet/in.h> |
1da177e4 | 12 | #include "chan_user.h" |
e99525f9 | 13 | #include "kern_constants.h" |
1da177e4 | 14 | #include "os.h" |
e99525f9 | 15 | #include "port.h" |
c13e5690 | 16 | #include "um_malloc.h" |
e99525f9 | 17 | #include "user.h" |
1da177e4 LT |
18 | |
19 | struct port_chan { | |
20 | int raw; | |
21 | struct termios tt; | |
22 | void *kernel_data; | |
23 | char dev[sizeof("32768\0")]; | |
24 | }; | |
25 | ||
5e7672ec | 26 | static void *port_init(char *str, int device, const struct chan_opts *opts) |
1da177e4 LT |
27 | { |
28 | struct port_chan *data; | |
29 | void *kern_data; | |
30 | char *end; | |
31 | int port; | |
32 | ||
e99525f9 JD |
33 | if (*str != ':') { |
34 | printk(UM_KERN_ERR "port_init : channel type 'port' must " | |
35 | "specify a port number\n"); | |
67608e0c | 36 | return NULL; |
1da177e4 LT |
37 | } |
38 | str++; | |
39 | port = strtoul(str, &end, 0); | |
e99525f9 JD |
40 | if ((*end != '\0') || (end == str)) { |
41 | printk(UM_KERN_ERR "port_init : couldn't parse port '%s'\n", | |
42 | str); | |
67608e0c | 43 | return NULL; |
1da177e4 LT |
44 | } |
45 | ||
46 | kern_data = port_data(port); | |
e99525f9 | 47 | if (kern_data == NULL) |
67608e0c | 48 | return NULL; |
1da177e4 | 49 | |
e4c4bf99 | 50 | data = kmalloc(sizeof(*data), UM_GFP_KERNEL); |
e99525f9 | 51 | if (data == NULL) |
1da177e4 LT |
52 | goto err; |
53 | ||
54 | *data = ((struct port_chan) { .raw = opts->raw, | |
55 | .kernel_data = kern_data }); | |
56 | sprintf(data->dev, "%d", port); | |
57 | ||
67608e0c | 58 | return data; |
1da177e4 LT |
59 | err: |
60 | port_kern_free(kern_data); | |
67608e0c | 61 | return NULL; |
1da177e4 LT |
62 | } |
63 | ||
64 | static void port_free(void *d) | |
65 | { | |
66 | struct port_chan *data = d; | |
67 | ||
68 | port_kern_free(data->kernel_data); | |
69 | kfree(data); | |
70 | } | |
71 | ||
72 | static int port_open(int input, int output, int primary, void *d, | |
73 | char **dev_out) | |
74 | { | |
75 | struct port_chan *data = d; | |
76 | int fd, err; | |
77 | ||
78 | fd = port_wait(data->kernel_data); | |
e99525f9 | 79 | if ((fd >= 0) && data->raw) { |
1da177e4 | 80 | CATCH_EINTR(err = tcgetattr(fd, &data->tt)); |
e99525f9 | 81 | if (err) |
67608e0c | 82 | return err; |
1da177e4 LT |
83 | |
84 | err = raw(fd); | |
e99525f9 | 85 | if (err) |
67608e0c | 86 | return err; |
1da177e4 LT |
87 | } |
88 | *dev_out = data->dev; | |
67608e0c | 89 | return fd; |
1da177e4 LT |
90 | } |
91 | ||
92 | static void port_close(int fd, void *d) | |
93 | { | |
94 | struct port_chan *data = d; | |
95 | ||
96 | port_remove_dev(data->kernel_data); | |
97 | os_close_file(fd); | |
98 | } | |
99 | ||
5e7672ec | 100 | const struct chan_ops port_ops = { |
1da177e4 LT |
101 | .type = "port", |
102 | .init = port_init, | |
103 | .open = port_open, | |
104 | .close = port_close, | |
105 | .read = generic_read, | |
106 | .write = generic_write, | |
fd9bc53b | 107 | .console_write = generic_console_write, |
1da177e4 LT |
108 | .window_size = generic_window_size, |
109 | .free = port_free, | |
110 | .winch = 1, | |
111 | }; | |
112 | ||
113 | int port_listen_fd(int port) | |
114 | { | |
115 | struct sockaddr_in addr; | |
116 | int fd, err, arg; | |
117 | ||
118 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
e99525f9 | 119 | if (fd == -1) |
67608e0c | 120 | return -errno; |
1da177e4 LT |
121 | |
122 | arg = 1; | |
e99525f9 | 123 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0) { |
1da177e4 LT |
124 | err = -errno; |
125 | goto out; | |
126 | } | |
127 | ||
128 | addr.sin_family = AF_INET; | |
129 | addr.sin_port = htons(port); | |
130 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
e99525f9 | 131 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
1da177e4 LT |
132 | err = -errno; |
133 | goto out; | |
134 | } | |
67608e0c | 135 | |
e99525f9 | 136 | if (listen(fd, 1) < 0) { |
1da177e4 LT |
137 | err = -errno; |
138 | goto out; | |
139 | } | |
140 | ||
141 | err = os_set_fd_block(fd, 0); | |
e99525f9 | 142 | if (err < 0) |
1da177e4 LT |
143 | goto out; |
144 | ||
67608e0c | 145 | return fd; |
1da177e4 | 146 | out: |
e99525f9 | 147 | close(fd); |
67608e0c | 148 | return err; |
1da177e4 LT |
149 | } |
150 | ||
151 | struct port_pre_exec_data { | |
152 | int sock_fd; | |
153 | int pipe_fd; | |
154 | }; | |
155 | ||
1605ec04 | 156 | static void port_pre_exec(void *arg) |
1da177e4 LT |
157 | { |
158 | struct port_pre_exec_data *data = arg; | |
159 | ||
160 | dup2(data->sock_fd, 0); | |
161 | dup2(data->sock_fd, 1); | |
162 | dup2(data->sock_fd, 2); | |
e99525f9 | 163 | close(data->sock_fd); |
1da177e4 | 164 | dup2(data->pipe_fd, 3); |
e99525f9 JD |
165 | shutdown(3, SHUT_RD); |
166 | close(data->pipe_fd); | |
1da177e4 LT |
167 | } |
168 | ||
169 | int port_connection(int fd, int *socket, int *pid_out) | |
170 | { | |
171 | int new, err; | |
67608e0c | 172 | char *argv[] = { "/usr/sbin/in.telnetd", "-L", |
1da177e4 LT |
173 | "/usr/lib/uml/port-helper", NULL }; |
174 | struct port_pre_exec_data data; | |
175 | ||
e99525f9 JD |
176 | new = accept(fd, NULL, 0); |
177 | if (new < 0) | |
178 | return -errno; | |
1da177e4 LT |
179 | |
180 | err = os_pipe(socket, 0, 0); | |
e99525f9 | 181 | if (err < 0) |
1da177e4 LT |
182 | goto out_close; |
183 | ||
184 | data = ((struct port_pre_exec_data) | |
185 | { .sock_fd = new, | |
186 | .pipe_fd = socket[1] }); | |
187 | ||
c4399016 | 188 | err = run_helper(port_pre_exec, &data, argv); |
e99525f9 | 189 | if (err < 0) |
1da177e4 LT |
190 | goto out_shutdown; |
191 | ||
192 | *pid_out = err; | |
67608e0c | 193 | return new; |
1da177e4 LT |
194 | |
195 | out_shutdown: | |
e99525f9 JD |
196 | shutdown(socket[0], SHUT_RDWR); |
197 | close(socket[0]); | |
198 | shutdown(socket[1], SHUT_RDWR); | |
199 | close(socket[1]); | |
1da177e4 | 200 | out_close: |
e99525f9 | 201 | close(new); |
67608e0c | 202 | return err; |
1da177e4 | 203 | } |