BUG_ON() Conversion in kernel/signal.c
[linux/fpc-iii.git] / arch / um / drivers / port_user.c
blobc43e8bb3250264940af52ea528fe6a378fe41f67
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"
23 struct port_chan {
24 int raw;
25 struct termios tt;
26 void *kernel_data;
27 char dev[sizeof("32768\0")];
30 static void *port_init(char *str, int device, struct chan_opts *opts)
32 struct port_chan *data;
33 void *kern_data;
34 char *end;
35 int port;
37 if(*str != ':'){
38 printk("port_init : channel type 'port' must specify a "
39 "port number\n");
40 return(NULL);
42 str++;
43 port = strtoul(str, &end, 0);
44 if((*end != '\0') || (end == str)){
45 printk("port_init : couldn't parse port '%s'\n", str);
46 return(NULL);
49 kern_data = port_data(port);
50 if(kern_data == NULL)
51 return(NULL);
53 data = um_kmalloc(sizeof(*data));
54 if(data == NULL)
55 goto err;
57 *data = ((struct port_chan) { .raw = opts->raw,
58 .kernel_data = kern_data });
59 sprintf(data->dev, "%d", port);
61 return(data);
62 err:
63 port_kern_free(kern_data);
64 return(NULL);
67 static void port_free(void *d)
69 struct port_chan *data = d;
71 port_kern_free(data->kernel_data);
72 kfree(data);
75 static int port_open(int input, int output, int primary, void *d,
76 char **dev_out)
78 struct port_chan *data = d;
79 int fd, err;
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);
87 err = raw(fd);
88 if(err)
89 return(err);
91 *dev_out = data->dev;
92 return(fd);
95 static void port_close(int fd, void *d)
97 struct port_chan *data = d;
99 port_remove_dev(data->kernel_data);
100 os_close_file(fd);
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 = generic_console_write,
111 .window_size = generic_window_size,
112 .free = port_free,
113 .winch = 1,
116 int port_listen_fd(int port)
118 struct sockaddr_in addr;
119 int fd, err, arg;
121 fd = socket(PF_INET, SOCK_STREAM, 0);
122 if(fd == -1)
123 return(-errno);
125 arg = 1;
126 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0){
127 err = -errno;
128 goto out;
131 addr.sin_family = AF_INET;
132 addr.sin_port = htons(port);
133 addr.sin_addr.s_addr = htonl(INADDR_ANY);
134 if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0){
135 err = -errno;
136 goto out;
139 if(listen(fd, 1) < 0){
140 err = -errno;
141 goto out;
144 err = os_set_fd_block(fd, 0);
145 if(err < 0)
146 goto out;
148 return(fd);
149 out:
150 os_close_file(fd);
151 return(err);
154 struct port_pre_exec_data {
155 int sock_fd;
156 int pipe_fd;
159 void port_pre_exec(void *arg)
161 struct port_pre_exec_data *data = arg;
163 dup2(data->sock_fd, 0);
164 dup2(data->sock_fd, 1);
165 dup2(data->sock_fd, 2);
166 os_close_file(data->sock_fd);
167 dup2(data->pipe_fd, 3);
168 os_shutdown_socket(3, 1, 0);
169 os_close_file(data->pipe_fd);
172 int port_connection(int fd, int *socket, int *pid_out)
174 int new, err;
175 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
176 "/usr/lib/uml/port-helper", NULL };
177 struct port_pre_exec_data data;
179 new = os_accept_connection(fd);
180 if(new < 0)
181 return(new);
183 err = os_pipe(socket, 0, 0);
184 if(err < 0)
185 goto out_close;
187 data = ((struct port_pre_exec_data)
188 { .sock_fd = new,
189 .pipe_fd = socket[1] });
191 err = run_helper(port_pre_exec, &data, argv, NULL);
192 if(err < 0)
193 goto out_shutdown;
195 *pid_out = err;
196 return(new);
198 out_shutdown:
199 os_shutdown_socket(socket[0], 1, 1);
200 os_close_file(socket[0]);
201 os_shutdown_socket(socket[1], 1, 1);
202 os_close_file(socket[1]);
203 out_close:
204 os_close_file(new);
205 return(err);
209 * Overrides for Emacs so that we follow Linus's tabbing style.
210 * Emacs will notice this stuff at the end of the file and automatically
211 * adjust the settings for this buffer only. This must remain at the end
212 * of the file.
213 * ---------------------------------------------------------------------------
214 * Local variables:
215 * c-file-style: "linux"
216 * End: