[PATCH] uml: code convention cleanup of a file
[linux-2.6/verdex.git] / arch / um / drivers / port_user.c
blobbc6afaf74c1a8123ace69f94c5d2d9a340bc6347
1 /*
2 * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
24 struct port_chan {
25 int raw;
26 struct termios tt;
27 void *kernel_data;
28 char dev[sizeof("32768\0")];
31 static void *port_init(char *str, int device, const struct chan_opts *opts)
33 struct port_chan *data;
34 void *kern_data;
35 char *end;
36 int port;
38 if(*str != ':'){
39 printk("port_init : channel type 'port' must specify a "
40 "port number\n");
41 return(NULL);
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);
50 kern_data = port_data(port);
51 if(kern_data == NULL)
52 return(NULL);
54 data = um_kmalloc(sizeof(*data));
55 if(data == NULL)
56 goto err;
58 *data = ((struct port_chan) { .raw = opts->raw,
59 .kernel_data = kern_data });
60 sprintf(data->dev, "%d", port);
62 return(data);
63 err:
64 port_kern_free(kern_data);
65 return(NULL);
68 static void port_free(void *d)
70 struct port_chan *data = d;
72 port_kern_free(data->kernel_data);
73 kfree(data);
76 static int port_open(int input, int output, int primary, void *d,
77 char **dev_out)
79 struct port_chan *data = d;
80 int fd, err;
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);
88 err = raw(fd);
89 if(err)
90 return(err);
92 *dev_out = data->dev;
93 return(fd);
96 static void port_close(int fd, void *d)
98 struct port_chan *data = d;
100 port_remove_dev(data->kernel_data);
101 os_close_file(fd);
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,
117 int port_listen_fd(int port)
119 struct sockaddr_in addr;
120 int fd, err, arg;
122 fd = socket(PF_INET, SOCK_STREAM, 0);
123 if(fd == -1)
124 return(-errno);
126 arg = 1;
127 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
128 err = -errno;
129 goto out;
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;
140 if(listen(fd, 1) < 0){
141 err = -errno;
142 goto out;
145 err = os_set_fd_block(fd, 0);
146 if(err < 0)
147 goto out;
149 return(fd);
150 out:
151 os_close_file(fd);
152 return(err);
155 struct port_pre_exec_data {
156 int sock_fd;
157 int pipe_fd;
160 void port_pre_exec(void *arg)
162 struct port_pre_exec_data *data = arg;
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);
173 int port_connection(int fd, int *socket, int *pid_out)
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;
180 new = os_accept_connection(fd);
181 if(new < 0)
182 return(new);
184 err = os_pipe(socket, 0, 0);
185 if(err < 0)
186 goto out_close;
188 data = ((struct port_pre_exec_data)
189 { .sock_fd = new,
190 .pipe_fd = socket[1] });
192 err = run_helper(port_pre_exec, &data, argv, NULL);
193 if(err < 0)
194 goto out_shutdown;
196 *pid_out = err;
197 return(new);
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);
210 * Overrides for Emacs so that we follow Linus's tabbing style.
211 * Emacs will notice this stuff at the end of the file and automatically
212 * adjust the settings for this buffer only. This must remain at the end
213 * of the file.
214 * ---------------------------------------------------------------------------
215 * Local variables:
216 * c-file-style: "linux"
217 * End: