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