Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-drm-fsl-dcu.git] / arch / um / drivers / port_user.c
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 "os.h"
22 #include "um_malloc.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, const 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 const struct chan_ops port_ops = {
105         .type           = "port",
106         .init           = port_init,
107         .open           = port_open,
108         .close          = port_close,
109         .read           = generic_read,
110         .write          = generic_write,
111         .console_write  = generic_console_write,
112         .window_size    = generic_window_size,
113         .free           = port_free,
114         .winch          = 1,
115 };
116
117 int 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
155 struct port_pre_exec_data {
156         int sock_fd;
157         int pipe_fd;
158 };
159
160 void 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
173 int 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 }