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