Linux 2.6.17.7
[linux/fpc-iii.git] / arch / um / drivers / slip_user.c
blob89fbec185cc16ec10d2d8df49410cf8bb32080a1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stddef.h>
5 #include <sched.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <sys/termios.h>
9 #include <sys/wait.h>
10 #include <sys/signal.h>
11 #include "user_util.h"
12 #include "kern_util.h"
13 #include "user.h"
14 #include "net_user.h"
15 #include "slip.h"
16 #include "slip_common.h"
17 #include "os.h"
19 void slip_user_init(void *data, void *dev)
21 struct slip_data *pri = data;
23 pri->dev = dev;
26 static int set_up_tty(int fd)
28 int i;
29 struct termios tios;
31 if (tcgetattr(fd, &tios) < 0) {
32 printk("could not get initial terminal attributes\n");
33 return(-1);
36 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
37 tios.c_iflag = IGNBRK | IGNPAR;
38 tios.c_oflag = 0;
39 tios.c_lflag = 0;
40 for (i = 0; i < NCCS; i++)
41 tios.c_cc[i] = 0;
42 tios.c_cc[VMIN] = 1;
43 tios.c_cc[VTIME] = 0;
45 cfsetospeed(&tios, B38400);
46 cfsetispeed(&tios, B38400);
48 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
49 printk("failed to set terminal attributes\n");
50 return(-1);
52 return(0);
55 struct slip_pre_exec_data {
56 int stdin;
57 int stdout;
58 int close_me;
61 static void slip_pre_exec(void *arg)
63 struct slip_pre_exec_data *data = arg;
65 if(data->stdin >= 0) dup2(data->stdin, 0);
66 dup2(data->stdout, 1);
67 if(data->close_me >= 0) os_close_file(data->close_me);
70 static int slip_tramp(char **argv, int fd)
72 struct slip_pre_exec_data pe_data;
73 char *output;
74 int status, pid, fds[2], err, output_len;
76 err = os_pipe(fds, 1, 0);
77 if(err < 0){
78 printk("slip_tramp : pipe failed, err = %d\n", -err);
79 goto out;
82 err = 0;
83 pe_data.stdin = fd;
84 pe_data.stdout = fds[1];
85 pe_data.close_me = fds[0];
86 err = run_helper(slip_pre_exec, &pe_data, argv, NULL);
87 if(err < 0)
88 goto out_close;
89 pid = err;
91 output_len = page_size();
92 output = um_kmalloc(output_len);
93 if(output == NULL){
94 printk("slip_tramp : failed to allocate output buffer\n");
95 os_kill_process(pid, 1);
96 err = -ENOMEM;
97 goto out_free;
100 os_close_file(fds[1]);
101 read_output(fds[0], output, output_len);
102 printk("%s", output);
104 CATCH_EINTR(err = waitpid(pid, &status, 0));
105 if(err < 0)
106 err = errno;
107 else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
108 printk("'%s' didn't exit with status 0\n", argv[0]);
109 err = -EINVAL;
111 else err = 0;
113 os_close_file(fds[0]);
115 out_free:
116 kfree(output);
117 return err;
119 out_close:
120 os_close_file(fds[0]);
121 os_close_file(fds[1]);
122 out:
123 return err;
126 static int slip_open(void *data)
128 struct slip_data *pri = data;
129 char version_buf[sizeof("nnnnn\0")];
130 char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
131 char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
132 NULL };
133 int sfd, mfd, err;
135 err = get_pty();
136 if(err < 0){
137 printk("slip-open : Failed to open pty, err = %d\n", -err);
138 goto out;
140 mfd = err;
142 err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0);
143 if(err < 0){
144 printk("Couldn't open tty for slip line, err = %d\n", -err);
145 goto out_close;
147 sfd = err;
149 if(set_up_tty(sfd))
150 goto out_close2;
152 pri->slave = sfd;
153 pri->slip.pos = 0;
154 pri->slip.esc = 0;
155 if(pri->gate_addr != NULL){
156 sprintf(version_buf, "%d", UML_NET_VERSION);
157 strcpy(gate_buf, pri->gate_addr);
159 err = slip_tramp(argv, sfd);
161 if(err < 0){
162 printk("slip_tramp failed - err = %d\n", -err);
163 goto out_close2;
165 err = os_get_ifname(pri->slave, pri->name);
166 if(err < 0){
167 printk("get_ifname failed, err = %d\n", -err);
168 goto out_close2;
170 iter_addresses(pri->dev, open_addr, pri->name);
172 else {
173 err = os_set_slip(sfd);
174 if(err < 0){
175 printk("Failed to set slip discipline encapsulation - "
176 "err = %d\n", -err);
177 goto out_close2;
180 return(mfd);
181 out_close2:
182 os_close_file(sfd);
183 out_close:
184 os_close_file(mfd);
185 out:
186 return err;
189 static void slip_close(int fd, void *data)
191 struct slip_data *pri = data;
192 char version_buf[sizeof("nnnnn\0")];
193 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
194 NULL };
195 int err;
197 if(pri->gate_addr != NULL)
198 iter_addresses(pri->dev, close_addr, pri->name);
200 sprintf(version_buf, "%d", UML_NET_VERSION);
202 err = slip_tramp(argv, pri->slave);
204 if(err != 0)
205 printk("slip_tramp failed - errno = %d\n", -err);
206 os_close_file(fd);
207 os_close_file(pri->slave);
208 pri->slave = -1;
211 int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
213 return slip_proto_read(fd, buf, len, &pri->slip);
216 int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
218 return slip_proto_write(fd, buf, len, &pri->slip);
221 static int slip_set_mtu(int mtu, void *data)
223 return(mtu);
226 static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
227 void *data)
229 struct slip_data *pri = data;
231 if(pri->slave < 0) return;
232 open_addr(addr, netmask, pri->name);
235 static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
236 void *data)
238 struct slip_data *pri = data;
240 if(pri->slave < 0) return;
241 close_addr(addr, netmask, pri->name);
244 struct net_user_info slip_user_info = {
245 .init = slip_user_init,
246 .open = slip_open,
247 .close = slip_close,
248 .remove = NULL,
249 .set_mtu = slip_set_mtu,
250 .add_address = slip_add_addr,
251 .delete_address = slip_del_addr,
252 .max_packet = BUF_SIZE