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