vserver 1.9.5.x5
[linux-2.6.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 "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 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 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 int port_open(int input, int output, int primary, void *d, char **dev_out)
77 {
78         struct port_chan *data = d;
79         int fd, err;
80
81         fd = port_wait(data->kernel_data);
82         if((fd >= 0) && data->raw){
83                 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
84                 if(err)
85                         return(err);
86
87                 err = raw(fd);
88                 if(err)
89                         return(err);
90         }
91         *dev_out = data->dev;
92         return(fd);
93 }
94
95 void port_close(int fd, void *d)
96 {
97         struct port_chan *data = d;
98
99         port_remove_dev(data->kernel_data);
100         os_close_file(fd);
101 }
102
103 int port_console_write(int fd, const char *buf, int n, void *d)
104 {
105         struct port_chan *data = d;
106
107         return(generic_console_write(fd, buf, n, &data->tt));
108 }
109
110 struct chan_ops port_ops = {
111         .type           = "port",
112         .init           = port_init,
113         .open           = port_open,
114         .close          = port_close,
115         .read           = generic_read,
116         .write          = generic_write,
117         .console_write  = port_console_write,
118         .window_size    = generic_window_size,
119         .free           = port_free,
120         .winch          = 1,
121 };
122
123 int port_listen_fd(int port)
124 {
125         struct sockaddr_in addr;
126         int fd, err, arg;
127
128         fd = socket(PF_INET, SOCK_STREAM, 0);
129         if(fd == -1) 
130                 return(-errno);
131
132         arg = 1;
133         if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
134                 err = -errno;
135                 goto out;
136         }
137
138         addr.sin_family = AF_INET;
139         addr.sin_port = htons(port);
140         addr.sin_addr.s_addr = htonl(INADDR_ANY);
141         if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
142                 err = -errno;
143                 goto out;
144         }
145   
146         if(listen(fd, 1) < 0){
147                 err = -errno;
148                 goto out;
149         }
150
151         err = os_set_fd_block(fd, 0);
152         if(err < 0)
153                 goto out;
154
155         return(fd);
156  out:
157         os_close_file(fd);
158         return(err);
159 }
160
161 struct port_pre_exec_data {
162         int sock_fd;
163         int pipe_fd;
164 };
165
166 void port_pre_exec(void *arg)
167 {
168         struct port_pre_exec_data *data = arg;
169
170         dup2(data->sock_fd, 0);
171         dup2(data->sock_fd, 1);
172         dup2(data->sock_fd, 2);
173         os_close_file(data->sock_fd);
174         dup2(data->pipe_fd, 3);
175         os_shutdown_socket(3, 1, 0);
176         os_close_file(data->pipe_fd);
177 }
178
179 int port_connection(int fd, int *socket, int *pid_out)
180 {
181         int new, err;
182         char *argv[] = { "/usr/sbin/in.telnetd", "-L", 
183                          "/usr/lib/uml/port-helper", NULL };
184         struct port_pre_exec_data data;
185
186         new = os_accept_connection(fd);
187         if(new < 0)
188                 return(new);
189
190         err = os_pipe(socket, 0, 0);
191         if(err < 0)
192                 goto out_close;
193
194         data = ((struct port_pre_exec_data)
195                 { .sock_fd              = new,
196                   .pipe_fd              = socket[1] });
197
198         err = run_helper(port_pre_exec, &data, argv, NULL);
199         if(err < 0) 
200                 goto out_shutdown;
201
202         *pid_out = err;
203         return(new);
204
205  out_shutdown:
206         os_shutdown_socket(socket[0], 1, 1);
207         os_close_file(socket[0]);
208         os_shutdown_socket(socket[1], 1, 1);    
209         os_close_file(socket[1]);
210  out_close:
211         os_close_file(new);
212         return(err);
213 }
214
215 /*
216  * Overrides for Emacs so that we follow Linus's tabbing style.
217  * Emacs will notice this stuff at the end of the file and automatically
218  * adjust the settings for this buffer only.  This must remain at the end
219  * of the file.
220  * ---------------------------------------------------------------------------
221  * Local variables:
222  * c-file-style: "linux"
223  * End:
224  */