blob: addd759026560bb75eb1129e3b1e2818e77cf345 [file] [log] [blame]
Jeff Dike67608e02007-02-10 01:44:02 -08001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <termios.h>
Jeff Dikee99525f2007-10-16 01:26:41 -070010#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <netinet/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "chan_user.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070013#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "os.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070015#include "port.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070016#include "um_malloc.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070017#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19struct port_chan {
20 int raw;
21 struct termios tt;
22 void *kernel_data;
23 char dev[sizeof("32768\0")];
24};
25
Jeff Dike5e7672e2006-09-27 01:50:33 -070026static void *port_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 struct port_chan *data;
29 void *kern_data;
30 char *end;
31 int port;
32
Jeff Dikee99525f2007-10-16 01:26:41 -070033 if (*str != ':') {
34 printk(UM_KERN_ERR "port_init : channel type 'port' must "
35 "specify a port number\n");
Jeff Dike67608e02007-02-10 01:44:02 -080036 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38 str++;
39 port = strtoul(str, &end, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -070040 if ((*end != '\0') || (end == str)) {
41 printk(UM_KERN_ERR "port_init : couldn't parse port '%s'\n",
42 str);
Jeff Dike67608e02007-02-10 01:44:02 -080043 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
45
46 kern_data = port_data(port);
Jeff Dikee99525f2007-10-16 01:26:41 -070047 if (kern_data == NULL)
Jeff Dike67608e02007-02-10 01:44:02 -080048 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jeff Dikee4c4bf992007-07-15 23:38:56 -070050 data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -070051 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 goto err;
53
54 *data = ((struct port_chan) { .raw = opts->raw,
55 .kernel_data = kern_data });
56 sprintf(data->dev, "%d", port);
57
Jeff Dike67608e02007-02-10 01:44:02 -080058 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 err:
60 port_kern_free(kern_data);
Jeff Dike67608e02007-02-10 01:44:02 -080061 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static 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
72static 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);
Jeff Dikee99525f2007-10-16 01:26:41 -070079 if ((fd >= 0) && data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070081 if (err)
Jeff Dike67608e02007-02-10 01:44:02 -080082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070085 if (err)
Jeff Dike67608e02007-02-10 01:44:02 -080086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 *dev_out = data->dev;
Jeff Dike67608e02007-02-10 01:44:02 -080089 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static 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
Jeff Dike5e7672e2006-09-27 01:50:33 -0700100const struct chan_ops port_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .type = "port",
102 .init = port_init,
103 .open = port_open,
104 .close = port_close,
105 .read = generic_read,
106 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -0800107 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .window_size = generic_window_size,
109 .free = port_free,
110 .winch = 1,
111};
112
113int 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);
Jeff Dikee99525f2007-10-16 01:26:41 -0700119 if (fd == -1)
Jeff Dike67608e02007-02-10 01:44:02 -0800120 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 arg = 1;
Jeff Dikee99525f2007-10-16 01:26:41 -0700123 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 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);
Jeff Dikee99525f2007-10-16 01:26:41 -0700131 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 err = -errno;
133 goto out;
134 }
Jeff Dike67608e02007-02-10 01:44:02 -0800135
Jeff Dikee99525f2007-10-16 01:26:41 -0700136 if (listen(fd, 1) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 err = -errno;
138 goto out;
139 }
140
141 err = os_set_fd_block(fd, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700142 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto out;
144
Jeff Dike67608e02007-02-10 01:44:02 -0800145 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700147 close(fd);
Jeff Dike67608e02007-02-10 01:44:02 -0800148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151struct port_pre_exec_data {
152 int sock_fd;
153 int pipe_fd;
154};
155
156void port_pre_exec(void *arg)
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);
Jeff Dikee99525f2007-10-16 01:26:41 -0700163 close(data->sock_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dup2(data->pipe_fd, 3);
Jeff Dikee99525f2007-10-16 01:26:41 -0700165 shutdown(3, SHUT_RD);
166 close(data->pipe_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169int port_connection(int fd, int *socket, int *pid_out)
170{
171 int new, err;
Jeff Dike67608e02007-02-10 01:44:02 -0800172 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 "/usr/lib/uml/port-helper", NULL };
174 struct port_pre_exec_data data;
175
Jeff Dikee99525f2007-10-16 01:26:41 -0700176 new = accept(fd, NULL, 0);
177 if (new < 0)
178 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 err = os_pipe(socket, 0, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700181 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out_close;
183
184 data = ((struct port_pre_exec_data)
185 { .sock_fd = new,
186 .pipe_fd = socket[1] });
187
Jeff Dikec4399012007-07-15 23:38:56 -0700188 err = run_helper(port_pre_exec, &data, argv);
Jeff Dikee99525f2007-10-16 01:26:41 -0700189 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out_shutdown;
191
192 *pid_out = err;
Jeff Dike67608e02007-02-10 01:44:02 -0800193 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 out_shutdown:
Jeff Dikee99525f2007-10-16 01:26:41 -0700196 shutdown(socket[0], SHUT_RDWR);
197 close(socket[0]);
198 shutdown(socket[1], SHUT_RDWR);
199 close(socket[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 out_close:
Jeff Dikee99525f2007-10-16 01:26:41 -0700201 close(new);
Jeff Dike67608e02007-02-10 01:44:02 -0800202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}